|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Smr\Page; |
|
4
|
|
|
|
|
5
|
|
|
use AbstractSmrPlayer; |
|
6
|
|
|
use Smr\Session; |
|
7
|
|
|
use Smr\Template; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* A container that holds data needed to create a new page. |
|
11
|
|
|
*/ |
|
12
|
|
|
class Page { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Used to skip redirect hooks at the beginning of page processing. |
|
16
|
|
|
*/ |
|
17
|
|
|
public bool $skipRedirect = false; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Used to allow a page to be processed from an AJAX call. |
|
21
|
|
|
*/ |
|
22
|
|
|
public bool $allowAjax = false; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Storage to remember if we need to display the Under Attack message. |
|
26
|
|
|
*/ |
|
27
|
|
|
protected bool $underAttack = false; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Template file associated with page (for display pages only). |
|
31
|
|
|
*/ |
|
32
|
|
|
public string $file = ''; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Defines if the page is is always available, or if it is invalid after one |
|
36
|
|
|
* use (i.e. if you get a back button error when navigating back to it). |
|
37
|
|
|
* Only relevant to pages stored as links in the session. |
|
38
|
|
|
*/ |
|
39
|
|
|
public function isLinkReusable(): bool { |
|
40
|
|
|
// Pages are single-use unless explicitly whitelisted by ReusableTrait |
|
41
|
|
|
return false; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Determine if we should show the player that they are under attack, |
|
46
|
|
|
* since it needs to persist across ajax updates. |
|
47
|
|
|
*/ |
|
48
|
|
|
public function showUnderAttack(AbstractSmrPlayer $player, bool $ajax): bool { |
|
49
|
|
|
// Only ever change the stored value from false -> true so that the under |
|
50
|
|
|
// attack warning persists for the lifetime of this Page. |
|
51
|
|
|
if ($player->isUnderAttack()) { |
|
52
|
|
|
$this->underAttack = true; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// Don't modify the player state in an ajax call so that the next real |
|
56
|
|
|
// page load will also show if the player is under attack (to avoid brief |
|
57
|
|
|
// warning flashes if the ajax call occurs just before a real page load). |
|
58
|
|
|
if (!$ajax) { |
|
59
|
|
|
$player->setUnderAttack(false); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $this->underAttack; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Forward to the page identified by this container. |
|
67
|
|
|
*/ |
|
68
|
|
|
public function go(): never { |
|
69
|
|
|
if (defined('OVERRIDE_FORWARD') && OVERRIDE_FORWARD === true) { |
|
70
|
|
|
overrideForward($this); |
|
71
|
|
|
} |
|
72
|
|
|
Session::getInstance()->setCurrentVar($this); |
|
73
|
|
|
do_voodoo(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Create an HREF (based on a random SN) to link to this page. |
|
78
|
|
|
* The container is saved in the Smr\Session under this SN so that on |
|
79
|
|
|
* the next request, we can grab the container out of the Smr\Session. |
|
80
|
|
|
*/ |
|
81
|
|
|
public function href(bool $forceFullURL = false): string { |
|
82
|
|
|
// We need to clone this instance in case it is modified after being added |
|
83
|
|
|
// to the session links. This would not be necessary if Page was readonly. |
|
84
|
|
|
$sn = Session::getInstance()->addLink(clone $this); |
|
85
|
|
|
|
|
86
|
|
|
$href = '?sn=' . $sn; |
|
87
|
|
|
if ($forceFullURL === true || $_SERVER['SCRIPT_NAME'] !== LOADER_URI) { |
|
88
|
|
|
return LOADER_URI . $href; |
|
89
|
|
|
} |
|
90
|
|
|
return $href; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Process this page by executing the associated file. |
|
95
|
|
|
*/ |
|
96
|
|
|
public function process(): void { |
|
97
|
|
|
if ($this instanceof PlayerPage) { |
|
98
|
|
|
$this->build(Session::getInstance()->getPlayer(), Template::getInstance()); |
|
99
|
|
|
} elseif ($this instanceof PlayerPageProcessor) { |
|
100
|
|
|
$this->build(Session::getInstance()->getPlayer()); |
|
101
|
|
|
} elseif ($this instanceof AccountPage) { |
|
102
|
|
|
$this->build(Session::getInstance()->getAccount(), Template::getInstance()); |
|
103
|
|
|
} elseif ($this instanceof AccountPageProcessor) { |
|
104
|
|
|
$this->build(Session::getInstance()->getAccount()); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|