From 19082aec56e4ae677575d362ebb273a68b05f5b8 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Thu, 15 Aug 2024 16:52:29 +0200 Subject: [PATCH 1/4] mod.php: use modern array syntax --- mod.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/mod.php b/mod.php index 20cff8e5..f1e1245c 100644 --- a/mod.php +++ b/mod.php @@ -1,13 +1,13 @@ ':?/', // redirect to dashboard '/' => 'dashboard', // dashboard '/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', '/(\%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'], '!') . - 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) { - $pages = array('!^(.+)?$!' => 'login'); + $pages = [ '!^(.+)?$!' => 'login' ]; } elseif (isset($_GET['status'], $_GET['r'])) { header('Location: ' . $_GET['r'], true, (int)$_GET['status']); exit; @@ -126,10 +126,11 @@ if (isset($config['mod']['custom_pages'])) { $pages = array_merge($pages, $config['mod']['custom_pages']); } -$new_pages = array(); +$new_pages = []; foreach ($pages as $key => $callback) { - if (is_string($callback) && preg_match('/^secure /', $callback)) + if (is_string($callback) && preg_match('/^secure /', $callback)) { $key .= '(/(?P[a-f0-9]{8}))?'; + } $key = str_replace('\%b', '?P' . sprintf(substr($config['board_path'], 0, -1), $config['board_regex']), $key); $new_pages[(!empty($key) and $key[0] == '!') ? $key : '!^' . $key . '(?:&[^&=]+=[^&]*)*$!u'] = $callback; } @@ -172,11 +173,11 @@ foreach ($pages as $uri => $handler) { } if ($config['debug']) { - $debug['mod_page'] = array( + $debug['mod_page'] = [ 'req' => $query, 'match' => $uri, 'handler' => $handler, - ); + ]; $debug['time']['parse_mod_req'] = '~' . round((microtime(true) - $parse_start_time) * 1000, 2) . 'ms'; } @@ -204,4 +205,3 @@ foreach ($pages as $uri => $handler) { } error($config['error']['404']); - From 524ae9462411e39140bc5463631fc03eb964cc3a Mon Sep 17 00:00:00 2001 From: Zankaria Date: Thu, 15 Aug 2024 17:05:43 +0200 Subject: [PATCH 2/4] mod.php: pass the context to the mod pages --- inc/mod/pages.php | 156 +++++++++++++++++++++++----------------------- mod.php | 4 +- 2 files changed, 80 insertions(+), 80 deletions(-) diff --git a/inc/mod/pages.php b/inc/mod/pages.php index 46e8fc7e..bd5c7c1b 100644 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -1,8 +1,8 @@ $request, 'token' => make_secure_link_token($request))); } -function mod_logout() { +function mod_logout(Context $ctx) { global $config; destroyCookies(); header('Location: ?/', true, $config['redirect_http']); } -function mod_dashboard() { +function mod_dashboard(Context $ctx) { global $config, $mod; $args = array(); @@ -173,7 +173,7 @@ function mod_dashboard() { mod_page(_('Dashboard'), $config['file_mod_dashboard'], $args); } -function mod_search_redirect() { +function mod_search_redirect(Context $ctx) { global $config; if (!hasPermission($config['mod']['search'])) @@ -196,7 +196,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; if (!hasPermission($config['mod']['search'])) @@ -351,7 +351,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; if (!openBoard($boardName)) @@ -453,7 +453,7 @@ function mod_edit_board($boardName) { } } -function mod_new_board() { +function mod_new_board(Context $ctx) { global $config, $board; if (!hasPermission($config['mod']['newboard'])) @@ -522,7 +522,7 @@ function mod_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; if ($page_no < 1) @@ -577,7 +577,7 @@ function mod_noticeboard($page_no = 1) { )); } -function mod_noticeboard_delete($id) { +function mod_noticeboard_delete(Context $ctx, $id) { global $config; if (!hasPermission($config['mod']['noticeboard_delete'])) @@ -595,7 +595,7 @@ function mod_noticeboard_delete($id) { 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; if ($page_no < 1) @@ -642,7 +642,7 @@ 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'))); } -function mod_news_delete($id) { +function mod_news_delete(Context $ctx, $id) { global $config; if (!hasPermission($config['mod']['news_delete'])) @@ -657,7 +657,7 @@ function mod_news_delete($id) { header('Location: ?/edit_news', true, $config['redirect_http']); } -function mod_log($page_no = 1) { +function mod_log(Context $ctx, $page_no = 1) { global $config; if ($page_no < 1) @@ -682,7 +682,7 @@ function mod_log($page_no = 1) { 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; if ($page_no < 1) @@ -709,7 +709,7 @@ function mod_user_log($username, $page_no = 1) { 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; if ($page_no < 1) @@ -745,8 +745,8 @@ 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)); } -function mod_view_catalog($boardName) { - global $config, $mod; +function mod_view_catalog(Context $ctx, $boardName) { + global $config; require_once($config['dir']['themes'].'/catalog/theme.php'); $settings = array(); $settings['boards'] = $boardName; @@ -757,7 +757,7 @@ function mod_view_catalog($boardName) { 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; if (!openBoard($boardName)) @@ -776,7 +776,7 @@ function mod_view_board($boardName, $page_no = 1) { echo Element($config['file_board_index'], $page); } -function mod_view_thread($boardName, $thread) { +function mod_view_thread(Context $ctx, $boardName, $thread) { global $config, $mod; if (!openBoard($boardName)) @@ -786,7 +786,7 @@ function mod_view_thread($boardName, $thread) { echo $page; } -function mod_view_thread50($boardName, $thread) { +function mod_view_thread50(Context $ctx, $boardName, $thread) { global $config, $mod; if (!openBoard($boardName)) @@ -796,7 +796,7 @@ function mod_view_thread50($boardName, $thread) { 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); global $config, $mod; @@ -818,7 +818,7 @@ function mod_ip_remove_note($cloaked_ip, $id) { -function mod_ip($cip) { +function mod_ip(Context $ctx, $cip) { $ip = uncloak_ip($cip); global $config, $mod; @@ -921,8 +921,8 @@ function mod_ip($cip) { mod_page(sprintf('%s: %s', _('IP'), htmlspecialchars($cip)), $config['file_mod_view_ip'], $args, $args['hostname']); } -function mod_edit_ban($ban_id) { - global $mod, $config; +function mod_edit_ban(Context $ctx, $ban_id) { + global $config; if (!hasPermission($config['mod']['edit_ban'])) error($config['error']['noaccess']); @@ -971,8 +971,7 @@ function mod_edit_ban($ban_id) { } - -function mod_ban() { +function mod_ban(Context $ctx) { global $config; if (!hasPermission($config['mod']['ban'])) @@ -991,9 +990,8 @@ function mod_ban() { header('Location: ?/', true, $config['redirect_http']); } -function mod_bans() { - global $config; - global $mod; +function mod_bans(Context $ctx) { + global $config, $mod; if (!hasPermission($config['mod']['view_banlist'])) error($config['error']['noaccess']); @@ -1026,11 +1024,11 @@ function mod_bans() { )); } -function mod_bans_json() { - global $config, $mod; +function mod_bans_json(Context $ctx) { + global $config, $mod; - if (!hasPermission($config['mod']['ban'])) - error($config['error']['noaccess']); + if (!hasPermission($config['mod']['ban'])) + error($config['error']['noaccess']); // Compress the json for faster loads if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); @@ -1038,7 +1036,7 @@ function mod_bans_json() { 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; if (!hasPermission($config['mod']['view_ban_appeals'])) @@ -1114,7 +1112,7 @@ function mod_ban_appeals() { )); } -function mod_lock($board, $unlock, $post) { +function mod_lock(Context $ctx, $board, $unlock, $post) { global $config; if (!openBoard($board)) @@ -1148,7 +1146,7 @@ function mod_lock($board, $unlock, $post) { event('lock', $post); } -function mod_sticky($board, $unsticky, $post) { +function mod_sticky(Context $ctx, $board, $unsticky, $post) { global $config; if (!openBoard($board)) @@ -1170,7 +1168,7 @@ function mod_sticky($board, $unsticky, $post) { 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; if (!openBoard($board)) @@ -1192,7 +1190,7 @@ function mod_cycle($board, $uncycle, $post) { 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; if (!openBoard($board)) @@ -1214,8 +1212,8 @@ function mod_bumplock($board, $unbumplock, $post) { header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']); } -function mod_move_reply($originBoard, $postID) { - global $board, $config, $mod; +function mod_move_reply(Context $ctx, $originBoard, $postID) { + global $board, $config; if (!openBoard($originBoard)) error($config['error']['noboard']); @@ -1316,8 +1314,8 @@ function mod_move_reply($originBoard, $postID) { } -function mod_move($originBoard, $postID) { - global $board, $config, $mod, $pdo; +function mod_move(Context $ctx, $originBoard, $postID) { + global $board, $config, $pdo; if (!openBoard($originBoard)) error($config['error']['noboard']); @@ -1526,7 +1524,7 @@ function mod_move($originBoard, $postID) { 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; if (!openBoard($board)) @@ -1596,8 +1594,8 @@ function mod_ban_post($board, $delete, $post, $token = false) { mod_page(_('New ban'), $config['file_mod_ban_form'], $args); } -function mod_edit_post($board, $edit_raw_html, $postID) { - global $config, $mod; +function mod_edit_post(Context $ctx, $board, $edit_raw_html, $postID) { + global $config; if (!openBoard($board)) error($config['error']['noboard']); @@ -1673,8 +1671,8 @@ function mod_edit_post($board, $edit_raw_html, $postID) { } } -function mod_delete($board, $post) { - global $config, $mod; +function mod_delete(Context $ctx, $board, $post) { + global $config; if (!openBoard($board)) error($config['error']['noboard']); @@ -1694,8 +1692,8 @@ function mod_delete($board, $post) { header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']); } -function mod_deletefile($board, $post, $file) { - global $config, $mod; +function mod_deletefile(Context $ctx, $board, $post, $file) { + global $config; if (!openBoard($board)) error($config['error']['noboard']); @@ -1717,8 +1715,8 @@ function mod_deletefile($board, $post, $file) { header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']); } -function mod_spoiler_image($board, $post, $file) { - global $config, $mod; +function mod_spoiler_image(Context $ctx, $board, $post, $file) { + global $config; if (!openBoard($board)) error($config['error']['noboard']); @@ -1762,8 +1760,8 @@ function mod_spoiler_image($board, $post, $file) { header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']); } -function mod_deletebyip($boardName, $post, $global = false) { - global $config, $mod, $board; +function mod_deletebyip(Context $ctx, $boardName, $post, $global = false) { + global $config, $board; $global = (bool)$global; @@ -1838,7 +1836,7 @@ function mod_deletebyip($boardName, $post, $global = false) { 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; if (!hasPermission($config['mod']['editusers']) && !(hasPermission($config['mod']['change_password']) && $uid == $mod['id'])) @@ -1963,7 +1961,7 @@ function mod_user($uid) { )); } -function mod_user_new() { +function mod_user_new(Context $ctx) { global $pdo, $config; if (!hasPermission($config['mod']['createusers'])) @@ -2016,7 +2014,7 @@ function mod_user_new() { } -function mod_users() { +function mod_users(Context $ctx) { global $config; if (!hasPermission($config['mod']['manageusers'])) @@ -2037,7 +2035,7 @@ function mod_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; if (!hasPermission($config['mod']['promoteusers'])) @@ -2080,7 +2078,7 @@ function mod_user_promote($uid, $action) { header('Location: ?/users', true, $config['redirect_http']); } -function mod_pm($id, $reply = false) { +function mod_pm(Context $ctx, $id, $reply = false) { global $mod, $config; if ($reply && !hasPermission($config['mod']['create_pm'])) @@ -2135,7 +2133,7 @@ function mod_pm($id, $reply = false) { } } -function mod_inbox() { +function mod_inbox(Context $ctx) { global $config, $mod; $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'); @@ -2159,7 +2157,7 @@ function mod_inbox() { } -function mod_new_pm($username) { +function mod_new_pm(Context $ctx, $username) { global $config, $mod; if (!hasPermission($config['mod']['create_pm'])) @@ -2207,7 +2205,7 @@ function mod_new_pm($username) { )); } -function mod_rebuild() { +function mod_rebuild(Context $ctx) { global $config, $twig; if (!hasPermission($config['mod']['rebuild'])) @@ -2279,7 +2277,7 @@ function mod_rebuild() { )); } -function mod_reports() { +function mod_reports(Context $ctx) { global $config, $mod; if (!hasPermission($config['mod']['reports'])) @@ -2362,7 +2360,7 @@ function mod_reports() { 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; $query = prepare("SELECT `post`, `board`, `ip` FROM ``reports`` WHERE `id` = :id"); @@ -2408,7 +2406,7 @@ function mod_report_dismiss($id, $action) { header('Location: ?/reports', true, $config['redirect_http']); } -function mod_recent_posts($lim) { +function mod_recent_posts(Context $ctx, $lim) { global $config, $mod, $pdo; if (!hasPermission($config['mod']['recent'])) @@ -2464,7 +2462,7 @@ function mod_recent_posts($lim) { } -function mod_config($board_config = false) { +function mod_config(Context $ctx, $board_config = false) { global $config, $mod, $board; if ($board_config && !openBoard($board_config)) @@ -2604,7 +2602,7 @@ function mod_config($board_config = false) { )); } -function mod_themes_list() { +function mod_themes_list(Context $ctx) { global $config; if (!hasPermission($config['mod']['themes'])) @@ -2638,7 +2636,7 @@ function mod_themes_list() { )); } -function mod_theme_configure($theme_name) { +function mod_theme_configure(Context $ctx, $theme_name) { global $config; if (!hasPermission($config['mod']['themes'])) @@ -2720,7 +2718,7 @@ function mod_theme_configure($theme_name) { )); } -function mod_theme_uninstall($theme_name) { +function mod_theme_uninstall(Context $ctx, $theme_name) { global $config; if (!hasPermission($config['mod']['themes'])) @@ -2737,7 +2735,7 @@ function mod_theme_uninstall($theme_name) { header('Location: ?/themes', true, $config['redirect_http']); } -function mod_theme_rebuild($theme_name) { +function mod_theme_rebuild(Context $ctx, $theme_name) { global $config; if (!hasPermission($config['mod']['themes'])) @@ -2778,15 +2776,15 @@ function delete_page_base($page = '', $board = false) { header('Location: ?/edit_pages' . ($board ? ('/' . $board) : ''), true, $config['redirect_http']); } -function mod_delete_page($page = '') { - delete_page_base($page); +function mod_delete_page(Context $ctx, $page = '') { + delete_page_base($ctx, $page); } -function mod_delete_page_board($page = '', $board = false) { - delete_page_base($page, $board); +function mod_delete_page_board(Context $ctx, $page = '', $board = false) { + delete_page_base($ctx, $page, $board); } -function mod_edit_page($id) { +function mod_edit_page(Context $ctx, $id) { global $config, $mod, $board; $query = prepare('SELECT * FROM ``pages`` WHERE `id` = :id'); @@ -2857,7 +2855,7 @@ 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)); } -function mod_pages($board = false) { +function mod_pages(Context $ctx, $board = false) { global $config, $mod, $pdo; if (empty($board)) @@ -2911,7 +2909,7 @@ 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)); } -function mod_debug_antispam() { +function mod_debug_antispam(Context $ctx) { global $pdo, $config; $args = array(); @@ -2948,7 +2946,7 @@ function mod_debug_antispam() { 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; $limit = 500; @@ -2982,7 +2980,7 @@ function mod_debug_recent_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; if (!hasPermission($config['mod']['debug_sql'])) diff --git a/mod.php b/mod.php index f1e1245c..474210cd 100644 --- a/mod.php +++ b/mod.php @@ -136,9 +136,11 @@ foreach ($pages as $key => $callback) { } $pages = $new_pages; +$ctx = Vichan\build_context($config); + foreach ($pages as $uri => $handler) { 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'])) { $board_match = $matches['board']; From b64bac5eb8ea65f8043855566068391e88557c4e Mon Sep 17 00:00:00 2001 From: Zankaria Date: Thu, 15 Aug 2024 17:12:14 +0200 Subject: [PATCH 3/4] pages.php: use the context to access the configuration array --- inc/mod/pages.php | 125 +++++++++++++++++++++++++++------------------- 1 file changed, 73 insertions(+), 52 deletions(-) diff --git a/inc/mod/pages.php b/inc/mod/pages.php index bd5c7c1b..0793ccb4 100644 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -31,7 +31,7 @@ function mod_page($title, $template, $args, $subtitle = false) { } function mod_login(Context $ctx, $redirect = false) { - global $config; + $config = $ctx->get('config'); $args = []; @@ -68,19 +68,20 @@ function mod_login(Context $ctx, $redirect = false) { } 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))); } function mod_logout(Context $ctx) { - global $config; + $config = $ctx->get('config'); destroyCookies(); header('Location: ?/', true, $config['redirect_http']); } function mod_dashboard(Context $ctx) { - global $config, $mod; + global $mod; + $config = $ctx->get('config'); $args = array(); @@ -174,7 +175,7 @@ function mod_dashboard(Context $ctx) { } function mod_search_redirect(Context $ctx) { - global $config; + $config = $ctx->get('config'); if (!hasPermission($config['mod']['search'])) error($config['error']['noaccess']); @@ -454,7 +455,8 @@ function mod_edit_board(Context $ctx, $boardName) { } function mod_new_board(Context $ctx) { - global $config, $board; + global $board; + $config = $ctx->get('config'); if (!hasPermission($config['mod']['newboard'])) error($config['error']['noaccess']); @@ -523,7 +525,8 @@ function mod_new_board(Context $ctx) { } function mod_noticeboard(Context $ctx, $page_no = 1) { - global $config, $pdo, $mod; + global $pdo, $mod; + $config = $ctx->get('config'); if ($page_no < 1) error($config['error']['404']); @@ -578,7 +581,7 @@ function mod_noticeboard(Context $ctx, $page_no = 1) { } function mod_noticeboard_delete(Context $ctx, $id) { - global $config; + $config = $ctx->get('config'); if (!hasPermission($config['mod']['noticeboard_delete'])) error($config['error']['noaccess']); @@ -596,7 +599,8 @@ function mod_noticeboard_delete(Context $ctx, $id) { } function mod_news(Context $ctx, $page_no = 1) { - global $config, $pdo, $mod; + global $pdo, $mod; + $config = $ctx->get('config'); if ($page_no < 1) error($config['error']['404']); @@ -643,7 +647,7 @@ function mod_news(Context $ctx, $page_no = 1) { } function mod_news_delete(Context $ctx, $id) { - global $config; + $config = $ctx->get('config'); if (!hasPermission($config['mod']['news_delete'])) error($config['error']['noaccess']); @@ -658,7 +662,7 @@ function mod_news_delete(Context $ctx, $id) { } function mod_log(Context $ctx, $page_no = 1) { - global $config; + $config = $ctx->get('config'); if ($page_no < 1) error($config['error']['404']); @@ -683,7 +687,7 @@ function mod_log(Context $ctx, $page_no = 1) { } function mod_user_log(Context $ctx, $username, $page_no = 1) { - global $config; + $config = $ctx->get('config'); if ($page_no < 1) error($config['error']['404']); @@ -710,7 +714,7 @@ function mod_user_log(Context $ctx, $username, $page_no = 1) { } 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) error($config['error']['404']); @@ -746,7 +750,7 @@ function mod_board_log(Context $ctx, $board, $page_no = 1, $hide_names = false, } function mod_view_catalog(Context $ctx, $boardName) { - global $config; + $config = $ctx->get('config'); require_once($config['dir']['themes'].'/catalog/theme.php'); $settings = array(); $settings['boards'] = $boardName; @@ -758,7 +762,8 @@ function mod_view_catalog(Context $ctx, $boardName) { } function mod_view_board(Context $ctx, $boardName, $page_no = 1) { - global $config, $mod; + global $mod; + $config = $ctx->get('config'); if (!openBoard($boardName)) error($config['error']['noboard']); @@ -777,7 +782,8 @@ function mod_view_board(Context $ctx, $boardName, $page_no = 1) { } function mod_view_thread(Context $ctx, $boardName, $thread) { - global $config, $mod; + global $mod; + $config = $ctx->get('config'); if (!openBoard($boardName)) error($config['error']['noboard']); @@ -787,7 +793,8 @@ function mod_view_thread(Context $ctx, $boardName, $thread) { } function mod_view_thread50(Context $ctx, $boardName, $thread) { - global $config, $mod; + global $mod; + $config = $ctx->get('config'); if (!openBoard($boardName)) error($config['error']['noboard']); @@ -798,10 +805,10 @@ function mod_view_thread50(Context $ctx, $boardName, $thread) { function mod_ip_remove_note(Context $ctx, $cloaked_ip, $id) { $ip = uncloak_ip($cloaked_ip); - global $config, $mod; + $config = $ctx->get('config'); if (!hasPermission($config['mod']['remove_notes'])) - error($config['error']['noaccess']); + error($config['error']['noaccess']); if (filter_var($ip, FILTER_VALIDATE_IP) === false) error("Invalid IP address."); @@ -820,7 +827,8 @@ function mod_ip_remove_note(Context $ctx, $cloaked_ip, $id) { function mod_ip(Context $ctx, $cip) { $ip = uncloak_ip($cip); - global $config, $mod; + global $mod; + $config = $ctx->get('config'); if (filter_var($ip, FILTER_VALIDATE_IP) === false) error("Invalid IP address."); @@ -922,7 +930,7 @@ function mod_ip(Context $ctx, $cip) { } function mod_edit_ban(Context $ctx, $ban_id) { - global $config; + $config = $ctx->get('config'); if (!hasPermission($config['mod']['edit_ban'])) error($config['error']['noaccess']); @@ -972,7 +980,7 @@ function mod_edit_ban(Context $ctx, $ban_id) { } function mod_ban(Context $ctx) { - global $config; + $config = $ctx->get('config'); if (!hasPermission($config['mod']['ban'])) error($config['error']['noaccess']); @@ -991,7 +999,8 @@ function mod_ban(Context $ctx) { } function mod_bans(Context $ctx) { - global $config, $mod; + global $mod; + $config = $ctx->get('config'); if (!hasPermission($config['mod']['view_banlist'])) error($config['error']['noaccess']); @@ -1025,7 +1034,8 @@ function mod_bans(Context $ctx) { } function mod_bans_json(Context $ctx) { - global $config, $mod; + global $mod; + $config = $ctx->get('config'); if (!hasPermission($config['mod']['ban'])) error($config['error']['noaccess']); @@ -1037,7 +1047,8 @@ function mod_bans_json(Context $ctx) { } function mod_ban_appeals(Context $ctx) { - global $config, $board; + global $board; + $config = $ctx->get('config'); if (!hasPermission($config['mod']['view_ban_appeals'])) error($config['error']['noaccess']); @@ -1113,7 +1124,7 @@ function mod_ban_appeals(Context $ctx) { } function mod_lock(Context $ctx, $board, $unlock, $post) { - global $config; + $config = $ctx->get('config'); if (!openBoard($board)) error($config['error']['noboard']); @@ -1147,7 +1158,7 @@ function mod_lock(Context $ctx, $board, $unlock, $post) { } function mod_sticky(Context $ctx, $board, $unsticky, $post) { - global $config; + $config = $ctx->get('config'); if (!openBoard($board)) error($config['error']['noboard']); @@ -1169,7 +1180,7 @@ function mod_sticky(Context $ctx, $board, $unsticky, $post) { } function mod_cycle(Context $ctx, $board, $uncycle, $post) { - global $config; + $config = $ctx->get('config'); if (!openBoard($board)) error($config['error']['noboard']); @@ -1191,7 +1202,7 @@ function mod_cycle(Context $ctx, $board, $uncycle, $post) { } function mod_bumplock(Context $ctx, $board, $unbumplock, $post) { - global $config; + $config = $ctx->get('config'); if (!openBoard($board)) error($config['error']['noboard']); @@ -1525,7 +1536,7 @@ function mod_move(Context $ctx, $originBoard, $postID) { } function mod_ban_post(Context $ctx, $board, $delete, $post, $token = false) { - global $config, $mod; + $config = $ctx->get('config'); if (!openBoard($board)) error($config['error']['noboard']); @@ -1595,7 +1606,7 @@ function mod_ban_post(Context $ctx, $board, $delete, $post, $token = false) { } function mod_edit_post(Context $ctx, $board, $edit_raw_html, $postID) { - global $config; + $config = $ctx->get('config'); if (!openBoard($board)) error($config['error']['noboard']); @@ -1672,7 +1683,7 @@ function mod_edit_post(Context $ctx, $board, $edit_raw_html, $postID) { } function mod_delete(Context $ctx, $board, $post) { - global $config; + $config = $ctx->get('config'); if (!openBoard($board)) error($config['error']['noboard']); @@ -1693,7 +1704,7 @@ function mod_delete(Context $ctx, $board, $post) { } function mod_deletefile(Context $ctx, $board, $post, $file) { - global $config; + $config = $ctx->get('config'); if (!openBoard($board)) error($config['error']['noboard']); @@ -1716,7 +1727,7 @@ function mod_deletefile(Context $ctx, $board, $post, $file) { } function mod_spoiler_image(Context $ctx, $board, $post, $file) { - global $config; + $config = $ctx->get('config'); if (!openBoard($board)) error($config['error']['noboard']); @@ -1761,7 +1772,8 @@ function mod_spoiler_image(Context $ctx, $board, $post, $file) { } function mod_deletebyip(Context $ctx, $boardName, $post, $global = false) { - global $config, $board; + global $board; + $config = $ctx->get('config'); $global = (bool)$global; @@ -1837,7 +1849,8 @@ function mod_deletebyip(Context $ctx, $boardName, $post, $global = false) { } 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'])) error($config['error']['noaccess']); @@ -2015,7 +2028,7 @@ function mod_user_new(Context $ctx) { function mod_users(Context $ctx) { - global $config; + $config = $ctx->get('config'); if (!hasPermission($config['mod']['manageusers'])) error($config['error']['noaccess']); @@ -2036,7 +2049,7 @@ function mod_users(Context $ctx) { } function mod_user_promote(Context $ctx, $uid, $action) { - global $config; + $config = $ctx->get('config'); if (!hasPermission($config['mod']['promoteusers'])) error($config['error']['noaccess']); @@ -2134,7 +2147,8 @@ function mod_pm(Context $ctx, $id, $reply = false) { } 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->bindValue(':mod', $mod['id']); @@ -2158,7 +2172,8 @@ function mod_inbox(Context $ctx) { function mod_new_pm(Context $ctx, $username) { - global $config, $mod; + global $mod; + $config = $ctx->get('config'); if (!hasPermission($config['mod']['create_pm'])) error($config['error']['noaccess']); @@ -2206,7 +2221,8 @@ function mod_new_pm(Context $ctx, $username) { } function mod_rebuild(Context $ctx) { - global $config, $twig; + global $twig; + $config = $ctx->get('config'); if (!hasPermission($config['mod']['rebuild'])) error($config['error']['noaccess']); @@ -2278,7 +2294,8 @@ function mod_rebuild(Context $ctx) { } function mod_reports(Context $ctx) { - global $config, $mod; + global $mod; + $config = $ctx->get('config'); if (!hasPermission($config['mod']['reports'])) error($config['error']['noaccess']); @@ -2361,7 +2378,7 @@ function mod_reports(Context $ctx) { } 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->bindValue(':id', $id); @@ -2407,7 +2424,8 @@ function mod_report_dismiss(Context $ctx, $id, $action) { } function mod_recent_posts(Context $ctx, $lim) { - global $config, $mod, $pdo; + global $mod, $pdo; + $config = $ctx->get('config'); if (!hasPermission($config['mod']['recent'])) error($config['error']['noaccess']); @@ -2463,7 +2481,8 @@ function mod_recent_posts(Context $ctx, $lim) { } 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)) error($config['error']['noboard']); @@ -2603,7 +2622,7 @@ function mod_config(Context $ctx, $board_config = false) { } function mod_themes_list(Context $ctx) { - global $config; + $config = $ctx->get('config'); if (!hasPermission($config['mod']['themes'])) error($config['error']['noaccess']); @@ -2637,7 +2656,7 @@ function mod_themes_list(Context $ctx) { } function mod_theme_configure(Context $ctx, $theme_name) { - global $config; + $config = $ctx->get('config'); if (!hasPermission($config['mod']['themes'])) error($config['error']['noaccess']); @@ -2719,7 +2738,7 @@ function mod_theme_configure(Context $ctx, $theme_name) { } function mod_theme_uninstall(Context $ctx, $theme_name) { - global $config; + $config = $ctx->get('config'); if (!hasPermission($config['mod']['themes'])) error($config['error']['noaccess']); @@ -2736,7 +2755,7 @@ function mod_theme_uninstall(Context $ctx, $theme_name) { } function mod_theme_rebuild(Context $ctx, $theme_name) { - global $config; + $config = $ctx->get('config'); if (!hasPermission($config['mod']['themes'])) error($config['error']['noaccess']); @@ -2785,7 +2804,8 @@ function mod_delete_page_board(Context $ctx, $page = '', $board = false) { } 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->bindValue(':id', $id); @@ -2856,7 +2876,8 @@ function mod_edit_page(Context $ctx, $id) { } function mod_pages(Context $ctx, $board = false) { - global $config, $mod, $pdo; + global $mod, $pdo; + $config = $ctx->get('config'); if (empty($board)) $board = false; @@ -2981,7 +3002,7 @@ function mod_debug_recent_posts(Context $ctx) { } function mod_debug_sql(Context $ctx) { - global $config; + $config = $ctx->get('config'); if (!hasPermission($config['mod']['debug_sql'])) error($config['error']['noaccess']); From 81aebef2f4c89dbc7351e2434888a210df7a6623 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Thu, 15 Aug 2024 17:21:41 +0200 Subject: [PATCH 4/4] pages.php: use modern array syntax for new empty arrays --- inc/mod/pages.php | 56 +++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/inc/mod/pages.php b/inc/mod/pages.php index 0793ccb4..95183246 100644 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -83,7 +83,7 @@ function mod_dashboard(Context $ctx) { global $mod; $config = $ctx->get('config'); - $args = array(); + $args = []; $args['boards'] = listBoards(); @@ -223,7 +223,7 @@ function mod_search(Context $ctx, $type, $search_query_escaped, $page_no = 1) { $query = str_replace('`', '!`', $query); // Array of phrases to match - $match = array(); + $match = []; // Exact phrases ("like this") if (preg_match_all('/"(.+?)"/', $query, $exact_phrases)) { @@ -752,7 +752,7 @@ function mod_board_log(Context $ctx, $board, $page_no = 1, $hide_names = false, function mod_view_catalog(Context $ctx, $boardName) { $config = $ctx->get('config'); require_once($config['dir']['themes'].'/catalog/theme.php'); - $settings = array(); + $settings = []; $settings['boards'] = $boardName; $settings['update_on_posts'] = true; $settings['title'] = 'Catalog'; @@ -871,9 +871,9 @@ function mod_ip(Context $ctx, $cip) { } - $args = array(); + $args = []; $args['ip'] = $ip; - $args['posts'] = array(); + $args['posts'] = []; if ($config['mod']['dns_lookup'] && empty($config['ipcrypt_key'])) $args['hostname'] = rDNS($ip); @@ -896,7 +896,7 @@ function mod_ip(Context $ctx, $cip) { } 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); } } @@ -921,7 +921,7 @@ function mod_ip(Context $ctx, $cip) { $query->execute() or error(db_error($query)); $args['logs'] = $query->fetchAll(PDO::FETCH_ASSOC); } else { - $args['logs'] = array(); + $args['logs'] = []; } $args['security_token'] = make_secure_link_token('IP/' . $cip); @@ -1009,7 +1009,7 @@ function mod_bans(Context $ctx) { if (!hasPermission($config['mod']['unban'])) error($config['error']['noaccess']); - $unban = array(); + $unban = []; foreach ($_POST as $name => $unused) { if (preg_match('/^ban_(\d+)$/', $name, $match)) $unban[] = $match[1]; @@ -1094,16 +1094,16 @@ function mod_ban_appeals(Context $ctx) { $query = query(sprintf("SELECT `num_files`, `files` FROM ``posts_%s`` WHERE `id` = " . (int)$ban['post']['id'], $board['uri'])); 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); } else { - $ban['post']['files'] = array(array()); + $ban['post']['files'] = array([]); $ban['post']['files'][0]['file'] = 'deleted'; $ban['post']['files'][0]['thumb'] = false; $ban['post']['num_files'] = 1; } } else { - $ban['post']['files'] = array(array()); + $ban['post']['files'] = array([]); $ban['post']['files'][0]['file'] = 'deleted'; $ban['post']['files'][0]['thumb'] = false; $ban['post']['num_files'] = 1; @@ -1395,7 +1395,7 @@ function mod_move(Context $ctx, $originBoard, $postID) { $query->bindValue(':id', $postID, PDO::PARAM_INT); $query->execute() or error(db_error($query)); - $replies = array(); + $replies = []; while ($post = $query->fetch(PDO::FETCH_ASSOC)) { $post['mod'] = true; @@ -1459,7 +1459,7 @@ function mod_move(Context $ctx, $originBoard, $postID) { if (!empty($post['tracked_cites'])) { - $insert_rows = array(); + $insert_rows = []; foreach ($post['tracked_cites'] as $cite) { $insert_rows[] = '(' . $pdo->quote($board['uri']) . ', ' . $newPostID . ', ' . @@ -1810,8 +1810,8 @@ function mod_deletebyip(Context $ctx, $boardName, $post, $global = false) { @set_time_limit($config['mod']['rebuild_timelimit']); - $threads_to_rebuild = array(); - $threads_deleted = array(); + $threads_to_rebuild = []; + $threads_deleted = []; while ($post = $query->fetch(PDO::FETCH_ASSOC)) { openBoard($post['board']); @@ -1870,7 +1870,7 @@ function mod_user(Context $ctx, $uid) { $board = $board['uri']; } - $boards = array(); + $boards = []; foreach ($_POST as $name => $value) { if (preg_match('/^board_(' . $config['board_regex'] . ')$/u', $name, $matches) && in_array($matches[1], $_boards)) $boards[] = $matches[1]; @@ -1961,7 +1961,7 @@ function mod_user(Context $ctx, $uid) { $query->execute() or error(db_error($query)); $log = $query->fetchAll(PDO::FETCH_ASSOC); } else { - $log = array(); + $log = []; } $user['boards'] = explode(',', $user['boards']); @@ -1994,7 +1994,7 @@ function mod_user_new(Context $ctx) { $board = $board['uri']; } - $boards = array(); + $boards = []; foreach ($_POST as $name => $value) { if (preg_match('/^board_(' . $config['board_regex'] . ')$/u', $name, $matches) && in_array($matches[1], $_boards)) $boards[] = $matches[1]; @@ -2230,9 +2230,9 @@ function mod_rebuild(Context $ctx) { if (isset($_POST['rebuild'])) { @set_time_limit($config['mod']['rebuild_timelimit']); - $log = array(); + $log = []; $boards = listBoards(); - $rebuilt_scripts = array(); + $rebuilt_scripts = []; if (isset($_POST['rebuild_cache'])) { if ($config['cache']['enabled']) { @@ -2305,16 +2305,16 @@ function mod_reports(Context $ctx) { $query->execute() or error(db_error($query)); $reports = $query->fetchAll(PDO::FETCH_ASSOC); - $report_queries = array(); + $report_queries = []; foreach ($reports as $report) { if (!isset($report_queries[$report['board']])) - $report_queries[$report['board']] = array(); + $report_queries[$report['board']] = []; $report_queries[$report['board']][] = $report['post']; } - $report_posts = array(); + $report_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()); while ($post = $query->fetch(PDO::FETCH_ASSOC)) { @@ -2433,7 +2433,7 @@ function mod_recent_posts(Context $ctx, $lim) { $limit = (is_numeric($lim))? $lim : 25; $last_time = (isset($_GET['last']) && is_numeric($_GET['last'])) ? $_GET['last'] : 0; - $mod_boards = array(); + $mod_boards = []; $boards = listBoards(); //if not all boards @@ -2593,7 +2593,7 @@ function mod_config(Context $ctx, $board_config = false) { if ($config['minify_html']) $config_append = str_replace("\n", ' ', $config_append); - $page = array(); + $page = []; $page['title'] = 'Cannot write to file!'; $page['config'] = $config; $page['body'] = ' @@ -2636,7 +2636,7 @@ function mod_themes_list(Context $ctx) { $themes_in_use = $query->fetchAll(PDO::FETCH_COLUMN); // Scan directory for themes - $themes = array(); + $themes = []; while ($file = readdir($dir)) { if ($file[0] != '.' && is_dir($config['dir']['themes'] . '/' . $file)) { $themes[$file] = loadThemeConfig($file); @@ -2933,7 +2933,7 @@ function mod_pages(Context $ctx, $board = false) { function mod_debug_antispam(Context $ctx) { global $pdo, $config; - $args = array(); + $args = []; if (isset($_POST['board'], $_POST['thread'])) { $where = '`board` = ' . $pdo->quote($_POST['board']);