/
href.php
200 lines (159 loc) · 5.17 KB
/
href.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/*
$Id$
CE Phoenix, E-Commerce made Easy
https://phoenixcart.org
Copyright (c) 2021 Phoenix Cart
Released under the GNU General Public License
*/
class Href implements \JsonSerializable {
protected $include_session = true;
protected $page;
protected $parameters = [];
protected $separator_encoding = true;
protected $hooks = null;
public function __construct($prefix = '', $page = null, $parameters = [], $add_session_id = true) {
$this->page = Text::output($prefix . ($page ?? Request::get_page()));
if (is_array($parameters)) {
$this->parameters = $parameters;
} elseif (!Text::is_empty($parameters)) {
parse_str($parameters, $this->parameters);
}
$this->set_include_session($add_session_id);
}
public function set_hooks(&$hooks) {
$this->hooks =& $hooks;
return $this;
}
public function set_include_session(bool $include_session) {
$this->include_session = $include_session && (SESSION_FORCE_COOKIE_USE === 'False');
return $this;
}
public function set_page($page) {
$this->page = $page;
return $this;
}
public function set_parameter(string $name, string $value) {
$this->parameters[$name] = $value;
return $this;
}
public function set_separator_encoding($separator_encoding) {
$this->separator_encoding = $separator_encoding;
return $this;
}
public function add_parameters(array $parameters) {
$this->parameters += $parameters;
return $this;
}
public function delete_parameter($name) {
unset($this->parameters[$name]);
return $this;
}
public function get_include_session() {
return $this->include_session;
}
public function get_page() {
return $this->page;
}
public function &get_parameters() {
return $this->parameters;
}
public function get_separator_encoding() {
return $this->separator_encoding;
}
public function retain_parameters(array $excludes = []) {
trigger_error('The retain_parameters method has been deprecated.', E_USER_DEPRECATED);
return $this->retain_query_except($excludes);
}
public function retain_query_except(array $excludes = []) {
$excludes = array_merge($excludes, ['x', 'y', 'error', session_name()]);
static::_flatten(array_diff_key(array_filter($_GET, function ($k) {
return rawurlencode($k) === $k;
}, ARRAY_FILTER_USE_KEY), array_flip($excludes)), $this->parameters);
return $this;
}
public function real_link() {
if (Text::is_empty($this->page)) {
die('<h5>Error!</h5><p>Unable to determine the page link!</p>');
}
// Add the session ID when SID is defined
if ( $this->include_session
&& Session::is_started()
&& isset($GLOBALS['SID'])
&& !Text::is_empty($GLOBALS['SID']))
{
$this->parameters[session_name()] = session_id();
}
$parameters = implode('&', array_map(function ($k, $v) {
return "$k=" . rawurlencode("$v");
}, array_keys($this->parameters), $this->parameters));
$link = $this->page;
if (!Text::is_empty($parameters)) {
$link .= '?' . Text::output($parameters);
}
$link = rtrim($link, '&?');
while (strpos($link, '&&') !== false) {
$link = str_replace('&&', '&', $link);
}
if ($this->separator_encoding) {
$link = str_replace('&', '&', $link);
}
return $link;
}
public function link() {
$chain = [
'link' => &$link,
'href' => $this,
];
$chain = ($this->hooks ?? $GLOBALS['all_hooks'])->chain('hrefLink', $chain);
return $chain['link'] ?? $this->real_link();
}
public function __toString() {
return $this->link();
}
#[\ReturnTypeWillChange]
public function jsonSerialize() {
return "$this";
}
public static function hook($chain) {
if (isset($chain['href'])) {
$chain['link'] = $chain['href']->real_link();
}
return $chain;
}
public static function build(...$arguments) {
return new static(...$arguments);
}
protected static function _build_prefixed_key(string $key, string $prefix = null) {
return is_null($prefix)
? $key
: sprintf('%s[%s]', $prefix, rawurlencode($key));
}
protected static function _flatten(
array $data,
array &$results,
string $prefix = null
) {
foreach ($data as $key => $value) {
$key = static::_build_prefixed_key($key, $prefix);
if (is_array($value)) {
static::_flatten($value, $results, $key);
} else {
$results[$key] = $value;
}
}
}
public static function redirect($url) {
if ($url instanceof Href) {
$url = $url->set_separator_encoding(false)->link();
}
if ( strstr($url, "\n") || strstr($url, "\r") ) {
static::redirect(static::build('index.php')->set_include_session(false)->link());
}
if ( strpos($url, '&') !== false ) {
$url = str_replace('&', '&', $url);
}
header('Location: ' . $url);
exit();
}
}