Changeset 581 for trunk/lib


Ignore:
Timestamp:
Feb 27, 2017 2:27:40 PM (7 years ago)
Author:
anonymous
Message:

Improved use of namespaces (returning last-used namespace if namespace not given during getInstance()). Minor fixes: setting default_charset, better variable isset checking, string formatting, boomerang variable init. Depreciated sslOff().

File:
1 edited

Legend:

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

    r563 r581  
    5757    public $db;
    5858
     59    // Instance of database session handler object.
     60    public $db_session;
     61
    5962    // Array of query arguments will be carried persistently between requests.
    6063    protected $_carry_queries = array();
     
    6366    protected $_raised_msg_counter = array(MSG_NOTICE => 0, MSG_SUCCESS => 0, MSG_WARNING => 0, MSG_ERR => 0);
    6467
    65     // We're running as CLI. Public becuase we must force this as false when testing sessions via CLI.
     68    // We're running as CLI. Public because we must force this as false when testing sessions via CLI.
    6669    public $cli = false;
    6770
     
    201204    public function __construct($namespace='')
    202205    {
    203         // Set namespace of application instance.
    204         $this->_ns = $namespace;
    205 
    206206        // Initialize default parameters.
    207207        $this->_params = array_merge($this->_params, $this->_param_defaults);
     
    227227    {
    228228        if (self::$instance === null) {
    229             // TODO: Yep, having a namespace with one singletone instance is not very useful.
     229            // FIXME: Yep, having a namespace with one singleton instance is not very useful. This is a design flaw with the Codebase.
     230            // We're currently getting instances of App throughout the codebase using `$app =& App::getInstance();`
     231            // with no way to determine what the namespace of the containing application is (e.g., `public` vs. `admin`).
     232            // Option 1: provide the project namespace to all classes that use App, and then instantiate with `$app =& App::getInstance($this->_ns);`.
     233            // In this case the namespace of the App and the namespace of the auxiliary class must match.
     234            // Option 2: may be to clone a specific instance to the "default" instance, so, e.g., `$app =& App::getInstance();`
     235            // refers to the same namespace as `$app =& App::getInstance('admin');`
     236            // Option 3: is to check if there is only one instance, and return it if an unspecified namespace is requested.
     237            // However, in the case when multiple namespaces are in play at the same time, this will fail; unspecified namespaces
     238            // would cause the creation of an additional instance, since there would not be an obvious named instance to return.
    230239            self::$instance = new self($namespace);
     240        }
     241
     242        if ('' != $namespace) {
     243            // We may not be able to request a specific instance, but we can specify a specific namespace.
     244            // We're ignoring all instance requests with a blank namespace, so we use the last given one.
     245            self::$instance->_ns = $namespace;
    231246        }
    232247
     
    322337        // Set character set to use for multi-byte string functions.
    323338        mb_internal_encoding($this->getParam('character_set'));
     339        ini_set('default_charset', $this->getParam('character_set'));
    324340        switch (mb_strtolower($this->getParam('character_set'))) {
    325341        case 'utf-8' :
     
    410426                // Database session handling.
    411427                require_once dirname(__FILE__) . '/DBSessionHandler.inc.php';
    412                 $db_save_handler = new DBSessionHandler($this->db, array(
     428                $this->db_session = new DBSessionHandler($this->db, array(
    413429                    'db_table' => 'session_tbl',
    414430                    'create_table' => $this->getParam('db_create_tables'),
     
    424440                $_SESSION['_app'][$this->_ns] = array(
    425441                    'messages' => array(),
    426                     'boomerang' => array('url' => array()),
     442                    'boomerang' => array(),
    427443                );
    428444            }
     
    460476        if (!$this->cli) {
    461477            if (!headers_sent($h_file, $h_line)) {
    462                 header('Content-type: text/html; charset=' . $this->getParam('character_set'));
     478                header(sprintf('Content-type: text/html; charset=%s', $this->getParam('character_set')));
    463479            } else {
    464480                $this->logMsg(sprintf('Unable to set Content-type; headers already sent (output started in %s : %s)', $h_file, $h_line), LOG_DEBUG, __FILE__, __LINE__);
     
    472488            $codebase_version = trim(file_get_contents($codebase_version_file));
    473489            $this->setParam(array('codebase_version' => $codebase_version));
    474             if (!$this->cli) {
     490            if (!$this->cli && $this->getParam('codebase_version')) {
    475491                if (!headers_sent($h_file, $h_line)) {
    476                     header('X-Codebase-Version: ' . $codebase_version);
     492                    header(sprintf('X-Codebase-Version: %s', $this->getParam('codebase_version')));
    477493                } else {
    478494                    $this->logMsg(sprintf('Unable to set X-Codebase-Version; headers already sent (output started in %s : %s)', $h_file, $h_line), LOG_DEBUG, __FILE__, __LINE__);
     
    489505            if (mb_strpos($site_version_file, '.json') !== false) {
    490506                $version_json = json_decode(trim(file_get_contents($site_version_file)), true);
    491                 $site_version = $version_json['version'];
     507                $site_version = isset($version_json['version']) ? $version_json['version'] : null;
    492508            } else {
    493509                $site_version = trim(file_get_contents($site_version_file));
     
    497513        if (!$this->cli && $this->getParam('site_version')) {
    498514            if (!headers_sent($h_file, $h_line)) {
    499                 header('X-Site-Version: ' . $site_version);
     515                header(sprintf('X-Site-Version: %s', $this->getParam('site_version')));
    500516            } else {
    501517                $this->logMsg(sprintf('Unable to set X-Site-Version; headers already sent (output started in %s : %s)', $h_file, $h_line), LOG_DEBUG, __FILE__, __LINE__);
    502518            }
    503519        }
     520
     521        // Unset environment variables we're done with.
     522        unset($_SERVER['DB_SERVER'], $_SERVER['DB_NAME'], $_SERVER['DB_USER'], $_SERVER['DB_PASS'], $_SERVER['SIGNING_KEY']);
    504523
    505524        $this->running = true;
     
    13931412            return false;
    13941413        }
    1395         // A redirection will never happen immediately after setting the boomerangURL.
    1396         // Set the time so ensure this doesn't happen. See $app->validBoomerangURL for more.
    13971414
    13981415        if ('' != $url && is_string($url)) {
     
    14011418
    14021419            if (isset($_SESSION['_app'][$this->_ns]['boomerang']) && is_array($_SESSION['_app'][$this->_ns]['boomerang']) && !empty($_SESSION['_app'][$this->_ns]['boomerang'])) {
    1403                 // If the ID=>URL pair currently exists in the boomerang array, delete.
     1420                // If the ID already exists in the boomerang array, delete it.
    14041421                foreach (array_keys($_SESSION['_app'][$this->_ns]['boomerang']) as $existing_id) {
     1422                    // $existing_id could be null if existing boomerang URL was set without an ID.
    14051423                    if ($existing_id === $id) {
    1406                         $this->logMsg(sprintf('Found and deleting existing ID=>URL pair: %s=>%s', $id, $url), LOG_DEBUG, __FILE__, __LINE__);
     1424                        $this->logMsg(sprintf('Deleted existing boomerang URL matching ID: %s=>%s', $id, $url), LOG_DEBUG, __FILE__, __LINE__);
    14071425                        unset($_SESSION['_app'][$this->_ns]['boomerang'][$existing_id]);
    14081426                    }
     
    14101428            }
    14111429
     1430            // A redirection will never happen immediately after setting the boomerang URL.
     1431            // Set the time so ensure this doesn't happen. See $app->validBoomerangURL for more.
    14121432            if (isset($id)) {
    14131433                $_SESSION['_app'][$this->_ns]['boomerang'][$id] = array(
     
    14481468                return '';
    14491469            }
    1450         } else if (is_array($_SESSION['_app'][$this->_ns]['boomerang']) && !empty($_SESSION['_app'][$this->_ns]['boomerang'])) {
     1470        } else if (isset($_SESSION['_app'][$this->_ns]['boomerang']) && is_array($_SESSION['_app'][$this->_ns]['boomerang']) && !empty($_SESSION['_app'][$this->_ns]['boomerang'])) {
    14511471            return end($_SESSION['_app'][$this->_ns]['boomerang'])['url'];
    14521472        } else {
     
    15561576
    15571577    /**
    1558      * to enforce the user to connect via http (port 80) by redirecting them to
    1559      * a http version of the current url.
     1578     * This function has changed to do nothing. There is no reason to prefer a non-SSL connection, and doing so may result in a redirect loop.
    15601579     */
    15611580    public function sslOff()
    15621581    {
    1563         if ('' != getenv('HTTPS')) {
    1564             $this->dieURL('http://' . getenv('HTTP_HOST') . getenv('REQUEST_URI'), null, true);
    1565         }
     1582        $app =& App::getInstance();
     1583        $app->logMsg(sprintf('sslOff was called and ignored.', null), LOG_INFO, __FILE__, __LINE__);
    15661584    }
    15671585
Note: See TracChangeset for help on using the changeset viewer.