Overview

Namespaces

  • Simpletools
    • Autoload
    • Config
    • Db
      • Mysql
    • Event
    • Http
    • Mvc
    • Page
    • Store

Classes

  • Simpletools\Autoload\Loader
  • Simpletools\Config\Ini
  • Simpletools\Db\Mysql\Client
  • Simpletools\Db\Mysql\Iterator
  • Simpletools\Db\Mysql\Model
  • Simpletools\Db\Mysql\QueryBuilder
  • Simpletools\Db\Mysql\Result
  • Simpletools\Db\Mysql\Sql
  • Simpletools\Event\Event
  • Simpletools\Http\Ssl
  • Simpletools\Mvc\Common
  • Simpletools\Mvc\Controller
  • Simpletools\Mvc\Model
  • Simpletools\Mvc\Router
  • Simpletools\Mvc\RoutingHook
  • Simpletools\Mvc\View
  • Simpletools\Page\Layout
  • Simpletools\Store\Cookie
  • Simpletools\Store\Flash
  • Simpletools\Store\Session
  • Overview
  • Namespace
  • Class
  1: <?php 
  2: /*
  3:  * Simpletools Framework.
  4:  * Copyright (c) 2009, Marcin Rosinski. (http://www.getsimpletools.com/)
  5:  * All rights reserved.
  6:  * 
  7:  * LICENCE
  8:  *
  9:  * Redistribution and use in source and binary forms, with or without modification, 
 10:  * are permitted provided that the following conditions are met:
 11:  *
 12:  * -    Redistributions of source code must retain the above copyright notice, 
 13:  *      this list of conditions and the following disclaimer.
 14:  * 
 15:  * -    Redistributions in binary form must reproduce the above copyright notice, 
 16:  *      this list of conditions and the following disclaimer in the documentation and/or other 
 17:  *      materials provided with the distribution.
 18:  * 
 19:  * -    Neither the name of the Simpletools nor the names of its contributors may be used to 
 20:  *      endorse or promote products derived from this software without specific prior written permission.
 21:  * 
 22:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
 23:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 24:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 25:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 26:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 27:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
 28:  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
 29:  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 30:  *
 31:  * @framework       Simpletools
 32:  * @packages        Session
 33:  * @description     Session & Cookies Handler
 34:  * @copyright       Copyright (c) 2011 Marcin Rosinski. (http://www.getsimpletools.com/)
 35:  * @license         BSD
 36:  *
 37:  */
 38: 
 39:     namespace Simpletools\Store;
 40: 
 41:     class Session
 42:     {
 43:         private static $default_return = 'exception';
 44:         private static $settings = array(
 45:             'autostart_if_session_cookie_set'           => false,
 46:             'session_auto_start'                        => true
 47:         );
 48:         private static $_sessionStarted                 = false;
 49:         private static $_regenerateSessionIdEverySec    = 600;
 50: 
 51:         public static function register($id,$data)
 52:         {
 53:             self::_autoStart();
 54: 
 55:             if(!isset($_SESSION[$id]))
 56:             {
 57:                 $_SESSION[$id] = $data;
 58:             }
 59:             else
 60:             {
 61:                 return false;
 62:             }
 63:         }
 64: 
 65:         public static function set($id,$data)
 66:         {
 67:             self::_autoStart();
 68: 
 69:             $_SESSION[$id] = $data;
 70:         }
 71: 
 72:         //depracated
 73:         public static function replace($id,$data)
 74:         {
 75:             return self::set($id,$data);
 76:         }
 77: 
 78:         public static function remove($id=false)
 79:         {
 80:             self::_autoStart();
 81: 
 82:             if($id)
 83:             {
 84:                 unset($_SESSION[$id]);
 85:             }
 86:             else
 87:             {
 88:                 self::destroy();
 89:             }
 90:         }
 91: 
 92:         public static function destroy($autoStart=true)
 93:         {
 94:             self::_autoStart();
 95: 
 96:             if(isset($_SESSION)) session_destroy();
 97:             if($autoStart) session_start();
 98:         }
 99: 
100:         public static function get($id='')
101:         {
102:             self::_autoStart();
103: 
104:             if(!$id) return $_SESSION;
105: 
106:             if(isset($_SESSION[$id]))
107:             {
108:                 return $_SESSION[$id];
109:             }
110:             else
111:             {
112:                 if(self::$default_return == 'Exception')
113:                 {
114:                     throw new \Exception('Couldn\'t return id: '.$id.' because it hasn\'t been set',12345);
115:                 }
116:                 else
117:                 {
118:                     return self::$default_return;
119:                 }
120:             }
121:         }
122: 
123:         public static function is($key)
124:         {
125:             self::_autoStart();
126: 
127:             return isset($_SESSION[$key]);
128:         }
129: 
130:         //depracted
131:         public static function load($id='')
132:         {
133:             return self::get($id);
134:         }
135: 
136:         public static function settings(array $options)
137:         {
138:             self::$settings['autostart_if_session_cookie_set']  = isset($options['autostartIfSessionCookieSet']) ? (boolean) $options['autostartIfSessionCookieSet'] : self::$settings['autostart_if_session_cookie_set'];
139: 
140:             self::$settings['session_auto_start']   = isset($options['session_auto_start']) ? (boolean) $options['sessionAutoStart'] : self::$settings['session_auto_start'];
141:             self::$_regenerateSessionIdEverySec     = isset($options['regenerateSessionIdEverySec']) ? (int) $options['regenerateSessionIdEverySec'] : self::$_regenerateSessionIdEverySec;
142: 
143:             /*
144:             if(self::$settings['autostart_if_session_cookie_set'])
145:             {
146:                 if(isset($_COOKIE[session_name()]) && session_id() == '') session_start();
147:             }
148:             */
149: 
150:             self::$default_return = (array_key_exists('defaultReturn',$options)) ? $options['defaultReturn'] : ((array_key_exists('default_return',$options)) ? $options['default_return'] : 'Exception');
151:         }
152: 
153:         protected static function _autoStart()
154:         {
155:             if(self::$_sessionStarted)
156:             {
157:                 return;
158:             }
159: 
160:             if(!self::$_sessionStarted && session_id() == '')
161:             {
162:                 if(self::$settings['session_auto_start']){@session_start();self::$_sessionStarted = true;}
163:                 elseif(self::$settings['autostart_if_session_cookie_set'] && isset($_COOKIE[session_name()]) && session_id() == ''){@session_start();self::$_sessionStarted = true;}
164:                 elseif(!self::$settings['autostart_if_session_cookie_set']){throw new \Exception('Please start session before using \Simpletools\Store\Session or set sessionAutoStart under ::settings() method.',11111);}
165:                 else return;
166:             }
167:             else
168:             {
169:                 self::$_sessionStarted = true;
170:             }
171: 
172:             $now = time();
173:             if(!isset($_SESSION['__regenerateSessionIdEverySec']) OR $_SESSION['__regenerateSessionIdEverySec']<$now)
174:             {
175:                 if(isset($_SESSION['__regenerateSessionIdEverySec']) && $_SESSION['__regenerateSessionIdEverySec']<$now)
176:                 {
177:                     @session_regenerate_id(true);
178:                 }
179: 
180:                 $_SESSION['__regenerateSessionIdEverySec'] = time()+self::$_regenerateSessionIdEverySec;
181:             }
182:         }
183:     }
184: 
185: ?>
API documentation generated by ApiGen