cookie.php
145 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikWP - Libraries |
| 4 | * @subpackage adapter.input |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | JLoader::import('adapter.input.input'); |
| 15 | |
| 16 | /** |
| 17 | * This is an abstracted input class used to handle retrieving data |
| 18 | * from the COOKIE environment. |
| 19 | * |
| 20 | * @since 10.0 |
| 21 | * |
| 22 | * @property-read Input $get |
| 23 | * @property-read Input $post |
| 24 | * @property-read Input $request |
| 25 | * @property-read Input $server |
| 26 | * @property-read Files $files |
| 27 | * @property-read Cookie $cookie |
| 28 | * |
| 29 | * @method integer getInt($name, $default = null) Get a signed integer. |
| 30 | * @method integer getUint($name, $default = null) Get an unsigned integer. |
| 31 | * @method float getFloat($name, $default = null) Get a floating-point number. |
| 32 | * @method boolean getBool($name, $default = null) Get a boolean value. |
| 33 | * @method string getWord($name, $default = null) Get a word. |
| 34 | * @method string getAlnum($name, $default = null) Get an alphanumeric string. |
| 35 | * @method string getCmd($name, $default = null) Get a CMD filtered string. |
| 36 | * @method string getBase64($name, $default = null) Get a base64 encoded string. |
| 37 | * @method string getString($name, $default = null) Get a string. |
| 38 | * @method string getHtml($name, $default = null) Get a HTML string. |
| 39 | * @method string getPath($name, $default = null) Get a file path. |
| 40 | * @method string getUsername($name, $default = null) Get a username. |
| 41 | */ |
| 42 | class JInputCookie extends JInput |
| 43 | { |
| 44 | /** |
| 45 | * Class constructor. |
| 46 | * |
| 47 | * @param array $source Optional source data. If omitted, a copy of the server variable '_REQUEST' is used. |
| 48 | * @param array $options An optional associative array of configuration parameters. |
| 49 | */ |
| 50 | public function __construct($source = null, array $options = array()) |
| 51 | { |
| 52 | parent::__construct($_COOKIE, $options); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Sets a value |
| 57 | * |
| 58 | * @param string $name Name of the value to set. |
| 59 | * @param mixed $value Value to assign to the input. |
| 60 | * @param array $options An associative array which may have any of the keys expires, path, domain, |
| 61 | * secure, httponly and samesite. The values have the same meaning as described |
| 62 | * for the parameters with the same name. The value of the samesite element |
| 63 | * should be either Lax or Strict. If any of the allowed options are not given, |
| 64 | * their default values are the same as the default values of the explicit |
| 65 | * parameters. If the samesite element is omitted, no SameSite cookie attribute |
| 66 | * is set. |
| 67 | * |
| 68 | * @return void |
| 69 | * |
| 70 | * @link https://www.ietf.org/rfc/rfc2109.txt |
| 71 | * @link https://php.net/manual/en/function.setcookie.php |
| 72 | * |
| 73 | * @since 10.1.30 Changed parameters signature. |
| 74 | */ |
| 75 | public function set($name, $value, $options = array()) |
| 76 | { |
| 77 | // BC layer to convert old method parameters |
| 78 | if (is_array($options) === false) |
| 79 | { |
| 80 | $argList = func_get_args(); |
| 81 | |
| 82 | $options = array( |
| 83 | 'expires' => isset($argList[2]) === true ? $argList[2] : 0, |
| 84 | 'path' => isset($argList[3]) === true ? $argList[3] : '', |
| 85 | 'domain' => isset($argList[4]) === true ? $argList[4] : '', |
| 86 | 'secure' => isset($argList[5]) === true ? $argList[5] : false, |
| 87 | 'httponly' => isset($argList[6]) === true ? $argList[6] : false, |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | if (!headers_sent()) |
| 92 | { |
| 93 | // set the cookie |
| 94 | if (version_compare(PHP_VERSION, '7.3', '>=')) |
| 95 | { |
| 96 | setcookie($name, $value, $options); |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | // using the setcookie function before php 7.3, make sure we have default values |
| 101 | if (array_key_exists('expires', $options) === false) |
| 102 | { |
| 103 | $options['expires'] = 0; |
| 104 | } |
| 105 | |
| 106 | if (array_key_exists('path', $options) === false) |
| 107 | { |
| 108 | $options['path'] = ''; |
| 109 | } |
| 110 | |
| 111 | if (array_key_exists('domain', $options) === false) |
| 112 | { |
| 113 | $options['domain'] = ''; |
| 114 | } |
| 115 | |
| 116 | if (array_key_exists('secure', $options) === false) |
| 117 | { |
| 118 | $options['secure'] = false; |
| 119 | } |
| 120 | |
| 121 | if (array_key_exists('httponly', $options) === false) |
| 122 | { |
| 123 | $options['httponly'] = false; |
| 124 | } |
| 125 | |
| 126 | setcookie($name, $value, $options['expires'], $options['path'], $options['domain'], $options['secure'], $options['httponly']); |
| 127 | } |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | // headers already sent, register cookie via javascript |
| 132 | $js = 'document.cookie="' . $name . '=' . $value |
| 133 | . (!empty($options['expires']) ? '; expires=' . date('r', $expire) : '') |
| 134 | . (!empty($options['path']) ? '; path=' . $path : '') |
| 135 | . (!empty($options['samesite']) ? '; SameSite=' . $options['samesite'] : '') |
| 136 | . (!empty($options['secure']) ? '; Secure' : ''); |
| 137 | |
| 138 | // register script |
| 139 | JFactory::getDocument()->addScriptDeclaration($js); |
| 140 | } |
| 141 | |
| 142 | $this->data[$name] = $value; |
| 143 | } |
| 144 | } |
| 145 |