00001 <?php 00002 /* SVN FILE: $Id: session.php 7805 2008-10-30 17:30:26Z AD7six $ */ 00003 /** 00004 * Short description for file. 00005 * 00006 * Long description for file 00007 * 00008 * PHP versions 4 and 5 00009 * 00010 * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org) 00011 * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) 00012 * 00013 * Licensed under The MIT License 00014 * Redistributions of files must retain the above copyright notice. 00015 * 00016 * @filesource 00017 * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) 00018 * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project 00019 * @package cake 00020 * @subpackage cake.cake.libs.controller.components 00021 * @since CakePHP(tm) v 0.10.0.1232 00022 * @version $Revision: 7805 $ 00023 * @modifiedby $LastChangedBy: AD7six $ 00024 * @lastmodified $Date: 2008-10-30 13:30:26 -0400 (Thu, 30 Oct 2008) $ 00025 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 00026 */ 00027 if (!class_exists('cakesession')) { 00028 require LIBS . 'session.php'; 00029 } 00030 /** 00031 * Session Component. 00032 * 00033 * Session handling from the controller. 00034 * 00035 * @package cake 00036 * @subpackage cake.cake.libs.controller.components 00037 * 00038 */ 00039 class SessionComponent extends CakeSession { 00040 /** 00041 * Used to determine if methods implementation is used, or bypassed 00042 * 00043 * @var boolean 00044 * @access private 00045 */ 00046 var $__active = true; 00047 /** 00048 * Used to determine if Session has been started 00049 * 00050 * @var boolean 00051 * @access private 00052 */ 00053 var $__started = false; 00054 /** 00055 * Used to determine if request are from an Ajax request 00056 * 00057 * @var boolean 00058 * @access private 00059 */ 00060 var $__bare = 0; 00061 /** 00062 * Class constructor 00063 * 00064 * @param string $base The base path for the Session 00065 */ 00066 function __construct($base = null) { 00067 if (Configure::read('Session.start') === true) { 00068 parent::__construct($base); 00069 } else { 00070 $this->__active = false; 00071 } 00072 } 00073 /** 00074 * Initializes the component, gets a reference to Controller::$param['bare']. 00075 * 00076 * @param object $controller A reference to the controller 00077 * @return void 00078 * @access public 00079 */ 00080 function initialize(&$controller) { 00081 if (isset($controller->params['bare'])) { 00082 $this->__bare = $controller->params['bare']; 00083 } 00084 } 00085 /** 00086 * Startup method. 00087 * 00088 * @param object $controller Instantiating controller 00089 * @return void 00090 * @access public 00091 */ 00092 function startup(&$controller) { 00093 if ($this->__started === false && $this->__active === true) { 00094 $this->__start(); 00095 } 00096 } 00097 /** 00098 * Starts Session on if 'Session.start' is set to false in core.php 00099 * 00100 * @param string $base The base path for the Session 00101 * @return void 00102 * @access public 00103 */ 00104 function activate($base = null) { 00105 if ($this->__active === true) { 00106 return; 00107 } 00108 parent::__construct($base); 00109 $this->__active = true; 00110 } 00111 /** 00112 * Used to write a value to a session key. 00113 * 00114 * In your controller: $this->Session->write('Controller.sessKey', 'session value'); 00115 * 00116 * @param string $name The name of the key your are setting in the session. 00117 * This should be in a Controller.key format for better organizing 00118 * @param string $value The value you want to store in a session. 00119 * @return boolean Success 00120 * @access public 00121 */ 00122 function write($name, $value = null) { 00123 if ($this->__active === true) { 00124 $this->__start(); 00125 if (is_array($name)) { 00126 foreach ($name as $key => $value) { 00127 if (parent::write($key, $value) === false) { 00128 return false; 00129 } 00130 } 00131 return true; 00132 } 00133 if (parent::write($name, $value) === false) { 00134 return false; 00135 } 00136 return true; 00137 } 00138 return false; 00139 } 00140 /** 00141 * Used to read a session values for a key or return values for all keys. 00142 * 00143 * In your controller: $this->Session->read('Controller.sessKey'); 00144 * Calling the method without a param will return all session vars 00145 * 00146 * @param string $name the name of the session key you want to read 00147 * @return mixed value from the session vars 00148 * @access public 00149 */ 00150 function read($name = null) { 00151 if ($this->__active === true) { 00152 $this->__start(); 00153 return parent::read($name); 00154 } 00155 return false; 00156 } 00157 /** 00158 * Used to delete a session variable. 00159 * 00160 * In your controller: $this->Session->del('Controller.sessKey'); 00161 * 00162 * @param string $name the name of the session key you want to delete 00163 * @return boolean true is session variable is set and can be deleted, false is variable was not set. 00164 * @access public 00165 */ 00166 function del($name) { 00167 if ($this->__active === true) { 00168 $this->__start(); 00169 return parent::del($name); 00170 } 00171 return false; 00172 } 00173 /** 00174 * Wrapper for SessionComponent::del(); 00175 * 00176 * In your controller: $this->Session->delete('Controller.sessKey'); 00177 * 00178 * @param string $name the name of the session key you want to delete 00179 * @return boolean true is session variable is set and can be deleted, false is variable was not set. 00180 * @access public 00181 */ 00182 function delete($name) { 00183 if ($this->__active === true) { 00184 $this->__start(); 00185 return $this->del($name); 00186 } 00187 return false; 00188 } 00189 /** 00190 * Used to check if a session variable is set 00191 * 00192 * In your controller: $this->Session->check('Controller.sessKey'); 00193 * 00194 * @param string $name the name of the session key you want to check 00195 * @return boolean true is session variable is set, false if not 00196 * @access public 00197 */ 00198 function check($name) { 00199 if ($this->__active === true) { 00200 $this->__start(); 00201 return parent::check($name); 00202 } 00203 return false; 00204 } 00205 /** 00206 * Used to determine the last error in a session. 00207 * 00208 * In your controller: $this->Session->error(); 00209 * 00210 * @return string Last session error 00211 * @access public 00212 */ 00213 function error() { 00214 if ($this->__active === true) { 00215 $this->__start(); 00216 return parent::error(); 00217 } 00218 return false; 00219 } 00220 /** 00221 * Used to set a session variable that can be used to output messages in the view. 00222 * 00223 * In your controller: $this->Session->setFlash('This has been saved'); 00224 * 00225 * Additional params below can be passed to customize the output, or the Message.[key] 00226 * 00227 * @param string $message Message to be flashed 00228 * @param string $layout Layout to wrap flash message in 00229 * @param array $params Parameters to be sent to layout as view variables 00230 * @param string $key Message key, default is 'flash' 00231 * @access public 00232 */ 00233 function setFlash($message, $layout = 'default', $params = array(), $key = 'flash') { 00234 if ($this->__active === true) { 00235 $this->__start(); 00236 $this->write('Message.' . $key, compact('message', 'layout', 'params')); 00237 } 00238 } 00239 /** 00240 * Used to renew a session id 00241 * 00242 * In your controller: $this->Session->renew(); 00243 * 00244 * @return void 00245 * @access public 00246 */ 00247 function renew() { 00248 if ($this->__active === true) { 00249 $this->__start(); 00250 parent::renew(); 00251 } 00252 } 00253 /** 00254 * Used to check for a valid session. 00255 * 00256 * In your controller: $this->Session->valid(); 00257 * 00258 * @return boolean true is session is valid, false is session is invalid 00259 * @access public 00260 */ 00261 function valid() { 00262 if ($this->__active === true) { 00263 $this->__start(); 00264 return parent::valid(); 00265 } 00266 return false; 00267 } 00268 /** 00269 * Used to destroy sessions 00270 * 00271 * In your controller: $this->Session->destroy(); 00272 * 00273 * @return void 00274 * @access public 00275 */ 00276 function destroy() { 00277 if ($this->__active === true) { 00278 $this->__start(); 00279 parent::destroy(); 00280 } 00281 } 00282 /** 00283 * Returns Session id 00284 * 00285 * If $id is passed in a beforeFilter, the Session will be started 00286 * with the specified id 00287 * 00288 * @param $id string 00289 * @return string 00290 * @access public 00291 */ 00292 function id($id = null) { 00293 return parent::id($id); 00294 } 00295 /** 00296 * Starts Session if SessionComponent is used in Controller::beforeFilter(), 00297 * or is called from 00298 * 00299 * @return boolean 00300 * @access private 00301 */ 00302 function __start() { 00303 if ($this->__started === false) { 00304 if (!$this->id() && parent::start()) { 00305 $this->__started = true; 00306 parent::_checkValid(); 00307 } else { 00308 $this->__started = parent::start(); 00309 } 00310 } 00311 return $this->__started; 00312 } 00313 } 00314 00315 ?>
1.4.7