data.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. * initialize privatebin
  201. *
  202. * @access private
  203. * @static
  204. * @return void
  205. */
  206. private static function _init()
  207. {
  208. // Create storage directory if it does not exist.
  209. if (!is_dir(self::$_dir)) mkdir(self::$_dir, 0705);
  210. // Create .htaccess file if it does not exist.
  211. if (!is_file(self::$_dir . '.htaccess'))
  212. {
  213. file_put_contents(
  214. self::$_dir . '.htaccess',
  215. 'Allow from none' . PHP_EOL .
  216. 'Deny from all'. PHP_EOL
  217. );
  218. }
  219. }
  220. /**
  221. * Convert paste id to storage path.
  222. *
  223. * The idea is to creates subdirectories in order to limit the number of files per directory.
  224. * (A high number of files in a single directory can slow things down.)
  225. * eg. "f468483c313401e8" will be stored in "data/f4/68/f468483c313401e8"
  226. * High-trafic websites may want to deepen the directory structure (like Squid does).
  227. *
  228. * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/'
  229. *
  230. * @access private
  231. * @static
  232. * @param string $dataid
  233. * @return void
  234. */
  235. private static function _dataid2path($dataid)
  236. {
  237. return self::$_dir . substr($dataid,0,2) . '/' . substr($dataid,2,2) . '/';
  238. }
  239. /**
  240. * Convert paste id to discussion storage path.
  241. *
  242. * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/e3570978f9e4aa90.discussion/'
  243. *
  244. * @access private
  245. * @static
  246. * @param string $dataid
  247. * @return void
  248. */
  249. private static function _dataid2discussionpath($dataid)
  250. {
  251. return self::_dataid2path($dataid) . $dataid . '.discussion/';
  252. }
  253. }