Changeset 660 for trunk/lib


Ignore:
Timestamp:
Jan 28, 2019 10:51:50 PM (5 years ago)
Author:
anonymous
Message:

Reduce timezone support to simply setting defaults for user, php, and mysql (all UTC), just to ensure consistency

Location:
trunk/lib
Files:
2 edited

Legend:

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

    r659 r660  
    119119        'sql_time_format' => '%k:%i',
    120120
     121        // Timezone support. No codebase apps currently support switching timezones, but we explicitly set these so they're consistent.
     122        'user_timezone' => 'UTC',
     123        'php_timezone' => 'UTC',
     124        'mysql_timezone' => 'UTC',
     125
    121126        // Use php sessions?
    122127        'enable_session' => false,
     
    370375        }
    371376
    372         // If the 'timezone' parameter is not set, check for a tz cookie.
    373         if (!$this->getParam('timezone') && isset($_COOKIE['tz']) && '' != $_COOKIE['tz']) {
    374             if (preg_match('!^[A-Z_/-]{3,}$!i', $_COOKIE['tz'])) {
    375                 // Valid: tz cookie has a timezone name, like "UTC" or "America/Mexico_City".
    376                 $this->setParam(array('timezone' => $_COOKIE['tz']));
    377             } else if (is_numeric($_COOKIE['tz']) && $_COOKIE['tz'] > -12 && $_COOKIE['tz'] < 14) { // https://en.wikipedia.org/wiki/List_of_UTC_time_offsets#UTC+14:00,_M%E2%80%A0
    378                 // tz cookie has a timezone offset, like "-6" (assume UTC).
    379                 $tz = timezone_name_from_abbr('', $_COOKIE['tz'] * 3600, 0);
    380                 if ($tz && preg_match('!^[A-Z_/-]{3,}$!i', $tz)) {
    381                     // Valid.
    382                     $this->setParam(array('timezone' => $tz));
    383                 } else {
    384                     $this->logMsg(sprintf('Failed to convert UTC offset to timezone: %s', $_COOKIE['tz']), LOG_NOTICE, __FILE__, __LINE__);
    385                 }
    386             } else {
    387                 $this->logMsg(sprintf('Invalid timezone cookie value: %s', $_COOKIE['tz']), LOG_NOTICE, __FILE__, __LINE__);
    388             }
    389         }
    390         if ($this->getParam('timezone')) {
    391             // Set timezone of the user.
    392             if (date_default_timezone_set($this->getParam('timezone'))) {
    393                 $this->logMsg(sprintf('Using timezone: %s', $this->getParam('timezone')), LOG_DEBUG, __FILE__, __LINE__);
    394             } else {
    395                 // Failed: unset the timezone parameter so it isn't used to set the database timezone.
    396                 $this->setParam(array('timezone' => null));
    397                 $this->logMsg(sprintf('Failed to set timezone: %s', $this->getParam('timezone')), LOG_NOTICE, __FILE__, __LINE__);
    398             }
    399         } else {
    400             $this->logMsg(sprintf('Using server timezone: %s', date_default_timezone_get()), LOG_DEBUG, __FILE__, __LINE__);
     377        // Server timezone used internally by PHP.
     378        if ($this->getParam('php_timezone')) {
     379            $this->setTimezone($this->getParam('php_timezone'));
    401380        }
    402381
     
    446425                'db_debug' => $this->getParam('db_debug'),
    447426                'db_die_on_failure' => $this->getParam('db_die_on_failure'),
     427                'timezone' => $this->getParam('mysql_timezone'),
    448428            ));
    449429
     
    17061686        return $ret;
    17071687    }
     1688
     1689    /*
     1690    * Set timezone used internally by PHP.
     1691    *
     1692    * @access   public
     1693    * @param    string  $tz     Timezone, e.g., America/Mexico_City
     1694    * @return
     1695    * @author   Quinn Comendant <quinn@strangecode.com>
     1696    * @since    28 Jan 2019 16:38:38
     1697    */
     1698    public function setTimezone($tz)
     1699    {
     1700        $this->setTimezone();
     1701        // Set timezone for PHP.
     1702        if (date_default_timezone_set($tz)) {
     1703            $this->logMsg(sprintf('Using php timezone: %s', $tz), LOG_DEBUG, __FILE__, __LINE__);
     1704        } else {
     1705            // Failed!
     1706            $this->logMsg(sprintf('Failed to set php timezone: %s', $tz), LOG_WARNING, __FILE__, __LINE__);
     1707        }
     1708    }
    17081709} // End.
  • trunk/lib/DB.inc.php

    r659 r660  
    6969        'zero_date' => '0000-00-00',
    7070        'infinity_date' => '9999-12-31',
     71
     72        // Timezone for MySQL.
     73        'timezone' => 'UTC',
    7174    );
    7275
     
    189192        $this->_connected = true;
    190193
     194        $init_sql = array();
     195
    191196        // Tell MySQL what character set we're using. Available only on MySQL versions > 4.01.01.
    192197        if ('' != $app->getParam('character_set') && isset($this->mysql_character_sets[mb_strtolower($app->getParam('character_set'))])) {
    193             $this->query("/*!40101 SET NAMES '" . $this->mysql_character_sets[mb_strtolower($app->getParam('character_set'))] . "' */");
     198            $init_sql[] = sprintf("SET NAMES '%s'", $this->mysql_character_sets[mb_strtolower($app->getParam('character_set'))]);
    194199        } else {
    195200            $app->logMsg(sprintf('%s is not a known character_set.', $app->getParam('character_set')), LOG_ERR, __FILE__, __LINE__);
     
    201206        }
    202207
    203         // Set timezone.
    204         $tz = date_default_timezone_get();
    205         // Check that PHP is configured with a valid timezone name, e.g., "UTC" or "America/Mexico_City" or "-6:00".
    206         if ($app->getParam('timezone') && $app->getParam('timezone') == $tz && preg_match('!^([A-Z_/-]{3,}|[-+\d:]+)$!i', $tz)) {
     208        // Set MySQL session timezone.
     209        if ($this->getParam('timezone')) {
    207210            // https://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
    208             $this->query(sprintf("SET SESSION time_zone = '%s'", $tz));
     211            $init_sql[] = sprintf("SET time_zone = '%s'", $this->getParam('timezone'));
     212        }
     213
     214        // Run init query, if set.
     215        if (!empty($init_sql)) {
     216            $this->query(join('; ', $init_sql));
    209217        }
    210218
     
    506514    }
    507515
    508 
    509516} // End.
    510517
Note: See TracChangeset for help on using the changeset viewer.