1 | <?php |
---|
2 | /** |
---|
3 | * App.inc.php |
---|
4 | * code by strangecode :: www.strangecode.com :: this document contains copyrighted information |
---|
5 | * |
---|
6 | * Primary application framework class. |
---|
7 | * |
---|
8 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
9 | * @version 2.0 |
---|
10 | */ |
---|
11 | |
---|
12 | // Message Types. |
---|
13 | define('MSG_ERR', 1); |
---|
14 | define('MSG_ERROR', MSG_ERR); |
---|
15 | define('MSG_WARNING', 2); |
---|
16 | define('MSG_NOTICE', 4); |
---|
17 | define('MSG_SUCCESS', 8); |
---|
18 | define('MSG_ALL', MSG_SUCCESS | MSG_NOTICE | MSG_WARNING | MSG_ERROR); |
---|
19 | |
---|
20 | require_once dirname(__FILE__) . '/Utilities.inc.php'; |
---|
21 | |
---|
22 | class App { |
---|
23 | |
---|
24 | // Name of this application. |
---|
25 | var $app = '_app_'; |
---|
26 | |
---|
27 | // If App::start has run successfully. |
---|
28 | var $running = false; |
---|
29 | |
---|
30 | // Instance of database object. |
---|
31 | var $db; |
---|
32 | |
---|
33 | // Array of query arguments will be carried persistently between requests. |
---|
34 | var $_carry_queries = array(); |
---|
35 | |
---|
36 | // Hash of global application parameters. |
---|
37 | var $_params = array(); |
---|
38 | |
---|
39 | // Default parameters. |
---|
40 | var $_param_defaults = array( |
---|
41 | |
---|
42 | // Public name and email address for this application. |
---|
43 | 'site_name' => null, |
---|
44 | 'site_email' => null, |
---|
45 | 'site_url' => '', // URL automatically determined by _SERVER['HTTP_HOST'] if not set here. |
---|
46 | |
---|
47 | // The location the user will go if the system doesn't knew where else to send them. |
---|
48 | 'redirect_home_url' => '/', |
---|
49 | |
---|
50 | // SSL URL used when redirecting with App::sslOn(). |
---|
51 | 'ssl_domain' => null, |
---|
52 | 'ssl_enabled' => false, |
---|
53 | |
---|
54 | // Character set for page output. Used in the Content-Type header and the HTML <meta content-type> tag. |
---|
55 | 'character_set' => 'utf-8', |
---|
56 | |
---|
57 | // Human-readable format used to display dates. |
---|
58 | 'date_format' => 'd M Y', |
---|
59 | 'time_format' => 'h:i:s A', |
---|
60 | 'sql_date_format' => '%e %b %Y', |
---|
61 | 'sql_time_format' => '%k:%i', |
---|
62 | |
---|
63 | // Use php sessions? |
---|
64 | 'enable_session' => false, |
---|
65 | 'session_name' => 'Strangecode', |
---|
66 | 'session_use_cookies' => true, |
---|
67 | |
---|
68 | // Use database? |
---|
69 | 'enable_db' => false, |
---|
70 | |
---|
71 | // Use db-based sessions? |
---|
72 | 'enable_db_session_handler' => false, |
---|
73 | |
---|
74 | // DB passwords should be set as apache environment variables in httpd.conf, readable only by root. |
---|
75 | 'db_server' => 'localhost', |
---|
76 | 'db_name' => null, |
---|
77 | 'db_user' => null, |
---|
78 | 'db_pass' => null, |
---|
79 | |
---|
80 | // Database debugging. |
---|
81 | 'db_always_debug' => false, // TRUE = display all SQL queries. |
---|
82 | 'db_debug' => false, // TRUE = display db errors. |
---|
83 | 'db_die_on_failure' => false, // TRUE = script stops on db error. |
---|
84 | |
---|
85 | // For classes that require db tables, do we check that a table exists and create if missing? |
---|
86 | 'db_create_tables' => true, |
---|
87 | |
---|
88 | // The level of error reporting. Don't set this to 0 to suppress messages, instead use display_errors to control display. |
---|
89 | 'error_reporting' => E_ALL, |
---|
90 | |
---|
91 | // Don't display errors by default; it is preferable to log them to a file. |
---|
92 | 'display_errors' => false, |
---|
93 | |
---|
94 | // Directory in which to store log files. |
---|
95 | 'log_directory' => '', |
---|
96 | |
---|
97 | // PHP error log. |
---|
98 | 'php_error_log' => 'php_error_log', |
---|
99 | |
---|
100 | // General application log. |
---|
101 | 'log_filename' => 'app_error_log', |
---|
102 | |
---|
103 | // Logging priority can be any of the following, or false to deactivate: |
---|
104 | // LOG_EMERG system is unusable |
---|
105 | // LOG_ALERT action must be taken immediately |
---|
106 | // LOG_CRIT critical conditions |
---|
107 | // LOG_ERR error conditions |
---|
108 | // LOG_WARNING warning conditions |
---|
109 | // LOG_NOTICE normal, but significant, condition |
---|
110 | // LOG_INFO informational message |
---|
111 | // LOG_DEBUG debug-level message |
---|
112 | 'log_file_priority' => false, |
---|
113 | 'log_email_priority' => false, |
---|
114 | 'log_sms_priority' => false, |
---|
115 | 'log_screen_priority' => false, |
---|
116 | |
---|
117 | // Email address to receive log event emails. |
---|
118 | 'log_to_email_address' => null, |
---|
119 | |
---|
120 | // SMS Email address to receive log event SMS messages. |
---|
121 | 'log_to_sms_address' => null, |
---|
122 | |
---|
123 | // A key for calculating simple cryptographic signatures. Set using as an environment variables in the httpd.conf with 'SetEnv SIGNING_KEY <key>'. |
---|
124 | 'signing_key' => 'aae6abd6209d82a691a9f96384a7634a', |
---|
125 | ); |
---|
126 | |
---|
127 | /** |
---|
128 | * This method enforces the singleton pattern for this class. Only one application is running at a time. |
---|
129 | * |
---|
130 | * @return object Reference to the global SessionCache object. |
---|
131 | * @access public |
---|
132 | * @static |
---|
133 | */ |
---|
134 | function &getInstance($app=null) |
---|
135 | { |
---|
136 | static $instance = null; |
---|
137 | |
---|
138 | if ($instance === null) { |
---|
139 | $instance = new App($app); |
---|
140 | } |
---|
141 | |
---|
142 | return $instance; |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | * Constructor. |
---|
147 | */ |
---|
148 | function App($app=null) |
---|
149 | { |
---|
150 | if (isset($app)) { |
---|
151 | $this->app .= $app; |
---|
152 | } |
---|
153 | |
---|
154 | // Initialize default parameters. |
---|
155 | $this->_params = array_merge($this->_params, $this->_param_defaults); |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * Set (or overwrite existing) parameters by passing an array of new parameters. |
---|
160 | * |
---|
161 | * @access public |
---|
162 | * @param array $param Array of parameters (key => val pairs). |
---|
163 | */ |
---|
164 | function setParam($param=null) |
---|
165 | { |
---|
166 | if (!isset($this) || !is_a($this, 'App') && !is_subclass_of($this, 'App')) { |
---|
167 | $this =& App::getInstance(); |
---|
168 | } |
---|
169 | |
---|
170 | if (isset($param) && is_array($param)) { |
---|
171 | // Merge new parameters with old overriding only those passed. |
---|
172 | $this->_params = array_merge($this->_params, $param); |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | * Return the value of a parameter. |
---|
178 | * |
---|
179 | * @access public |
---|
180 | * @param string $param The key of the parameter to return. |
---|
181 | * @return mixed Parameter value, or null if not existing. |
---|
182 | */ |
---|
183 | function &getParam($param=null) |
---|
184 | { |
---|
185 | if (!isset($this) || !is_a($this, 'App') && !is_subclass_of($this, 'App')) { |
---|
186 | $this =& App::getInstance(); |
---|
187 | } |
---|
188 | |
---|
189 | if ($param === null) { |
---|
190 | return $this->_params; |
---|
191 | } else if (isset($this->_params[$param])) { |
---|
192 | return $this->_params[$param]; |
---|
193 | } else { |
---|
194 | trigger_error(sprintf('Parameter is not set: %s', $param), E_USER_NOTICE); |
---|
195 | return null; |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * Begin running this application. |
---|
201 | * |
---|
202 | * @access public |
---|
203 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
204 | * @since 15 Jul 2005 00:32:21 |
---|
205 | */ |
---|
206 | function start() |
---|
207 | { |
---|
208 | if ($this->running) { |
---|
209 | return false; |
---|
210 | } |
---|
211 | |
---|
212 | // Error reporting. |
---|
213 | ini_set('error_reporting', $this->getParam('error_reporting')); |
---|
214 | ini_set('display_errors', $this->getParam('display_errors')); |
---|
215 | ini_set('log_errors', true); |
---|
216 | if (is_dir($this->getParam('log_directory')) && is_writable($this->getParam('log_directory'))) { |
---|
217 | ini_set('error_log', $this->getParam('log_directory') . '/' . $this->getParam('php_error_log')); |
---|
218 | } |
---|
219 | |
---|
220 | |
---|
221 | /** |
---|
222 | * 1. Start Database. |
---|
223 | */ |
---|
224 | |
---|
225 | if (true === $this->getParam('enable_db')) { |
---|
226 | |
---|
227 | // DB connection parameters taken from environment variables in the httpd.conf file, readable only by root. |
---|
228 | if (!empty($_SERVER['DB_SERVER'])) { |
---|
229 | $this->setParam(array('db_server' => $_SERVER['DB_SERVER'])); |
---|
230 | } |
---|
231 | if (!empty($_SERVER['DB_NAME'])) { |
---|
232 | $this->setParam(array('db_name' => $_SERVER['DB_NAME'])); |
---|
233 | } |
---|
234 | if (!empty($_SERVER['DB_USER'])) { |
---|
235 | $this->setParam(array('db_user' => $_SERVER['DB_USER'])); |
---|
236 | } |
---|
237 | if (!empty($_SERVER['DB_PASS'])) { |
---|
238 | $this->setParam(array('db_pass' => $_SERVER['DB_PASS'])); |
---|
239 | } |
---|
240 | |
---|
241 | // The only instance of the DB object. |
---|
242 | require_once dirname(__FILE__) . '/DB.inc.php'; |
---|
243 | |
---|
244 | $this->db =& DB::getInstance(); |
---|
245 | |
---|
246 | $this->db->setParam(array( |
---|
247 | 'db_server' => $this->getParam('db_server'), |
---|
248 | 'db_name' => $this->getParam('db_name'), |
---|
249 | 'db_user' => $this->getParam('db_user'), |
---|
250 | 'db_pass' => $this->getParam('db_pass'), |
---|
251 | 'db_always_debug' => $this->getParam('db_always_debug'), |
---|
252 | 'db_debug' => $this->getParam('db_debug'), |
---|
253 | 'db_die_on_failure' => $this->getParam('db_die_on_failure'), |
---|
254 | )); |
---|
255 | |
---|
256 | // Connect to database. |
---|
257 | $this->db->connect(); |
---|
258 | } |
---|
259 | |
---|
260 | |
---|
261 | /** |
---|
262 | * 2. Start PHP session. |
---|
263 | */ |
---|
264 | |
---|
265 | // Skip session for some user agents. |
---|
266 | if (preg_match('/Atomz|ApacheBench|Wget/i', getenv('HTTP_USER_AGENT'))) { |
---|
267 | $this->setParam(array('enable_session' => false)); |
---|
268 | } |
---|
269 | |
---|
270 | if (true === $this->getParam('enable_session')) { |
---|
271 | |
---|
272 | // Set the session ID to one provided in GET/POST. This is necessary for linking |
---|
273 | // between domains and keeping the same session. |
---|
274 | if ($ses = getFormData($this->getParam('session_name'), false)) { |
---|
275 | session_id($ses); |
---|
276 | } |
---|
277 | |
---|
278 | if (true === $this->getParam('enable_db_session_handler') && true === $this->getParam('enable_db')) { |
---|
279 | // Database session handling. |
---|
280 | require_once dirname(__FILE__) . '/DBSessionHandler.inc.php'; |
---|
281 | $db_save_handler = new DBSessionHandler($this->db, array( |
---|
282 | 'db_table' => 'session_tbl', |
---|
283 | 'create_table' => $this->getParam('db_create_tables'), |
---|
284 | )); |
---|
285 | } |
---|
286 | |
---|
287 | // Session parameters. |
---|
288 | ini_set('session.use_cookies', $this->getParam('session_use_cookies')); |
---|
289 | ini_set('session.use_trans_sid', false); |
---|
290 | ini_set('session.entropy_file', '/dev/urandom'); |
---|
291 | ini_set('session.entropy_length', '512'); |
---|
292 | session_name($this->getParam('session_name')); |
---|
293 | |
---|
294 | // Start the session. |
---|
295 | session_start(); |
---|
296 | |
---|
297 | if (!isset($_SESSION[$this->app])) { |
---|
298 | // Access session data using: $_SESSION['...']. |
---|
299 | // Initialize here _after_ session has started. |
---|
300 | $_SESSION[$this->app] = array( |
---|
301 | 'messages' => array(), |
---|
302 | 'boomerang' => array('url'), |
---|
303 | ); |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | |
---|
308 | /** |
---|
309 | * 3. Misc setup. |
---|
310 | */ |
---|
311 | |
---|
312 | // Script URI will be something like http://host.name.tld (no ending slash) |
---|
313 | // and is used whenever a URL need be used to the current site. |
---|
314 | // Not available on cli scripts obviously. |
---|
315 | if (isset($_SERVER['HTTP_HOST']) && '' != $_SERVER['HTTP_HOST'] && '' == $this->getParam('site_url')) { |
---|
316 | $this->setParam(array('site_url' => sprintf('%s://%s', ('on' == getenv('HTTPS') ? 'https' : 'http'), getenv('HTTP_HOST')))); |
---|
317 | } |
---|
318 | |
---|
319 | // A key for calculating simple cryptographic signatures. |
---|
320 | if (isset($_SERVER['SIGNING_KEY'])) { |
---|
321 | $this->setParam(array('signing_key' => $_SERVER['SIGNING_KEY'])); |
---|
322 | } |
---|
323 | |
---|
324 | // Character set. This should also be printed in the html header template. |
---|
325 | header('Content-type: text/html; charset=' . $this->getParam('character_set')); |
---|
326 | |
---|
327 | $this->running = true; |
---|
328 | } |
---|
329 | |
---|
330 | /** |
---|
331 | * Stop running this application. |
---|
332 | * |
---|
333 | * @access public |
---|
334 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
335 | * @since 17 Jul 2005 17:20:18 |
---|
336 | */ |
---|
337 | function stop() |
---|
338 | { |
---|
339 | session_write_close(); |
---|
340 | restore_include_path(); |
---|
341 | $this->running = false; |
---|
342 | if (true === $this->getParam('enable_db')) { |
---|
343 | $this->db->close(); |
---|
344 | } |
---|
345 | } |
---|
346 | |
---|
347 | |
---|
348 | /** |
---|
349 | * Add a message to the session, which is printed in the header. |
---|
350 | * Just a simple way to print messages to the user. |
---|
351 | * |
---|
352 | * @access public |
---|
353 | * |
---|
354 | * @param string $message The text description of the message. |
---|
355 | * @param int $type The type of message: MSG_NOTICE, |
---|
356 | * MSG_SUCCESS, MSG_WARNING, or MSG_ERR. |
---|
357 | * @param string $file __FILE__. |
---|
358 | * @param string $line __LINE__. |
---|
359 | */ |
---|
360 | function raiseMsg($message, $type=MSG_NOTICE, $file=null, $line=null) |
---|
361 | { |
---|
362 | if (!isset($this) || !is_a($this, 'App') && !is_subclass_of($this, 'App')) { |
---|
363 | $this =& App::getInstance(); |
---|
364 | } |
---|
365 | |
---|
366 | $message = trim($message); |
---|
367 | |
---|
368 | if (!$this->running || '' == $message) { |
---|
369 | $this->logMsg(sprintf('Canceled method call %s, application not running or message is an empty string.', __FUNCTION__), LOG_DEBUG, __FILE__, __LINE__); |
---|
370 | return false; |
---|
371 | } |
---|
372 | |
---|
373 | // Save message in session under unique key to avoid duplicate messages. |
---|
374 | $msg_id = md5($type . $message . $file . $line); |
---|
375 | $_SESSION[$this->app]['messages'][$msg_id] = array( |
---|
376 | 'type' => $type, |
---|
377 | 'message' => $message, |
---|
378 | 'file' => $file, |
---|
379 | 'line' => $line, |
---|
380 | 'count' => (isset($_SESSION[$this->app]['messages'][$msg_id]['count']) ? (1 + $_SESSION[$this->app]['messages'][$msg_id]['count']) : 1) |
---|
381 | ); |
---|
382 | |
---|
383 | if (!in_array($type, array(MSG_NOTICE, MSG_SUCCESS, MSG_WARNING, MSG_ERR))) { |
---|
384 | $this->logMsg(sprintf('Invalid MSG_* type: %s', $type), LOG_DEBUG, __FILE__, __LINE__); |
---|
385 | } |
---|
386 | } |
---|
387 | |
---|
388 | /** |
---|
389 | * Returns an array of the raised messages. |
---|
390 | * |
---|
391 | * @access public |
---|
392 | * @return array List of messags in FIFO order. |
---|
393 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
394 | * @since 21 Dec 2005 13:09:20 |
---|
395 | */ |
---|
396 | function getRaisedMessages() |
---|
397 | { |
---|
398 | if (!isset($this) || !is_a($this, 'App') && !is_subclass_of($this, 'App')) { |
---|
399 | $this =& App::getInstance(); |
---|
400 | } |
---|
401 | |
---|
402 | if (!$this->running) { |
---|
403 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_DEBUG, __FILE__, __LINE__); |
---|
404 | return false; |
---|
405 | } |
---|
406 | |
---|
407 | $output = array(); |
---|
408 | while (isset($_SESSION[$this->app]['messages']) && $message = array_shift($_SESSION[$this->app]['messages'])) { |
---|
409 | $output[] = $message; |
---|
410 | } |
---|
411 | return $output; |
---|
412 | } |
---|
413 | |
---|
414 | /** |
---|
415 | * Resets the message list. |
---|
416 | * |
---|
417 | * @access public |
---|
418 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
419 | * @since 21 Dec 2005 13:21:54 |
---|
420 | */ |
---|
421 | function clearRaisedMessages() |
---|
422 | { |
---|
423 | if (!isset($this) || !is_a($this, 'App') && !is_subclass_of($this, 'App')) { |
---|
424 | $this =& App::getInstance(); |
---|
425 | } |
---|
426 | |
---|
427 | if (!$this->running) { |
---|
428 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_DEBUG, __FILE__, __LINE__); |
---|
429 | return false; |
---|
430 | } |
---|
431 | |
---|
432 | $_SESSION[$this->app]['messages'] = array(); |
---|
433 | } |
---|
434 | |
---|
435 | /** |
---|
436 | * Prints the HTML for displaying raised messages. |
---|
437 | * |
---|
438 | * @access public |
---|
439 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
440 | * @since 15 Jul 2005 01:39:14 |
---|
441 | */ |
---|
442 | function printRaisedMessages() |
---|
443 | { |
---|
444 | if (!isset($this) || !is_a($this, 'App') && !is_subclass_of($this, 'App')) { |
---|
445 | $this =& App::getInstance(); |
---|
446 | } |
---|
447 | |
---|
448 | if (!$this->running) { |
---|
449 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_DEBUG, __FILE__, __LINE__); |
---|
450 | return false; |
---|
451 | } |
---|
452 | |
---|
453 | while (isset($_SESSION[$this->app]['messages']) && $message = array_shift($_SESSION[$this->app]['messages'])) { |
---|
454 | ?><div class="sc-msg"><?php |
---|
455 | if (error_reporting() > 0 && $this->getParam('display_errors')) { |
---|
456 | echo "\n<!-- [" . $message['file'] . ' : ' . $message['line'] . '] -->'; |
---|
457 | } |
---|
458 | switch ($message['type']) { |
---|
459 | case MSG_ERR: |
---|
460 | echo '<div class="sc-msg-error">' . $message['message'] . '</div>'; |
---|
461 | break; |
---|
462 | |
---|
463 | case MSG_WARNING: |
---|
464 | echo '<div class="sc-msg-warning">' . $message['message'] . '</div>'; |
---|
465 | break; |
---|
466 | |
---|
467 | case MSG_SUCCESS: |
---|
468 | echo '<div class="sc-msg-success">' . $message['message'] . '</div>'; |
---|
469 | break; |
---|
470 | |
---|
471 | case MSG_NOTICE: |
---|
472 | default: |
---|
473 | echo '<div class="sc-msg-notice">' . $message['message'] . '</div>'; |
---|
474 | break; |
---|
475 | |
---|
476 | } |
---|
477 | ?></div><?php |
---|
478 | } |
---|
479 | } |
---|
480 | |
---|
481 | /** |
---|
482 | * Logs messages to defined channels: file, email, sms, and screen. Repeated messages are |
---|
483 | * not repeated but printed once with count. |
---|
484 | * |
---|
485 | * @access public |
---|
486 | * @param string $message The text description of the message. |
---|
487 | * @param int $priority The type of message priority (in descending order): |
---|
488 | * LOG_EMERG system is unusable |
---|
489 | * LOG_ALERT action must be taken immediately |
---|
490 | * LOG_CRIT critical conditions |
---|
491 | * LOG_ERR error conditions |
---|
492 | * LOG_WARNING warning conditions |
---|
493 | * LOG_NOTICE normal, but significant, condition |
---|
494 | * LOG_INFO informational message |
---|
495 | * LOG_DEBUG debug-level message |
---|
496 | * @param string $file The file where the log event occurs. |
---|
497 | * @param string $line The line of the file where the log event occurs. |
---|
498 | */ |
---|
499 | function logMsg($message, $priority=LOG_INFO, $file=null, $line=null) |
---|
500 | { |
---|
501 | static $previous_events = array(); |
---|
502 | |
---|
503 | if (!isset($this) || !is_a($this, 'App') && !is_subclass_of($this, 'App')) { |
---|
504 | $this =& App::getInstance(); |
---|
505 | } |
---|
506 | |
---|
507 | // If priority is not specified, assume the worst. |
---|
508 | if (!$this->logPriorityToString($priority)) { |
---|
509 | $this->logMsg(sprintf('Log priority %s not defined. (Message: %s)', $priority, $message), LOG_EMERG, $file, $line); |
---|
510 | $priority = LOG_EMERG; |
---|
511 | } |
---|
512 | |
---|
513 | // If log file is not specified, don't log to a file. |
---|
514 | if (!$this->getParam('log_directory') || !$this->getParam('log_filename') || !is_dir($this->getParam('log_directory')) || !is_writable($this->getParam('log_directory'))) { |
---|
515 | $this->setParam(array('log_file_priority' => false)); |
---|
516 | // We must use trigger_error to report this problem rather than calling App::logMsg, which might lead to an infinite loop. |
---|
517 | trigger_error(sprintf('Codebase error: log directory (%s) not found or writable.', $this->getParam('log_directory')), E_USER_NOTICE); |
---|
518 | } |
---|
519 | |
---|
520 | // Make sure to log in the system's locale. |
---|
521 | $locale = setlocale(LC_TIME, 0); |
---|
522 | setlocale(LC_TIME, 'C'); |
---|
523 | |
---|
524 | // Strip HTML tags except any with more than 7 characters because that's probably not a HTML tag, e.g. <email@address.com>. |
---|
525 | preg_match_all('/(<[^>\s]{7,})[^>]*>/', $message, $strip_tags_allow); |
---|
526 | $message = strip_tags(preg_replace('/\s+/', ' ', $message), (!empty($strip_tags_allow[1]) ? join('> ', $strip_tags_allow[1]) . '>' : null)); |
---|
527 | |
---|
528 | // Store this event under a unique key, counting each time it occurs so that it only gets reported a limited number of times. |
---|
529 | $msg_id = md5($message . $priority . $file . $line); |
---|
530 | if (isset($previous_events[$msg_id])) { |
---|
531 | $previous_events[$msg_id]++; |
---|
532 | if ($previous_events[$msg_id] == 2) { |
---|
533 | $this->logMsg(sprintf('%s (Event repeated %s or more times)', $message, $previous_events[$msg_id]), $priority, $file, $line); |
---|
534 | } |
---|
535 | return false; |
---|
536 | } else { |
---|
537 | $previous_events[$msg_id] = 1; |
---|
538 | } |
---|
539 | |
---|
540 | // Data to be stored for a log event. |
---|
541 | $event = array( |
---|
542 | 'date' => date('Y-m-d H:i:s'), |
---|
543 | 'remote ip' => getRemoteAddr(), |
---|
544 | 'pid' => (substr(PHP_OS, 0, 3) != 'WIN' ? posix_getpid() : ''), |
---|
545 | 'type' => $this->logPriorityToString($priority), |
---|
546 | 'file:line' => "$file : $line", |
---|
547 | 'url' => (isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''), |
---|
548 | 'message' => $message |
---|
549 | ); |
---|
550 | |
---|
551 | // FILE ACTION |
---|
552 | if ($this->getParam('log_file_priority') && $priority <= $this->getParam('log_file_priority')) { |
---|
553 | $event_str = '[' . join('] [', $event) . ']'; |
---|
554 | error_log($event_str . "\n", 3, $this->getParam('log_directory') . '/' . $this->getParam('log_filename')); |
---|
555 | } |
---|
556 | |
---|
557 | // EMAIL ACTION |
---|
558 | if ($this->getParam('log_email_priority') && $priority <= $this->getParam('log_email_priority')) { |
---|
559 | $subject = sprintf('[%s %s] %s', getenv('HTTP_HOST'), $event['type'], $message); |
---|
560 | $email_msg = sprintf("A %s log event occured on %s\n\n", $event['type'], getenv('HTTP_HOST')); |
---|
561 | $headers = "From: codebase@strangecode.com"; |
---|
562 | foreach ($event as $k=>$v) { |
---|
563 | $email_msg .= sprintf("%-11s%s\n", $k, $v); |
---|
564 | } |
---|
565 | mail($this->getParam('log_to_email_address'), $subject, $email_msg, $headers, '-f codebase@strangecode.com'); |
---|
566 | } |
---|
567 | |
---|
568 | // SMS ACTION |
---|
569 | if ($this->getParam('log_sms_priority') && $priority <= $this->getParam('log_sms_priority')) { |
---|
570 | $subject = sprintf('[%s %s]', getenv('HTTP_HOST'), $priority); |
---|
571 | $sms_msg = sprintf('%s [%s:%s]', $event['message'], basename($file), $line); |
---|
572 | $headers = "From: codebase@strangecode.com"; |
---|
573 | mail($this->getParam('log_to_sms_address'), $subject, $sms_msg, $headers, '-f codebase@strangecode.com'); |
---|
574 | } |
---|
575 | |
---|
576 | // SCREEN ACTION |
---|
577 | if ($this->getParam('log_screen_priority') && $priority <= $this->getParam('log_screen_priority')) { |
---|
578 | echo "[{$event['date']}] [{$event['type']}] [{$event['file:line']}] [{$event['message']}]\n"; |
---|
579 | } |
---|
580 | |
---|
581 | // Restore original locale. |
---|
582 | setlocale(LC_TIME, $locale); |
---|
583 | } |
---|
584 | |
---|
585 | /** |
---|
586 | * Returns the string representation of a LOG_* integer constant. |
---|
587 | * |
---|
588 | * @param int $priority The LOG_* integer constant. |
---|
589 | * |
---|
590 | * @return The string representation of $priority. |
---|
591 | */ |
---|
592 | function logPriorityToString ($priority) { |
---|
593 | $priorities = array( |
---|
594 | LOG_EMERG => 'emergency', |
---|
595 | LOG_ALERT => 'alert', |
---|
596 | LOG_CRIT => 'critical', |
---|
597 | LOG_ERR => 'error', |
---|
598 | LOG_WARNING => 'warning', |
---|
599 | LOG_NOTICE => 'notice', |
---|
600 | LOG_INFO => 'info', |
---|
601 | LOG_DEBUG => 'debug' |
---|
602 | ); |
---|
603 | if (isset($priorities[$priority])) { |
---|
604 | return $priorities[$priority]; |
---|
605 | } else { |
---|
606 | return false; |
---|
607 | } |
---|
608 | } |
---|
609 | |
---|
610 | /** |
---|
611 | * Sets which query arguments will be carried persistently between requests. |
---|
612 | * Values in the _carry_queries array will be copied to URLs (via App::url()) and |
---|
613 | * to hidden input values (via printHiddenSession()). |
---|
614 | * |
---|
615 | * @access public |
---|
616 | * @param string $query_key The key of the query argument to save. |
---|
617 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
618 | * @since 14 Nov 2005 19:24:52 |
---|
619 | */ |
---|
620 | function carryQuery($query_key) |
---|
621 | { |
---|
622 | if (!isset($this) || !is_a($this, 'App') && !is_subclass_of($this, 'App')) { |
---|
623 | $this =& App::getInstance(); |
---|
624 | } |
---|
625 | |
---|
626 | // If not already set, and there is a non-empty value provided in the request... |
---|
627 | if (!isset($this->_carry_queries[$query_key]) && getFormData($query_key, false)) { |
---|
628 | // Copy the value of the specified query argument into the _carry_queries array. |
---|
629 | $this->_carry_queries[$query_key] = getFormData($query_key); |
---|
630 | } |
---|
631 | } |
---|
632 | |
---|
633 | /** |
---|
634 | * Outputs a fully qualified URL with a query of all the used (ie: not empty) |
---|
635 | * keys and values, including optional queries. This allows mindless retention |
---|
636 | * of query arguments across page requests. If cookies are not |
---|
637 | * used, the session id will be propogated in the URL. |
---|
638 | * |
---|
639 | * @param string $url The initial url |
---|
640 | * @param mixed $carry_args Additional url arguments to carry in the query, |
---|
641 | * or FALSE to prevent carrying queries. Can be any of the following formats: |
---|
642 | * array('key1', key2', key3') <-- to save these keys if in the form data. |
---|
643 | * array('key1'=>'value', key2'='value') <-- to set keys to default values if not present in form data. |
---|
644 | * false <-- To not carry any queries. If URL already has queries those will be retained. |
---|
645 | * |
---|
646 | * @param mixed $always_include_sid Always add the session id, even if using_trans_sid = true. This is required when |
---|
647 | * URL starts with http, since PHP using_trans_sid doesn't do those and also for |
---|
648 | * header('Location...') redirections. |
---|
649 | * |
---|
650 | * @return string url with attached queries and, if not using cookies, the session id |
---|
651 | */ |
---|
652 | function url($url, $carry_args=null, $always_include_sid=false) |
---|
653 | { |
---|
654 | if (!isset($this) || !is_a($this, 'App') && !is_subclass_of($this, 'App')) { |
---|
655 | $this =& App::getInstance(); |
---|
656 | } |
---|
657 | |
---|
658 | if (!$this->running) { |
---|
659 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_DEBUG, __FILE__, __LINE__); |
---|
660 | return false; |
---|
661 | } |
---|
662 | |
---|
663 | // Get any provided query arguments to include in the final URL. |
---|
664 | // If FALSE is a provided here, DO NOT carry the queries. |
---|
665 | $do_carry_queries = true; |
---|
666 | $one_time_carry_queries = array(); |
---|
667 | if (!is_null($carry_args)) { |
---|
668 | if (is_array($carry_args) && !empty($carry_args)) { |
---|
669 | foreach ($carry_args as $key=>$arg) { |
---|
670 | // Get query from appropriate source. |
---|
671 | if (false === $arg) { |
---|
672 | $do_carry_queries = false; |
---|
673 | } else if (false !== getFormData($arg, false)) { |
---|
674 | $one_time_carry_queries[$arg] = getFormData($arg); // Set arg to form data if available. |
---|
675 | } else if (!is_numeric($key) && '' != $arg) { |
---|
676 | $one_time_carry_queries[$key] = getFormData($key, $arg); // Set to arg to default if specified (overwritten by form data). |
---|
677 | } |
---|
678 | } |
---|
679 | } else if (false !== getFormData($carry_args, false)) { |
---|
680 | $one_time_carry_queries[$carry_args] = getFormData($carry_args); |
---|
681 | } else if (false === $carry_args) { |
---|
682 | $do_carry_queries = false; |
---|
683 | } |
---|
684 | } |
---|
685 | |
---|
686 | // Get the first delimiter that is needed in the url. |
---|
687 | $delim = strpos($url, '?') !== false ? ini_get('arg_separator.output') : '?'; |
---|
688 | |
---|
689 | |
---|
690 | $q = ''; |
---|
691 | if ($do_carry_queries) { |
---|
692 | // Join the global _carry_queries and local one_time_carry_queries. |
---|
693 | $query_args = urlEncodeArray(array_merge($this->_carry_queries, $one_time_carry_queries)); |
---|
694 | foreach ($query_args as $key=>$val) { |
---|
695 | // Check value is set and value does not already exist in the url. |
---|
696 | if (!preg_match('/[?&]' . preg_quote($key) . '=/', $url)) { |
---|
697 | $q .= $delim . $key . '=' . $val; |
---|
698 | $delim = ini_get('arg_separator.output'); |
---|
699 | } |
---|
700 | } |
---|
701 | } |
---|
702 | |
---|
703 | // Include the necessary SID if the following is true: |
---|
704 | // - no cookie in http request OR cookies disabled in App |
---|
705 | // - sessions are enabled |
---|
706 | // - the link stays on our site |
---|
707 | // - transparent SID propogation with session.use_trans_sid is not being used OR url begins with protocol (using_trans_sid has no effect here) |
---|
708 | // OR |
---|
709 | // - we must include the SID because we say so (it's used in a context where cookies will not be effective, ie. moving from http to https) |
---|
710 | // AND |
---|
711 | // - the SID is not already in the query. |
---|
712 | if ( |
---|
713 | ( |
---|
714 | ( |
---|
715 | ( |
---|
716 | !isset($_COOKIE[session_name()]) |
---|
717 | || !$this->getParam('session_use_cookies') |
---|
718 | ) |
---|
719 | && $this->getParam('enable_session') |
---|
720 | && isMyDomain($url) |
---|
721 | && |
---|
722 | ( |
---|
723 | !ini_get('session.use_trans_sid') |
---|
724 | || preg_match('!^(http|https)://!i', $url) |
---|
725 | ) |
---|
726 | ) |
---|
727 | || $always_include_sid |
---|
728 | ) |
---|
729 | && !preg_match('/[?&]' . preg_quote(session_name()) . '=/', $url) |
---|
730 | ) { |
---|
731 | $url .= $q . $delim . session_name() . '=' . session_id(); |
---|
732 | return $url; |
---|
733 | } else { |
---|
734 | $url .= $q; |
---|
735 | return $url; |
---|
736 | } |
---|
737 | } |
---|
738 | |
---|
739 | /** |
---|
740 | * Returns a HTML-friendly URL processed with App::url and & replaced with & |
---|
741 | * |
---|
742 | * @access public |
---|
743 | * @param string $url Input URL to parse. |
---|
744 | * @return string URL with App::url() and htmlentities() applied. |
---|
745 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
746 | * @since 09 Dec 2005 17:58:45 |
---|
747 | */ |
---|
748 | function oHREF($url, $carry_args=null, $always_include_sid=false) |
---|
749 | { |
---|
750 | if (!isset($this) || !is_a($this, 'App') && !is_subclass_of($this, 'App')) { |
---|
751 | $this =& App::getInstance(); |
---|
752 | } |
---|
753 | |
---|
754 | $url = $this->url($url, $carry_args, $always_include_sid); |
---|
755 | |
---|
756 | // Replace any & not followed by an html or unicode entity with it's & equivalent. |
---|
757 | $url = preg_replace('/&(?![\w\d#]{1,10};)/', '&', $url); |
---|
758 | |
---|
759 | return $url; |
---|
760 | } |
---|
761 | |
---|
762 | /** |
---|
763 | * Prints a hidden form element with the PHPSESSID when cookies are not used, as well |
---|
764 | * as hidden form elements for GET_VARS that might be in use. |
---|
765 | * |
---|
766 | * @param mixed $carry_args Additional url arguments to carry in the query, |
---|
767 | * or FALSE to prevent carrying queries. Can be any of the following formats: |
---|
768 | * array('key1', key2', key3') <-- to save these keys if in the form data. |
---|
769 | * array('key1'=>'value', key2'='value') <-- to set keys to default values if not present in form data. |
---|
770 | * false <-- To not carry any queries. If URL already has queries those will be retained. |
---|
771 | */ |
---|
772 | function printHiddenSession($carry_args=null) |
---|
773 | { |
---|
774 | if (!isset($this) || !is_a($this, 'App') && !is_subclass_of($this, 'App')) { |
---|
775 | $this =& App::getInstance(); |
---|
776 | } |
---|
777 | |
---|
778 | if (!$this->running) { |
---|
779 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_DEBUG, __FILE__, __LINE__); |
---|
780 | return false; |
---|
781 | } |
---|
782 | |
---|
783 | // Get any provided query arguments to include in the final hidden form data. |
---|
784 | // If FALSE is a provided here, DO NOT carry the queries. |
---|
785 | $do_carry_queries = true; |
---|
786 | $one_time_carry_queries = array(); |
---|
787 | if (!is_null($carry_args)) { |
---|
788 | if (is_array($carry_args) && !empty($carry_args)) { |
---|
789 | foreach ($carry_args as $key=>$arg) { |
---|
790 | // Get query from appropriate source. |
---|
791 | if (false === $arg) { |
---|
792 | $do_carry_queries = false; |
---|
793 | } else if (false !== getFormData($arg, false)) { |
---|
794 | $one_time_carry_queries[$arg] = getFormData($arg); // Set arg to form data if available. |
---|
795 | } else if (!is_numeric($key) && '' != $arg) { |
---|
796 | $one_time_carry_queries[$key] = getFormData($key, $arg); // Set to arg to default if specified (overwritten by form data). |
---|
797 | } |
---|
798 | } |
---|
799 | } else if (false !== getFormData($carry_args, false)) { |
---|
800 | $one_time_carry_queries[$carry_args] = getFormData($carry_args); |
---|
801 | } else if (false === $carry_args) { |
---|
802 | $do_carry_queries = false; |
---|
803 | } |
---|
804 | } |
---|
805 | |
---|
806 | // For each existing POST value, we create a hidden input to carry it through a form. |
---|
807 | if ($do_carry_queries) { |
---|
808 | // Join the global _carry_queries and local one_time_carry_queries. |
---|
809 | // urlencode is not used here, not for form data! |
---|
810 | $query_args = array_merge($this->_carry_queries, $one_time_carry_queries); |
---|
811 | foreach ($query_args as $key=>$val) { |
---|
812 | echo '<input type="hidden" name="' . $key . '" value="' . $val . '" />'; |
---|
813 | } |
---|
814 | } |
---|
815 | |
---|
816 | // Include the SID if cookies are disabled. |
---|
817 | if (!isset($_COOKIE[session_name()]) && !ini_get('session.use_trans_sid')) { |
---|
818 | echo '<input type="hidden" name="' . session_name() . '" value="' . session_id() . '" />'; |
---|
819 | } |
---|
820 | } |
---|
821 | |
---|
822 | /** |
---|
823 | * Uses an http header to redirect the client to the given $url. If sessions are not used |
---|
824 | * and the session is not already defined in the given $url, the SID is appended as a URI query. |
---|
825 | * As with all header generating functions, make sure this is called before any other output. |
---|
826 | * |
---|
827 | * @param string $url The URL the client will be redirected to. |
---|
828 | * @param mixed $carry_args Additional url arguments to carry in the query, |
---|
829 | * or FALSE to prevent carrying queries. Can be any of the following formats: |
---|
830 | * -array('key1', key2', key3') <-- to save these keys if in the form data. |
---|
831 | * -array('key1'=>'value', key2'='value') <-- to set keys to default values if not present in form data. |
---|
832 | * -false <-- To not carry any queries. If URL already has queries those will be retained. |
---|
833 | * @param bool $always_include_sid Force session id to be added to Location header. |
---|
834 | */ |
---|
835 | function dieURL($url, $carry_args=null, $always_include_sid=false) |
---|
836 | { |
---|
837 | if (!isset($this) || !is_a($this, 'App') && !is_subclass_of($this, 'App')) { |
---|
838 | $this =& App::getInstance(); |
---|
839 | } |
---|
840 | |
---|
841 | if (!$this->running) { |
---|
842 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_DEBUG, __FILE__, __LINE__); |
---|
843 | return false; |
---|
844 | } |
---|
845 | |
---|
846 | if ('' == $url) { |
---|
847 | // If URL is not specified, use the redirect_home_url. |
---|
848 | $url = $this->getParam('redirect_home_url'); |
---|
849 | } |
---|
850 | |
---|
851 | if (preg_match('!^/!', $url)) { |
---|
852 | // If relative URL is given, prepend correct local hostname. |
---|
853 | $scheme = 'on' == getenv('HTTPS') ? 'https' : 'http'; |
---|
854 | $host = getenv('HTTP_HOST'); |
---|
855 | $url = sprintf('%s://%s%s', $scheme, $host, $url); |
---|
856 | } |
---|
857 | |
---|
858 | $url = $this->url($url, $carry_args, $always_include_sid); |
---|
859 | |
---|
860 | header(sprintf('Location: %s', $url)); |
---|
861 | $this->logMsg(sprintf('dieURL: %s', $url), LOG_DEBUG, __FILE__, __LINE__); |
---|
862 | |
---|
863 | // End this application. |
---|
864 | // Recommended, although I'm not sure it's necessary: http://cn2.php.net/session_write_close |
---|
865 | $this->stop(); |
---|
866 | die; |
---|
867 | } |
---|
868 | |
---|
869 | /* |
---|
870 | * Redirects a user by calling App::dieURL(). It will use: |
---|
871 | * 1. the stored boomerang URL, it it exists |
---|
872 | * 2. a specified $default_url, it it exists |
---|
873 | * 3. the referring URL, it it exists. |
---|
874 | * 4. redirect_home_url configuration variable. |
---|
875 | * |
---|
876 | * @access public |
---|
877 | * @param string $id Identifier for this script. |
---|
878 | * @param mixed $carry_args Additional arguments to carry in the URL automatically (see App::oHREF()). |
---|
879 | * @param string $default_url A default URL if there is not a valid specified boomerang URL. |
---|
880 | * @return bool False if the session is not running. No return otherwise. |
---|
881 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
882 | * @since 31 Mar 2006 19:17:00 |
---|
883 | */ |
---|
884 | function dieBoomerangURL($id=null, $carry_args=null, $default_url=null) |
---|
885 | { |
---|
886 | if (!isset($this) || !is_a($this, 'App') && !is_subclass_of($this, 'App')) { |
---|
887 | $this =& App::getInstance(); |
---|
888 | } |
---|
889 | |
---|
890 | if (!$this->running) { |
---|
891 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_DEBUG, __FILE__, __LINE__); |
---|
892 | return false; |
---|
893 | } |
---|
894 | |
---|
895 | // Get URL from stored boomerang. Allow non specific URL if ID not valid. |
---|
896 | if ($this->validBoomerangURL($id, true)) { |
---|
897 | if (isset($id) && isset($_SESSION[$this->app]['boomerang']['url'][$id])) { |
---|
898 | $url = $_SESSION[$this->app]['boomerang']['url'][$id]; |
---|
899 | $this->logMsg(sprintf('dieBoomerangURL(%s) found: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); |
---|
900 | } else { |
---|
901 | $url = end($_SESSION[$this->app]['boomerang']['url']); |
---|
902 | $this->logMsg(sprintf('dieBoomerangURL(%s) using: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); |
---|
903 | } |
---|
904 | // Delete stored boomerang. |
---|
905 | $this->deleteBoomerangURL($id); |
---|
906 | } else if (isset($default_url)) { |
---|
907 | $url = $default_url; |
---|
908 | } else if (!refererIsMe()) { |
---|
909 | // Ensure that the redirecting page is not also the referrer. |
---|
910 | $url = getenv('HTTP_REFERER'); |
---|
911 | $this->logMsg(sprintf('dieBoomerangURL(%s) using referrer: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); |
---|
912 | } else { |
---|
913 | // If URL is not specified, use the redirect_home_url. |
---|
914 | $url = $this->getParam('redirect_home_url'); |
---|
915 | $this->logMsg(sprintf('dieBoomerangURL(%s) not found, using redirect_home_url: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); |
---|
916 | } |
---|
917 | |
---|
918 | // A redirection will never happen immediately twice. |
---|
919 | // Set the time so ensure this doesn't happen. |
---|
920 | $_SESSION[$this->app]['boomerang']['time'] = time(); |
---|
921 | $this->dieURL($url, $carry_args); |
---|
922 | } |
---|
923 | |
---|
924 | /** |
---|
925 | * Set the URL to return to when App::dieBoomerangURL() is called. |
---|
926 | * |
---|
927 | * @param string $url A fully validated URL. |
---|
928 | * @param bool $id An identification tag for this url. |
---|
929 | * FIXME: url garbage collection? |
---|
930 | */ |
---|
931 | function setBoomerangURL($url=null, $id=null) |
---|
932 | { |
---|
933 | if (!isset($this) || !is_a($this, 'App') && !is_subclass_of($this, 'App')) { |
---|
934 | $this =& App::getInstance(); |
---|
935 | } |
---|
936 | |
---|
937 | if (!$this->running) { |
---|
938 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_DEBUG, __FILE__, __LINE__); |
---|
939 | return false; |
---|
940 | } |
---|
941 | // A redirection will never happen immediately after setting the boomerangURL. |
---|
942 | // Set the time so ensure this doesn't happen. See App::validBoomerangURL for more. |
---|
943 | |
---|
944 | if ('' != $url && is_string($url)) { |
---|
945 | // Delete any boomerang request keys in the query string. |
---|
946 | $url = preg_replace('/boomerang=[\w]+/', '', $url); |
---|
947 | |
---|
948 | if (isset($_SESSION[$this->app]['boomerang']['url']) && is_array($_SESSION[$this->app]['boomerang']['url']) && !empty($_SESSION[$this->app]['boomerang']['url'])) { |
---|
949 | // If the URL currently exists in the boomerang array, delete. |
---|
950 | while ($existing_key = array_search($url, $_SESSION[$this->app]['boomerang']['url'])) { |
---|
951 | unset($_SESSION[$this->app]['boomerang']['url'][$existing_key]); |
---|
952 | } |
---|
953 | } |
---|
954 | |
---|
955 | if (isset($id)) { |
---|
956 | $_SESSION[$this->app]['boomerang']['url'][$id] = $url; |
---|
957 | } else { |
---|
958 | $_SESSION[$this->app]['boomerang']['url'][] = $url; |
---|
959 | } |
---|
960 | $this->logMsg(sprintf('setBoomerangURL(%s): %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); |
---|
961 | return true; |
---|
962 | } else { |
---|
963 | $this->logMsg(sprintf('setBoomerangURL(%s) is empty!', $id, $url), LOG_NOTICE, __FILE__, __LINE__); |
---|
964 | return false; |
---|
965 | } |
---|
966 | } |
---|
967 | |
---|
968 | /** |
---|
969 | * Return the URL set for the specified $id. |
---|
970 | * |
---|
971 | * @param string $id An identification tag for this url. |
---|
972 | */ |
---|
973 | function getBoomerangURL($id=null) |
---|
974 | { |
---|
975 | if (!isset($this) || !is_a($this, 'App') && !is_subclass_of($this, 'App')) { |
---|
976 | $this =& App::getInstance(); |
---|
977 | } |
---|
978 | |
---|
979 | if (!$this->running) { |
---|
980 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_DEBUG, __FILE__, __LINE__); |
---|
981 | return false; |
---|
982 | } |
---|
983 | |
---|
984 | if (isset($id)) { |
---|
985 | if (isset($_SESSION[$this->app]['boomerang']['url'][$id])) { |
---|
986 | return $_SESSION[$this->app]['boomerang']['url'][$id]; |
---|
987 | } else { |
---|
988 | return ''; |
---|
989 | } |
---|
990 | } else if (is_array($_SESSION[$this->app]['boomerang']['url'])) { |
---|
991 | return end($_SESSION[$this->app]['boomerang']['url']); |
---|
992 | } else { |
---|
993 | return false; |
---|
994 | } |
---|
995 | } |
---|
996 | |
---|
997 | /** |
---|
998 | * Delete the URL set for the specified $id. |
---|
999 | * |
---|
1000 | * @param string $id An identification tag for this url. |
---|
1001 | */ |
---|
1002 | function deleteBoomerangURL($id=null) |
---|
1003 | { |
---|
1004 | if (!isset($this) || !is_a($this, 'App') && !is_subclass_of($this, 'App')) { |
---|
1005 | $this =& App::getInstance(); |
---|
1006 | } |
---|
1007 | |
---|
1008 | if (!$this->running) { |
---|
1009 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_DEBUG, __FILE__, __LINE__); |
---|
1010 | return false; |
---|
1011 | } |
---|
1012 | |
---|
1013 | $this->logMsg(sprintf('deleteBoomerangURL(%s): %s', $id, $this->getBoomerangURL($id)), LOG_DEBUG, __FILE__, __LINE__); |
---|
1014 | |
---|
1015 | if (isset($id) && isset($_SESSION[$this->app]['boomerang']['url'][$id])) { |
---|
1016 | unset($_SESSION[$this->app]['boomerang']['url'][$id]); |
---|
1017 | } else if (is_array($_SESSION[$this->app]['boomerang']['url'])) { |
---|
1018 | array_pop($_SESSION[$this->app]['boomerang']['url']); |
---|
1019 | } |
---|
1020 | } |
---|
1021 | |
---|
1022 | /** |
---|
1023 | * Check if a valid boomerang URL value has been set. A boomerang URL is considered |
---|
1024 | * valid if: 1) it is not empty, 2) it is not the current URL, and 3) has not been accessed within n seconds. |
---|
1025 | * |
---|
1026 | * @return bool True if it is set and valid, false otherwise. |
---|
1027 | */ |
---|
1028 | function validBoomerangURL($id=null, $use_nonspecificboomerang=false) |
---|
1029 | { |
---|
1030 | if (!isset($this) || !is_a($this, 'App') && !is_subclass_of($this, 'App')) { |
---|
1031 | $this =& App::getInstance(); |
---|
1032 | } |
---|
1033 | |
---|
1034 | if (!$this->running) { |
---|
1035 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_DEBUG, __FILE__, __LINE__); |
---|
1036 | return false; |
---|
1037 | } |
---|
1038 | |
---|
1039 | if (!isset($_SESSION[$this->app]['boomerang']['url'])) { |
---|
1040 | $this->logMsg(sprintf('validBoomerangURL(%s) no boomerang URL set.', $id), LOG_DEBUG, __FILE__, __LINE__); |
---|
1041 | return false; |
---|
1042 | } |
---|
1043 | |
---|
1044 | // Time is the timestamp of a boomerangURL redirection, or setting of a boomerangURL. |
---|
1045 | // a boomerang redirection will always occur at least several seconds after the last boomerang redirect |
---|
1046 | // or a boomerang being set. |
---|
1047 | $boomerang_time = isset($_SESSION[$this->app]['boomerang']['time']) ? $_SESSION[$this->app]['boomerang']['time'] : 0; |
---|
1048 | |
---|
1049 | $url = ''; |
---|
1050 | if (isset($id) && isset($_SESSION[$this->app]['boomerang']['url'][$id])) { |
---|
1051 | $url = $_SESSION[$this->app]['boomerang']['url'][$id]; |
---|
1052 | } else if (!isset($id) || $use_nonspecificboomerang) { |
---|
1053 | // Use non specific boomerang if available. |
---|
1054 | $url = end($_SESSION[$this->app]['boomerang']['url']); |
---|
1055 | } |
---|
1056 | |
---|
1057 | $this->logMsg(sprintf('validBoomerangURL(%s) testing: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); |
---|
1058 | |
---|
1059 | if ('' == $url) { |
---|
1060 | $this->logMsg(sprintf('validBoomerangURL(%s) not valid, empty!', $id), LOG_DEBUG, __FILE__, __LINE__); |
---|
1061 | return false; |
---|
1062 | } |
---|
1063 | if ($url == absoluteMe()) { |
---|
1064 | // The URL we are directing to is the current page. |
---|
1065 | $this->logMsg(sprintf('validBoomerangURL(%s) not valid, same as absoluteMe: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); |
---|
1066 | return false; |
---|
1067 | } |
---|
1068 | if ($boomerang_time >= (time() - 2)) { |
---|
1069 | // Last boomerang direction was more than 2 seconds ago. |
---|
1070 | $this->logMsg(sprintf('validBoomerangURL(%s) not valid, boomerang_time too short: %s', $id, time() - $boomerang_time), LOG_DEBUG, __FILE__, __LINE__); |
---|
1071 | return false; |
---|
1072 | } |
---|
1073 | |
---|
1074 | $this->logMsg(sprintf('validBoomerangURL(%s) is valid: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); |
---|
1075 | return true; |
---|
1076 | } |
---|
1077 | |
---|
1078 | /** |
---|
1079 | * Force the user to connect via https (port 443) by redirecting them to |
---|
1080 | * the same page but with https. |
---|
1081 | */ |
---|
1082 | function sslOn() |
---|
1083 | { |
---|
1084 | if (!isset($this) || !is_a($this, 'App') && !is_subclass_of($this, 'App')) { |
---|
1085 | $this =& App::getInstance(); |
---|
1086 | } |
---|
1087 | |
---|
1088 | if (function_exists('apache_get_modules')) { |
---|
1089 | $modules = apache_get_modules(); |
---|
1090 | } else { |
---|
1091 | // It's safe to assume we have mod_ssl if we can't determine otherwise. |
---|
1092 | $modules = array('mod_ssl'); |
---|
1093 | } |
---|
1094 | |
---|
1095 | if ('' == getenv('HTTPS') && $this->getParam('ssl_enabled') && in_array('mod_ssl', $modules)) { |
---|
1096 | $this->raiseMsg(sprintf(_("Secure SSL connection made to %s"), $this->getParam('ssl_domain')), MSG_NOTICE, __FILE__, __LINE__); |
---|
1097 | // Always append session because some browsers do not send cookie when crossing to SSL URL. |
---|
1098 | $this->dieURL('https://' . $this->getParam('ssl_domain') . getenv('REQUEST_URI'), null, true); |
---|
1099 | } |
---|
1100 | } |
---|
1101 | |
---|
1102 | |
---|
1103 | /** |
---|
1104 | * to enforce the user to connect via http (port 80) by redirecting them to |
---|
1105 | * a http version of the current url. |
---|
1106 | */ |
---|
1107 | function sslOff() |
---|
1108 | { |
---|
1109 | if (!isset($this) || !is_a($this, 'App') && !is_subclass_of($this, 'App')) { |
---|
1110 | $this =& App::getInstance(); |
---|
1111 | } |
---|
1112 | |
---|
1113 | if ('' != getenv('HTTPS')) { |
---|
1114 | $this->dieURL('http://' . getenv('HTTP_HOST') . getenv('REQUEST_URI'), null, true); |
---|
1115 | } |
---|
1116 | } |
---|
1117 | |
---|
1118 | |
---|
1119 | } // End. |
---|
1120 | |
---|
1121 | ?> |
---|