1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package boot |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Redirects the browser to a specified location. Safer than using a |
9
|
|
|
* direct header() call |
10
|
|
|
* |
11
|
|
|
* @param string $url |
12
|
|
|
*/ |
13
|
|
|
function redirect($url) |
14
|
|
|
{ |
15
|
|
|
// Just make sure. |
16
|
|
|
$url = str_replace('Location:', null, $url); |
17
|
|
|
|
18
|
|
|
if (headers_sent($filename, $line)) { |
19
|
|
|
echo "<h1>Error: Cannot redirect to <a href=\"$url\">$url</a></h1><p>Output has already started in $filename on line $line</p>"; |
|
|
|
|
20
|
|
|
exit; |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
// convert idn back to ascii for redirect |
24
|
|
|
|
25
|
|
|
if (function_exists('idn_to_ascii')) { |
26
|
|
|
$root = parse_url(URL); |
|
|
|
|
27
|
|
|
$host = $root['host']; |
28
|
|
|
$url = str_replace($host, idn_to_ascii($host), $url); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
cleanup_session_cookies(); |
32
|
|
|
header('Status: 302 Found'); |
33
|
|
|
header('Expires: Mon, 12 Dec 1982 06:00:00 GMT'); |
34
|
|
|
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); |
35
|
|
|
header('Cache-Control: no-cache, must-revalidate, max-age=0'); |
36
|
|
|
header('Pragma: no-cache'); |
37
|
|
|
header("Location: $url"); |
|
|
|
|
38
|
|
|
|
39
|
|
|
exit; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns the current working directory, replacing any \ |
44
|
|
|
* with /. Use for Windows compatibility. |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
function getcwd_safe() |
49
|
|
|
{ |
50
|
|
|
return str_replace('\\', '/', getcwd()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Checks that a constant has not been defined before defining |
55
|
|
|
* it. If the constant is already defined, this function will do |
56
|
|
|
* nothing, otherwise, it will set the constant |
57
|
|
|
* |
58
|
|
|
* @param string $name |
59
|
|
|
* The name of the constant to set |
60
|
|
|
* @param string|integer|boolean $value |
61
|
|
|
* The value of the desired constant |
62
|
|
|
*/ |
63
|
|
|
function define_safe($name, $value) |
64
|
|
|
{ |
65
|
|
|
if (!defined($name)) { |
66
|
|
|
define($name, $value); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Retrieve a value from the $_SERVER array. Makes sure the key exists. |
72
|
|
|
* Returns null otherwise. |
73
|
|
|
* |
74
|
|
|
* This function is an extension point. We could check other storage for |
75
|
|
|
* specific values or enforce some security restrictions. |
76
|
|
|
* |
77
|
|
|
* @param string $name |
78
|
|
|
* The name of the value to retrieve |
79
|
|
|
* @return mixed |
80
|
|
|
* The value, is it exists |
81
|
|
|
*/ |
82
|
|
|
function server_safe($name) |
83
|
|
|
{ |
84
|
|
|
if (isset($_SERVER[$name])) { |
85
|
|
|
return $_SERVER[$name]; |
86
|
|
|
} |
|
|
|
|
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns the current URL string from within the Administration |
92
|
|
|
* context. It omits the Symphony directory from the current URL. |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
function getCurrentPage() |
97
|
|
|
{ |
98
|
|
|
if (!isset($_GET['symphony-page']) || !is_string($_GET['symphony-page'])) { |
99
|
|
|
return null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return '/' . filter_var(trim($_GET['symphony-page'], '/'), FILTER_SANITIZE_STRING) . '/'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Used as a basic stopwatch for profiling. The default `$action` |
107
|
|
|
* starts the timer. Setting `$action` to 'stop' and passing the |
108
|
|
|
* start time returns the difference between now and that time. |
109
|
|
|
* |
110
|
|
|
* @param string $action (optional) |
111
|
|
|
* @param integer $start_time (optional) |
112
|
|
|
* @return integer |
113
|
|
|
*/ |
114
|
|
|
function precision_timer($action = 'start', $start_time = null) |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
$currtime = microtime(true); |
117
|
|
|
|
118
|
|
|
if ($action == 'stop') { |
119
|
|
|
return $currtime - $start_time; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $currtime; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Convert php.ini size format to bytes |
127
|
|
|
* |
128
|
|
|
* @param string $val (optional) |
129
|
|
|
* @return integer |
130
|
|
|
*/ |
131
|
|
|
function ini_size_to_bytes($val) |
132
|
|
|
{ |
133
|
|
|
$val = trim($val); |
134
|
|
|
$last = strtolower($val[strlen($val)-1]); |
135
|
|
|
|
136
|
|
|
$val = (int) $val; |
137
|
|
|
|
138
|
|
|
switch ($last) { |
139
|
|
|
case 'g': |
140
|
|
|
$val *= 1024; |
|
|
|
|
141
|
|
|
case 'm': |
142
|
|
|
$val *= 1024; |
|
|
|
|
143
|
|
|
case 'k': |
144
|
|
|
$val *= 1024; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $val; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Cleans up Session Cookies. When there is no data in the session the cookie will be unset. |
152
|
|
|
* If there is data, the cookie will be renewed, expiring it in two weeks from now. |
153
|
|
|
* This will improve the interoperability with caches like Varnish and Squid. |
154
|
|
|
* |
155
|
|
|
* @since 2.3.3 |
156
|
|
|
* @author creativedutchmen (Huib Keemink) |
157
|
|
|
* @return void |
158
|
|
|
*/ |
159
|
|
|
function cleanup_session_cookies() |
160
|
|
|
{ |
161
|
|
|
/* |
162
|
|
|
Unfortunately there is no way to delete a specific previously set cookie from PHP. |
163
|
|
|
The only way seems to be the method employed here: store all the cookie we need to keep, then delete every cookie and add the stored cookies again. |
164
|
|
|
Luckily we can just store the raw header and output them again, so we do not need to actively parse the header string. |
165
|
|
|
*/ |
166
|
|
|
$cookie_params = session_get_cookie_params(); |
167
|
|
|
$list = headers_list(); |
168
|
|
|
$custom_cookies = array(); |
169
|
|
|
|
170
|
|
|
foreach ($list as $hdr) { |
171
|
|
|
if ((stripos($hdr, 'Set-Cookie') !== false) && (stripos($hdr, session_id()) === false)) { |
172
|
|
|
$custom_cookies[] = $hdr; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
header_remove('Set-Cookie'); |
177
|
|
|
|
178
|
|
|
foreach ($custom_cookies as $custom_cookie) { |
179
|
|
|
header($custom_cookie); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$session_is_empty = is_session_empty(); |
183
|
|
|
|
184
|
|
|
if ($session_is_empty && !empty($_COOKIE[session_name()])) { |
185
|
|
|
setcookie( |
186
|
|
|
session_name(), |
187
|
|
|
session_id(), |
188
|
|
|
time() - 3600, |
189
|
|
|
$cookie_params['path'], |
190
|
|
|
$cookie_params['domain'], |
191
|
|
|
$cookie_params['secure'], |
192
|
|
|
$cookie_params['httponly'] |
193
|
|
|
); |
194
|
|
|
} elseif (!$session_is_empty) { |
195
|
|
|
setcookie( |
196
|
|
|
session_name(), |
197
|
|
|
session_id(), |
198
|
|
|
time() + TWO_WEEKS, |
|
|
|
|
199
|
|
|
$cookie_params['path'], |
200
|
|
|
$cookie_params['domain'], |
201
|
|
|
$cookie_params['secure'], |
202
|
|
|
$cookie_params['httponly'] |
203
|
|
|
); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Function will loop over the $_SESSION and find out if it is empty or not |
209
|
|
|
* |
210
|
|
|
* @since Symphony 2.4 |
211
|
|
|
* @return boolean |
212
|
|
|
*/ |
213
|
|
|
function is_session_empty() |
214
|
|
|
{ |
215
|
|
|
$session_is_empty = true; |
216
|
|
|
if (isset($_SESSION) && is_array($_SESSION)) { |
217
|
|
|
foreach ($_SESSION as $contents) { |
218
|
|
|
if (!empty($contents)) { |
219
|
|
|
$session_is_empty = false; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $session_is_empty; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Responsible for picking the launcher function and starting it. |
229
|
|
|
* |
230
|
|
|
* @param string $mode (optional) |
231
|
|
|
*/ |
232
|
|
|
function symphony($mode) |
233
|
|
|
{ |
234
|
|
|
$launcher = SYMPHONY_LAUNCHER; |
235
|
|
|
$launcher($mode); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Responsible for launching a standard symphony instance and |
240
|
|
|
* sending output to the browser. |
241
|
|
|
* |
242
|
|
|
* @param string $mode (optional) |
243
|
|
|
* @return integer |
244
|
|
|
*/ |
245
|
|
|
function symphony_launcher($mode) |
246
|
|
|
{ |
247
|
|
|
if (is_string($mode) && strtolower($mode) == 'administration') { |
248
|
|
|
$renderer = Administration::instance(); |
249
|
|
|
} else { |
250
|
|
|
$renderer = Frontend::instance(); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
$output = $renderer->display(getCurrentPage()); |
254
|
|
|
|
255
|
|
|
// #1808 |
256
|
|
|
if (server_safe('HTTP_MOD_REWRITE') != null) { |
257
|
|
|
$output = file_get_contents(GenericExceptionHandler::getTemplate('fatalerror.rewrite')); |
|
|
|
|
258
|
|
|
$output = str_replace('{ASSETS_URL}', ASSETS_URL, $output); |
|
|
|
|
259
|
|
|
$output = str_replace('{SYMPHONY_URL}', SYMPHONY_URL, $output); |
|
|
|
|
260
|
|
|
$output = str_replace('{URL}', URL, $output); |
|
|
|
|
261
|
|
|
echo $output; |
262
|
|
|
exit; |
|
|
|
|
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
cleanup_session_cookies(); |
266
|
|
|
|
267
|
|
|
echo $output; |
268
|
|
|
|
269
|
|
|
return $renderer; |
|
|
|
|
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* The translation function accepts an English string and returns its translation |
275
|
|
|
* to the active system language. If the given string is not available in the |
276
|
|
|
* current dictionary the original English string will be returned. Given an optional |
277
|
|
|
* `$inserts` array, the function will replace translation placeholders using `vsprintf()`. |
278
|
|
|
* Since Symphony 2.3, it is also possible to have multiple translation of the same string |
279
|
|
|
* according to the page namespace (i.e. the value returned by Symphony's `getPageNamespace()` |
280
|
|
|
* method). In your lang file, use the `$dictionary` key as namespace and its value as an array |
281
|
|
|
* of context-aware translations, as shown below: |
282
|
|
|
* |
283
|
|
|
* $dictionary = array( |
284
|
|
|
* [...] |
285
|
|
|
* |
286
|
|
|
* 'Create new' => 'Translation for Create New', |
287
|
|
|
* |
288
|
|
|
* '/blueprints/datasources' => array( |
289
|
|
|
* 'Create new' => |
290
|
|
|
* 'If we are inside a /blueprints/datasources/* page, this translation will be returned for the string' |
291
|
|
|
* ), |
292
|
|
|
* |
293
|
|
|
* [...] |
294
|
|
|
* ); |
295
|
|
|
* |
296
|
|
|
* @see core.Symphony#getPageNamespace() |
297
|
|
|
* @param string $string |
298
|
|
|
* The string that should be translated |
299
|
|
|
* @param array $inserts (optional) |
300
|
|
|
* Optional array used to replace translation placeholders, defaults to NULL |
301
|
|
|
* @return string |
302
|
|
|
* Returns the translated string |
303
|
|
|
*/ |
304
|
|
|
function __($string, $inserts = null) |
|
|
|
|
305
|
|
|
{ |
306
|
|
|
return Lang::translate($string, $inserts); |
307
|
|
|
} |
308
|
|
|
|
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.