data.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. $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->meta->commentid = $items[1];
  171. $comment->meta->parentid = $items[2];
  172. // Store in array
  173. $comments[$comment->meta->postdate] = $comment;
  174. }
  175. }
  176. $dir->close();
  177. // Sort comments by date, oldest first.
  178. ksort($comments);
  179. }
  180. return $comments;
  181. }
  182. /**
  183. * Test if a comment exists.
  184. *
  185. * @access public
  186. * @param string $dataid
  187. * @param string $parentid
  188. * @param string $commentid
  189. * @return void
  190. */
  191. public function existsComment($pasteid, $parentid, $commentid)
  192. {
  193. return is_file(
  194. self::_dataid2discussionpath($pasteid) .
  195. $pasteid . '.' . $commentid . '.' . $parentid
  196. );
  197. }
  198. /**
  199. * initialize zerobin
  200. *
  201. * @access private
  202. * @static
  203. * @return void
  204. */
  205. private static function _init()
  206. {
  207. // Create storage directory if it does not exist.
  208. if (!is_dir(self::$_dir)) mkdir(self::$_dir, 0705);
  209. // Create .htaccess file if it does not exist.
  210. if (!is_file(self::$_dir . '.htaccess'))
  211. {
  212. file_put_contents(
  213. self::$_dir . '.htaccess',
  214. 'Allow from none' . PHP_EOL .
  215. 'Deny from all'. PHP_EOL
  216. );
  217. }
  218. }
  219. /**
  220. * Convert paste id to storage path.
  221. *
  222. * The idea is to creates subdirectories in order to limit the number of files per directory.
  223. * (A high number of files in a single directory can slow things down.)
  224. * eg. "f468483c313401e8" will be stored in "data/f4/68/f468483c313401e8"
  225. * High-trafic websites may want to deepen the directory structure (like Squid does).
  226. *
  227. * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/'
  228. *
  229. * @access private
  230. * @static
  231. * @param string $dataid
  232. * @return void
  233. */
  234. private static function _dataid2path($dataid)
  235. {
  236. return self::$_dir . substr($dataid,0,2) . '/' . substr($dataid,2,2) . '/';
  237. }
  238. /**
  239. * Convert paste id to discussion storage path.
  240. *
  241. * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/e3570978f9e4aa90.discussion/'
  242. *
  243. * @access private
  244. * @static
  245. * @param string $dataid
  246. * @return void
  247. */
  248. private static function _dataid2discussionpath($dataid)
  249. {
  250. return self::_dataid2path($dataid) . $dataid . '.discussion/';
  251. }
  252. }