data.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * ZeroBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link http://sebsauvage.net/wiki/doku.php?id=php:zerobin
  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.15
  11. */
  12. /**
  13. * zerobin_data
  14. *
  15. * Model for data access, implemented as a singleton.
  16. */
  17. class zerobin_data extends zerobin_abstract
  18. {
  19. /*
  20. * @access private
  21. * @static
  22. * @var string directory where data is stored
  23. */
  24. private static $_dir = 'data/';
  25. /**
  26. * get instance of singleton
  27. *
  28. * @access public
  29. * @static
  30. * @return zerobin
  31. */
  32. public static function getInstance($options)
  33. {
  34. // if given update the data directory
  35. if (
  36. is_array($options) &&
  37. array_key_exists('dir', $options)
  38. ) self::$_dir = $options['dir'] . '/';
  39. // if needed initialize the singleton
  40. if(null === parent::$_instance) {
  41. parent::$_instance = new self;
  42. self::_init();
  43. }
  44. return parent::$_instance;
  45. }
  46. /**
  47. * Create a paste.
  48. *
  49. * @access public
  50. * @param string $pasteid
  51. * @param array $paste
  52. * @return int|false
  53. */
  54. public function create($pasteid, $paste)
  55. {
  56. $storagedir = self::_dataid2path($pasteid);
  57. if (is_file($storagedir . $pasteid)) return false;
  58. if (!is_dir($storagedir)) mkdir($storagedir, 0705, true);
  59. return file_put_contents($storagedir . $pasteid, json_encode($paste));
  60. }
  61. /**
  62. * Read a paste.
  63. *
  64. * @access public
  65. * @param string $pasteid
  66. * @return string
  67. */
  68. public function read($pasteid)
  69. {
  70. if(!$this->exists($pasteid)) return json_decode(
  71. '{"data":"","meta":{"burnafterreading":true,"postdate":0}}'
  72. );
  73. return json_decode(
  74. file_get_contents(self::_dataid2path($pasteid) . $pasteid)
  75. );
  76. }
  77. /**
  78. * Delete a paste and its discussion.
  79. *
  80. * @access public
  81. * @param string $pasteid
  82. * @return void
  83. */
  84. public function delete($pasteid)
  85. {
  86. // Delete the paste itself.
  87. unlink(self::_dataid2path($pasteid) . $pasteid);
  88. // Delete discussion if it exists.
  89. $discdir = self::_dataid2discussionpath($pasteid);
  90. if (is_dir($discdir))
  91. {
  92. // Delete all files in discussion directory
  93. $dir = dir($discdir);
  94. while (false !== ($filename = $dir->read()))
  95. {
  96. if (is_file($discdir.$filename)) unlink($discdir.$filename);
  97. }
  98. $dir->close();
  99. // Delete the discussion directory.
  100. rmdir($discdir);
  101. }
  102. }
  103. /**
  104. * Test if a paste exists.
  105. *
  106. * @access public
  107. * @param string $dataid
  108. * @return void
  109. */
  110. public function exists($pasteid)
  111. {
  112. return is_file(self::_dataid2path($pasteid) . $pasteid);
  113. }
  114. /**
  115. * Create a comment in a paste.
  116. *
  117. * @access public
  118. * @param string $pasteid
  119. * @param string $parentid
  120. * @param string $commentid
  121. * @param array $comment
  122. * @return int|false
  123. */
  124. public function createComment($pasteid, $parentid, $commentid, $comment)
  125. {
  126. $storagedir = self::_dataid2discussionpath($pasteid);
  127. $filename = $pasteid . '.' . $commentid . '.' . $parentid;
  128. if (is_file($storagedir . $filename)) return false;
  129. if (!is_dir($storagedir)) mkdir($storagedir, 0705, true);
  130. return file_put_contents($storagedir . $filename, json_encode($comment));
  131. }
  132. /**
  133. * Read all comments of paste.
  134. *
  135. * @access public
  136. * @param string $pasteid
  137. * @return array
  138. */
  139. public function readComments($pasteid)
  140. {
  141. $comments = array();
  142. $discdir = self::_dataid2discussionpath($pasteid);
  143. if (is_dir($discdir))
  144. {
  145. // Delete all files in discussion directory
  146. $dir = dir($discdir);
  147. while (false !== ($filename = $dir->read()))
  148. {
  149. // Filename is in the form pasteid.commentid.parentid:
  150. // - pasteid is the paste this reply belongs to.
  151. // - commentid is the comment identifier itself.
  152. // - parentid is the comment this comment replies to (It can be pasteid)
  153. if (is_file($discdir.$filename))
  154. {
  155. $comment = json_decode(file_get_contents($discdir.$filename));
  156. $items = explode('.', $filename);
  157. // Add some meta information not contained in file.
  158. $comment->meta->commentid=$items[1];
  159. $comment->meta->parentid=$items[2];
  160. // Store in array
  161. $comments[$comment->meta->postdate]=$comment;
  162. }
  163. }
  164. $dir->close();
  165. // Sort comments by date, oldest first.
  166. ksort($comments);
  167. }
  168. return $comments;
  169. }
  170. /**
  171. * Test if a comment exists.
  172. *
  173. * @access public
  174. * @param string $dataid
  175. * @param string $parentid
  176. * @param string $commentid
  177. * @return void
  178. */
  179. public function existsComment($pasteid, $parentid, $commentid)
  180. {
  181. return is_file(
  182. self::_dataid2discussionpath($pasteid) .
  183. $pasteid . '.' . $dataid . '.' . $parentid
  184. );
  185. }
  186. /**
  187. * initialize zerobin
  188. *
  189. * @access private
  190. * @static
  191. * @return void
  192. */
  193. private static function _init()
  194. {
  195. // Create storage directory if it does not exist.
  196. if (!is_dir(self::$_dir)) mkdir(self::$_dir, 0705);
  197. // Create .htaccess file if it does not exist.
  198. if (!is_file(self::$_dir . '.htaccess'))
  199. {
  200. file_put_contents(
  201. self::$_dir . '.htaccess',
  202. 'Allow from none' . PHP_EOL .
  203. 'Deny from all'. PHP_EOL
  204. );
  205. }
  206. }
  207. /**
  208. * Convert paste id to storage path.
  209. *
  210. * The idea is to creates subdirectories in order to limit the number of files per directory.
  211. * (A high number of files in a single directory can slow things down.)
  212. * eg. "f468483c313401e8" will be stored in "data/f4/68/f468483c313401e8"
  213. * High-trafic websites may want to deepen the directory structure (like Squid does).
  214. *
  215. * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/'
  216. *
  217. * @access private
  218. * @static
  219. * @param string $dataid
  220. * @return void
  221. */
  222. private static function _dataid2path($dataid)
  223. {
  224. return self::$_dir . substr($dataid,0,2) . '/' . substr($dataid,2,2) . '/';
  225. }
  226. /**
  227. * Convert paste id to discussion storage path.
  228. *
  229. * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/e3570978f9e4aa90.discussion/'
  230. *
  231. * @access private
  232. * @static
  233. * @param string $dataid
  234. * @return void
  235. */
  236. private static function _dataid2discussionpath($dataid)
  237. {
  238. return self::_dataid2path($dataid) . $dataid . '.discussion/';
  239. }
  240. }