-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCookie.php
268 lines (241 loc) · 6.77 KB
/
Cookie.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
declare(strict_types=1);
namespace Tamedevelopers\Support;
use Tamedevelopers\Support\Time;
class Cookie{
/**
* Site name
* @var string
*/
static protected $name;
/**
* Expire Cookie name
* @var string
*/
static protected $expireName;
/**
* Time Cookie name
* @var string
*/
static protected $timeName;
/**
* Time format
* @var string
*/
static protected $timeFormat;
/**
* Expire time format
* @var mixed
*/
static protected $expireFormat;
/**
* Create Sitename From .env
*
* @return $this
*/
static protected function init()
{
self::$name = strtolower(str_replace([' '], '', env('APP_NAME', '')));
self::$timeName = "__time_" . self::$name;
self::$expireName = "__expire_" . self::$name;
self::$timeFormat = Time::timestamp('next year', 'Y-m-d');
self::$expireFormat = Time::timestamp('last year', 'Y-m-d');
return new self();
}
/**
* Create Cookie
* @param string $name
* - Cookie Name
*
* @param string|null $value
* - Cookie Value
*
* @param int|string $minutes
* [optional] The time the cookie expires.
* This is a Unix timestamp so is in number of seconds since the epoch.
* In other words, you'll most likely set this with the time function plus the number of seconds before you want it to expire.
* Or you might use mktime. time()+606024*30 will set the cookie to expire in 30 days.
* If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).
*
* @param string|null $path
* [optional] The path on the server in which the cookie will be available on.
* If set to '/', the cookie will be available within the entire domain.
*
* @param string|null $domain
* [optional] The domain that the cookie is available.
* To make the cookie available on all subdomains of example.com then you'd set it to '.example.com'.
*
* @param bool|null $secure
* [optional] Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client.
*
* @param bool|null $httponly
* [optional] When true the cookie will be made accessible only through the HTTP protocol.
*
* @return void
* If output exists prior to calling this function, setcookie will fail and return false.
* If setcookie successfully runs, it will return true.
* This does not indicate whether the user accepted the cookie.
*/
static public function set($name, $value = null, $minutes = 0, $path = null, $domain = null, $secure = null, $httponly = null, $force = null)
{
// minutes
$minutes = self::minutesToExpire($minutes);
// create default values
[$path, $value, $domain, $secure, $httponly, $force] = self::getDefaultPathAndDomain(
$path, $value, $domain, $secure, $httponly, $force
);
// set cookie
if ( !headers_sent() || $force === true) {
@setcookie($name, $value, $minutes, $path, $domain, $secure, $httponly);
}
}
/**
* Expire the given cookie.
*
* @param string $name
* @param string|null $path
* @param string|null $domain
* @return void
*/
static public function forget($name, $path = null, $domain = null)
{
self::set(
name: $name,
minutes: 'last year',
path: $path,
domain: $domain,
force: true
);
}
/**
* Expire the given cookie.
*
* @param string $name
* @param string|null $path
* @param string $domain
* @return void
*/
static public function expire($name, $path = null, $domain = null)
{
self::forget($name, $path, $domain);
}
/**
* Set Cookie Time
*
* @return void
*/
static public function setTime()
{
self::init()
->set(self::$timeName, self::$timeFormat);
}
/**
* Set Cookie Expiration Time
*
* @return void
*/
static public function setExpire()
{
self::init()
->set(self::$expireName, self::$expireFormat);
}
/**
* Get Time Data
*
* @return mixed
*/
static public function getTime()
{
return self::init()->get(self::$timeName);
}
/**
* Get Expire Time Data
*
* @return mixed
*/
static public function getExpire()
{
return self::init()->get(self::$expireName);
}
/**
* Cookie has name that exists
*
* @param string $name
* - Cookie name
*
* @return bool
*/
static public function has($name = null)
{
return isset($_COOKIE[(string) $name]);
}
/**
* Get cookie
* @param string $name
* - Cookie name
*
* @return mixed
*/
static public function get($name = null)
{
return self::has($name)
? $_COOKIE[(string) $name]
: null;
}
/**
* Get all cookie
* @param string $name
*
* [optional] Cookiename or return all cookies
*
* @return mixed
*/
static public function all($name = null)
{
return self::get($name) ?? $_COOKIE;
}
/**
* Set minutes
*
* @param int|string $minutes
* @return int
*/
static private function minutesToExpire($minutes = 0)
{
// options
if(empty($minutes)){
$minutes = 0;
} elseif(is_numeric($minutes)){
$minutes = time() + (((int) $minutes) * 60);
} else{
$minutes = strtotime($minutes);
if ($minutes === false || $minutes === -1) {
// Invalid timestamp format, default to 0 (end of session)
return 0;
}
}
return (int) $minutes;
}
/**
* Get the path and domain, or the default values.
*
* @param string|null $path
* @param string|null $value
* @param string|null $domain
* @param bool|null $secure
* @param bool|null $httponly
* @param bool|null $force
* @return array
*/
static private function getDefaultPathAndDomain($path = null, $value = null, $domain = null, $secure = null, $httponly = null, $force = null)
{
return [
!empty($path) ? $path : '/',
!empty($value) ? $value : '',
!empty($domain) ? $domain : '',
is_bool($secure) ? $secure : false,
is_bool($httponly) ? $httponly : false,
is_bool($force) ? $force : false,
];
}
}