1 <?php
2
3 namespace Alo\Statics;
4
5 if (!defined('GEN_START')) {
6 http_response_code(404);
7 die();
8 }
9
10 /**
11 * Cookie handler
12 *
13 * @author Art <a.molcanovas@gmail.com>
14 * @package Statics
15 */
16 class Cookie {
17
18 /**
19 * Wrapper for PHP's setcookie with
20 *
21 * @author Art <a.molcanovas@gmail.com>
22 * @param string $name Cookie name
23 * @param string $value Cookie value
24 * @param int|boolean $expire False if deleting a cookie,
25 * 0 for session-length cookie, expiration time in seconds otherwise
26 * @param string $path The path the cookie will be available at
27 * @param string $domain The domain the cookie will be available at
28 * @param boolean $secure Whether to only transfer the cookie via HTTPS.
29 * @param boolean $httponly Whether to only allow server-side usage of the cookie
30 * @return boolean Whether the cookie was set. Always returns false on CLI requests.
31 */
32 static function set($name, $value, $expire = 0, $path = '/', $domain = '', $secure = false, $httponly = true) {
33 if (!\Alo::$router->is_cli_request() && !defined('PHPUNIT_RUNNING')) {
34 $expire = (int)$expire;
35 $secure = (bool)$secure;
36 $httponly = (bool)$httponly;
37
38 if ($expire === false) {
39 $expire = time() - 100;
40 } elseif ($expire > 0) {
41 $expire = time() + $expire;
42 }
43
44 \Log::debug('Set cookie ' . $name . ' to ' . $value . ' (expires ' . date('Y-m-d H:i:s', $expire) . ')');
45
46 return setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
47 } else {
48 \Log::debug('Not setting cookie ' . $name . ' as we\'re in CLI mode');
49
50 return false;
51 }
52 }
53
54 /**
55 * Deletes a cookie
56 *
57 * @author Art <a.molcanovas@gmail.com>
58 * @param string $name Cookie name
59 * @return boolean Whether the cookie was deleted. Always returns false on CLI requests.
60 */
61 static function delete($name) {
62 if (!\Alo::$router->is_cli_request() && !defined('PHPUNIT_RUNNING')) {
63 \Log::debug('Deleted cookie ' . $name);
64
65 return setcookie($name, '', false, '/', '', false, true);
66 } else {
67 \Log::debug('Not deleting cookie ' . $name . ' as we\'re in CLI mode');
68
69 return false;
70 }
71 }
72
73 }