o2system /
kernel
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the O2System Framework package. |
||
| 4 | * |
||
| 5 | * For the full copyright and license information, please view the LICENSE |
||
| 6 | * file that was distributed with this source code. |
||
| 7 | * |
||
| 8 | * @author Steeve Andrian Salim |
||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
| 10 | */ |
||
| 11 | // ------------------------------------------------------------------------ |
||
| 12 | |||
| 13 | if ( ! function_exists('set_cookie')) { |
||
| 14 | /** |
||
| 15 | * set_cookie |
||
| 16 | * |
||
| 17 | * Accepts seven parameters, or you can submit an associative |
||
| 18 | * array in the first parameter containing all the values. |
||
| 19 | * |
||
| 20 | * @param mixed $name |
||
| 21 | * @param string $value The value of the cookie |
||
| 22 | * @param int $expire The number of seconds until expiration |
||
| 23 | * @param string $domain For site-wide cookie. |
||
| 24 | * Usually: .yourdomain.com |
||
| 25 | * @param string $path The cookie path |
||
| 26 | * @param string $prefix The cookie prefix |
||
| 27 | * @param bool $secure true makes the cookie secure |
||
| 28 | * @param bool $httpOnly true makes the cookie accessible via |
||
| 29 | * http(s) only (no javascript) |
||
| 30 | * |
||
| 31 | * @return bool |
||
| 32 | */ |
||
| 33 | function set_cookie( |
||
| 34 | $name, |
||
| 35 | $value = '', |
||
| 36 | $expire = 0, |
||
| 37 | $domain = '', |
||
| 38 | $path = '/', |
||
| 39 | $prefix = '', |
||
| 40 | $secure = null, |
||
| 41 | $httponly = null |
||
| 42 | ) { |
||
| 43 | if (is_array($name)) { |
||
| 44 | // always leave 'name' in last place, as the loop will break otherwise, due to $$item |
||
| 45 | foreach (['value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly', 'name'] as $item) { |
||
| 46 | if (isset($name[ $item ])) { |
||
| 47 | $$item = $name[ $item ]; |
||
| 48 | } |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | if ($prefix === '' && function_exists('config')) { |
||
| 53 | $prefix = config()->offsetGet('cookie')[ 'prefix' ]; |
||
| 54 | } |
||
| 55 | |||
| 56 | if ($domain === '' && function_exists('config')) { |
||
| 57 | $domain = config()->offsetGet('cookie')[ 'domain' ]; |
||
| 58 | } |
||
| 59 | |||
| 60 | if ($path === '' && function_exists('config')) { |
||
| 61 | $path = config()->offsetGet('cookie')[ 'path' ]; |
||
| 62 | } |
||
| 63 | |||
| 64 | if ($secure === null && function_exists('config')) { |
||
| 65 | $secure = config()->offsetGet('cookie')[ 'secure' ]; |
||
| 66 | } |
||
| 67 | |||
| 68 | if ($httponly === null && function_exists('config')) { |
||
| 69 | $httponly = config()->offsetGet('cookie')[ 'httpOnly' ]; |
||
| 70 | } |
||
| 71 | |||
| 72 | if ( ! is_numeric($expire) OR $expire < 0) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 73 | $expire = -1; |
||
| 74 | } else { |
||
| 75 | $expire = ($expire > 0) ? time() + $expire : -1; |
||
| 76 | } |
||
| 77 | |||
| 78 | return setcookie( |
||
| 79 | $prefix . $name, |
||
| 80 | $value, |
||
| 81 | $expire, |
||
| 82 | $path, |
||
| 83 | '.' . ltrim($domain, '.'), |
||
| 84 | $secure, |
||
| 85 | $httponly |
||
| 86 | ); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | //-------------------------------------------------------------------- |
||
| 90 | if ( ! function_exists('get_cookie')) { |
||
| 91 | /** |
||
| 92 | * get_cookie |
||
| 93 | * |
||
| 94 | * Fetch an item from the COOKIE array |
||
| 95 | * |
||
| 96 | * @param string $name The cookie index name. |
||
| 97 | * |
||
| 98 | * @return mixed Returns FALSE if the cookie is not exists. |
||
| 99 | */ |
||
| 100 | function get_cookie($name) |
||
| 101 | { |
||
| 102 | if (isset($_COOKIE[ $name ])) { |
||
| 103 | return $_COOKIE[ $name ]; |
||
| 104 | } elseif (function_exists('config')) { |
||
| 105 | $prefix = config()->offsetGet('cookie')[ 'prefix' ]; |
||
| 106 | if (isset($_COOKIE[ $prefix . $name ])) { |
||
| 107 | return $_COOKIE[ $prefix . $name ]; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | return false; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | //-------------------------------------------------------------------- |
||
| 116 | |||
| 117 | if ( ! function_exists('delete_cookie')) { |
||
| 118 | /** |
||
| 119 | * delete_cookie |
||
| 120 | * |
||
| 121 | * Delete a COOKIE |
||
| 122 | * |
||
| 123 | * @param mixed $name The cookie name. |
||
| 124 | * @param string $domain The cookie domain. Usually: .yourdomain.com |
||
| 125 | * @param string $path The cookie path |
||
| 126 | * @param string $prefix The cookie prefix |
||
| 127 | * |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | function delete_cookie($name, $domain = '', $path = '/', $prefix = '') |
||
| 131 | { |
||
| 132 | if ($prefix === '' && function_exists('config')) { |
||
| 133 | $prefix = config()->offsetGet('cookie')[ 'prefix' ]; |
||
| 134 | } |
||
| 135 | |||
| 136 | if (isset($_COOKIE[ $prefix . $name ])) { |
||
| 137 | unset($_COOKIE[ $prefix . $name ]); |
||
| 138 | } |
||
| 139 | |||
| 140 | return set_cookie($name, '', -1, $domain, $path, $prefix); |
||
| 141 | } |
||
| 142 | } |