Filesystem.php 10 KB

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