/cake/libs/controller/components/session.php

https://bitbucket.org/d1rk/cakephp-including-capistrano · PHP · 315 lines · 126 code · 1 blank · 188 comment · 24 complexity · 17e717723456c7bb3ada16744158f9a7 MD5 · raw file

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