1
0

zerobin.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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
  14. *
  15. * Controller, puts it all together.
  16. */
  17. class zerobin
  18. {
  19. /*
  20. * @const string version
  21. */
  22. const VERSION = 'Alpha 0.15';
  23. /**
  24. * @access private
  25. * @var array
  26. */
  27. private $_conf = array(
  28. 'model' => 'zerobin_data',
  29. );
  30. /**
  31. * @access private
  32. * @var string
  33. */
  34. private $_data = '';
  35. /**
  36. * @access private
  37. * @var string
  38. */
  39. private $_error = '';
  40. /**
  41. * @access private
  42. * @var zerobin_data
  43. */
  44. private $_model;
  45. /**
  46. * constructor
  47. *
  48. * initializes and runs ZeroBin
  49. *
  50. * @access public
  51. */
  52. public function __construct()
  53. {
  54. if (version_compare(PHP_VERSION, '5.2.6') < 0)
  55. die('ZeroBin requires php 5.2.6 or above to work. Sorry.');
  56. // In case stupid admin has left magic_quotes enabled in php.ini.
  57. if (get_magic_quotes_gpc())
  58. {
  59. require_once PATH . 'lib/filter.php';
  60. $_POST = array_map('filter::stripslashes_deep', $_POST);
  61. $_GET = array_map('filter::stripslashes_deep', $_GET);
  62. $_COOKIE = array_map('filter::stripslashes_deep', $_COOKIE);
  63. }
  64. // Load config from ini file.
  65. $this->_init();
  66. // Create new paste or comment.
  67. if (!empty($_POST['data']))
  68. {
  69. $this->_create();
  70. }
  71. // Display an existing paste.
  72. elseif (!empty($_SERVER['QUERY_STRING']))
  73. {
  74. $this->_read();
  75. }
  76. // Display ZeroBin frontend
  77. $this->_view();
  78. }
  79. /**
  80. * initialize zerobin
  81. *
  82. * @access private
  83. * @return void
  84. */
  85. private function _init()
  86. {
  87. $this->_conf = parse_ini_file(PATH . 'cfg/conf.ini');
  88. $this->_model = $this->_conf['model'];
  89. }
  90. /**
  91. * get the model, create one if needed
  92. *
  93. * @access private
  94. * @return zerobin_data
  95. */
  96. private function _model()
  97. {
  98. // if needed, initialize the model
  99. if(is_string($this->_model)) {
  100. require_once PATH . 'lib/' . $this->_model . '.php';
  101. $this->_model = forward_static_call(array($this->_model, 'getInstance'), $this->_conf['model_options']);
  102. }
  103. return $this->_model;
  104. }
  105. /**
  106. * Store new paste or comment.
  107. *
  108. * POST contains:
  109. * data (mandatory) = json encoded SJCL encrypted text (containing keys: iv,salt,ct)
  110. *
  111. * All optional data will go to meta information:
  112. * expire (optional) = expiration delay (never,10min,1hour,1day,1month,1year,burn) (default:never)
  113. * opendiscusssion (optional) = is the discussion allowed on this paste ? (0/1) (default:0)
  114. * nickname (optional) = in discussion, encoded SJCL encrypted text nickname of author of comment (containing keys: iv,salt,ct)
  115. * parentid (optional) = in discussion, which comment this comment replies to.
  116. * pasteid (optional) = in discussion, which paste this comment belongs to.
  117. *
  118. * @access private
  119. * @return void
  120. */
  121. private function _create()
  122. {
  123. header('Content-type: application/json');
  124. $error = false;
  125. // Make sure last paste from the IP address was more than 10 seconds ago.
  126. require_once PATH . 'lib/traffic_limiter.php';
  127. traffic_limiter::setLimit($this->_conf['traffic_limit']);
  128. traffic_limiter::setPath($this->_conf['traffic_dir']);
  129. if (
  130. !traffic_limiter::canPass($_SERVER['REMOTE_ADDR'])
  131. ) $this->_return_message(1, 'Please wait 10 seconds between each post.');
  132. // Make sure content is not too big.
  133. $data = $_POST['data'];
  134. if (
  135. strlen($data) > 2000000
  136. ) $this->_return_message(1, 'Paste is limited to 2 MB of encrypted data.');
  137. // Make sure format is correct.
  138. require_once PATH . 'lib/sjcl.php';
  139. if (!sjcl::isValid($data)) $this->_return_message(1, 'Invalid data.');
  140. // Read additional meta-information.
  141. $meta=array();
  142. // Read expiration date
  143. if (!empty($_POST['expire']))
  144. {
  145. switch ($_POST['expire'])
  146. {
  147. case '10min':
  148. $meta['expire_date'] = time()+10*60;
  149. break;
  150. case '1hour':
  151. $meta['expire_date'] = time()+60*60;
  152. break;
  153. case '1day':
  154. $meta['expire_date'] = time()+24*60*60;
  155. break;
  156. case '1month':
  157. $meta['expire_date'] = strtotime('+1 month');
  158. break;
  159. case '1year':
  160. $meta['expire_date'] = strtotime('+1 year');
  161. break;
  162. case 'burn':
  163. $meta['burnafterreading'] = true;
  164. }
  165. }
  166. // Read open discussion flag.
  167. if (!empty($_POST['opendiscussion']))
  168. {
  169. $opendiscussion = $_POST['opendiscussion'];
  170. if ($opendiscussion != 0)
  171. {
  172. if ($opendiscussion != 1) $error = true;
  173. $meta['opendiscussion'] = true;
  174. }
  175. }
  176. // You can't have an open discussion on a "Burn after reading" paste:
  177. if (isset($meta['burnafterreading'])) unset($meta['opendiscussion']);
  178. // Optional nickname for comments
  179. if (!empty($_POST['nickname']))
  180. {
  181. // Generation of the anonymous avatar (Vizhash):
  182. // If a nickname is provided, we generate a Vizhash.
  183. // (We assume that if the user did not enter a nickname, he/she wants
  184. // to be anonymous and we will not generate the vizhash.)
  185. $nick = $_POST['nickname'];
  186. if (!sjcl::isValid($nick))
  187. {
  188. $error = true;
  189. }
  190. else
  191. {
  192. require_once PATH . 'lib/vizhash_gd_zero.php';
  193. $meta['nickname'] = $nick;
  194. $vz = new vizhash16x16();
  195. $pngdata = $vz->generate($_SERVER['REMOTE_ADDR']);
  196. if ($pngdata != '')
  197. {
  198. $meta['vizhash'] = 'data:image/png;base64,' . base64_encode($pngdata);
  199. }
  200. // Once the avatar is generated, we do not keep the IP address, nor its hash.
  201. }
  202. }
  203. if ($error) $this->_return_message(1, 'Invalid data.');
  204. // Add post date to meta.
  205. $meta['postdate'] = time();
  206. // We just want a small hash to avoid collisions:
  207. // Half-MD5 (64 bits) will do the trick
  208. $dataid = substr(hash('md5', $data), 0, 16);
  209. $storage = array('data' => $data);
  210. // Add meta-information only if necessary.
  211. if (count($meta)) $storage['meta'] = $meta;
  212. // The user posts a comment.
  213. if (
  214. !empty($_POST['parentid']) &&
  215. !empty($_POST['pasteid'])
  216. )
  217. {
  218. $pasteid = $_POST['pasteid'];
  219. $parentid = $_POST['parentid'];
  220. if (
  221. !preg_match('/[a-f\d]{16}/', $pasteid) ||
  222. !preg_match('/[a-f\d]{16}/', $parentid)
  223. ) $this->_return_message(1, 'Invalid data.');
  224. // Comments do not expire (it's the paste that expires)
  225. unset($storage['expire_date']);
  226. unset($storage['opendiscussion']);
  227. // Make sure paste exists.
  228. if (
  229. !$this->_model()->exists($pasteid)
  230. ) $this->_return_message(1, 'Invalid data.');
  231. // Make sure the discussion is opened in this paste.
  232. $paste = $this->_model()->read($pasteid);
  233. if (
  234. !$paste->meta->opendiscussion
  235. ) $this->_return_message(1, 'Invalid data.');
  236. // Check for improbable collision.
  237. if (
  238. $this->_model()->existsComment($pasteid, $parentid, $dataid)
  239. ) $this->_return_message(1, 'You are unlucky. Try again.');
  240. // New comment
  241. if (
  242. $this->_model()->createComment($pasteid, $parentid, $dataid, $storage) === false
  243. ) $this->_return_message(1, 'Error saving comment. Sorry.');
  244. // 0 = no error
  245. $this->_return_message(0, $dataid);
  246. }
  247. // The user posts a standard paste.
  248. else
  249. {
  250. // Check for improbable collision.
  251. if (
  252. $this->_model()->exists($dataid)
  253. ) $this->_return_message(1, 'You are unlucky. Try again.');
  254. // New paste
  255. if (
  256. $this->_model()->create($dataid, $storage) === false
  257. ) $this->_return_message(1, 'Error saving paste. Sorry.');
  258. // 0 = no error
  259. $this->_return_message(0, $dataid);
  260. }
  261. $this->_return_message(1, 'Server error.');
  262. }
  263. /**
  264. * Read an existing paste or comment.
  265. *
  266. * @access private
  267. * @return void
  268. */
  269. private function _read()
  270. {
  271. $dataid = $_SERVER['QUERY_STRING'];
  272. // Is this a valid paste identifier?
  273. if (preg_match('/[a-f\d]{16}/', $dataid))
  274. {
  275. // Check that paste exists.
  276. if ($this->_model()->exists($dataid))
  277. {
  278. // Get the paste itself.
  279. $paste = $this->_model()->read($dataid);
  280. // See if paste has expired.
  281. if (
  282. isset($paste->meta->expire_date) &&
  283. $paste->meta->expire_date < time()
  284. )
  285. {
  286. // Delete the paste
  287. $this->_model()->delete($dataid);
  288. $this->_error = 'Paste does not exist or has expired.';
  289. }
  290. // If no error, return the paste.
  291. else
  292. {
  293. // We kindly provide the remaining time before expiration (in seconds)
  294. if (
  295. property_exists($paste->meta, 'expire_date')
  296. ) $paste->meta->remaining_time = $paste->meta->expire_date - time();
  297. // The paste itself is the first in the list of encrypted messages.
  298. $messages = array($paste);
  299. // If it's a discussion, get all comments.
  300. if (
  301. property_exists($paste->meta, 'opendiscussion') &&
  302. $paste->meta->opendiscussion
  303. )
  304. {
  305. $messages = array_merge(
  306. $messages,
  307. $this->_model()->readComments($dataid)
  308. );
  309. }
  310. $this->_data = json_encode($messages);
  311. // If the paste was meant to be read only once, delete it.
  312. if (
  313. property_exists($paste->meta, 'burnafterreading') &&
  314. $paste->meta->burnafterreading
  315. ) $this->_model()->delete($dataid);
  316. }
  317. }
  318. else
  319. {
  320. $this->_error = 'Paste does not exist or has expired.';
  321. }
  322. }
  323. }
  324. /**
  325. * Display ZeroBin frontend.
  326. *
  327. * @access private
  328. * @return void
  329. */
  330. private function _view()
  331. {
  332. require_once PATH . 'lib/rain.tpl.class.php';
  333. header('Content-Type: text/html; charset=utf-8');
  334. $page = new RainTPL;
  335. // We escape it here because ENT_NOQUOTES can't be used in RainTPL templates.
  336. $page->assign('CIPHERDATA', htmlspecialchars($this->_data, ENT_NOQUOTES));
  337. $page->assign('ERRORMESSAGE', $this->_error);
  338. $page->assign('VERSION', self::VERSION);
  339. $page->draw('page');
  340. }
  341. /**
  342. * return JSON encoded message and exit
  343. *
  344. * @access private
  345. * @param bool $status
  346. * @param string $message
  347. * @return void
  348. */
  349. private function _return_message($status, $message)
  350. {
  351. $result = array('status' => $status);
  352. if ($status)
  353. {
  354. $result['message'] = $message;
  355. }
  356. else
  357. {
  358. $result['id'] = $message;
  359. }
  360. exit(json_encode($result));
  361. }
  362. }