PageRenderTime 1652ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/sticky-notes/show.php

#
PHP | 261 lines | 195 code | 35 blank | 31 comment | 68 complexity | baea4accc1d49d6250394a810feb7658 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Sticky Notes pastebin
  4. * @ver 0.3
  5. * @license BSD License - www.opensource.org/licenses/bsd-license.php
  6. *
  7. * Copyright (c) 2012 Sayak Banerjee <sayakb@kde.org>
  8. * All rights reserved. Do not remove this copyright notice.
  9. */
  10. // Invoke required files
  11. include_once('init.php');
  12. // Collect some data
  13. $paste_id = $core->variable('id', 0);
  14. $hash = $core->variable('hash', 0);
  15. $mode = $core->variable('mode', '');
  16. $project = $core->variable('project', '');
  17. $password = $core->variable('password', '');
  18. $sid = $core->variable('session_id_' . $paste_id, '', true);
  19. $mode = strtolower($mode);
  20. // Password exempt
  21. $exempt = false;
  22. // Trim trailing /
  23. if (strrpos($password, '/') == strlen($password) - 1)
  24. {
  25. $password = substr($password, 0, strlen($password) - 1);
  26. }
  27. if (empty($mode))
  28. {
  29. $mode = $core->variable('format', '');
  30. $_GET['mode'] = $mode;
  31. }
  32. // Check for mode validity
  33. if ($mode && $mode != 'raw' && $mode != 'xml' && $mode != 'json')
  34. {
  35. die;
  36. }
  37. // Initialize the skin file
  38. if ($mode != 'raw')
  39. {
  40. $skin->init('tpl_show');
  41. }
  42. // We want paste id
  43. if ($paste_id == 0)
  44. {
  45. $core->redirect($core->path() . 'all/');
  46. }
  47. // Escape the paste id
  48. $db->escape($paste_id);
  49. // Get the paste data
  50. $sql = "SELECT * FROM {$db->prefix}main WHERE id = {$paste_id} LIMIT 1";
  51. $row = $db->query($sql, true);
  52. // Check if something was returned
  53. if ($row == null)
  54. {
  55. if ($mode == 'xml' || $mode == 'json')
  56. {
  57. $skin->assign('error_message', 'err_not_found');
  58. echo $skin->output("api_error.{$mode}");
  59. die;
  60. }
  61. else if ($mode == 'raw')
  62. {
  63. die($lang->get('error_404'));
  64. }
  65. else
  66. {
  67. $skin->assign(array(
  68. 'error_text' => $lang->get('error_404'),
  69. 'data_visibility' => 'hidden',
  70. ));
  71. $skin->kill();
  72. }
  73. }
  74. // Is it a private paste?
  75. if ($row['private'] == "1")
  76. {
  77. if (empty($hash) || $row['hash'] != $hash)
  78. {
  79. if ($mode == 'xml' || $mode == 'json')
  80. {
  81. $skin->assign('error_message', 'err_invalid_hash');
  82. echo $skin->output("api_error.{$mode}");
  83. die;
  84. }
  85. else if ($mode == 'raw')
  86. {
  87. die($lang->get('error_hash'));
  88. }
  89. else
  90. {
  91. $skin->assign(array(
  92. 'error_text' => $lang->get('error_hash'),
  93. 'data_visibility' => 'hidden',
  94. ));
  95. $skin->kill();
  96. }
  97. }
  98. }
  99. // Check if password cookie is there
  100. if (!empty($row['password']) && !empty($sid))
  101. {
  102. // Escape the session id
  103. $db->escape($sid);
  104. // Clean up the session data every 30 seconds
  105. if (time() % 30 == 0)
  106. {
  107. $age = time() - 1200;
  108. $db->query("DELETE FROM {$db->prefix}session " .
  109. "WHERE timestamp < {$age}");
  110. }
  111. $pass_data = $db->query("SELECT sid FROM {$db->prefix}session " .
  112. "WHERE sid = '{$sid}'", true);
  113. if (!empty($pass_data['sid']))
  114. {
  115. $exempt = true;
  116. }
  117. }
  118. // Is it password protected?
  119. if (!empty($row['password']) && empty($password) && !$exempt)
  120. {
  121. if ($mode == 'xml' || $mode == 'json')
  122. {
  123. $skin->assign('error_message', 'err_password_required');
  124. echo $skin->output("api_error.{$mode}");
  125. die;
  126. }
  127. else if ($mode == 'raw')
  128. {
  129. die($lang->get('err_passreqd'));
  130. }
  131. else
  132. {
  133. $skin->init('tpl_show_password');
  134. $skin->title("#{$row['id']} &bull; " . $lang->get('site_title'));
  135. $skin->output();
  136. exit;
  137. }
  138. }
  139. // Check password
  140. if (!empty($row['password']) && !empty($password) && !$exempt)
  141. {
  142. $check = sha1(sha1($password) . $row['salt']);
  143. if ($check != $row['password'])
  144. {
  145. if ($mode == 'xml' || $mode == 'json')
  146. {
  147. $skin->assign('error_message', 'err_invalid_password');
  148. echo $skin->output("api_error.{$mode}");
  149. die;
  150. }
  151. else if ($mode == 'raw')
  152. {
  153. die($lang->get('invalid_password'));
  154. }
  155. else
  156. {
  157. $skin->assign(array(
  158. 'error_text' => $lang->get('invalid_password'),
  159. 'data_visibility' => 'hidden',
  160. ));
  161. $skin->kill();
  162. }
  163. }
  164. else
  165. {
  166. // Create a session
  167. $sid = sha1(time() . $core->remote_ip());
  168. $core->set_cookie('session_id_' . $paste_id, $sid);
  169. $db->query("INSERT INTO {$db->prefix}session " .
  170. "(sid, timestamp) VALUES ('{$sid}', " . time() . ")");
  171. }
  172. }
  173. // Is it raw? just dump the code then
  174. if ($mode == 'raw')
  175. {
  176. header('Content-type: text/plain; charset=UTF-8');
  177. header('Content-Disposition: inline; filename="pastedata"');
  178. echo $row['data'];
  179. exit;
  180. }
  181. // Prepare GeSHi
  182. $geshi = new GeSHi($row['data'], $row['language']);
  183. $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 2);
  184. $geshi->set_header_type(GESHI_HEADER_DIV);
  185. $geshi->set_line_style('background: #f7f7f7; text-shadow: 0px 1px #fff; padding: 1px;',
  186. 'background: #fbfbfb; text-shadow: 0px 1px #fff; padding: 1px;');
  187. $geshi->set_overall_style('word-wrap:break-word;');
  188. // Generate the data
  189. $user = empty($row['author']) ? $lang->get('anonymous') : htmlentities($row['author']);
  190. $time = date('d M Y, h:i:s e', $row['timestamp']);
  191. $info = $lang->get('posted_info');
  192. $info = preg_replace('/\_\_user\_\_/', $user, $info);
  193. $info = preg_replace('/\_\_time\_\_/', $time, $info);
  194. // Before we display, we need to escape the data from the skin/lang parsers
  195. $code_data = (empty($mode) ? $geshi->parse_code() : htmlentities($row['data']));
  196. $lang->escape($code_data);
  197. $skin->escape($code_data);
  198. // Assign template variables
  199. $skin->assign(array(
  200. 'paste_id' => $row['id'],
  201. 'paste_data' => $code_data,
  202. 'paste_lang' => htmlentities($row['language']),
  203. 'paste_info' => $info,
  204. 'paste_user' => $user,
  205. 'paste_timestamp' => $row['timestamp'],
  206. 'raw_url' => $nav->get_paste($row['id'], $hash, $project, false, 'raw'),
  207. 'share_url' => urlencode($core->base_uri()),
  208. 'share_title' => urlencode($lang->get('paste') . ' #' . $row['id']),
  209. 'error_visibility' => 'hidden',
  210. 'geshi_stylesheet' => $geshi->get_stylesheet(),
  211. ));
  212. // Let's output the page now
  213. $skin->title("#{$row['id']} &bull; " . $lang->get('site_title'));
  214. if ($mode == 'raw')
  215. {
  216. $skin->output(false, true);
  217. }
  218. else if ($mode)
  219. {
  220. echo $skin->output("api_show.{$mode}");
  221. }
  222. else
  223. {
  224. $skin->output();
  225. }
  226. ?>