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 $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
|
|
|
* Returns the current URL string from within the Administration |
72
|
|
|
* context. It omits the Symphony directory from the current URL. |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
function getCurrentPage() |
77
|
|
|
{ |
78
|
|
|
if (!isset($_GET['symphony-page']) || !is_string($_GET['symphony-page'])) { |
79
|
|
|
return null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return '/' . filter_var(trim($_GET['symphony-page'], '/'), FILTER_SANITIZE_STRING) . '/'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Used as a basic stopwatch for profiling. The default `$action` |
87
|
|
|
* starts the timer. Setting `$action` to 'stop' and passing the |
88
|
|
|
* start time returns the difference between now and that time. |
89
|
|
|
* |
90
|
|
|
* @param string $action (optional) |
91
|
|
|
* @param integer $start_time (optional) |
92
|
|
|
* @return integer |
93
|
|
|
*/ |
94
|
|
|
function precision_timer($action = 'start', $start_time = null) |
95
|
|
|
{ |
96
|
|
|
$currtime = microtime(true); |
97
|
|
|
|
98
|
|
|
if ($action == 'stop') { |
99
|
|
|
return $currtime - $start_time; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $currtime; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Convert php.ini size format to bytes |
107
|
|
|
* |
108
|
|
|
* @param string $val (optional) |
109
|
|
|
* @return integer |
110
|
|
|
*/ |
111
|
|
|
function ini_size_to_bytes($val) |
112
|
|
|
{ |
113
|
|
|
$val = trim($val); |
114
|
|
|
$last = strtolower($val[strlen($val)-1]); |
115
|
|
|
|
116
|
|
View Code Duplication |
switch ($last) { |
117
|
|
|
case 'g': |
|
|
|
|
118
|
|
|
$val *= 1024; |
119
|
|
|
case 'm': |
|
|
|
|
120
|
|
|
$val *= 1024; |
121
|
|
|
case 'k': |
122
|
|
|
$val *= 1024; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $val; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Cleans up Session Cookies. When there is no data in the session the cookie will be unset. |
130
|
|
|
* If there is data, the cookie will be renewed, expiring it in two weeks from now. |
131
|
|
|
* This will improve the interoperability with caches like Varnish and Squid. |
132
|
|
|
* |
133
|
|
|
* @since 2.3.3 |
134
|
|
|
* @author creativedutchmen (Huib Keemink) |
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
|
|
function cleanup_session_cookies() |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
/* |
140
|
|
|
Unfortunately there is no way to delete a specific previously set cookie from PHP. |
141
|
|
|
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. |
142
|
|
|
Luckily we can just store the raw header and output them again, so we do not need to actively parse the header string. |
143
|
|
|
*/ |
144
|
|
|
$cookie_params = session_get_cookie_params(); |
145
|
|
|
$list = headers_list(); |
146
|
|
|
$custom_cookies = array(); |
147
|
|
|
|
148
|
|
|
foreach ($list as $hdr) { |
149
|
|
|
if ((stripos($hdr, 'Set-Cookie') !== false) && (stripos($hdr, session_id()) === false)) { |
150
|
|
|
$custom_cookies[] = $hdr; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
header_remove('Set-Cookie'); |
155
|
|
|
|
156
|
|
|
foreach ($custom_cookies as $custom_cookie) { |
157
|
|
|
header($custom_cookie); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$session_is_empty = is_session_empty(); |
161
|
|
|
|
162
|
|
|
if ($session_is_empty && !empty($_COOKIE[session_name()])) { |
163
|
|
|
setcookie( |
164
|
|
|
session_name(), |
165
|
|
|
session_id(), |
166
|
|
|
time() - 3600, |
167
|
|
|
$cookie_params['path'], |
168
|
|
|
$cookie_params['domain'], |
169
|
|
|
$cookie_params['secure'], |
170
|
|
|
$cookie_params['httponly'] |
171
|
|
|
); |
172
|
|
|
} elseif (!$session_is_empty) { |
173
|
|
|
setcookie( |
174
|
|
|
session_name(), |
175
|
|
|
session_id(), |
176
|
|
|
time() + TWO_WEEKS, |
177
|
|
|
$cookie_params['path'], |
178
|
|
|
$cookie_params['domain'], |
179
|
|
|
$cookie_params['secure'], |
180
|
|
|
$cookie_params['httponly'] |
181
|
|
|
); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Function will loop over the $_SESSION and find out if it is empty or not |
187
|
|
|
* |
188
|
|
|
* @since Symphony 2.4 |
189
|
|
|
* @return boolean |
190
|
|
|
*/ |
191
|
|
|
function is_session_empty() |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
$session_is_empty = true; |
194
|
|
|
if (is_array($_SESSION)) { |
195
|
|
|
foreach ($_SESSION as $contents) { |
196
|
|
|
if (!empty($contents)) { |
197
|
|
|
$session_is_empty = false; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $session_is_empty; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Responsible for picking the launcher function and starting it. |
207
|
|
|
* |
208
|
|
|
* @param string $mode (optional) |
209
|
|
|
*/ |
210
|
|
|
function symphony($mode) |
211
|
|
|
{ |
212
|
|
|
$launcher = SYMPHONY_LAUNCHER; |
213
|
|
|
$launcher($mode); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Responsible for launching a standard symphony instance and |
218
|
|
|
* sending output to the browser. |
219
|
|
|
* |
220
|
|
|
* @param string $mode (optional) |
221
|
|
|
* @return integer |
222
|
|
|
*/ |
223
|
|
|
function symphony_launcher($mode) |
|
|
|
|
224
|
|
|
{ |
225
|
|
|
if (is_string($mode) && strtolower($mode) == 'administration') { |
226
|
|
|
$renderer = Administration::instance(); |
227
|
|
|
} else { |
228
|
|
|
$renderer = Frontend::instance(); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$output = $renderer->display(getCurrentPage()); |
232
|
|
|
|
233
|
|
|
// #1808 |
234
|
|
|
if (isset($_SERVER['HTTP_MOD_REWRITE'])) { |
235
|
|
|
$output = file_get_contents(GenericExceptionHandler::getTemplate('fatalerror.rewrite')); |
236
|
|
|
$output = str_replace('{ASSETS_URL}', ASSETS_URL, $output); |
237
|
|
|
$output = str_replace('{SYMPHONY_URL}', SYMPHONY_URL, $output); |
238
|
|
|
$output = str_replace('{URL}', URL, $output); |
239
|
|
|
echo $output; |
240
|
|
|
exit; |
|
|
|
|
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
cleanup_session_cookies(); |
244
|
|
|
|
245
|
|
|
echo $output; |
246
|
|
|
|
247
|
|
|
return $renderer; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* The translation function accepts an English string and returns its translation |
253
|
|
|
* to the active system language. If the given string is not available in the |
254
|
|
|
* current dictionary the original English string will be returned. Given an optional |
255
|
|
|
* `$inserts` array, the function will replace translation placeholders using `vsprintf()`. |
256
|
|
|
* Since Symphony 2.3, it is also possible to have multiple translation of the same string |
257
|
|
|
* according to the page namespace (i.e. the value returned by Symphony's `getPageNamespace()` |
258
|
|
|
* method). In your lang file, use the `$dictionary` key as namespace and its value as an array |
259
|
|
|
* of context-aware translations, as shown below: |
260
|
|
|
* |
261
|
|
|
* $dictionary = array( |
262
|
|
|
* [...] |
263
|
|
|
* |
264
|
|
|
* 'Create new' => 'Translation for Create New', |
265
|
|
|
* |
266
|
|
|
* '/blueprints/datasources' => array( |
267
|
|
|
* 'Create new' => |
268
|
|
|
* 'If we are inside a /blueprints/datasources/* page, this translation will be returned for the string' |
269
|
|
|
* ), |
270
|
|
|
* |
271
|
|
|
* [...] |
272
|
|
|
* ); |
273
|
|
|
* |
274
|
|
|
* @see core.Symphony#getPageNamespace() |
275
|
|
|
* @param string $string |
276
|
|
|
* The string that should be translated |
277
|
|
|
* @param array $inserts (optional) |
278
|
|
|
* Optional array used to replace translation placeholders, defaults to NULL |
279
|
|
|
* @return string |
280
|
|
|
* Returns the translated string |
281
|
|
|
*/ |
282
|
|
|
function __($string, $inserts = null) |
283
|
|
|
{ |
284
|
|
|
return Lang::translate($string, $inserts); |
285
|
|
|
} |
286
|
|
|
|
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.