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
|
|
|
*
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://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
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
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-07-18 01:55:29 +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
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-libraries/components/sessions.html
|
|
|
|
* @link https://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
|
2015-09-25 15:11:20 +00:00
|
|
|
* @return string Current user agent.
|
2010-06-09 21:15:34 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
2014-12-23 02:50:35 +00:00
|
|
|
* Writes a value to a session key.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* In your controller: $this->Session->write('Controller.sessKey', 'session value');
|
|
|
|
*
|
2018-06-12 08:24:24 +00:00
|
|
|
* @param string|array $name The name of the key your are setting in the session.
|
2008-05-30 11:40:08 +00:00
|
|
|
* This should be in a Controller.key format for better organizing
|
2019-04-29 08:40:10 +00:00
|
|
|
* @param mixed $value The value you want to store in a session.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Success
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://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
|
|
|
/**
|
2014-12-23 02:50:35 +00:00
|
|
|
* Reads a session value for a key or returns values for all keys.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* 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
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://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
|
|
|
/**
|
2014-12-23 02:50:35 +00:00
|
|
|
* Deletes a session value for a key.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* In your controller: $this->Session->delete('Controller.sessKey');
|
|
|
|
*
|
|
|
|
* @param string $name the name of the session key you want to delete
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool true is session variable is set and can be deleted, false is variable was not set.
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://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
|
|
|
/**
|
2014-12-23 02:50:35 +00:00
|
|
|
* Reads and deletes a session value for a key.
|
|
|
|
*
|
|
|
|
* In your controller: `$this->Session->consume('Controller.sessKey');`
|
|
|
|
*
|
|
|
|
* @param string $name the name of the session key you want to read
|
|
|
|
* @return mixed values from the session vars
|
|
|
|
*/
|
|
|
|
public function consume($name) {
|
|
|
|
return CakeSession::consume($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a session variable is set.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* In your controller: $this->Session->check('Controller.sessKey');
|
|
|
|
*
|
|
|
|
* @param string $name the name of the session key you want to check
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool true is session variable is set, false if not
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://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
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#creating-notification-messages
|
2015-02-10 08:15:01 +00:00
|
|
|
* @deprecated 3.0.0 Since 2.7, use the FlashComponent instead.
|
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') {
|
2017-08-30 15:06:56 +00:00
|
|
|
$messages = (array)CakeSession::read('Message.' . $key);
|
2017-08-31 14:02:08 +00:00
|
|
|
$messages[] = array(
|
2017-08-30 15:06:56 +00:00
|
|
|
'message' => $message,
|
|
|
|
'element' => $element,
|
|
|
|
'params' => $params,
|
2017-08-31 14:02:08 +00:00
|
|
|
);
|
2017-08-30 15:06:56 +00:00
|
|
|
CakeSession::write('Message.' . $key, $messages);
|
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() {
|
2017-10-02 06:40:48 +00:00
|
|
|
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();
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool true is session is valid, false is session is invalid
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
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
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://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() {
|
2017-10-02 06:40:48 +00:00
|
|
|
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
|
2012-12-22 22:48:15 +00:00
|
|
|
* if it has not already been started. When setting the session id,
|
2012-04-03 17:21:52 +00:00
|
|
|
* 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
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
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
|
|
|
}
|