data.php 10 KB

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