1<?php 2 3declare(strict_types=1); 4 5namespace phpweb; 6 7use function explode; 8use function in_array; 9use function is_string; 10use function join; 11use function mirror_setcookie; 12 13/** 14 * Handles the "My PHP.net" preferences. 15 */ 16final class UserPreferences 17{ 18 public const URL_NONE = false; 19 20 public const URL_FUNC = 'quickref'; 21 22 public const URL_MANUAL = 'manual'; 23 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 } 72 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]; 83 84 // Set all the preferred values for a year 85 mirror_setcookie("MYPHPNET", join(",", $preferences), 60 * 60 * 24 * 365); 86 } 87} 88