1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-12-18 02:16:03 +01:00
vichan/inc/display.php

446 lines
16 KiB
PHP
Raw Normal View History

2010-11-02 11:57:33 +01:00
<?php
2012-04-11 18:49:22 +02:00
/*
* Copyright (c) 2010-2012 Tinyboard Development Group
*/
2012-04-12 16:18:19 +02:00
if (realpath($_SERVER['SCRIPT_FILENAME']) == str_replace('\\', '/', __FILE__)) {
2012-04-11 18:49:22 +02:00
// You cannot request this file directly.
exit;
}
/*
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];
}
function doBoardListPart($list, $root) {
global $config;
$body = '';
2012-04-12 16:18:19 +02:00
foreach ($list as $board) {
if (is_array($board))
2012-04-11 18:49:22 +02:00
$body .= ' [' . doBoardListPart($board, $root) . '] ';
else {
2012-04-12 16:18:19 +02:00
if (($key = array_search($board, $list)) && gettype($key) == 'string') {
2012-04-11 18:49:22 +02:00
$body .= ' <a href="' . $board . '">' . $key . '</a> /';
} else {
$body .= ' <a href="' . $root . $board . '/' . $config['file_index'] . '">' . $board . '</a> /';
}
}
2011-04-13 14:21:07 +02:00
}
2012-04-11 18:49:22 +02:00
$body = preg_replace('/\/$/', '', $body);
2011-04-13 14:21:07 +02:00
2012-04-11 18:49:22 +02:00
return $body;
}
function createBoardlist($mod=false) {
global $config;
2010-11-02 11:57:33 +01:00
2012-08-27 13:50:15 +02:00
if (!isset($config['boards'])) return array('top'=>'','bottom'=>'');
2010-11-02 11:57:33 +01:00
2012-04-11 18:49:22 +02:00
$body = doBoardListPart($config['boards'], $mod?'?/':$config['root']);
2012-04-12 16:18:19 +02:00
if (!preg_match('/\] $/', $body))
2012-04-11 18:49:22 +02:00
$body = '[' . $body . ']';
2010-11-02 11:57:33 +01:00
2012-04-11 18:49:22 +02:00
$body = trim($body);
2011-02-19 10:16:13 +01:00
2012-08-27 13:50:15 +02:00
return array(
2012-04-11 18:49:22 +02:00
'top' => '<div class="boardlist">' . $body . '</div>',
'bottom' => '<div class="boardlist bottom">' . $body . '</div>'
);
}
function error($message, $priority = true) {
global $board, $mod, $config;
2011-02-19 10:16:13 +01:00
2012-04-12 16:18:19 +02:00
if ($config['syslog'] && $priority !== false) {
2012-04-11 18:49:22 +02:00
// Use LOG_NOTICE instead of LOG_ERR or LOG_WARNING because most error message are not significant.
_syslog($priority !== true ? $priority : LOG_NOTICE, $message);
2011-02-19 10:16:13 +01:00
}
2011-11-18 13:39:13 +01:00
2012-04-12 16:18:19 +02:00
if (defined('STDIN')) {
2012-04-11 18:49:22 +02:00
// Running from CLI
die('Error: ' . $message . "\n");
2010-11-02 11:57:33 +01:00
}
2012-08-27 13:50:15 +02:00
die(Element('page.html', array(
2012-04-11 18:49:22 +02:00
'config'=>$config,
'title'=>'Error',
'subtitle'=>'An error has occured.',
'body'=>'<center>' .
'<h2>' . _($message) . '</h2>' .
(isset($board) ?
"<p><a href=\"" . $config['root'] .
($mod ? $config['file_mod'] . '?/' : '') .
$board['dir'] . $config['file_index'] . "\">Go back</a>.</p>" : '') .
'</center>'
)));
}
function loginForm($error=false, $username=false, $redirect=false) {
global $config;
2012-08-27 07:19:05 +02:00
die(Element('page.html', array(
'index' => $config['root'],
'title' => _('Login'),
'config' => $config,
'body' => Element('login.html', array(
2011-03-26 09:11:48 +01:00
'config'=>$config,
2012-04-11 18:49:22 +02:00
'error'=>$error,
'username'=>utf8tohtml($username),
'redirect'=>$redirect
2010-12-01 11:24:14 +01:00
)
2012-04-11 18:49:22 +02:00
)
)));
}
function pm_snippet($body, $len=null) {
global $config;
2012-04-12 16:18:19 +02:00
if (!isset($len))
2012-04-11 18:49:22 +02:00
$len = &$config['mod']['snippet_length'];
// Replace line breaks with some whitespace
$body = str_replace('<br/>', ' ', $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 '<em>' . utf8tohtml($body) . ($strlen > $len ? '&hellip;' : '') . '</em>';
}
function capcode($cap) {
global $config;
2012-04-12 16:18:19 +02:00
if (!$cap)
2012-04-11 18:49:22 +02:00
return false;
2012-08-27 13:50:15 +02:00
$capcode = array();
2012-04-12 16:18:19 +02:00
if (isset($config['custom_capcode'][$cap])) {
if (is_array($config['custom_capcode'][$cap])) {
2012-04-11 18:49:22 +02:00
$capcode['cap'] = sprintf($config['custom_capcode'][$cap][0], $cap);
2012-04-12 16:18:19 +02:00
if (isset($config['custom_capcode'][$cap][1]))
2012-04-11 18:49:22 +02:00
$capcode['name'] = $config['custom_capcode'][$cap][1];
2012-04-12 16:18:19 +02:00
if (isset($config['custom_capcode'][$cap][2]))
2012-04-11 18:49:22 +02:00
$capcode['trip'] = $config['custom_capcode'][$cap][2];
} else {
$capcode['cap'] = sprintf($config['custom_capcode'][$cap], $cap);
}
} else {
$capcode['cap'] = sprintf($config['capcode'], $cap);
2010-12-01 11:24:14 +01:00
}
2012-04-11 18:49:22 +02:00
return $capcode;
}
function truncate($body, $url, $max_lines = false, $max_chars = false) {
global $config;
2012-04-12 16:18:19 +02:00
if ($max_lines === false)
2012-04-11 18:49:22 +02:00
$max_lines = $config['body_truncate'];
2012-04-12 16:18:19 +02:00
if ($max_chars === false)
2012-04-11 18:49:22 +02:00
$max_chars = $config['body_truncate_char'];
$original_body = $body;
$lines = substr_count($body, '<br/>');
// Limit line count
2012-04-12 16:18:19 +02:00
if ($lines > $max_lines) {
if (preg_match('/(((.*?)<br\/>){' . $max_lines . '})/', $body, $m))
2012-04-11 18:49:22 +02:00
$body = $m[0];
2011-04-12 10:02:20 +02:00
}
2012-04-11 18:49:22 +02:00
$body = substr($body, 0, $max_chars);
2012-04-12 16:18:19 +02:00
if ($body != $original_body) {
2012-04-11 18:49:22 +02:00
// Remove any corrupt tags at the end
$body = preg_replace('/<([\w]+)?([^>]*)?$/', '', $body);
2011-12-05 00:33:31 +01:00
2012-04-11 18:49:22 +02:00
// Open tags
2012-04-12 16:18:19 +02:00
if (preg_match_all('/<([\w]+)[^>]*>/', $body, $open_tags)) {
2012-04-11 18:49:22 +02:00
2012-08-27 13:50:15 +02:00
$tags = array();
2012-04-12 16:18:19 +02:00
for ($x=0;$x<count($open_tags[0]);$x++) {
if (!preg_match('/\/(\s+)?>$/', $open_tags[0][$x]))
2012-04-11 18:49:22 +02:00
$tags[] = $open_tags[1][$x];
}
// List successfully closed tags
2012-04-12 16:18:19 +02:00
if (preg_match_all('/(<\/([\w]+))>/', $body, $closed_tags)) {
for ($x=0;$x<count($closed_tags[0]);$x++) {
2012-04-11 18:49:22 +02:00
unset($tags[array_search($closed_tags[2][$x], $tags)]);
}
}
// remove broken HTML entity at the end (if existent)
$body = preg_replace('/&[^;]+$/', '', $body);
$tags_no_close_needed = array("colgroup", "dd", "dt", "li", "optgroup", "option", "p", "tbody", "td", "tfoot", "th", "thead", "tr", "br", "img");
2012-04-11 18:49:22 +02:00
// Close any open tags
2012-04-12 16:18:19 +02:00
foreach ($tags as &$tag) {
if (!in_array($tag, $tags_no_close_needed))
$body .= "</{$tag}>";
2011-12-05 00:33:31 +01:00
}
} else {
2012-04-11 18:49:22 +02:00
// remove broken HTML entity at the end (if existent)
$body = preg_replace('/&[^;]+$/', '', $body);
}
2012-04-11 18:49:22 +02:00
$body .= '<span class="toolong">Post too long. Click <a href="' . $url . '">here</a> to view the full text.</span>';
}
2012-04-11 18:49:22 +02:00
return $body;
}
2012-08-27 07:19:05 +02:00
function secure_link_confirm($text, $title, $confirm_message, $href) {
global $config;
return '<a onclick="if (confirm(\'' . htmlentities(addslashes($confirm_message)) . '\')) document.location=\'?/' . htmlentities(addslashes($href . '/' . make_secure_link_token($href))) . '\';return false;" title="' . htmlentities($title) . '" href="?/' . $href . '">' . $text . '</a>';
}
function secure_link($href) {
return $href . '/' . make_secure_link_token($href);
2012-04-11 18:49:22 +02:00
}
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) {
2011-04-17 07:28:15 +02:00
global $config;
2012-04-12 16:18:19 +02:00
if (!isset($root))
2012-04-11 18:49:22 +02:00
$root = &$config['root'];
2012-04-11 18:49:22 +02:00
$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;
2011-04-17 07:28:15 +02:00
2012-04-12 16:18:19 +02:00
if ($this->mod)
2012-04-11 18:49:22 +02:00
// Fix internal links
// Very complicated regex
$this->body = preg_replace(
'/<a((([a-zA-Z]+="[^"]+")|[a-zA-Z]+=[a-zA-Z]+|\s)*)href="' . preg_quote($config['root'], '/') . '(' . sprintf(preg_quote($config['board_path'], '/'), '\w+') . ')/',
'<a $1href="?/$4',
$this->body
);
}
public function link($pre = '') {
global $config, $board;
2011-04-17 07:28:15 +02:00
2012-04-11 18:49:22 +02:00
return $this->root . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $this->thread) . '#' . $pre . $this->id;
}
public function postControls() {
global $board, $config;
2011-04-17 07:28:15 +02:00
2012-04-11 18:49:22 +02:00
$built = '';
2012-04-12 16:18:19 +02:00
if ($this->mod) {
2012-04-11 18:49:22 +02:00
// Mod controls (on posts)
2011-04-17 07:28:15 +02:00
2012-04-11 18:49:22 +02:00
// Delete
2012-04-12 16:18:19 +02:00
if (hasPermission($config['mod']['delete'], $board['uri'], $this->mod))
2012-08-27 07:19:05 +02:00
$built .= ' ' . secure_link_confirm($config['mod']['link_delete'], 'Delete', 'Are you sure you want to delete this?', $board['uri'] . '/delete/' . $this->id);
2012-04-11 18:49:22 +02:00
// Delete all posts by IP
2012-04-12 16:18:19 +02:00
if (hasPermission($config['mod']['deletebyip'], $board['uri'], $this->mod))
2012-08-27 07:19:05 +02:00
$built .= ' ' . secure_link_confirm($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);
2012-04-11 18:49:22 +02:00
// Delete all posts by IP (global)
2012-04-12 16:18:19 +02:00
if (hasPermission($config['mod']['deletebyip_global'], $board['uri'], $this->mod))
2012-08-27 07:19:05 +02:00
$built .= ' ' . secure_link_confirm($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');
2012-04-11 18:49:22 +02:00
// Ban
2012-04-12 16:18:19 +02:00
if (hasPermission($config['mod']['ban'], $board['uri'], $this->mod))
2012-04-11 18:49:22 +02:00
$built .= ' <a title="Ban" href="?/' . $board['uri'] . '/ban/' . $this->id . '">' . $config['mod']['link_ban'] . '</a>';
// Ban & Delete
2012-04-12 16:18:19 +02:00
if (hasPermission($config['mod']['bandelete'], $board['uri'], $this->mod))
2012-04-11 18:49:22 +02:00
$built .= ' <a title="Ban & Delete" href="?/' . $board['uri'] . '/ban&amp;delete/' . $this->id . '">' . $config['mod']['link_bandelete'] . '</a>';
2012-04-11 18:49:22 +02:00
// Delete file (keep post)
2012-04-12 16:18:19 +02:00
if (!empty($this->file) && hasPermission($config['mod']['deletefile'], $board['uri'], $this->mod))
2012-09-27 20:00:13 +02:00
$built .= ' ' . secure_link_confirm($config['mod']['link_deletefile'], 'Delete file', 'Are you sure you want to delete this file?', $board['uri'] . '/deletefile/' . $this->id);
2012-04-11 18:49:22 +02:00
// Edit post
2012-04-12 16:18:19 +02:00
if (hasPermission($config['mod']['editpost'], $board['uri'], $this->mod))
2012-04-11 18:49:22 +02:00
$built .= ' <a title="Edit post" href="?/' . $board['uri'] . '/edit/' . $this->id . '">' . $config['mod']['link_editpost'] . '</a>';
2012-04-12 16:18:19 +02:00
if (!empty($built))
2012-04-11 18:49:22 +02:00
$built = '<span class="controls">' . $built . '</span>';
2011-04-17 07:28:15 +02:00
}
2012-04-11 18:49:22 +02:00
return $built;
2011-04-17 07:28:15 +02:00
}
2012-04-11 18:49:22 +02:00
public function build($index=false) {
global $board, $config;
2012-08-27 13:50:15 +02:00
return Element('post_reply.html', array('config' => $config, 'board' => $board, 'post' => &$this, 'index' => $index));
}
2012-04-11 18:49:22 +02:00
};
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;
2012-04-12 16:18:19 +02:00
if (!isset($root))
2012-04-11 18:49:22 +02:00
$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;
2012-08-27 13:50:15 +02:00
$this->posts = array();
2012-04-11 18:49:22 +02:00
$this->ip = $ip;
$this->sticky = $sticky;
$this->locked = $locked;
$this->bumplocked = $bumplocked;
$this->embed = $embed;
$this->root = $root;
$this->mod = $mod;
$this->hr = $hr;
2012-04-12 16:18:19 +02:00
if ($this->mod)
2012-04-11 18:49:22 +02:00
// Fix internal links
// Very complicated regex
$this->body = preg_replace(
'/<a(([a-zA-Z]+="[^"]+")|[a-zA-Z]+=[a-zA-Z]+|\s)*href="' . preg_quote($config['root'], '/') . '(' . sprintf(preg_quote($config['board_path'], '/'), '\w+') . ')/',
'<a href="?/$3',
$this->body
);
}
public function link($pre = '') {
global $config, $board;
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;
$built = '';
2012-04-12 16:18:19 +02:00
if ($this->mod) {
2012-04-11 18:49:22 +02:00
// Mod controls (on posts)
// Delete
2012-04-12 16:18:19 +02:00
if (hasPermission($config['mod']['delete'], $board['uri'], $this->mod))
2012-08-27 07:19:05 +02:00
$built .= ' ' . secure_link_confirm($config['mod']['link_delete'], 'Delete', 'Are you sure you want to delete this?', $board['uri'] . '/delete/' . $this->id);
2011-02-12 07:25:15 +01:00
2012-04-11 18:49:22 +02:00
// Delete all posts by IP
2012-04-12 16:18:19 +02:00
if (hasPermission($config['mod']['deletebyip'], $board['uri'], $this->mod))
2012-08-27 07:19:05 +02:00
$built .= ' ' . secure_link_confirm($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);
2012-04-11 18:49:22 +02:00
// Delete all posts by IP (global)
2012-04-12 16:18:19 +02:00
if (hasPermission($config['mod']['deletebyip_global'], $board['uri'], $this->mod))
2012-08-27 07:19:05 +02:00
$built .= ' ' . secure_link_confirm($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');
2011-04-17 07:28:15 +02:00
2012-04-11 18:49:22 +02:00
// Ban
2012-04-12 16:18:19 +02:00
if (hasPermission($config['mod']['ban'], $board['uri'], $this->mod))
2012-04-11 18:49:22 +02:00
$built .= ' <a title="Ban" href="?/' . $board['uri'] . '/ban/' . $this->id . '">' . $config['mod']['link_ban'] . '</a>';
2011-01-18 06:48:39 +01:00
2012-04-11 18:49:22 +02:00
// Ban & Delete
2012-04-12 16:18:19 +02:00
if (hasPermission($config['mod']['bandelete'], $board['uri'], $this->mod))
2012-04-11 18:49:22 +02:00
$built .= ' <a title="Ban & Delete" href="?/' . $board['uri'] . '/ban&amp;delete/' . $this->id . '">' . $config['mod']['link_bandelete'] . '</a>';
2011-05-19 13:50:19 +02:00
2012-04-11 18:49:22 +02:00
// Delete file (keep post)
2012-04-12 16:18:19 +02:00
if (!empty($this->file) && $this->file != 'deleted' && hasPermission($config['mod']['deletefile'], $board['uri'], $this->mod))
2012-09-27 20:00:13 +02:00
$built .= ' ' . secure_link_confirm($config['mod']['link_deletefile'], 'Delete file', 'Are you sure you want to delete this file?', $board['uri'] . '/deletefile/' . $this->id);
2011-02-12 07:25:15 +01:00
2012-04-11 18:49:22 +02:00
// Sticky
2012-04-12 16:18:19 +02:00
if (hasPermission($config['mod']['sticky'], $board['uri'], $this->mod))
if ($this->sticky)
2012-09-30 14:56:09 +02:00
$built .= ' <a title="Make thread not sticky" href="?/' . secure_link($board['uri'] . '/unsticky/' . $this->id) . '">' . $config['mod']['link_desticky'] . '</a>';
2012-04-11 18:49:22 +02:00
else
2012-09-30 14:56:09 +02:00
$built .= ' <a title="Make thread sticky" href="?/' . secure_link($board['uri'] . '/sticky/' . $this->id) . '">' . $config['mod']['link_sticky'] . '</a>';
2012-04-12 16:18:19 +02:00
if (hasPermission($config['mod']['bumplock'], $board['uri'], $this->mod))
if ($this->bumplocked)
2012-08-27 07:19:05 +02:00
$built .= ' <a title="Allow thread to be bumped" href="?/' . secure_link($board['uri'] . '/bumpunlock/' . $this->id) . '">' . $config['mod']['link_bumpunlock'] . '</a>';
2012-04-11 18:49:22 +02:00
else
2012-08-27 07:19:05 +02:00
$built .= ' <a title="Prevent thread from being bumped" href="?/' . secure_link($board['uri'] . '/bumplock/' . $this->id) . '">' . $config['mod']['link_bumplock'] . '</a>';
2011-04-17 07:28:15 +02:00
2012-04-11 18:49:22 +02:00
// Lock
2012-04-12 16:18:19 +02:00
if (hasPermission($config['mod']['lock'], $board['uri'], $this->mod))
if ($this->locked)
2012-08-27 07:19:05 +02:00
$built .= ' <a title="Unlock thread" href="?/' . secure_link($board['uri'] . '/unlock/' . $this->id) . '">' . $config['mod']['link_unlock'] . '</a>';
2012-04-11 18:49:22 +02:00
else
2012-08-27 07:19:05 +02:00
$built .= ' <a title="Lock thread" href="?/' . secure_link($board['uri'] . '/lock/' . $this->id) . '">' . $config['mod']['link_lock'] . '</a>';
2011-01-18 06:48:39 +01:00
2012-04-12 16:18:19 +02:00
if (hasPermission($config['mod']['move'], $board['uri'], $this->mod))
2012-04-11 18:49:22 +02:00
$built .= ' <a title="Move thread to another board" href="?/' . $board['uri'] . '/move/' . $this->id . '">' . $config['mod']['link_move'] . '</a>';
2012-04-11 18:49:22 +02:00
// Edit post
2012-04-12 16:18:19 +02:00
if (hasPermission($config['mod']['editpost'], $board['uri'], $this->mod))
2012-04-11 18:49:22 +02:00
$built .= ' <a title="Edit post" href="?/' . $board['uri'] . '/edit/' . $this->id . '">' . $config['mod']['link_editpost'] . '</a>';
2010-11-04 07:16:47 +01:00
2012-04-12 16:18:19 +02:00
if (!empty($built))
2012-04-11 18:49:22 +02:00
$built = '<span class="controls op">' . $built . '</span>';
2010-11-02 11:57:33 +01:00
}
2012-04-11 18:49:22 +02:00
return $built;
}
public function ratio() {
return fraction($this->filex, $this->filey, ':');
}
public function build($index=false) {
global $board, $config, $debug;
2012-08-27 13:50:15 +02:00
$built = Element('post_thread.html', array('config' => $config, 'board' => $board, 'post' => &$this, 'index' => $index));
2012-04-11 18:49:22 +02:00
2012-04-12 16:18:19 +02:00
if (!$this->mod && $index && $config['cache']['enabled']) {
2012-04-11 18:49:22 +02:00
cache::set($this->cache_key($index), $built);
}
2012-04-11 18:49:22 +02:00
return $built;
}
function cache_key($index) {
global $board;
return 'thread_' . ($index ? 'index_' : '') . $board['uri'] . '_' . $this->id;
}
};