SessionCookie.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Slim - a micro PHP 5 framework
  4. *
  5. * @author Josh Lockhart <[email protected]>
  6. * @copyright 2011 Josh Lockhart
  7. * @link http://www.slimframework.com
  8. * @license http://www.slimframework.com/license
  9. * @version 2.2.0
  10. * @package Slim
  11. *
  12. * MIT LICENSE
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining
  15. * a copy of this software and associated documentation files (the
  16. * "Software"), to deal in the Software without restriction, including
  17. * without limitation the rights to use, copy, modify, merge, publish,
  18. * distribute, sublicense, and/or sell copies of the Software, and to
  19. * permit persons to whom the Software is furnished to do so, subject to
  20. * the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be
  23. * included in all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. */
  33. namespace Slim\Middleware;
  34. /**
  35. * Session Cookie
  36. *
  37. * This class provides an HTTP cookie storage mechanism
  38. * for session data. This class avoids using a PHP session
  39. * and instead serializes/unserializes the $_SESSION global
  40. * variable to/from an HTTP cookie.
  41. *
  42. * If a secret key is provided with this middleware, the HTTP
  43. * cookie will be checked for integrity to ensure the client-side
  44. * cookie is not changed.
  45. *
  46. * You should NEVER store sensitive data in a client-side cookie
  47. * in any format, encrypted or not. If you need to store sensitive
  48. * user information in a session, you should rely on PHP's native
  49. * session implementation, or use other middleware to store
  50. * session data in a database or alternative server-side cache.
  51. *
  52. * Because this class stores serialized session data in an HTTP cookie,
  53. * you are inherently limtied to 4 Kb. If you attempt to store
  54. * more than this amount, serialization will fail.
  55. *
  56. * @package Slim
  57. * @author Josh Lockhart
  58. * @since 1.6.0
  59. */
  60. class SessionCookie extends \Slim\Middleware
  61. {
  62. /**
  63. * @var array
  64. */
  65. protected $settings;
  66. /**
  67. * Constructor
  68. *
  69. * @param array $settings
  70. */
  71. public function __construct($settings = array())
  72. {
  73. $this->settings = array_merge(array(
  74. 'expires' => '20 minutes',
  75. 'path' => '/',
  76. 'domain' => null,
  77. 'secure' => false,
  78. 'httponly' => false,
  79. 'name' => 'slim_session',
  80. 'secret' => 'CHANGE_ME',
  81. 'cipher' => MCRYPT_RIJNDAEL_256,
  82. 'cipher_mode' => MCRYPT_MODE_CBC
  83. ), $settings);
  84. if (is_string($this->settings['expires'])) {
  85. $this->settings['expires'] = strtotime($this->settings['expires']);
  86. }
  87. /**
  88. * Session
  89. *
  90. * We must start a native PHP session to initialize the $_SESSION superglobal.
  91. * However, we won't be using the native session store for persistence, so we
  92. * disable the session cookie and cache limiter. We also set the session
  93. * handler to this class instance to avoid PHP's native session file locking.
  94. */
  95. ini_set('session.use_cookies', 0);
  96. session_cache_limiter(false);
  97. session_set_save_handler(
  98. array($this, 'open'),
  99. array($this, 'close'),
  100. array($this, 'read'),
  101. array($this, 'write'),
  102. array($this, 'destroy'),
  103. array($this, 'gc')
  104. );
  105. }
  106. /**
  107. * Call
  108. */
  109. public function call()
  110. {
  111. $this->loadSession();
  112. $this->next->call();
  113. $this->saveSession();
  114. }
  115. /**
  116. * Load session
  117. * @param array $env
  118. */
  119. protected function loadSession()
  120. {
  121. if (session_id() === '') {
  122. session_start();
  123. }
  124. $value = \Slim\Http\Util::decodeSecureCookie(
  125. $this->app->request()->cookies($this->settings['name']),
  126. $this->settings['secret'],
  127. $this->settings['cipher'],
  128. $this->settings['cipher_mode']
  129. );
  130. if ($value) {
  131. $_SESSION = unserialize($value);
  132. } else {
  133. $_SESSION = array();
  134. }
  135. }
  136. /**
  137. * Save session
  138. */
  139. protected function saveSession()
  140. {
  141. $value = \Slim\Http\Util::encodeSecureCookie(
  142. serialize($_SESSION),
  143. $this->settings['expires'],
  144. $this->settings['secret'],
  145. $this->settings['cipher'],
  146. $this->settings['cipher_mode']
  147. );
  148. if (strlen($value) > 4096) {
  149. $this->app->getLog()->error('WARNING! Slim\Middleware\SessionCookie data size is larger than 4KB. Content save failed.');
  150. } else {
  151. $this->app->response()->setCookie($this->settings['name'], array(
  152. 'value' => $value,
  153. 'domain' => $this->settings['domain'],
  154. 'path' => $this->settings['path'],
  155. 'expires' => $this->settings['expires'],
  156. 'secure' => $this->settings['secure'],
  157. 'httponly' => $this->settings['httponly']
  158. ));
  159. }
  160. session_destroy();
  161. }
  162. /********************************************************************************
  163. * Session Handler
  164. *******************************************************************************/
  165. public function open($savePath, $sessionName)
  166. {
  167. return true;
  168. }
  169. public function close()
  170. {
  171. return true;
  172. }
  173. public function read($id)
  174. {
  175. return '';
  176. }
  177. public function write($id, $data)
  178. {
  179. return true;
  180. }
  181. public function destroy($id)
  182. {
  183. return true;
  184. }
  185. public function gc($maxlifetime)
  186. {
  187. return true;
  188. }
  189. }