code cleanup

This commit is contained in:
Michael Save
2012-04-13 00:18:19 +10:00
parent da52307c67
commit 51675e7a9b
14 changed files with 578 additions and 578 deletions

View File

@@ -4,7 +4,7 @@
* Copyright (c) 2010-2012 Tinyboard Development Group
*/
if(realpath($_SERVER['SCRIPT_FILENAME']) == str_replace('\\', '/', __FILE__)) {
if (realpath($_SERVER['SCRIPT_FILENAME']) == str_replace('\\', '/', __FILE__)) {
// You cannot request this file directly.
exit;
}
@@ -23,11 +23,11 @@ function doBoardListPart($list, $root) {
global $config;
$body = '';
foreach($list as $board) {
if(is_array($board))
foreach ($list as $board) {
if (is_array($board))
$body .= ' [' . doBoardListPart($board, $root) . '] ';
else {
if(($key = array_search($board, $list)) && gettype($key) == 'string') {
if (($key = array_search($board, $list)) && gettype($key) == 'string') {
$body .= ' <a href="' . $board . '">' . $key . '</a> /';
} else {
$body .= ' <a href="' . $root . $board . '/' . $config['file_index'] . '">' . $board . '</a> /';
@@ -42,10 +42,10 @@ function doBoardListPart($list, $root) {
function createBoardlist($mod=false) {
global $config;
if(!isset($config['boards'])) return Array('top'=>'','bottom'=>'');
if (!isset($config['boards'])) return Array('top'=>'','bottom'=>'');
$body = doBoardListPart($config['boards'], $mod?'?/':$config['root']);
if(!preg_match('/\] $/', $body))
if (!preg_match('/\] $/', $body))
$body = '[' . $body . ']';
$body = trim($body);
@@ -59,12 +59,12 @@ function createBoardlist($mod=false) {
function error($message, $priority = true) {
global $board, $mod, $config;
if($config['syslog'] && $priority !== false) {
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')) {
if (defined('STDIN')) {
// Running from CLI
die('Error: ' . $message . "\n");
}
@@ -103,7 +103,7 @@ function loginForm($error=false, $username=false, $redirect=false) {
function pm_snippet($body, $len=null) {
global $config;
if(!isset($len))
if (!isset($len))
$len = &$config['mod']['snippet_length'];
// Replace line breaks with some whitespace
@@ -127,16 +127,16 @@ function pm_snippet($body, $len=null) {
function capcode($cap) {
global $config;
if(!$cap)
if (!$cap)
return false;
$capcode = Array();
if(isset($config['custom_capcode'][$cap])) {
if(is_array($config['custom_capcode'][$cap])) {
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]))
if (isset($config['custom_capcode'][$cap][1]))
$capcode['name'] = $config['custom_capcode'][$cap][1];
if(isset($config['custom_capcode'][$cap][2]))
if (isset($config['custom_capcode'][$cap][2]))
$capcode['trip'] = $config['custom_capcode'][$cap][2];
} else {
$capcode['cap'] = sprintf($config['custom_capcode'][$cap], $cap);
@@ -151,38 +151,38 @@ function capcode($cap) {
function truncate($body, $url, $max_lines = false, $max_chars = false) {
global $config;
if($max_lines === false)
if ($max_lines === false)
$max_lines = $config['body_truncate'];
if($max_chars === false)
if ($max_chars === false)
$max_chars = $config['body_truncate_char'];
$original_body = $body;
$lines = substr_count($body, '<br/>');
// Limit line count
if($lines > $max_lines) {
if(preg_match('/(((.*?)<br\/>){' . $max_lines . '})/', $body, $m))
if ($lines > $max_lines) {
if (preg_match('/(((.*?)<br\/>){' . $max_lines . '})/', $body, $m))
$body = $m[0];
}
$body = substr($body, 0, $max_chars);
if($body != $original_body) {
if ($body != $original_body) {
// Remove any corrupt tags at the end
$body = preg_replace('/<([\w]+)?([^>]*)?$/', '', $body);
// Open tags
if(preg_match_all('/<([\w]+)[^>]*>/', $body, $open_tags)) {
if (preg_match_all('/<([\w]+)[^>]*>/', $body, $open_tags)) {
$tags = Array();
for($x=0;$x<count($open_tags[0]);$x++) {
if(!preg_match('/\/(\s+)?>$/', $open_tags[0][$x]))
for ($x=0;$x<count($open_tags[0]);$x++) {
if (!preg_match('/\/(\s+)?>$/', $open_tags[0][$x]))
$tags[] = $open_tags[1][$x];
}
// List successfully closed tags
if(preg_match_all('/(<\/([\w]+))>/', $body, $closed_tags)) {
for($x=0;$x<count($closed_tags[0]);$x++) {
if (preg_match_all('/(<\/([\w]+))>/', $body, $closed_tags)) {
for ($x=0;$x<count($closed_tags[0]);$x++) {
unset($tags[array_search($closed_tags[2][$x], $tags)]);
}
}
@@ -191,7 +191,7 @@ function truncate($body, $url, $max_lines = false, $max_chars = false) {
$body = preg_replace('/&[^;]+$/', '', $body);
// Close any open tags
foreach($tags as &$tag) {
foreach ($tags as &$tag) {
$body .= "</{$tag}>";
}
} else {
@@ -207,8 +207,8 @@ function truncate($body, $url, $max_lines = false, $max_chars = false) {
function confirmLink($text, $title, $confirm, $href) {
global $config, $mod;
if($config['mod']['server-side_confirm'])
return '<a onclick="if(confirm(\'' . htmlentities(addslashes($confirm)) . '\')) document.location=\'?/' . htmlentities(addslashes($href)) . '\';return false;" title="' . htmlentities($title) . '" href="?/confirm/' . $href . '">' . $text . '</a>';
if ($config['mod']['server-side_confirm'])
return '<a onclick="if (confirm(\'' . htmlentities(addslashes($confirm)) . '\')) document.location=\'?/' . htmlentities(addslashes($href)) . '\';return false;" title="' . htmlentities($title) . '" href="?/confirm/' . $href . '">' . $text . '</a>';
else
return '<a onclick="return confirm(\'' . htmlentities(addslashes($confirm)) . '\')" title="' . htmlentities($title) . '" href="?/' . $href . '">' . $text . '</a>';
}
@@ -216,7 +216,7 @@ function confirmLink($text, $title, $confirm, $href) {
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))
if (!isset($root))
$root = &$config['root'];
$this->id = $id;
@@ -241,7 +241,7 @@ class Post {
$this->root = $root;
$this->mod = $mod;
if($this->mod)
if ($this->mod)
// Fix internal links
// Very complicated regex
$this->body = preg_replace(
@@ -259,38 +259,38 @@ class Post {
global $board, $config;
$built = '';
if($this->mod) {
if ($this->mod) {
// Mod controls (on posts)
// Delete
if(hasPermission($config['mod']['delete'], $board['uri'], $this->mod))
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))
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))
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))
if (hasPermission($config['mod']['ban'], $board['uri'], $this->mod))
$built .= ' <a title="Ban" href="?/' . $board['uri'] . '/ban/' . $this->id . '">' . $config['mod']['link_ban'] . '</a>';
// Ban & Delete
if(hasPermission($config['mod']['bandelete'], $board['uri'], $this->mod))
if (hasPermission($config['mod']['bandelete'], $board['uri'], $this->mod))
$built .= ' <a title="Ban & Delete" href="?/' . $board['uri'] . '/ban&amp;delete/' . $this->id . '">' . $config['mod']['link_bandelete'] . '</a>';
// Delete file (keep post)
if(!empty($this->file) && hasPermission($config['mod']['deletefile'], $board['uri'], $this->mod))
if (!empty($this->file) && hasPermission($config['mod']['deletefile'], $board['uri'], $this->mod))
$built .= ' <a title="Remove file" href="?/' . $board['uri'] . '/deletefile/' . $this->id . '">' . $config['mod']['link_deletefile'] . '</a>';
// Edit post
if(hasPermission($config['mod']['editpost'], $board['uri'], $this->mod))
if (hasPermission($config['mod']['editpost'], $board['uri'], $this->mod))
$built .= ' <a title="Edit post" href="?/' . $board['uri'] . '/edit/' . $this->id . '">' . $config['mod']['link_editpost'] . '</a>';
if(!empty($built))
if (!empty($built))
$built = '<span class="controls">' . $built . '</span>';
}
return $built;
@@ -306,7 +306,7 @@ class Post {
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))
if (!isset($root))
$root = &$config['root'];
$this->id = $id;
@@ -337,7 +337,7 @@ class Thread {
$this->mod = $mod;
$this->hr = $hr;
if($this->mod)
if ($this->mod)
// Fix internal links
// Very complicated regex
$this->body = preg_replace(
@@ -358,60 +358,60 @@ class Thread {
global $board, $config;
$built = '';
if($this->mod) {
if ($this->mod) {
// Mod controls (on posts)
// Delete
if(hasPermission($config['mod']['delete'], $board['uri'], $this->mod))
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))
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))
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))
if (hasPermission($config['mod']['ban'], $board['uri'], $this->mod))
$built .= ' <a title="Ban" href="?/' . $board['uri'] . '/ban/' . $this->id . '">' . $config['mod']['link_ban'] . '</a>';
// Ban & Delete
if(hasPermission($config['mod']['bandelete'], $board['uri'], $this->mod))
if (hasPermission($config['mod']['bandelete'], $board['uri'], $this->mod))
$built .= ' <a title="Ban & Delete" href="?/' . $board['uri'] . '/ban&amp;delete/' . $this->id . '">' . $config['mod']['link_bandelete'] . '</a>';
// Delete file (keep post)
if(!empty($this->file) && $this->file != 'deleted' && hasPermission($config['mod']['deletefile'], $board['uri'], $this->mod))
if (!empty($this->file) && $this->file != 'deleted' && hasPermission($config['mod']['deletefile'], $board['uri'], $this->mod))
$built .= ' <a title="Remove file" href="?/' . $board['uri'] . '/deletefile/' . $this->id . '">' . $config['mod']['link_deletefile'] . '</a>';
// Sticky
if(hasPermission($config['mod']['sticky'], $board['uri'], $this->mod))
if($this->sticky)
if (hasPermission($config['mod']['sticky'], $board['uri'], $this->mod))
if ($this->sticky)
$built .= ' <a title="Make thread not sticky" href="?/' . $board['uri'] . '/unsticky/' . $this->id . '">' . $config['mod']['link_desticky'] . '</a>';
else
$built .= ' <a title="Make thread sticky" href="?/' . $board['uri'] . '/sticky/' . $this->id . '">' . $config['mod']['link_sticky'] . '</a>';
if(hasPermission($config['mod']['bumplock'], $board['uri'], $this->mod))
if($this->bumplocked)
if (hasPermission($config['mod']['bumplock'], $board['uri'], $this->mod))
if ($this->bumplocked)
$built .= ' <a title="Allow thread to be bumped" href="?/' . $board['uri'] . '/bumpunlock/' . $this->id . '">' . $config['mod']['link_bumpunlock'] . '</a>';
else
$built .= ' <a title="Prevent thread from being bumped" href="?/' . $board['uri'] . '/bumplock/' . $this->id . '">' . $config['mod']['link_bumplock'] . '</a>';
// Lock
if(hasPermission($config['mod']['lock'], $board['uri'], $this->mod))
if($this->locked)
if (hasPermission($config['mod']['lock'], $board['uri'], $this->mod))
if ($this->locked)
$built .= ' <a title="Unlock thread" href="?/' . $board['uri'] . '/unlock/' . $this->id . '">' . $config['mod']['link_unlock'] . '</a>';
else
$built .= ' <a title="Lock thread" href="?/' . $board['uri'] . '/lock/' . $this->id . '">' . $config['mod']['link_lock'] . '</a>';
if(hasPermission($config['mod']['move'], $board['uri'], $this->mod))
if (hasPermission($config['mod']['move'], $board['uri'], $this->mod))
$built .= ' <a title="Move thread to another board" href="?/' . $board['uri'] . '/move/' . $this->id . '">' . $config['mod']['link_move'] . '</a>';
// Edit post
if(hasPermission($config['mod']['editpost'], $board['uri'], $this->mod))
if (hasPermission($config['mod']['editpost'], $board['uri'], $this->mod))
$built .= ' <a title="Edit post" href="?/' . $board['uri'] . '/edit/' . $this->id . '">' . $config['mod']['link_editpost'] . '</a>';
if(!empty($built))
if (!empty($built))
$built = '<span class="controls op">' . $built . '</span>';
}
return $built;
@@ -426,7 +426,7 @@ class Thread {
$built = Element('post_thread.html', Array('config' => $config, 'board' => $board, 'post' => &$this, 'index' => $index));
if(!$this->mod && $index && $config['cache']['enabled']) {
if (!$this->mod && $index && $config['cache']['enabled']) {
cache::set($this->cache_key($index), $built);
}