Filesystem.php 16 KB

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