1: <?php
2: /**
3: * SessionComponent. Provides access to Sessions from the Controller layer
4: *
5: * PHP 5
6: *
7: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8: * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
9: *
10: * Licensed under The MIT License
11: * For full copyright and license information, please see the LICENSE.txt
12: * Redistributions of files must retain the above copyright notice.
13: *
14: * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
15: * @link http://cakephp.org CakePHP(tm) Project
16: * @package Cake.Controller.Component
17: * @since CakePHP(tm) v 0.10.0.1232
18: * @license http://www.opensource.org/licenses/mit-license.php MIT License
19: */
20:
21: App::uses('Component', 'Controller');
22: App::uses('CakeSession', 'Model/Datasource');
23:
24: /**
25: * The CakePHP SessionComponent provides a way to persist client data between
26: * page requests. It acts as a wrapper for the `$_SESSION` as well as providing
27: * convenience methods for several `$_SESSION` related functions.
28: *
29: * @package Cake.Controller.Component
30: * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html
31: * @link http://book.cakephp.org/2.0/en/development/sessions.html
32: */
33: class SessionComponent extends Component {
34:
35: /**
36: * Get / Set the userAgent
37: *
38: * @param string $userAgent Set the userAgent
39: * @return void
40: */
41: public function userAgent($userAgent = null) {
42: return CakeSession::userAgent($userAgent);
43: }
44:
45: /**
46: * Used to write a value to a session key.
47: *
48: * In your controller: $this->Session->write('Controller.sessKey', 'session value');
49: *
50: * @param string $name The name of the key your are setting in the session.
51: * This should be in a Controller.key format for better organizing
52: * @param string $value The value you want to store in a session.
53: * @return boolean Success
54: * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::write
55: */
56: public function write($name, $value = null) {
57: return CakeSession::write($name, $value);
58: }
59:
60: /**
61: * Used to read a session values for a key or return values for all keys.
62: *
63: * In your controller: $this->Session->read('Controller.sessKey');
64: * Calling the method without a param will return all session vars
65: *
66: * @param string $name the name of the session key you want to read
67: * @return mixed value from the session vars
68: * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::read
69: */
70: public function read($name = null) {
71: return CakeSession::read($name);
72: }
73:
74: /**
75: * Wrapper for SessionComponent::del();
76: *
77: * In your controller: $this->Session->delete('Controller.sessKey');
78: *
79: * @param string $name the name of the session key you want to delete
80: * @return boolean true is session variable is set and can be deleted, false is variable was not set.
81: * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::delete
82: */
83: public function delete($name) {
84: return CakeSession::delete($name);
85: }
86:
87: /**
88: * Used to check if a session variable is set
89: *
90: * In your controller: $this->Session->check('Controller.sessKey');
91: *
92: * @param string $name the name of the session key you want to check
93: * @return boolean true is session variable is set, false if not
94: * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::check
95: */
96: public function check($name) {
97: return CakeSession::check($name);
98: }
99:
100: /**
101: * Used to determine the last error in a session.
102: *
103: * In your controller: $this->Session->error();
104: *
105: * @return string Last session error
106: */
107: public function error() {
108: return CakeSession::error();
109: }
110:
111: /**
112: * Used to set a session variable that can be used to output messages in the view.
113: *
114: * In your controller: $this->Session->setFlash('This has been saved');
115: *
116: * Additional params below can be passed to customize the output, or the Message.[key].
117: * You can also set additional parameters when rendering flash messages. See SessionHelper::flash()
118: * for more information on how to do that.
119: *
120: * @param string $message Message to be flashed
121: * @param string $element Element to wrap flash message in.
122: * @param array $params Parameters to be sent to layout as view variables
123: * @param string $key Message key, default is 'flash'
124: * @return void
125: * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#creating-notification-messages
126: */
127: public function setFlash($message, $element = 'default', $params = array(), $key = 'flash') {
128: CakeSession::write('Message.' . $key, compact('message', 'element', 'params'));
129: }
130:
131: /**
132: * Used to renew a session id
133: *
134: * In your controller: $this->Session->renew();
135: *
136: * @return void
137: */
138: public function renew() {
139: return CakeSession::renew();
140: }
141:
142: /**
143: * Used to check for a valid session.
144: *
145: * In your controller: $this->Session->valid();
146: *
147: * @return boolean true is session is valid, false is session is invalid
148: */
149: public function valid() {
150: return CakeSession::valid();
151: }
152:
153: /**
154: * Used to destroy sessions
155: *
156: * In your controller: $this->Session->destroy();
157: *
158: * @return void
159: * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::destroy
160: */
161: public function destroy() {
162: return CakeSession::destroy();
163: }
164:
165: /**
166: * Get/Set the session id.
167: *
168: * When fetching the session id, the session will be started
169: * if it has not already been started. When setting the session id,
170: * the session will not be started.
171: *
172: * @param string $id Id to use (optional)
173: * @return string The current session id.
174: */
175: public function id($id = null) {
176: if (empty($id)) {
177: CakeSession::start();
178: }
179: return CakeSession::id($id);
180: }
181:
182: /**
183: * Returns a bool, whether or not the session has been started.
184: *
185: * @return boolean
186: */
187: public function started() {
188: return CakeSession::started();
189: }
190:
191: }
192: