The version of vichan running on lainchan.org
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

458 рядки
12KB

  1. <?php
  2. /*
  3. * Copyright (c) 2010-2013 Tinyboard Development Group
  4. */
  5. if (realpath($_SERVER['SCRIPT_FILENAME']) == str_replace('\\', '/', __FILE__)) {
  6. // You cannot request this file directly.
  7. exit;
  8. }
  9. /*
  10. joaoptm78@gmail.com
  11. http://www.php.net/manual/en/function.filesize.php#100097
  12. */
  13. function format_bytes($size) {
  14. $units = array(' B', ' KB', ' MB', ' GB', ' TB');
  15. for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
  16. return round($size, 2).$units[$i];
  17. }
  18. function doBoardListPart($list, $root, &$boards) {
  19. global $config;
  20. $body = '';
  21. foreach ($list as $key => $board) {
  22. if (is_array($board))
  23. $body .= ' <span class="sub" data-description="' . $key . '">[' . doBoardListPart($board, $root, $boards) . ']</span> ';
  24. else {
  25. if (gettype($key) == 'string') {
  26. $body .= ' <a href="' . $board . '">' . $key . '</a> /';
  27. } else {
  28. $title = '';
  29. if (isset ($boards[$board])) {
  30. $title = ' title="'.$boards[$board].'"';
  31. }
  32. $body .= ' <a href="' . $root . $board . '/' . $config['file_index'] . '"'.$title.'>' . $board . '</a> /';
  33. }
  34. }
  35. }
  36. $body = preg_replace('/\/$/', '', $body);
  37. return $body;
  38. }
  39. function createBoardlist($mod=false) {
  40. global $config;
  41. if (!isset($config['boards'])) return array('top'=>'','bottom'=>'');
  42. $xboards = listBoards();
  43. $boards = array();
  44. foreach ($xboards as $val) {
  45. $boards[$val['uri']] = $val['title'];
  46. }
  47. $body = doBoardListPart($config['boards'], $mod?'?/':$config['root'], $boards);
  48. if ($config['boardlist_wrap_bracket'] && !preg_match('/\] $/', $body))
  49. $body = '[' . $body . ']';
  50. $body = trim($body);
  51. // Message compact-boardlist.js faster, so that page looks less ugly during loading
  52. $top = "<script type='text/javascript'>if (typeof do_boardlist != 'undefined') do_boardlist();</script>";
  53. return array(
  54. 'top' => '<div class="boardlist">' . $body . '</div>' . $top,
  55. 'bottom' => '<div class="boardlist bottom">' . $body . '</div>'
  56. );
  57. }
  58. function error($message, $priority = true, $debug_stuff = false) {
  59. global $board, $mod, $config, $db_error;
  60. if ($config['syslog'] && $priority !== false) {
  61. // Use LOG_NOTICE instead of LOG_ERR or LOG_WARNING because most error message are not significant.
  62. _syslog($priority !== true ? $priority : LOG_NOTICE, $message);
  63. }
  64. if (defined('STDIN')) {
  65. // Running from CLI
  66. echo('Error: ' . $message . "\n");
  67. debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
  68. die();
  69. }
  70. if ($config['debug'] && isset($db_error)) {
  71. $debug_stuff = array_combine(array('SQLSTATE', 'Error code', 'Error message'), $db_error);
  72. }
  73. if ($config['debug']) {
  74. $debug_stuff['backtrace'] = debug_backtrace();
  75. }
  76. if (isset($_POST['json_response'])) {
  77. header('Content-Type: text/json; charset=utf-8');
  78. die(json_encode(array(
  79. 'error' => $message
  80. )));
  81. }
  82. else {
  83. header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
  84. }
  85. $pw = $config['db']['password'];
  86. $debug_callback = function(&$item) use (&$debug_callback, $pw) {
  87. if (is_array($item)) {
  88. $item = array_filter($item, $debug_callback);
  89. }
  90. return ($item !== $pw || !$pw);
  91. };
  92. if ($debug_stuff)
  93. $debug_stuff = array_filter($debug_stuff, $debug_callback);
  94. die(Element('page.html', array(
  95. 'config' => $config,
  96. 'title' => _('Error'),
  97. 'subtitle' => _('An error has occured.'),
  98. 'body' => Element('error.html', array(
  99. 'config' => $config,
  100. 'message' => $message,
  101. 'mod' => $mod,
  102. 'board' => isset($board) ? $board : false,
  103. 'debug' => is_array($debug_stuff) ? str_replace("\n", '&#10;', utf8tohtml(print_r($debug_stuff, true))) : utf8tohtml($debug_stuff)
  104. ))
  105. )));
  106. }
  107. function loginForm($error=false, $username=false, $redirect=false) {
  108. global $config;
  109. die(Element('page.html', array(
  110. 'index' => $config['root'],
  111. 'title' => _('Login'),
  112. 'config' => $config,
  113. 'body' => Element('login.html', array(
  114. 'config'=>$config,
  115. 'error'=>$error,
  116. 'username'=>utf8tohtml($username),
  117. 'redirect'=>$redirect
  118. )
  119. )
  120. )));
  121. }
  122. function pm_snippet($body, $len=null) {
  123. global $config;
  124. if (!isset($len))
  125. $len = &$config['mod']['snippet_length'];
  126. // Replace line breaks with some whitespace
  127. $body = preg_replace('@<br/?>@i', ' ', $body);
  128. // Strip tags
  129. $body = strip_tags($body);
  130. // Unescape HTML characters, to avoid splitting them in half
  131. $body = html_entity_decode($body, ENT_COMPAT, 'UTF-8');
  132. // calculate strlen() so we can add "..." after if needed
  133. $strlen = mb_strlen($body);
  134. $body = mb_substr($body, 0, $len);
  135. // Re-escape the characters.
  136. return '<em>' . utf8tohtml($body) . ($strlen > $len ? '&hellip;' : '') . '</em>';
  137. }
  138. function capcode($cap) {
  139. global $config;
  140. if (!$cap)
  141. return false;
  142. $capcode = array();
  143. if (isset($config['custom_capcode'][$cap])) {
  144. if (is_array($config['custom_capcode'][$cap])) {
  145. $capcode['cap'] = sprintf($config['custom_capcode'][$cap][0], $cap);
  146. if (isset($config['custom_capcode'][$cap][1]))
  147. $capcode['name'] = $config['custom_capcode'][$cap][1];
  148. if (isset($config['custom_capcode'][$cap][2]))
  149. $capcode['trip'] = $config['custom_capcode'][$cap][2];
  150. } else {
  151. $capcode['cap'] = sprintf($config['custom_capcode'][$cap], $cap);
  152. }
  153. } else {
  154. $capcode['cap'] = sprintf($config['capcode'], $cap);
  155. }
  156. return $capcode;
  157. }
  158. function truncate($body, $url, $max_lines = false, $max_chars = false) {
  159. global $config;
  160. if ($max_lines === false)
  161. $max_lines = $config['body_truncate'];
  162. if ($max_chars === false)
  163. $max_chars = $config['body_truncate_char'];
  164. // We don't want to risk truncating in the middle of an HTML comment.
  165. // It's easiest just to remove them all first.
  166. $body = preg_replace('/<!--.*?-->/s', '', $body);
  167. $original_body = $body;
  168. $lines = substr_count($body, '<br/>');
  169. // Limit line count
  170. if ($lines > $max_lines) {
  171. if (preg_match('/(((.*?)<br\/>){' . $max_lines . '})/', $body, $m))
  172. $body = $m[0];
  173. }
  174. $body = mb_substr($body, 0, $max_chars);
  175. if ($body != $original_body) {
  176. // Remove any corrupt tags at the end
  177. $body = preg_replace('/<([\w]+)?([^>]*)?$/', '', $body);
  178. // Open tags
  179. if (preg_match_all('/<([\w]+)[^>]*>/', $body, $open_tags)) {
  180. $tags = array();
  181. for ($x=0;$x<count($open_tags[0]);$x++) {
  182. if (!preg_match('/\/(\s+)?>$/', $open_tags[0][$x]))
  183. $tags[] = $open_tags[1][$x];
  184. }
  185. // List successfully closed tags
  186. if (preg_match_all('/(<\/([\w]+))>/', $body, $closed_tags)) {
  187. for ($x=0;$x<count($closed_tags[0]);$x++) {
  188. unset($tags[array_search($closed_tags[2][$x], $tags)]);
  189. }
  190. }
  191. // remove broken HTML entity at the end (if existent)
  192. $body = preg_replace('/&[^;]+$/', '', $body);
  193. $tags_no_close_needed = array("colgroup", "dd", "dt", "li", "optgroup", "option", "p", "tbody", "td", "tfoot", "th", "thead", "tr", "br", "img");
  194. // Close any open tags
  195. foreach ($tags as &$tag) {
  196. if (!in_array($tag, $tags_no_close_needed))
  197. $body .= "</{$tag}>";
  198. }
  199. } else {
  200. // remove broken HTML entity at the end (if existent)
  201. $body = preg_replace('/&[^;]*$/', '', $body);
  202. }
  203. $body .= '<span class="toolong">'.sprintf(_('Post too long. Click <a href="%s">here</a> to view the full text.'), $url).'</span>';
  204. }
  205. return $body;
  206. }
  207. function bidi_cleanup($data) {
  208. // Closes all embedded RTL and LTR unicode formatting blocks in a string so that
  209. // it can be used inside another without controlling its direction.
  210. $explicits = '\xE2\x80\xAA|\xE2\x80\xAB|\xE2\x80\xAD|\xE2\x80\xAE';
  211. $pdf = '\xE2\x80\xAC';
  212. preg_match_all("!$explicits!", $data, $m1, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
  213. preg_match_all("!$pdf!", $data, $m2, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
  214. if (count($m1) || count($m2)){
  215. $p = array();
  216. foreach ($m1 as $m){ $p[$m[0][1]] = 'push'; }
  217. foreach ($m2 as $m){ $p[$m[0][1]] = 'pop'; }
  218. ksort($p);
  219. $offset = 0;
  220. $stack = 0;
  221. foreach ($p as $pos => $type){
  222. if ($type == 'push'){
  223. $stack++;
  224. }else{
  225. if ($stack){
  226. $stack--;
  227. }else{
  228. # we have a pop without a push - remove it
  229. $data = substr($data, 0, $pos-$offset)
  230. .substr($data, $pos+3-$offset);
  231. $offset += 3;
  232. }
  233. }
  234. }
  235. # now add some pops if your stack is bigger than 0
  236. for ($i=0; $i<$stack; $i++){
  237. $data .= "\xE2\x80\xAC";
  238. }
  239. return $data;
  240. }
  241. return $data;
  242. }
  243. function secure_link_confirm($text, $title, $confirm_message, $href) {
  244. global $config;
  245. return '<a onclick="if (event.which==2) return true;if (confirm(\'' . htmlentities(addslashes($confirm_message)) . '\')) document.location=\'?/' . htmlspecialchars(addslashes($href . '/' . make_secure_link_token($href))) . '\';return false;" title="' . htmlentities($title) . '" href="?/' . $href . '">' . $text . '</a>';
  246. }
  247. function secure_link($href) {
  248. return $href . '/' . make_secure_link_token($href);
  249. }
  250. function embed_html($link) {
  251. global $config;
  252. foreach ($config['embedding'] as $embed) {
  253. if ($html = preg_replace($embed[0], $embed[1], $link)) {
  254. if ($html == $link)
  255. continue; // Nope
  256. $html = str_replace('%%tb_width%%', $config['embed_width'], $html);
  257. $html = str_replace('%%tb_height%%', $config['embed_height'], $html);
  258. return $html;
  259. }
  260. }
  261. if ($link[0] == '<') {
  262. // Prior to v0.9.6-dev-8, HTML code for embedding was stored in the database instead of the link.
  263. return $link;
  264. }
  265. return 'Embedding error.';
  266. }
  267. class Post {
  268. public function __construct($post, $root=null, $mod=false) {
  269. global $config;
  270. if (!isset($root))
  271. $root = &$config['root'];
  272. foreach ($post as $key => $value) {
  273. $this->{$key} = $value;
  274. }
  275. if (isset($this->files) && $this->files)
  276. $this->files = @json_decode($this->files);
  277. $this->subject = utf8tohtml($this->subject);
  278. $this->name = utf8tohtml($this->name);
  279. $this->mod = $mod;
  280. $this->root = $root;
  281. if ($this->embed)
  282. $this->embed = embed_html($this->embed);
  283. $this->modifiers = extract_modifiers($this->body_nomarkup);
  284. if ($config['always_regenerate_markup']) {
  285. $this->body = $this->body_nomarkup;
  286. markup($this->body);
  287. }
  288. if ($this->mod)
  289. // Fix internal links
  290. // Very complicated regex
  291. $this->body = preg_replace(
  292. '/<a((([a-zA-Z]+="[^"]+")|[a-zA-Z]+=[a-zA-Z]+|\s)*)href="' . preg_quote($config['root'], '/') . '(' . sprintf(preg_quote($config['board_path'], '/'), $config['board_regex']) . ')/u',
  293. '<a $1href="?/$4',
  294. $this->body
  295. );
  296. }
  297. public function link($pre = '', $page = false) {
  298. global $config, $board;
  299. return $this->root . $board['dir'] . $config['dir']['res'] . link_for((array)$this, $page == '50') . '#' . $pre . $this->id;
  300. }
  301. public function build($index=false) {
  302. global $board, $config;
  303. return Element('post_reply.html', array('config' => $config, 'board' => $board, 'post' => &$this, 'index' => $index, 'mod' => $this->mod));
  304. }
  305. };
  306. class Thread {
  307. public function __construct($post, $root = null, $mod = false, $hr = true) {
  308. global $config;
  309. if (!isset($root))
  310. $root = &$config['root'];
  311. foreach ($post as $key => $value) {
  312. $this->{$key} = $value;
  313. }
  314. if (isset($this->files))
  315. $this->files = @json_decode($this->files);
  316. $this->subject = utf8tohtml($this->subject);
  317. $this->name = utf8tohtml($this->name);
  318. $this->mod = $mod;
  319. $this->root = $root;
  320. $this->hr = $hr;
  321. $this->posts = array();
  322. $this->omitted = 0;
  323. $this->omitted_images = 0;
  324. if ($this->embed)
  325. $this->embed = embed_html($this->embed);
  326. $this->modifiers = extract_modifiers($this->body_nomarkup);
  327. if ($config['always_regenerate_markup']) {
  328. $this->body = $this->body_nomarkup;
  329. markup($this->body);
  330. }
  331. if ($this->mod)
  332. // Fix internal links
  333. // Very complicated regex
  334. $this->body = preg_replace(
  335. '/<a((([a-zA-Z]+="[^"]+")|[a-zA-Z]+=[a-zA-Z]+|\s)*)href="' . preg_quote($config['root'], '/') . '(' . sprintf(preg_quote($config['board_path'], '/'), $config['board_regex']) . ')/u',
  336. '<a $1href="?/$4',
  337. $this->body
  338. );
  339. }
  340. public function link($pre = '', $page = false) {
  341. global $config, $board;
  342. return $this->root . $board['dir'] . $config['dir']['res'] . link_for((array)$this, $page == '50') . '#' . $pre . $this->id;
  343. }
  344. public function add(Post $post) {
  345. $this->posts[] = $post;
  346. }
  347. public function postCount() {
  348. return count($this->posts) + $this->omitted;
  349. }
  350. public function build($index=false, $isnoko50=false) {
  351. global $board, $config, $debug;
  352. $hasnoko50 = $this->postCount() >= $config['noko50_min'];
  353. event('show-thread', $this);
  354. $file = ($index && $config['file_board']) ? 'post_thread_fileboard.html' : 'post_thread.html';
  355. $built = Element($file, array('config' => $config, 'board' => $board, 'post' => &$this, 'index' => $index, 'hasnoko50' => $hasnoko50, 'isnoko50' => $isnoko50, 'mod' => $this->mod));
  356. return $built;
  357. }
  358. };