Changeset 136 for trunk/lib/App.inc.php


Ignore:
Timestamp:
Jun 3, 2006 7:47:48 PM (18 years ago)
Author:
scdev
Message:

Q - Merged branches/2.0singleton into trunk. Completed updating classes to use singleton methods. Implemented tests. Fixed some bugs. Changed some interfaces.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/App.inc.php

    r124 r136  
    77 *
    88 * @author  Quinn Comendant <quinn@strangecode.com>
    9  * @version 2.0
     9 * @version 2.1
    1010 */
    1111
     
    2222class App {
    2323
    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.
    2828    var $running = false;
    2929
     
    3434    var $_carry_queries = array();
    3535
    36     // Hash of global application parameters.
     36    // Dictionary of global application parameters.
    3737    var $_params = array();
    3838
     
    4545        'site_url' => '', // URL automatically determined by _SERVER['HTTP_HOST'] if not set here.
    4646
    47         // The location the user will go if the system doesn't knew where else to send them.
     47        // The location the user will go if the system doesn't know where else to send them.
    4848        'redirect_home_url' => '/',
    4949
    50         // SSL URL used when redirecting with App::sslOn().
     50        // SSL URL used when redirecting with $app->sslOn().
    5151        'ssl_domain' => null,
    5252        'ssl_enabled' => false,
     
    8686        'db_create_tables' => true,
    8787
    88         // The level of error reporting. Don't set this to 0 to 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.
    8989        'error_reporting' => E_ALL,
    9090
     
    9999
    100100        // General application log.
    101         'log_filename' => 'app_error_log',
     101        'log_filename' => 'app_log',
    102102
    103103        // Logging priority can be any of the following, or false to deactivate:
     
    122122
    123123        // 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!
    124126        'signing_key' => 'aae6abd6209d82a691a9f96384a7634a',
    125127    );
     
    128130     * This method enforces the singleton pattern for this class. Only one application is running at a time.
    129131     *
    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.
    131134     * @access  public
    132135     * @static
    133136     */
    134     function &getInstance($app=null)
     137    function &getInstance($namespace='')
    135138    {
    136139        static $instance = null;
    137140
    138141        if ($instance === null) {
    139             $instance = new App($app);
     142            $instance = new App($namespace);
    140143        }
    141144
     
    146149     * Constructor.
    147150     */
    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;
    153155
    154156        // Initialize default parameters.
     
    164166    function setParam($param=null)
    165167    {
    166         if (!isset($_this) || !is_a($_this, 'App') && !is_subclass_of($_this, 'App')) {
    167             $_this =& App::getInstance();
    168         }
    169 
    170168        if (isset($param) && is_array($param)) {
    171169            // 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);
    173171        }
    174172    }
     
    181179     * @return  mixed               Parameter value, or null if not existing.
    182180     */
    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    {
    189183        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];
    193187        } else {
    194188            trigger_error(sprintf('Parameter is not set: %s', $param), E_USER_NOTICE);
     
    239233            }
    240234
    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.
    242236            require_once dirname(__FILE__) . '/DB.inc.php';
    243 
    244237            $this->db =& DB::getInstance();
    245 
    246238            $this->db->setParam(array(
    247239                'db_server' => $this->getParam('db_server'),
     
    270262        if (true === $this->getParam('enable_session')) {
    271263
    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 
    278264            if (true === $this->getParam('enable_db_session_handler') && true === $this->getParam('enable_db')) {
    279265                // Database session handling.
     
    295281            session_start();
    296282
    297             if (!isset($_SESSION[$this->app])) {
     283            if (!isset($_SESSION[$this->_ns])) {
    298284                // Access session data using: $_SESSION['...'].
    299285                // Initialize here _after_ session has started.
    300                 $_SESSION[$this->app] = array(
     286                $_SESSION[$this->_ns] = array(
    301287                    'messages' => array(),
    302288                    'boomerang' => array('url'),
     
    324310        // Character set. This should also be printed in the html header template.
    325311        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        }
    326320
    327321        $this->running = true;
     
    360354    function raiseMsg($message, $type=MSG_NOTICE, $file=null, $line=null)
    361355    {
    362         if (!isset($_this) || !is_a($_this, 'App') && !is_subclass_of($_this, 'App')) {
    363             $_this =& App::getInstance();
    364         }
    365 
    366356        $message = trim($message);
    367357
    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__);
    370360            return false;
    371361        }
     
    373363        // Save message in session under unique key to avoid duplicate messages.
    374364        $msg_id = md5($type . $message . $file . $line);
    375         $_SESSION[$_this->app]['messages'][$msg_id] = array(
     365        $_SESSION[$this->_ns]['messages'][$msg_id] = array(
    376366            'type'    => $type,
    377367            'message' => $message,
    378368            'file'    => $file,
    379369            '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)
    381371        );
    382372
    383373        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__);
    385375        }
    386376    }
     
    396386    function getRaisedMessages()
    397387    {
    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();
    412394    }
    413395   
     
    421403    function clearRaisedMessages()
    422404    {
    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__);
    429407            return false;
    430408        }
    431409       
    432         $_SESSION[$_this->app]['messages'] = array();
     410        $_SESSION[$this->_ns]['messages'] = array();
    433411    }
    434412
     
    442420    function printRaisedMessages()
    443421    {
    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) {
    454429            ?><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']) {
     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']) {
    459434            case MSG_ERR:
    460                 echo '<div class="sc-msg-error">' . $message['message'] . '</div>';
     435                echo '<div class="sc-msg-error">' . $m['message'] . '</div>';
    461436                break;
    462437
    463438            case MSG_WARNING:
    464                 echo '<div class="sc-msg-warning">' . $message['message'] . '</div>';
     439                echo '<div class="sc-msg-warning">' . $m['message'] . '</div>';
    465440                break;
    466441
    467442            case MSG_SUCCESS:
    468                 echo '<div class="sc-msg-success">' . $message['message'] . '</div>';
     443                echo '<div class="sc-msg-success">' . $m['message'] . '</div>';
    469444                break;
    470445
    471446            case MSG_NOTICE:
    472447            default:
    473                 echo '<div class="sc-msg-notice">' . $message['message'] . '</div>';
     448                echo '<div class="sc-msg-notice">' . $m['message'] . '</div>';
    474449                break;
    475450
     
    477452            ?></div><?php
    478453        }
     454        $this->clearRaisedMessages();
    479455    }
    480456
     
    501477        static $previous_events = array();
    502478
    503         if (!isset($_this) || !is_a($_this, 'App') && !is_subclass_of($_this, 'App')) {
    504             $_this =& App::getInstance();
    505         }
    506 
    507479        // 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);
    510482            $priority = LOG_EMERG;
    511483        }
    512484
    513485        // 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);
    518490        }
    519491
     
    531503            $previous_events[$msg_id]++;
    532504            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);
    534506            }
    535507            return false;
     
    543515            'remote ip' => getRemoteAddr(),
    544516            'pid'       => (substr(PHP_OS, 0, 3) != 'WIN' ? posix_getpid() : ''),
    545             'type'      => $_this->logPriorityToString($priority),
     517            'type'      => $this->logPriorityToString($priority),
    546518            'file:line' => "$file : $line",
    547519            'url'       => (isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''),
     
    550522
    551523        // 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')) {
    553525            $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'));
    555527        }
    556528
    557529        // 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')) {
    559531            $subject = sprintf('[%s %s] %s', getenv('HTTP_HOST'), $event['type'], $message);
    560532            $email_msg = sprintf("A %s log event occured on %s\n\n", $event['type'], getenv('HTTP_HOST'));
     
    563535                $email_msg .= sprintf("%-11s%s\n", $k, $v);
    564536            }
    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');
    566538        }
    567539
    568540        // 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')) {
    570542            $subject = sprintf('[%s %s]', getenv('HTTP_HOST'), $priority);
    571543            $sms_msg = sprintf('%s [%s:%s]', $event['message'], basename($file), $line);
    572544            $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');
    574546        }
    575547
    576548        // 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')) {
    578550            echo "[{$event['date']}] [{$event['type']}] [{$event['file:line']}] [{$event['message']}]\n";
    579551        }
     
    610582    /**
    611583     * 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
     584     * Values in the _carry_queries array will be copied to URLs (via $app->url()) and
    613585     * to hidden input values (via printHiddenSession()).
    614586     *
     
    620592    function carryQuery($query_key)
    621593    {
    622         if (!isset($_this) || !is_a($_this, 'App') && !is_subclass_of($_this, 'App')) {
    623             $_this =& App::getInstance();
    624         }
    625 
    626594        // 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)) {
    628596            // 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);
    630598        }
    631599    }
     
    652620    function url($url, $carry_args=null, $always_include_sid=false)
    653621    {
    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__);
    660624            return false;
    661625        }
     
    691655        if ($do_carry_queries) {
    692656            // 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));
    694658            foreach ($query_args as $key=>$val) {
    695659                // Check value is set and value does not already exist in the url.
     
    715679                    (
    716680                        !isset($_COOKIE[session_name()])
    717                         || !$_this->getParam('session_use_cookies')
     681                        || !$this->getParam('session_use_cookies')
    718682                    )
    719                     && $_this->getParam('enable_session')
     683                    && $this->getParam('enable_session')
    720684                    && isMyDomain($url)
    721685                    &&
     
    738702
    739703    /**
    740      * Returns a HTML-friendly URL processed with App::url and & replaced with &amp;
     704     * Returns a HTML-friendly URL processed with $app->url and & replaced with &amp;
    741705     *
    742706     * @access  public
    743707     * @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.
    745709     * @author  Quinn Comendant <quinn@strangecode.com>
    746710     * @since   09 Dec 2005 17:58:45
     
    748712    function oHREF($url, $carry_args=null, $always_include_sid=false)
    749713    {
    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);
    755715
    756716        // Replace any & not followed by an html or unicode entity with it's &amp; equivalent.
     
    772732    function printHiddenSession($carry_args=null)
    773733    {
    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__);
    780736            return false;
    781737        }
     
    808764            // Join the global _carry_queries and local one_time_carry_queries.
    809765            // 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);
    811767            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);
    813769            }
    814770        }
     
    816772        // Include the SID if cookies are disabled.
    817773        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());
    819775        }
    820776    }
     
    829785     *                                          or FALSE to prevent carrying queries. Can be any of the following formats:
    830786     *                                          -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.
    832788     *                                          -false  <-- To not carry any queries. If URL already has queries those will be retained.
    833789     * @param   bool    $always_include_sid     Force session id to be added to Location header.
     
    835791    function dieURL($url, $carry_args=null, $always_include_sid=false)
    836792    {
    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__);
    843795            return false;
    844796        }
     
    846798        if ('' == $url) {
    847799            // If URL is not specified, use the redirect_home_url.
    848             $url = $_this->getParam('redirect_home_url');
     800            $url = $this->getParam('redirect_home_url');
    849801        }
    850802
     
    856808        }
    857809
    858         $url = $_this->url($url, $carry_args, $always_include_sid);
     810        $url = $this->url($url, $carry_args, $always_include_sid);
    859811
    860812        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__);
    862814
    863815        // End this application.
    864816        // Recommended, although I'm not sure it's necessary: http://cn2.php.net/session_write_close
    865         $_this->stop();
     817        $this->stop();
    866818        die;
    867819    }
    868820
    869821    /*
    870     * Redirects a user by calling App::dieURL(). It will use:
     822    * Redirects a user by calling $app->dieURL(). It will use:
    871823    * 1. the stored boomerang URL, it it exists
    872824    * 2. a specified $default_url, it it exists
     
    876828    * @access   public
    877829    * @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()).
    879831    * @param    string  $default_url    A default URL if there is not a valid specified boomerang URL.
    880832    * @return   bool                    False if the session is not running. No return otherwise.
     
    884836    function dieBoomerangURL($id=null, $carry_args=null, $default_url=null)
    885837    {
    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__);
    892840            return false;
    893841        }
    894842
    895843        // 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__);
    900848            } 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__);
    903851            }
    904852            // Delete stored boomerang.
    905             $_this->deleteBoomerangURL($id);
     853            $this->deleteBoomerangURL($id);
    906854        } else if (isset($default_url)) {
    907855            $url = $default_url;
     
    909857            // Ensure that the redirecting page is not also the referrer.
    910858            $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__);
    912860        } else {
    913861            // 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__);
    916864        }
    917865
    918866        // A redirection will never happen immediately twice.
    919867        // 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.
    926874     *
    927875     * @param string  $url  A fully validated URL.
     
    931879    function setBoomerangURL($url=null, $id=null)
    932880    {
    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__);
    939883            return false;
    940884        }
    941885        // 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.
    943887
    944888        if ('' != $url && is_string($url)) {
     
    946890            $url = preg_replace('/boomerang=[\w]+/', '', $url);
    947891
    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'])) {
    949893                // 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]);
    952896                }
    953897            }
    954898
    955899            if (isset($id)) {
    956                 $_SESSION[$_this->app]['boomerang']['url'][$id] = $url;
     900                $_SESSION[$this->_ns]['boomerang']['url'][$id] = $url;
    957901            } 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__);
    961905            return true;
    962906        } 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__);
    964908            return false;
    965909        }
     
    973917    function getBoomerangURL($id=null)
    974918    {
    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__);
    981921            return false;
    982922        }
    983923
    984924        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];
    987927            } else {
    988928                return '';
    989929            }
    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']);
    992932        } else {
    993933            return false;
     
    1002942    function deleteBoomerangURL($id=null)
    1003943    {
    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']);
    1019955        }
    1020956    }
     
    1028964    function validBoomerangURL($id=null, $use_nonspecificboomerang=false)
    1029965    {
    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__);
    1041973            return false;
    1042974        }
     
    1045977        // a boomerang redirection will always occur at least several seconds after the last boomerang redirect
    1046978        // 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;
    1048980
    1049981        $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];
    1052984        } else if (!isset($id) || $use_nonspecificboomerang) {
    1053985            // 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__);
    1058990
    1059991        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__);
    1061993            return false;
    1062994        }
    1063995        if ($url == absoluteMe()) {
    1064996            // 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__);
    1066998            return false;
    1067999        }
    10681000        if ($boomerang_time >= (time() - 2)) {
    10691001            // 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__);
    10751007        return true;
    10761008    }
     
    10821014    function sslOn()
    10831015    {
    1084         if (!isset($_this) || !is_a($_this, 'App') && !is_subclass_of($_this, 'App')) {
    1085             $_this =& App::getInstance();
    1086         }
    1087 
    10881016        if (function_exists('apache_get_modules')) {
    10891017            $modules = apache_get_modules();
     
    10931021        }
    10941022
    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__);
    10971025            // 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);
    10991027        }
    11001028    }
     
    11071035    function sslOff()
    11081036    {
    1109         if (!isset($this) || !is_a($this, 'App') && !is_subclass_of($this, 'App')) {
    1110             $this =& App::getInstance();
    1111         }
    1112 
    11131037        if ('' != getenv('HTTPS')) {
    11141038            $this->dieURL('http://' . getenv('HTTP_HOST') . getenv('REQUEST_URI'), null, true);
Note: See TracChangeset for help on using the changeset viewer.