From e6e80145ccb4e8b9d4d299ccae988c3d299e6bdb Mon Sep 17 00:00:00 2001 From: Yury Pikhtarev Date: Sun, 22 Jun 2025 12:12:51 +0400 Subject: [PATCH] refactor(controllers): move legacy PHP controllers to dedicated directory - Move 19 controller files from root to controllers/ directory - Update routing and include paths in supporting files - Maintain backward compatibility through legacy controller handling Moved files: - dl.php, dl_list.php, feed.php, filelist.php, group.php - group_edit.php, info.php, login.php, memberlist.php - modcp.php, playback_m3u.php, poll.php, posting.php - privmsg.php, profile.php, search.php, tracker.php - viewforum.php, viewtopic.php Updated supporting files to handle new structure: - library/includes/functions.php - library/includes/init_bb.php - library/includes/page_header.php - src/Presentation/Http/Controllers/Web/LegacyController.php - src/Presentation/Http/Kernel.php - src/Presentation/Http/Routes/web.php --- dl.php => controllers/dl.php | 6 ++++- dl_list.php => controllers/dl_list.php | 5 +++- feed.php => controllers/feed.php | 5 +++- filelist.php => controllers/filelist.php | 5 +++- group.php => controllers/group.php | 6 ++++- group_edit.php => controllers/group_edit.php | 5 +++- info.php => controllers/info.php | 5 +++- login.php => controllers/login.php | 5 +++- memberlist.php => controllers/memberlist.php | 5 +++- modcp.php => controllers/modcp.php | 6 ++++- .../playback_m3u.php | 5 +++- poll.php => controllers/poll.php | 5 +++- posting.php => controllers/posting.php | 6 ++++- privmsg.php => controllers/privmsg.php | 6 ++++- profile.php => controllers/profile.php | 5 +++- search.php => controllers/search.php | 6 ++++- tracker.php => controllers/tracker.php | 5 +++- viewforum.php => controllers/viewforum.php | 5 +++- viewtopic.php => controllers/viewtopic.php | 6 ++++- library/includes/functions.php | 8 ++++--- library/includes/init_bb.php | 16 ++++++++----- library/includes/page_header.php | 2 +- .../Http/Controllers/Web/LegacyController.php | 20 ++++++++++++++-- src/Presentation/Http/Kernel.php | 16 ++++++++----- src/Presentation/Http/Routes/web.php | 23 +++++++++++++++---- 25 files changed, 146 insertions(+), 41 deletions(-) rename dl.php => controllers/dl.php (98%) rename dl_list.php => controllers/dl_list.php (96%) rename feed.php => controllers/feed.php (94%) rename filelist.php => controllers/filelist.php (96%) rename group.php => controllers/group.php (99%) rename group_edit.php => controllers/group_edit.php (96%) rename info.php => controllers/info.php (89%) rename login.php => controllers/login.php (96%) rename memberlist.php => controllers/memberlist.php (97%) rename modcp.php => controllers/modcp.php (99%) rename playback_m3u.php => controllers/playback_m3u.php (95%) rename poll.php => controllers/poll.php (97%) rename posting.php => controllers/posting.php (99%) rename privmsg.php => controllers/privmsg.php (99%) rename profile.php => controllers/profile.php (89%) rename search.php => controllers/search.php (99%) rename tracker.php => controllers/tracker.php (99%) rename viewforum.php => controllers/viewforum.php (99%) rename viewtopic.php => controllers/viewtopic.php (99%) diff --git a/dl.php b/controllers/dl.php similarity index 98% rename from dl.php rename to controllers/dl.php index bfc0ef678..cd76b6f92 100644 --- a/dl.php +++ b/controllers/dl.php @@ -10,7 +10,11 @@ define('BB_SCRIPT', 'dl'); define('NO_GZIP', true); -require __DIR__ . '/common.php'; +// Skip loading common.php if already loaded (when run through routing system) +if (!defined('IN_TORRENTPIER')) { + require __DIR__ . '/../common.php'; +} + require ATTACH_DIR . '/attachment_mod.php'; $datastore->enqueue([ diff --git a/dl_list.php b/controllers/dl_list.php similarity index 96% rename from dl_list.php rename to controllers/dl_list.php index d31bb701b..56f92f685 100644 --- a/dl_list.php +++ b/controllers/dl_list.php @@ -9,7 +9,10 @@ define('BB_SCRIPT', 'dl_list'); -require __DIR__ . '/common.php'; +// Skip loading common.php if already loaded (when run through routing system) +if (!defined('IN_TORRENTPIER')) { + require __DIR__ . '/../common.php'; +} $forum_id = $_REQUEST[POST_FORUM_URL] ?? 0; $topic_id = $_REQUEST[POST_TOPIC_URL] ?? 0; diff --git a/feed.php b/controllers/feed.php similarity index 94% rename from feed.php rename to controllers/feed.php index bbd9eb3e0..45ba72271 100644 --- a/feed.php +++ b/controllers/feed.php @@ -9,7 +9,10 @@ define('BB_SCRIPT', 'feed'); -require __DIR__ . '/common.php'; +// Skip loading common.php if already loaded (when run through routing system) +if (!defined('IN_TORRENTPIER')) { + require __DIR__ . '/../common.php'; +} // Init userdata $user->session_start(['req_login' => true]); diff --git a/filelist.php b/controllers/filelist.php similarity index 96% rename from filelist.php rename to controllers/filelist.php index 81a241d20..7c00a836c 100644 --- a/filelist.php +++ b/controllers/filelist.php @@ -9,7 +9,10 @@ define('BB_SCRIPT', 'filelist'); -require __DIR__ . '/common.php'; +// Skip loading common.php if already loaded (when run through routing system) +if (!defined('IN_TORRENTPIER')) { + require __DIR__ . '/../common.php'; +} // Start session management $user->session_start(); diff --git a/group.php b/controllers/group.php similarity index 99% rename from group.php rename to controllers/group.php index e88bb6de7..9a1e26ec0 100644 --- a/group.php +++ b/controllers/group.php @@ -9,7 +9,11 @@ define('BB_SCRIPT', 'group'); -require __DIR__ . '/common.php'; +// Skip loading common.php if already loaded (when run through routing system) +if (!defined('IN_TORRENTPIER')) { + require __DIR__ . '/../common.php'; +} + require INC_DIR . '/bbcode.php'; $page_cfg['use_tablesorter'] = true; diff --git a/group_edit.php b/controllers/group_edit.php similarity index 96% rename from group_edit.php rename to controllers/group_edit.php index 041365bf4..8f579132d 100644 --- a/group_edit.php +++ b/controllers/group_edit.php @@ -9,7 +9,10 @@ define('BB_SCRIPT', 'group_edit'); -require __DIR__ . '/common.php'; +// Skip loading common.php if already loaded (when run through routing system) +if (!defined('IN_TORRENTPIER')) { + require __DIR__ . '/../common.php'; +} $page_cfg['include_bbcode_js'] = true; diff --git a/info.php b/controllers/info.php similarity index 89% rename from info.php rename to controllers/info.php index e47c3ef32..eecde3e52 100644 --- a/info.php +++ b/controllers/info.php @@ -9,7 +9,10 @@ define('BB_SCRIPT', 'info'); -require __DIR__ . '/common.php'; +// Skip loading common.php if already loaded (when run through routing system) +if (!defined('IN_TORRENTPIER')) { + require __DIR__ . '/../common.php'; +} // Start session management $user->session_start(); diff --git a/login.php b/controllers/login.php similarity index 96% rename from login.php rename to controllers/login.php index eb45e7109..5a8ed50de 100644 --- a/login.php +++ b/controllers/login.php @@ -10,7 +10,10 @@ define('BB_SCRIPT', 'login'); define('IN_LOGIN', true); -require __DIR__ . '/common.php'; +// Skip loading common.php if already loaded (when run through routing system) +if (!defined('IN_TORRENTPIER')) { + require __DIR__ . '/../common.php'; +} array_deep($_POST, 'trim'); diff --git a/memberlist.php b/controllers/memberlist.php similarity index 97% rename from memberlist.php rename to controllers/memberlist.php index e70cfc0e3..ae5b2b5ac 100644 --- a/memberlist.php +++ b/controllers/memberlist.php @@ -9,7 +9,10 @@ define('BB_SCRIPT', 'memberlist'); -require __DIR__ . '/common.php'; +// Skip loading common.php if already loaded (when run through routing system) +if (!defined('IN_TORRENTPIER')) { + require __DIR__ . '/../common.php'; +} $user->session_start(['req_login' => true]); diff --git a/modcp.php b/controllers/modcp.php similarity index 99% rename from modcp.php rename to controllers/modcp.php index 63b059130..9650dcdb9 100644 --- a/modcp.php +++ b/controllers/modcp.php @@ -9,7 +9,11 @@ define('BB_SCRIPT', 'modcp'); -require __DIR__ . '/common.php'; +// Skip loading common.php if already loaded (when run through routing system) +if (!defined('IN_TORRENTPIER')) { + require __DIR__ . '/../common.php'; +} + require INC_DIR . '/bbcode.php'; // diff --git a/playback_m3u.php b/controllers/playback_m3u.php similarity index 95% rename from playback_m3u.php rename to controllers/playback_m3u.php index 0cdcc3115..2a1002e89 100644 --- a/playback_m3u.php +++ b/controllers/playback_m3u.php @@ -9,7 +9,10 @@ define('BB_SCRIPT', 'playback_m3u'); -require __DIR__ . '/common.php'; +// Skip loading common.php if already loaded (when run through routing system) +if (!defined('IN_TORRENTPIER')) { + require __DIR__ . '/../common.php'; +} if (!config()->get('torr_server.enabled')) { redirect('index.php'); diff --git a/poll.php b/controllers/poll.php similarity index 97% rename from poll.php rename to controllers/poll.php index b770e49c5..2a8204c9b 100644 --- a/poll.php +++ b/controllers/poll.php @@ -9,7 +9,10 @@ define('BB_SCRIPT', 'vote'); -require __DIR__ . '/common.php'; +// Skip loading common.php if already loaded (when run through routing system) +if (!defined('IN_TORRENTPIER')) { + require __DIR__ . '/../common.php'; +} // Start session management $user->session_start(['req_login' => true]); diff --git a/posting.php b/controllers/posting.php similarity index 99% rename from posting.php rename to controllers/posting.php index 5fb5146cb..8e6413acc 100644 --- a/posting.php +++ b/controllers/posting.php @@ -9,7 +9,11 @@ define('BB_SCRIPT', 'posting'); -require __DIR__ . '/common.php'; +// Skip loading common.php if already loaded (when run through routing system) +if (!defined('IN_TORRENTPIER')) { + require __DIR__ . '/../common.php'; +} + require INC_DIR . '/bbcode.php'; require ATTACH_DIR . '/attachment_mod.php'; diff --git a/privmsg.php b/controllers/privmsg.php similarity index 99% rename from privmsg.php rename to controllers/privmsg.php index 409d0aacb..ef6defd14 100644 --- a/privmsg.php +++ b/controllers/privmsg.php @@ -10,7 +10,11 @@ define('BB_SCRIPT', 'pm'); define('IN_PM', true); -require __DIR__ . '/common.php'; +// Skip loading common.php if already loaded (when run through routing system) +if (!defined('IN_TORRENTPIER')) { + require __DIR__ . '/../common.php'; +} + require INC_DIR . '/bbcode.php'; $privmsg_sent_id = $l_box_name = $to_username = $privmsg_subject = $privmsg_message = $error_msg = ''; diff --git a/profile.php b/controllers/profile.php similarity index 89% rename from profile.php rename to controllers/profile.php index 9f036bf7e..de1f7706f 100644 --- a/profile.php +++ b/controllers/profile.php @@ -10,7 +10,10 @@ define('BB_SCRIPT', 'profile'); define('IN_PROFILE', true); -require __DIR__ . '/common.php'; +// Skip loading common.php if already loaded (when run through routing system) +if (!defined('IN_TORRENTPIER')) { + require __DIR__ . '/../common.php'; +} // Start session management $user->session_start(); diff --git a/search.php b/controllers/search.php similarity index 99% rename from search.php rename to controllers/search.php index 7075e6a23..a26cd6e6e 100644 --- a/search.php +++ b/controllers/search.php @@ -9,7 +9,11 @@ define('BB_SCRIPT', 'search'); -require __DIR__ . '/common.php'; +// Skip loading common.php if already loaded (when run through routing system) +if (!defined('IN_TORRENTPIER')) { + require __DIR__ . '/../common.php'; +} + require INC_DIR . '/bbcode.php'; $page_cfg['use_tablesorter'] = true; diff --git a/tracker.php b/controllers/tracker.php similarity index 99% rename from tracker.php rename to controllers/tracker.php index ddb56a51f..f346c4c34 100644 --- a/tracker.php +++ b/controllers/tracker.php @@ -9,7 +9,10 @@ define('BB_SCRIPT', 'tracker'); -require __DIR__ . '/common.php'; +// Skip loading common.php if already loaded (when run through routing system) +if (!defined('IN_TORRENTPIER')) { + require __DIR__ . '/../common.php'; +} // Page config $page_cfg['include_bbcode_js'] = true; diff --git a/viewforum.php b/controllers/viewforum.php similarity index 99% rename from viewforum.php rename to controllers/viewforum.php index 98519bce7..8e88a22b5 100644 --- a/viewforum.php +++ b/controllers/viewforum.php @@ -9,7 +9,10 @@ define('BB_SCRIPT', 'forum'); -require __DIR__ . '/common.php'; +// Skip loading common.php if already loaded (when run through routing system) +if (!defined('IN_TORRENTPIER')) { + require __DIR__ . '/../common.php'; +} $page_cfg['include_bbcode_js'] = true; diff --git a/viewtopic.php b/controllers/viewtopic.php similarity index 99% rename from viewtopic.php rename to controllers/viewtopic.php index 806327ded..87ed6e605 100644 --- a/viewtopic.php +++ b/controllers/viewtopic.php @@ -9,7 +9,11 @@ define('BB_SCRIPT', 'topic'); -require __DIR__ . '/common.php'; +// Skip loading common.php if already loaded (when run through routing system) +if (!defined('IN_TORRENTPIER')) { + require __DIR__ . '/../common.php'; +} + require INC_DIR . '/bbcode.php'; $datastore->enqueue([ diff --git a/library/includes/functions.php b/library/includes/functions.php index 6398907c9..0ed44eced 100644 --- a/library/includes/functions.php +++ b/library/includes/functions.php @@ -1332,7 +1332,7 @@ function bb_die($msg_text, $status_code = null) { global $ajax, $lang, $template, $theme, $userdata, $user; - if (isset($status_code)) { + if (isset($status_code) && !defined('MODERN_ROUTING')) { http_response_code($status_code); } @@ -1388,9 +1388,11 @@ function bb_die($msg_text, $status_code = null) function bb_simple_die($txt, $status_code = null) { - header('Content-Type: text/plain; charset=' . DEFAULT_CHARSET); + if (!defined('MODERN_ROUTING')) { + header('Content-Type: text/plain; charset=' . DEFAULT_CHARSET); + } - if (isset($status_code)) { + if (isset($status_code) && !defined('MODERN_ROUTING')) { http_response_code($status_code); } diff --git a/library/includes/init_bb.php b/library/includes/init_bb.php index 04942b438..e5cc65e65 100644 --- a/library/includes/init_bb.php +++ b/library/includes/init_bb.php @@ -41,7 +41,9 @@ function compress_output($contents) { if (config()->get('gzip_compress') && GZIP_OUTPUT_ALLOWED && !defined('NO_GZIP')) { if (UA_GZIP_SUPPORTED && strlen($contents) > 2000) { - header('Content-Encoding: gzip'); + if (!defined('MODERN_ROUTING')) { + header('Content-Encoding: gzip'); + } $contents = gzencode($contents, 1); } } @@ -312,11 +314,13 @@ define('SELECT', 6); // Functions function send_no_cache_headers() { - header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); - header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); - header('Cache-Control: no-store, no-cache, must-revalidate'); - header('Cache-Control: post-check=0, pre-check=0', false); - header('Pragma: no-cache'); + if (!defined('MODERN_ROUTING')) { + header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); + header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); + header('Cache-Control: no-store, no-cache, must-revalidate'); + header('Cache-Control: post-check=0, pre-check=0', false); + header('Pragma: no-cache'); + } } /** diff --git a/library/includes/page_header.php b/library/includes/page_header.php index 46ccbe4a6..8f5ac5157 100644 --- a/library/includes/page_header.php +++ b/library/includes/page_header.php @@ -241,7 +241,7 @@ if (!empty(config()->get('page.show_torhelp')[$bb_script]) && !empty($userdata[' $in_out = ($logged_in) ? 'in' : 'out'; $template->assign_block_vars("switch_user_logged_{$in_out}", []); -if (!IS_GUEST) { +if (!IS_GUEST && !defined('MODERN_ROUTING')) { header('Cache-Control: private, pre-check=0, post-check=0, max-age=0'); header('Expires: 0'); header('Pragma: no-cache'); diff --git a/src/Presentation/Http/Controllers/Web/LegacyController.php b/src/Presentation/Http/Controllers/Web/LegacyController.php index 2d6ddc49e..ec0baf82d 100644 --- a/src/Presentation/Http/Controllers/Web/LegacyController.php +++ b/src/Presentation/Http/Controllers/Web/LegacyController.php @@ -62,8 +62,24 @@ class LegacyController $originalGet = $_GET; $originalPost = $_POST; - // Make legacy globals available in the included file's scope - global $bb_cfg, $config, $user, $template, $datastore, $lang, $userdata, $userinfo, $images; + // Import essential legacy globals into local scope + global $bb_cfg, $config, $user, $template, $datastore, $lang, $userdata, $userinfo, $images, + $tracking_topics, $tracking_forums, $theme, $bf, $attach_config, $gen_simple_header, + $client_ip, $user_ip, $log_action, $html, $wordCensor, $forum_key, $search_id, + $session_id, $items_found, $per_page, $topic_id, $req_topics, $forum_id, $mode, + $is_auth, $t_data, $postrow, $group_id, $group_info, $post_id, $folder, $post_info, + $tor, $post_data, $privmsg, $forums, $redirect, $attachment, $forum_data, $search_all, + $redirect_url, $topic_csv, $poster_id, $emailer, $s_hidden_fields, $opt, $msg, $stats, + $poster_id_key, $text_match_key, $poster_name_key, $my_topics_key, $my_topics_val, $title_only_key, + $all_words_key, $dl_cancel_key, $dl_cancel_val, $dl_compl_key, $dl_compl_val, $dl_down_key, $dl_down_val, + $dl_will_key, $dl_will_val, $new_key, $new_val, $new_topics_key, $new_topics_val, $time_key, $time_val, + $order_key, $order_val, $sort_key, $sort_val, $display_as_key, $display_as_val, $topic_key, $topic_val, + $poster_id_val, $poster_id_key, $forum_val, $forum_key; + + // Signal to legacy code that we're running through modern routing + if (!defined('MODERN_ROUTING')) { + define('MODERN_ROUTING', true); + } // Include the legacy controller // Note: We don't use require_once to allow multiple includes if needed diff --git a/src/Presentation/Http/Kernel.php b/src/Presentation/Http/Kernel.php index 60c069946..99a8e0c17 100644 --- a/src/Presentation/Http/Kernel.php +++ b/src/Presentation/Http/Kernel.php @@ -136,13 +136,17 @@ class Kernel private function sendResponse(ResponseInterface $response): void { - // Send status line - http_response_code($response->getStatusCode()); + // Send status line (only if headers haven't been sent already) + if (!headers_sent()) { + http_response_code($response->getStatusCode()); + } - // Send headers - foreach ($response->getHeaders() as $name => $values) { - foreach ($values as $value) { - header("{$name}: {$value}", false); + // Send headers (only if headers haven't been sent already) + if (!headers_sent()) { + foreach ($response->getHeaders() as $name => $values) { + foreach ($values as $value) { + header("{$name}: {$value}", false); + } } } diff --git a/src/Presentation/Http/Routes/web.php b/src/Presentation/Http/Routes/web.php index 04fca7efb..0b78f8432 100644 --- a/src/Presentation/Http/Routes/web.php +++ b/src/Presentation/Http/Routes/web.php @@ -14,12 +14,27 @@ return function (Router $router): void { // Legacy controller routes (hacky but organized approach) $legacyRoutes = [ 'ajax.php', + 'dl.php', + 'dl_list.php', + 'feed.php', + 'filelist.php', + 'group.php', + 'group_edit.php', 'index.php', + 'info.php', + 'login.php', + 'memberlist.php', + 'modcp.php', + 'playback_m3u.php', + 'poll.php', + 'posting.php', + 'privmsg.php', + 'profile.php', + 'search.php', 'terms.php', - // Add more legacy controllers here as needed: - // 'login.php', - // 'search.php', - // 'tracker.php', + 'tracker.php', + 'viewforum.php', + 'viewtopic.php', ]; foreach ($legacyRoutes as $route) {