rpicms

A CMS for the Raspberry Pi
git clone git://archive.git.mtrnord.blog/RpicmsTeam/rpicms.git
Log | Files | Refs | README | LICENSE

SessionCookie.php (5919B)


      1 <?php
      2 /**
      3  * Slim - a micro PHP 5 framework
      4  *
      5  * @author      Josh Lockhart <info@slimframework.com>
      6  * @copyright   2011 Josh Lockhart
      7  * @link        http://www.slimframework.com
      8  * @license     http://www.slimframework.com/license
      9  * @version     2.4.2
     10  * @package     Slim
     11  *
     12  * MIT LICENSE
     13  *
     14  * Permission is hereby granted, free of charge, to any person obtaining
     15  * a copy of this software and associated documentation files (the
     16  * "Software"), to deal in the Software without restriction, including
     17  * without limitation the rights to use, copy, modify, merge, publish,
     18  * distribute, sublicense, and/or sell copies of the Software, and to
     19  * permit persons to whom the Software is furnished to do so, subject to
     20  * the following conditions:
     21  *
     22  * The above copyright notice and this permission notice shall be
     23  * included in all copies or substantial portions of the Software.
     24  *
     25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     29  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     30  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     31  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     32  */
     33 namespace Slim\Middleware;
     34 
     35 /**
     36  * Session Cookie
     37  *
     38  * This class provides an HTTP cookie storage mechanism
     39  * for session data. This class avoids using a PHP session
     40  * and instead serializes/unserializes the $_SESSION global
     41  * variable to/from an HTTP cookie.
     42  *
     43  * You should NEVER store sensitive data in a client-side cookie
     44  * in any format, encrypted (with cookies.encrypt) or not. If you
     45  * need to store sensitive user information in a session, you should
     46  * rely on PHP's native session implementation, or use other middleware
     47  * to store session data in a database or alternative server-side cache.
     48  *
     49  * Because this class stores serialized session data in an HTTP cookie,
     50  * you are inherently limited to 4 Kb. If you attempt to store
     51  * more than this amount, serialization will fail.
     52  *
     53  * @package     Slim
     54  * @author     Josh Lockhart
     55  * @since      1.6.0
     56  */
     57 class SessionCookie extends \Slim\Middleware
     58 {
     59     /**
     60      * @var array
     61      */
     62     protected $settings;
     63 
     64     /**
     65      * Constructor
     66      *
     67      * @param array $settings
     68      */
     69     public function __construct($settings = array())
     70     {
     71         $defaults = array(
     72             'expires' => '20 minutes',
     73             'path' => '/',
     74             'domain' => null,
     75             'secure' => false,
     76             'httponly' => false,
     77             'name' => 'slim_session',
     78         );
     79         $this->settings = array_merge($defaults, $settings);
     80         if (is_string($this->settings['expires'])) {
     81             $this->settings['expires'] = strtotime($this->settings['expires']);
     82         }
     83 
     84         /**
     85          * Session
     86          *
     87          * We must start a native PHP session to initialize the $_SESSION superglobal.
     88          * However, we won't be using the native session store for persistence, so we
     89          * disable the session cookie and cache limiter. We also set the session
     90          * handler to this class instance to avoid PHP's native session file locking.
     91          */
     92         ini_set('session.use_cookies', 0);
     93         session_cache_limiter(false);
     94         session_set_save_handler(
     95             array($this, 'open'),
     96             array($this, 'close'),
     97             array($this, 'read'),
     98             array($this, 'write'),
     99             array($this, 'destroy'),
    100             array($this, 'gc')
    101         );
    102     }
    103 
    104     /**
    105      * Call
    106      */
    107     public function call()
    108     {
    109         $this->loadSession();
    110         $this->next->call();
    111         $this->saveSession();
    112     }
    113 
    114     /**
    115      * Load session
    116      */
    117     protected function loadSession()
    118     {
    119         if (session_id() === '') {
    120             session_start();
    121         }
    122 
    123         $value = $this->app->getCookie($this->settings['name']);
    124 
    125         if ($value) {
    126             try {
    127                 $_SESSION = unserialize($value);
    128             } catch (\Exception $e) {
    129                 $this->app->getLog()->error('Error unserializing session cookie value! ' . $e->getMessage());
    130             }
    131         } else {
    132             $_SESSION = array();
    133         }
    134     }
    135 
    136     /**
    137      * Save session
    138      */
    139     protected function saveSession()
    140     {
    141         $value = serialize($_SESSION);
    142 
    143         if (strlen($value) > 4096) {
    144             $this->app->getLog()->error('WARNING! Slim\Middleware\SessionCookie data size is larger than 4KB. Content save failed.');
    145         } else {
    146             $this->app->setCookie(
    147                 $this->settings['name'],
    148                 $value,
    149                 $this->settings['expires'],
    150                 $this->settings['path'],
    151                 $this->settings['domain'],
    152                 $this->settings['secure'],
    153                 $this->settings['httponly']
    154             );
    155         }
    156         // session_destroy();
    157     }
    158 
    159     /********************************************************************************
    160     * Session Handler
    161     *******************************************************************************/
    162 
    163     /**
    164      * @codeCoverageIgnore
    165      */
    166     public function open($savePath, $sessionName)
    167     {
    168         return true;
    169     }
    170 
    171     /**
    172      * @codeCoverageIgnore
    173      */
    174     public function close()
    175     {
    176         return true;
    177     }
    178 
    179     /**
    180      * @codeCoverageIgnore
    181      */
    182     public function read($id)
    183     {
    184         return '';
    185     }
    186 
    187     /**
    188      * @codeCoverageIgnore
    189      */
    190     public function write($id, $data)
    191     {
    192         return true;
    193     }
    194 
    195     /**
    196      * @codeCoverageIgnore
    197      */
    198     public function destroy($id)
    199     {
    200         return true;
    201     }
    202 
    203     /**
    204      * @codeCoverageIgnore
    205      */
    206     public function gc($maxlifetime)
    207     {
    208         return true;
    209     }
    210 }