PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/slim/slim/Slim/Middleware/SessionCookie.php

https://gitlab.com/donyradjah/cvglobal
PHP | 210 lines | 94 code | 16 blank | 100 comment | 6 complexity | 1b19fdde3339ac43f936e0e0f863abc8 MD5 | raw file
  1. <?php
  2. /**
  3. * Slim - a micro PHP 5 framework
  4. *
  5. * @author Josh Lockhart <info@slimframework.com>
  6. * @copyright 2011 Josh Lockhart
  7. * @link http://www.slimframework.com
  8. * @license http://www.slimframework.com/license
  9. * @version 2.4.2
  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. * You should NEVER store sensitive data in a client-side cookie
  43. * in any format, encrypted (with cookies.encrypt) or not. If you
  44. * need to store sensitive user information in a session, you should
  45. * rely on PHP's native session implementation, or use other middleware
  46. * to store session data in a database or alternative server-side cache.
  47. *
  48. * Because this class stores serialized session data in an HTTP cookie,
  49. * you are inherently limited to 4 Kb. If you attempt to store
  50. * more than this amount, serialization will fail.
  51. *
  52. * @package Slim
  53. * @author Josh Lockhart
  54. * @since 1.6.0
  55. */
  56. class SessionCookie extends \Slim\Middleware
  57. {
  58. /**
  59. * @var array
  60. */
  61. protected $settings;
  62. /**
  63. * Constructor
  64. *
  65. * @param array $settings
  66. */
  67. public function __construct($settings = array())
  68. {
  69. $defaults = array(
  70. 'expires' => '20 minutes',
  71. 'path' => '/',
  72. 'domain' => null,
  73. 'secure' => false,
  74. 'httponly' => false,
  75. 'name' => 'slim_session',
  76. );
  77. $this->settings = array_merge($defaults, $settings);
  78. if (is_string($this->settings['expires'])) {
  79. $this->settings['expires'] = strtotime($this->settings['expires']);
  80. }
  81. /**
  82. * Session
  83. *
  84. * We must start a native PHP session to initialize the $_SESSION superglobal.
  85. * However, we won't be using the native session store for persistence, so we
  86. * disable the session cookie and cache limiter. We also set the session
  87. * handler to this class instance to avoid PHP's native session file locking.
  88. */
  89. ini_set('session.use_cookies', 0);
  90. session_cache_limiter(false);
  91. session_set_save_handler(
  92. array($this, 'open'),
  93. array($this, 'close'),
  94. array($this, 'read'),
  95. array($this, 'write'),
  96. array($this, 'destroy'),
  97. array($this, 'gc')
  98. );
  99. }
  100. /**
  101. * Call
  102. */
  103. public function call()
  104. {
  105. $this->loadSession();
  106. $this->next->call();
  107. $this->saveSession();
  108. }
  109. /**
  110. * Load session
  111. */
  112. protected function loadSession()
  113. {
  114. if (session_id() === '') {
  115. session_start();
  116. }
  117. $value = $this->app->getCookie($this->settings['name']);
  118. if ($value) {
  119. try {
  120. $_SESSION = json_decode($value, true);
  121. } catch (\Exception $e) {
  122. $this->app->getLog()->error('Error unserializing session cookie value! ' . $e->getMessage());
  123. }
  124. } else {
  125. $_SESSION = array();
  126. }
  127. }
  128. /**
  129. * Save session
  130. */
  131. protected function saveSession()
  132. {
  133. $value = json_encode($_SESSION);
  134. if (strlen($value) > 4096) {
  135. $this->app->getLog()->error('WARNING! Slim\Middleware\SessionCookie data size is larger than 4KB. Content save failed.');
  136. } else {
  137. $this->app->setCookie(
  138. $this->settings['name'],
  139. $value,
  140. $this->settings['expires'],
  141. $this->settings['path'],
  142. $this->settings['domain'],
  143. $this->settings['secure'],
  144. $this->settings['httponly']
  145. );
  146. }
  147. // session_destroy();
  148. }
  149. /********************************************************************************
  150. * Session Handler
  151. *******************************************************************************/
  152. /**
  153. * @codeCoverageIgnore
  154. */
  155. public function open($savePath, $sessionName)
  156. {
  157. return true;
  158. }
  159. /**
  160. * @codeCoverageIgnore
  161. */
  162. public function close()
  163. {
  164. return true;
  165. }
  166. /**
  167. * @codeCoverageIgnore
  168. */
  169. public function read($id)
  170. {
  171. return '';
  172. }
  173. /**
  174. * @codeCoverageIgnore
  175. */
  176. public function write($id, $data)
  177. {
  178. return true;
  179. }
  180. /**
  181. * @codeCoverageIgnore
  182. */
  183. public function destroy($id)
  184. {
  185. return true;
  186. }
  187. /**
  188. * @codeCoverageIgnore
  189. */
  190. public function gc($maxlifetime)
  191. {
  192. return true;
  193. }
  194. }