-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
168 lines (150 loc) · 5.02 KB
/
Copy pathapi.php
File metadata and controls
168 lines (150 loc) · 5.02 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
<?php
global $PHORUM;
/**
* The maximum number of entries to store in the ip_to_offset cache.
*/
define('AUTOMATIC_TIMEZONES_IP_CACHE_SIZE', 100);
/**
* The maximum time (in minutes) to cache the detected timezones.
*/
define('AUTOMATIC_TIMEZONES_CACHE_TIME', 24 * 60);
/**
* The maximum time (in minutes) to cache the detected timezones when
* using IP-based caching (smaller value than the general cache time,
* just in case we are dealing with roaming / dynamic IP addresses
* that cross time zones ... fat chance really ;-)
*/
define('AUTOMATIC_TIMEZONES_IP_CACHE_TIME', 2 * 60);
// Initialize the module settings data array.
if (empty($PHORUM["mod_automatic_timezones"])) {
$PHORUM["mod_automatic_timezones"] = array();
}
if (empty($PHORUM['mod_automatic_timezones']['offset_by_ip'])) {
$PHORUM['mod_automatic_timezones']['offset_by_ip'] = array();
}
if (!isset($PHORUM['mod_automatic_timezones']['allow_user_override'])) {
$PHORUM['mod_automatic_timezones']['allow_user_override'] = TRUE;
}
/**
* Store the TZ offset for the current visitor.
*
* @param float $offset
* The offset in hours.
*/
function automatic_timezones_set_offset($offset)
{
global $PHORUM;
settype($offset, 'float');
// When a cookie is set, then we can use that one for tracking the
// timezone offset. There is no need to use another local store
// in such case.
if (isset($_COOKIE['auto_tz_offset']))
{
// To make sure that the offset matches the $offset parameter,
// we (re)set the cookie here. This should always match the
// existing cookie.
setcookie(
'auto_tz_offset', $offset,
time() + AUTOMATIC_TIMEZONES_CACHE_TIME * 60
);
}
// For authenticated users, we store the offset in the
// settings data for the user.
elseif ($PHORUM['user']['user_id'])
{
phorum_api_user_save_settings(array(
'automatic_timezone' => array($offset, time())
));
}
// For anonymous users, we store the offset based on the IP address
// in the Phorum settings table.
else
{
// Do garbage collection on the IP cache, which prevents the
// stored data from overflowing the available database storage size.
automatic_timezones_collect_garbage();
$map =& $PHORUM['mod_automatic_timezones']['offset_by_ip'];
$ip = $_SERVER['REMOTE_ADDR'];
if (!isset($map[$ip]) || $map[$ip] !== $offset)
{
$map[$ip] = array($offset, time());
phorum_db_update_settings(array(
'mod_automatic_timezones' =>
$PHORUM['mod_automatic_timezones']
));
}
}
}
/**
* Retrieve the TZ offset for the current visitor.
*
* @return array()
* An array containing two values:
* - the timezone offset
* - whether or not DST is active
* These values can be NULL when the offset is unknown.
*/
function automatic_timezones_get_offset()
{
global $PHORUM;
// If the admin has allowed users to set their timezone manually,
// then check if a specific timezone is set for the active user.
if (!empty($PHORUM['mod_automatic_timezones']['allow_user_override']))
{
if ($PHORUM['user']['user_id'])
{
if ($PHORUM['user']['tz_offset'] != -99) {
return array(
$PHORUM['user']['tz_offset'],
$PHORUM['user']['is_dst']
);
}
}
}
// Check for an offset in the module cookie.
if (isset($_COOKIE['auto_tz_offset'])) {
return array($_COOKIE['auto_tz_offset'], FALSE);
}
// Check for an offset in the user settings.
if ($PHORUM['user']['user_id'])
{
list ($offset, $time) =
phorum_api_user_get_setting('automatic_timezone');
if (($time + AUTOMATIC_TIMEZONES_CACHE_TIME * 60) > time()) {
return array($map[$ip], FALSE);
}
}
// Check for an offset in the offset_by_ip map.
$map =& $PHORUM['mod_automatic_timezones']['offset_by_ip'];
$ip = $_SERVER['REMOTE_ADDR'];
if (isset($map[$ip])) {
list ($offset, $time) = $map[$ip];
if (($time + AUTOMATIC_TIMEZONES_IP_CACHE_TIME * 60) > time()) {
return array($map[$ip], FALSE);
}
}
return array(NULL, NULL);
}
/**
* Garbage collection: cleanup entries from the offset_by_ip map.
*
* This keeps the map size limited and will prevent overflowing the
* maximum storage size in the database for the settings field.
*/
function automatic_timezones_collect_garbage()
{
$map =& $PHORUM['mod_automatic_timezones']['offset_by_ip'];
if (count($map) > AUTOMATIC_TIMEZONES_IP_CACHE_SIZE)
{
$delete = floor(count($map) - AUTOMATIC_TIMEZONES_IP_CACHE_SIZE * 0.8);
foreach ($map as $ip => $offset) {
unset($map[$ip]);
if (--$delete == 0) break;
}
phorum_db_update_settings(array(
'mod_automatic_timezones' =>
$PHORUM['mod_automatic_timezones']
));
}
}
?>