Filesystem.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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.3.5
  11. */
  12. namespace PrivateBin\Data;
  13. use Exception;
  14. use PrivateBin\Json;
  15. /**
  16. * Filesystem
  17. *
  18. * Model for filesystem data access, implemented as a singleton.
  19. */
  20. class Filesystem extends AbstractData
  21. {
  22. /**
  23. * first line in file, to protect its contents
  24. *
  25. * @const string
  26. */
  27. const PROTECTION_LINE = '<?php http_response_code(403); /*';
  28. /**
  29. * path in which to persist something
  30. *
  31. * @access private
  32. * @static
  33. * @var string
  34. */
  35. private static $_path = 'data';
  36. /**
  37. * get instance of singleton
  38. *
  39. * @access public
  40. * @static
  41. * @param array $options
  42. * @return Filesystem
  43. */
  44. public static function getInstance(array $options)
  45. {
  46. // if needed initialize the singleton
  47. if (!(self::$_instance instanceof self)) {
  48. self::$_instance = new self;
  49. }
  50. // if given update the data directory
  51. if (
  52. is_array($options) &&
  53. array_key_exists('dir', $options)
  54. ) {
  55. self::$_path = $options['dir'];
  56. }
  57. return self::$_instance;
  58. }
  59. /**
  60. * Create a paste.
  61. *
  62. * @access public
  63. * @param string $pasteid
  64. * @param array $paste
  65. * @return bool
  66. */
  67. public function create($pasteid, array $paste)
  68. {
  69. $storagedir = self::_dataid2path($pasteid);
  70. $file = $storagedir . $pasteid . '.php';
  71. if (is_file($file)) {
  72. return false;
  73. }
  74. if (!is_dir($storagedir)) {
  75. mkdir($storagedir, 0700, true);
  76. }
  77. return self::_store($file, $paste);
  78. }
  79. /**
  80. * Read a paste.
  81. *
  82. * @access public
  83. * @param string $pasteid
  84. * @return array|false
  85. */
  86. public function read($pasteid)
  87. {
  88. if (!$this->exists($pasteid)) {
  89. return false;
  90. }
  91. return self::upgradePreV1Format(
  92. self::_get(self::_dataid2path($pasteid) . $pasteid . '.php')
  93. );
  94. }
  95. /**
  96. * Delete a paste and its discussion.
  97. *
  98. * @access public
  99. * @param string $pasteid
  100. */
  101. public function delete($pasteid)
  102. {
  103. $pastedir = self::_dataid2path($pasteid);
  104. if (is_dir($pastedir)) {
  105. // Delete the paste itself.
  106. if (is_file($pastedir . $pasteid . '.php')) {
  107. unlink($pastedir . $pasteid . '.php');
  108. }
  109. // Delete discussion if it exists.
  110. $discdir = self::_dataid2discussionpath($pasteid);
  111. if (is_dir($discdir)) {
  112. // Delete all files in discussion directory
  113. $dir = dir($discdir);
  114. while (false !== ($filename = $dir->read())) {
  115. if (is_file($discdir . $filename)) {
  116. unlink($discdir . $filename);
  117. }
  118. }
  119. $dir->close();
  120. rmdir($discdir);
  121. }
  122. }
  123. }
  124. /**
  125. * Test if a paste exists.
  126. *
  127. * @access public
  128. * @param string $pasteid
  129. * @return bool
  130. */
  131. public function exists($pasteid)
  132. {
  133. $basePath = self::_dataid2path($pasteid) . $pasteid;
  134. $pastePath = $basePath . '.php';
  135. // convert to PHP protected files if needed
  136. if (is_readable($basePath)) {
  137. self::_prependRename($basePath, $pastePath);
  138. // convert comments, too
  139. $discdir = self::_dataid2discussionpath($pasteid);
  140. if (is_dir($discdir)) {
  141. $dir = dir($discdir);
  142. while (false !== ($filename = $dir->read())) {
  143. if (substr($filename, -4) !== '.php' && strlen($filename) >= 16) {
  144. $commentFilename = $discdir . $filename . '.php';
  145. self::_prependRename($discdir . $filename, $commentFilename);
  146. }
  147. }
  148. $dir->close();
  149. }
  150. }
  151. return is_readable($pastePath);
  152. }
  153. /**
  154. * Create a comment in a paste.
  155. *
  156. * @access public
  157. * @param string $pasteid
  158. * @param string $parentid
  159. * @param string $commentid
  160. * @param array $comment
  161. * @return bool
  162. */
  163. public function createComment($pasteid, $parentid, $commentid, array $comment)
  164. {
  165. $storagedir = self::_dataid2discussionpath($pasteid);
  166. $file = $storagedir . $pasteid . '.' . $commentid . '.' . $parentid . '.php';
  167. if (is_file($file)) {
  168. return false;
  169. }
  170. if (!is_dir($storagedir)) {
  171. mkdir($storagedir, 0700, true);
  172. }
  173. return self::_store($file, $comment);
  174. }
  175. /**
  176. * Read all comments of paste.
  177. *
  178. * @access public
  179. * @param string $pasteid
  180. * @return array
  181. */
  182. public function readComments($pasteid)
  183. {
  184. $comments = array();
  185. $discdir = self::_dataid2discussionpath($pasteid);
  186. if (is_dir($discdir)) {
  187. $dir = dir($discdir);
  188. while (false !== ($filename = $dir->read())) {
  189. // Filename is in the form pasteid.commentid.parentid.php:
  190. // - pasteid is the paste this reply belongs to.
  191. // - commentid is the comment identifier itself.
  192. // - parentid is the comment this comment replies to (It can be pasteid)
  193. if (is_file($discdir . $filename)) {
  194. $comment = self::_get($discdir . $filename);
  195. $items = explode('.', $filename);
  196. // Add some meta information not contained in file.
  197. $comment['id'] = $items[1];
  198. $comment['parentid'] = $items[2];
  199. // Store in array
  200. $key = $this->getOpenSlot($comments, (int) $comment['meta']['created']);
  201. $comments[$key] = $comment;
  202. }
  203. }
  204. $dir->close();
  205. // Sort comments by date, oldest first.
  206. ksort($comments);
  207. }
  208. return $comments;
  209. }
  210. /**
  211. * Test if a comment exists.
  212. *
  213. * @access public
  214. * @param string $pasteid
  215. * @param string $parentid
  216. * @param string $commentid
  217. * @return bool
  218. */
  219. public function existsComment($pasteid, $parentid, $commentid)
  220. {
  221. return is_file(
  222. self::_dataid2discussionpath($pasteid) .
  223. $pasteid . '.' . $commentid . '.' . $parentid . '.php'
  224. );
  225. }
  226. /**
  227. * Save a value.
  228. *
  229. * @access public
  230. * @param string $value
  231. * @param string $namespace
  232. * @param string $key
  233. * @return bool
  234. */
  235. public function setValue($value, $namespace, $key = '')
  236. {
  237. switch ($namespace) {
  238. case 'purge_limiter':
  239. return self::_storeString(
  240. self::$_path . DIRECTORY_SEPARATOR . 'purge_limiter.php',
  241. '<?php' . PHP_EOL . '$GLOBALS[\'purge_limiter\'] = ' . $value . ';'
  242. );
  243. break;
  244. case 'salt':
  245. ;
  246. break;
  247. case 'traffic_limiter':
  248. ;
  249. break;
  250. }
  251. return false;
  252. }
  253. /**
  254. * Load a value.
  255. *
  256. * @access public
  257. * @param string $namespace
  258. * @param string $key
  259. * @return string
  260. */
  261. public function getValue($namespace, $key = '')
  262. {
  263. switch ($namespace) {
  264. case 'purge_limiter':
  265. $file = self::$_path . DIRECTORY_SEPARATOR . 'purge_limiter.php';
  266. if (is_file($file)) {
  267. require $file;
  268. return $GLOBALS['purge_limiter'];
  269. }
  270. break;
  271. case 'salt':
  272. ;
  273. break;
  274. case 'traffic_limiter':
  275. ;
  276. break;
  277. }
  278. return '';
  279. }
  280. /**
  281. * get the data
  282. *
  283. * @access public
  284. * @static
  285. * @param string $filename
  286. * @return array|false $data
  287. */
  288. private static function _get($filename)
  289. {
  290. return Json::decode(
  291. substr(
  292. file_get_contents($filename),
  293. strlen(self::PROTECTION_LINE . PHP_EOL)
  294. )
  295. );
  296. }
  297. /**
  298. * Returns up to batch size number of paste ids that have expired
  299. *
  300. * @access private
  301. * @param int $batchsize
  302. * @return array
  303. */
  304. protected function _getExpiredPastes($batchsize)
  305. {
  306. $pastes = array();
  307. $firstLevel = array_filter(
  308. scandir(self::$_path),
  309. 'self::_isFirstLevelDir'
  310. );
  311. if (count($firstLevel) > 0) {
  312. // try at most 10 times the $batchsize pastes before giving up
  313. for ($i = 0, $max = $batchsize * 10; $i < $max; ++$i) {
  314. $firstKey = array_rand($firstLevel);
  315. $secondLevel = array_filter(
  316. scandir(self::$_path . DIRECTORY_SEPARATOR . $firstLevel[$firstKey]),
  317. 'self::_isSecondLevelDir'
  318. );
  319. // skip this folder in the next checks if it is empty
  320. if (count($secondLevel) == 0) {
  321. unset($firstLevel[$firstKey]);
  322. continue;
  323. }
  324. $secondKey = array_rand($secondLevel);
  325. $path = self::$_path . DIRECTORY_SEPARATOR .
  326. $firstLevel[$firstKey] . DIRECTORY_SEPARATOR .
  327. $secondLevel[$secondKey];
  328. if (!is_dir($path)) {
  329. continue;
  330. }
  331. $thirdLevel = array_filter(
  332. array_map(
  333. function ($filename) {
  334. return strlen($filename) >= 20 ?
  335. substr($filename, 0, -4) :
  336. $filename;
  337. },
  338. scandir($path)
  339. ),
  340. 'PrivateBin\\Model\\Paste::isValidId'
  341. );
  342. if (count($thirdLevel) == 0) {
  343. continue;
  344. }
  345. $thirdKey = array_rand($thirdLevel);
  346. $pasteid = $thirdLevel[$thirdKey];
  347. if (in_array($pasteid, $pastes)) {
  348. continue;
  349. }
  350. if ($this->exists($pasteid)) {
  351. $data = $this->read($pasteid);
  352. if (
  353. array_key_exists('expire_date', $data['meta']) &&
  354. $data['meta']['expire_date'] < time()
  355. ) {
  356. $pastes[] = $pasteid;
  357. if (count($pastes) >= $batchsize) {
  358. break;
  359. }
  360. }
  361. }
  362. }
  363. }
  364. return $pastes;
  365. }
  366. /**
  367. * Convert paste id to storage path.
  368. *
  369. * The idea is to creates subdirectories in order to limit the number of files per directory.
  370. * (A high number of files in a single directory can slow things down.)
  371. * eg. "f468483c313401e8" will be stored in "data/f4/68/f468483c313401e8"
  372. * High-trafic websites may want to deepen the directory structure (like Squid does).
  373. *
  374. * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/'
  375. *
  376. * @access private
  377. * @static
  378. * @param string $dataid
  379. * @return string
  380. */
  381. private static function _dataid2path($dataid)
  382. {
  383. return self::$_path . DIRECTORY_SEPARATOR .
  384. substr($dataid, 0, 2) . DIRECTORY_SEPARATOR .
  385. substr($dataid, 2, 2) . DIRECTORY_SEPARATOR;
  386. }
  387. /**
  388. * Convert paste id to discussion storage path.
  389. *
  390. * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/e3570978f9e4aa90.discussion/'
  391. *
  392. * @access private
  393. * @static
  394. * @param string $dataid
  395. * @return string
  396. */
  397. private static function _dataid2discussionpath($dataid)
  398. {
  399. return self::_dataid2path($dataid) . $dataid .
  400. '.discussion' . DIRECTORY_SEPARATOR;
  401. }
  402. /**
  403. * Check that the given element is a valid first level directory.
  404. *
  405. * @access private
  406. * @static
  407. * @param string $element
  408. * @return bool
  409. */
  410. private static function _isFirstLevelDir($element)
  411. {
  412. return self::_isSecondLevelDir($element) &&
  413. is_dir(self::$_path . DIRECTORY_SEPARATOR . $element);
  414. }
  415. /**
  416. * Check that the given element is a valid second level directory.
  417. *
  418. * @access private
  419. * @static
  420. * @param string $element
  421. * @return bool
  422. */
  423. private static function _isSecondLevelDir($element)
  424. {
  425. return (bool) preg_match('/^[a-f0-9]{2}$/', $element);
  426. }
  427. /**
  428. * store the data
  429. *
  430. * @access public
  431. * @static
  432. * @param string $filename
  433. * @param array $data
  434. * @return bool
  435. */
  436. private static function _store($filename, array $data)
  437. {
  438. try {
  439. return self::_storeString(
  440. $filename,
  441. self::PROTECTION_LINE . PHP_EOL . Json::encode($data)
  442. );
  443. } catch (Exception $e) {
  444. return false;
  445. }
  446. }
  447. /**
  448. * store a string
  449. *
  450. * @access public
  451. * @static
  452. * @param string $filename
  453. * @param string $data
  454. * @return bool
  455. */
  456. private static function _storeString($filename, $data)
  457. {
  458. // Create storage directory if it does not exist.
  459. if (!is_dir(self::$_path)) {
  460. if (!@mkdir(self::$_path, 0700)) {
  461. return false;
  462. }
  463. }
  464. $file = self::$_path . DIRECTORY_SEPARATOR . '.htaccess';
  465. if (!is_file($file)) {
  466. $writtenBytes = 0;
  467. if ($fileCreated = @touch($file)) {
  468. $writtenBytes = @file_put_contents(
  469. $file,
  470. 'Require all denied' . PHP_EOL,
  471. LOCK_EX
  472. );
  473. }
  474. if ($fileCreated === false || $writtenBytes === false || $writtenBytes < 19) {
  475. return false;
  476. }
  477. }
  478. $fileCreated = true;
  479. $writtenBytes = 0;
  480. if (!is_file($filename)) {
  481. $fileCreated = @touch($filename);
  482. }
  483. if ($fileCreated) {
  484. $writtenBytes = @file_put_contents($filename, $data, LOCK_EX);
  485. }
  486. if ($fileCreated === false || $writtenBytes === false || $writtenBytes < strlen($data)) {
  487. return false;
  488. }
  489. @chmod($filename, 0640); // protect file access
  490. return true;
  491. }
  492. /**
  493. * rename a file, prepending the protection line at the beginning
  494. *
  495. * @access public
  496. * @static
  497. * @param string $srcFile
  498. * @param string $destFile
  499. * @return void
  500. */
  501. private static function _prependRename($srcFile, $destFile)
  502. {
  503. // don't overwrite already converted file
  504. if (!is_readable($destFile)) {
  505. $handle = fopen($srcFile, 'r', false, stream_context_create());
  506. file_put_contents($destFile, self::PROTECTION_LINE . PHP_EOL);
  507. file_put_contents($destFile, $handle, FILE_APPEND);
  508. fclose($handle);
  509. }
  510. unlink($srcFile);
  511. }
  512. }