1 | <?php |
||
37 | class CookieComponent extends Component |
||
38 | { |
||
39 | use CookieCryptTrait; |
||
40 | |||
41 | /** |
||
42 | * Default config |
||
43 | * |
||
44 | * - `expires` - How long the cookies should last for. Defaults to 1 month. |
||
45 | * - `path` - The path on the server in which the cookie will be available on. |
||
46 | * If path is set to '/foo/', the cookie will only be available within the |
||
47 | * /foo/ directory and all sub-directories such as /foo/bar/ of domain. |
||
48 | * The default value is base path of app. For e.g. if your app is running |
||
49 | * under a subfolder "cakeapp" of document root the path would be "/cakeapp/" |
||
50 | * else it would be "/". |
||
51 | * - `domain` - The domain that the cookie is available. To make the cookie |
||
52 | * available on all subdomains of example.com set domain to '.example.com'. |
||
53 | * - `secure` - Indicates that the cookie should only be transmitted over a |
||
54 | * secure HTTPS connection. When set to true, the cookie will only be set if |
||
55 | * a secure connection exists. |
||
56 | * - `key` - Encryption key used when encrypted cookies are enabled. Defaults to Security.salt. |
||
57 | * - `httpOnly` - Set to true to make HTTP only cookies. Cookies that are HTTP only |
||
58 | * are not accessible in JavaScript. Default false. |
||
59 | * - `encryption` - Type of encryption to use. Defaults to 'aes'. |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $_defaultConfig = [ |
||
64 | 'path' => null, |
||
65 | 'domain' => '', |
||
66 | 'secure' => false, |
||
67 | 'key' => null, |
||
68 | 'httpOnly' => false, |
||
69 | 'encryption' => 'aes', |
||
70 | 'expires' => '+1 month', |
||
71 | ]; |
||
72 | |||
73 | /** |
||
74 | * Config specific to a given top level key name. |
||
75 | * |
||
76 | * The values in this array are merged with the general config |
||
77 | * to generate the configuration for a given top level cookie name. |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | protected $_keyConfig = []; |
||
82 | |||
83 | /** |
||
84 | * Values stored in the cookie. |
||
85 | * |
||
86 | * Accessed in the controller using $this->Cookie->read('Name.key'); |
||
87 | * |
||
88 | * @var string |
||
89 | */ |
||
90 | protected $_values = []; |
||
91 | |||
92 | /** |
||
93 | * A map of keys that have been loaded. |
||
94 | * |
||
95 | * Since CookieComponent lazily reads cookie data, |
||
96 | * we need to track which cookies have been read to account for |
||
97 | * read, delete, read patterns. |
||
98 | * |
||
99 | * @var array |
||
100 | */ |
||
101 | protected $_loaded = []; |
||
102 | |||
103 | /** |
||
104 | * A reference to the Controller's Cake\Network\Response object |
||
105 | * |
||
106 | * @var \Cake\Network\Response |
||
107 | */ |
||
108 | protected $_response = null; |
||
109 | |||
110 | /** |
||
111 | * Initialize config data and properties. |
||
112 | * |
||
113 | * @param array $config The config data. |
||
114 | * @return void |
||
115 | */ |
||
116 | public function initialize(array $config) |
||
137 | |||
138 | /** |
||
139 | * Set the configuration for a specific top level key. |
||
140 | * |
||
141 | * ### Examples: |
||
142 | * |
||
143 | * Set a single config option for a key: |
||
144 | * |
||
145 | * ``` |
||
146 | * $this->Cookie->configKey('User', 'expires', '+3 months'); |
||
147 | * ``` |
||
148 | * |
||
149 | * Set multiple options: |
||
150 | * |
||
151 | * ``` |
||
152 | * $this->Cookie->configKey('User', [ |
||
153 | * 'expires', '+3 months', |
||
154 | * 'httpOnly' => true, |
||
155 | * ]); |
||
156 | * ``` |
||
157 | * |
||
158 | * @param string $keyname The top level keyname to configure. |
||
159 | * @param null|string|array $option Either the option name to set, or an array of options to set, |
||
160 | * or null to read config options for a given key. |
||
161 | * @param string|null $value Either the value to set, or empty when $option is an array. |
||
162 | * @return array|null |
||
163 | */ |
||
164 | public function configKey($keyname, $option = null, $value = null) |
||
177 | |||
178 | /** |
||
179 | * Events supported by this component. |
||
180 | * |
||
181 | * @return array |
||
182 | */ |
||
183 | public function implementedEvents() |
||
187 | |||
188 | /** |
||
189 | * Write a value to the response cookies. |
||
190 | * |
||
191 | * You must use this method before any output is sent to the browser. |
||
192 | * Failure to do so will result in header already sent errors. |
||
193 | * |
||
194 | * @param string|array $key Key for the value |
||
195 | * @param mixed $value Value |
||
196 | * @return void |
||
197 | */ |
||
198 | public function write($key, $value = null) |
||
217 | |||
218 | /** |
||
219 | * Read the value of key path from request cookies. |
||
220 | * |
||
221 | * This method will also allow you to read cookies that have been written in this |
||
222 | * request, but not yet sent to the client. |
||
223 | * |
||
224 | * @param string|null $key Key of the value to be obtained. |
||
225 | * @return string or null, value for specified key |
||
226 | */ |
||
227 | public function read($key = null) |
||
232 | |||
233 | /** |
||
234 | * Load the cookie data from the request and response objects. |
||
235 | * |
||
236 | * Based on the configuration data, cookies will be decrypted. When cookies |
||
237 | * contain array data, that data will be expanded. |
||
238 | * |
||
239 | * @param string|array $key The key to load. |
||
240 | * @return void |
||
241 | */ |
||
242 | protected function _load($key) |
||
257 | |||
258 | /** |
||
259 | * Returns true if given key is set in the cookie. |
||
260 | * |
||
261 | * @param string|null $key Key to check for |
||
262 | * @return bool True if the key exists |
||
263 | */ |
||
264 | public function check($key = null) |
||
271 | |||
272 | /** |
||
273 | * Delete a cookie value |
||
274 | * |
||
275 | * You must use this method before any output is sent to the browser. |
||
276 | * Failure to do so will result in header already sent errors. |
||
277 | * |
||
278 | * Deleting a top level key will delete all keys nested within that key. |
||
279 | * For example deleting the `User` key, will also delete `User.email`. |
||
280 | * |
||
281 | * @param string $key Key of the value to be deleted |
||
282 | * @return void |
||
283 | */ |
||
284 | public function delete($key) |
||
298 | |||
299 | /** |
||
300 | * Set cookie |
||
301 | * |
||
302 | * @param string $name Name for cookie |
||
303 | * @param string $value Value for cookie |
||
304 | * @return void |
||
305 | */ |
||
306 | protected function _write($name, $value) |
||
321 | |||
322 | /** |
||
323 | * Sets a cookie expire time to remove cookie value. |
||
324 | * |
||
325 | * This is only done once all values in a cookie key have been |
||
326 | * removed with delete. |
||
327 | * |
||
328 | * @param string $name Name of cookie |
||
329 | * @return void |
||
330 | */ |
||
331 | protected function _delete($name) |
||
346 | |||
347 | /** |
||
348 | * Returns the encryption key to be used. |
||
349 | * |
||
350 | * @return string |
||
351 | */ |
||
352 | protected function _getCookieEncryptionKey() |
||
356 | } |
||
357 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.