Filesystem.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. /**
  3. * PrivateBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link https://github.com/PrivateBin/PrivateBin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. * @version 1.1.1
  11. */
  12. namespace PrivateBin\Data;
  13. use PrivateBin\Model\Paste;
  14. use PrivateBin\Persistence\DataStore;
  15. /**
  16. * Filesystem
  17. *
  18. * Model for filesystem data access, implemented as a singleton.
  19. */
  20. class Filesystem extends AbstractData
  21. {
  22. /**
  23. * get instance of singleton
  24. *
  25. * @access public
  26. * @static
  27. * @param array $options
  28. * @return Filesystem
  29. */
  30. public static function getInstance($options = null)
  31. {
  32. // if needed initialize the singleton
  33. if (!(self::$_instance instanceof self)) {
  34. self::$_instance = new self;
  35. }
  36. // if given update the data directory
  37. if (
  38. is_array($options) &&
  39. array_key_exists('dir', $options)
  40. ) {
  41. DataStore::setPath($options['dir']);
  42. }
  43. return self::$_instance;
  44. }
  45. /**
  46. * Create a paste.
  47. *
  48. * @access public
  49. * @param string $pasteid
  50. * @param array $paste
  51. * @return bool
  52. */
  53. public function create($pasteid, $paste)
  54. {
  55. $storagedir = self::_dataid2path($pasteid);
  56. $file = $storagedir . $pasteid . '.php';
  57. if (is_file($file)) {
  58. return false;
  59. }
  60. if (!is_dir($storagedir)) {
  61. mkdir($storagedir, 0700, true);
  62. }
  63. return DataStore::store($file, $paste);
  64. }
  65. /**
  66. * Read a paste.
  67. *
  68. * @access public
  69. * @param string $pasteid
  70. * @return stdClass|false
  71. */
  72. public function read($pasteid)
  73. {
  74. if (!$this->exists($pasteid)) {
  75. return false;
  76. }
  77. $paste = DataStore::get(self::_dataid2path($pasteid) . $pasteid . '.php');
  78. if (property_exists($paste->meta, 'attachment')) {
  79. $paste->attachment = $paste->meta->attachment;
  80. unset($paste->meta->attachment);
  81. if (property_exists($paste->meta, 'attachmentname')) {
  82. $paste->attachmentname = $paste->meta->attachmentname;
  83. unset($paste->meta->attachmentname);
  84. }
  85. }
  86. return $paste;
  87. }
  88. /**
  89. * Delete a paste and its discussion.
  90. *
  91. * @access public
  92. * @param string $pasteid
  93. */
  94. public function delete($pasteid)
  95. {
  96. $pastedir = self::_dataid2path($pasteid);
  97. if (is_dir($pastedir)) {
  98. // Delete the paste itself.
  99. if (is_file($pastedir . $pasteid . '.php')) {
  100. unlink($pastedir . $pasteid . '.php');
  101. }
  102. // Delete discussion if it exists.
  103. $discdir = self::_dataid2discussionpath($pasteid);
  104. if (is_dir($discdir)) {
  105. // Delete all files in discussion directory
  106. $dir = dir($discdir);
  107. while (false !== ($filename = $dir->read())) {
  108. if (is_file($discdir . $filename)) {
  109. unlink($discdir . $filename);
  110. }
  111. }
  112. $dir->close();
  113. rmdir($discdir);
  114. }
  115. }
  116. }
  117. /**
  118. * Test if a paste exists.
  119. *
  120. * @access public
  121. * @param string $pasteid
  122. * @return bool
  123. */
  124. public function exists($pasteid)
  125. {
  126. $basePath = self::_dataid2path($pasteid) . $pasteid;
  127. $pastePath = $basePath . '.php';
  128. // convert to PHP protected files if needed
  129. if (is_readable($basePath)) {
  130. $context = stream_context_create();
  131. // don't overwrite already converted file
  132. if (!is_file($pastePath)) {
  133. $handle = fopen($basePath, 'r', false, $context);
  134. file_put_contents($pastePath, DataStore::PROTECTION_LINE . PHP_EOL);
  135. file_put_contents($pastePath, $handle, FILE_APPEND);
  136. fclose($handle);
  137. }
  138. unlink($basePath);
  139. // convert comments, too
  140. $discdir = self::_dataid2discussionpath($pasteid);
  141. if (is_dir($discdir)) {
  142. $dir = dir($discdir);
  143. while (false !== ($filename = $dir->read())) {
  144. if (substr($filename, -4) !== '.php' && strlen($filename) >= 16) {
  145. $commentFilename = $discdir . $filename . '.php';
  146. // don't overwrite already converted file
  147. if (!is_file($commentFilename)) {
  148. $handle = fopen($discdir . $filename, 'r', false, $context);
  149. file_put_contents($commentFilename, DataStore::PROTECTION_LINE . PHP_EOL);
  150. file_put_contents($commentFilename, $handle, FILE_APPEND);
  151. fclose($handle);
  152. }
  153. unlink($discdir . $filename);
  154. }
  155. }
  156. $dir->close();
  157. }
  158. }
  159. return is_readable($pastePath);
  160. }
  161. /**
  162. * Create a comment in a paste.
  163. *
  164. * @access public
  165. * @param string $pasteid
  166. * @param string $parentid
  167. * @param string $commentid
  168. * @param array $comment
  169. * @return bool
  170. */
  171. public function createComment($pasteid, $parentid, $commentid, $comment)
  172. {
  173. $storagedir = self::_dataid2discussionpath($pasteid);
  174. $file = $storagedir . $pasteid . '.' . $commentid . '.' . $parentid . '.php';
  175. if (is_file($file)) {
  176. return false;
  177. }
  178. if (!is_dir($storagedir)) {
  179. mkdir($storagedir, 0700, true);
  180. }
  181. return DataStore::store($file, $comment);
  182. }
  183. /**
  184. * Read all comments of paste.
  185. *
  186. * @access public
  187. * @param string $pasteid
  188. * @return array
  189. */
  190. public function readComments($pasteid)
  191. {
  192. $comments = array();
  193. $discdir = self::_dataid2discussionpath($pasteid);
  194. if (is_dir($discdir)) {
  195. $dir = dir($discdir);
  196. while (false !== ($filename = $dir->read())) {
  197. // Filename is in the form pasteid.commentid.parentid.php:
  198. // - pasteid is the paste this reply belongs to.
  199. // - commentid is the comment identifier itself.
  200. // - parentid is the comment this comment replies to (It can be pasteid)
  201. if (is_file($discdir . $filename)) {
  202. $comment = DataStore::get($discdir . $filename);
  203. $items = explode('.', $filename);
  204. // Add some meta information not contained in file.
  205. $comment->id = $items[1];
  206. $comment->parentid = $items[2];
  207. // Store in array
  208. $key = $this->getOpenSlot($comments, (int) $comment->meta->postdate);
  209. $comments[$key] = $comment;
  210. }
  211. }
  212. $dir->close();
  213. // Sort comments by date, oldest first.
  214. ksort($comments);
  215. }
  216. return $comments;
  217. }
  218. /**
  219. * Test if a comment exists.
  220. *
  221. * @access public
  222. * @param string $pasteid
  223. * @param string $parentid
  224. * @param string $commentid
  225. * @return bool
  226. */
  227. public function existsComment($pasteid, $parentid, $commentid)
  228. {
  229. return is_file(
  230. self::_dataid2discussionpath($pasteid) .
  231. $pasteid . '.' . $commentid . '.' . $parentid . '.php'
  232. );
  233. }
  234. /**
  235. * Returns up to batch size number of paste ids that have expired
  236. *
  237. * @access private
  238. * @param int $batchsize
  239. * @return array
  240. */
  241. protected function _getExpiredPastes($batchsize)
  242. {
  243. $pastes = array();
  244. $mainpath = DataStore::getPath();
  245. $firstLevel = array_filter(
  246. scandir($mainpath),
  247. 'self::_isFirstLevelDir'
  248. );
  249. if (count($firstLevel) > 0) {
  250. // try at most 10 times the $batchsize pastes before giving up
  251. for ($i = 0, $max = $batchsize * 10; $i < $max; ++$i) {
  252. $firstKey = array_rand($firstLevel);
  253. $secondLevel = array_filter(
  254. scandir($mainpath . DIRECTORY_SEPARATOR . $firstLevel[$firstKey]),
  255. 'self::_isSecondLevelDir'
  256. );
  257. // skip this folder in the next checks if it is empty
  258. if (count($secondLevel) == 0) {
  259. unset($firstLevel[$firstKey]);
  260. continue;
  261. }
  262. $secondKey = array_rand($secondLevel);
  263. $path = $mainpath . DIRECTORY_SEPARATOR .
  264. $firstLevel[$firstKey] . DIRECTORY_SEPARATOR .
  265. $secondLevel[$secondKey];
  266. if (!is_dir($path)) {
  267. continue;
  268. }
  269. $thirdLevel = array_filter(
  270. array_map(
  271. function ($filename) {
  272. return strlen($filename) >= 20 ?
  273. substr($filename, 0, -4) :
  274. $filename;
  275. },
  276. scandir($path)
  277. ),
  278. 'PrivateBin\\Model\\Paste::isValidId'
  279. );
  280. if (count($thirdLevel) == 0) {
  281. continue;
  282. }
  283. $thirdKey = array_rand($thirdLevel);
  284. $pasteid = $thirdLevel[$thirdKey];
  285. if (in_array($pasteid, $pastes)) {
  286. continue;
  287. }
  288. if ($this->exists($pasteid)) {
  289. $data = $this->read($pasteid);
  290. if (
  291. property_exists($data->meta, 'expire_date') &&
  292. $data->meta->expire_date < time()
  293. ) {
  294. $pastes[] = $pasteid;
  295. if (count($pastes) >= $batchsize) {
  296. break;
  297. }
  298. }
  299. }
  300. }
  301. }
  302. return $pastes;
  303. }
  304. /**
  305. * Convert paste id to storage path.
  306. *
  307. * The idea is to creates subdirectories in order to limit the number of files per directory.
  308. * (A high number of files in a single directory can slow things down.)
  309. * eg. "f468483c313401e8" will be stored in "data/f4/68/f468483c313401e8"
  310. * High-trafic websites may want to deepen the directory structure (like Squid does).
  311. *
  312. * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/'
  313. *
  314. * @access private
  315. * @static
  316. * @param string $dataid
  317. * @return string
  318. */
  319. private static function _dataid2path($dataid)
  320. {
  321. return DataStore::getPath(
  322. substr($dataid, 0, 2) . DIRECTORY_SEPARATOR .
  323. substr($dataid, 2, 2) . DIRECTORY_SEPARATOR
  324. );
  325. }
  326. /**
  327. * Convert paste id to discussion storage path.
  328. *
  329. * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/e3570978f9e4aa90.discussion/'
  330. *
  331. * @access private
  332. * @static
  333. * @param string $dataid
  334. * @return string
  335. */
  336. private static function _dataid2discussionpath($dataid)
  337. {
  338. return self::_dataid2path($dataid) . $dataid .
  339. '.discussion' . DIRECTORY_SEPARATOR;
  340. }
  341. /**
  342. * Check that the given element is a valid first level directory.
  343. *
  344. * @access private
  345. * @static
  346. * @param string $element
  347. * @return bool
  348. */
  349. private static function _isFirstLevelDir($element)
  350. {
  351. return self::_isSecondLevelDir($element) &&
  352. is_dir(DataStore::getPath($element));
  353. }
  354. /**
  355. * Check that the given element is a valid second level directory.
  356. *
  357. * @access private
  358. * @static
  359. * @param string $element
  360. * @return bool
  361. */
  362. private static function _isSecondLevelDir($element)
  363. {
  364. return (bool) preg_match('/^[a-f0-9]{2}$/', $element);
  365. }
  366. }