PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/station-games/vendor/cakephp/cakephp/src/Controller/Component/CookieComponent.php

https://gitlab.com/ViniciusP/project-games
PHP | 356 lines | 147 code | 29 blank | 180 comment | 12 complexity | 34d5d79bab85826c2db9508da758b87c MD5 | raw file
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Controller\Component;
  16. use Cake\Controller\Component;
  17. use Cake\I18n\Time;
  18. use Cake\Network\Request;
  19. use Cake\Network\Response;
  20. use Cake\Utility\CookieCryptTrait;
  21. use Cake\Utility\Hash;
  22. use Cake\Utility\Security;
  23. /**
  24. * Cookie Component.
  25. *
  26. * Provides enhanced cookie handling features for use in the controller layer.
  27. * In addition to the basic features offered be Cake\Network\Response, this class lets you:
  28. *
  29. * - Create and read encrypted cookies.
  30. * - Store non-scalar data.
  31. * - Use hash compatible syntax to read/write/delete values.
  32. *
  33. * @link http://book.cakephp.org/3.0/en/controllers/components/cookie.html
  34. */
  35. class CookieComponent extends Component
  36. {
  37. use CookieCryptTrait;
  38. /**
  39. * Default config
  40. *
  41. * - `expires` - How long the cookies should last for. Defaults to 1 month.
  42. * - `path` - The path on the server in which the cookie will be available on.
  43. * If path is set to '/foo/', the cookie will only be available within the
  44. * /foo/ directory and all sub-directories such as /foo/bar/ of domain.
  45. * The default value is base path of app. For e.g. if your app is running
  46. * under a subfolder "cakeapp" of document root the path would be "/cakeapp/"
  47. * else it would be "/".
  48. * - `domain` - The domain that the cookie is available. To make the cookie
  49. * available on all subdomains of example.com set domain to '.example.com'.
  50. * - `secure` - Indicates that the cookie should only be transmitted over a
  51. * secure HTTPS connection. When set to true, the cookie will only be set if
  52. * a secure connection exists.
  53. * - `key` - Encryption key used when encrypted cookies are enabled. Defaults to Security.salt.
  54. * - `httpOnly` - Set to true to make HTTP only cookies. Cookies that are HTTP only
  55. * are not accessible in JavaScript. Default false.
  56. * - `encryption` - Type of encryption to use. Defaults to 'aes'.
  57. *
  58. * @var array
  59. */
  60. protected $_defaultConfig = [
  61. 'path' => null,
  62. 'domain' => '',
  63. 'secure' => false,
  64. 'key' => null,
  65. 'httpOnly' => false,
  66. 'encryption' => 'aes',
  67. 'expires' => '+1 month',
  68. ];
  69. /**
  70. * Config specific to a given top level key name.
  71. *
  72. * The values in this array are merged with the general config
  73. * to generate the configuration for a given top level cookie name.
  74. *
  75. * @var array
  76. */
  77. protected $_keyConfig = [];
  78. /**
  79. * Values stored in the cookie.
  80. *
  81. * Accessed in the controller using $this->Cookie->read('Name.key');
  82. *
  83. * @var string
  84. */
  85. protected $_values = [];
  86. /**
  87. * A map of keys that have been loaded.
  88. *
  89. * Since CookieComponent lazily reads cookie data,
  90. * we need to track which cookies have been read to account for
  91. * read, delete, read patterns.
  92. *
  93. * @var array
  94. */
  95. protected $_loaded = [];
  96. /**
  97. * A reference to the Controller's Cake\Network\Response object
  98. *
  99. * @var \Cake\Network\Response
  100. */
  101. protected $_response = null;
  102. /**
  103. * Initialize config data and properties.
  104. *
  105. * @param array $config The config data.
  106. * @return void
  107. */
  108. public function initialize(array $config)
  109. {
  110. if (!$this->_config['key']) {
  111. $this->config('key', Security::salt());
  112. }
  113. $controller = $this->_registry->getController();
  114. if ($controller !== null) {
  115. $this->_response =& $controller->response;
  116. }
  117. if ($controller === null) {
  118. $this->request = Request::createFromGlobals();
  119. $this->_response = new Response();
  120. }
  121. if (empty($this->_config['path'])) {
  122. $this->config('path', $this->request->webroot);
  123. }
  124. }
  125. /**
  126. * Set the configuration for a specific top level key.
  127. *
  128. * ### Examples:
  129. *
  130. * Set a single config option for a key:
  131. *
  132. * ```
  133. * $this->Cookie->configKey('User', 'expires', '+3 months');
  134. * ```
  135. *
  136. * Set multiple options:
  137. *
  138. * ```
  139. * $this->Cookie->configKey('User', [
  140. * 'expires', '+3 months',
  141. * 'httpOnly' => true,
  142. * ]);
  143. * ```
  144. *
  145. * @param string $keyname The top level keyname to configure.
  146. * @param null|string|array $option Either the option name to set, or an array of options to set,
  147. * or null to read config options for a given key.
  148. * @param string|null $value Either the value to set, or empty when $option is an array.
  149. * @return array|null
  150. */
  151. public function configKey($keyname, $option = null, $value = null)
  152. {
  153. if ($option === null) {
  154. $default = $this->_config;
  155. $local = isset($this->_keyConfig[$keyname]) ? $this->_keyConfig[$keyname] : [];
  156. return $local + $default;
  157. }
  158. if (!is_array($option)) {
  159. $option = [$option => $value];
  160. }
  161. $this->_keyConfig[$keyname] = $option;
  162. return null;
  163. }
  164. /**
  165. * Events supported by this component.
  166. *
  167. * @return array
  168. */
  169. public function implementedEvents()
  170. {
  171. return [];
  172. }
  173. /**
  174. * Write a value to the response cookies.
  175. *
  176. * You must use this method before any output is sent to the browser.
  177. * Failure to do so will result in header already sent errors.
  178. *
  179. * @param string|array $key Key for the value
  180. * @param mixed $value Value
  181. * @return void
  182. */
  183. public function write($key, $value = null)
  184. {
  185. if (!is_array($key)) {
  186. $key = [$key => $value];
  187. }
  188. $keys = [];
  189. foreach ($key as $name => $value) {
  190. $this->_load($name);
  191. $this->_values = Hash::insert($this->_values, $name, $value);
  192. $parts = explode('.', $name);
  193. $keys[] = $parts[0];
  194. }
  195. foreach ($keys as $name) {
  196. $this->_write($name, $this->_values[$name]);
  197. }
  198. }
  199. /**
  200. * Read the value of key path from request cookies.
  201. *
  202. * This method will also allow you to read cookies that have been written in this
  203. * request, but not yet sent to the client.
  204. *
  205. * @param string|null $key Key of the value to be obtained.
  206. * @return string or null, value for specified key
  207. */
  208. public function read($key = null)
  209. {
  210. $this->_load($key);
  211. return Hash::get($this->_values, $key);
  212. }
  213. /**
  214. * Load the cookie data from the request and response objects.
  215. *
  216. * Based on the configuration data, cookies will be decrypted. When cookies
  217. * contain array data, that data will be expanded.
  218. *
  219. * @param string|array $key The key to load.
  220. * @return void
  221. */
  222. protected function _load($key)
  223. {
  224. $parts = explode('.', $key);
  225. $first = array_shift($parts);
  226. if (isset($this->_loaded[$first])) {
  227. return;
  228. }
  229. if (!isset($this->request->cookies[$first])) {
  230. return;
  231. }
  232. $cookie = $this->request->cookies[$first];
  233. $config = $this->configKey($first);
  234. $this->_loaded[$first] = true;
  235. $this->_values[$first] = $this->_decrypt($cookie, $config['encryption']);
  236. }
  237. /**
  238. * Returns true if given key is set in the cookie.
  239. *
  240. * @param string|null $key Key to check for
  241. * @return bool True if the key exists
  242. */
  243. public function check($key = null)
  244. {
  245. if (empty($key)) {
  246. return false;
  247. }
  248. return $this->read($key) !== null;
  249. }
  250. /**
  251. * Delete a cookie value
  252. *
  253. * You must use this method before any output is sent to the browser.
  254. * Failure to do so will result in header already sent errors.
  255. *
  256. * Deleting a top level key will delete all keys nested within that key.
  257. * For example deleting the `User` key, will also delete `User.email`.
  258. *
  259. * @param string $key Key of the value to be deleted
  260. * @return void
  261. */
  262. public function delete($key)
  263. {
  264. $this->_load($key);
  265. $this->_values = Hash::remove($this->_values, $key);
  266. $parts = explode('.', $key);
  267. $top = $parts[0];
  268. if (isset($this->_values[$top])) {
  269. $this->_write($top, $this->_values[$top]);
  270. } else {
  271. $this->_delete($top);
  272. }
  273. }
  274. /**
  275. * Set cookie
  276. *
  277. * @param string $name Name for cookie
  278. * @param string $value Value for cookie
  279. * @return void
  280. */
  281. protected function _write($name, $value)
  282. {
  283. $config = $this->configKey($name);
  284. $expires = new Time($config['expires']);
  285. $this->_response->cookie([
  286. 'name' => $name,
  287. 'value' => $this->_encrypt($value, $config['encryption']),
  288. 'expire' => $expires->format('U'),
  289. 'path' => $config['path'],
  290. 'domain' => $config['domain'],
  291. 'secure' => $config['secure'],
  292. 'httpOnly' => $config['httpOnly']
  293. ]);
  294. }
  295. /**
  296. * Sets a cookie expire time to remove cookie value.
  297. *
  298. * This is only done once all values in a cookie key have been
  299. * removed with delete.
  300. *
  301. * @param string $name Name of cookie
  302. * @return void
  303. */
  304. protected function _delete($name)
  305. {
  306. $config = $this->configKey($name);
  307. $expires = new Time('now');
  308. $this->_response->cookie([
  309. 'name' => $name,
  310. 'value' => '',
  311. 'expire' => $expires->format('U') - 42000,
  312. 'path' => $config['path'],
  313. 'domain' => $config['domain'],
  314. 'secure' => $config['secure'],
  315. 'httpOnly' => $config['httpOnly']
  316. ]);
  317. }
  318. /**
  319. * Returns the encryption key to be used.
  320. *
  321. * @return string
  322. */
  323. protected function _getCookieEncryptionKey()
  324. {
  325. return $this->_config['key'];
  326. }
  327. }