PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/code/ryzom/tools/server/www/webtt/cake/libs/controller/components/session.php

https://bitbucket.org/mattraykowski/ryzomcore_demoshard
PHP | 295 lines | 111 code | 17 blank | 167 comment | 22 complexity | f945d090acbe20c5df2e33618a89c2bf MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, LGPL-2.1
  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. * Session Component.
  25. *
  26. * Session handling from the controller.
  27. *
  28. * @package cake
  29. * @subpackage cake.cake.libs.controller.components
  30. * @link http://book.cakephp.org/view/1310/Sessions
  31. *
  32. */
  33. class SessionComponent extends CakeSession {
  34. /**
  35. * Used to determine if methods implementation is used, or bypassed
  36. *
  37. * @var boolean
  38. * @access private
  39. */
  40. var $__active = true;
  41. /**
  42. * Used to determine if request are from an Ajax request
  43. *
  44. * @var boolean
  45. * @access private
  46. */
  47. var $__bare = 0;
  48. /**
  49. * Class constructor
  50. *
  51. * @param string $base The base path for the Session
  52. */
  53. function __construct($base = null) {
  54. if (Configure::read('Session.start') === true) {
  55. parent::__construct($base);
  56. } else {
  57. $this->__active = false;
  58. }
  59. }
  60. /**
  61. * Startup method.
  62. *
  63. * @param object $controller Instantiating controller
  64. * @return void
  65. * @access public
  66. */
  67. function startup(&$controller) {
  68. if ($this->started() === false && $this->__active === true) {
  69. $this->__start();
  70. }
  71. }
  72. /**
  73. * Starts Session on if 'Session.start' is set to false in core.php
  74. *
  75. * @param string $base The base path for the Session
  76. * @return void
  77. * @access public
  78. */
  79. function activate($base = null) {
  80. if ($this->__active === true) {
  81. return;
  82. }
  83. parent::__construct($base);
  84. $this->__active = true;
  85. }
  86. /**
  87. * Used to write a value to a session key.
  88. *
  89. * In your controller: $this->Session->write('Controller.sessKey', 'session value');
  90. *
  91. * @param string $name The name of the key your are setting in the session.
  92. * This should be in a Controller.key format for better organizing
  93. * @param string $value The value you want to store in a session.
  94. * @return boolean Success
  95. * @access public
  96. * @link http://book.cakephp.org/view/1312/write
  97. */
  98. function write($name, $value = null) {
  99. if ($this->__active === true) {
  100. $this->__start();
  101. if (is_array($name)) {
  102. foreach ($name as $key => $value) {
  103. if (parent::write($key, $value) === false) {
  104. return false;
  105. }
  106. }
  107. return true;
  108. }
  109. if (parent::write($name, $value) === false) {
  110. return false;
  111. }
  112. return true;
  113. }
  114. return false;
  115. }
  116. /**
  117. * Used to read a session values for a key or return values for all keys.
  118. *
  119. * In your controller: $this->Session->read('Controller.sessKey');
  120. * Calling the method without a param will return all session vars
  121. *
  122. * @param string $name the name of the session key you want to read
  123. * @return mixed value from the session vars
  124. * @access public
  125. * @link http://book.cakephp.org/view/1314/read
  126. */
  127. function read($name = null) {
  128. if ($this->__active === true) {
  129. $this->__start();
  130. return parent::read($name);
  131. }
  132. return false;
  133. }
  134. /**
  135. * Wrapper for SessionComponent::del();
  136. *
  137. * In your controller: $this->Session->delete('Controller.sessKey');
  138. *
  139. * @param string $name the name of the session key you want to delete
  140. * @return boolean true is session variable is set and can be deleted, false is variable was not set.
  141. * @access public
  142. * @link http://book.cakephp.org/view/1316/delete
  143. */
  144. function delete($name) {
  145. if ($this->__active === true) {
  146. $this->__start();
  147. return parent::delete($name);
  148. }
  149. return false;
  150. }
  151. /**
  152. * Used to check if a session variable is set
  153. *
  154. * In your controller: $this->Session->check('Controller.sessKey');
  155. *
  156. * @param string $name the name of the session key you want to check
  157. * @return boolean true is session variable is set, false if not
  158. * @access public
  159. * @link http://book.cakephp.org/view/1315/check
  160. */
  161. function check($name) {
  162. if ($this->__active === true) {
  163. $this->__start();
  164. return parent::check($name);
  165. }
  166. return false;
  167. }
  168. /**
  169. * Used to determine the last error in a session.
  170. *
  171. * In your controller: $this->Session->error();
  172. *
  173. * @return string Last session error
  174. * @access public
  175. * @link http://book.cakephp.org/view/1318/error
  176. */
  177. function error() {
  178. if ($this->__active === true) {
  179. $this->__start();
  180. return parent::error();
  181. }
  182. return false;
  183. }
  184. /**
  185. * Used to set a session variable that can be used to output messages in the view.
  186. *
  187. * In your controller: $this->Session->setFlash('This has been saved');
  188. *
  189. * Additional params below can be passed to customize the output, or the Message.[key]
  190. *
  191. * @param string $message Message to be flashed
  192. * @param string $element Element to wrap flash message in.
  193. * @param array $params Parameters to be sent to layout as view variables
  194. * @param string $key Message key, default is 'flash'
  195. * @access public
  196. * @link http://book.cakephp.org/view/1313/setFlash
  197. */
  198. function setFlash($message, $element = 'default', $params = array(), $key = 'flash') {
  199. if ($this->__active === true) {
  200. $this->__start();
  201. $this->write('Message.' . $key, compact('message', 'element', 'params'));
  202. }
  203. }
  204. /**
  205. * Used to renew a session id
  206. *
  207. * In your controller: $this->Session->renew();
  208. *
  209. * @return void
  210. * @access public
  211. */
  212. function renew() {
  213. if ($this->__active === true) {
  214. $this->__start();
  215. parent::renew();
  216. }
  217. }
  218. /**
  219. * Used to check for a valid session.
  220. *
  221. * In your controller: $this->Session->valid();
  222. *
  223. * @return boolean true is session is valid, false is session is invalid
  224. * @access public
  225. */
  226. function valid() {
  227. if ($this->__active === true) {
  228. $this->__start();
  229. return parent::valid();
  230. }
  231. return false;
  232. }
  233. /**
  234. * Used to destroy sessions
  235. *
  236. * In your controller: $this->Session->destroy();
  237. *
  238. * @return void
  239. * @access public
  240. * @link http://book.cakephp.org/view/1317/destroy
  241. */
  242. function destroy() {
  243. if ($this->__active === true) {
  244. $this->__start();
  245. parent::destroy();
  246. }
  247. }
  248. /**
  249. * Returns Session id
  250. *
  251. * If $id is passed in a beforeFilter, the Session will be started
  252. * with the specified id
  253. *
  254. * @param $id string
  255. * @return string
  256. * @access public
  257. */
  258. function id($id = null) {
  259. return parent::id($id);
  260. }
  261. /**
  262. * Starts Session if SessionComponent is used in Controller::beforeFilter(),
  263. * or is called from
  264. *
  265. * @return boolean
  266. * @access private
  267. */
  268. function __start() {
  269. if ($this->started() === false) {
  270. if (!$this->id() && parent::start()) {
  271. parent::_checkValid();
  272. } else {
  273. parent::start();
  274. }
  275. }
  276. return $this->started();
  277. }
  278. }