1
0

Filesystem.php 11 KB

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