Filesystem.php 11 KB

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