PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/display.php

https://gitlab.com/potion/librechan
PHP | 528 lines | 415 code | 86 blank | 27 comment | 57 complexity | ac083f5e284840a908fcb9eac9d27b2f MD5 | raw file
  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 (isset($debug_stuff['file']))
  61. $message .= " {$debug_stuff['file']}";
  62. if ($config['syslog'] && $priority !== false) {
  63. // Use LOG_NOTICE instead of LOG_ERR or LOG_WARNING because most error message are not significant.
  64. _syslog($priority !== true ? $priority : LOG_NOTICE, $message);
  65. }
  66. if (defined('STDIN')) {
  67. // Running from CLI
  68. echo('Error: ' . $message . "\n");
  69. debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
  70. die();
  71. }
  72. if ($config['debug'] && isset($db_error)) {
  73. $debug_stuff = array_combine(array('SQLSTATE', 'Error code', 'Error message'), $db_error);
  74. }
  75. if ($config['debug']) {
  76. $debug_stuff['backtrace'] = debug_backtrace();
  77. }
  78. if (isset($_POST['json_response'])) {
  79. header('Content-Type: text/json; charset=utf-8');
  80. die(json_encode(array(
  81. 'error' => $message
  82. )));
  83. }
  84. else {
  85. header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
  86. }
  87. $pw = $config['db']['password'];
  88. $debug_callback = function(&$item) use (&$debug_callback, $pw) {
  89. if (is_array($item)) {
  90. $item = array_filter($item, $debug_callback);
  91. }
  92. return ($item !== $pw || !$pw);
  93. };
  94. if ($debug_stuff)
  95. $debug_stuff = array_filter($debug_stuff, $debug_callback);
  96. die(Element('page.html', array(
  97. 'config' => $config,
  98. 'title' => _('Error'),
  99. 'subtitle' => _('An error has occured.'),
  100. 'body' => Element('error.html', array(
  101. 'config' => $config,
  102. 'message' => $message,
  103. 'mod' => $mod,
  104. 'board' => isset($board) ? $board : false,
  105. 'debug' => is_array($debug_stuff) ? str_replace("\n", '&#10;', utf8tohtml(print_r($debug_stuff, true))) : utf8tohtml($debug_stuff)
  106. ))
  107. )));
  108. }
  109. function loginForm($error=false, $username=false, $redirect=false) {
  110. global $config;
  111. die(Element('page.html', array(
  112. 'index' => $config['root'],
  113. 'title' => _('Login'),
  114. 'config' => $config,
  115. 'body' => Element('login.html', array(
  116. 'config'=>$config,
  117. 'error'=>$error,
  118. 'username'=>utf8tohtml($username),
  119. 'redirect'=>$redirect
  120. )
  121. )
  122. )));
  123. }
  124. function pm_snippet($body, $len=null) {
  125. global $config;
  126. if (!isset($len))
  127. $len = &$config['mod']['snippet_length'];
  128. // Replace line breaks with some whitespace
  129. $body = preg_replace('@<br/?>@i', ' ', $body);
  130. // Strip tags
  131. $body = strip_tags($body);
  132. // Unescape HTML characters, to avoid splitting them in half
  133. $body = html_entity_decode($body, ENT_COMPAT, 'UTF-8');
  134. // calculate strlen() so we can add "..." after if needed
  135. $strlen = mb_strlen($body);
  136. $body = mb_substr($body, 0, $len);
  137. // Re-escape the characters.
  138. return '<em>' . utf8tohtml($body) . ($strlen > $len ? '&hellip;' : '') . '</em>';
  139. }
  140. function capcode($cap) {
  141. global $config;
  142. if (!$cap)
  143. return false;
  144. $capcode = array();
  145. if (isset($config['custom_capcode'][$cap])) {
  146. if (is_array($config['custom_capcode'][$cap])) {
  147. $capcode['cap'] = sprintf($config['custom_capcode'][$cap][0], $cap);
  148. if (isset($config['custom_capcode'][$cap][1]))
  149. $capcode['name'] = $config['custom_capcode'][$cap][1];
  150. if (isset($config['custom_capcode'][$cap][2]))
  151. $capcode['trip'] = $config['custom_capcode'][$cap][2];
  152. } else {
  153. $capcode['cap'] = sprintf($config['custom_capcode'][$cap], $cap);
  154. }
  155. } else {
  156. $capcode['cap'] = sprintf($config['capcode'], $cap);
  157. }
  158. return $capcode;
  159. }
  160. function truncate($body, $url, $max_lines = false, $max_chars = false) {
  161. global $config;
  162. if ($max_lines === false)
  163. $max_lines = $config['body_truncate'];
  164. if ($max_chars === false)
  165. $max_chars = $config['body_truncate_char'];
  166. // We don't want to risk truncating in the middle of an HTML comment.
  167. // It's easiest just to remove them all first.
  168. $body = preg_replace('/<!--.*?-->/s', '', $body);
  169. $original_body = $body;
  170. $lines = substr_count($body, '<br/>');
  171. // Limit line count
  172. if ($lines > $max_lines) {
  173. if (preg_match('/(((.*?)<br\/>){' . $max_lines . '})/', $body, $m))
  174. $body = $m[0];
  175. }
  176. $body = mb_substr($body, 0, $max_chars);
  177. if ($body != $original_body) {
  178. // Remove any corrupt tags at the end
  179. $body = preg_replace('/<([\w]+)?([^>]*)?$/', '', $body);
  180. // Open tags
  181. if (preg_match_all('/<([\w]+)[^>]*>/', $body, $open_tags)) {
  182. $tags = array();
  183. for ($x=0;$x<count($open_tags[0]);$x++) {
  184. if (!preg_match('/\/(\s+)?>$/', $open_tags[0][$x]))
  185. $tags[] = $open_tags[1][$x];
  186. }
  187. // List successfully closed tags
  188. if (preg_match_all('/(<\/([\w]+))>/', $body, $closed_tags)) {
  189. for ($x=0;$x<count($closed_tags[0]);$x++) {
  190. unset($tags[array_search($closed_tags[2][$x], $tags)]);
  191. }
  192. }
  193. // remove broken HTML entity at the end (if existent)
  194. $body = preg_replace('/&[^;]+$/', '', $body);
  195. $tags_no_close_needed = array("colgroup", "dd", "dt", "li", "optgroup", "option", "p", "tbody", "td", "tfoot", "th", "thead", "tr", "br", "img");
  196. // Close any open tags
  197. foreach ($tags as &$tag) {
  198. if (!in_array($tag, $tags_no_close_needed))
  199. $body .= "</{$tag}>";
  200. }
  201. } else {
  202. // remove broken HTML entity at the end (if existent)
  203. $body = preg_replace('/&[^;]*$/', '', $body);
  204. }
  205. $body .= '<span class="toolong">'.sprintf(_('Post too long. Click <a href="%s">here</a> to view the full text.'), $url).'</span>';
  206. }
  207. return $body;
  208. }
  209. function bidi_cleanup($data) {
  210. // Closes all embedded RTL and LTR unicode formatting blocks in a string so that
  211. // it can be used inside another without controlling its direction.
  212. $explicits = '\xE2\x80\xAA|\xE2\x80\xAB|\xE2\x80\xAD|\xE2\x80\xAE';
  213. $pdf = '\xE2\x80\xAC';
  214. preg_match_all("!$explicits!", $data, $m1, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
  215. preg_match_all("!$pdf!", $data, $m2, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
  216. if (count($m1) || count($m2)){
  217. $p = array();
  218. foreach ($m1 as $m){ $p[$m[0][1]] = 'push'; }
  219. foreach ($m2 as $m){ $p[$m[0][1]] = 'pop'; }
  220. ksort($p);
  221. $offset = 0;
  222. $stack = 0;
  223. foreach ($p as $pos => $type){
  224. if ($type == 'push'){
  225. $stack++;
  226. }else{
  227. if ($stack){
  228. $stack--;
  229. }else{
  230. # we have a pop without a push - remove it
  231. $data = substr($data, 0, $pos-$offset)
  232. .substr($data, $pos+3-$offset);
  233. $offset += 3;
  234. }
  235. }
  236. }
  237. # now add some pops if your stack is bigger than 0
  238. for ($i=0; $i<$stack; $i++){
  239. $data .= "\xE2\x80\xAC";
  240. }
  241. return $data;
  242. }
  243. return $data;
  244. }
  245. function secure_link_confirm($text, $title, $confirm_message, $href) {
  246. global $config;
  247. 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>';
  248. }
  249. function secure_link($href) {
  250. return $href . '/' . make_secure_link_token($href);
  251. }
  252. function embed_html($link) {
  253. global $config, $board;
  254. foreach ($config['embedding'] as $embed) {
  255. if (preg_match($config['youtube_regex'], $link))
  256. $embed[1] = sprintf($embed[1], $board['uri']);
  257. if ($html = preg_replace($embed[0], $embed[1], $link)) {
  258. if ($html == $link)
  259. continue; // Nope
  260. $html = str_replace('%%tb_width%%', $config['embed_width'], $html);
  261. $html = str_replace('%%tb_height%%', $config['embed_height'], $html);
  262. return $html;
  263. }
  264. }
  265. if ($link[0] == '<') {
  266. // Prior to v0.9.6-dev-8, HTML code for embedding was stored in the database instead of the link.
  267. return $link;
  268. }
  269. return 'Embedding error.';
  270. }
  271. class Post {
  272. public $clean;
  273. public function __construct($post, $root=null, $mod=false) {
  274. global $config;
  275. if (!isset($root))
  276. $root = &$config['root'];
  277. foreach ($post as $key => $value) {
  278. $this->{$key} = $value;
  279. }
  280. if (isset($this->files) && $this->files) {
  281. $this->files = is_string($this->files) ? json_decode($this->files) : $this->files;
  282. // Compatibility for posts before individual file hashing
  283. if ($this->files) {
  284. foreach ($this->files as $i => &$file) {
  285. if (empty($file)) {
  286. unset($this->files[$i]);
  287. continue;
  288. }
  289. if (!isset($file->hash))
  290. $file->hash = $this->filehash;
  291. }
  292. }
  293. }
  294. $this->subject = utf8tohtml($this->subject);
  295. $this->name = utf8tohtml($this->name);
  296. $this->mod = $mod;
  297. $this->root = $root;
  298. if ($this->embed)
  299. $this->embed = embed_html($this->embed);
  300. $this->modifiers = extract_modifiers($this->body_nomarkup);
  301. if ($config['always_regenerate_markup']) {
  302. $this->body = $this->body_nomarkup;
  303. markup($this->body);
  304. }
  305. if ($this->mod)
  306. // Fix internal links
  307. // Very complicated regex
  308. $this->body = preg_replace(
  309. '/<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',
  310. '<a $1href="?/$4',
  311. $this->body
  312. );
  313. }
  314. public function link($pre = '', $page = false) {
  315. global $config, $board;
  316. return $this->root . $board['dir'] . $config['dir']['res'] . sprintf(($page ? $page : $config['file_page']), $this->thread) . '#' . $pre . $this->id;
  317. }
  318. public function build($index=false) {
  319. global $board, $config;
  320. return Element('post_reply.html', array(
  321. 'config' => $config,
  322. 'board' => $board,
  323. 'post' => &$this,
  324. 'index' => $index,
  325. 'mod' => $this->mod,
  326. 'clean' => $this->getClean(),
  327. ));
  328. }
  329. public function getClean($actually_do = false) {
  330. global $board, $config;
  331. if( !isset( $this->clean ) && $actually_do ) {
  332. if ($config['cache']['enabled'] && $this->clean = cache::get("post_clean_{$board['uri']}_{$this->id}")) {
  333. return $this->clean;
  334. }
  335. $query = prepare("SELECT * FROM `post_clean` WHERE `post_id` = :post AND `board_id` = :board");
  336. $query->bindValue( ':board', $board['uri'] );
  337. $query->bindValue( ':post', $this->id );
  338. $query->execute() or error(db_error($query));
  339. if( !($this->clean = $query->fetch(PDO::FETCH_ASSOC)) ) {
  340. $this->clean = array(
  341. 'post_id' => $this->id,
  342. 'board_id' => $board['uri'],
  343. 'clean_local' => "0",
  344. 'clean_global' => "0",
  345. 'clean_local_mod_id' => null,
  346. 'clean_global_mod_id' => null,
  347. );
  348. if ($config['cache']['enabled'])
  349. cache::set("post_clean_{$board['uri']}_{$this->id}", $this->clean);
  350. }
  351. } else {
  352. $this->clean = array();
  353. }
  354. return $this->clean;
  355. }
  356. };
  357. class Thread extends Post {
  358. public function __construct($post, $root = null, $mod = false, $hr = true) {
  359. global $config;
  360. if (!isset($root))
  361. $root = &$config['root'];
  362. foreach ($post as $key => $value) {
  363. $this->{$key} = $value;
  364. }
  365. if (isset($this->files))
  366. $this->files = is_string($this->files) ? json_decode($this->files) : $this->files;
  367. $this->subject = utf8tohtml($this->subject);
  368. $this->name = utf8tohtml($this->name);
  369. $this->mod = $mod;
  370. $this->root = $root;
  371. $this->hr = $hr;
  372. $this->posts = array();
  373. $this->omitted = 0;
  374. $this->omitted_images = 0;
  375. if ($this->embed)
  376. $this->embed = embed_html($this->embed);
  377. $this->modifiers = extract_modifiers($this->body_nomarkup);
  378. if ($config['always_regenerate_markup']) {
  379. $this->body = $this->body_nomarkup;
  380. markup($this->body);
  381. }
  382. if ($this->mod)
  383. // Fix internal links
  384. // Very complicated regex
  385. $this->body = preg_replace(
  386. '/<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',
  387. '<a $1href="?/$4',
  388. $this->body
  389. );
  390. }
  391. public function link($pre = '', $page = false) {
  392. global $config, $board;
  393. return $this->root . $board['dir'] . $config['dir']['res'] . sprintf(($page ? $page : $config['file_page']), $this->id) . '#' . $pre . $this->id;
  394. }
  395. public function add(Post $post) {
  396. $this->posts[] = $post;
  397. }
  398. public function postCount() {
  399. return count($this->posts) + $this->omitted;
  400. }
  401. public function build($index=false, $isnoko50=false) {
  402. global $board, $config, $debug;
  403. $hasnoko50 = $this->postCount() >= $config['noko50_min'];
  404. event('show-thread', $this);
  405. $file = ($index && $config['file_board']) ? 'post_thread_fileboard.html' : 'post_thread.html';
  406. $built = Element($file, array(
  407. 'config' => $config,
  408. 'board' => $board,
  409. 'post' => &$this,
  410. 'index' => $index,
  411. 'hasnoko50' => $hasnoko50,
  412. 'isnoko50' => $isnoko50,
  413. 'mod' => $this->mod,
  414. 'clean' => $this->getClean(),
  415. ));
  416. return $built;
  417. }
  418. };