data.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 http://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] . '/' . $secondLevel[$secondKey];
  231. if (!is_dir($path)) continue;
  232. $thirdLevel = array_filter(
  233. scandir($path),
  234. array('model_paste', 'isValidId')
  235. );
  236. if (count($thirdLevel) == 0) continue;
  237. $thirdKey = array_rand($thirdLevel);
  238. $pasteid = $thirdLevel[$thirdKey];
  239. if (in_array($pasteid, $pastes)) continue;
  240. if ($this->exists($pasteid))
  241. {
  242. $data = $this->read($pasteid);
  243. if (
  244. property_exists($data->meta, 'expire_date') &&
  245. $data->meta->expire_date < time()
  246. )
  247. {
  248. $pastes[] = $pasteid;
  249. if (count($pastes) >= $batchsize) break;
  250. }
  251. }
  252. }
  253. }
  254. return $pastes;
  255. }
  256. /**
  257. * initialize privatebin
  258. *
  259. * @access private
  260. * @static
  261. * @return void
  262. */
  263. private static function _init()
  264. {
  265. // Create storage directory if it does not exist.
  266. if (!is_dir(self::$_dir)) mkdir(self::$_dir, 0705);
  267. // Create .htaccess file if it does not exist.
  268. if (!is_file(self::$_dir . '.htaccess'))
  269. {
  270. file_put_contents(
  271. self::$_dir . '.htaccess',
  272. 'Allow from none' . PHP_EOL .
  273. 'Deny from all'. PHP_EOL
  274. );
  275. }
  276. }
  277. /**
  278. * Convert paste id to storage path.
  279. *
  280. * The idea is to creates subdirectories in order to limit the number of files per directory.
  281. * (A high number of files in a single directory can slow things down.)
  282. * eg. "f468483c313401e8" will be stored in "data/f4/68/f468483c313401e8"
  283. * High-trafic websites may want to deepen the directory structure (like Squid does).
  284. *
  285. * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/'
  286. *
  287. * @access private
  288. * @static
  289. * @param string $dataid
  290. * @return void
  291. */
  292. private static function _dataid2path($dataid)
  293. {
  294. return self::$_dir . substr($dataid,0,2) . '/' . substr($dataid,2,2) . '/';
  295. }
  296. /**
  297. * Convert paste id to discussion storage path.
  298. *
  299. * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/e3570978f9e4aa90.discussion/'
  300. *
  301. * @access private
  302. * @static
  303. * @param string $dataid
  304. * @return void
  305. */
  306. private static function _dataid2discussionpath($dataid)
  307. {
  308. return self::_dataid2path($dataid) . $dataid . '.discussion/';
  309. }
  310. /**
  311. * Check that the given element is a valid first level directory.
  312. *
  313. * @access private
  314. * @static
  315. * @param string $element
  316. * @return bool
  317. */
  318. private static function _isFirstLevelDir($element)
  319. {
  320. return self::_isSecondLevelDir($element) && is_dir(self::$_dir . '/' . $element);
  321. }
  322. /**
  323. * Check that the given element is a valid second level directory.
  324. *
  325. * @access private
  326. * @static
  327. * @param string $element
  328. * @return bool
  329. */
  330. private static function _isSecondLevelDir($element)
  331. {
  332. return (bool) preg_match('/^[a-f0-9]{2}$/', $element);
  333. }
  334. }