Skip to content

Commit

Permalink
Replace MYPHPNET global var with the UserPreferences class (php#1071)
Browse files Browse the repository at this point in the history
  • Loading branch information
MauricioFauth authored Sep 17, 2024
1 parent c7425ed commit f252981
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 55 deletions.
4 changes: 3 additions & 1 deletion error.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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),
Expand Down
94 changes: 45 additions & 49 deletions include/prepend.inc
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions include/shared-manual.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 6 additions & 3 deletions my.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

use phpweb\UserPreferences;

$_SERVER['BASE_PAGE'] = 'my.php';
include_once __DIR__ . '/include/prepend.inc';

Expand All @@ -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>' .
Expand Down Expand Up @@ -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"';
}
?>
Expand Down
26 changes: 26 additions & 0 deletions src/UserPreferences.php
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit f252981

Please sign in to comment.