Filesystem.php 11 KB

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