Filesystem.php 11 KB

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