flag.cookie_storage.inc
4.25 KB
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
<?php
/**
* @file
* Contains the FlagCookieStorage class.
*/
/**
* Utility class to handle cookies.
*
* Cookies are used to record flaggings for anonymous users on cached pages.
*
* This class contains only two instance methods. Usage example:
* @code
* $storage = FlagCookieStorage::factory($flag);
* $storage->flag(145);
* $storage->unflag(17);
* @endcode
*
* You may delete all the cookies with <code>FlagCookieStorage::drop()</code>.
*/
abstract class FlagCookieStorage {
/**
* Returns the actual storage object compatible with the flag.
*/
static function factory($flag) {
if ($flag->global) {
return new FlagGlobalCookieStorage($flag);
}
else {
return new FlagNonGlobalCookieStorage($flag);
}
}
function __construct($flag) {
$this->flag = $flag;
}
/**
* "Flags" an item.
*
* It just records this fact in a cookie.
*/
abstract function flag($entity_id);
/**
* "Unflags" an item.
*
* It just records this fact in a cookie.
*/
abstract function unflag($entity_id);
/**
* Deletes all the cookies.
*
* (Etymology: "drop" as in "drop database".)
*/
static function drop() {
FlagGlobalCookieStorage::drop();
FlagNonGlobalCookieStorage::drop();
}
}
/**
* Storage handler for global flags.
*/
class FlagGlobalCookieStorage extends FlagCookieStorage {
function flag($entity_id) {
$cookie_key = $this->cookie_key($entity_id);
setcookie($cookie_key, 1, REQUEST_TIME + $this->get_lifetime(), base_path());
$_COOKIE[$cookie_key] = 1;
}
function unflag($entity_id) {
$cookie_key = $this->cookie_key($entity_id);
setcookie($cookie_key, 0, REQUEST_TIME + $this->get_lifetime(), base_path());
$_COOKIE[$cookie_key] = 0;
}
// Global flags persist for the length of the minimum cache lifetime.
protected function get_lifetime() {
$cookie_lifetime = variable_get('cache', 0) ? variable_get('cache_lifetime', 0) : -1;
// Do not let the cookie lifetime be 0 (which is the no cache limit on
// anonymous page caching), since it would expire immediately. Usually
// the no cache limit means caches are cleared on cron, which usually runs
// at least once an hour.
if ($cookie_lifetime == 0) {
$cookie_lifetime = 3600;
}
return $cookie_lifetime;
}
protected function cookie_key($entity_id) {
return 'flag_global_' . $this->flag->name . '_' . $entity_id;
}
/**
* Deletes all the global cookies.
*/
static function drop() {
foreach ($_COOKIE as $key => $value) {
if (strpos($key, 'flag_global_') === 0) {
setcookie($key, FALSE, 0, base_path());
unset($_COOKIE[$key]);
}
}
}
}
/**
* Storage handler for non-global flags.
*/
class FlagNonGlobalCookieStorage extends FlagCookieStorage {
// The anonymous per-user flaggings are stored in a single cookie, so that
// all of them persist as long as the Drupal cookie lifetime.
function __construct($flag) {
parent::__construct($flag);
$this->flaggings = isset($_COOKIE['flags']) ? explode(' ', $_COOKIE['flags']) : array();
}
function flag($entity_id) {
if (!$this->is_flagged($entity_id)) {
$this->flaggings[] = $this->cookie_key($entity_id);
$this->write();
}
}
function unflag($entity_id) {
if (($index = $this->index_of($entity_id)) !== FALSE) {
unset($this->flaggings[$index]);
$this->write();
}
}
protected function get_lifetime() {
return min((int) ini_get('session.cookie_lifetime'), (int) ini_get('session.gc_maxlifetime'));
}
protected function cookie_key($entity_id) {
return $this->flag->name . '_' . $entity_id;
}
protected function write() {
$serialized = implode(' ', array_filter($this->flaggings));
setcookie('flags', $serialized, REQUEST_TIME + $this->get_lifetime(), base_path());
$_COOKIE['flags'] = $serialized;
}
protected function is_flagged($entity_id) {
return $this->index_of($entity_id) !== FALSE;
}
protected function index_of($entity_id) {
return array_search($this->cookie_key($entity_id), $this->flaggings);
}
/**
* Deletes the cookie.
*/
static function drop() {
if (isset($_COOKIE['flags'])) {
setcookie('flags', FALSE, 0, base_path());
unset($_COOKIE['flags']);
}
}
}