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