Filesystem.php 11 KB

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