-
Notifications
You must be signed in to change notification settings - Fork 4
/
IpLog.php
250 lines (213 loc) · 7.88 KB
/
IpLog.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
<?php
namespace Sunlight;
use Sunlight\Database\Database as DB;
abstract class IpLog
{
/**
* IP log entry for failed login attempt
*
* var: none
*/
const FAILED_LOGIN_ATTEMPT = 1;
/**
* IP log entry for article view counter cooldown
*
* var: article ID
*/
const ARTICLE_VIEW = 2;
/**
* IP log entry for article rating cooldown
*
* var: article ID
*/
const ARTICLE_RATED = 3;
/**
* IP log entry for poll vote cooldown
*
* var: poll ID
*/
const POLL_VOTE = 4;
/**
* IP log entry anti-spam cooldown
*
* var: none
*/
const ANTI_SPAM = 5;
/**
* IP log entry for failed account activation attempt
*
* var: none
*/
const FAILED_ACCOUNT_ACTIVATION = 6;
/**
* IP log entry for password reset request
*/
const PASSWORD_RESET_REQUESTED = 7;
/**
* Check IP address log
*
* @param int $type entry type, see class constants
* @param int|null $var variable argument (depends on type)
* @param int|null $expires expiration time (for custom types)
*/
static function check(int $type, ?int $var = null, ?int $expires = null): bool
{
// clean IP log
static $cleaned = [
'system' => false,
'custom' => [],
];
if ($type <= self::PASSWORD_RESET_REQUESTED) {
if (!$cleaned['system']) {
DB::query(
'DELETE FROM ' . DB::table('iplog')
. ' WHERE (type=' . self::FAILED_LOGIN_ATTEMPT . ' AND ' . time() . '-time>' . Settings::get('maxloginexpire') . ')'
. ' OR (type=' . self::ARTICLE_VIEW . ' AND ' . time() . '-time>' . Settings::get('artviewexpire') . ')'
. ' OR (type=' . self::ARTICLE_RATED . ' AND ' . time() . '-time>' . Settings::get('artrateexpire') . ')'
. ' OR (type=' . self::POLL_VOTE . ' AND ' . time() . '-time>' . Settings::get('pollvoteexpire') . ')'
. ' OR (type=' . self::ANTI_SPAM . ' AND ' . time() . '-time>' . Settings::get('antispamtimeout') . ')'
. ' OR (type=' . self::FAILED_ACCOUNT_ACTIVATION . ' AND ' . time() . '-time>' . Settings::get('accactexpire') . ')'
. ' OR (type=' . self::PASSWORD_RESET_REQUESTED . ' AND ' . time() . '-time>' . Settings::get('lostpassexpire') . ')'
);
$cleaned['system'] = true;
}
} elseif (!isset($cleaned['custom'][$type])) {
if ($expires === null) {
throw new \InvalidArgumentException('The "expires" argument must be specified for custom types');
}
DB::delete('iplog', 'type=' . $type .(($var !== null) ? ' AND var=' . $var : '') . ' AND ' . time() . '-time>' . $expires);
$cleaned['custom'][$type] = true;
}
// check IP log
$result = true;
$querybasic = 'SELECT * FROM ' . DB::table('iplog') . ' WHERE ip=' . DB::val(Core::getClientIp()) . ' AND type=' . $type;
switch ($type) {
case self::FAILED_LOGIN_ATTEMPT:
$query = DB::queryRow($querybasic);
if ($query !== false && $query['var'] >= Settings::get('maxloginattempts')) {
$result = false;
}
break;
case self::ARTICLE_VIEW:
case self::ARTICLE_RATED:
case self::POLL_VOTE:
$query = DB::query($querybasic . ' AND var=' . $var);
if (DB::size($query) != 0) {
$result = false;
}
break;
case self::ANTI_SPAM:
case self::PASSWORD_RESET_REQUESTED:
$query = DB::query($querybasic);
if (DB::size($query) != 0) {
$result = false;
}
break;
case self::FAILED_ACCOUNT_ACTIVATION:
$query = DB::queryRow($querybasic);
if ($query !== false && $query['var'] >= 5) {
$result = false;
}
break;
default:
$query = DB::query($querybasic . (($var !== null) ? ' AND var=' . $var : ''));
if (DB::size($query) != 0) {
$result = false;
}
break;
}
Extend::call('iplog.check', [
'type' => $type,
'var' => $var,
'result' => &$result,
]);
if ($result === false) {
Logger::debug('ip_log', 'IP log check failed', ['type' => $type, 'var' => $var]);
}
return $result;
}
/**
* Update IP log
*
* @param int $type entry type, see class constants
* @param int|null $var variable argument (depends on type)
*/
static function update(int $type, ?int $var = null): void
{
$querybasic = 'SELECT * FROM ' . DB::table('iplog') . ' WHERE ip=' . DB::val(Core::getClientIp()) . ' AND type=' . $type;
switch ($type) {
case self::FAILED_LOGIN_ATTEMPT:
$query = DB::queryRow($querybasic);
if ($query !== false) {
DB::update('iplog', 'id=' . $query['id'], ['var' => ($query['var'] + 1)]);
} else {
DB::insert('iplog', [
'ip' => Core::getClientIp(),
'type' => self::FAILED_LOGIN_ATTEMPT,
'time' => time(),
'var' => 1
]);
}
break;
case self::ARTICLE_VIEW:
DB::insert('iplog', [
'ip' => Core::getClientIp(),
'type' => self::ARTICLE_VIEW,
'time' => time(),
'var' => $var
]);
break;
case self::ARTICLE_RATED:
DB::insert('iplog', [
'ip' => Core::getClientIp(),
'type' => self::ARTICLE_RATED,
'time' => time(),
'var' => $var
]);
break;
case self::POLL_VOTE:
DB::insert('iplog', [
'ip' => Core::getClientIp(),
'type' => self::POLL_VOTE,
'time' => time(),
'var' => $var
]);
break;
case self::ANTI_SPAM:
case self::PASSWORD_RESET_REQUESTED:
DB::insert('iplog', [
'ip' => Core::getClientIp(),
'type' => $type,
'time' => time(),
'var' => 0
]);
break;
case self::FAILED_ACCOUNT_ACTIVATION:
$query = DB::queryRow($querybasic);
if ($query !== false) {
DB::update('iplog', 'id=' . $query['id'], ['var' => ($query['var'] + 1)]);
} else {
DB::insert('iplog', [
'ip' => Core::getClientIp(),
'type' => self::FAILED_ACCOUNT_ACTIVATION,
'time' => time(),
'var' => 1
]);
}
break;
default:
$query = DB::queryRow($querybasic . (($var !== null) ? ' AND var=' . $var : ''));
if ($query !== false) {
DB::update('iplog', 'id=' . $query['id'], ['time' => time()]);
} else {
DB::insert('iplog', [
'ip' => Core::getClientIp(),
'type' => $type,
'time' => time(),
'var' => $var
]);
}
break;
}
}
}