1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-12-01 02:27:24 +01:00

Merge pull request #796 from Zankaria/mod-use-di

Add the context dependency injection container to the mod pages
This commit is contained in:
Lorenzo Yario 2024-08-16 21:43:07 -07:00 committed by GitHub
commit 84a3bedd18
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 183 additions and 162 deletions

View File

@ -1,8 +1,8 @@
<?php <?php
/* /*
* Copyright (c) 2010-2013 Tinyboard Development Group * Copyright (c) 2010-2013 Tinyboard Development Group
*/ */
use Vichan\Context;
use Vichan\Functions\Format; use Vichan\Functions\Format;
use Vichan\Functions\Net; use Vichan\Functions\Net;
@ -30,8 +30,8 @@ function mod_page($title, $template, $args, $subtitle = false) {
); );
} }
function mod_login($redirect = false) { function mod_login(Context $ctx, $redirect = false) {
global $config; $config = $ctx->get('config');
$args = []; $args = [];
@ -67,22 +67,23 @@ function mod_login($redirect = false) {
mod_page(_('Login'), $config['file_mod_login'], $args); mod_page(_('Login'), $config['file_mod_login'], $args);
} }
function mod_confirm($request) { function mod_confirm(Context $ctx, $request) {
global $config; $config = $ctx->get('config');
mod_page(_('Confirm action'), $config['file_mod_confim'], array('request' => $request, 'token' => make_secure_link_token($request))); mod_page(_('Confirm action'), $config['file_mod_confim'], array('request' => $request, 'token' => make_secure_link_token($request)));
} }
function mod_logout() { function mod_logout(Context $ctx) {
global $config; $config = $ctx->get('config');
destroyCookies(); destroyCookies();
header('Location: ?/', true, $config['redirect_http']); header('Location: ?/', true, $config['redirect_http']);
} }
function mod_dashboard() { function mod_dashboard(Context $ctx) {
global $config, $mod; global $mod;
$config = $ctx->get('config');
$args = array(); $args = [];
$args['boards'] = listBoards(); $args['boards'] = listBoards();
@ -173,8 +174,8 @@ function mod_dashboard() {
mod_page(_('Dashboard'), $config['file_mod_dashboard'], $args); mod_page(_('Dashboard'), $config['file_mod_dashboard'], $args);
} }
function mod_search_redirect() { function mod_search_redirect(Context $ctx) {
global $config; $config = $ctx->get('config');
if (!hasPermission($config['mod']['search'])) if (!hasPermission($config['mod']['search']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
@ -196,7 +197,7 @@ function mod_search_redirect() {
} }
} }
function mod_search($type, $search_query_escaped, $page_no = 1) { function mod_search(Context $ctx, $type, $search_query_escaped, $page_no = 1) {
global $pdo, $config; global $pdo, $config;
if (!hasPermission($config['mod']['search'])) if (!hasPermission($config['mod']['search']))
@ -222,7 +223,7 @@ function mod_search($type, $search_query_escaped, $page_no = 1) {
$query = str_replace('`', '!`', $query); $query = str_replace('`', '!`', $query);
// Array of phrases to match // Array of phrases to match
$match = array(); $match = [];
// Exact phrases ("like this") // Exact phrases ("like this")
if (preg_match_all('/"(.+?)"/', $query, $exact_phrases)) { if (preg_match_all('/"(.+?)"/', $query, $exact_phrases)) {
@ -351,7 +352,7 @@ function mod_search($type, $search_query_escaped, $page_no = 1) {
)); ));
} }
function mod_edit_board($boardName) { function mod_edit_board(Context $ctx, $boardName) {
global $board, $config; global $board, $config;
if (!openBoard($boardName)) if (!openBoard($boardName))
@ -453,8 +454,9 @@ function mod_edit_board($boardName) {
} }
} }
function mod_new_board() { function mod_new_board(Context $ctx) {
global $config, $board; global $board;
$config = $ctx->get('config');
if (!hasPermission($config['mod']['newboard'])) if (!hasPermission($config['mod']['newboard']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
@ -522,8 +524,9 @@ function mod_new_board() {
mod_page(_('New board'), $config['file_mod_board'], array('new' => true, 'token' => make_secure_link_token('new-board'))); mod_page(_('New board'), $config['file_mod_board'], array('new' => true, 'token' => make_secure_link_token('new-board')));
} }
function mod_noticeboard($page_no = 1) { function mod_noticeboard(Context $ctx, $page_no = 1) {
global $config, $pdo, $mod; global $pdo, $mod;
$config = $ctx->get('config');
if ($page_no < 1) if ($page_no < 1)
error($config['error']['404']); error($config['error']['404']);
@ -577,8 +580,8 @@ function mod_noticeboard($page_no = 1) {
)); ));
} }
function mod_noticeboard_delete($id) { function mod_noticeboard_delete(Context $ctx, $id) {
global $config; $config = $ctx->get('config');
if (!hasPermission($config['mod']['noticeboard_delete'])) if (!hasPermission($config['mod']['noticeboard_delete']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
@ -595,8 +598,9 @@ function mod_noticeboard_delete($id) {
header('Location: ?/noticeboard', true, $config['redirect_http']); header('Location: ?/noticeboard', true, $config['redirect_http']);
} }
function mod_news($page_no = 1) { function mod_news(Context $ctx, $page_no = 1) {
global $config, $pdo, $mod; global $pdo, $mod;
$config = $ctx->get('config');
if ($page_no < 1) if ($page_no < 1)
error($config['error']['404']); error($config['error']['404']);
@ -642,8 +646,8 @@ function mod_news($page_no = 1) {
mod_page(_('News'), $config['file_mod_news'], array('news' => $news, 'count' => $count, 'token' => make_secure_link_token('edit_news'))); mod_page(_('News'), $config['file_mod_news'], array('news' => $news, 'count' => $count, 'token' => make_secure_link_token('edit_news')));
} }
function mod_news_delete($id) { function mod_news_delete(Context $ctx, $id) {
global $config; $config = $ctx->get('config');
if (!hasPermission($config['mod']['news_delete'])) if (!hasPermission($config['mod']['news_delete']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
@ -657,8 +661,8 @@ function mod_news_delete($id) {
header('Location: ?/edit_news', true, $config['redirect_http']); header('Location: ?/edit_news', true, $config['redirect_http']);
} }
function mod_log($page_no = 1) { function mod_log(Context $ctx, $page_no = 1) {
global $config; $config = $ctx->get('config');
if ($page_no < 1) if ($page_no < 1)
error($config['error']['404']); error($config['error']['404']);
@ -682,8 +686,8 @@ function mod_log($page_no = 1) {
mod_page(_('Moderation log'), $config['file_mod_log'], array('logs' => $logs, 'count' => $count)); mod_page(_('Moderation log'), $config['file_mod_log'], array('logs' => $logs, 'count' => $count));
} }
function mod_user_log($username, $page_no = 1) { function mod_user_log(Context $ctx, $username, $page_no = 1) {
global $config; $config = $ctx->get('config');
if ($page_no < 1) if ($page_no < 1)
error($config['error']['404']); error($config['error']['404']);
@ -709,8 +713,8 @@ function mod_user_log($username, $page_no = 1) {
mod_page(_('Moderation log'), $config['file_mod_log'], array('logs' => $logs, 'count' => $count, 'username' => $username)); mod_page(_('Moderation log'), $config['file_mod_log'], array('logs' => $logs, 'count' => $count, 'username' => $username));
} }
function mod_board_log($board, $page_no = 1, $hide_names = false, $public = false) { function mod_board_log(Context $ctx, $board, $page_no = 1, $hide_names = false, $public = false) {
global $config; $config = $ctx->get('config');
if ($page_no < 1) if ($page_no < 1)
error($config['error']['404']); error($config['error']['404']);
@ -745,10 +749,10 @@ function mod_board_log($board, $page_no = 1, $hide_names = false, $public = fals
mod_page(_('Board log'), $config['file_mod_log'], array('logs' => $logs, 'count' => $count, 'board' => $board, 'hide_names' => $hide_names, 'public' => $public)); mod_page(_('Board log'), $config['file_mod_log'], array('logs' => $logs, 'count' => $count, 'board' => $board, 'hide_names' => $hide_names, 'public' => $public));
} }
function mod_view_catalog($boardName) { function mod_view_catalog(Context $ctx, $boardName) {
global $config, $mod; $config = $ctx->get('config');
require_once($config['dir']['themes'].'/catalog/theme.php'); require_once($config['dir']['themes'].'/catalog/theme.php');
$settings = array(); $settings = [];
$settings['boards'] = $boardName; $settings['boards'] = $boardName;
$settings['update_on_posts'] = true; $settings['update_on_posts'] = true;
$settings['title'] = 'Catalog'; $settings['title'] = 'Catalog';
@ -757,8 +761,9 @@ function mod_view_catalog($boardName) {
echo $catalog->build($settings, $boardName, true); echo $catalog->build($settings, $boardName, true);
} }
function mod_view_board($boardName, $page_no = 1) { function mod_view_board(Context $ctx, $boardName, $page_no = 1) {
global $config, $mod; global $mod;
$config = $ctx->get('config');
if (!openBoard($boardName)) if (!openBoard($boardName))
error($config['error']['noboard']); error($config['error']['noboard']);
@ -776,8 +781,9 @@ function mod_view_board($boardName, $page_no = 1) {
echo Element($config['file_board_index'], $page); echo Element($config['file_board_index'], $page);
} }
function mod_view_thread($boardName, $thread) { function mod_view_thread(Context $ctx, $boardName, $thread) {
global $config, $mod; global $mod;
$config = $ctx->get('config');
if (!openBoard($boardName)) if (!openBoard($boardName))
error($config['error']['noboard']); error($config['error']['noboard']);
@ -786,8 +792,9 @@ function mod_view_thread($boardName, $thread) {
echo $page; echo $page;
} }
function mod_view_thread50($boardName, $thread) { function mod_view_thread50(Context $ctx, $boardName, $thread) {
global $config, $mod; global $mod;
$config = $ctx->get('config');
if (!openBoard($boardName)) if (!openBoard($boardName))
error($config['error']['noboard']); error($config['error']['noboard']);
@ -796,12 +803,12 @@ function mod_view_thread50($boardName, $thread) {
echo $page; echo $page;
} }
function mod_ip_remove_note($cloaked_ip, $id) { function mod_ip_remove_note(Context $ctx, $cloaked_ip, $id) {
$ip = uncloak_ip($cloaked_ip); $ip = uncloak_ip($cloaked_ip);
global $config, $mod; $config = $ctx->get('config');
if (!hasPermission($config['mod']['remove_notes'])) if (!hasPermission($config['mod']['remove_notes']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
if (filter_var($ip, FILTER_VALIDATE_IP) === false) if (filter_var($ip, FILTER_VALIDATE_IP) === false)
error("Invalid IP address."); error("Invalid IP address.");
@ -818,9 +825,10 @@ function mod_ip_remove_note($cloaked_ip, $id) {
function mod_ip($cip) { function mod_ip(Context $ctx, $cip) {
$ip = uncloak_ip($cip); $ip = uncloak_ip($cip);
global $config, $mod; global $mod;
$config = $ctx->get('config');
if (filter_var($ip, FILTER_VALIDATE_IP) === false) if (filter_var($ip, FILTER_VALIDATE_IP) === false)
error("Invalid IP address."); error("Invalid IP address.");
@ -863,9 +871,9 @@ function mod_ip($cip) {
} }
$args = array(); $args = [];
$args['ip'] = $ip; $args['ip'] = $ip;
$args['posts'] = array(); $args['posts'] = [];
if ($config['mod']['dns_lookup'] && empty($config['ipcrypt_key'])) if ($config['mod']['dns_lookup'] && empty($config['ipcrypt_key']))
$args['hostname'] = rDNS($ip); $args['hostname'] = rDNS($ip);
@ -888,7 +896,7 @@ function mod_ip($cip) {
} }
if (!isset($args['posts'][$board['uri']])) if (!isset($args['posts'][$board['uri']]))
$args['posts'][$board['uri']] = array('board' => $board, 'posts' => array()); $args['posts'][$board['uri']] = array('board' => $board, 'posts' => []);
$args['posts'][$board['uri']]['posts'][] = $po->build(true); $args['posts'][$board['uri']]['posts'][] = $po->build(true);
} }
} }
@ -913,7 +921,7 @@ function mod_ip($cip) {
$query->execute() or error(db_error($query)); $query->execute() or error(db_error($query));
$args['logs'] = $query->fetchAll(PDO::FETCH_ASSOC); $args['logs'] = $query->fetchAll(PDO::FETCH_ASSOC);
} else { } else {
$args['logs'] = array(); $args['logs'] = [];
} }
$args['security_token'] = make_secure_link_token('IP/' . $cip); $args['security_token'] = make_secure_link_token('IP/' . $cip);
@ -921,8 +929,8 @@ function mod_ip($cip) {
mod_page(sprintf('%s: %s', _('IP'), htmlspecialchars($cip)), $config['file_mod_view_ip'], $args, $args['hostname']); mod_page(sprintf('%s: %s', _('IP'), htmlspecialchars($cip)), $config['file_mod_view_ip'], $args, $args['hostname']);
} }
function mod_edit_ban($ban_id) { function mod_edit_ban(Context $ctx, $ban_id) {
global $mod, $config; $config = $ctx->get('config');
if (!hasPermission($config['mod']['edit_ban'])) if (!hasPermission($config['mod']['edit_ban']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
@ -971,9 +979,8 @@ function mod_edit_ban($ban_id) {
} }
function mod_ban(Context $ctx) {
function mod_ban() { $config = $ctx->get('config');
global $config;
if (!hasPermission($config['mod']['ban'])) if (!hasPermission($config['mod']['ban']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
@ -991,9 +998,9 @@ function mod_ban() {
header('Location: ?/', true, $config['redirect_http']); header('Location: ?/', true, $config['redirect_http']);
} }
function mod_bans() { function mod_bans(Context $ctx) {
global $config;
global $mod; global $mod;
$config = $ctx->get('config');
if (!hasPermission($config['mod']['view_banlist'])) if (!hasPermission($config['mod']['view_banlist']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
@ -1002,7 +1009,7 @@ function mod_bans() {
if (!hasPermission($config['mod']['unban'])) if (!hasPermission($config['mod']['unban']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
$unban = array(); $unban = [];
foreach ($_POST as $name => $unused) { foreach ($_POST as $name => $unused) {
if (preg_match('/^ban_(\d+)$/', $name, $match)) if (preg_match('/^ban_(\d+)$/', $name, $match))
$unban[] = $match[1]; $unban[] = $match[1];
@ -1026,11 +1033,12 @@ function mod_bans() {
)); ));
} }
function mod_bans_json() { function mod_bans_json(Context $ctx) {
global $config, $mod; global $mod;
$config = $ctx->get('config');
if (!hasPermission($config['mod']['ban'])) if (!hasPermission($config['mod']['ban']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
// Compress the json for faster loads // Compress the json for faster loads
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler");
@ -1038,8 +1046,9 @@ function mod_bans_json() {
Bans::stream_json(false, false, !hasPermission($config['mod']['view_banstaff']), $mod['boards']); Bans::stream_json(false, false, !hasPermission($config['mod']['view_banstaff']), $mod['boards']);
} }
function mod_ban_appeals() { function mod_ban_appeals(Context $ctx) {
global $config, $board; global $board;
$config = $ctx->get('config');
if (!hasPermission($config['mod']['view_ban_appeals'])) if (!hasPermission($config['mod']['view_ban_appeals']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
@ -1085,16 +1094,16 @@ function mod_ban_appeals() {
$query = query(sprintf("SELECT `num_files`, `files` FROM ``posts_%s`` WHERE `id` = " . $query = query(sprintf("SELECT `num_files`, `files` FROM ``posts_%s`` WHERE `id` = " .
(int)$ban['post']['id'], $board['uri'])); (int)$ban['post']['id'], $board['uri']));
if ($_post = $query->fetch(PDO::FETCH_ASSOC)) { if ($_post = $query->fetch(PDO::FETCH_ASSOC)) {
$_post['files'] = $_post['files'] ? json_decode($_post['files']) : array(); $_post['files'] = $_post['files'] ? json_decode($_post['files']) : [];
$ban['post'] = array_merge($ban['post'], $_post); $ban['post'] = array_merge($ban['post'], $_post);
} else { } else {
$ban['post']['files'] = array(array()); $ban['post']['files'] = array([]);
$ban['post']['files'][0]['file'] = 'deleted'; $ban['post']['files'][0]['file'] = 'deleted';
$ban['post']['files'][0]['thumb'] = false; $ban['post']['files'][0]['thumb'] = false;
$ban['post']['num_files'] = 1; $ban['post']['num_files'] = 1;
} }
} else { } else {
$ban['post']['files'] = array(array()); $ban['post']['files'] = array([]);
$ban['post']['files'][0]['file'] = 'deleted'; $ban['post']['files'][0]['file'] = 'deleted';
$ban['post']['files'][0]['thumb'] = false; $ban['post']['files'][0]['thumb'] = false;
$ban['post']['num_files'] = 1; $ban['post']['num_files'] = 1;
@ -1114,8 +1123,8 @@ function mod_ban_appeals() {
)); ));
} }
function mod_lock($board, $unlock, $post) { function mod_lock(Context $ctx, $board, $unlock, $post) {
global $config; $config = $ctx->get('config');
if (!openBoard($board)) if (!openBoard($board))
error($config['error']['noboard']); error($config['error']['noboard']);
@ -1148,8 +1157,8 @@ function mod_lock($board, $unlock, $post) {
event('lock', $post); event('lock', $post);
} }
function mod_sticky($board, $unsticky, $post) { function mod_sticky(Context $ctx, $board, $unsticky, $post) {
global $config; $config = $ctx->get('config');
if (!openBoard($board)) if (!openBoard($board))
error($config['error']['noboard']); error($config['error']['noboard']);
@ -1170,8 +1179,8 @@ function mod_sticky($board, $unsticky, $post) {
header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']); header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']);
} }
function mod_cycle($board, $uncycle, $post) { function mod_cycle(Context $ctx, $board, $uncycle, $post) {
global $config; $config = $ctx->get('config');
if (!openBoard($board)) if (!openBoard($board))
error($config['error']['noboard']); error($config['error']['noboard']);
@ -1192,8 +1201,8 @@ function mod_cycle($board, $uncycle, $post) {
header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']); header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']);
} }
function mod_bumplock($board, $unbumplock, $post) { function mod_bumplock(Context $ctx, $board, $unbumplock, $post) {
global $config; $config = $ctx->get('config');
if (!openBoard($board)) if (!openBoard($board))
error($config['error']['noboard']); error($config['error']['noboard']);
@ -1214,8 +1223,8 @@ function mod_bumplock($board, $unbumplock, $post) {
header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']); header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']);
} }
function mod_move_reply($originBoard, $postID) { function mod_move_reply(Context $ctx, $originBoard, $postID) {
global $board, $config, $mod; global $board, $config;
if (!openBoard($originBoard)) if (!openBoard($originBoard))
error($config['error']['noboard']); error($config['error']['noboard']);
@ -1316,8 +1325,8 @@ function mod_move_reply($originBoard, $postID) {
} }
function mod_move($originBoard, $postID) { function mod_move(Context $ctx, $originBoard, $postID) {
global $board, $config, $mod, $pdo; global $board, $config, $pdo;
if (!openBoard($originBoard)) if (!openBoard($originBoard))
error($config['error']['noboard']); error($config['error']['noboard']);
@ -1386,7 +1395,7 @@ function mod_move($originBoard, $postID) {
$query->bindValue(':id', $postID, PDO::PARAM_INT); $query->bindValue(':id', $postID, PDO::PARAM_INT);
$query->execute() or error(db_error($query)); $query->execute() or error(db_error($query));
$replies = array(); $replies = [];
while ($post = $query->fetch(PDO::FETCH_ASSOC)) { while ($post = $query->fetch(PDO::FETCH_ASSOC)) {
$post['mod'] = true; $post['mod'] = true;
@ -1450,7 +1459,7 @@ function mod_move($originBoard, $postID) {
if (!empty($post['tracked_cites'])) { if (!empty($post['tracked_cites'])) {
$insert_rows = array(); $insert_rows = [];
foreach ($post['tracked_cites'] as $cite) { foreach ($post['tracked_cites'] as $cite) {
$insert_rows[] = '(' . $insert_rows[] = '(' .
$pdo->quote($board['uri']) . ', ' . $newPostID . ', ' . $pdo->quote($board['uri']) . ', ' . $newPostID . ', ' .
@ -1526,8 +1535,8 @@ function mod_move($originBoard, $postID) {
mod_page(_('Move thread'), $config['file_mod_move'], array('post' => $postID, 'board' => $originBoard, 'boards' => $boards, 'token' => $security_token)); mod_page(_('Move thread'), $config['file_mod_move'], array('post' => $postID, 'board' => $originBoard, 'boards' => $boards, 'token' => $security_token));
} }
function mod_ban_post($board, $delete, $post, $token = false) { function mod_ban_post(Context $ctx, $board, $delete, $post, $token = false) {
global $config, $mod; $config = $ctx->get('config');
if (!openBoard($board)) if (!openBoard($board))
error($config['error']['noboard']); error($config['error']['noboard']);
@ -1596,8 +1605,8 @@ function mod_ban_post($board, $delete, $post, $token = false) {
mod_page(_('New ban'), $config['file_mod_ban_form'], $args); mod_page(_('New ban'), $config['file_mod_ban_form'], $args);
} }
function mod_edit_post($board, $edit_raw_html, $postID) { function mod_edit_post(Context $ctx, $board, $edit_raw_html, $postID) {
global $config, $mod; $config = $ctx->get('config');
if (!openBoard($board)) if (!openBoard($board))
error($config['error']['noboard']); error($config['error']['noboard']);
@ -1673,8 +1682,8 @@ function mod_edit_post($board, $edit_raw_html, $postID) {
} }
} }
function mod_delete($board, $post) { function mod_delete(Context $ctx, $board, $post) {
global $config, $mod; $config = $ctx->get('config');
if (!openBoard($board)) if (!openBoard($board))
error($config['error']['noboard']); error($config['error']['noboard']);
@ -1694,8 +1703,8 @@ function mod_delete($board, $post) {
header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']); header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']);
} }
function mod_deletefile($board, $post, $file) { function mod_deletefile(Context $ctx, $board, $post, $file) {
global $config, $mod; $config = $ctx->get('config');
if (!openBoard($board)) if (!openBoard($board))
error($config['error']['noboard']); error($config['error']['noboard']);
@ -1717,8 +1726,8 @@ function mod_deletefile($board, $post, $file) {
header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']); header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']);
} }
function mod_spoiler_image($board, $post, $file) { function mod_spoiler_image(Context $ctx, $board, $post, $file) {
global $config, $mod; $config = $ctx->get('config');
if (!openBoard($board)) if (!openBoard($board))
error($config['error']['noboard']); error($config['error']['noboard']);
@ -1762,8 +1771,9 @@ function mod_spoiler_image($board, $post, $file) {
header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']); header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']);
} }
function mod_deletebyip($boardName, $post, $global = false) { function mod_deletebyip(Context $ctx, $boardName, $post, $global = false) {
global $config, $mod, $board; global $board;
$config = $ctx->get('config');
$global = (bool)$global; $global = (bool)$global;
@ -1800,8 +1810,8 @@ function mod_deletebyip($boardName, $post, $global = false) {
@set_time_limit($config['mod']['rebuild_timelimit']); @set_time_limit($config['mod']['rebuild_timelimit']);
$threads_to_rebuild = array(); $threads_to_rebuild = [];
$threads_deleted = array(); $threads_deleted = [];
while ($post = $query->fetch(PDO::FETCH_ASSOC)) { while ($post = $query->fetch(PDO::FETCH_ASSOC)) {
openBoard($post['board']); openBoard($post['board']);
@ -1838,8 +1848,9 @@ function mod_deletebyip($boardName, $post, $global = false) {
header('Location: ?/' . sprintf($config['board_path'], $boardName) . $config['file_index'], true, $config['redirect_http']); header('Location: ?/' . sprintf($config['board_path'], $boardName) . $config['file_index'], true, $config['redirect_http']);
} }
function mod_user($uid) { function mod_user(Context $ctx, $uid) {
global $config, $mod; global $mod;
$config = $ctx->get('config');
if (!hasPermission($config['mod']['editusers']) && !(hasPermission($config['mod']['change_password']) && $uid == $mod['id'])) if (!hasPermission($config['mod']['editusers']) && !(hasPermission($config['mod']['change_password']) && $uid == $mod['id']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
@ -1859,7 +1870,7 @@ function mod_user($uid) {
$board = $board['uri']; $board = $board['uri'];
} }
$boards = array(); $boards = [];
foreach ($_POST as $name => $value) { foreach ($_POST as $name => $value) {
if (preg_match('/^board_(' . $config['board_regex'] . ')$/u', $name, $matches) && in_array($matches[1], $_boards)) if (preg_match('/^board_(' . $config['board_regex'] . ')$/u', $name, $matches) && in_array($matches[1], $_boards))
$boards[] = $matches[1]; $boards[] = $matches[1];
@ -1950,7 +1961,7 @@ function mod_user($uid) {
$query->execute() or error(db_error($query)); $query->execute() or error(db_error($query));
$log = $query->fetchAll(PDO::FETCH_ASSOC); $log = $query->fetchAll(PDO::FETCH_ASSOC);
} else { } else {
$log = array(); $log = [];
} }
$user['boards'] = explode(',', $user['boards']); $user['boards'] = explode(',', $user['boards']);
@ -1963,7 +1974,7 @@ function mod_user($uid) {
)); ));
} }
function mod_user_new() { function mod_user_new(Context $ctx) {
global $pdo, $config; global $pdo, $config;
if (!hasPermission($config['mod']['createusers'])) if (!hasPermission($config['mod']['createusers']))
@ -1983,7 +1994,7 @@ function mod_user_new() {
$board = $board['uri']; $board = $board['uri'];
} }
$boards = array(); $boards = [];
foreach ($_POST as $name => $value) { foreach ($_POST as $name => $value) {
if (preg_match('/^board_(' . $config['board_regex'] . ')$/u', $name, $matches) && in_array($matches[1], $_boards)) if (preg_match('/^board_(' . $config['board_regex'] . ')$/u', $name, $matches) && in_array($matches[1], $_boards))
$boards[] = $matches[1]; $boards[] = $matches[1];
@ -2016,8 +2027,8 @@ function mod_user_new() {
} }
function mod_users() { function mod_users(Context $ctx) {
global $config; $config = $ctx->get('config');
if (!hasPermission($config['mod']['manageusers'])) if (!hasPermission($config['mod']['manageusers']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
@ -2037,8 +2048,8 @@ function mod_users() {
mod_page(sprintf('%s (%d)', _('Manage users'), count($users)), $config['file_mod_users'], array('users' => $users)); mod_page(sprintf('%s (%d)', _('Manage users'), count($users)), $config['file_mod_users'], array('users' => $users));
} }
function mod_user_promote($uid, $action) { function mod_user_promote(Context $ctx, $uid, $action) {
global $config; $config = $ctx->get('config');
if (!hasPermission($config['mod']['promoteusers'])) if (!hasPermission($config['mod']['promoteusers']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
@ -2080,7 +2091,7 @@ function mod_user_promote($uid, $action) {
header('Location: ?/users', true, $config['redirect_http']); header('Location: ?/users', true, $config['redirect_http']);
} }
function mod_pm($id, $reply = false) { function mod_pm(Context $ctx, $id, $reply = false) {
global $mod, $config; global $mod, $config;
if ($reply && !hasPermission($config['mod']['create_pm'])) if ($reply && !hasPermission($config['mod']['create_pm']))
@ -2135,8 +2146,9 @@ function mod_pm($id, $reply = false) {
} }
} }
function mod_inbox() { function mod_inbox(Context $ctx) {
global $config, $mod; global $mod;
$config = $ctx->get('config');
$query = prepare('SELECT `unread`,``pms``.`id`, `time`, `sender`, `to`, `message`, `username` FROM ``pms`` LEFT JOIN ``mods`` ON ``mods``.`id` = `sender` WHERE `to` = :mod ORDER BY `unread` DESC, `time` DESC'); $query = prepare('SELECT `unread`,``pms``.`id`, `time`, `sender`, `to`, `message`, `username` FROM ``pms`` LEFT JOIN ``mods`` ON ``mods``.`id` = `sender` WHERE `to` = :mod ORDER BY `unread` DESC, `time` DESC');
$query->bindValue(':mod', $mod['id']); $query->bindValue(':mod', $mod['id']);
@ -2159,8 +2171,9 @@ function mod_inbox() {
} }
function mod_new_pm($username) { function mod_new_pm(Context $ctx, $username) {
global $config, $mod; global $mod;
$config = $ctx->get('config');
if (!hasPermission($config['mod']['create_pm'])) if (!hasPermission($config['mod']['create_pm']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
@ -2207,8 +2220,9 @@ function mod_new_pm($username) {
)); ));
} }
function mod_rebuild() { function mod_rebuild(Context $ctx) {
global $config, $twig; global $twig;
$config = $ctx->get('config');
if (!hasPermission($config['mod']['rebuild'])) if (!hasPermission($config['mod']['rebuild']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
@ -2216,9 +2230,9 @@ function mod_rebuild() {
if (isset($_POST['rebuild'])) { if (isset($_POST['rebuild'])) {
@set_time_limit($config['mod']['rebuild_timelimit']); @set_time_limit($config['mod']['rebuild_timelimit']);
$log = array(); $log = [];
$boards = listBoards(); $boards = listBoards();
$rebuilt_scripts = array(); $rebuilt_scripts = [];
if (isset($_POST['rebuild_cache'])) { if (isset($_POST['rebuild_cache'])) {
if ($config['cache']['enabled']) { if ($config['cache']['enabled']) {
@ -2279,8 +2293,9 @@ function mod_rebuild() {
)); ));
} }
function mod_reports() { function mod_reports(Context $ctx) {
global $config, $mod; global $mod;
$config = $ctx->get('config');
if (!hasPermission($config['mod']['reports'])) if (!hasPermission($config['mod']['reports']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
@ -2290,16 +2305,16 @@ function mod_reports() {
$query->execute() or error(db_error($query)); $query->execute() or error(db_error($query));
$reports = $query->fetchAll(PDO::FETCH_ASSOC); $reports = $query->fetchAll(PDO::FETCH_ASSOC);
$report_queries = array(); $report_queries = [];
foreach ($reports as $report) { foreach ($reports as $report) {
if (!isset($report_queries[$report['board']])) if (!isset($report_queries[$report['board']]))
$report_queries[$report['board']] = array(); $report_queries[$report['board']] = [];
$report_queries[$report['board']][] = $report['post']; $report_queries[$report['board']][] = $report['post'];
} }
$report_posts = array(); $report_posts = [];
foreach ($report_queries as $board => $posts) { foreach ($report_queries as $board => $posts) {
$report_posts[$board] = array(); $report_posts[$board] = [];
$query = query(sprintf('SELECT * FROM ``posts_%s`` WHERE `id` = ' . implode(' OR `id` = ', $posts), $board)) or error(db_error()); $query = query(sprintf('SELECT * FROM ``posts_%s`` WHERE `id` = ' . implode(' OR `id` = ', $posts), $board)) or error(db_error());
while ($post = $query->fetch(PDO::FETCH_ASSOC)) { while ($post = $query->fetch(PDO::FETCH_ASSOC)) {
@ -2362,8 +2377,8 @@ function mod_reports() {
mod_page(sprintf('%s (%d)', _('Report queue'), $count), $config['file_mod_reports'], array('reports' => $body, 'count' => $count)); mod_page(sprintf('%s (%d)', _('Report queue'), $count), $config['file_mod_reports'], array('reports' => $body, 'count' => $count));
} }
function mod_report_dismiss($id, $action) { function mod_report_dismiss(Context $ctx, $id, $action) {
global $config; $config = $ctx->get('config');
$query = prepare("SELECT `post`, `board`, `ip` FROM ``reports`` WHERE `id` = :id"); $query = prepare("SELECT `post`, `board`, `ip` FROM ``reports`` WHERE `id` = :id");
$query->bindValue(':id', $id); $query->bindValue(':id', $id);
@ -2408,8 +2423,9 @@ function mod_report_dismiss($id, $action) {
header('Location: ?/reports', true, $config['redirect_http']); header('Location: ?/reports', true, $config['redirect_http']);
} }
function mod_recent_posts($lim) { function mod_recent_posts(Context $ctx, $lim) {
global $config, $mod, $pdo; global $mod, $pdo;
$config = $ctx->get('config');
if (!hasPermission($config['mod']['recent'])) if (!hasPermission($config['mod']['recent']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
@ -2417,7 +2433,7 @@ function mod_recent_posts($lim) {
$limit = (is_numeric($lim))? $lim : 25; $limit = (is_numeric($lim))? $lim : 25;
$last_time = (isset($_GET['last']) && is_numeric($_GET['last'])) ? $_GET['last'] : 0; $last_time = (isset($_GET['last']) && is_numeric($_GET['last'])) ? $_GET['last'] : 0;
$mod_boards = array(); $mod_boards = [];
$boards = listBoards(); $boards = listBoards();
//if not all boards //if not all boards
@ -2464,8 +2480,9 @@ function mod_recent_posts($lim) {
} }
function mod_config($board_config = false) { function mod_config(Context $ctx, $board_config = false) {
global $config, $mod, $board; global $mod, $board;
$config = $ctx->get('config');
if ($board_config && !openBoard($board_config)) if ($board_config && !openBoard($board_config))
error($config['error']['noboard']); error($config['error']['noboard']);
@ -2576,7 +2593,7 @@ function mod_config($board_config = false) {
if ($config['minify_html']) if ($config['minify_html'])
$config_append = str_replace("\n", '&#010;', $config_append); $config_append = str_replace("\n", '&#010;', $config_append);
$page = array(); $page = [];
$page['title'] = 'Cannot write to file!'; $page['title'] = 'Cannot write to file!';
$page['config'] = $config; $page['config'] = $config;
$page['body'] = ' $page['body'] = '
@ -2604,8 +2621,8 @@ function mod_config($board_config = false) {
)); ));
} }
function mod_themes_list() { function mod_themes_list(Context $ctx) {
global $config; $config = $ctx->get('config');
if (!hasPermission($config['mod']['themes'])) if (!hasPermission($config['mod']['themes']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
@ -2619,7 +2636,7 @@ function mod_themes_list() {
$themes_in_use = $query->fetchAll(PDO::FETCH_COLUMN); $themes_in_use = $query->fetchAll(PDO::FETCH_COLUMN);
// Scan directory for themes // Scan directory for themes
$themes = array(); $themes = [];
while ($file = readdir($dir)) { while ($file = readdir($dir)) {
if ($file[0] != '.' && is_dir($config['dir']['themes'] . '/' . $file)) { if ($file[0] != '.' && is_dir($config['dir']['themes'] . '/' . $file)) {
$themes[$file] = Vichan\Functions\Theme\load_theme_config($file); $themes[$file] = Vichan\Functions\Theme\load_theme_config($file);
@ -2638,8 +2655,8 @@ function mod_themes_list() {
)); ));
} }
function mod_theme_configure($theme_name) { function mod_theme_configure(Context $ctx, $theme_name) {
global $config; $config = $ctx->get('config');
if (!hasPermission($config['mod']['themes'])) if (!hasPermission($config['mod']['themes']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
@ -2720,8 +2737,8 @@ function mod_theme_configure($theme_name) {
)); ));
} }
function mod_theme_uninstall($theme_name) { function mod_theme_uninstall(Context $ctx, $theme_name) {
global $config; $config = $ctx->get('config');
if (!hasPermission($config['mod']['themes'])) if (!hasPermission($config['mod']['themes']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
@ -2737,8 +2754,8 @@ function mod_theme_uninstall($theme_name) {
header('Location: ?/themes', true, $config['redirect_http']); header('Location: ?/themes', true, $config['redirect_http']);
} }
function mod_theme_rebuild($theme_name) { function mod_theme_rebuild(Context $ctx, $theme_name) {
global $config; $config = $ctx->get('config');
if (!hasPermission($config['mod']['themes'])) if (!hasPermission($config['mod']['themes']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
@ -2778,16 +2795,17 @@ function delete_page_base($page = '', $board = false) {
header('Location: ?/edit_pages' . ($board ? ('/' . $board) : ''), true, $config['redirect_http']); header('Location: ?/edit_pages' . ($board ? ('/' . $board) : ''), true, $config['redirect_http']);
} }
function mod_delete_page($page = '') { function mod_delete_page(Context $ctx, $page = '') {
delete_page_base($page); delete_page_base($ctx, $page);
} }
function mod_delete_page_board($page = '', $board = false) { function mod_delete_page_board(Context $ctx, $page = '', $board = false) {
delete_page_base($page, $board); delete_page_base($ctx, $page, $board);
} }
function mod_edit_page($id) { function mod_edit_page(Context $ctx, $id) {
global $config, $mod, $board; global $mod, $board;
$config = $ctx->get('config');
$query = prepare('SELECT * FROM ``pages`` WHERE `id` = :id'); $query = prepare('SELECT * FROM ``pages`` WHERE `id` = :id');
$query->bindValue(':id', $id); $query->bindValue(':id', $id);
@ -2857,8 +2875,9 @@ function mod_edit_page($id) {
mod_page(sprintf(_('Editing static page: %s'), $page['name']), $config['file_mod_edit_page'], array('page' => $page, 'token' => make_secure_link_token("edit_page/$id"), 'content' => prettify_textarea($content), 'board' => $board)); mod_page(sprintf(_('Editing static page: %s'), $page['name']), $config['file_mod_edit_page'], array('page' => $page, 'token' => make_secure_link_token("edit_page/$id"), 'content' => prettify_textarea($content), 'board' => $board));
} }
function mod_pages($board = false) { function mod_pages(Context $ctx, $board = false) {
global $config, $mod, $pdo; global $mod, $pdo;
$config = $ctx->get('config');
if (empty($board)) if (empty($board))
$board = false; $board = false;
@ -2911,10 +2930,10 @@ function mod_pages($board = false) {
mod_page(_('Pages'), $config['file_mod_pages'], array('pages' => $pages, 'token' => make_secure_link_token('edit_pages' . ($board ? ('/' . $board) : '')), 'board' => $board)); mod_page(_('Pages'), $config['file_mod_pages'], array('pages' => $pages, 'token' => make_secure_link_token('edit_pages' . ($board ? ('/' . $board) : '')), 'board' => $board));
} }
function mod_debug_antispam() { function mod_debug_antispam(Context $ctx) {
global $pdo, $config; global $pdo, $config;
$args = array(); $args = [];
if (isset($_POST['board'], $_POST['thread'])) { if (isset($_POST['board'], $_POST['thread'])) {
$where = '`board` = ' . $pdo->quote($_POST['board']); $where = '`board` = ' . $pdo->quote($_POST['board']);
@ -2948,7 +2967,7 @@ function mod_debug_antispam() {
mod_page(_('Debug: Anti-spam'), $config['file_mod_debug_antispam'], $args); mod_page(_('Debug: Anti-spam'), $config['file_mod_debug_antispam'], $args);
} }
function mod_debug_recent_posts() { function mod_debug_recent_posts(Context $ctx) {
global $pdo, $config; global $pdo, $config;
$limit = 500; $limit = 500;
@ -2982,8 +3001,8 @@ function mod_debug_recent_posts() {
mod_page(_('Debug: Recent posts'), $config['file_mod_debug_recent_posts'], array('posts' => $posts, 'flood_posts' => $flood_posts)); mod_page(_('Debug: Recent posts'), $config['file_mod_debug_recent_posts'], array('posts' => $posts, 'flood_posts' => $flood_posts));
} }
function mod_debug_sql() { function mod_debug_sql(Context $ctx) {
global $config; $config = $ctx->get('config');
if (!hasPermission($config['mod']['debug_sql'])) if (!hasPermission($config['mod']['debug_sql']))
error($config['error']['noaccess']); error($config['error']['noaccess']);

28
mod.php
View File

@ -1,13 +1,13 @@
<?php <?php
/* /*
* Copyright (c) 2010-2014 Tinyboard Development Group * Copyright (c) 2010-2014 Tinyboard Development Group
*/ */
require_once 'inc/bootstrap.php'; require_once 'inc/bootstrap.php';
if ($config['debug']) if ($config['debug']) {
$parse_start_time = microtime(true); $parse_start_time = microtime(true);
}
require_once 'inc/mod/pages.php'; require_once 'inc/mod/pages.php';
@ -15,7 +15,7 @@ check_login(true);
$query = isset($_SERVER['QUERY_STRING']) ? rawurldecode($_SERVER['QUERY_STRING']) : ''; $query = isset($_SERVER['QUERY_STRING']) ? rawurldecode($_SERVER['QUERY_STRING']) : '';
$pages = array( $pages = [
'' => ':?/', // redirect to dashboard '' => ':?/', // redirect to dashboard
'/' => 'dashboard', // dashboard '/' => 'dashboard', // dashboard
'/confirm/(.+)' => 'confirm', // confirm action (if javascript didn't work) '/confirm/(.+)' => 'confirm', // confirm action (if javascript didn't work)
@ -109,14 +109,14 @@ $pages = array(
str_replace('%d', '(\d+)', preg_quote($config['file_page'], '!')) => 'view_thread', str_replace('%d', '(\d+)', preg_quote($config['file_page'], '!')) => 'view_thread',
'/(\%b)/' . preg_quote($config['dir']['res'], '!') . '/(\%b)/' . preg_quote($config['dir']['res'], '!') .
str_replace(array('%d','%s'), array('(\d+)', '[a-z0-9-]+'), preg_quote($config['file_page50_slug'], '!')) => 'view_thread50', str_replace([ '%d','%s' ], [ '(\d+)', '[a-z0-9-]+' ], preg_quote($config['file_page50_slug'], '!')) => 'view_thread50',
'/(\%b)/' . preg_quote($config['dir']['res'], '!') . '/(\%b)/' . preg_quote($config['dir']['res'], '!') .
str_replace(array('%d','%s'), array('(\d+)', '[a-z0-9-]+'), preg_quote($config['file_page_slug'], '!')) => 'view_thread', str_replace([ '%d','%s' ], [ '(\d+)', '[a-z0-9-]+' ], preg_quote($config['file_page_slug'], '!')) => 'view_thread',
); ];
if (!$mod) { if (!$mod) {
$pages = array('!^(.+)?$!' => 'login'); $pages = [ '!^(.+)?$!' => 'login' ];
} elseif (isset($_GET['status'], $_GET['r'])) { } elseif (isset($_GET['status'], $_GET['r'])) {
header('Location: ' . $_GET['r'], true, (int)$_GET['status']); header('Location: ' . $_GET['r'], true, (int)$_GET['status']);
exit; exit;
@ -126,18 +126,21 @@ if (isset($config['mod']['custom_pages'])) {
$pages = array_merge($pages, $config['mod']['custom_pages']); $pages = array_merge($pages, $config['mod']['custom_pages']);
} }
$new_pages = array(); $new_pages = [];
foreach ($pages as $key => $callback) { foreach ($pages as $key => $callback) {
if (is_string($callback) && preg_match('/^secure /', $callback)) if (is_string($callback) && preg_match('/^secure /', $callback)) {
$key .= '(/(?P<token>[a-f0-9]{8}))?'; $key .= '(/(?P<token>[a-f0-9]{8}))?';
}
$key = str_replace('\%b', '?P<board>' . sprintf(substr($config['board_path'], 0, -1), $config['board_regex']), $key); $key = str_replace('\%b', '?P<board>' . sprintf(substr($config['board_path'], 0, -1), $config['board_regex']), $key);
$new_pages[(!empty($key) and $key[0] == '!') ? $key : '!^' . $key . '(?:&[^&=]+=[^&]*)*$!u'] = $callback; $new_pages[(!empty($key) and $key[0] == '!') ? $key : '!^' . $key . '(?:&[^&=]+=[^&]*)*$!u'] = $callback;
} }
$pages = $new_pages; $pages = $new_pages;
$ctx = Vichan\build_context($config);
foreach ($pages as $uri => $handler) { foreach ($pages as $uri => $handler) {
if (preg_match($uri, $query, $matches)) { if (preg_match($uri, $query, $matches)) {
$matches = array_slice($matches, 1); $matches[0] = $ctx; // Replace the text captured by the full pattern with a reference to the context.
if (isset($matches['board'])) { if (isset($matches['board'])) {
$board_match = $matches['board']; $board_match = $matches['board'];
@ -172,11 +175,11 @@ foreach ($pages as $uri => $handler) {
} }
if ($config['debug']) { if ($config['debug']) {
$debug['mod_page'] = array( $debug['mod_page'] = [
'req' => $query, 'req' => $query,
'match' => $uri, 'match' => $uri,
'handler' => $handler, 'handler' => $handler,
); ];
$debug['time']['parse_mod_req'] = '~' . round((microtime(true) - $parse_start_time) * 1000, 2) . 'ms'; $debug['time']['parse_mod_req'] = '~' . round((microtime(true) - $parse_start_time) * 1000, 2) . 'ms';
} }
@ -204,4 +207,3 @@ foreach ($pages as $uri => $handler) {
} }
error($config['error']['404']); error($config['error']['404']);