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