1: <?php
2: /**
3: * SessionComponent. Provides access to Sessions from the Controller layer
4: *
5: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6: * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
13: * @link http://cakephp.org CakePHP(tm) Project
14: * @package Cake.Controller.Component
15: * @since CakePHP(tm) v 0.10.0.1232
16: * @license http://www.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 http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html
29: * @link http://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 void
38: */
39: public function userAgent($userAgent = null) {
40: return CakeSession::userAgent($userAgent);
41: }
42:
43: /**
44: * Used to write a value to a session key.
45: *
46: * In your controller: $this->Session->write('Controller.sessKey', 'session value');
47: *
48: * @param string $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 string $value The value you want to store in a session.
51: * @return boolean Success
52: * @link http://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: * Used to read a session values for a key or return 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 http://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: * Wrapper for SessionComponent::del();
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 boolean true is session variable is set and can be deleted, false is variable was not set.
79: * @link http://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: * Used to check if a session variable is set
87: *
88: * In your controller: $this->Session->check('Controller.sessKey');
89: *
90: * @param string $name the name of the session key you want to check
91: * @return boolean true is session variable is set, false if not
92: * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::check
93: */
94: public function check($name) {
95: return CakeSession::check($name);
96: }
97:
98: /**
99: * Used to determine the last error in a session.
100: *
101: * In your controller: $this->Session->error();
102: *
103: * @return string Last session error
104: */
105: public function error() {
106: return CakeSession::error();
107: }
108:
109: /**
110: * Used to set a session variable that can be used to output messages in the view.
111: *
112: * In your controller: $this->Session->setFlash('This has been saved');
113: *
114: * Additional params below can be passed to customize the output, or the Message.[key].
115: * You can also set additional parameters when rendering flash messages. See SessionHelper::flash()
116: * for more information on how to do that.
117: *
118: * @param string $message Message to be flashed
119: * @param string $element Element to wrap flash message in.
120: * @param array $params Parameters to be sent to layout as view variables
121: * @param string $key Message key, default is 'flash'
122: * @return void
123: * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#creating-notification-messages
124: */
125: public function setFlash($message, $element = 'default', $params = array(), $key = 'flash') {
126: CakeSession::write('Message.' . $key, compact('message', 'element', 'params'));
127: }
128:
129: /**
130: * Used to renew a session id
131: *
132: * In your controller: $this->Session->renew();
133: *
134: * @return void
135: */
136: public function renew() {
137: return CakeSession::renew();
138: }
139:
140: /**
141: * Used to check for a valid session.
142: *
143: * In your controller: $this->Session->valid();
144: *
145: * @return boolean true is session is valid, false is session is invalid
146: */
147: public function valid() {
148: return CakeSession::valid();
149: }
150:
151: /**
152: * Used to destroy sessions
153: *
154: * In your controller: $this->Session->destroy();
155: *
156: * @return void
157: * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::destroy
158: */
159: public function destroy() {
160: return CakeSession::destroy();
161: }
162:
163: /**
164: * Get/Set the session id.
165: *
166: * When fetching the session id, the session will be started
167: * if it has not already been started. When setting the session id,
168: * the session will not be started.
169: *
170: * @param string $id Id to use (optional)
171: * @return string The current session id.
172: */
173: public function id($id = null) {
174: if (empty($id)) {
175: CakeSession::start();
176: }
177: return CakeSession::id($id);
178: }
179:
180: /**
181: * Returns a bool, whether or not the session has been started.
182: *
183: * @return boolean
184: */
185: public function started() {
186: return CakeSession::started();
187: }
188:
189: }
190: