Author: Maurício Meneghini Fauth (MauricioFauth) Committer: GitHub (web-flow) Pusher: sy-records Date: 2024-09-17T15:55:10+08:00
Commit: https://github.com/php/web-php/commit/f252981e5bf16659b2bdadd1d9d68743e41f9d15 Raw diff: https://github.com/php/web-php/commit/f252981e5bf16659b2bdadd1d9d68743e41f9d15.diff Replace MYPHPNET global var with the UserPreferences class (#1071) Changed paths: A src/UserPreferences.php M error.php M include/prepend.inc M include/shared-manual.inc M my.php Diff: diff --git a/error.php b/error.php index f240ea11ec..56a375fe65 100644 --- a/error.php +++ b/error.php @@ -9,6 +9,8 @@ */ +use phpweb\UserPreferences; + // Ensure that our environment is set up include_once __DIR__ . '/include/prepend.inc'; include_once __DIR__ . '/include/languages.inc'; @@ -708,7 +710,7 @@ // ============================================================================ // If no match was found till this point, the last action is to start a // search with the URI the user typed in -$fallback = (myphpnet_urlsearch() === MYPHPNET_URL_MANUAL ? "404manual" : "404quickref"); +$fallback = (myphpnet_urlsearch() === UserPreferences::URL_MANUAL ? "404manual" : "404quickref"); mirror_redirect( '/search.php?show=' . $fallback . '&lang=' . urlencode($LANG) . '&pattern=' . substr($_SERVER['REQUEST_URI'], 1), diff --git a/include/prepend.inc b/include/prepend.inc index 270e57e527..eaf07cab4b 100644 --- a/include/prepend.inc +++ b/include/prepend.inc @@ -1,4 +1,9 @@ <?php + +use phpweb\UserPreferences; + +require_once __DIR__ . '/../src/autoload.php'; + // Compress all pages, if ext/zlib is available on the mirror // XXX Deactivated by sas, causes errors towards delivery machines // ini_set("zlib.output_compression", 1); @@ -68,7 +73,6 @@ unset($SEARCH_BASE); unset($LANG); unset($COUNTRY); unset($ONLOAD); -unset($MYPHPNET); unset($LAST_UPDATED); // Load the My PHP.net settings before any includes @@ -96,91 +100,83 @@ include __DIR__ . '/last_updated.inc'; // Load in the user preferences function myphpnet_load(): void { - global $MYPHPNET, $MYSITE; - - // Empty the preferences array - $MYPHPNET = [false, false, "NONE", 0, false]; + UserPreferences::$languageCode = ''; + UserPreferences::$searchType = UserPreferences::URL_NONE; + UserPreferences::$isUserGroupTipsEnabled = false; - if ($MYSITE === 'http://docs.php.net/') { - $MYPHPNET[4] = true; + if (!isset($_COOKIE['MYPHPNET']) || !is_string($_COOKIE['MYPHPNET']) || $_COOKIE['MYPHPNET'] === '') { + return; } - // If we have a cookie, set the values in the array - if (!empty($_COOKIE['MYPHPNET'])) { - $MYPHPNET = explode(",", $_COOKIE['MYPHPNET']); + /** + * 0 - Language code + * 1 - URL search fallback + * 2 - Mirror site (removed) + * 3 - User Group tips + * 4 - Documentation developmental server (removed) + */ + $preferences = explode(",", $_COOKIE['MYPHPNET']); + UserPreferences::$languageCode = $preferences[0] ?? ''; + if (isset($preferences[1]) && in_array($preferences[1], [UserPreferences::URL_FUNC, UserPreferences::URL_MANUAL], true)) { + UserPreferences::$searchType = $preferences[1]; } - // Ignore site part, and always use https://www.php.net - $MYPHPNET[2] = 'https://www.php.net'; + UserPreferences::$isUserGroupTipsEnabled = isset($preferences[3]) && $preferences[3]; } // Get preferred language code function myphpnet_language(): string { - global $MYPHPNET; - - // Return code - if (isset($MYPHPNET[0]) && $MYPHPNET[0]) { - return $MYPHPNET[0]; - } - return ''; + return UserPreferences::$languageCode; } -const MYPHPNET_URL_NONE = false; -const MYPHPNET_URL_FUNC = 'quickref'; -const MYPHPNET_URL_MANUAL = 'manual'; - // Set URL search fallback preference function myphpnet_urlsearch($type = false) { - global $MYPHPNET; - // Set type if specified and if correct - if ($type && in_array($type, [MYPHPNET_URL_FUNC, MYPHPNET_URL_MANUAL], true)) { - $MYPHPNET[1] = $type; + if ($type && in_array($type, [UserPreferences::URL_FUNC, UserPreferences::URL_MANUAL], true)) { + UserPreferences::$searchType = $type; } - // Return code or NONE - elseif (isset($MYPHPNET[1]) && !empty($MYPHPNET[1])) { - return $MYPHPNET[1]; - } else { return MYPHPNET_URL_NONE; } + return UserPreferences::$searchType; } function myphpnet_showug($enable = null) { - global $MYPHPNET; - if (isset($_GET["showug"])) { $enable = true; } - if ($enable !== null) { - $MYPHPNET[3] = $enable; - } - - if (isset($MYPHPNET[3])) { - return $MYPHPNET[3]; + if (is_bool($enable)) { + UserPreferences::$isUserGroupTipsEnabled = $enable; } + // Show the ug tips to lucky few, depending on time. if ($_SERVER["REQUEST_TIME"] % 10) { - return true; + UserPreferences::$isUserGroupTipsEnabled = true; } - return false; + return UserPreferences::$isUserGroupTipsEnabled; } // Save user settings in cookie function myphpnet_save(): void { - global $MYPHPNET; - - // Fill in values not specified - for ($i = 0; $i <= 3; $i++) { - if (!isset($MYPHPNET[$i])) { $MYPHPNET[$i] = false; } - } + /** + * 0 - Language code + * 1 - URL search fallback + * 2 - Mirror site (removed) + * 3 - User Group tips + * 4 - Documentation developmental server (removed) + */ + $preferences = [ + UserPreferences::$languageCode, + UserPreferences::$searchType, + '', + UserPreferences::$isUserGroupTipsEnabled, + ]; // Set all the preferred values for a year - mirror_setcookie("MYPHPNET", join(",", $MYPHPNET), 60 * 60 * 24 * 365); - + mirror_setcookie("MYPHPNET", join(",", $preferences), 60 * 60 * 24 * 365); } // Embed Google Custom Search engine diff --git a/include/shared-manual.inc b/include/shared-manual.inc index 3162728108..6b0fed09f3 100644 --- a/include/shared-manual.inc +++ b/include/shared-manual.inc @@ -22,8 +22,6 @@ $PGI = []; $SIDEBAR_DATA = ''; // User note display functions // ============================================================================= -require_once __DIR__ . '/../src/autoload.php'; - use phpweb\UserNotes\Sorter; use phpweb\UserNotes\UserNote; diff --git a/my.php b/my.php index dd68a9d3bd..2a0ae0e39c 100644 --- a/my.php +++ b/my.php @@ -1,4 +1,7 @@ <?php + +use phpweb\UserPreferences; + $_SERVER['BASE_PAGE'] = 'my.php'; include_once __DIR__ . '/include/prepend.inc'; @@ -13,7 +16,7 @@ if (isset($_POST['my_lang'], $langs[$_POST['my_lang']])) { // Set the language preference - $MYPHPNET[0] = $_POST['my_lang']; + UserPreferences::$languageCode = $_POST['my_lang']; // Add this as first option, selected $options[] = '<option value="' . $_POST['my_lang'] . '" selected>' . @@ -175,11 +178,11 @@ Your setting: <input id="form-urlsearch-quickref" type="radio" name="urlsearch" value="quickref" <?php $type = myphpnet_urlsearch(); -if ($type === MYPHPNET_URL_NONE || $type === MYPHPNET_URL_FUNC) { +if ($type === UserPreferences::URL_NONE || $type === UserPreferences::URL_FUNC) { echo ' checked="checked"'; } echo '> <label for="form-urlsearch-quickref">Function list search</label> <input id="form-urlsearch-manual" type="radio" name="urlsearch" value="manual"'; -if ($type === MYPHPNET_URL_MANUAL) { +if ($type === UserPreferences::URL_MANUAL) { echo ' checked="checked"'; } ?> diff --git a/src/UserPreferences.php b/src/UserPreferences.php new file mode 100644 index 0000000000..6b7b1eef60 --- /dev/null +++ b/src/UserPreferences.php @@ -0,0 +1,26 @@ +<?php + +namespace phpweb; + +/** + * Handles the "My PHP.net" preferences. + */ +final class UserPreferences +{ + public const URL_NONE = false; + + public const URL_FUNC = 'quickref'; + + public const URL_MANUAL = 'manual'; + + public static string $languageCode = ''; + + /** + * URL search fallback + * + * @var 'manual'|'quickref'|false + */ + public static string|false $searchType = self::URL_NONE; + + public static bool $isUserGroupTipsEnabled = false; +}