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