Filesystem.php 11 KB

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