comparison cake/libs/controller/components/session.php @ 0:261e66bd5a0c

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 24 Jul 2011 21:08:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:261e66bd5a0c
1 <?php
2 /**
3 * SessionComponent. Provides access to Sessions from the Controller layer
4 *
5 * PHP versions 4 and 5
6 *
7 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
9 *
10 * Licensed under The MIT License
11 * Redistributions of files must retain the above copyright notice.
12 *
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link http://cakephp.org CakePHP(tm) Project
15 * @package cake
16 * @subpackage cake.cake.libs.controller.components
17 * @since CakePHP(tm) v 0.10.0.1232
18 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
19 */
20 if (!class_exists('cakesession')) {
21 require LIBS . 'cake_session.php';
22 }
23
24 /**
25 * Session Component.
26 *
27 * Session handling from the controller.
28 *
29 * @package cake
30 * @subpackage cake.cake.libs.controller.components
31 * @link http://book.cakephp.org/view/1310/Sessions
32 *
33 */
34 class SessionComponent extends CakeSession {
35
36 /**
37 * Used to determine if methods implementation is used, or bypassed
38 *
39 * @var boolean
40 * @access private
41 */
42 var $__active = true;
43
44 /**
45 * Used to determine if request are from an Ajax request
46 *
47 * @var boolean
48 * @access private
49 */
50 var $__bare = 0;
51
52 /**
53 * Class constructor
54 *
55 * @param string $base The base path for the Session
56 */
57 function __construct($base = null) {
58 if (Configure::read('Session.start') === true) {
59 parent::__construct($base);
60 } else {
61 $this->__active = false;
62 }
63 }
64
65 /**
66 * Startup method.
67 *
68 * @param object $controller Instantiating controller
69 * @return void
70 * @access public
71 */
72 function startup(&$controller) {
73 if ($this->started() === false && $this->__active === true) {
74 $this->__start();
75 }
76 }
77
78 /**
79 * Starts Session on if 'Session.start' is set to false in core.php
80 *
81 * @param string $base The base path for the Session
82 * @return void
83 * @access public
84 */
85 function activate($base = null) {
86 if ($this->__active === true) {
87 return;
88 }
89 parent::__construct($base);
90 $this->__active = true;
91 }
92
93 /**
94 * Used to write a value to a session key.
95 *
96 * In your controller: $this->Session->write('Controller.sessKey', 'session value');
97 *
98 * @param string $name The name of the key your are setting in the session.
99 * This should be in a Controller.key format for better organizing
100 * @param string $value The value you want to store in a session.
101 * @return boolean Success
102 * @access public
103 * @link http://book.cakephp.org/view/1312/write
104 */
105 function write($name, $value = null) {
106 if ($this->__active === true) {
107 $this->__start();
108 if (is_array($name)) {
109 foreach ($name as $key => $value) {
110 if (parent::write($key, $value) === false) {
111 return false;
112 }
113 }
114 return true;
115 }
116 if (parent::write($name, $value) === false) {
117 return false;
118 }
119 return true;
120 }
121 return false;
122 }
123
124 /**
125 * Used to read a session values for a key or return values for all keys.
126 *
127 * In your controller: $this->Session->read('Controller.sessKey');
128 * Calling the method without a param will return all session vars
129 *
130 * @param string $name the name of the session key you want to read
131 * @return mixed value from the session vars
132 * @access public
133 * @link http://book.cakephp.org/view/1314/read
134 */
135 function read($name = null) {
136 if ($this->__active === true) {
137 $this->__start();
138 return parent::read($name);
139 }
140 return false;
141 }
142
143 /**
144 * Wrapper for SessionComponent::del();
145 *
146 * In your controller: $this->Session->delete('Controller.sessKey');
147 *
148 * @param string $name the name of the session key you want to delete
149 * @return boolean true is session variable is set and can be deleted, false is variable was not set.
150 * @access public
151 * @link http://book.cakephp.org/view/1316/delete
152 */
153 function delete($name) {
154 if ($this->__active === true) {
155 $this->__start();
156 return parent::delete($name);
157 }
158 return false;
159 }
160
161 /**
162 * Used to check if a session variable is set
163 *
164 * In your controller: $this->Session->check('Controller.sessKey');
165 *
166 * @param string $name the name of the session key you want to check
167 * @return boolean true is session variable is set, false if not
168 * @access public
169 * @link http://book.cakephp.org/view/1315/check
170 */
171 function check($name) {
172 if ($this->__active === true) {
173 $this->__start();
174 return parent::check($name);
175 }
176 return false;
177 }
178
179 /**
180 * Used to determine the last error in a session.
181 *
182 * In your controller: $this->Session->error();
183 *
184 * @return string Last session error
185 * @access public
186 * @link http://book.cakephp.org/view/1318/error
187 */
188 function error() {
189 if ($this->__active === true) {
190 $this->__start();
191 return parent::error();
192 }
193 return false;
194 }
195
196 /**
197 * Used to set a session variable that can be used to output messages in the view.
198 *
199 * In your controller: $this->Session->setFlash('This has been saved');
200 *
201 * Additional params below can be passed to customize the output, or the Message.[key]
202 *
203 * @param string $message Message to be flashed
204 * @param string $element Element to wrap flash message in.
205 * @param array $params Parameters to be sent to layout as view variables
206 * @param string $key Message key, default is 'flash'
207 * @access public
208 * @link http://book.cakephp.org/view/1313/setFlash
209 */
210 function setFlash($message, $element = 'default', $params = array(), $key = 'flash') {
211 if ($this->__active === true) {
212 $this->__start();
213 $this->write('Message.' . $key, compact('message', 'element', 'params'));
214 }
215 }
216
217 /**
218 * Used to renew a session id
219 *
220 * In your controller: $this->Session->renew();
221 *
222 * @return void
223 * @access public
224 */
225 function renew() {
226 if ($this->__active === true) {
227 $this->__start();
228 parent::renew();
229 }
230 }
231
232 /**
233 * Used to check for a valid session.
234 *
235 * In your controller: $this->Session->valid();
236 *
237 * @return boolean true is session is valid, false is session is invalid
238 * @access public
239 */
240 function valid() {
241 if ($this->__active === true) {
242 $this->__start();
243 return parent::valid();
244 }
245 return false;
246 }
247
248 /**
249 * Used to destroy sessions
250 *
251 * In your controller: $this->Session->destroy();
252 *
253 * @return void
254 * @access public
255 * @link http://book.cakephp.org/view/1317/destroy
256 */
257 function destroy() {
258 if ($this->__active === true) {
259 $this->__start();
260 parent::destroy();
261 }
262 }
263
264 /**
265 * Returns Session id
266 *
267 * If $id is passed in a beforeFilter, the Session will be started
268 * with the specified id
269 *
270 * @param $id string
271 * @return string
272 * @access public
273 */
274 function id($id = null) {
275 return parent::id($id);
276 }
277
278 /**
279 * Starts Session if SessionComponent is used in Controller::beforeFilter(),
280 * or is called from
281 *
282 * @return boolean
283 * @access private
284 */
285 function __start() {
286 if ($this->started() === false) {
287 if (!$this->id() && parent::start()) {
288 parent::_checkValid();
289 } else {
290 parent::start();
291 }
292 }
293 return $this->started();
294 }
295 }