-
Notifications
You must be signed in to change notification settings - Fork 3
/
oui_cookie.php
297 lines (239 loc) · 7.87 KB
/
oui_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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
/*
* This file is part of oui_cookie,
* a plugin allowing easy cookies management in Textpattern CMS.
*
* https://github.com/NicolasGraph/oui_cookie
*
* Copyright (C) 2018 Nicolas Morand
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA..
*/
namespace Oui {
class cookie {
/**
* Cookie name.
*
* @var string
* @see setName(), getName().
*/
protected $name;
/**
* Cookie related GET/POST parameter.
*
* @var string
* @see setParam(), getParam().
*/
protected $param;
/**
* In process cookie setting value.
*
* @var array The provided value as a string or FALSE.
* @see setProcessing(), getProcessing().
*/
protected static $processing = array();
/**
* Cookie value set.
*
* @var string
* @see setCookie(), getCookie().
*/
protected $cookie;
/**
* $name property setter.
*
* @return object $this.
*/
protected function setName($value) {
$this->name = $value;
return $this;
}
/**
* $name property getter.
*
* @return string $this->name.
*/
protected function getName() {
return $this->name;
}
/**
* $param property setter.
*
* @return object $this.
*/
protected function setParam() {
$this->param = strval(gps($this->getName()));
return $this;
}
/**
* $param property getter.
*
* @return string $this->param.
*/
protected function getParam() {
$this->param or $this->setParam();
return $this->param;
}
/**
* $processing property setter.
*
* @return object $this.
*/
protected function setProcessing($value) {
static::$processing[$this->getName()] = $value;
return $this;
}
/**
* $processing property getter.
*
* @return mixed
*/
protected function getProcessing() {
if (array_key_exists($this->getName(), static::$processing)) {
return static::$processing[$this->getName()];
}
return null;
}
/**
* $cookie property setter.
*
* @return object $this.
*/
protected function setCookie() {
$this->cookie = cs($this->getName());
return $this;
}
/**
* $cookie property getter.
*
* @return mixed $this->cookie
*/
protected function getCookie() {
$this->cookie or $this->setCookie();
return $this->cookie;
}
/**
* Delete a cookie.
* Set the related $processing property value to false.
*
* @return bool TRUE if the cookie unsetting succeeded.
*/
protected function delete() {
$this->setProcessing(false);
return setcookie($this->getName(), '', -1, '/');
}
/**
* Set a cookie.
* Set the related $processing property value.
*
* @return bool TRUE if the cookie setting succeeded.
*/
public function set($value, $duration) {
$name = $this->getName();
setcookie($name, $value, strtotime($duration), '/', null, false, true);
$this->setProcessing($value);
return setcookie($name, $value, strtotime($duration), '/', null, false, true);
}
/**
* Check the status/value of a defined cookie name against its cookie/URL-parameter related value.
*
* @param string $value A value to check against the set one;
* @return bool TRUE if the defined cookie is set or in setting
* and, optionally, if its value match the one provided.
*/
public function isSet($value = null) {
$cs = $this->getCookie();
$processing = $this->getProcessing();
$out = false;
if ($processing === false) {
$out = false;
} elseif ($processing) {
$out = $value ? ($value === $processing ? true : false) : true;
} elseif ($cs) {
$out = $value ? ($value === $cs ? true : false) : true;
}
return $out;
}
/**
* oui_cookie callback method.
* Set, read or delete a cookie, manually or from a GET/POST parameter.
*
* @param array $atts Attributes in use;
* @param string $thing Tag content when used as a container.
* @return string The cookie value on reading.
*/
public static function renderCookie($atts, $thing = null)
{
$instance = \Txp::get('Oui\Cookie');
extract(lAtts(array(
'name' => '',
'value' => '',
'values' => '',
'default' => '',
'duration' => '+1 day',
'delete' => '',
), $atts));
if ($name) {
$cs = $instance->setName($name)->getCookie();
$gps = $instance->getParam();
} else {
trigger_error('oui_cookie requires an "name" attribute.');
return;
}
if ($thing) {
$value = parse($thing);
} elseif ($values && in_list($gps, $values, $delim = ',')) {
$value = $gps;
} elseif ($default) {
$value = $default;
}
if ($value) {
$instance->set($value, $duration);
} elseif ($delete && !$values || $values && $gps && $gps === $delete) {
$instance->delete();
} elseif (!$values) {
$processing = $instance->getProcessing();
return $cs && $processing !== false ? $cs : $processing;
}
}
/**
* oui_if_cookie callback method.
* Switch between two defined contents/behaviours depending on the $isSet() result.
*
* @param array $atts Attributes in use;
* @param string $thing Tag content.
* @return mixed The default contents/behaviour if $isSet returns TRUE, the else part otherwise.
*/
public static function renderIfCookie($atts, $thing = null)
{
$instance = \Txp::get('Oui\Cookie');
extract(lAtts(array(
'name' => '',
'value' => '',
), $atts));
if ($name) {
$instance->setName($name);
} else {
trigger_error('oui_cookie requires a name attribute.');
return;
}
return parse($thing, $instance->isSet($value));
}
}
}
namespace {
Txp::get('\Textpattern\Tag\Registry')
->register('Oui\Cookie::renderCookie', 'oui_cookie')
->register('Oui\Cookie::renderIfCookie', 'oui_if_cookie');
}