1
0

Filesystem.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. * first line in paste or comment files, to protect their contents from browsing exposed data directories
  24. *
  25. * @const string
  26. */
  27. const PROTECTION_LINE = '<?php http_response_code(403); /*';
  28. /**
  29. * line in generated .htaccess files, to protect exposed directories from being browsable on apache web servers
  30. *
  31. * @const string
  32. */
  33. const HTACCESS_LINE = 'Require all denied';
  34. /**
  35. * path in which to persist something
  36. *
  37. * @access private
  38. * @var string
  39. */
  40. private $_path = 'data';
  41. /**
  42. * instantiates a new Filesystem data backend
  43. *
  44. * @access public
  45. * @param array $options
  46. * @return
  47. */
  48. public function __construct(array $options)
  49. {
  50. // if given update the data directory
  51. if (
  52. is_array($options) &&
  53. array_key_exists('dir', $options)
  54. ) {
  55. $this->_path = $options['dir'];
  56. }
  57. }
  58. /**
  59. * Create a paste.
  60. *
  61. * @access public
  62. * @param string $pasteid
  63. * @param array $paste
  64. * @return bool
  65. */
  66. public function create($pasteid, array $paste)
  67. {
  68. $storagedir = $this->_dataid2path($pasteid);
  69. $file = $storagedir . $pasteid . '.php';
  70. if (is_file($file)) {
  71. return false;
  72. }
  73. if (!is_dir($storagedir)) {
  74. mkdir($storagedir, 0700, true);
  75. }
  76. return $this->_store($file, $paste);
  77. }
  78. /**
  79. * Read a paste.
  80. *
  81. * @access public
  82. * @param string $pasteid
  83. * @return array|false
  84. */
  85. public function read($pasteid)
  86. {
  87. if (
  88. !$this->exists($pasteid) ||
  89. !$paste = $this->_get($this->_dataid2path($pasteid) . $pasteid . '.php')
  90. ) {
  91. return false;
  92. }
  93. return self::upgradePreV1Format($paste);
  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 = $this->_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 = $this->_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 = $this->_dataid2path($pasteid) . $pasteid;
  134. $pastePath = $basePath . '.php';
  135. // convert to PHP protected files if needed
  136. if (is_readable($basePath)) {
  137. $this->_prependRename($basePath, $pastePath);
  138. // convert comments, too
  139. $discdir = $this->_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. $this->_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 = $this->_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 $this->_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 = $this->_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 = $this->_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. $this->_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 $this->_storeString(
  240. $this->_path . DIRECTORY_SEPARATOR . 'purge_limiter.php',
  241. '<?php' . PHP_EOL . '$GLOBALS[\'purge_limiter\'] = ' . $value . ';'
  242. );
  243. case 'salt':
  244. return $this->_storeString(
  245. $this->_path . DIRECTORY_SEPARATOR . 'salt.php',
  246. '<?php # |' . $value . '|'
  247. );
  248. case 'traffic_limiter':
  249. $this->_last_cache[$key] = $value;
  250. return $this->_storeString(
  251. $this->_path . DIRECTORY_SEPARATOR . 'traffic_limiter.php',
  252. '<?php' . PHP_EOL . '$GLOBALS[\'traffic_limiter\'] = ' . var_export($this->_last_cache, true) . ';'
  253. );
  254. }
  255. return false;
  256. }
  257. /**
  258. * Load a value.
  259. *
  260. * @access public
  261. * @param string $namespace
  262. * @param string $key
  263. * @return string
  264. */
  265. public function getValue($namespace, $key = '')
  266. {
  267. switch ($namespace) {
  268. case 'purge_limiter':
  269. $file = $this->_path . DIRECTORY_SEPARATOR . 'purge_limiter.php';
  270. if (is_readable($file)) {
  271. require $file;
  272. return $GLOBALS['purge_limiter'];
  273. }
  274. break;
  275. case 'salt':
  276. $file = $this->_path . DIRECTORY_SEPARATOR . 'salt.php';
  277. if (is_readable($file)) {
  278. $items = explode('|', file_get_contents($file));
  279. if (is_array($items) && count($items) == 3) {
  280. return $items[1];
  281. }
  282. }
  283. break;
  284. case 'traffic_limiter':
  285. $file = $this->_path . DIRECTORY_SEPARATOR . 'traffic_limiter.php';
  286. if (is_readable($file)) {
  287. require $file;
  288. $this->_last_cache = $GLOBALS['traffic_limiter'];
  289. if (array_key_exists($key, $this->_last_cache)) {
  290. return $this->_last_cache[$key];
  291. }
  292. }
  293. break;
  294. }
  295. return '';
  296. }
  297. /**
  298. * get the data
  299. *
  300. * @access public
  301. * @param string $filename
  302. * @return array|false $data
  303. */
  304. private function _get($filename)
  305. {
  306. return Json::decode(
  307. substr(
  308. file_get_contents($filename),
  309. strlen(self::PROTECTION_LINE . PHP_EOL)
  310. )
  311. );
  312. }
  313. /**
  314. * Returns up to batch size number of paste ids that have expired
  315. *
  316. * @access private
  317. * @param int $batchsize
  318. * @return array
  319. */
  320. protected function _getExpiredPastes($batchsize)
  321. {
  322. $pastes = array();
  323. $files = $this->_getPasteIterator();
  324. $count = 0;
  325. $time = time();
  326. foreach ($files as $file) {
  327. if ($file->isDir()) {
  328. continue;
  329. }
  330. $pasteid = $file->getBasename('.php');
  331. if ($this->exists($pasteid)) {
  332. $data = $this->read($pasteid);
  333. if (
  334. array_key_exists('expire_date', $data['meta']) &&
  335. $data['meta']['expire_date'] < $time
  336. ) {
  337. $pastes[] = $pasteid;
  338. ++$count;
  339. if ($count >= $batchsize) {
  340. break;
  341. }
  342. }
  343. }
  344. }
  345. return $pastes;
  346. }
  347. /**
  348. * @inheritDoc
  349. */
  350. public function getAllPastes()
  351. {
  352. $pastes = array();
  353. $files = $this->_getPasteIterator();
  354. foreach ($files as $file) {
  355. if ($file->isFile()) {
  356. $pastes[] = $file->getBasename('.php');
  357. }
  358. }
  359. return $pastes;
  360. }
  361. /**
  362. * Convert paste id to storage path.
  363. *
  364. * The idea is to creates subdirectories in order to limit the number of files per directory.
  365. * (A high number of files in a single directory can slow things down.)
  366. * eg. "f468483c313401e8" will be stored in "data/f4/68/f468483c313401e8"
  367. * High-trafic websites may want to deepen the directory structure (like Squid does).
  368. *
  369. * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/'
  370. *
  371. * @access private
  372. * @param string $dataid
  373. * @return string
  374. */
  375. private function _dataid2path($dataid)
  376. {
  377. return $this->_path . DIRECTORY_SEPARATOR .
  378. substr($dataid, 0, 2) . DIRECTORY_SEPARATOR .
  379. substr($dataid, 2, 2) . DIRECTORY_SEPARATOR;
  380. }
  381. /**
  382. * Convert paste id to discussion storage path.
  383. *
  384. * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/e3570978f9e4aa90.discussion/'
  385. *
  386. * @access private
  387. * @param string $dataid
  388. * @return string
  389. */
  390. private function _dataid2discussionpath($dataid)
  391. {
  392. return $this->_dataid2path($dataid) . $dataid .
  393. '.discussion' . DIRECTORY_SEPARATOR;
  394. }
  395. /**
  396. * Get an iterator matching paste files.
  397. *
  398. * @access private
  399. * @return \GlobIterator
  400. */
  401. private function _getPasteIterator()
  402. {
  403. return new \GlobIterator($this->_path . DIRECTORY_SEPARATOR .
  404. '[a-f0-9][a-f0-9]' . DIRECTORY_SEPARATOR .
  405. '[a-f0-9][a-f0-9]' . DIRECTORY_SEPARATOR .
  406. '[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]' .
  407. '[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]*');
  408. // need to return both files with and without .php suffix, so they can
  409. // be hardened by _prependRename(), which is hooked into exists()
  410. }
  411. /**
  412. * store the data
  413. *
  414. * @access public
  415. * @param string $filename
  416. * @param array $data
  417. * @return bool
  418. */
  419. private function _store($filename, array $data)
  420. {
  421. try {
  422. return $this->_storeString(
  423. $filename,
  424. self::PROTECTION_LINE . PHP_EOL . Json::encode($data)
  425. );
  426. } catch (Exception $e) {
  427. return false;
  428. }
  429. }
  430. /**
  431. * store a string
  432. *
  433. * @access public
  434. * @param string $filename
  435. * @param string $data
  436. * @return bool
  437. */
  438. private function _storeString($filename, $data)
  439. {
  440. // Create storage directory if it does not exist.
  441. if (!is_dir($this->_path)) {
  442. if (!@mkdir($this->_path, 0700)) {
  443. return false;
  444. }
  445. }
  446. $file = $this->_path . DIRECTORY_SEPARATOR . '.htaccess';
  447. if (!is_file($file)) {
  448. $writtenBytes = 0;
  449. if ($fileCreated = @touch($file)) {
  450. $writtenBytes = @file_put_contents(
  451. $file,
  452. self::HTACCESS_LINE . PHP_EOL,
  453. LOCK_EX
  454. );
  455. }
  456. if (
  457. $fileCreated === false ||
  458. $writtenBytes === false ||
  459. $writtenBytes < strlen(self::HTACCESS_LINE . PHP_EOL)
  460. ) {
  461. return false;
  462. }
  463. }
  464. $fileCreated = true;
  465. $writtenBytes = 0;
  466. if (!is_file($filename)) {
  467. $fileCreated = @touch($filename);
  468. }
  469. if ($fileCreated) {
  470. $writtenBytes = @file_put_contents($filename, $data, LOCK_EX);
  471. }
  472. if ($fileCreated === false || $writtenBytes === false || $writtenBytes < strlen($data)) {
  473. return false;
  474. }
  475. @chmod($filename, 0640); // protect file from access by other users on the host
  476. return true;
  477. }
  478. /**
  479. * rename a file, prepending the protection line at the beginning
  480. *
  481. * @access public
  482. * @param string $srcFile
  483. * @param string $destFile
  484. * @return void
  485. */
  486. private function _prependRename($srcFile, $destFile)
  487. {
  488. // don't overwrite already converted file
  489. if (!is_readable($destFile)) {
  490. $handle = fopen($srcFile, 'r', false, stream_context_create());
  491. file_put_contents($destFile, self::PROTECTION_LINE . PHP_EOL);
  492. file_put_contents($destFile, $handle, FILE_APPEND);
  493. fclose($handle);
  494. }
  495. unlink($srcFile);
  496. }
  497. }