data.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.21.1
  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. * 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 zerobin_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 zerobin_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. return json_decode(
  75. file_get_contents(self::_dataid2path($pasteid) . $pasteid)
  76. );
  77. }
  78. /**
  79. * Delete a paste and its discussion.
  80. *
  81. * @access public
  82. * @param string $pasteid
  83. * @return void
  84. */
  85. public function delete($pasteid)
  86. {
  87. // Delete the paste itself.
  88. unlink(self::_dataid2path($pasteid) . $pasteid);
  89. // Delete discussion if it exists.
  90. $discdir = self::_dataid2discussionpath($pasteid);
  91. if (is_dir($discdir))
  92. {
  93. // Delete all files in discussion directory
  94. $dir = dir($discdir);
  95. while (false !== ($filename = $dir->read()))
  96. {
  97. if (is_file($discdir.$filename)) unlink($discdir.$filename);
  98. }
  99. $dir->close();
  100. // Delete the discussion directory.
  101. @rmdir($discdir);
  102. }
  103. }
  104. /**
  105. * Test if a paste exists.
  106. *
  107. * @access public
  108. * @param string $dataid
  109. * @return void
  110. */
  111. public function exists($pasteid)
  112. {
  113. return is_file(self::_dataid2path($pasteid) . $pasteid);
  114. }
  115. /**
  116. * Create a comment in a paste.
  117. *
  118. * @access public
  119. * @param string $pasteid
  120. * @param string $parentid
  121. * @param string $commentid
  122. * @param array $comment
  123. * @return bool
  124. */
  125. public function createComment($pasteid, $parentid, $commentid, $comment)
  126. {
  127. $storagedir = self::_dataid2discussionpath($pasteid);
  128. $filename = $pasteid . '.' . $commentid . '.' . $parentid;
  129. if (is_file($storagedir . $filename)) return false;
  130. if (!is_dir($storagedir)) mkdir($storagedir, 0705, true);
  131. return (bool) @file_put_contents($storagedir . $filename, json_encode($comment));
  132. }
  133. /**
  134. * Read all comments of paste.
  135. *
  136. * @access public
  137. * @param string $pasteid
  138. * @return array
  139. */
  140. public function readComments($pasteid)
  141. {
  142. $comments = array();
  143. $discdir = self::_dataid2discussionpath($pasteid);
  144. if (is_dir($discdir))
  145. {
  146. // Delete all files in discussion directory
  147. $dir = dir($discdir);
  148. while (false !== ($filename = $dir->read()))
  149. {
  150. // Filename is in the form pasteid.commentid.parentid:
  151. // - pasteid is the paste this reply belongs to.
  152. // - commentid is the comment identifier itself.
  153. // - parentid is the comment this comment replies to (It can be pasteid)
  154. if (is_file($discdir.$filename))
  155. {
  156. $comment = json_decode(file_get_contents($discdir.$filename));
  157. $items = explode('.', $filename);
  158. // Add some meta information not contained in file.
  159. $comment->meta->commentid=$items[1];
  160. $comment->meta->parentid=$items[2];
  161. // Store in array
  162. $comments[$comment->meta->postdate]=$comment;
  163. }
  164. }
  165. $dir->close();
  166. // Sort comments by date, oldest first.
  167. ksort($comments);
  168. }
  169. return $comments;
  170. }
  171. /**
  172. * Test if a comment exists.
  173. *
  174. * @access public
  175. * @param string $dataid
  176. * @param string $parentid
  177. * @param string $commentid
  178. * @return void
  179. */
  180. public function existsComment($pasteid, $parentid, $commentid)
  181. {
  182. return is_file(
  183. self::_dataid2discussionpath($pasteid) .
  184. $pasteid . '.' . $commentid . '.' . $parentid
  185. );
  186. }
  187. /**
  188. * initialize zerobin
  189. *
  190. * @access private
  191. * @static
  192. * @return void
  193. */
  194. private static function _init()
  195. {
  196. // Create storage directory if it does not exist.
  197. if (!is_dir(self::$_dir)) mkdir(self::$_dir, 0705);
  198. // Create .htaccess file if it does not exist.
  199. if (!is_file(self::$_dir . '.htaccess'))
  200. {
  201. file_put_contents(
  202. self::$_dir . '.htaccess',
  203. 'Allow from none' . PHP_EOL .
  204. 'Deny from all'. PHP_EOL
  205. );
  206. }
  207. }
  208. /**
  209. * Convert paste id to storage path.
  210. *
  211. * The idea is to creates subdirectories in order to limit the number of files per directory.
  212. * (A high number of files in a single directory can slow things down.)
  213. * eg. "f468483c313401e8" will be stored in "data/f4/68/f468483c313401e8"
  214. * High-trafic websites may want to deepen the directory structure (like Squid does).
  215. *
  216. * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/'
  217. *
  218. * @access private
  219. * @static
  220. * @param string $dataid
  221. * @return void
  222. */
  223. private static function _dataid2path($dataid)
  224. {
  225. return self::$_dir . substr($dataid,0,2) . '/' . substr($dataid,2,2) . '/';
  226. }
  227. /**
  228. * Convert paste id to discussion storage path.
  229. *
  230. * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/e3570978f9e4aa90.discussion/'
  231. *
  232. * @access private
  233. * @static
  234. * @param string $dataid
  235. * @return void
  236. */
  237. private static function _dataid2discussionpath($dataid)
  238. {
  239. return self::_dataid2path($dataid) . $dataid . '.discussion/';
  240. }
  241. }