data.php 10 KB

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