summaryrefslogtreecommitdiff
path: root/inc/functions.php
diff options
context:
space:
mode:
Diffstat (limited to 'inc/functions.php')
-rwxr-xr-x[-rw-r--r--]inc/functions.php839
1 files changed, 561 insertions, 278 deletions
diff --git a/inc/functions.php b/inc/functions.php
index 06262e30..956f49ff 100644..100755
--- a/inc/functions.php
+++ b/inc/functions.php
@@ -18,8 +18,9 @@ require_once 'inc/template.php';
require_once 'inc/database.php';
require_once 'inc/events.php';
require_once 'inc/api.php';
-require_once 'inc/bans.php';
-require_once 'inc/lib/gettext/gettext.inc';
+if (!extension_loaded('gettext')) {
+ require_once 'inc/lib/gettext/gettext.inc';
+}
// the user is not currently logged in as a moderator
$mod = false;
@@ -29,14 +30,17 @@ mb_internal_encoding('UTF-8');
loadConfig();
function init_locale($locale, $error='error') {
- if (_setlocale(LC_ALL, $locale) === false) {
- $error('The specified locale (' . $locale . ') does not exist on your platform!');
- }
if (extension_loaded('gettext')) {
+ if (setlocale(LC_ALL, $locale) === false) {
+ //$error('The specified locale (' . $locale . ') does not exist on your platform!');
+ }
bindtextdomain('tinyboard', './inc/locale');
bind_textdomain_codeset('tinyboard', 'UTF-8');
textdomain('tinyboard');
} else {
+ if (_setlocale(LC_ALL, $locale) === false) {
+ $error('The specified locale (' . $locale . ') does not exist on your platform!');
+ }
_bindtextdomain('tinyboard', './inc/locale');
_bind_textdomain_codeset('tinyboard', 'UTF-8');
_textdomain('tinyboard');
@@ -46,171 +50,224 @@ $current_locale = 'en';
function loadConfig() {
- global $board, $config, $__ip, $debug, $__version, $microtime_start, $current_locale;
+ global $board, $config, $__ip, $debug, $__version, $microtime_start, $current_locale, $events;
$error = function_exists('error') ? 'error' : 'basic_error_function_because_the_other_isnt_loaded_yet';
- reset_events();
+ $boardsuffix = isset($board['uri']) ? $board['uri'] : '';
if (!isset($_SERVER['REMOTE_ADDR']))
$_SERVER['REMOTE_ADDR'] = '0.0.0.0';
- $arrays = array(
- 'db',
- 'api',
- 'cache',
- 'cookies',
- 'error',
- 'dir',
- 'mod',
- 'spam',
- 'filters',
- 'wordfilters',
- 'custom_capcode',
- 'custom_tripcode',
- 'dnsbl',
- 'dnsbl_exceptions',
- 'remote',
- 'allowed_ext',
- 'allowed_ext_files',
- 'file_icons',
- 'footer',
- 'stylesheets',
- 'code_stylesheets',
- 'additional_javascript',
- 'markup',
- 'custom_pages',
- 'dashboard_links'
- );
+ if (file_exists('tmp/cache/cache_config.php')) {
+ require_once('tmp/cache/cache_config.php');
+ }
+
+ if (isset($config['cache_config']) &&
+ $config['cache_config'] &&
+ $config = Cache::get('config_' . $boardsuffix ) ) {
+ $events = Cache::get('events_' . $boardsuffix );
+
+ define_groups();
- $config = array();
- foreach ($arrays as $key) {
- $config[$key] = array();
+ if (file_exists('inc/instance-functions.php')) {
+ require_once('inc/instance-functions.php');
+ }
+
+ if ($config['locale'] != $current_locale) {
+ $current_locale = $config['locale'];
+ init_locale($config['locale'], $error);
+ }
}
+ else {
+ $config = array();
- require 'inc/config.php';
+ reset_events();
- if (!file_exists('inc/instance-config.php'))
- $error('Tinyboard is not configured! Create inc/instance-config.php.');
+ $arrays = array(
+ 'db',
+ 'api',
+ 'cache',
+ 'cookies',
+ 'error',
+ 'dir',
+ 'mod',
+ 'spam',
+ 'filters',
+ 'wordfilters',
+ 'custom_capcode',
+ 'custom_tripcode',
+ 'dnsbl',
+ 'dnsbl_exceptions',
+ 'remote',
+ 'allowed_ext',
+ 'allowed_ext_files',
+ 'file_icons',
+ 'footer',
+ 'stylesheets',
+ 'additional_javascript',
+ 'markup',
+ 'custom_pages',
+ 'dashboard_links'
+ );
- // Initialize locale as early as possible
+ foreach ($arrays as $key) {
+ $config[$key] = array();
+ }
- $config['locale'] = 'en';
+ if (!file_exists('inc/instance-config.php'))
+ $error('Tinyboard is not configured! Create inc/instance-config.php.');
- $configstr = file_get_contents('inc/instance-config.php');
+ // Initialize locale as early as possible
- if (isset($board['dir']) && file_exists($board['dir'] . '/config.php')) {
+ // Those calls are expensive. Unfortunately, our cache system is not initialized at this point.
+ // So, we may store the locale in a tmp/ filesystem.
+
+ if (file_exists($fn = 'tmp/cache/locale_' . $boardsuffix ) ) {
+ $config['locale'] = file_get_contents($fn);
+ }
+ else {
+ $config['locale'] = 'en';
+
+ $configstr = file_get_contents('inc/instance-config.php');
+
+ if (isset($board['dir']) && file_exists($board['dir'] . '/config.php')) {
$configstr .= file_get_contents($board['dir'] . '/config.php');
+ }
+ $matches = array();
+ preg_match_all('/[^\/*#]\$config\s*\[\s*[\'"]locale[\'"]\s*\]\s*=\s*([\'"])(.*?)\1/', $configstr, $matches);
+ if ($matches && isset ($matches[2]) && $matches[2]) {
+ $matches = $matches[2];
+ $config['locale'] = $matches[count($matches)-1];
+ }
+
+ file_put_contents($fn, $config['locale']);
}
- $matches = array();
- preg_match_all('/[^\/*#]\$config\s*\[\s*[\'"]locale[\'"]\s*\]\s*=\s*([\'"])(.*?)\1/', $configstr, $matches);
- if ($matches && isset ($matches[2]) && $matches[2]) {
- $matches = $matches[2];
- $config['locale'] = $matches[count($matches)-1];
- }
- if ($config['locale'] != $current_locale) {
- $current_locale = $config['locale'];
- init_locale($config['locale'], $error);
- }
+ if ($config['locale'] != $current_locale) {
+ $current_locale = $config['locale'];
+ init_locale($config['locale'], $error);
+ }
- require 'inc/instance-config.php';
+ require 'inc/config.php';
- if (isset($board['dir']) && file_exists($board['dir'] . '/config.php')) {
- require $board['dir'] . '/config.php';
- }
+ require 'inc/instance-config.php';
- if ($config['locale'] != $current_locale) {
- $current_locale = $config['locale'];
- init_locale($config['locale'], $error);
- }
+ if (isset($board['dir']) && file_exists($board['dir'] . '/config.php')) {
+ require $board['dir'] . '/config.php';
+ }
- if (!isset($__version))
- $__version = file_exists('.installed') ? trim(file_get_contents('.installed')) : false;
- $config['version'] = $__version;
+ if ($config['locale'] != $current_locale) {
+ $current_locale = $config['locale'];
+ init_locale($config['locale'], $error);
+ }
- date_default_timezone_set($config['timezone']);
+ if (!isset($config['global_message']))
+ $config['global_message'] = false;
- if (!isset($config['global_message']))
- $config['global_message'] = false;
+ if (!isset($config['post_url']))
+ $config['post_url'] = $config['root'] . $config['file_post'];
- if (!isset($config['post_url']))
- $config['post_url'] = $config['root'] . $config['file_post'];
- if (!isset($config['referer_match']))
- if (isset($_SERVER['HTTP_HOST'])) {
- $config['referer_match'] = '/^' .
- (preg_match('@^https?://@', $config['root']) ? '' :
- 'https?:\/\/' . $_SERVER['HTTP_HOST']) .
- preg_quote($config['root'], '/') .
- '(' .
- str_replace('%s', $config['board_regex'], preg_quote($config['board_path'], '/')) .
- '(' .
- preg_quote($config['file_index'], '/') . '|' .
- str_replace('%d', '\d+', preg_quote($config['file_page'])) .
- ')?' .
- '|' .
- str_replace('%s', $config['board_regex'], preg_quote($config['board_path'], '/')) .
- preg_quote($config['dir']['res'], '/') .
- '(' .
- str_replace('%d', '\d+', preg_quote($config['file_page'], '/')) . '|' .
- str_replace('%d', '\d+', preg_quote($config['file_page50'], '/')) .
- ')' .
- '|' .
- preg_quote($config['file_mod'], '/') . '\?\/.+' .
- ')([#?](.+)?)?$/ui';
- } else {
- // CLI mode
- $config['referer_match'] = '//';
- }
- if (!isset($config['cookies']['path']))
- $config['cookies']['path'] = &$config['root'];
+ if (!isset($config['referer_match']))
+ if (isset($_SERVER['HTTP_HOST'])) {
+ $config['referer_match'] = '/^' .
+ (preg_match('@^https?://@', $config['root']) ? '' :
+ 'https?:\/\/' . $_SERVER['HTTP_HOST']) .
+ preg_quote($config['root'], '/') .
+ '(' .
+ str_replace('%s', $config['board_regex'], preg_quote($config['board_path'], '/')) .
+ '(' .
+ preg_quote($config['file_index'], '/') . '|' .
+ str_replace('%d', '\d+', preg_quote($config['file_page'])) .
+ ')?' .
+ '|' .
+ str_replace('%s', $config['board_regex'], preg_quote($config['board_path'], '/')) .
+ preg_quote($config['dir']['res'], '/') .
+ '(' .
+ str_replace('%d', '\d+', preg_quote($config['file_page'], '/')) . '|' .
+ str_replace('%d', '\d+', preg_quote($config['file_page50'], '/')) . '|' .
+ str_replace(array('%d', '%s'), array('\d+', '[a-z0-9-]+'), preg_quote($config['file_page_slug'], '/')) . '|' .
+ str_replace(array('%d', '%s'), array('\d+', '[a-z0-9-]+'), preg_quote($config['file_page50_slug'], '/')) .
+ ')' .
+ '|' .
+ preg_quote($config['file_mod'], '/') . '\?\/.+' .
+ ')([#?](.+)?)?$/ui';
+ } else {
+ // CLI mode
+ $config['referer_match'] = '//';
+ }
+ if (!isset($config['cookies']['path']))
+ $config['cookies']['path'] = &$config['root'];
- if (!isset($config['dir']['static']))
- $config['dir']['static'] = $config['root'] . 'static/';
+ if (!isset($config['dir']['static']))
+ $config['dir']['static'] = $config['root'] . 'static/';
- if (!isset($config['image_blank']))
- $config['image_blank'] = $config['dir']['static'] . 'blank.gif';
+ if (!isset($config['image_blank']))
+ $config['image_blank'] = $config['dir']['static'] . 'blank.gif';
- if (!isset($config['image_sticky']))
- $config['image_sticky'] = $config['dir']['static'] . 'sticky.gif';
- if (!isset($config['image_locked']))
- $config['image_locked'] = $config['dir']['static'] . 'locked.gif';
- if (!isset($config['image_bumplocked']))
- $config['image_bumplocked'] = $config['dir']['static'] . 'sage.gif';
- if (!isset($config['image_deleted']))
- $config['image_deleted'] = $config['dir']['static'] . 'deleted.png';
+ if (!isset($config['image_sticky']))
+ $config['image_sticky'] = $config['dir']['static'] . 'sticky.gif';
+ if (!isset($config['image_locked']))
+ $config['image_locked'] = $config['dir']['static'] . 'locked.gif';
+ if (!isset($config['image_bumplocked']))
+ $config['image_bumplocked'] = $config['dir']['static'] . 'sage.gif';
+ if (!isset($config['image_deleted']))
+ $config['image_deleted'] = $config['dir']['static'] . 'deleted.png';
- if (!isset($config['uri_thumb']))
- $config['uri_thumb'] = $config['root'] . $board['dir'] . $config['dir']['thumb'];
- elseif (isset($board['dir']))
- $config['uri_thumb'] = sprintf($config['uri_thumb'], $board['dir']);
+ if (!isset($config['uri_thumb']))
+ $config['uri_thumb'] = $config['root'] . $board['dir'] . $config['dir']['thumb'];
+ elseif (isset($board['dir']))
+ $config['uri_thumb'] = sprintf($config['uri_thumb'], $board['dir']);
- if (!isset($config['uri_img']))
- $config['uri_img'] = $config['root'] . $board['dir'] . $config['dir']['img'];
- elseif (isset($board['dir']))
- $config['uri_img'] = sprintf($config['uri_img'], $board['dir']);
+ if (!isset($config['uri_img']))
+ $config['uri_img'] = $config['root'] . $board['dir'] . $config['dir']['img'];
+ elseif (isset($board['dir']))
+ $config['uri_img'] = sprintf($config['uri_img'], $board['dir']);
- if (!isset($config['uri_stylesheets']))
- $config['uri_stylesheets'] = $config['root'] . 'stylesheets/';
+ if (!isset($config['uri_stylesheets']))
+ $config['uri_stylesheets'] = $config['root'] . 'stylesheets/';
- if (!isset($config['url_stylesheet']))
- $config['url_stylesheet'] = $config['uri_stylesheets'] . 'style.css';
- if (!isset($config['url_javascript']))
- $config['url_javascript'] = $config['root'] . $config['file_script'];
- if (!isset($config['additional_javascript_url']))
- $config['additional_javascript_url'] = $config['root'];
- if (!isset($config['uri_flags']))
- $config['uri_flags'] = $config['root'] . 'static/flags/%s.png';
- if (!isset($config['user_flag']))
- $config['user_flag'] = false;
- if (!isset($config['user_flags']))
- $config['user_flags'] = array();
+ if (!isset($config['url_stylesheet']))
+ $config['url_stylesheet'] = $config['uri_stylesheets'] . 'style.css';
+ if (!isset($config['url_javascript']))
+ $config['url_javascript'] = $config['root'] . $config['file_script'];
+ if (!isset($config['additional_javascript_url']))
+ $config['additional_javascript_url'] = $config['root'];
+ if (!isset($config['uri_flags']))
+ $config['uri_flags'] = $config['root'] . 'static/flags/%s.png';
+ if (!isset($config['user_flag']))
+ $config['user_flag'] = false;
+ if (!isset($config['user_flags']))
+ $config['user_flags'] = array();
+
+ if (!isset($__version))
+ $__version = file_exists('.installed') ? trim(file_get_contents('.installed')) : false;
+ $config['version'] = $__version;
+
+ if ($config['allow_roll'])
+ event_handler('post', 'diceRoller');
+
+ if (is_array($config['anonymous']))
+ $config['anonymous'] = $config['anonymous'][array_rand($config['anonymous'])];
+
+ }
+ // Effectful config processing below:
+
+ date_default_timezone_set($config['timezone']);
if ($config['root_file']) {
chdir($config['root_file']);
}
+ // Keep the original address to properly comply with other board configurations
+ if (!isset($__ip))
+ $__ip = $_SERVER['REMOTE_ADDR'];
+
+ // ::ffff:0.0.0.0
+ if (preg_match('/^\:\:(ffff\:)?(\d+\.\d+\.\d+\.\d+)$/', $__ip, $m))
+ $_SERVER['REMOTE_ADDR'] = $m[2];
+
if ($config['verbose_errors']) {
set_error_handler('verbose_error_handler');
error_reporting(E_ALL);
@@ -220,19 +277,12 @@ function loadConfig() {
ini_set('display_errors', false);
}
- // Keep the original address to properly comply with other board configurations
- if (!isset($__ip))
- $__ip = $_SERVER['REMOTE_ADDR'];
-
- // ::ffff:0.0.0.0
- if (preg_match('/^\:\:(ffff\:)?(\d+\.\d+\.\d+\.\d+)$/', $__ip, $m))
- $_SERVER['REMOTE_ADDR'] = $m[2];
-
if ($config['syslog'])
openlog('tinyboard', LOG_ODELAY, LOG_SYSLOG); // open a connection to sysem logger
if ($config['recaptcha'])
require_once 'inc/lib/recaptcha/recaptchalib.php';
+
if ($config['cache']['enabled'])
require_once 'inc/cache.php';
@@ -241,13 +291,22 @@ function loadConfig() {
event_handler('post', 'postHandler');
}
- if (is_array($config['anonymous']))
- $config['anonymous'] = $config['anonymous'][array_rand($config['anonymous'])];
+ event('load-config');
+
+ if ($config['cache_config'] && !isset ($config['cache_config_loaded'])) {
+ file_put_contents('tmp/cache/cache_config.php', '<?php '.
+ '$config = array();'.
+ '$config[\'cache\'] = '.var_export($config['cache'], true).';'.
+ '$config[\'cache_config\'] = true;'.
+ '$config[\'debug\'] = '.var_export($config['debug'], true).';'.
+ 'require_once(\'inc/cache.php\');'
+ );
- if ($config['allow_roll'])
- event_handler('post', 'diceRoller');
+ $config['cache_config_loaded'] = true;
- event('load-config');
+ Cache::set('config_'.$boardsuffix, $config);
+ Cache::set('events_'.$boardsuffix, $events);
+ }
if ($config['debug']) {
if (!isset($debug)) {
@@ -323,8 +382,12 @@ function verbose_error_handler($errno, $errstr, $errfile, $errline) {
function define_groups() {
global $config;
- foreach ($config['mod']['groups'] as $group_value => $group_name)
- defined($group_name) or define($group_name, $group_value, true);
+ foreach ($config['mod']['groups'] as $group_value => $group_name) {
+ $group_name = strtoupper($group_name);
+ if(!defined($group_name)) {
+ define($group_name, $group_value, true);
+ }
+ }
ksort($config['mod']['groups']);
}
@@ -336,22 +399,59 @@ function create_antibot($board, $thread = null) {
}
function rebuildThemes($action, $boardname = false) {
- global $config, $board;
+ global $config, $board, $current_locale;
// Save the global variables
$_config = $config;
$_board = $board;
// List themes
- $query = query("SELECT `theme` FROM ``theme_settings`` WHERE `name` IS NULL AND `value` IS NULL") or error(db_error());
+ if ($themes = Cache::get("themes")) {
+ // OK, we already have themes loaded
+ }
+ else {
+ $query = query("SELECT `theme` FROM ``theme_settings`` WHERE `name` IS NULL AND `value` IS NULL") or error(db_error());
+
+ $themes = array();
+
+ while ($theme = $query->fetch(PDO::FETCH_ASSOC)) {
+ $themes[] = $theme;
+ }
+
+ Cache::set("themes", $themes);
+ }
+
+ foreach ($themes as $theme) {
+ // Restore them
+ $config = $_config;
+ $board = $_board;
+
+ // Reload the locale
+ if ($config['locale'] != $current_locale) {
+ $current_locale = $config['locale'];
+ init_locale($config['locale']);
+ }
+
+ if (PHP_SAPI === 'cli') {
+ echo "Rebuilding theme ".$theme['theme']."... ";
+ }
- while ($theme = $query->fetch(PDO::FETCH_ASSOC)) {
rebuildTheme($theme['theme'], $action, $boardname);
+
+ if (PHP_SAPI === 'cli') {
+ echo "done\n";
+ }
}
- // Restore them
+ // Restore them again
$config = $_config;
$board = $_board;
+
+ // Reload the locale
+ if ($config['locale'] != $current_locale) {
+ $current_locale = $config['locale'];
+ init_locale($config['locale']);
+ }
}
@@ -382,6 +482,10 @@ function rebuildTheme($theme, $action, $board = false) {
function themeSettings($theme) {
+ if ($settings = Cache::get("theme_settings_".$theme)) {
+ return $settings;
+ }
+
$query = prepare("SELECT `name`, `value` FROM ``theme_settings`` WHERE `theme` = :theme AND `name` IS NOT NULL");
$query->bindValue(':theme', $theme);
$query->execute() or error(db_error($query));
@@ -391,6 +495,8 @@ function themeSettings($theme) {
$settings[$s['name']] = $s['value'];
}
+ Cache::set("theme_settings_".$theme, $settings);
+
return $settings;
}
@@ -446,6 +552,11 @@ function openBoard($uri) {
$board = getBoardInfo($uri);
if ($board) {
setupBoard($board);
+
+ if (function_exists('after_open_board')) {
+ after_open_board();
+ }
+
return true;
}
return false;
@@ -567,8 +678,8 @@ function file_write($path, $data, $simple = false, $skip_purge = false) {
if ($bytes & ~0x3ff) { // if ($bytes >= 1024)
if (file_put_contents($gzpath, gzencode($data), $simple ? 0 : LOCK_EX) === false)
error("Unable to write to file: $gzpath");
- if (!touch($gzpath, filemtime($path), fileatime($path)))
- error("Unable to touch file: $gzpath");
+ //if (!touch($gzpath, filemtime($path), fileatime($path)))
+ // error("Unable to touch file: $gzpath");
}
else {
@unlink($gzpath);
@@ -607,6 +718,13 @@ function file_unlink($path) {
}
$ret = @unlink($path);
+
+ if ($config['gzip_static']) {
+ $gzpath = "$path.gz";
+
+ @unlink($gzpath);
+ }
+
if (isset($config['purge']) && $path[0] != '/' && isset($_SERVER['HTTP_HOST'])) {
// Purge cache
if (basename($path) == $config['file_index']) {
@@ -680,36 +798,38 @@ function listBoards($just_uri = false) {
function until($timestamp) {
$difference = $timestamp - time();
- if ($difference < 60) {
+ switch(TRUE){
+ case ($difference < 60):
return $difference . ' ' . ngettext('second', 'seconds', $difference);
- } elseif ($difference < 60*60) {
+ case ($difference < 3600): //60*60 = 3600
return ($num = round($difference/(60))) . ' ' . ngettext('minute', 'minutes', $num);
- } elseif ($difference < 60*60*24) {
- return ($num = round($difference/(60*60))) . ' ' . ngettext('hour', 'hours', $num);
- } elseif ($difference < 60*60*24*7) {
- return ($num = round($difference/(60*60*24))) . ' ' . ngettext('day', 'days', $num);
- } elseif ($difference < 60*60*24*365) {
- return ($num = round($difference/(60*60*24*7))) . ' ' . ngettext('week', 'weeks', $num);
+ case ($difference < 86400): //60*60*24 = 86400
+ return ($num = round($difference/(3600))) . ' ' . ngettext('hour', 'hours', $num);
+ case ($difference < 604800): //60*60*24*7 = 604800
+ return ($num = round($difference/(86400))) . ' ' . ngettext('day', 'days', $num);
+ case ($difference < 31536000): //60*60*24*365 = 31536000
+ return ($num = round($difference/(604800))) . ' ' . ngettext('week', 'weeks', $num);
+ default:
+ return ($num = round($difference/(31536000))) . ' ' . ngettext('year', 'years', $num);
}
-
- return ($num = round($difference/(60*60*24*365))) . ' ' . ngettext('year', 'years', $num);
}
function ago($timestamp) {
$difference = time() - $timestamp;
- if ($difference < 60) {
+ switch(TRUE){
+ case ($difference < 60) :
return $difference . ' ' . ngettext('second', 'seconds', $difference);
- } elseif ($difference < 60*60) {
+ case ($difference < 3600): //60*60 = 3600
return ($num = round($difference/(60))) . ' ' . ngettext('minute', 'minutes', $num);
- } elseif ($difference < 60*60*24) {
- return ($num = round($difference/(60*60))) . ' ' . ngettext('hour', 'hours', $num);
- } elseif ($difference < 60*60*24*7) {
- return ($num = round($difference/(60*60*24))) . ' ' . ngettext('day', 'days', $num);
- } elseif ($difference < 60*60*24*365) {
- return ($num = round($difference/(60*60*24*7))) . ' ' . ngettext('week', 'weeks', $num);
+ case ($difference < 86400): //60*60*24 = 86400
+ return ($num = round($difference/(3600))) . ' ' . ngettext('hour', 'hours', $num);
+ case ($difference < 604800): //60*60*24*7 = 604800
+ return ($num = round($difference/(86400))) . ' ' . ngettext('day', 'days', $num);
+ case ($difference < 31536000): //60*60*24*365 = 31536000
+ return ($num = round($difference/(604800))) . ' ' . ngettext('week', 'weeks', $num);
+ default:
+ return ($num = round($difference/(31536000))) . ' ' . ngettext('year', 'years', $num);
}
-
- return ($num = round($difference/(60*60*24*365))) . ' ' . ngettext('year', 'years', $num);
}
function displayBan($ban) {
@@ -740,7 +860,7 @@ function displayBan($ban) {
$pending_appeal = false;
if ($config['ban_appeals']) {
- $query = query("SELECT `time`, `denied` FROM `ban_appeals` WHERE `ban_id` = " . (int)$ban['id']) or error(db_error());
+ $query = query("SELECT `time`, `denied` FROM ``ban_appeals`` WHERE `ban_id` = " . (int)$ban['id']) or error(db_error());
while ($ban_appeal = $query->fetch(PDO::FETCH_ASSOC)) {
if ($ban_appeal['denied']) {
$denied_appeals[] = $ban_appeal['time'];
@@ -779,12 +899,29 @@ function checkBan($board = false) {
if (event('check-ban', $board))
return true;
- $bans = Bans::find($_SERVER['REMOTE_ADDR'], $board, $config['show_modname']);
+ $ips = array();
+
+ $ips[] = $_SERVER['REMOTE_ADDR'];
+
+ if ($config['proxy_check'] && isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
+ $ips = array_merge($ips, explode(", ", $_SERVER['HTTP_X_FORWARDED_FOR']));
+ }
+
+ foreach ($ips as $ip) {
+ $bans = Bans::find($_SERVER['REMOTE_ADDR'], $board, $config['show_modname']);
- foreach ($bans as &$ban) {
- if ($ban['expires'] && $ban['expires'] < time()) {
- Bans::delete($ban['id']);
- if ($config['require_ban_view'] && !$ban['seen']) {
+ foreach ($bans as &$ban) {
+ if ($ban['expires'] && $ban['expires'] < time()) {
+ Bans::delete($ban['id']);
+ if ($config['require_ban_view'] && !$ban['seen']) {
+ if (!isset($_POST['json_response'])) {
+ displayBan($ban);
+ } else {
+ header('Content-Type: text/json');
+ die(json_encode(array('error' => true, 'banned' => true)));
+ }
+ }
+ } else {
if (!isset($_POST['json_response'])) {
displayBan($ban);
} else {
@@ -792,13 +929,6 @@ function checkBan($board = false) {
die(json_encode(array('error' => true, 'banned' => true)));
}
}
- } else {
- if (!isset($_POST['json_response'])) {
- displayBan($ban);
- } else {
- header('Content-Type: text/json');
- die(json_encode(array('error' => true, 'banned' => true)));
- }
}
}
@@ -883,7 +1013,7 @@ function insertFloodPost(array $post) {
function post(array $post) {
global $pdo, $board;
- $query = prepare(sprintf("INSERT INTO ``posts_%s`` VALUES ( NULL, :thread, :subject, :email, :name, :trip, :capcode, :body, :body_nomarkup, :time, :time, :files, :num_files, :filehash, :password, :ip, :sticky, :locked, 0, :embed)", $board['uri']));
+ $query = prepare(sprintf("INSERT INTO ``posts_%s`` VALUES ( NULL, :thread, :subject, :email, :name, :trip, :capcode, :body, :body_nomarkup, :time, :time, :files, :num_files, :filehash, :password, :ip, :sticky, :locked, 0, :embed, :slug)", $board['uri']));
// Basic stuff
if (!empty($post['subject'])) {
@@ -952,6 +1082,13 @@ function post(array $post) {
$query->bindValue(':filehash', null, PDO::PARAM_NULL);
}
+ if ($post['op']) {
+ $query->bindValue(':slug', slugify($post));
+ }
+ else {
+ $query->bindValue(':slug', NULL);
+ }
+
if (!$query->execute()) {
undoImage($post);
error(db_error($query));
@@ -966,8 +1103,9 @@ function bumpThread($id) {
if (event('bump', $id))
return true;
- if ($config['try_smarter'])
- $build_pages[] = thread_find_page($id);
+ if ($config['try_smarter']) {
+ $build_pages = array_merge(range(1, thread_find_page($id)), $build_pages);
+ }
$query = prepare(sprintf("UPDATE ``posts_%s`` SET `bump` = :time WHERE `id` = :id AND `thread` IS NULL", $board['uri']));
$query->bindValue(':time', time(), PDO::PARAM_INT);
@@ -1047,7 +1185,7 @@ function deletePost($id, $error_if_doesnt_exist=true, $rebuild_after=true) {
global $board, $config;
// Select post and replies (if thread) in one query
- $query = prepare(sprintf("SELECT `id`,`thread`,`files` FROM ``posts_%s`` WHERE `id` = :id OR `thread` = :id", $board['uri']));
+ $query = prepare(sprintf("SELECT `id`,`thread`,`files`,`slug` FROM ``posts_%s`` WHERE `id` = :id OR `thread` = :id", $board['uri']));
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->execute() or error(db_error($query));
@@ -1065,8 +1203,8 @@ function deletePost($id, $error_if_doesnt_exist=true, $rebuild_after=true) {
if (!$post['thread']) {
// Delete thread HTML page
- file_unlink($board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $post['id']));
- file_unlink($board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $post['id']));
+ file_unlink($board['dir'] . $config['dir']['res'] . link_for($post) );
+ file_unlink($board['dir'] . $config['dir']['res'] . link_for($post, true) ); // noko50
file_unlink($board['dir'] . $config['dir']['res'] . sprintf('%d.json', $post['id']));
$antispam_query = prepare('DELETE FROM ``antispam`` WHERE `board` = :board AND `thread` = :thread');
@@ -1219,6 +1357,10 @@ function index($page, $mod=false) {
$body .= $thread->build(true);
}
+ if ($config['file_board']) {
+ $body = Element('fileboard.html', array('body' => $body, 'mod' => $mod));
+ }
+
return array(
'board' => $board,
'body' => $body,
@@ -1430,56 +1572,65 @@ function checkMute() {
}
}
-function buildIndex() {
+function buildIndex($global_api = "yes") {
global $board, $config, $build_pages;
- $pages = getPages();
- if (!$config['try_smarter'])
- $antibot = create_antibot($board['uri']);
+ if (!$config['smart_build']) {
+ $pages = getPages();
+ if (!$config['try_smarter'])
+ $antibot = create_antibot($board['uri']);
- if ($config['api']['enabled']) {
- $api = new Api();
- $catalog = array();
+ if ($config['api']['enabled']) {
+ $api = new Api();
+ $catalog = array();
+ }
}
for ($page = 1; $page <= $config['max_pages']; $page++) {
$filename = $board['dir'] . ($page == 1 ? $config['file_index'] : sprintf($config['file_page'], $page));
+ $jsonFilename = $board['dir'] . ($page - 1) . '.json'; // pages should start from 0
- if (!$config['api']['enabled'] && $config['try_smarter'] && isset($build_pages) && !empty($build_pages)
- && !in_array($page, $build_pages) && is_file($filename))
+ if ((!$config['api']['enabled'] || $global_api == "skip" || $config['smart_build']) && $config['try_smarter']
+ && isset($build_pages) && !empty($build_pages) && !in_array($page, $build_pages) )
continue;
- $content = index($page);
- if (!$content)
- break;
- // json api
- if ($config['api']['enabled']) {
- $threads = $content['threads'];
- $json = json_encode($api->translatePage($threads));
- $jsonFilename = $board['dir'] . ($page - 1) . '.json'; // pages should start from 0
- file_write($jsonFilename, $json);
+ if (!$config['smart_build']) {
+ $content = index($page);
+ if (!$content)
+ break;
- $catalog[$page-1] = $threads;
- }
+ // json api
+ if ($config['api']['enabled']) {
+ $threads = $content['threads'];
+ $json = json_encode($api->translatePage($threads));
+ file_write($jsonFilename, $json);
- if ($config['api']['enabled'] && $config['try_smarter'] && isset($build_pages) && !empty($build_pages)
- && !in_array($page, $build_pages) && is_file($filename))
- continue;
+ $catalog[$page-1] = $threads;
+ }
- if ($config['try_smarter']) {
- $antibot = create_antibot($board['uri'], 0 - $page);
- $content['current_page'] = $page;
- }
- $antibot->reset();
- $content['pages'] = $pages;
- $content['pages'][$page-1]['selected'] = true;
- $content['btn'] = getPageButtons($content['pages']);
- $content['antibot'] = $antibot;
+ if ($config['api']['enabled'] && $global_api != "skip" && $config['try_smarter'] && isset($build_pages)
+ && !empty($build_pages) && !in_array($page, $build_pages) )
+ continue;
- file_write($filename, Element('index.html', $content));
+ if ($config['try_smarter']) {
+ $antibot = create_antibot($board['uri'], 0 - $page);
+ $content['current_page'] = $page;
+ }
+ $antibot->reset();
+ $content['pages'] = $pages;
+ $content['pages'][$page-1]['selected'] = true;
+ $content['btn'] = getPageButtons($content['pages']);
+ $content['antibot'] = $antibot;
+
+ file_write($filename, Element('index.html', $content));
+ }
+ else {
+ file_unlink($filename);
+ file_unlink($jsonFilename);
+ }
}
- if ($page < $config['max_pages']) {
+ if (!$config['smart_build'] && $page < $config['max_pages']) {
for (;$page<=$config['max_pages'];$page++) {
$filename = $board['dir'] . ($page==1 ? $config['file_index'] : sprintf($config['file_page'], $page));
file_unlink($filename);
@@ -1492,14 +1643,22 @@ function buildIndex() {
}
// json api catalog
- if ($config['api']['enabled']) {
- $json = json_encode($api->translateCatalog($catalog));
- $jsonFilename = $board['dir'] . 'catalog.json';
- file_write($jsonFilename, $json);
+ if ($config['api']['enabled'] && $global_api != "skip") {
+ if ($config['smart_build']) {
+ $jsonFilename = $board['dir'] . 'catalog.json';
+ file_unlink($jsonFilename);
+ $jsonFilename = $board['dir'] . 'threads.json';
+ file_unlink($jsonFilename);
+ }
+ else {
+ $json = json_encode($api->translateCatalog($catalog));
+ $jsonFilename = $board['dir'] . 'catalog.json';
+ file_write($jsonFilename, $json);
- $json = json_encode($api->translateCatalog($catalog, true));
- $jsonFilename = $board['dir'] . 'threads.json';
- file_write($jsonFilename, $json);
+ $json = json_encode($api->translateCatalog($catalog, true));
+ $jsonFilename = $board['dir'] . 'threads.json';
+ file_write($jsonFilename, $json);
+ }
}
if ($config['try_smarter'])
@@ -1709,6 +1868,15 @@ function markup(&$body, $track_cites = false) {
if (mysql_version() < 50503)
$body = mb_encode_numericentity($body, array(0x010000, 0xffffff, 0, 0xffffff), 'UTF-8');
+ if ($config['markup_code']) {
+ $code_markup = array();
+ $body = preg_replace_callback($config['markup_code'], function($matches) use (&$code_markup) {
+ $d = count($code_markup);
+ $code_markup[] = $matches;
+ return "<code $d>";
+ }, $body);
+ }
+
foreach ($config['markup'] as $markup) {
if (is_string($markup[1])) {
$body = preg_replace($markup[0], $markup[1], $body);
@@ -1780,7 +1948,7 @@ function markup(&$body, $track_cites = false) {
if (isset($cited_posts[$cite])) {
$replacement = '<a onclick="highlightReply(\''.$cite.'\');" href="' .
$config['root'] . $board['dir'] . $config['dir']['res'] .
- ($cited_posts[$cite] ? $cited_posts[$cite] : $cite) . '.html#' . $cite . '">' .
+ link_for(array('id' => $cite, 'thread' => $cited_posts[$cite])) . '#' . $cite . '">' .
'&gt;&gt;' . $cite .
'</a>';
@@ -1846,12 +2014,12 @@ function markup(&$body, $track_cites = false) {
if (!empty($clauses)) {
$cited_posts[$_board] = array();
- $query = query(sprintf('SELECT `thread`, `id` FROM ``posts_%s`` WHERE ' .
+ $query = query(sprintf('SELECT `thread`, `id`, `slug` FROM ``posts_%s`` WHERE ' .
implode(' OR ', $clauses), $board['uri'])) or error(db_error());
while ($cite = $query->fetch(PDO::FETCH_ASSOC)) {
$cited_posts[$_board][$cite['id']] = $config['root'] . $board['dir'] . $config['dir']['res'] .
- ($cite['thread'] ? $cite['thread'] : $cite['id']) . '.html#' . $cite['id'];
+ link_for($cite) . '#' . $cite['id'];
}
}
@@ -1906,7 +2074,19 @@ function markup(&$body, $track_cites = false) {
$body = preg_replace('/\s+$/', '', $body);
$body = preg_replace("/\n/", '<br/>', $body);
-
+
+ // Fix code markup
+ if ($config['markup_code']) {
+ foreach ($code_markup as $id => $val) {
+ $code = isset($val[2]) ? $val[2] : $val[1];
+ $code_lang = isset($val[2]) ? $val[1] : "";
+
+ $code = "<pre class='code lang-$code_lang'>".str_replace(array("\n","\t"), array("&#10;","&#9;"), htmlspecialchars($code))."</pre>";
+
+ $body = str_replace("<code $id>", $code, $body);
+ }
+ }
+
if ($config['markup_repair_tidy']) {
$tidy = new tidy();
$body = str_replace("\t", '&#09;', $body);
@@ -1924,10 +2104,10 @@ function markup(&$body, $track_cites = false) {
), 'utf8');
$body = str_replace("\n", '', $body);
}
-
+
// replace tabs with 8 spaces
$body = str_replace("\t", ' ', $body);
-
+
return $tracked_cites;
}
@@ -1999,59 +2179,70 @@ function buildThread($id, $return = false, $mod = false) {
cache::delete("thread_{$board['uri']}_{$id}");
}
- $query = prepare(sprintf("SELECT * FROM ``posts_%s`` WHERE (`thread` IS NULL AND `id` = :id) OR `thread` = :id ORDER BY `thread`,`id`", $board['uri']));
- $query->bindValue(':id', $id, PDO::PARAM_INT);
- $query->execute() or error(db_error($query));
+ if ($config['try_smarter'] && !$mod)
+ $build_pages[] = thread_find_page($id);
- while ($post = $query->fetch(PDO::FETCH_ASSOC)) {
- if (!isset($thread)) {
- $thread = new Thread($post, $mod ? '?/' : $config['root'], $mod);
- } else {
- $thread->add(new Post($post, $mod ? '?/' : $config['root'], $mod));
+ if (!$config['smart_build'] || $return || $mod) {
+ $query = prepare(sprintf("SELECT * FROM ``posts_%s`` WHERE (`thread` IS NULL AND `id` = :id) OR `thread` = :id ORDER BY `thread`,`id`", $board['uri']));
+ $query->bindValue(':id', $id, PDO::PARAM_INT);
+ $query->execute() or error(db_error($query));
+
+ while ($post = $query->fetch(PDO::FETCH_ASSOC)) {
+ if (!isset($thread)) {
+ $thread = new Thread($post, $mod ? '?/' : $config['root'], $mod);
+ } else {
+ $thread->add(new Post($post, $mod ? '?/' : $config['root'], $mod));
+ }
}
- }
- // Check if any posts were found
- if (!isset($thread))
- error($config['error']['nonexistant']);
+ // Check if any posts were found
+ if (!isset($thread))
+ error($config['error']['nonexistant']);
- $hasnoko50 = $thread->postCount() >= $config['noko50_min'];
- $antibot = $mod || $return ? false : create_antibot($board['uri'], $id);
+ $hasnoko50 = $thread->postCount() >= $config['noko50_min'];
+ $antibot = $mod || $return ? false : create_antibot($board['uri'], $id);
- $body = Element('thread.html', array(
- 'board' => $board,
- 'thread' => $thread,
- 'body' => $thread->build(),
- 'config' => $config,
- 'id' => $id,
- 'mod' => $mod,
- 'hasnoko50' => $hasnoko50,
- 'isnoko50' => false,
- 'antibot' => $antibot,
- 'boardlist' => createBoardlist($mod),
- 'return' => ($mod ? '?' . $board['url'] . $config['file_index'] : $config['root'] . $board['dir'] . $config['file_index'])
- ));
-
- if ($config['try_smarter'] && !$mod)
- $build_pages[] = thread_find_page($id);
+ $body = Element('thread.html', array(
+ 'board' => $board,
+ 'thread' => $thread,
+ 'body' => $thread->build(),
+ 'config' => $config,
+ 'id' => $id,
+ 'mod' => $mod,
+ 'hasnoko50' => $hasnoko50,
+ 'isnoko50' => false,
+ 'antibot' => $antibot,
+ 'boardlist' => createBoardlist($mod),
+ 'return' => ($mod ? '?' . $board['url'] . $config['file_index'] : $config['root'] . $board['dir'] . $config['file_index'])
+ ));
- // json api
- if ($config['api']['enabled']) {
- $api = new Api();
- $json = json_encode($api->translateThread($thread));
+ // json api
+ if ($config['api']['enabled']) {
+ $api = new Api();
+ $json = json_encode($api->translateThread($thread));
+ $jsonFilename = $board['dir'] . $config['dir']['res'] . $id . '.json';
+ file_write($jsonFilename, $json);
+ }
+ }
+ else {
$jsonFilename = $board['dir'] . $config['dir']['res'] . $id . '.json';
- file_write($jsonFilename, $json);
+ file_unlink($jsonFilename);
}
- if ($return) {
+ if ($config['smart_build'] && !$return && !$mod) {
+ $noko50fn = $board['dir'] . $config['dir']['res'] . link_for(array('id' => $id), true);
+ file_unlink($noko50fn);
+
+ file_unlink($board['dir'] . $config['dir']['res'] . link_for(array('id' => $id)));
+ } else if ($return) {
return $body;
} else {
- $noko50fn = $board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $id);
+ $noko50fn = $board['dir'] . $config['dir']['res'] . link_for($thread, true);
if ($hasnoko50 || file_exists($noko50fn)) {
buildThread50($id, $return, $mod, $thread, $antibot);
}
- file_write($board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $id), $body);
+ file_write($board['dir'] . $config['dir']['res'] . link_for($thread), $body);
}
}
@@ -2131,7 +2322,7 @@ function buildThread50($id, $return = false, $mod = false, $thread = null, $anti
if ($return) {
return $body;
} else {
- file_write($board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $id), $body);
+ file_write($board['dir'] . $config['dir']['res'] . link_for($thread, true), $body);
}
}
@@ -2282,6 +2473,12 @@ function rDNS($ip_addr) {
$host = $ip_addr;
}
+ $isip = filter_var($host, FILTER_VALIDATE_IP);
+
+ if ($config['fcrdns'] && !$isip && DNS($host) != $ip_addr) {
+ $host = $ip_addr;
+ }
+
if ($config['cache']['enabled'])
cache::set('rdns_' . $ip_addr, $host);
@@ -2394,3 +2591,89 @@ function diceRoller($post) {
}
}
}
+
+function slugify($post) {
+ global $config;
+
+ $slug = "";
+
+ if (isset($post['subject']) && $post['subject'])
+ $slug = $post['subject'];
+ elseif (isset ($post['body_nomarkup']) && $post['body_nomarkup'])
+ $slug = $post['body_nomarkup'];
+ elseif (isset ($post['body']) && $post['body'])
+ $slug = strip_html($post['body']);
+
+ // Fix UTF-8 first
+ $slug = mb_convert_encoding($slug, "UTF-8", "UTF-8");
+
+ // Transliterate local characters like ΓΌ, I wonder how would it work for weird alphabets :^)
+ $slug = iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", $slug);
+
+ // Remove Tinyboard custom markup
+ $slug = preg_replace("/<tinyboard [^>]+>.*?<\/tinyboard>/s", '', $slug);
+
+ // Downcase everything
+ $slug = strtolower($slug);
+
+ // Strip bad characters, alphanumerics should suffice
+ $slug = preg_replace('/[^a-zA-Z0-9]/', '-', $slug);
+
+ // Replace multiple dashes with single ones
+ $slug = preg_replace('/-+/', '-', $slug);
+
+ // Strip dashes at the beginning and at the end
+ $slug = preg_replace('/^-|-$/', '', $slug);
+
+ // Slug should be X characters long, at max (80?)
+ $slug = substr($slug, 0, $config['slug_max_size']);
+
+ // Slug is now ready
+ return $slug;
+}
+
+function link_for($post, $page50 = false, $foreignlink = false, $thread = false) {
+ global $config, $board;
+
+ $post = (array)$post;
+
+ // Where do we need to look for OP?
+ $b = $foreignlink ? $foreignlink : (isset($post['board']) ? array('uri' => $post['board']) : $board);
+
+ $id = (isset($post['thread']) && $post['thread']) ? $post['thread'] : $post['id'];
+
+ $slug = false;
+
+ if ($config['slugify'] && ( (isset($post['thread']) && $post['thread']) || !isset ($post['slug']) ) ) {
+ $cvar = "slug_".$b['uri']."_".$id;
+ if (!$thread) {
+ $slug = Cache::get($cvar);
+
+ if ($slug === false) {
+ $query = prepare(sprintf("SELECT `slug` FROM ``posts_%s`` WHERE `id` = :id", $b['uri']));
+ $query->bindValue(':id', $id, PDO::PARAM_INT);
+ $query->execute() or error(db_error($query));
+
+ $thread = $query->fetch(PDO::FETCH_ASSOC);
+
+ $slug = $thread['slug'];
+
+ Cache::set($cvar, $slug);
+ }
+ }
+ else {
+ $slug = $thread['slug'];
+ }
+ }
+ elseif ($config['slugify']) {
+ $slug = $post['slug'];
+ }
+
+
+ if ( $page50 && $slug) $tpl = $config['file_page50_slug'];
+ else if (!$page50 && $slug) $tpl = $config['file_page_slug'];
+ else if ( $page50 && !$slug) $tpl = $config['file_page50'];
+ else if (!$page50 && !$slug) $tpl = $config['file_page'];
+
+ return sprintf($tpl, $id, $slug);
+}