/
PreparePHP_SELF.fnc.php
264 lines (235 loc) · 5.13 KB
/
PreparePHP_SELF.fnc.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/**
* Prepare PHP SELF function
*
* @package RosarioSIS
* @subpackage functions
*/
/**
* Prepare PHP SELF
* Generates `Modules.php` SELF URL with GET parameters
*
* @example PreparePHP_SELF( $_REQUEST, array(), array( 'modfunc' => 'delete' ) );
*
* @uses URLEscape()
*
* @param array $tmp_REQUEST REQUEST vars (optional). Defaults to $_REQUEST array.
* @param array $remove Remove indexes from $tmp_REQUEST (optional).
* @param array $add Add values $tmp_REQUEST (associative array) (optional).
*
* @return string Modules.php SELF URL
*/
function PreparePHP_SELF( $tmp_REQUEST = [], $remove = [], $add = [] )
{
if ( empty( $tmp_REQUEST ) )
{
$tmp_REQUEST = $_REQUEST;
}
// Remove Cookie vars.
foreach ( $_COOKIE as $key => $value )
{
unset( $tmp_REQUEST[ $key ] );
}
// Remove vars in $remove.
foreach ( (array) $remove as $key )
{
unset( $tmp_REQUEST[ $key ] );
}
// Unescape DB strings.
array_rwalk(
$tmp_REQUEST,
function ( $input )
{
return is_null( $input ) ? $input : str_replace( "''", "'", $input );
}
);
// Add vars in $add.
foreach ( (array) $add as $key => $value )
{
$tmp_REQUEST[ $key ] = $value;
}
// Add modname param.
$PHP_tmp_SELF = 'Modules.php?modname=' . ( isset( $tmp_REQUEST['modname'] ) ? $tmp_REQUEST['modname'] : '' );
unset( $tmp_REQUEST['modname'] );
// Add other params.
foreach ( (array) $tmp_REQUEST as $key => $value )
{
if ( is_array( $value ) )
{
foreach ( $value as $key1 => $value1 )
{
if ( is_array( $value1 ) )
{
foreach ( $value1 as $key2 => $value2 )
{
if ( is_array( $value2 ) )
{
foreach ( $value2 as $key3 => $value3 )
{
if ( $value3 !== '' )
{
$PHP_tmp_SELF .= '&' . $key .
'[' . $key1 . '][' . $key2 .
'][' . $key3 . ']=' . $value3;
}
}
}
elseif ( $value2 !== '' )
{
$PHP_tmp_SELF .= '&' . $key . '[' . $key1 .
'][' . $key2 . ']=' . $value2;
}
}
}
elseif ( $value1 !== '' )
{
$PHP_tmp_SELF .= '&' . $key . '[' . $key1 . ']=' . $value1;
}
}
}
elseif ( $value !== '' )
{
$PHP_tmp_SELF .= '&' . $key . "=" . $value;
}
}
return URLEscape( $PHP_tmp_SELF );
}
/**
* Redirect URL
* Will update the requested URL in the browser,
* (soft redirection using the X-Redirect-Url header)
* removing the requested parameters passed as argument.
* Use after a successful remove / delete / update / save operation.
* Prevents showing an obsolete & confusing delete confirmation screen on page reload.
*
* @since 3.3
*
* @example RedirectURL( array( 'modfunc', 'id' ) );
*
* @uses X-Redirect-Url header.
* @uses PreparePHP_SELF
*
* @see warehouse.js check for X-Redirect-Url
*
* @param array|string $remove Parameters to remove from the $_REQUEST & $_SESSION['_REQUEST_vars'] arrays.
*
* @return boolean False if nothing to remove, else true.
*/
function RedirectURL( $remove )
{
static $remove_all = [];
if ( ! $remove )
{
return false;
}
foreach ( (array) $remove as $request_key )
{
if ( ! isset( $_REQUEST[ $request_key ] ) )
{
continue;
}
$_REQUEST[ $request_key ] = false;
if ( isset( $_SESSION['_REQUEST_vars'][ $request_key ] ) )
{
$_SESSION['_REQUEST_vars'][ $request_key ] = false;
}
}
$remove_all = array_unique( array_merge( $remove_all, (array) $remove ) );
$redirect_url = PreparePHP_SELF( $_GET, $remove_all );
// Redirect URL.
header( 'X-Redirect-Url: ' . $redirect_url );
return true;
}
/**
* My URL encode
* RFC 3986 compliant
*
* @see http://php.net/manual/en/function.urlencode.php#97969
*
* @param string $string String to encode.
*
* @return string Encoded string
*/
function URLEscape( $string )
{
$string = preg_replace_callback(
// Match both decimal & hex code (although hex codes can contain a-f letters).
// Should be enough as the alphabet hex codes only have numbers.
"/(&#x?[0-9]+;?)/i",
function( $match ) {
if ( mb_substr( $match[1], -1 ) !== ';' )
{
// Fix stored XSS security issue: add semicolon to HTML entity so it can be decoded.
// @link https://www.php.net/manual/en/function.html-entity-decode.php#104617
$match[1] .= ';';
}
return $match[1];
},
$string
);
// Fix stored XSS security issue: decode HTML entities from URL.
$string = html_entity_decode( (string) $string );
$remove = [
// Fix stored XSS security issue: remove inline JS from URL.
'javascript:',
];
foreach ( $remove as $remove_string )
{
while ( strpos( $string, $remove_string ) !== false )
{
$string = str_ireplace( $remove, '', $string );
}
}
$entities = [
'%21',
'%2A',
'%27',
'%28',
'%29',
'%3B',
'%3A',
'%40',
'%26',
'%3D',
'%2B',
'%24',
'%2C',
'%2F',
'%3F',
'%25',
'%23',
'%5B',
'%5D',
];
$replacements = [
'!',
'*',
"'",
'(',
')',
';',
':',
'@',
'&',
'=',
'+',
'$',
',',
'/',
'?',
'%',
'#',
'[',
']',
];
return str_replace(
$entities,
$replacements,
rawurlencode( $string )
);
}
// @deprecated since 6.8 Use URLEscape().
function _myURLEncode( $string )
{
return URLEscape( $string );
}