From c9423a2c34a5b93bf9cf2c506acca713781e60b6 Mon Sep 17 00:00:00 2001 From: Michael Save Date: Thu, 12 Apr 2012 02:49:22 +1000 Subject: large cleanup --- inc/display.php | 788 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 392 insertions(+), 396 deletions(-) (limited to 'inc/display.php') diff --git a/inc/display.php b/inc/display.php index ef6d1efc..9b118a00 100644 --- a/inc/display.php +++ b/inc/display.php @@ -1,445 +1,441 @@ = 1024 && $i < 4; $i++) $size /= 1024; + return round($size, 2).$units[$i]; +} + +function doBoardListPart($list, $root) { + global $config; + + $body = ''; + foreach($list as $board) { + if(is_array($board)) + $body .= ' [' . doBoardListPart($board, $root) . '] '; + else { + if(($key = array_search($board, $list)) && gettype($key) == 'string') { + $body .= ' ' . $key . ' /'; + } else { + $body .= ' ' . $board . ' /'; + } + } } + $body = preg_replace('/\/$/', '', $body); - /* - Stuff to help with the display. - */ + return $body; +} + +function createBoardlist($mod=false) { + global $config; + if(!isset($config['boards'])) return Array('top'=>'','bottom'=>''); - /* - joaoptm78@gmail.com - http://www.php.net/manual/en/function.filesize.php#100097 - */ - function format_bytes($size) { - $units = array(' B', ' KB', ' MB', ' GB', ' TB'); - for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024; - return round($size, 2).$units[$i]; - } + $body = doBoardListPart($config['boards'], $mod?'?/':$config['root']); + if(!preg_match('/\] $/', $body)) + $body = '[' . $body . ']'; - function commaize($n) { - $n = strval($n); - return (intval($n) < 1000) ? $n : commaize(substr($n, 0, -3)) . ',' . substr($n, -3); - } + $body = trim($body); - function doBoardListPart($list, $root) { - global $config; - - $body = ''; - foreach($list as $board) { - if(is_array($board)) - $body .= ' [' . doBoardListPart($board, $root) . '] '; - else { - if(($key = array_search($board, $list)) && gettype($key) == 'string') { - $body .= ' ' . $key . ' /'; - } else { - $body .= ' ' . $board . ' /'; - } - } - } - $body = preg_replace('/\/$/', '', $body); - - return $body; - } + return Array( + 'top' => '
' . $body . '
', + 'bottom' => '
' . $body . '
' + ); +} + +function error($message, $priority = true) { + global $board, $mod, $config; - function createBoardlist($mod=false) { - global $config; - - if(!isset($config['boards'])) return Array('top'=>'','bottom'=>''); - - $body = doBoardListPart($config['boards'], $mod?'?/':$config['root']); - if(!preg_match('/\] $/', $body)) - $body = '[' . $body . ']'; - - $body = trim($body); - - return Array( - 'top' => '
' . $body . '
', - 'bottom' => '
' . $body . '
' - ); + if($config['syslog'] && $priority !== false) { + // Use LOG_NOTICE instead of LOG_ERR or LOG_WARNING because most error message are not significant. + _syslog($priority !== true ? $priority : LOG_NOTICE, $message); } - function error($message, $priority = true) { - global $board, $mod, $config; - - if($config['syslog'] && $priority !== false) { - // Use LOG_NOTICE instead of LOG_ERR or LOG_WARNING because most error message are not significant. - _syslog($priority !== true ? $priority : LOG_NOTICE, $message); - } - - if(defined('STDIN')) { - // Running from CLI - die('Error: ' . $message . "\n"); - } - - die(Element('page.html', Array( - 'config'=>$config, - 'title'=>'Error', - 'subtitle'=>'An error has occured.', - 'body'=>'
' . - '

' . _($message) . '

' . - (isset($board) ? - "

Go back.

" : '') . - '
' - ))); + if(defined('STDIN')) { + // Running from CLI + die('Error: ' . $message . "\n"); } - function loginForm($error=false, $username=false, $redirect=false) { - global $config; - - die(Element('page.html', Array( - 'index'=>$config['root'], - 'title'=>_('Login'), + die(Element('page.html', Array( + 'config'=>$config, + 'title'=>'Error', + 'subtitle'=>'An error has occured.', + 'body'=>'
' . + '

' . _($message) . '

' . + (isset($board) ? + "

Go back.

" : '') . + '
' + ))); +} + +function loginForm($error=false, $username=false, $redirect=false) { + global $config; + + die(Element('page.html', Array( + 'index'=>$config['root'], + 'title'=>_('Login'), + 'config'=>$config, + 'body'=>Element('login.html', Array( 'config'=>$config, - 'body'=>Element('login.html', Array( - 'config'=>$config, - 'error'=>$error, - 'username'=>utf8tohtml($username), - 'redirect'=>$redirect - ) + 'error'=>$error, + 'username'=>utf8tohtml($username), + 'redirect'=>$redirect ) - ))); + ) + ))); +} + +function pm_snippet($body, $len=null) { + global $config; + + if(!isset($len)) + $len = &$config['mod']['snippet_length']; + + // Replace line breaks with some whitespace + $body = str_replace('
', ' ', $body); + + // Strip tags + $body = strip_tags($body); + + // Unescape HTML characters, to avoid splitting them in half + $body = html_entity_decode($body, ENT_COMPAT, 'UTF-8'); + + // calculate strlen() so we can add "..." after if needed + $strlen = mb_strlen($body); + + $body = substr($body, 0, $len); + + // Re-escape the characters. + return '' . utf8tohtml($body) . ($strlen > $len ? '…' : '') . ''; +} + +function capcode($cap) { + global $config; + + if(!$cap) + return false; + + $capcode = Array(); + if(isset($config['custom_capcode'][$cap])) { + if(is_array($config['custom_capcode'][$cap])) { + $capcode['cap'] = sprintf($config['custom_capcode'][$cap][0], $cap); + if(isset($config['custom_capcode'][$cap][1])) + $capcode['name'] = $config['custom_capcode'][$cap][1]; + if(isset($config['custom_capcode'][$cap][2])) + $capcode['trip'] = $config['custom_capcode'][$cap][2]; + } else { + $capcode['cap'] = sprintf($config['custom_capcode'][$cap], $cap); + } + } else { + $capcode['cap'] = sprintf($config['capcode'], $cap); } - function pm_snippet($body, $len=null) { - global $config; - - if(!isset($len)) - $len = &$config['mod']['snippet_length']; - - // Replace line breaks with some whitespace - $body = str_replace('
', ' ', $body); - - // Strip tags - $body = strip_tags($body); - - // Unescape HTML characters, to avoid splitting them in half - $body = html_entity_decode($body, ENT_COMPAT, 'UTF-8'); - - // calculate strlen() so we can add "..." after if needed - $strlen = mb_strlen($body); - - $body = substr($body, 0, $len); - - // Re-escape the characters. - return '' . utf8tohtml($body) . ($strlen > $len ? '…' : '') . ''; + return $capcode; +} + +function truncate($body, $url, $max_lines = false, $max_chars = false) { + global $config; + + if($max_lines === false) + $max_lines = $config['body_truncate']; + if($max_chars === false) + $max_chars = $config['body_truncate_char']; + $original_body = $body; + + $lines = substr_count($body, '
'); + + // Limit line count + if($lines > $max_lines) { + if(preg_match('/(((.*?)){' . $max_lines . '})/', $body, $m)) + $body = $m[0]; } - function capcode($cap) { - global $config; - - if(!$cap) - return false; + $body = substr($body, 0, $max_chars); + + if($body != $original_body) { + // Remove any corrupt tags at the end + $body = preg_replace('/<([\w]+)?([^>]*)?$/', '', $body); - $capcode = Array(); - if(isset($config['custom_capcode'][$cap])) { - if(is_array($config['custom_capcode'][$cap])) { - $capcode['cap'] = sprintf($config['custom_capcode'][$cap][0], $cap); - if(isset($config['custom_capcode'][$cap][1])) - $capcode['name'] = $config['custom_capcode'][$cap][1]; - if(isset($config['custom_capcode'][$cap][2])) - $capcode['trip'] = $config['custom_capcode'][$cap][2]; - } else { - $capcode['cap'] = sprintf($config['custom_capcode'][$cap], $cap); + // Open tags + if(preg_match_all('/<([\w]+)[^>]*>/', $body, $open_tags)) { + + $tags = Array(); + for($x=0;$x$/', $open_tags[0][$x])) + $tags[] = $open_tags[1][$x]; + } + + // List successfully closed tags + if(preg_match_all('/(<\/([\w]+))>/', $body, $closed_tags)) { + for($x=0;$xhere to view the full text.'; } - function truncate($body, $url, $max_lines = false, $max_chars = false) { + return $body; +} + +function confirmLink($text, $title, $confirm, $href) { + global $config, $mod; + if($config['mod']['server-side_confirm']) + return '' . $text . ''; + else + return '' . $text . ''; +} + +class Post { + public function __construct($id, $thread, $subject, $email, $name, $trip, $capcode, $body, $time, $thumb, $thumbx, $thumby, $file, $filex, $filey, $filesize, $filename, $ip, $embed, $root=null, $mod=false) { global $config; + if(!isset($root)) + $root = &$config['root']; - if($max_lines === false) - $max_lines = $config['body_truncate']; - if($max_chars === false) - $max_chars = $config['body_truncate_char']; - $original_body = $body; + $this->id = $id; + $this->thread = $thread; + $this->subject = utf8tohtml($subject); + $this->email = $email; + $this->name = utf8tohtml($name); + $this->trip = $trip; + $this->capcode = $capcode; + $this->body = $body; + $this->time = $time; + $this->thumb = $thumb; + $this->thumbx = $thumbx; + $this->thumby = $thumby; + $this->file = $file; + $this->filex = $filex; + $this->filey = $filey; + $this->filesize = $filesize; + $this->filename = $filename; + $this->ip = $ip; + $this->embed = $embed; + $this->root = $root; + $this->mod = $mod; - $lines = substr_count($body, '
'); - - // Limit line count - if($lines > $max_lines) { - if(preg_match('/(((.*?)){' . $max_lines . '})/', $body, $m)) - $body = $m[0]; - } + if($this->mod) + // Fix internal links + // Very complicated regex + $this->body = preg_replace( + '/"; - } - } else { - // remove broken HTML entity at the end (if existent) - $body = preg_replace('/&[^;]+$/', '', $body); - } + // Delete + if(hasPermission($config['mod']['delete'], $board['uri'], $this->mod)) + $built .= ' ' . confirmLink($config['mod']['link_delete'], 'Delete', 'Are you sure you want to delete this?', $board['uri'] . '/delete/' . $this->id); + + // Delete all posts by IP + if(hasPermission($config['mod']['deletebyip'], $board['uri'], $this->mod)) + $built .= ' ' . confirmLink($config['mod']['link_deletebyip'], 'Delete all posts by IP', 'Are you sure you want to delete all posts by this IP address?', $board['uri'] . '/deletebyip/' . $this->id); + + // Delete all posts by IP (global) + if(hasPermission($config['mod']['deletebyip_global'], $board['uri'], $this->mod)) + $built .= ' ' . confirmLink($config['mod']['link_deletebyip_global'], 'Delete all posts by IP across all boards', 'Are you sure you want to delete all posts by this IP address, across all boards?', $board['uri'] . '/deletebyip/' . $this->id . '/global'); + + // Ban + if(hasPermission($config['mod']['ban'], $board['uri'], $this->mod)) + $built .= ' ' . $config['mod']['link_ban'] . ''; + + // Ban & Delete + if(hasPermission($config['mod']['bandelete'], $board['uri'], $this->mod)) + $built .= ' ' . $config['mod']['link_bandelete'] . ''; - $body .= 'Post too long. Click here to view the full text.'; + // Delete file (keep post) + if(!empty($this->file) && hasPermission($config['mod']['deletefile'], $board['uri'], $this->mod)) + $built .= ' ' . $config['mod']['link_deletefile'] . ''; + + // Edit post + if(hasPermission($config['mod']['editpost'], $board['uri'], $this->mod)) + $built .= ' ' . $config['mod']['link_editpost'] . ''; + + if(!empty($built)) + $built = '' . $built . ''; } - - return $body; + return $built; } - function confirmLink($text, $title, $confirm, $href) { - global $config, $mod; - if($config['mod']['server-side_confirm']) - return '' . $text . ''; - else - return '' . $text . ''; + public function build($index=false) { + global $board, $config; + + return Element('post_reply.html', Array('config' => $config, 'board' => $board, 'post' => &$this, 'index' => $index)); } - - class Post { - public function __construct($id, $thread, $subject, $email, $name, $trip, $capcode, $body, $time, $thumb, $thumbx, $thumby, $file, $filex, $filey, $filesize, $filename, $ip, $embed, $root=null, $mod=false) { - global $config; - if(!isset($root)) $root = &$config['root']; +}; + +class Thread { + public function __construct($id, $subject, $email, $name, $trip, $capcode, $body, $time, $thumb, $thumbx, $thumby, $file, $filex, $filey, $filesize, $filename, $ip, $sticky, $locked, $bumplocked, $embed, $root=null, $mod=false, $hr=true) { + global $config; + if(!isset($root)) + $root = &$config['root']; + + $this->id = $id; + $this->subject = utf8tohtml($subject); + $this->email = $email; + $this->name = utf8tohtml($name); + $this->trip = $trip; + $this->capcode = $capcode; + $this->body = $body; + $this->time = $time; + $this->thumb = $thumb; + $this->thumbx = $thumbx; + $this->thumby = $thumby; + $this->file = $file; + $this->filex = $filex; + $this->filey = $filey; + $this->filesize = $filesize; + $this->filename = $filename; + $this->omitted = 0; + $this->omitted_images = 0; + $this->posts = Array(); + $this->ip = $ip; + $this->sticky = $sticky; + $this->locked = $locked; + $this->bumplocked = $bumplocked; + $this->embed = $embed; + $this->root = $root; + $this->mod = $mod; + $this->hr = $hr; + + if($this->mod) + // Fix internal links + // Very complicated regex + $this->body = preg_replace( + '/' . $config['mod']['link_ban'] . ''; - $built = ''; - if($this->mod) { - // Mod controls (on posts) - - // Delete - if(hasPermission($config['mod']['delete'], $board['uri'], $this->mod)) - $built .= ' ' . confirmLink($config['mod']['link_delete'], 'Delete', 'Are you sure you want to delete this?', $board['uri'] . '/delete/' . $this->id); - - // Delete all posts by IP - if(hasPermission($config['mod']['deletebyip'], $board['uri'], $this->mod)) - $built .= ' ' . confirmLink($config['mod']['link_deletebyip'], 'Delete all posts by IP', 'Are you sure you want to delete all posts by this IP address?', $board['uri'] . '/deletebyip/' . $this->id); - - // Delete all posts by IP (global) - if(hasPermission($config['mod']['deletebyip_global'], $board['uri'], $this->mod)) - $built .= ' ' . confirmLink($config['mod']['link_deletebyip_global'], 'Delete all posts by IP across all boards', 'Are you sure you want to delete all posts by this IP address, across all boards?', $board['uri'] . '/deletebyip/' . $this->id . '/global'); - - // Ban - if(hasPermission($config['mod']['ban'], $board['uri'], $this->mod)) - $built .= ' ' . $config['mod']['link_ban'] . ''; - - // Ban & Delete - if(hasPermission($config['mod']['bandelete'], $board['uri'], $this->mod)) - $built .= ' ' . $config['mod']['link_bandelete'] . ''; - - // Delete file (keep post) - if(!empty($this->file) && hasPermission($config['mod']['deletefile'], $board['uri'], $this->mod)) - $built .= ' ' . $config['mod']['link_deletefile'] . ''; - - // Edit post - if(hasPermission($config['mod']['editpost'], $board['uri'], $this->mod)) - $built .= ' ' . $config['mod']['link_editpost'] . ''; - - if(!empty($built)) - $built = '' . $built . ''; - } - return $built; - } - - public function build($index=false) { - global $board, $config; + // Ban & Delete + if(hasPermission($config['mod']['bandelete'], $board['uri'], $this->mod)) + $built .= ' ' . $config['mod']['link_bandelete'] . ''; - return Element('post_reply.html', Array('config' => $config, 'board' => $board, 'post' => &$this, 'index' => $index)); - } - }; - - class Thread { - public function __construct($id, $subject, $email, $name, $trip, $capcode, $body, $time, $thumb, $thumbx, $thumby, $file, $filex, $filey, $filesize, $filename, $ip, $sticky, $locked, $bumplocked, $embed, $root=null, $mod=false, $hr=true) { - global $config; - if(!isset($root)) $root = &$config['root']; + // Delete file (keep post) + if(!empty($this->file) && $this->file != 'deleted' && hasPermission($config['mod']['deletefile'], $board['uri'], $this->mod)) + $built .= ' ' . $config['mod']['link_deletefile'] . ''; - $this->id = $id; - $this->subject = utf8tohtml($subject); - $this->email = $email; - $this->name = utf8tohtml($name); - $this->trip = $trip; - $this->capcode = $capcode; - $this->body = $body; - $this->time = $time; - $this->thumb = $thumb; - $this->thumbx = $thumbx; - $this->thumby = $thumby; - $this->file = $file; - $this->filex = $filex; - $this->filey = $filey; - $this->filesize = $filesize; - $this->filename = $filename; - $this->omitted = 0; - $this->omitted_images = 0; - $this->posts = Array(); - $this->ip = $ip; - $this->sticky = $sticky; - $this->locked = $locked; - $this->bumplocked = $bumplocked; - $this->embed = $embed; - $this->root = $root; - $this->mod = $mod; - $this->hr = $hr; + // Sticky + if(hasPermission($config['mod']['sticky'], $board['uri'], $this->mod)) + if($this->sticky) + $built .= ' ' . $config['mod']['link_desticky'] . ''; + else + $built .= ' ' . $config['mod']['link_sticky'] . ''; - if($this->mod) - // Fix internal links - // Very complicated regex - $this->body = preg_replace( - '/' . $config['mod']['link_bumpunlock'] . ''; + else + $built .= ' ' . $config['mod']['link_bumplock'] . ''; - return $this->root . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $this->id) . '#' . $pre . $this->id; - } - public function add(Post $post) { - $this->posts[] = $post; - } - public function postControls() { - global $board, $config; + // Lock + if(hasPermission($config['mod']['lock'], $board['uri'], $this->mod)) + if($this->locked) + $built .= ' ' . $config['mod']['link_unlock'] . ''; + else + $built .= ' ' . $config['mod']['link_lock'] . ''; - $built = ''; - if($this->mod) { - // Mod controls (on posts) - // Delete - if(hasPermission($config['mod']['delete'], $board['uri'], $this->mod)) - $built .= ' ' . confirmLink($config['mod']['link_delete'], 'Delete', 'Are you sure you want to delete this?', $board['uri'] . '/delete/' . $this->id); - - // Delete all posts by IP - if(hasPermission($config['mod']['deletebyip'], $board['uri'], $this->mod)) - $built .= ' ' . confirmLink($config['mod']['link_deletebyip'], 'Delete all posts by IP', 'Are you sure you want to delete all posts by this IP address?', $board['uri'] . '/deletebyip/' . $this->id); - - // Delete all posts by IP (global) - if(hasPermission($config['mod']['deletebyip_global'], $board['uri'], $this->mod)) - $built .= ' ' . confirmLink($config['mod']['link_deletebyip_global'], 'Delete all posts by IP across all boards', 'Are you sure you want to delete all posts by this IP address, across all boards?', $board['uri'] . '/deletebyip/' . $this->id . '/global'); - - // Ban - if(hasPermission($config['mod']['ban'], $board['uri'], $this->mod)) - $built .= ' ' . $config['mod']['link_ban'] . ''; - - // Ban & Delete - if(hasPermission($config['mod']['bandelete'], $board['uri'], $this->mod)) - $built .= ' ' . $config['mod']['link_bandelete'] . ''; - - // Delete file (keep post) - if(!empty($this->file) && $this->file != 'deleted' && hasPermission($config['mod']['deletefile'], $board['uri'], $this->mod)) - $built .= ' ' . $config['mod']['link_deletefile'] . ''; - - // Sticky - if(hasPermission($config['mod']['sticky'], $board['uri'], $this->mod)) - if($this->sticky) - $built .= ' ' . $config['mod']['link_desticky'] . ''; - else - $built .= ' ' . $config['mod']['link_sticky'] . ''; - - if(hasPermission($config['mod']['bumplock'], $board['uri'], $this->mod)) - if($this->bumplocked) - $built .= ' ' . $config['mod']['link_bumpunlock'] . ''; - else - $built .= ' ' . $config['mod']['link_bumplock'] . ''; - - // Lock - if(hasPermission($config['mod']['lock'], $board['uri'], $this->mod)) - if($this->locked) - $built .= ' ' . $config['mod']['link_unlock'] . ''; - else - $built .= ' ' . $config['mod']['link_lock'] . ''; - - if(hasPermission($config['mod']['move'], $board['uri'], $this->mod)) - $built .= ' ' . $config['mod']['link_move'] . ''; - - // Edit post - if(hasPermission($config['mod']['editpost'], $board['uri'], $this->mod)) - $built .= ' ' . $config['mod']['link_editpost'] . ''; - - if(!empty($built)) - $built = '' . $built . ''; - } - return $built; - } - - public function ratio() { - return fraction($this->filex, $this->filey, ':'); - } - - public function build($index=false) { - global $board, $config, $debug; + if(hasPermission($config['mod']['move'], $board['uri'], $this->mod)) + $built .= ' ' . $config['mod']['link_move'] . ''; - $built = Element('post_thread.html', Array('config' => $config, 'board' => $board, 'post' => &$this, 'index' => $index)); + // Edit post + if(hasPermission($config['mod']['editpost'], $board['uri'], $this->mod)) + $built .= ' ' . $config['mod']['link_editpost'] . ''; - if(!$this->mod && $index && $config['cache']['enabled']) { - cache::set($this->cache_key($index), $built); - } - - return $built; + if(!empty($built)) + $built = '' . $built . ''; } - function cache_key($index) { - global $board; - - return 'thread_' . ($index ? 'index_' : '') . $board['uri'] . '_' . $this->id; + return $built; + } + + public function ratio() { + return fraction($this->filex, $this->filey, ':'); + } + + public function build($index=false) { + global $board, $config, $debug; + + $built = Element('post_thread.html', Array('config' => $config, 'board' => $board, 'post' => &$this, 'index' => $index)); + + if(!$this->mod && $index && $config['cache']['enabled']) { + cache::set($this->cache_key($index), $built); } - }; -?> + + return $built; + } + function cache_key($index) { + global $board; + + return 'thread_' . ($index ? 'index_' : '') . $board['uri'] . '_' . $this->id; + } +}; + -- cgit v1.3