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


Ignore:
Timestamp:
May 3, 2014 3:13:19 PM (10 years ago)
Author:
anonymous
Message:

Added cookie storage to Prefs(). Created App->addCookie method. Improved PHP version checks.

File:
1 edited

Legend:

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

    r476 r477  
    179179        $this->timer = new ScriptTimer();
    180180        $this->timer->start('_app');
     181
     182        // The codebase now requires a minimum PHP version.
     183        $codebase_minimum_php_version = '5.3.0';
     184        if (version_compare(PHP_VERSION, $codebase_minimum_php_version, '<')) {
     185            $this->logMsg(sprintf('Codebase minimum PHP version of %s not satisfied (you have %s). ', $codebase_minimum_php_version, phpversion()), LOG_NOTICE, __FILE__, __LINE__);
     186        }
    181187    }
    182188
     
    608614        preg_match_all('/(<[^>\s]{7,})[^>]*>/', $message, $strip_tags_allow);
    609615        $message = strip_tags(preg_replace('/\s+/', ' ', $message), (!empty($strip_tags_allow[1]) ? join('> ', $strip_tags_allow[1]) . '>' : null));
     616
     617        // Serialize multi-line messages.
     618        $message = preg_replace('/\s+/m', ' ', $message);
    610619
    611620        // Store this event under a unique key, counting each time it occurs so that it only gets reported a limited number of times.
     
    12411250    }
    12421251
    1243 
    12441252    /**
    12451253     * to enforce the user to connect via http (port 80) by redirecting them to
     
    12521260        }
    12531261    }
     1262
     1263    /*
     1264    * Sets a cookie, with error checking and some sane defaults.
     1265    *
     1266    * @access   public
     1267    * @param    string  $name       The name of the cookie.
     1268    * @param    string  $value      The value of the cookie.
     1269    * @param    string  $expire     The time the cookie expires, as a unix timestamp or string value passed to strtotime.
     1270    * @param    string  $path       The path on the server in which the cookie will be available on
     1271    * @param    string  $domain     The domain that the cookie is available to
     1272    * @param    bool    $secure     Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client.
     1273    * @param    bool    $httponly   When TRUE the cookie will be made accessible only through the HTTP protocol (makes cookies unreadable to javascript).
     1274    * @return   bool                True on success, false on error.
     1275    * @author   Quinn Comendant <quinn@strangecode.com>
     1276    * @version  1.0
     1277    * @since    02 May 2014 16:36:34
     1278    */
     1279    public function setCookie($name, $value, $expire='+10 years', $path=null, $domain=null, $secure=null, $httponly=null)
     1280    {
     1281        if (!is_scalar($name)) {
     1282            $this->logMsg(sprintf('Cookie name must be scalar, is not: %s', getDump($name)), LOG_NOTICE, __FILE__, __LINE__);
     1283            return false;
     1284        }
     1285        if (!is_scalar($value)) {
     1286            $this->logMsg(sprintf('Cookie "%s" value must be scalar, is not: %s', $name, getDump($value)), LOG_NOTICE, __FILE__, __LINE__);
     1287            return false;
     1288        }
     1289
     1290        // Defaults.
     1291        $expire = (is_numeric($expire) ? $expire : (is_string($expire) ? strtotime($expire) : $expire));
     1292        $secure = $secure ?: ('' != getenv('HTTPS') && $this->getParam('ssl_enabled'));
     1293        $httponly = $httponly ?: true;
     1294
     1295        // Make sure the expiration date is a valid 32bit integer.
     1296        if (is_int($expire) && $expire > 2147483647) {
     1297            $this->logMsg(sprintf('Cookie "%s" expire time exceeds a 32bit integer (%s)', $key, date('r', $expire)), LOG_NOTICE, __FILE__, __LINE__);
     1298        }
     1299
     1300        // Measure total cookie length and warn if larger than max recommended size of 4093.
     1301        // https://stackoverflow.com/questions/640938/what-is-the-maximum-size-of-a-web-browsers-cookies-key
     1302        // The date the header name include 51 bytes: Set-Cookie: ; expires=Fri, 03-May-2024 00:04:47 GMT
     1303        $cookielen = strlen($name . $value . $path . $domain . ($secure ? '; secure' : '') . ($httponly ? '; httponly' : '')) + 51;
     1304        if ($cookielen > 4093) {
     1305            $this->logMsg(sprintf('Cookie "%s" has a size greater than 4093 bytes (is %s bytes)', $key, $cookielen), LOG_NOTICE, __FILE__, __LINE__);
     1306        }
     1307
     1308        // Ensure PHP version allow use of httponly.
     1309        if (version_compare(PHP_VERSION, '5.2.0', '>=')) {
     1310            $ret = setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
     1311        } else {
     1312            $ret = setcookie($name, $value, $expire, $path, $domain, $secure);
     1313        }
     1314
     1315        if (false === $ret) {
     1316            $this->logMsg(sprintf('Failed to set cookie (%s=%s) probably due to output before headers.', $name, $value), LOG_NOTICE, __FILE__, __LINE__);
     1317        }
     1318        return $ret;
     1319    }
    12541320} // End.
Note: See TracChangeset for help on using the changeset viewer.