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 *
23 * @param string $name Cookie name
24 * @param string $value Cookie value
25 * @param int|boolean $expire False if deleting a cookie,
26 * 0 for session-length cookie, expiration time in seconds otherwise
27 * @param string $path The path the cookie will be available at
28 * @param string $domain The domain the cookie will be available at
29 * @param boolean $secure Whether to only transfer the cookie via HTTPS.
30 * @param boolean $httponly Whether to only allow server-side usage of the cookie
31 *
32 * @return boolean Whether the cookie was set. Always returns false on CLI requests.
33 */
34 static function set($name, $value, $expire = 0, $path = '/', $domain = '', $secure = false, $httponly = true) {
35 if(!\Alo::$router->is_cli_request() && !defined('PHPUNIT_RUNNING')) {
36 $expire = (int)$expire;
37 $secure = (bool)$secure;
38 $httponly = (bool)$httponly;
39
40 if($expire === false) {
41 $expire = time() - 100;
42 } elseif($expire > 0) {
43 $expire = time() + $expire;
44 }
45
46 \Log::debug('Set cookie ' . $name . ' to ' . $value . ' (expires ' . date('Y-m-d H:i:s', $expire) . ')');
47
48 return setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
49 } else {
50 \Log::debug('Not setting cookie ' . $name . ' as we\'re in CLI mode');
51
52 return false;
53 }
54 }
55
56 /**
57 * Deletes a cookie
58 *
59 * @author Art <a.molcanovas@gmail.com>
60 *
61 * @param string $name Cookie name
62 *
63 * @return boolean Whether the cookie was deleted. Always returns false on CLI requests.
64 */
65 static function delete($name) {
66 if(!\Alo::$router->is_cli_request() && !defined('PHPUNIT_RUNNING')) {
67 \Log::debug('Deleted cookie ' . $name);
68
69 return setcookie($name, '', false, '/', '', false, true);
70 } else {
71 \Log::debug('Not deleting cookie ' . $name . ' as we\'re in CLI mode');
72
73 return false;
74 }
75 }
76
77 }