session.php

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