data.php 7.0 KB

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