Skip to content

Commit 817a3e7

Browse files
Move myphpnet_* functions to the UserPreferences class (#1075)
1 parent 7478275 commit 817a3e7

File tree

8 files changed

+182
-110
lines changed

8 files changed

+182
-110
lines changed

error.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@
725725
// ============================================================================
726726
// If no match was found till this point, the last action is to start a
727727
// search with the URI the user typed in
728-
$fallback = (myphpnet_urlsearch() === UserPreferences::URL_MANUAL ? "404manual" : "404quickref");
728+
$fallback = ($userPreferences->searchType === UserPreferences::URL_MANUAL ? "404manual" : "404quickref");
729729
mirror_redirect(
730730
'/search.php?show=' . $fallback . '&lang=' . urlencode($LANG) .
731731
'&pattern=' . substr($_SERVER['REQUEST_URI'], 1),

include/langchooser.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ $_SERVER['STRIPPED_URI'] = htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES,
3737

3838
// The code is encapsulated in a function,
3939
// so the variable namespace is not polluted
40-
list($LANG, $EXPL_LANG) = (new LangChooser($LANGUAGES, $INACTIVE_ONLINE_LANGUAGES, myphpnet_language(), default_language() ?: ''))->chooseCode(
40+
list($LANG, $EXPL_LANG) = (new LangChooser($LANGUAGES, $INACTIVE_ONLINE_LANGUAGES, $userPreferences->languageCode, default_language() ?: ''))->chooseCode(
4141
$_REQUEST['lang'] ?? null,
4242
$_SERVER['REQUEST_URI'],
4343
$_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? null,

include/layout.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,13 +425,13 @@ EOT;
425425

426426
function site_header(string $title = 'Hypertext Preprocessor', array $config = []): void
427427
{
428-
global $MYSITE;
428+
global $MYSITE, $LANG;
429429

430430
$meta_image_path = $MYSITE . 'images/meta-image.png';
431431
$meta_description = "PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.";
432432

433433
$defaults = [
434-
"lang" => myphpnet_language(),
434+
"lang" => $LANG,
435435
"current" => "",
436436
"meta-navigation" => [],
437437
'classes' => '',

include/prepend.inc

Lines changed: 3 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ unset($COUNTRY);
7575
unset($ONLOAD);
7676
unset($LAST_UPDATED);
7777

78+
$userPreferences = new UserPreferences();
79+
7880
// Load the My PHP.net settings before any includes
79-
myphpnet_load();
81+
$userPreferences->load();
8082

8183
// Site details (mirror site information)
8284
include __DIR__ . '/site.inc';
@@ -97,88 +99,6 @@ include __DIR__ . '/last_updated.inc';
9799

98100
// -----------------------------------------------------------------------------
99101

100-
// Load in the user preferences
101-
function myphpnet_load(): void
102-
{
103-
UserPreferences::$languageCode = '';
104-
UserPreferences::$searchType = UserPreferences::URL_NONE;
105-
UserPreferences::$isUserGroupTipsEnabled = false;
106-
107-
if (!isset($_COOKIE['MYPHPNET']) || !is_string($_COOKIE['MYPHPNET']) || $_COOKIE['MYPHPNET'] === '') {
108-
return;
109-
}
110-
111-
/**
112-
* 0 - Language code
113-
* 1 - URL search fallback
114-
* 2 - Mirror site (removed)
115-
* 3 - User Group tips
116-
* 4 - Documentation developmental server (removed)
117-
*/
118-
$preferences = explode(",", $_COOKIE['MYPHPNET']);
119-
UserPreferences::$languageCode = $preferences[0] ?? '';
120-
if (isset($preferences[1]) && in_array($preferences[1], [UserPreferences::URL_FUNC, UserPreferences::URL_MANUAL], true)) {
121-
UserPreferences::$searchType = $preferences[1];
122-
}
123-
124-
UserPreferences::$isUserGroupTipsEnabled = isset($preferences[3]) && $preferences[3];
125-
}
126-
127-
// Get preferred language code
128-
function myphpnet_language(): string
129-
{
130-
return UserPreferences::$languageCode;
131-
}
132-
133-
// Set URL search fallback preference
134-
function myphpnet_urlsearch($type = false)
135-
{
136-
// Set type if specified and if correct
137-
if ($type && in_array($type, [UserPreferences::URL_FUNC, UserPreferences::URL_MANUAL], true)) {
138-
UserPreferences::$searchType = $type;
139-
}
140-
141-
return UserPreferences::$searchType;
142-
}
143-
144-
function myphpnet_showug($enable = null) {
145-
if (isset($_GET["showug"])) {
146-
$enable = true;
147-
}
148-
149-
if (is_bool($enable)) {
150-
UserPreferences::$isUserGroupTipsEnabled = $enable;
151-
}
152-
153-
// Show the ug tips to lucky few, depending on time.
154-
if ($_SERVER["REQUEST_TIME"] % 10) {
155-
UserPreferences::$isUserGroupTipsEnabled = true;
156-
}
157-
158-
return UserPreferences::$isUserGroupTipsEnabled;
159-
}
160-
161-
// Save user settings in cookie
162-
function myphpnet_save(): void
163-
{
164-
/**
165-
* 0 - Language code
166-
* 1 - URL search fallback
167-
* 2 - Mirror site (removed)
168-
* 3 - User Group tips
169-
* 4 - Documentation developmental server (removed)
170-
*/
171-
$preferences = [
172-
UserPreferences::$languageCode,
173-
UserPreferences::$searchType,
174-
'',
175-
UserPreferences::$isUserGroupTipsEnabled,
176-
];
177-
178-
// Set all the preferred values for a year
179-
mirror_setcookie("MYPHPNET", join(",", $preferences), 60 * 60 * 24 * 365);
180-
}
181-
182102
// Embed Google Custom Search engine
183103
function google_cse(): void {
184104
$cse_snippet = <<<EOF

my.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
if (isset($_POST['my_lang'], $langs[$_POST['my_lang']])) {
1717

1818
// Set the language preference
19-
UserPreferences::$languageCode = $_POST['my_lang'];
19+
$userPreferences->languageCode = $_POST['my_lang'];
2020

2121
// Add this as first option, selected
2222
$options[] = '<option value="' . $_POST['my_lang'] . '" selected>' .
@@ -27,14 +27,14 @@
2727
}
2828

2929
// We have received a cookie and it is an available language
30-
elseif (isset($langs[myphpnet_language()])) {
30+
elseif (isset($langs[$userPreferences->languageCode])) {
3131

3232
// Add this as first option, selected
33-
$options[] = '<option value="' . myphpnet_language() . '" selected>' .
34-
$langs[myphpnet_language()] . "</option>\n";
33+
$options[] = '<option value="' . $userPreferences->languageCode . '" selected>' .
34+
$langs[$userPreferences->languageCode] . "</option>\n";
3535

3636
// Remove, so it is not listed two times
37-
unset($langs[myphpnet_language()]);
37+
unset($langs[$userPreferences->languageCode]);
3838
}
3939

4040
// We have no cookie and no form submitted
@@ -54,18 +54,18 @@
5454

5555
// Save URL shortcut fallback setting
5656
if (isset($_POST['urlsearch'])) {
57-
myphpnet_urlsearch($_POST['urlsearch']);
57+
$userPreferences->setUrlSearchType($_POST['urlsearch']);
5858
}
5959

6060
if (isset($_POST["showug"])) {
61-
myphpnet_showug($_POST["showug"] === "enable");
61+
$userPreferences->setIsUserGroupTipsEnabled($_POST["showug"] === "enable");
6262
}
6363

6464
// Prepare mirror array
6565
$mirror_sites = $MIRRORS;
6666
$mirror_sites["NONE"] = [7 => MIRROR_OK];
6767

68-
myphpnet_save();
68+
$userPreferences->save();
6969

7070
site_header("My PHP.net", ["current" => "community"]);
7171
?>
@@ -177,7 +177,7 @@
177177
<div class="indent">
178178
Your setting: <input id="form-urlsearch-quickref" type="radio" name="urlsearch" value="quickref"
179179
<?php
180-
$type = myphpnet_urlsearch();
180+
$type = $userPreferences->searchType;
181181
if ($type === UserPreferences::URL_NONE || $type === UserPreferences::URL_FUNC) {
182182
echo ' checked="checked"';
183183
}
@@ -196,8 +196,8 @@
196196
We are experimenting with listing nearby user groups. This feature is highly experimental
197197
and will very likely change a lot and be broken at times.
198198
</p>
199-
<label for="showugenable">Enable UG tips</label> <input type="radio" name="showug" id="showugenable" value="enable" <?php echo myphpnet_showug() ? "checked=checked" : "" ?>><br>
200-
<label for="showugdisable">Disable UG tips</label> <input type="radio" name="showug" id="showugdisable" value="disable" <?php echo myphpnet_showug() ? "" : "checked=checked" ?>>
199+
<label for="showugenable">Enable UG tips</label> <input type="radio" name="showug" id="showugenable" value="enable" <?php echo $userPreferences->isUserGroupTipsEnabled ? "checked=checked" : "" ?>><br>
200+
<label for="showugdisable">Disable UG tips</label> <input type="radio" name="showug" id="showugdisable" value="disable" <?php echo $userPreferences->isUserGroupTipsEnabled ? "" : "checked=checked" ?>>
201201

202202
<p class="center">
203203
<input type="submit" value="Set All Preferences">

src/UserPreferences.php

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace phpweb;
46

7+
use function explode;
8+
use function in_array;
9+
use function is_string;
10+
use function join;
11+
use function mirror_setcookie;
12+
513
/**
614
* Handles the "My PHP.net" preferences.
715
*/
@@ -13,14 +21,67 @@ final class UserPreferences
1321

1422
public const URL_MANUAL = 'manual';
1523

16-
public static string $languageCode = '';
24+
/** @param self::URL_* $searchType URL search fallback */
25+
public function __construct(
26+
public string $languageCode = '',
27+
public string|false $searchType = self::URL_NONE,
28+
public bool $isUserGroupTipsEnabled = false,
29+
) {
30+
}
31+
32+
public function load(): void
33+
{
34+
$this->languageCode = '';
35+
$this->searchType = self::URL_NONE;
36+
$this->isUserGroupTipsEnabled = false;
37+
38+
if (!isset($_COOKIE['MYPHPNET']) || !is_string($_COOKIE['MYPHPNET']) || $_COOKIE['MYPHPNET'] === '') {
39+
return;
40+
}
41+
42+
/**
43+
* 0 - Language code
44+
* 1 - URL search fallback
45+
* 2 - Mirror site (removed)
46+
* 3 - User Group tips
47+
* 4 - Documentation developmental server (removed)
48+
*/
49+
$preferences = explode(",", $_COOKIE['MYPHPNET']);
50+
$this->languageCode = $preferences[0] ?? '';
51+
$this->setUrlSearchType($preferences[1] ?? self::URL_NONE);
52+
$this->isUserGroupTipsEnabled = isset($preferences[3]) && $preferences[3];
53+
}
54+
55+
public function setUrlSearchType(mixed $type): void
56+
{
57+
if (!in_array($type, [self::URL_FUNC, self::URL_MANUAL, self::URL_NONE], true)) {
58+
return;
59+
}
60+
61+
$this->searchType = $type;
62+
}
63+
64+
public function setIsUserGroupTipsEnabled(bool $enable): void {
65+
// Show the ug tips to lucky few, depending on time.
66+
if ($_SERVER["REQUEST_TIME"] % 10) {
67+
$enable = true;
68+
}
69+
70+
$this->isUserGroupTipsEnabled = $enable;
71+
}
1772

18-
/**
19-
* URL search fallback
20-
*
21-
* @var 'manual'|'quickref'|false
22-
*/
23-
public static string|false $searchType = self::URL_NONE;
73+
public function save(): void
74+
{
75+
/**
76+
* 0 - Language code
77+
* 1 - URL search fallback
78+
* 2 - Mirror site (removed)
79+
* 3 - User Group tips
80+
* 4 - Documentation developmental server (removed)
81+
*/
82+
$preferences = [$this->languageCode, $this->searchType, '', $this->isUserGroupTipsEnabled];
2483

25-
public static bool $isUserGroupTipsEnabled = false;
84+
// Set all the preferred values for a year
85+
mirror_setcookie("MYPHPNET", join(",", $preferences), 60 * 60 * 24 * 365);
86+
}
2687
}

tests/Unit/LangChooserTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use phpweb\LangChooser;
88
use PHPUnit\Framework;
9-
use phpweb\UserPreferences;
109

1110
#[Framework\Attributes\CoversClass(LangChooser::class)]
1211
class LangChooserTest extends Framework\TestCase
@@ -107,9 +106,7 @@ public function testChooseCodeWithLangParameterAndManualPath(): void
107106

108107
public function testChooseCodeWithManualPathAndUserPreference(): void
109108
{
110-
UserPreferences::$languageCode = 'en';
111-
112-
$langChooser = new LangChooser(self::DEFAULT_LANGUAGE_LIST, [], '', 'en');
109+
$langChooser = new LangChooser(self::DEFAULT_LANGUAGE_LIST, [], 'en', 'en');
113110
$result = $langChooser->chooseCode('', '/manual/de', null);
114111

115112
self::assertSame(['de', 'de'], $result);

0 commit comments

Comments
 (0)