-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCookie.php
146 lines (115 loc) · 4.24 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
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <[email protected]>
* License: https://github.com/quidphp/base/blob/master/LICENSE
*/
namespace Quid\Base;
// cookie
// class with static methods to add and remove cookies
final class Cookie extends Root
{
// config
protected static array $config = [
'lifetime'=>3600, // durée de vie, 0 signifie fermeture du browser, a priorité sur expire car le timestamp courant est ajouté
'expires'=>null, // expiration, 0 signifie fermeture du browser, le timestamp n'est pas additionné, a priorité sur lifetime
'path'=>'/', // chemin dans le domaine
'domain'=>'', // ce paramètre est étrange, le plus strict est de laisser domain comme chaîne vide
'secure'=>null, // cookie doit être servis via https
'httponly'=>true, // cookie ne peut pas être modifié dans javaScript
'samesite'=>'Lax' // une requête post externe termine la session
];
// is
// retoune vrai si le cookie existe
final public static function is($name):bool
{
return Superglobal::cookieExists($name);
}
// get
// retourne la valeur du cookie
final public static function get(string $name):?string
{
return Superglobal::getCookie($name);
}
// set
// crée ou met à jour un cookie
// si global est true, le cookie est ajouté dans la superglobale cookie
final public static function set(string $name,string $value,?array $option=null):bool
{
$return = false;
$option = self::option('set',$option);
if(!empty($option) && !Response::areHeadersSent())
$return = setcookie($name,$value,$option);
return $return;
}
// unset
// enlève un cookie
// si global est true, le cookie est enlevé de la superglobale cookie
final public static function unset(string $name,?array $option=null):bool
{
$return = false;
$option = self::option('unset',$option);
if(!empty($option) && !Response::areHeadersSent())
$return = setcookie($name,'',$option);
return $return;
}
// option
// prépare le tableau option pour cookie
final public static function option(string $mode,?array $option=null):array
{
$return = [];
$option = Arr::plus(self::$config,$option);
if(in_array($mode,['set','unset','cookieParams'],true))
{
$time = Datetime::time();
$return = $option;
// expire et lifetime set
if(in_array($mode,['set','cookieParams'],true))
{
if(is_int($return['expires']))
{
if($return['expires'] > $time)
$return['lifetime'] = $return['expires'] - $time;
else
$return['lifetime'] = 0;
}
elseif(is_int($return['lifetime']))
$return['expires'] = $time + $return['lifetime'];
if(!is_int($return['expires']))
$return['expires'] = 0;
if(!is_int($return['lifetime']))
$return['lifetime'] = 0;
if($mode === 'set')
unset($return['lifetime']);
else
unset($return['expires']);
}
// expire et lifetime unset
elseif($mode === 'unset')
{
$return['expires'] = $time - 3600;
unset($return['lifetime']);
}
// path
if(!is_string($return['path']))
$return['path'] = '/';
// domain
if($return['domain'] === true)
$return['domain'] = Request::host();
if(!is_string($return['domain']))
$return['domain'] = '';
// secure
if(!is_bool($return['secure']))
$return['secure'] = Request::isSsl();
// httponly
if(!is_bool($return['httponly']))
$return['httponly'] = true;
// samesite
if(!in_array($return['samesite'],['Lax','Strict'],true))
$return['samesite'] = '';
}
return $return;
}
}
?>