zerobin_data.php 7.6 KB

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