1 <?php
2
3 namespace Alo;
4
5 if (!defined('GEN_START')) {
6 http_response_code(404);
7 } else {
8
9 /**
10 * Cookie handler
11 *
12 * @author Art <a.molcanovas@gmail.com>
13 * @package Statics
14 */
15 abstract class Cookie {
16
17 /**
18 * Wrapper for PHP's setcookie with
19 *
20 * @author Art <a.molcanovas@gmail.com>
21 *
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 *
31 * @return boolean Whether the cookie was set. Always returns false on CLI requests.
32 */
33 static function set($name, $value, $expire = 0, $path = '/', $domain = '', $secure = false,
34 $httponly = true) {
35 if (!\Alo::$router->isCliRequest() && !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
49 return setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
50 } else {
51 \Log::debug('Not setting cookie ' . $name . ' as we\'re in CLI mode');
52
53 return false;
54 }
55 }
56
57 /**
58 * Deletes a cookie
59 *
60 * @author Art <a.molcanovas@gmail.com>
61 *
62 * @param string $name Cookie name
63 *
64 * @return boolean Whether the cookie was deleted. Always returns false on CLI requests.
65 */
66 static function delete($name) {
67 if (!\Alo::$router->isCliRequest() && !defined('PHPUNIT_RUNNING')) {
68 \Log::debug('Deleted cookie ' . $name);
69
70 return setcookie($name, '', false, '/', '', false, true);
71 } else {
72 \Log::debug('Not deleting cookie ' . $name . ' as we\'re in CLI mode');
73
74 return false;
75 }
76 }
77
78 }
79 }
80