2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2011-12-08 15:35:02 +00:00
|
|
|
* SessionComponent. Provides access to Sessions from the Controller layer
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2012-03-13 02:46:07 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2012-03-13 02:46:07 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Controller.Component
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.0.1232
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-03 22:38:52 +00:00
|
|
|
|
2011-01-28 06:30:20 +00:00
|
|
|
App::uses('Component', 'Controller');
|
2010-12-07 04:32:42 +00:00
|
|
|
App::uses('CakeSession', 'Model/Datasource');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-10-16 00:19:22 +00:00
|
|
|
* The CakePHP SessionComponent provides a way to persist client data between
|
|
|
|
* page requests. It acts as a wrapper for the `$_SESSION` as well as providing
|
2011-12-08 15:35:02 +00:00
|
|
|
* convenience methods for several `$_SESSION` related functions.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Controller.Component
|
2011-10-15 16:08:49 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html
|
|
|
|
* @link http://book.cakephp.org/2.0/en/development/sessions.html
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-31 19:34:27 +00:00
|
|
|
class SessionComponent extends Component {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-06-09 21:15:34 +00:00
|
|
|
/**
|
2011-08-16 03:55:08 +00:00
|
|
|
* Get / Set the userAgent
|
2010-06-09 21:15:34 +00:00
|
|
|
*
|
|
|
|
* @param string $userAgent Set the userAgent
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function userAgent($userAgent = null) {
|
2010-07-21 03:35:59 +00:00
|
|
|
return CakeSession::userAgent($userAgent);
|
2010-06-09 21:15:34 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used to write a value to a session key.
|
|
|
|
*
|
|
|
|
* In your controller: $this->Session->write('Controller.sessKey', 'session value');
|
|
|
|
*
|
|
|
|
* @param string $name The name of the key your are setting in the session.
|
|
|
|
* This should be in a Controller.key format for better organizing
|
|
|
|
* @param string $value The value you want to store in a session.
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return boolean Success
|
2011-10-15 16:08:49 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::write
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function write($name, $value = null) {
|
2010-07-21 03:35:59 +00:00
|
|
|
return CakeSession::write($name, $value);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used to read a session values for a key or return values for all keys.
|
|
|
|
*
|
|
|
|
* In your controller: $this->Session->read('Controller.sessKey');
|
|
|
|
* Calling the method without a param will return all session vars
|
|
|
|
*
|
|
|
|
* @param string $name the name of the session key you want to read
|
|
|
|
* @return mixed value from the session vars
|
2011-10-15 16:08:49 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::read
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function read($name = null) {
|
2010-07-21 03:35:59 +00:00
|
|
|
return CakeSession::read($name);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Wrapper for SessionComponent::del();
|
|
|
|
*
|
|
|
|
* In your controller: $this->Session->delete('Controller.sessKey');
|
|
|
|
*
|
|
|
|
* @param string $name the name of the session key you want to delete
|
|
|
|
* @return boolean true is session variable is set and can be deleted, false is variable was not set.
|
2011-10-15 16:08:49 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::delete
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function delete($name) {
|
2010-07-21 03:35:59 +00:00
|
|
|
return CakeSession::delete($name);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used to check if a session variable is set
|
|
|
|
*
|
|
|
|
* In your controller: $this->Session->check('Controller.sessKey');
|
|
|
|
*
|
|
|
|
* @param string $name the name of the session key you want to check
|
|
|
|
* @return boolean true is session variable is set, false if not
|
2011-10-15 16:08:49 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::check
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function check($name) {
|
2010-07-21 03:35:59 +00:00
|
|
|
return CakeSession::check($name);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used to determine the last error in a session.
|
|
|
|
*
|
|
|
|
* In your controller: $this->Session->error();
|
|
|
|
*
|
|
|
|
* @return string Last session error
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function error() {
|
2010-07-21 03:35:59 +00:00
|
|
|
return CakeSession::error();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used to set a session variable that can be used to output messages in the view.
|
|
|
|
*
|
|
|
|
* In your controller: $this->Session->setFlash('This has been saved');
|
|
|
|
*
|
2011-02-21 19:08:01 +00:00
|
|
|
* Additional params below can be passed to customize the output, or the Message.[key].
|
|
|
|
* You can also set additional parameters when rendering flash messages. See SessionHelper::flash()
|
|
|
|
* for more information on how to do that.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $message Message to be flashed
|
2009-09-01 03:59:40 +00:00
|
|
|
* @param string $element Element to wrap flash message in.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param array $params Parameters to be sent to layout as view variables
|
|
|
|
* @param string $key Message key, default is 'flash'
|
2011-07-30 20:48:37 +00:00
|
|
|
* @return void
|
2011-10-15 16:08:49 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#creating-notification-messages
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function setFlash($message, $element = 'default', $params = array(), $key = 'flash') {
|
2010-07-21 03:35:59 +00:00
|
|
|
CakeSession::write('Message.' . $key, compact('message', 'element', 'params'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used to renew a session id
|
|
|
|
*
|
|
|
|
* In your controller: $this->Session->renew();
|
|
|
|
*
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function renew() {
|
2010-07-21 03:35:59 +00:00
|
|
|
return CakeSession::renew();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used to check for a valid session.
|
|
|
|
*
|
|
|
|
* In your controller: $this->Session->valid();
|
|
|
|
*
|
|
|
|
* @return boolean true is session is valid, false is session is invalid
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function valid() {
|
2010-07-21 03:35:59 +00:00
|
|
|
return CakeSession::valid();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used to destroy sessions
|
|
|
|
*
|
|
|
|
* In your controller: $this->Session->destroy();
|
|
|
|
*
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2011-10-15 16:08:49 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::destroy
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function destroy() {
|
2010-07-21 03:35:59 +00:00
|
|
|
return CakeSession::destroy();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-04-03 17:21:52 +00:00
|
|
|
* Get/Set the session id.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-04-03 17:21:52 +00:00
|
|
|
* When fetching the session id, the session will be started
|
|
|
|
* if it has not already been started. When setting the session id,
|
|
|
|
* the session will not be started.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-04-03 17:21:52 +00:00
|
|
|
* @param string $id Id to use (optional)
|
|
|
|
* @return string The current session id.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function id($id = null) {
|
2012-04-03 17:21:52 +00:00
|
|
|
if (empty($id)) {
|
|
|
|
CakeSession::start();
|
|
|
|
}
|
2010-07-21 03:35:59 +00:00
|
|
|
return CakeSession::id($id);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-07-25 23:57:48 +00:00
|
|
|
* Returns a bool, whether or not the session has been started.
|
2008-09-26 14:03:16 +00:00
|
|
|
*
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return boolean
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-25 23:57:48 +00:00
|
|
|
public function started() {
|
|
|
|
return CakeSession::started();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-06-09 14:00:00 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|