From e5aaaf4abde218f0fe6de18113cf0d7189bef7b6 Mon Sep 17 00:00:00 2001 From: Roman Kelesidis Date: Sun, 5 Jan 2025 00:57:20 +0700 Subject: [PATCH] Use `DEFAULT_CHARSET` constant instead of hardcoded string (#1734) * Use `DEFAULT_CHARSET` constant instead of hardcoded string * Update CHANGELOG.md * Update defines.php * Update info.php * Updated * Update page_header.tpl * Update index.tpl * Update index.tpl --- CHANGELOG.md | 1 + admin/index.php | 2 +- common.php | 2 +- dl.php | 2 +- info.php | 2 +- library/ajax/ffprobe_info.php | 8 ++++---- library/ajax/posts.php | 4 ++-- library/config.php | 1 - library/defines.php | 5 ++++- library/includes/functions.php | 14 +++++++------- library/includes/init_bb.php | 2 +- library/includes/page_header.php | 2 +- library/includes/ucp/register.php | 2 +- src/Dev.php | 2 +- src/Helpers/VersionHelper.php | 2 +- src/Legacy/BBCode.php | 2 +- src/Updater.php | 2 +- src/Validate.php | 8 ++++---- styles/templates/admin/index.tpl | 3 +-- styles/templates/default/page_header.tpl | 2 +- 20 files changed, 35 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a409682b2..916e18bb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - Added `m4a` extension support in M3U playback [\#1724](https://github.com/torrentpier/torrentpier/pull/1724) ([belomaxorka](https://github.com/belomaxorka)) - Created `VersionHelper.php` [\#1731](https://github.com/torrentpier/torrentpier/pull/1731) ([belomaxorka](https://github.com/belomaxorka)) - Drop Ocelot announcer support 🫡 [\#1727](https://github.com/torrentpier/torrentpier/pull/1727) ([belomaxorka](https://github.com/belomaxorka)) +- Use `DEFAULT_CHARSET` constant instead of hardcoded string [\#1734](https://github.com/torrentpier/torrentpier/pull/1734) ([belomaxorka](https://github.com/belomaxorka)) - Replaced some `html_entity_decode` to engine's built-in function [\#1733](https://github.com/torrentpier/torrentpier/pull/1733) ([belomaxorka](https://github.com/belomaxorka)) - Show torrent's announcers list in `filelist.php` page [\#1708](https://github.com/torrentpier/torrentpier/pull/1708) ([belomaxorka](https://github.com/belomaxorka)) - [PHP 8.4] Fixed some deprecations [\#1718](https://github.com/torrentpier/torrentpier/pull/1718) ([belomaxorka](https://github.com/belomaxorka)) diff --git a/admin/index.php b/admin/index.php index e91d7ef16..110544328 100644 --- a/admin/index.php +++ b/admin/index.php @@ -234,7 +234,7 @@ if (isset($_GET['pane']) && $_GET['pane'] == 'left') { } else { // Generate frameset $template->assign_vars([ - 'CONTENT_ENCODING' => $bb_cfg['charset'], + 'CONTENT_ENCODING' => DEFAULT_CHARSET, 'TPL_ADMIN_FRAMESET' => true, ]); send_no_cache_headers(); diff --git a/common.php b/common.php index 9a11e06ff..eaf360c13 100644 --- a/common.php +++ b/common.php @@ -239,7 +239,7 @@ function clean_filename($fname) * @param ?string $charset * @return string */ -function htmlCHR($txt, bool $double_encode = false, int $quote_style = ENT_QUOTES, ?string $charset = 'UTF-8'): string +function htmlCHR($txt, bool $double_encode = false, int $quote_style = ENT_QUOTES, ?string $charset = DEFAULT_CHARSET): string { return (string)htmlspecialchars($txt ?? '', $quote_style, $charset, $double_encode); } diff --git a/dl.php b/dl.php index ce37c5b80..5b8852a96 100644 --- a/dl.php +++ b/dl.php @@ -53,7 +53,7 @@ function send_file_to_browser($attachment, $upload_dir) header('Pragma: public'); $real_filename = clean_filename(basename($attachment['real_filename'])); $mimetype = $attachment['mimetype'] . ';'; - $charset = "charset={$bb_cfg['charset']};"; + $charset = 'charset=' . DEFAULT_CHARSET . ';'; // Send out the Headers header("Content-Type: $mimetype $charset name=\"$real_filename\""); diff --git a/info.php b/info.php index 3a66fae2a..0a75bf23a 100644 --- a/info.php +++ b/info.php @@ -43,7 +43,7 @@ switch ((string)$_REQUEST['show'] ?? 'not_found') { $require = is_file($htmlDir . $info['src']) ? ($htmlDir . $info['src']) : false; $template->assign_vars([ - 'PAGE_TITLE' => mb_strtoupper($info['title'], 'UTF-8'), + 'PAGE_TITLE' => mb_strtoupper($info['title'], DEFAULT_CHARSET), 'REQUIRE' => $require ? file_get_contents($require) : $lang['NOT_FOUND'], ]); diff --git a/library/ajax/ffprobe_info.php b/library/ajax/ffprobe_info.php index eaf72e43c..31753d4d7 100644 --- a/library/ajax/ffprobe_info.php +++ b/library/ajax/ffprobe_info.php @@ -57,15 +57,15 @@ if (isset($ffpInfo->streams)) { $result = '' . sprintf($lang['AUDIO_TRACK'], (!isset($stream->index) || $stream->index === 0) ? 1 : $stream->index) . '
'; if (isset($stream->tags->language)) { if (isset($stream->tags->title)) { - $result .= '' . mb_strtoupper($stream->tags->language, 'UTF-8') . ' (' . $stream->tags->title . ')' . ''; + $result .= '' . mb_strtoupper($stream->tags->language, DEFAULT_CHARSET) . ' (' . $stream->tags->title . ')' . ''; } else { - $result .= '' . mb_strtoupper($stream->tags->language, 'UTF-8') . ''; + $result .= '' . mb_strtoupper($stream->tags->language, DEFAULT_CHARSET) . ''; } $result .= '
'; } if (!empty($stream->codec_name)) { - $result .= sprintf($lang['AUDIO_CODEC'], $stream->codec_long_name, mb_strtoupper($stream->codec_name, 'UTF-8')) . '
'; + $result .= sprintf($lang['AUDIO_CODEC'], $stream->codec_long_name, mb_strtoupper($stream->codec_name, DEFAULT_CHARSET)) . '
'; } if (!empty($stream->bit_rate)) { $result .= sprintf($lang['BITRATE'], humn_bitrate((int)$stream->bit_rate)) . '
'; @@ -87,7 +87,7 @@ if (isset($ffpInfo->streams)) { $data = [ 'filesize' => sprintf($lang['FILESIZE'] . ': %s', humn_size($ffpInfo->format->size)), 'resolution' => (!$isAudio && isset($videoCodecInfo)) ? sprintf($lang['RESOLUTION'], $videoCodecInfo->width . 'x' . $videoCodecInfo->height) : '', - 'video_codec' => (!$isAudio && isset($videoCodecInfo->codec_name)) ? sprintf($lang['VIDEO_CODEC'], $videoCodecInfo->codec_long_name, mb_strtoupper($videoCodecInfo->codec_name, 'UTF-8')) : '', + 'video_codec' => (!$isAudio && isset($videoCodecInfo->codec_name)) ? sprintf($lang['VIDEO_CODEC'], $videoCodecInfo->codec_long_name, mb_strtoupper($videoCodecInfo->codec_name, DEFAULT_CHARSET)) : '', 'audio_dub' => implode('
', $audioDub) ]; diff --git a/library/ajax/posts.php b/library/ajax/posts.php index 76a589bc3..5a3632da9 100644 --- a/library/ajax/posts.php +++ b/library/ajax/posts.php @@ -85,7 +85,7 @@ switch ($this->request['type']) { if ($post['post_id'] == $post['topic_first_post_id']) { $message = "[quote]" . $post['topic_title'] . "[/quote]\r"; } - if (mb_strlen($message, 'UTF-8') > 1000) { + if (mb_strlen($message, DEFAULT_CHARSET) > 1000) { $this->response['redirect'] = make_url(POSTING_URL . '?mode=quote&' . POST_POST_URL . '=' . $post_id); } @@ -112,7 +112,7 @@ switch ($this->request['type']) { if ($post['poster_id'] != $userdata['user_id'] && !$is_auth['auth_mod']) { $this->ajax_die($lang['EDIT_OWN_POSTS']); } - if ((mb_strlen($post['post_text'], 'UTF-8') > 1000) || $post['post_attachment'] || ($post['topic_first_post_id'] == $post_id)) { + if ((mb_strlen($post['post_text'], DEFAULT_CHARSET) > 1000) || $post['post_attachment'] || ($post['topic_first_post_id'] == $post_id)) { $this->response['redirect'] = make_url(POSTING_URL . '?mode=editpost&' . POST_POST_URL . '=' . $post_id); } elseif ($this->request['type'] == 'editor') { $text = (string)$this->request['text']; diff --git a/library/config.php b/library/config.php index 7c887c51d..b1d7f0bb8 100644 --- a/library/config.php +++ b/library/config.php @@ -170,7 +170,6 @@ $bb_cfg['posting_url'] = 'posting.php'; # "http://{$domain_name}/posting.php" $bb_cfg['pm_url'] = 'privmsg.php'; # "http://{$domain_name}/privmsg.php" // Language -$bb_cfg['charset'] = 'UTF-8'; $bb_cfg['lang'] = [ // Languages available for selecting 'af' => [ diff --git a/library/defines.php b/library/defines.php index f087efa17..d3ce318a9 100644 --- a/library/defines.php +++ b/library/defines.php @@ -30,13 +30,16 @@ define('TEMPLATES_DIR', BB_PATH . '/styles/templates'); // System define('APP_NAME', 'TorrentPier'); +define('DEFAULT_CHARSET', 'UTF-8'); define('UPDATER_URL', 'https://api.github.com/repos/torrentpier/torrentpier/releases'); define('UPDATER_FILE', INT_DATA_DIR . '/updater.json'); -define('API_IP_URL', 'https://freeipapi.com/api/json/'); define('CHECKSUMS_FILE', INT_DATA_DIR . '/checksums.md5'); define('RESTORE_CORRUPT_CONFIRM_FILE', INT_DATA_DIR . '/rescorrupt.integrity'); define('COOKIE_DBG', 'bb_dbg'); +// TODO: Move in another section +define('API_IP_URL', 'https://freeipapi.com/api/json/'); + // Templates define('ADMIN_TPL_DIR', TEMPLATES_DIR . '/admin/'); define('XS_USE_ISSET', '1'); diff --git a/library/includes/functions.php b/library/includes/functions.php index 9a2f8860d..2300e3141 100644 --- a/library/includes/functions.php +++ b/library/includes/functions.php @@ -772,13 +772,13 @@ function get_user_id($username) function str_short($text, $max_length, $space = ' ') { - if (!empty($max_length) && !empty($text) && (mb_strlen($text, 'UTF-8') > $max_length)) { - $text = mb_substr($text, 0, $max_length, 'UTF-8'); + if (!empty($max_length) && !empty($text) && (mb_strlen($text, DEFAULT_CHARSET) > $max_length)) { + $text = mb_substr($text, 0, $max_length, DEFAULT_CHARSET); if ($last_space_pos = $max_length - (int)strpos(strrev($text), (string)$space)) { if ($last_space_pos > round($max_length * 3 / 4)) { $last_space_pos--; - $text = mb_substr($text, 0, $last_space_pos, 'UTF-8'); + $text = mb_substr($text, 0, $last_space_pos, DEFAULT_CHARSET); } } $text .= '...'; @@ -927,7 +927,7 @@ function bb_update_config($params, $table = BB_CONFIG) function clean_username($username) { - $username = mb_substr(htmlspecialchars(str_replace("\'", "'", trim($username))), 0, 25, 'UTF-8'); + $username = mb_substr(htmlspecialchars(str_replace("\'", "'", trim($username))), 0, 25, DEFAULT_CHARSET); $username = rtrim($username, "\\"); $username = str_replace("'", "\'", $username); @@ -1372,7 +1372,7 @@ function bb_simple_die($txt, $status_code = null) { global $bb_cfg; - header('Content-Type: text/plain; charset=' . $bb_cfg['charset']); + header('Content-Type: text/plain; charset=' . DEFAULT_CHARSET); if (isset($status_code)) { http_response_code($status_code); @@ -2028,9 +2028,9 @@ function hash_search($hash) $info_hash = DB()->escape(pack('H*', $hash)); // Check info_hash version - if (mb_strlen($hash, 'UTF-8') == 40) { + if (mb_strlen($hash, DEFAULT_CHARSET) == 40) { $info_hash_where = "WHERE info_hash = '$info_hash'"; - } elseif (mb_strlen($hash, 'UTF-8') == 64) { + } elseif (mb_strlen($hash, DEFAULT_CHARSET) == 64) { $info_hash_where = "WHERE info_hash_v2 = '$info_hash'"; } else { bb_die(sprintf($lang['HASH_INVALID'], $hash)); diff --git a/library/includes/init_bb.php b/library/includes/init_bb.php index a823ccecb..bf3b298bd 100644 --- a/library/includes/init_bb.php +++ b/library/includes/init_bb.php @@ -364,7 +364,7 @@ function commify(?float $num, int $decimals = 0, ?string $decimal_separator = '. * @param string $encoding * @return string */ -function html_ent_decode(string $string, int $flags = ENT_QUOTES, string $encoding = 'UTF-8'): string +function html_ent_decode(string $string, int $flags = ENT_QUOTES, string $encoding = DEFAULT_CHARSET): string { return html_entity_decode($string, $flags, $encoding); } diff --git a/library/includes/page_header.php b/library/includes/page_header.php index c4b72c0e4..a4f0434e7 100644 --- a/library/includes/page_header.php +++ b/library/includes/page_header.php @@ -111,7 +111,7 @@ $template->assign_vars([ // The following assigns all _common_ variables that may be used at any point in a template $template->assign_vars([ 'SIMPLE_HEADER' => !empty($gen_simple_header), - 'CONTENT_ENCODING' => $bb_cfg['charset'], + 'CONTENT_ENCODING' => DEFAULT_CHARSET, 'IN_ADMIN' => defined('IN_ADMIN'), 'USER_HIDE_CAT' => (BB_SCRIPT == 'index'), diff --git a/library/includes/ucp/register.php b/library/includes/ucp/register.php index f43f15c49..663497036 100644 --- a/library/includes/ucp/register.php +++ b/library/includes/ucp/register.php @@ -481,7 +481,7 @@ foreach ($profile_fields as $field => $can_edit) { if ($submit && $sig != $pr_data['user_sig']) { $sig = prepare_message($sig); - if (mb_strlen($sig, 'UTF-8') > $bb_cfg['max_sig_chars']) { + if (mb_strlen($sig, DEFAULT_CHARSET) > $bb_cfg['max_sig_chars']) { $errors[] = $lang['SIGNATURE_TOO_LONG']; } elseif (preg_match('#<(a|b|i|u|table|tr|td|img) #i', $sig) || preg_match('#(href|src|target|title)=#i', $sig)) { $errors[] = $lang['SIGNATURE_ERROR_HTML']; diff --git a/src/Dev.php b/src/Dev.php index ce990aaa0..08187f428 100644 --- a/src/Dev.php +++ b/src/Dev.php @@ -255,7 +255,7 @@ class Dev $sql = str_compact($sql); if (!empty($_COOKIE['sql_log_full'])) { - if (mb_strlen($sql, 'UTF-8') > $max_len) { + if (mb_strlen($sql, DEFAULT_CHARSET) > $max_len) { $sql = mb_substr($sql, 0, 50) . ' [...cut...] ' . mb_substr($sql, -50); } } diff --git a/src/Helpers/VersionHelper.php b/src/Helpers/VersionHelper.php index 665342407..00767ac01 100644 --- a/src/Helpers/VersionHelper.php +++ b/src/Helpers/VersionHelper.php @@ -31,7 +31,7 @@ class VersionHelper public static function removerPrefix(string $version): string { $version = trim($version); - $version = mb_strtolower($version, 'UTF-8'); + $version = mb_strtolower($version, DEFAULT_CHARSET); return str_replace(self::VERSION_PREFIX, '', $version); } diff --git a/src/Legacy/BBCode.php b/src/Legacy/BBCode.php index 98df22726..17c12ab6f 100644 --- a/src/Legacy/BBCode.php +++ b/src/Legacy/BBCode.php @@ -322,7 +322,7 @@ class BBCode { $max_len = 70; $href = $m[1]; - $name = (mb_strlen($href, 'UTF-8') > $max_len) ? mb_substr($href, 0, $max_len - 19) . '...' . mb_substr($href, -16) : $href; + $name = (mb_strlen($href, DEFAULT_CHARSET) > $max_len) ? mb_substr($href, 0, $max_len - 19) . '...' . mb_substr($href, -16) : $href; return $this->nofollow_url($href, $name); } diff --git a/src/Updater.php b/src/Updater.php index 6eb4ab722..c4f8f0f00 100644 --- a/src/Updater.php +++ b/src/Updater.php @@ -62,7 +62,7 @@ class Updater $response = file_get_contents(UPDATER_URL, context: $context); if ($response !== false) { - $this->jsonResponse = json_decode(mb_convert_encoding($response, 'UTF-8', mb_detect_encoding($response)), true); + $this->jsonResponse = json_decode(mb_convert_encoding($response, DEFAULT_CHARSET, mb_detect_encoding($response)), true); } // Empty JSON result diff --git a/src/Validate.php b/src/Validate.php index 95c742376..7f4e03380 100644 --- a/src/Validate.php +++ b/src/Validate.php @@ -45,10 +45,10 @@ class Validate $username = clean_username($username); // Length - if (mb_strlen($username, 'UTF-8') > USERNAME_MAX_LENGTH) { + if (mb_strlen($username, DEFAULT_CHARSET) > USERNAME_MAX_LENGTH) { return $lang['USERNAME_TOO_LONG']; } - if (mb_strlen($username, 'UTF-8') < USERNAME_MIN_LENGTH) { + if (mb_strlen($username, DEFAULT_CHARSET) < USERNAME_MIN_LENGTH) { return $lang['USERNAME_TOO_SMALL']; } @@ -170,10 +170,10 @@ class Validate } // Length - if (mb_strlen($password, 'UTF-8') > PASSWORD_MAX_LENGTH) { + if (mb_strlen($password, DEFAULT_CHARSET) > PASSWORD_MAX_LENGTH) { return sprintf($lang['CHOOSE_PASS_ERR_MAX'], PASSWORD_MAX_LENGTH); } - if (mb_strlen($password, 'UTF-8') < PASSWORD_MIN_LENGTH) { + if (mb_strlen($password, DEFAULT_CHARSET) < PASSWORD_MIN_LENGTH) { return sprintf($lang['CHOOSE_PASS_ERR_MIN'], PASSWORD_MIN_LENGTH); } diff --git a/styles/templates/admin/index.tpl b/styles/templates/admin/index.tpl index 0b6155561..fd46b0f1a 100644 --- a/styles/templates/admin/index.tpl +++ b/styles/templates/admin/index.tpl @@ -3,8 +3,7 @@ - - + {L_ADMIN} diff --git a/styles/templates/default/page_header.tpl b/styles/templates/default/page_header.tpl index 0ec86069b..4fa536406 100644 --- a/styles/templates/default/page_header.tpl +++ b/styles/templates/default/page_header.tpl @@ -1,7 +1,7 @@ - +