Changeset 693 for trunk/lib


Ignore:
Timestamp:
Jul 10, 2019 2:01:24 AM (5 years ago)
Author:
anonymous
Message:

Add PDO class. (And add a few /u unicode flags to preg functions in App - more of those coming in the next commit).

Location:
trunk/lib
Files:
1 added
1 edited

Legend:

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

    r692 r693  
    5454    public $running = false;
    5555
    56     // Instance of database object.
     56    // Instance of database object (from mysql_connect() PHP version < 7).
    5757    public $db;
     58
     59    // Instance of PDO object.
     60    public $pdo;
    5861
    5962    // Instance of database session handler object.
     
    135138        // Use database?
    136139        'enable_db' => false,
     140        'enable_db_pdo' => false,
    137141
    138142        // Use db-based sessions?
     
    397401         */
    398402
    399         if (true === $this->getParam('enable_db')) {
     403        if (true === $this->getParam('enable_db') || true === $this->getParam('enable_db_pdo')) {
    400404
    401405            // DB connection parameters taken from environment variables in the server httpd.conf file (readable only by root)

     
    418422                if (false !== $db_auth_file = stream_resolve_include_path($this->getParam('db_auth_file'))) {
    419423                    if (is_readable($db_auth_file)) {
    420                         $this->setParam(json_decode(file_get_contents($db_auth_file), true));
     424                        $db_auth = json_decode(file_get_contents($db_auth_file), true);
     425                        if (is_null($db_auth)) {
     426                            $app->logMsg(sprintf('Unable to decode json in DB auth file: %s', $db_auth_file), LOG_ERR, __FILE__, __LINE__);
     427                        } else {
     428                            $this->setParam($db_auth);
     429                        }
    421430                    } else {
    422                         $this->logMsg(sprintf('Unable to read DB auth file: %s', $db_auth_file), LOG_NOTICE, __FILE__, __LINE__);
     431                        $this->logMsg(sprintf('Unable to read DB auth file: %s', $db_auth_file), LOG_ERR, __FILE__, __LINE__);
    423432                    }
    424433                } else {
    425                     $this->logMsg(sprintf('DB auth file not found: %s', $this->getParam('db_auth_file')), LOG_NOTICE, __FILE__, __LINE__);
     434                    $this->logMsg(sprintf('DB auth file not found: %s', $this->getParam('db_auth_file')), LOG_ERR, __FILE__, __LINE__);
    426435                }
    427436            }
    428437
    429             // There will ever only be one instance of the DB object, and here is where it is instantiated.
    430             require_once dirname(__FILE__) . '/DB.inc.php';
    431             $this->db =& DB::getInstance();
    432             $this->db->setParam(array(
     438            // If the app wants a DB connection, always set up a PDO object.
     439            require_once dirname(__FILE__) . '/PDO.inc.php';
     440            $this->pdo =& \Strangecode\Codebase\PDO::getInstance();
     441            $this->pdo->setParam(array(
    433442                'db_server' => $this->getParam('db_server'),
    434443                'db_name' => $this->getParam('db_name'),
     
    442451                'collation' => $this->getParam('db_collation'),
    443452            ));
    444 
    445             // Connect to database.
    446             $this->db->connect();
     453            $this->pdo->connect();
     454
     455            // Only create a legacy mysql_* DB object if it is explicitly requested.
     456            if (true === $this->getParam('enable_db')) {
     457                require_once dirname(__FILE__) . '/DB.inc.php';
     458                $this->db =& DB::getInstance();
     459                $this->db->setParam(array(
     460                    'db_server' => $this->getParam('db_server'),
     461                    'db_name' => $this->getParam('db_name'),
     462                    'db_user' => $this->getParam('db_user'),
     463                    'db_pass' => $this->getParam('db_pass'),
     464                    'db_always_debug' => $this->getParam('db_always_debug'),
     465                    'db_debug' => $this->getParam('db_debug'),
     466                    'db_die_on_failure' => $this->getParam('db_die_on_failure'),
     467                    'timezone' => $this->getParam('db_timezone'),
     468                    'character_set' => $this->getParam('db_character_set'),
     469                    'collation' => $this->getParam('db_collation'),
     470                ));
     471                $this->db->connect();
     472            }
    447473        }
    448474
     
    506532
    507533        // To get a safe hostname, remove port and invalid hostname characters.
    508         $safe_http_host = preg_replace('/[^a-z\d.:-]/', '', strtok(getenv('HTTP_HOST'), ':')); // FIXME: strtok shouldn't be used if there is a chance HTTP_HOST may be empty except for the port, e.g., `:80` will return `80`
     534        $safe_http_host = preg_replace('/[^a-z\d.:-]/u', '', strtok(getenv('HTTP_HOST'), ':')); // FIXME: strtok shouldn't be used if there is a chance HTTP_HOST may be empty except for the port, e.g., `:80` will return `80`
    509535        // If strtok() matched a ':' in the previous line, the rest of the string contains the port number (or FALSE)
    510         $safe_http_port = preg_replace('/[^0-9]/', '', strtok(''));
     536        $safe_http_port = preg_replace('/[^0-9]/u', '', strtok(''));
    511537        if ('' != $safe_http_host && '' == $this->getParam('site_hostname')) {
    512538            $this->setParam(array('site_hostname' => $safe_http_host));
     
    11531179                // Avoid indexed-array query params because in a URL array param keys should all match.
    11541180                // I.e, we want to use `array[]=A&array[]=B` instead of `array[0]=A&array[1]=B`.
    1155                 $key = preg_replace('/\[\d+\]$/', '[]', $key);
     1181                $key = preg_replace('/\[\d+\]$/u', '[]', $key);
    11561182                // Check value is set and value does not already exist in the url.
    11571183                if (!preg_match('/[?&]' . preg_quote($key) . '=/', $url)) {
     
    12221248
    12231249        // Replace any & not followed by an html or unicode entity with its &amp; equivalent.
    1224         $url = preg_replace('/&(?![\w\d#]{1,10};)/', '&amp;', $url);
     1250        $url = preg_replace('/&(?![\w\d#]{1,10};)/u', '&amp;', $url);
    12251251
    12261252        return $url;
     
    15381564        if ('' != $url && is_string($url)) {
    15391565            // Delete any boomerang request keys in the query string (along with any trailing delimiters after the deletion).
    1540             $url = preg_replace(array('/([&?])boomerang=[^&?]+[&?]?/', '/[&?]$/'), array('$1', ''), $url);
     1566            $url = preg_replace(array('/([&?])boomerang=[^&?]+[&?]?/u', '/[&?]$/'), array('$1', ''), $url);
    15411567
    15421568            if (isset($_SESSION['_app'][$this->_ns]['boomerang']) && is_array($_SESSION['_app'][$this->_ns]['boomerang']) && !empty($_SESSION['_app'][$this->_ns]['boomerang'])) {
Note: See TracChangeset for help on using the changeset viewer.