1
0

Filesystem.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 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. * @return void
  96. */
  97. public function delete($pasteid)
  98. {
  99. $pastedir = self::_dataid2path($pasteid);
  100. if (is_dir($pastedir)) {
  101. // Delete the paste itself.
  102. if (is_file($pastedir . $pasteid)) {
  103. unlink($pastedir . $pasteid);
  104. }
  105. // Delete discussion if it exists.
  106. $discdir = self::_dataid2discussionpath($pasteid);
  107. if (is_dir($discdir)) {
  108. // Delete all files in discussion directory
  109. $dir = dir($discdir);
  110. while (false !== ($filename = $dir->read())) {
  111. if (is_file($discdir . $filename)) {
  112. unlink($discdir . $filename);
  113. }
  114. }
  115. $dir->close();
  116. rmdir($discdir);
  117. }
  118. }
  119. }
  120. /**
  121. * Test if a paste exists.
  122. *
  123. * @access public
  124. * @param string $pasteid
  125. * @return bool
  126. */
  127. public function exists($pasteid)
  128. {
  129. return is_file(self::_dataid2path($pasteid) . $pasteid);
  130. }
  131. /**
  132. * Create a comment in a paste.
  133. *
  134. * @access public
  135. * @param string $pasteid
  136. * @param string $parentid
  137. * @param string $commentid
  138. * @param array $comment
  139. * @return bool
  140. */
  141. public function createComment($pasteid, $parentid, $commentid, $comment)
  142. {
  143. $storagedir = self::_dataid2discussionpath($pasteid);
  144. $file = $storagedir . $pasteid . '.' . $commentid . '.' . $parentid;
  145. if (is_file($file)) {
  146. return false;
  147. }
  148. if (!is_dir($storagedir)) {
  149. mkdir($storagedir, 0700, true);
  150. }
  151. return DataStore::store($file, $comment);
  152. }
  153. /**
  154. * Read all comments of paste.
  155. *
  156. * @access public
  157. * @param string $pasteid
  158. * @return array
  159. */
  160. public function readComments($pasteid)
  161. {
  162. $comments = array();
  163. $discdir = self::_dataid2discussionpath($pasteid);
  164. if (is_dir($discdir)) {
  165. // Delete all files in discussion directory
  166. $dir = dir($discdir);
  167. while (false !== ($filename = $dir->read())) {
  168. // Filename is in the form pasteid.commentid.parentid:
  169. // - pasteid is the paste this reply belongs to.
  170. // - commentid is the comment identifier itself.
  171. // - parentid is the comment this comment replies to (It can be pasteid)
  172. if (is_file($discdir . $filename)) {
  173. $comment = json_decode(file_get_contents($discdir . $filename));
  174. $items = explode('.', $filename);
  175. // Add some meta information not contained in file.
  176. $comment->id = $items[1];
  177. $comment->parentid = $items[2];
  178. // Store in array
  179. $key = $this->getOpenSlot($comments, (int) $comment->meta->postdate);
  180. $comments[$key] = $comment;
  181. }
  182. }
  183. $dir->close();
  184. // Sort comments by date, oldest first.
  185. ksort($comments);
  186. }
  187. return $comments;
  188. }
  189. /**
  190. * Test if a comment exists.
  191. *
  192. * @access public
  193. * @param string $pasteid
  194. * @param string $parentid
  195. * @param string $commentid
  196. * @return bool
  197. */
  198. public function existsComment($pasteid, $parentid, $commentid)
  199. {
  200. return is_file(
  201. self::_dataid2discussionpath($pasteid) .
  202. $pasteid . '.' . $commentid . '.' . $parentid
  203. );
  204. }
  205. /**
  206. * Returns up to batch size number of paste ids that have expired
  207. *
  208. * @access private
  209. * @param int $batchsize
  210. * @return array
  211. */
  212. protected function _getExpiredPastes($batchsize)
  213. {
  214. $pastes = array();
  215. $mainpath = DataStore::getPath();
  216. $firstLevel = array_filter(
  217. scandir($mainpath),
  218. 'self::_isFirstLevelDir'
  219. );
  220. if (count($firstLevel) > 0) {
  221. // try at most 10 times the $batchsize pastes before giving up
  222. for ($i = 0, $max = $batchsize * 10; $i < $max; ++$i) {
  223. $firstKey = array_rand($firstLevel);
  224. $secondLevel = array_filter(
  225. scandir($mainpath . DIRECTORY_SEPARATOR . $firstLevel[$firstKey]),
  226. 'self::_isSecondLevelDir'
  227. );
  228. // skip this folder in the next checks if it is empty
  229. if (count($secondLevel) == 0) {
  230. unset($firstLevel[$firstKey]);
  231. continue;
  232. }
  233. $secondKey = array_rand($secondLevel);
  234. $path = $mainpath . DIRECTORY_SEPARATOR .
  235. $firstLevel[$firstKey] . DIRECTORY_SEPARATOR .
  236. $secondLevel[$secondKey];
  237. if (!is_dir($path)) {
  238. continue;
  239. }
  240. $thirdLevel = array_filter(
  241. scandir($path),
  242. 'PrivateBin\\Model\\Paste::isValidId'
  243. );
  244. if (count($thirdLevel) == 0) {
  245. continue;
  246. }
  247. $thirdKey = array_rand($thirdLevel);
  248. $pasteid = $thirdLevel[$thirdKey];
  249. if (in_array($pasteid, $pastes)) {
  250. continue;
  251. }
  252. if ($this->exists($pasteid)) {
  253. $data = $this->read($pasteid);
  254. if (
  255. property_exists($data->meta, 'expire_date') &&
  256. $data->meta->expire_date < time()
  257. ) {
  258. $pastes[] = $pasteid;
  259. if (count($pastes) >= $batchsize) {
  260. break;
  261. }
  262. }
  263. }
  264. }
  265. }
  266. return $pastes;
  267. }
  268. /**
  269. * Convert paste id to storage path.
  270. *
  271. * The idea is to creates subdirectories in order to limit the number of files per directory.
  272. * (A high number of files in a single directory can slow things down.)
  273. * eg. "f468483c313401e8" will be stored in "data/f4/68/f468483c313401e8"
  274. * High-trafic websites may want to deepen the directory structure (like Squid does).
  275. *
  276. * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/'
  277. *
  278. * @access private
  279. * @static
  280. * @param string $dataid
  281. * @return string
  282. */
  283. private static function _dataid2path($dataid)
  284. {
  285. return DataStore::getPath(
  286. substr($dataid, 0, 2) . DIRECTORY_SEPARATOR .
  287. substr($dataid, 2, 2) . DIRECTORY_SEPARATOR
  288. );
  289. }
  290. /**
  291. * Convert paste id to discussion storage path.
  292. *
  293. * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/e3570978f9e4aa90.discussion/'
  294. *
  295. * @access private
  296. * @static
  297. * @param string $dataid
  298. * @return string
  299. */
  300. private static function _dataid2discussionpath($dataid)
  301. {
  302. return self::_dataid2path($dataid) . $dataid .
  303. '.discussion' . DIRECTORY_SEPARATOR;
  304. }
  305. /**
  306. * Check that the given element is a valid first level directory.
  307. *
  308. * @access private
  309. * @static
  310. * @param string $element
  311. * @return bool
  312. */
  313. private static function _isFirstLevelDir($element)
  314. {
  315. return self::_isSecondLevelDir($element) &&
  316. is_dir(DataStore::getPath($element));
  317. }
  318. /**
  319. * Check that the given element is a valid second level directory.
  320. *
  321. * @access private
  322. * @static
  323. * @param string $element
  324. * @return bool
  325. */
  326. private static function _isSecondLevelDir($element)
  327. {
  328. return (bool) preg_match('/^[a-f0-9]{2}$/', $element);
  329. }
  330. }