Changeset 136 for trunk/lib/App.inc.php
- Timestamp:
- Jun 3, 2006 7:47:48 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/App.inc.php
r124 r136 7 7 * 8 8 * @author Quinn Comendant <quinn@strangecode.com> 9 * @version 2. 09 * @version 2.1 10 10 */ 11 11 … … 22 22 class App { 23 23 24 // Name of this application.25 var $ app = '_app_';26 27 // If App::start has run successfully.24 // Namespace of this application instance. 25 var $_ns; 26 27 // If $app->start has run successfully. 28 28 var $running = false; 29 29 … … 34 34 var $_carry_queries = array(); 35 35 36 // Hashof global application parameters.36 // Dictionary of global application parameters. 37 37 var $_params = array(); 38 38 … … 45 45 'site_url' => '', // URL automatically determined by _SERVER['HTTP_HOST'] if not set here. 46 46 47 // The location the user will go if the system doesn't kn ew where else to send them.47 // The location the user will go if the system doesn't know where else to send them. 48 48 'redirect_home_url' => '/', 49 49 50 // SSL URL used when redirecting with App::sslOn().50 // SSL URL used when redirecting with $app->sslOn(). 51 51 'ssl_domain' => null, 52 52 'ssl_enabled' => false, … … 86 86 'db_create_tables' => true, 87 87 88 // The level of error reporting. Don't set this to 0to suppress messages, instead use display_errors to control display.88 // The level of error reporting. Don't change this to suppress messages, instead use display_errors to control display. 89 89 'error_reporting' => E_ALL, 90 90 … … 99 99 100 100 // General application log. 101 'log_filename' => 'app_ error_log',101 'log_filename' => 'app_log', 102 102 103 103 // Logging priority can be any of the following, or false to deactivate: … … 122 122 123 123 // A key for calculating simple cryptographic signatures. Set using as an environment variables in the httpd.conf with 'SetEnv SIGNING_KEY <key>'. 124 // Existing password hashes rely on the same key/salt being used to compare encryptions. 125 // Don't change this unless you know existing hashes or signatures will not be affected! 124 126 'signing_key' => 'aae6abd6209d82a691a9f96384a7634a', 125 127 ); … … 128 130 * This method enforces the singleton pattern for this class. Only one application is running at a time. 129 131 * 130 * @return object Reference to the global SessionCache object. 132 * $param string $namespace Name of this application. 133 * @return object Reference to the global Cache object. 131 134 * @access public 132 135 * @static 133 136 */ 134 function &getInstance($ app=null)137 function &getInstance($namespace='') 135 138 { 136 139 static $instance = null; 137 140 138 141 if ($instance === null) { 139 $instance = new App($ app);142 $instance = new App($namespace); 140 143 } 141 144 … … 146 149 * Constructor. 147 150 */ 148 function App($app=null) 149 { 150 if (isset($app)) { 151 $this->app .= $app; 152 } 151 function App($namespace='') 152 { 153 // Set namespace of application instance. 154 $this->_ns = '_app_' . $namespace; 153 155 154 156 // Initialize default parameters. … … 164 166 function setParam($param=null) 165 167 { 166 if (!isset($_this) || !is_a($_this, 'App') && !is_subclass_of($_this, 'App')) {167 $_this =& App::getInstance();168 }169 170 168 if (isset($param) && is_array($param)) { 171 169 // Merge new parameters with old overriding only those passed. 172 $ _this->_params = array_merge($_this->_params, $param);170 $this->_params = array_merge($this->_params, $param); 173 171 } 174 172 } … … 181 179 * @return mixed Parameter value, or null if not existing. 182 180 */ 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 181 function getParam($param=null) 182 { 189 183 if ($param === null) { 190 return $ _this->_params;191 } else if (isset($ _this->_params[$param])) {192 return $ _this->_params[$param];184 return $this->_params; 185 } else if (isset($this->_params[$param])) { 186 return $this->_params[$param]; 193 187 } else { 194 188 trigger_error(sprintf('Parameter is not set: %s', $param), E_USER_NOTICE); … … 239 233 } 240 234 241 // The only instance of the DB object.235 // There will ever only be one instance of the DB object, and here is where it is instantiated. 242 236 require_once dirname(__FILE__) . '/DB.inc.php'; 243 244 237 $this->db =& DB::getInstance(); 245 246 238 $this->db->setParam(array( 247 239 'db_server' => $this->getParam('db_server'), … … 270 262 if (true === $this->getParam('enable_session')) { 271 263 272 // Set the session ID to one provided in GET/POST. This is necessary for linking273 // between domains and keeping the same session.274 if ($ses = getFormData($this->getParam('session_name'), false)) {275 session_id($ses);276 }277 278 264 if (true === $this->getParam('enable_db_session_handler') && true === $this->getParam('enable_db')) { 279 265 // Database session handling. … … 295 281 session_start(); 296 282 297 if (!isset($_SESSION[$this-> app])) {283 if (!isset($_SESSION[$this->_ns])) { 298 284 // Access session data using: $_SESSION['...']. 299 285 // Initialize here _after_ session has started. 300 $_SESSION[$this-> app] = array(286 $_SESSION[$this->_ns] = array( 301 287 'messages' => array(), 302 288 'boomerang' => array('url'), … … 324 310 // Character set. This should also be printed in the html header template. 325 311 header('Content-type: text/html; charset=' . $this->getParam('character_set')); 312 313 // Set the version of the codebase we're using. 314 $codebase_version_file = dirname(__FILE__) . '/../docs/version.txt'; 315 if (is_readable($codebase_version_file)) { 316 $codebase_version = trim(file_get_contents($codebase_version_file)); 317 header('X-Codebase-Version: ' . $codebase_version); 318 define('CODEBASE_VERSION', $codebase_version); 319 } 326 320 327 321 $this->running = true; … … 360 354 function raiseMsg($message, $type=MSG_NOTICE, $file=null, $line=null) 361 355 { 362 if (!isset($_this) || !is_a($_this, 'App') && !is_subclass_of($_this, 'App')) {363 $_this =& App::getInstance();364 }365 366 356 $message = trim($message); 367 357 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__);358 if (!$this->running || '' == $message) { 359 $this->logMsg(sprintf('Canceled method call %s, application not running or message is an empty string.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); 370 360 return false; 371 361 } … … 373 363 // Save message in session under unique key to avoid duplicate messages. 374 364 $msg_id = md5($type . $message . $file . $line); 375 $_SESSION[$ _this->app]['messages'][$msg_id] = array(365 $_SESSION[$this->_ns]['messages'][$msg_id] = array( 376 366 'type' => $type, 377 367 'message' => $message, 378 368 'file' => $file, 379 369 'line' => $line, 380 'count' => (isset($_SESSION[$ _this->app]['messages'][$msg_id]['count']) ? (1 + $_SESSION[$_this->app]['messages'][$msg_id]['count']) : 1)370 'count' => (isset($_SESSION[$this->_ns]['messages'][$msg_id]['count']) ? (1 + $_SESSION[$this->_ns]['messages'][$msg_id]['count']) : 1) 381 371 ); 382 372 383 373 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__);374 $this->logMsg(sprintf('Invalid MSG_* type: %s', $type), LOG_NOTICE, __FILE__, __LINE__); 385 375 } 386 376 } … … 396 386 function getRaisedMessages() 397 387 { 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; 388 if (!$this->running) { 389 $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); 390 return false; 391 } 392 393 return isset($_SESSION[$this->_ns]['messages']) ? $_SESSION[$this->_ns]['messages'] : array(); 412 394 } 413 395 … … 421 403 function clearRaisedMessages() 422 404 { 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__); 405 if (!$this->running) { 406 $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); 429 407 return false; 430 408 } 431 409 432 $_SESSION[$ _this->app]['messages'] = array();410 $_SESSION[$this->_ns]['messages'] = array(); 433 411 } 434 412 … … 442 420 function printRaisedMessages() 443 421 { 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'])) { 422 if (!$this->running) { 423 $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); 424 return false; 425 } 426 427 $messages = $this->getRaisedMessages(); 428 foreach ($messages as $m) { 454 429 ?><div class="sc-msg"><?php 455 if (error_reporting() > 0 && $ _this->getParam('display_errors')) {456 echo "\n<!-- [" . $m essage['file'] . ' : ' . $message['line'] . '] -->';457 } 458 switch ($m essage['type']) {430 if (error_reporting() > 0 && $this->getParam('display_errors') && isset($m['file']) && isset($m['line'])) { 431 echo "\n<!-- [" . $m['file'] . ' : ' . $m['line'] . '] -->'; 432 } 433 switch ($m['type']) { 459 434 case MSG_ERR: 460 echo '<div class="sc-msg-error">' . $m essage['message'] . '</div>';435 echo '<div class="sc-msg-error">' . $m['message'] . '</div>'; 461 436 break; 462 437 463 438 case MSG_WARNING: 464 echo '<div class="sc-msg-warning">' . $m essage['message'] . '</div>';439 echo '<div class="sc-msg-warning">' . $m['message'] . '</div>'; 465 440 break; 466 441 467 442 case MSG_SUCCESS: 468 echo '<div class="sc-msg-success">' . $m essage['message'] . '</div>';443 echo '<div class="sc-msg-success">' . $m['message'] . '</div>'; 469 444 break; 470 445 471 446 case MSG_NOTICE: 472 447 default: 473 echo '<div class="sc-msg-notice">' . $m essage['message'] . '</div>';448 echo '<div class="sc-msg-notice">' . $m['message'] . '</div>'; 474 449 break; 475 450 … … 477 452 ?></div><?php 478 453 } 454 $this->clearRaisedMessages(); 479 455 } 480 456 … … 501 477 static $previous_events = array(); 502 478 503 if (!isset($_this) || !is_a($_this, 'App') && !is_subclass_of($_this, 'App')) {504 $_this =& App::getInstance();505 }506 507 479 // 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);480 if (!$this->logPriorityToString($priority)) { 481 $this->logMsg(sprintf('Log priority %s not defined. (Message: %s)', $priority, $message), LOG_EMERG, $file, $line); 510 482 $priority = LOG_EMERG; 511 483 } 512 484 513 485 // 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);486 if (!$this->getParam('log_directory') || !$this->getParam('log_filename') || !is_dir($this->getParam('log_directory')) || !is_writable($this->getParam('log_directory'))) { 487 $this->setParam(array('log_file_priority' => false)); 488 // We must use trigger_error to report this problem rather than calling $app->logMsg, which might lead to an infinite loop. 489 trigger_error(sprintf('Codebase error: log directory (%s) not found or writable.', $this->getParam('log_directory')), E_USER_NOTICE); 518 490 } 519 491 … … 531 503 $previous_events[$msg_id]++; 532 504 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);505 $this->logMsg(sprintf('%s (Event repeated %s or more times)', $message, $previous_events[$msg_id]), $priority, $file, $line); 534 506 } 535 507 return false; … … 543 515 'remote ip' => getRemoteAddr(), 544 516 'pid' => (substr(PHP_OS, 0, 3) != 'WIN' ? posix_getpid() : ''), 545 'type' => $ _this->logPriorityToString($priority),517 'type' => $this->logPriorityToString($priority), 546 518 'file:line' => "$file : $line", 547 519 'url' => (isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''), … … 550 522 551 523 // FILE ACTION 552 if ($ _this->getParam('log_file_priority') && $priority <= $_this->getParam('log_file_priority')) {524 if ($this->getParam('log_file_priority') && $priority <= $this->getParam('log_file_priority')) { 553 525 $event_str = '[' . join('] [', $event) . ']'; 554 error_log($event_str . "\n", 3, $ _this->getParam('log_directory') . '/' . $_this->getParam('log_filename'));526 error_log($event_str . "\n", 3, $this->getParam('log_directory') . '/' . $this->getParam('log_filename')); 555 527 } 556 528 557 529 // EMAIL ACTION 558 if ($ _this->getParam('log_email_priority') && $priority <= $_this->getParam('log_email_priority')) {530 if ($this->getParam('log_email_priority') && $priority <= $this->getParam('log_email_priority')) { 559 531 $subject = sprintf('[%s %s] %s', getenv('HTTP_HOST'), $event['type'], $message); 560 532 $email_msg = sprintf("A %s log event occured on %s\n\n", $event['type'], getenv('HTTP_HOST')); … … 563 535 $email_msg .= sprintf("%-11s%s\n", $k, $v); 564 536 } 565 mail($ _this->getParam('log_to_email_address'), $subject, $email_msg, $headers, '-f codebase@strangecode.com');537 mail($this->getParam('log_to_email_address'), $subject, $email_msg, $headers, '-f codebase@strangecode.com'); 566 538 } 567 539 568 540 // SMS ACTION 569 if ($ _this->getParam('log_sms_priority') && $priority <= $_this->getParam('log_sms_priority')) {541 if ($this->getParam('log_sms_priority') && $priority <= $this->getParam('log_sms_priority')) { 570 542 $subject = sprintf('[%s %s]', getenv('HTTP_HOST'), $priority); 571 543 $sms_msg = sprintf('%s [%s:%s]', $event['message'], basename($file), $line); 572 544 $headers = "From: codebase@strangecode.com"; 573 mail($ _this->getParam('log_to_sms_address'), $subject, $sms_msg, $headers, '-f codebase@strangecode.com');545 mail($this->getParam('log_to_sms_address'), $subject, $sms_msg, $headers, '-f codebase@strangecode.com'); 574 546 } 575 547 576 548 // SCREEN ACTION 577 if ($ _this->getParam('log_screen_priority') && $priority <= $_this->getParam('log_screen_priority')) {549 if ($this->getParam('log_screen_priority') && $priority <= $this->getParam('log_screen_priority')) { 578 550 echo "[{$event['date']}] [{$event['type']}] [{$event['file:line']}] [{$event['message']}]\n"; 579 551 } … … 610 582 /** 611 583 * 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()) and584 * Values in the _carry_queries array will be copied to URLs (via $app->url()) and 613 585 * to hidden input values (via printHiddenSession()). 614 586 * … … 620 592 function carryQuery($query_key) 621 593 { 622 if (!isset($_this) || !is_a($_this, 'App') && !is_subclass_of($_this, 'App')) {623 $_this =& App::getInstance();624 }625 626 594 // 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)) {595 if (!isset($this->_carry_queries[$query_key]) && getFormData($query_key, false)) { 628 596 // Copy the value of the specified query argument into the _carry_queries array. 629 $ _this->_carry_queries[$query_key] = getFormData($query_key);597 $this->_carry_queries[$query_key] = getFormData($query_key); 630 598 } 631 599 } … … 652 620 function url($url, $carry_args=null, $always_include_sid=false) 653 621 { 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__); 622 if (!$this->running) { 623 $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); 660 624 return false; 661 625 } … … 691 655 if ($do_carry_queries) { 692 656 // Join the global _carry_queries and local one_time_carry_queries. 693 $query_args = urlEncodeArray(array_merge($ _this->_carry_queries, $one_time_carry_queries));657 $query_args = urlEncodeArray(array_merge($this->_carry_queries, $one_time_carry_queries)); 694 658 foreach ($query_args as $key=>$val) { 695 659 // Check value is set and value does not already exist in the url. … … 715 679 ( 716 680 !isset($_COOKIE[session_name()]) 717 || !$ _this->getParam('session_use_cookies')681 || !$this->getParam('session_use_cookies') 718 682 ) 719 && $ _this->getParam('enable_session')683 && $this->getParam('enable_session') 720 684 && isMyDomain($url) 721 685 && … … 738 702 739 703 /** 740 * Returns a HTML-friendly URL processed with App::url and & replaced with &704 * Returns a HTML-friendly URL processed with $app->url and & replaced with & 741 705 * 742 706 * @access public 743 707 * @param string $url Input URL to parse. 744 * @return string URL with App::url() and htmlentities() applied.708 * @return string URL with $app->url() and htmlentities() applied. 745 709 * @author Quinn Comendant <quinn@strangecode.com> 746 710 * @since 09 Dec 2005 17:58:45 … … 748 712 function oHREF($url, $carry_args=null, $always_include_sid=false) 749 713 { 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); 714 $url = $this->url($url, $carry_args, $always_include_sid); 755 715 756 716 // Replace any & not followed by an html or unicode entity with it's & equivalent. … … 772 732 function printHiddenSession($carry_args=null) 773 733 { 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__); 734 if (!$this->running) { 735 $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); 780 736 return false; 781 737 } … … 808 764 // Join the global _carry_queries and local one_time_carry_queries. 809 765 // urlencode is not used here, not for form data! 810 $query_args = array_merge($ _this->_carry_queries, $one_time_carry_queries);766 $query_args = array_merge($this->_carry_queries, $one_time_carry_queries); 811 767 foreach ($query_args as $key=>$val) { 812 echo '<input type="hidden" name="' . $key . '" value="' . $val . '" />';768 printf('<input type="hidden" name="%s" value="%s" />', $key, $val); 813 769 } 814 770 } … … 816 772 // Include the SID if cookies are disabled. 817 773 if (!isset($_COOKIE[session_name()]) && !ini_get('session.use_trans_sid')) { 818 echo '<input type="hidden" name="' . session_name() . '" value="' . session_id() . '" />';774 printf('<input type="hidden" name="%s" value="%s" />', session_name(), session_id()); 819 775 } 820 776 } … … 829 785 * or FALSE to prevent carrying queries. Can be any of the following formats: 830 786 * -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.787 * -array('key1' => 'value', key2' => 'value') <-- to set keys to default values if not present in form data. 832 788 * -false <-- To not carry any queries. If URL already has queries those will be retained. 833 789 * @param bool $always_include_sid Force session id to be added to Location header. … … 835 791 function dieURL($url, $carry_args=null, $always_include_sid=false) 836 792 { 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__); 793 if (!$this->running) { 794 $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); 843 795 return false; 844 796 } … … 846 798 if ('' == $url) { 847 799 // If URL is not specified, use the redirect_home_url. 848 $url = $ _this->getParam('redirect_home_url');800 $url = $this->getParam('redirect_home_url'); 849 801 } 850 802 … … 856 808 } 857 809 858 $url = $ _this->url($url, $carry_args, $always_include_sid);810 $url = $this->url($url, $carry_args, $always_include_sid); 859 811 860 812 header(sprintf('Location: %s', $url)); 861 $ _this->logMsg(sprintf('dieURL: %s', $url), LOG_DEBUG, __FILE__, __LINE__);813 $this->logMsg(sprintf('dieURL: %s', $url), LOG_DEBUG, __FILE__, __LINE__); 862 814 863 815 // End this application. 864 816 // Recommended, although I'm not sure it's necessary: http://cn2.php.net/session_write_close 865 $ _this->stop();817 $this->stop(); 866 818 die; 867 819 } 868 820 869 821 /* 870 * Redirects a user by calling App::dieURL(). It will use:822 * Redirects a user by calling $app->dieURL(). It will use: 871 823 * 1. the stored boomerang URL, it it exists 872 824 * 2. a specified $default_url, it it exists … … 876 828 * @access public 877 829 * @param string $id Identifier for this script. 878 * @param mixed $carry_args Additional arguments to carry in the URL automatically (see App::oHREF()).830 * @param mixed $carry_args Additional arguments to carry in the URL automatically (see $app->oHREF()). 879 831 * @param string $default_url A default URL if there is not a valid specified boomerang URL. 880 832 * @return bool False if the session is not running. No return otherwise. … … 884 836 function dieBoomerangURL($id=null, $carry_args=null, $default_url=null) 885 837 { 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__); 838 if (!$this->running) { 839 $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); 892 840 return false; 893 841 } 894 842 895 843 // 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__);844 if ($this->validBoomerangURL($id, true)) { 845 if (isset($id) && isset($_SESSION[$this->_ns]['boomerang']['url'][$id])) { 846 $url = $_SESSION[$this->_ns]['boomerang']['url'][$id]; 847 $this->logMsg(sprintf('dieBoomerangURL(%s) found: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); 900 848 } else { 901 $url = end($_SESSION[$ _this->app]['boomerang']['url']);902 $ _this->logMsg(sprintf('dieBoomerangURL(%s) using: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__);849 $url = end($_SESSION[$this->_ns]['boomerang']['url']); 850 $this->logMsg(sprintf('dieBoomerangURL(%s) using: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); 903 851 } 904 852 // Delete stored boomerang. 905 $ _this->deleteBoomerangURL($id);853 $this->deleteBoomerangURL($id); 906 854 } else if (isset($default_url)) { 907 855 $url = $default_url; … … 909 857 // Ensure that the redirecting page is not also the referrer. 910 858 $url = getenv('HTTP_REFERER'); 911 $ _this->logMsg(sprintf('dieBoomerangURL(%s) using referrer: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__);859 $this->logMsg(sprintf('dieBoomerangURL(%s) using referrer: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); 912 860 } else { 913 861 // 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__);862 $url = $this->getParam('redirect_home_url'); 863 $this->logMsg(sprintf('dieBoomerangURL(%s) not found, using redirect_home_url: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); 916 864 } 917 865 918 866 // A redirection will never happen immediately twice. 919 867 // 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.868 $_SESSION[$this->_ns]['boomerang']['time'] = time(); 869 $this->dieURL($url, $carry_args); 870 } 871 872 /** 873 * Set the URL to return to when $app->dieBoomerangURL() is called. 926 874 * 927 875 * @param string $url A fully validated URL. … … 931 879 function setBoomerangURL($url=null, $id=null) 932 880 { 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__); 881 if (!$this->running) { 882 $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); 939 883 return false; 940 884 } 941 885 // 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.886 // Set the time so ensure this doesn't happen. See $app->validBoomerangURL for more. 943 887 944 888 if ('' != $url && is_string($url)) { … … 946 890 $url = preg_replace('/boomerang=[\w]+/', '', $url); 947 891 948 if (isset($_SESSION[$ _this->app]['boomerang']['url']) && is_array($_SESSION[$_this->app]['boomerang']['url']) && !empty($_SESSION[$_this->app]['boomerang']['url'])) {892 if (isset($_SESSION[$this->_ns]['boomerang']['url']) && is_array($_SESSION[$this->_ns]['boomerang']['url']) && !empty($_SESSION[$this->_ns]['boomerang']['url'])) { 949 893 // 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]);894 while ($existing_key = array_search($url, $_SESSION[$this->_ns]['boomerang']['url'])) { 895 unset($_SESSION[$this->_ns]['boomerang']['url'][$existing_key]); 952 896 } 953 897 } 954 898 955 899 if (isset($id)) { 956 $_SESSION[$ _this->app]['boomerang']['url'][$id] = $url;900 $_SESSION[$this->_ns]['boomerang']['url'][$id] = $url; 957 901 } else { 958 $_SESSION[$ _this->app]['boomerang']['url'][] = $url;959 } 960 $ _this->logMsg(sprintf('setBoomerangURL(%s): %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__);902 $_SESSION[$this->_ns]['boomerang']['url'][] = $url; 903 } 904 $this->logMsg(sprintf('setBoomerangURL(%s): %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); 961 905 return true; 962 906 } else { 963 $ _this->logMsg(sprintf('setBoomerangURL(%s) is empty!', $id, $url), LOG_NOTICE, __FILE__, __LINE__);907 $this->logMsg(sprintf('setBoomerangURL(%s) is empty!', $id, $url), LOG_NOTICE, __FILE__, __LINE__); 964 908 return false; 965 909 } … … 973 917 function getBoomerangURL($id=null) 974 918 { 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__); 919 if (!$this->running) { 920 $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); 981 921 return false; 982 922 } 983 923 984 924 if (isset($id)) { 985 if (isset($_SESSION[$ _this->app]['boomerang']['url'][$id])) {986 return $_SESSION[$ _this->app]['boomerang']['url'][$id];925 if (isset($_SESSION[$this->_ns]['boomerang']['url'][$id])) { 926 return $_SESSION[$this->_ns]['boomerang']['url'][$id]; 987 927 } else { 988 928 return ''; 989 929 } 990 } else if (is_array($_SESSION[$ _this->app]['boomerang']['url'])) {991 return end($_SESSION[$ _this->app]['boomerang']['url']);930 } else if (is_array($_SESSION[$this->_ns]['boomerang']['url'])) { 931 return end($_SESSION[$this->_ns]['boomerang']['url']); 992 932 } else { 993 933 return false; … … 1002 942 function deleteBoomerangURL($id=null) 1003 943 { 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']); 944 if (!$this->running) { 945 $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); 946 return false; 947 } 948 949 $this->logMsg(sprintf('deleteBoomerangURL(%s): %s', $id, $this->getBoomerangURL($id)), LOG_DEBUG, __FILE__, __LINE__); 950 951 if (isset($id) && isset($_SESSION[$this->_ns]['boomerang']['url'][$id])) { 952 unset($_SESSION[$this->_ns]['boomerang']['url'][$id]); 953 } else if (is_array($_SESSION[$this->_ns]['boomerang']['url'])) { 954 array_pop($_SESSION[$this->_ns]['boomerang']['url']); 1019 955 } 1020 956 } … … 1028 964 function validBoomerangURL($id=null, $use_nonspecificboomerang=false) 1029 965 { 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__); 966 if (!$this->running) { 967 $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); 968 return false; 969 } 970 971 if (!isset($_SESSION[$this->_ns]['boomerang']['url'])) { 972 $this->logMsg(sprintf('validBoomerangURL(%s) no boomerang URL set.', $id), LOG_DEBUG, __FILE__, __LINE__); 1041 973 return false; 1042 974 } … … 1045 977 // a boomerang redirection will always occur at least several seconds after the last boomerang redirect 1046 978 // or a boomerang being set. 1047 $boomerang_time = isset($_SESSION[$ _this->app]['boomerang']['time']) ? $_SESSION[$_this->app]['boomerang']['time'] : 0;979 $boomerang_time = isset($_SESSION[$this->_ns]['boomerang']['time']) ? $_SESSION[$this->_ns]['boomerang']['time'] : 0; 1048 980 1049 981 $url = ''; 1050 if (isset($id) && isset($_SESSION[$ _this->app]['boomerang']['url'][$id])) {1051 $url = $_SESSION[$ _this->app]['boomerang']['url'][$id];982 if (isset($id) && isset($_SESSION[$this->_ns]['boomerang']['url'][$id])) { 983 $url = $_SESSION[$this->_ns]['boomerang']['url'][$id]; 1052 984 } else if (!isset($id) || $use_nonspecificboomerang) { 1053 985 // 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__);986 $url = end($_SESSION[$this->_ns]['boomerang']['url']); 987 } 988 989 $this->logMsg(sprintf('validBoomerangURL(%s) testing: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); 1058 990 1059 991 if ('' == $url) { 1060 $ _this->logMsg(sprintf('validBoomerangURL(%s) not valid, empty!', $id), LOG_DEBUG, __FILE__, __LINE__);992 $this->logMsg(sprintf('validBoomerangURL(%s) not valid, empty!', $id), LOG_DEBUG, __FILE__, __LINE__); 1061 993 return false; 1062 994 } 1063 995 if ($url == absoluteMe()) { 1064 996 // 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__);997 $this->logMsg(sprintf('validBoomerangURL(%s) not valid, same as absoluteMe: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); 1066 998 return false; 1067 999 } 1068 1000 if ($boomerang_time >= (time() - 2)) { 1069 1001 // 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__);1002 $this->logMsg(sprintf('validBoomerangURL(%s) not valid, boomerang_time too short: %s', $id, time() - $boomerang_time), LOG_DEBUG, __FILE__, __LINE__); 1003 return false; 1004 } 1005 1006 $this->logMsg(sprintf('validBoomerangURL(%s) is valid: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); 1075 1007 return true; 1076 1008 } … … 1082 1014 function sslOn() 1083 1015 { 1084 if (!isset($_this) || !is_a($_this, 'App') && !is_subclass_of($_this, 'App')) {1085 $_this =& App::getInstance();1086 }1087 1088 1016 if (function_exists('apache_get_modules')) { 1089 1017 $modules = apache_get_modules(); … … 1093 1021 } 1094 1022 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__);1023 if ('' == getenv('HTTPS') && $this->getParam('ssl_enabled') && in_array('mod_ssl', $modules)) { 1024 $this->raiseMsg(sprintf(_("Secure SSL connection made to %s"), $this->getParam('ssl_domain')), MSG_NOTICE, __FILE__, __LINE__); 1097 1025 // 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);1026 $this->dieURL('https://' . $this->getParam('ssl_domain') . getenv('REQUEST_URI'), null, true); 1099 1027 } 1100 1028 } … … 1107 1035 function sslOff() 1108 1036 { 1109 if (!isset($this) || !is_a($this, 'App') && !is_subclass_of($this, 'App')) {1110 $this =& App::getInstance();1111 }1112 1113 1037 if ('' != getenv('HTTPS')) { 1114 1038 $this->dieURL('http://' . getenv('HTTP_HOST') . getenv('REQUEST_URI'), null, true);
Note: See TracChangeset
for help on using the changeset viewer.