forked from anvyst/domain_blocker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDomainBlocker.php
344 lines (274 loc) · 12.9 KB
/
DomainBlocker.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
* DomainBlocker class
* Main class for domain blacklist addon
* */
require_once dirname(dirname(dirname(dirname(__FILE__)))).'/init.php';
class DomainBlocker {
protected static $table_settings = 'mod_domain_blocker';
protected static $table_domains = 'mod_domains_blocked';
protected function __construct() { /* prevents creating new instance of Singleton */ }
private function __clone() { /* prevents cloning of Singleton */ }
private function __wakeup() { /* prevents unserializing of Singleton */ }
/**
* And that's where all the fun starts!
* @return Signleton $instance
*/
public static function getInstance() {
static $instance = null;
if (null == $instance) {
$instance = new static();
}
return $instance;
}
/**
* getBlockStrings method
* @param Array $options
* @return Array $data
* */
public static function getBlockStrings($options = array()) {
$data = array();
$sql = "SELECT * FROM ".self::$table_settings." ORDER BY `pattern` ASC";
$records = full_query($sql);
while( $row = mysql_fetch_assoc($records) ) {
array_push($data, $row);
}
return $data;
}
/**
* checkDomain method
* @param Array $data - containing domain
* @return Mixed $result - containing info on domain validation
* */
public static function checkDomain($data = array()) {
$result = array('status' => 0, 'msg' => array());
$sql = "SELECT id, pattern FROM ".self::$table_settings." WHERE activated=1 ORDER BY id ASC";
$res = full_query($sql);
$patterns = array();
while( $row = mysql_fetch_assoc($res) ) {
array_push($patterns, $row);
}
if( !empty($patterns) ) {
foreach($patterns as $k => $pattern) {
$position = strpos($data['sld'], $pattern['pattern']);
if( $position !== false ) {
$result['status'] = 1;
break;
}
}
}
if( $result['status'] == 1 ) {
$data = array(
'domain' => mysql_real_escape_string("{$data['sld']}{$data['tld']}"),
'ipaddress' => $_SERVER['REMOTE_ADDR'],
'description' => "Domain contains blocked words",
'attempts' => '1',
'created' => date('Y-m-d H:i:s', time()),
);
$res = full_query("SELECT * FROM ".self::$table_domains." WHERE domain='{$data['domain']}' AND ipaddress='{$data['ipaddress']}' LIMIT 1");
$record = mysql_fetch_assoc($res);
if( !empty($record) ) {
update_query(
self::$table_domains,
array('attempts' => ++$record['attempts']),
array('id' => $record['id'])
);
} else {
insert_query(self::$table_domains, $data);
}
$result['msg'][] = "Domain contains \"offensive\" words.";
}
return $result;
}
/**
* getBlockedDomains method
* @param Array $options
* @return Array $data
* */
public static function getBlockedDomains($options = array()) {
$data = array();
$records = full_query("SELECT * FROM ".self::$table_domains." ORDER BY domain ASC");
while( $row = mysql_fetch_assoc($records) ) {
array_push($data, $row);
}
return $data;
}
/**
* getStyles method
* @return String $files with all
* */
public static function getStyles() {
$files[] = '<script src="../modules/addons/domain_blocker/js/domain_blocker.js"></script>';
$files[] = '<script src="../modules/addons/domain_blocker/bootstrap/js/bootstrap.min.js"></script>';
$files[] = '<link href="../modules/addons/domain_blocker/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">';
$files[] = '<link href="../modules/addons/domain_blocker/css/style.css" rel="stylesheet" media="screen">';
return join("\n", $files);
}
/**
* dispatchActions method
* Main dispatcher method for handling all the AJAX/POST
* method
* @return Mixed $result containing response JSON/Html
* */
public static function dispatchActions() {
if( isset($_POST['form_submit']) && !empty($_POST['data']) ) {
$result = self::savePatternForm($_POST);
$_SESSION['domain_blocker'] = json_decode($result, true);
header('location: ' . $_SERVER['HTTP_REFERER']);
exit;
}
if( !empty($_POST['action']) && in_array($_POST['action'], array('edit_pattern','add_pattern')) ) {
return self::getEditPatternForm($_POST);
}
if( !empty($_POST['action']) && in_array($_POST['action'], array('delete_pattern')) ) {
$result = self::removePattern($_POST);
}
if( !empty($_POST['action']) && in_array($_POST['action'], array('activate_pattern')) ) {
$result = self::activatePattern($_POST);
}
// got the session into the front-end
if( isset($result) && !empty($result) ) {
$_SESSION['domain_blocker'] = json_decode($result, true);
return $result;
}
}
/**
* removePattern function
* @param Array $postdata - post data
* @return JSON $result - containing error/msg data
* */
public static function removePattern($postdata = array()) {
$result = array('status' => 0, 'msg' => array());
$deleted = full_query("DELETE FROM ".self::$table_settings." WHERE id='".mysql_real_escape_string($postdata['id'])."'");
if( $deleted ) {
$result['status'] = 1;
$result['msg'][] = "Pattern was successfully removed";
} else {
$result['msg'][] = "Couldn't delete Pattern, please check the code";
}
return json_encode($result);
}
/**
* activatePattern method
* @param Array $postdata - for ids being activated
* @return Array $result - on method success/failure
* */
public static function activatePattern($postdata = array()) {
$result = array('status' => 0, 'msg' => array());
if( !empty($postdata['ids']) ) {
$sql = "UPDATE ".self::$table_settings." SET activated=1 WHERE id IN(".mysql_real_escape_string(implode(',', $postdata['ids'])).")";
$updated = full_query($sql);
if( $updated ) {
$result['status'] = 1;
$result['msg'][] = "Successfully activated patterns";
} else {
$result['msg'][] = "Couldn't activate the patterns";
}
}
return json_encode($result);
}
/**
* savePatternForm function
* @param Array $postdata - containing all require posts
* return JSON $result - with success/failure on saving the record
* */
public static function savePatternForm($postdata = array()) {
$data = array();
$result = array('status' => 0, 'msg' => array());
if( !empty($postdata) ) {
$data = array(
'pattern' => mysql_real_escape_string($postdata['data']['pattern']),
'pattern_type' => mysql_real_escape_string($postdata['data']['pattern_type']),
'activated' => ( isset($postdata['data']['activated']) ? 1 : 0 ),
'created' => date("Y-m-d H:i:s", time()),
);
if( !isset($postdata['data']['id']) ) {
$pattern_id = insert_query( self::$table_settings, $data );
} else {
$data['id'] = $pattern_id = $postdata['data']['id'];
update_query(self::$table_settings, $data, array('id' => $pattern_id));
}
}
if( !empty($pattern_id) ) {
$result['status'] = $pattern_id;
$result['msg'][] = "Pattern #$pattern_id was successfully saved";
} else {
$result['msg'][] = "Couldn't save Pattern record";
}
return json_encode($result);
}
/**
* getEditPatternForm function
* @param Array $data containing post fields optionally
* @return String $content with the modal form
* */
public static function getEditPatternForm($data = array()) {
$content = array();
$record = array();
if( !empty($data['id']) ) {
$resource = full_query("SELECT * FROM ".self::$table_settings." WHERE id='".mysql_real_escape_string($data['id'])."'");
$pattern = mysql_fetch_assoc($resource);
if( !empty($pattern) ) {
$record = $pattern;
}
}
$content[] = "<form method='post' class='form-horizontal'>";
$content[] = "<div class='modal-body'>";
$content[] = "<input type='hidden' name='module' value='domain_blocker'>";
$content[] = "<input type='hidden' name='action' value='{$_POST['action']}'>";
if( !empty($record['id']) ) {
$content[] = "<input type='hidden' name='data[id]' value='{$record['id']}'/>";
}
$content[] = "<div class='control-group'><div class='controls'><input type='text' name='data[pattern]' ".(!empty($record['pattern']) ? "value='{$record['pattern']}'" : '')."/></div></div>";
$content[] = "<div class='control-group'><div class='controls'><select name='data[pattern_type]'>";
$content[] = "<option value=\"\">Choose Pattern Type</option>";
$content[] = "<option value='string' ".(($record['pattern_type'] == 'string') ? 'selected' : '').">String</option>";
$content[] = "<option value='regexp' ".(($record['pattern_type'] == 'regexp') ? 'selected' : '').">Regular Expression</option>";
$content[] = "</select></div></div>";
$content[] = "</div>";
$content[] = "<div class='control-group'><Div class='controls'>";
$content[] = "<label class='checkbox'><input type='checkbox' name='data[activated]' ".(($record['activated'] == 1) ? 'checked' : '')."/>Activated</label>";
$content[] = "</div></div>";
$content[] = "<div class='modal-footer'>";
$content[] = "<a href=\"javascript:void(0);\" class=\"btn\" data-dismiss=\"modal\">Close</a>";
$content[] = "<input type='submit' name='form_submit' value='Save' class=\"btn btn-primary\">";
$content[] = "</div>";
$content[] = "</form>";
return join("\n", $content);
}
/**
* activate method
* @return Array $result - checking if tables were properly created
* */
public static function activate() {
$result = array('status' => 0, 'msg' => array());
$sql[self::$table_settings] = "CREATE TABLE `mod_domain_blocker` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`pattern` varchar(200) NOT NULL, `pattern_type` enum('regexp','string') NOT NULL,`activated` tinyint(4) DEFAULT '1',`created` datetime DEFAULT NULL,`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `idx_pattern` (`pattern`),KEY `idx_activated` (`activated`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
$sql[self::$table_domains] = "CREATE TABLE `mod_domains_blocked` (`id` bigint(20) NOT NULL AUTO_INCREMENT, `ipaddress` varchar(100) DEFAULT NULL, `domain` varchar(200) DEFAULT NULL,`attempts` int(11) DEFAULT '0',`description` mediumtext, `created` datetime DEFAULT NULL, `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `idx_domain` (`domain`), KEY `idx_ipaddress` (`ipaddress`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
foreach( $sql as $table_name => $qry ) {
$result = mysql_query($qry);
if( !$result ) {
$result['status'] = 1;
$result['msg'][] = "Table '$table_name' wasn't properly created. Try deactivating the plugin and activate it again";
}
}
return $result;
}
/**
* deactivate method
* @return Array $result - on droping all module tables
* */
public static function deactivate() {
$result = array('status' => 0, 'msg' => array());
$tables = array( self::$table_settings, self::$table_domains );
foreach( $tables as $table_name ) {
$sql = "DROP TABLE `$table_name`";
$result = mysql_query($sql);
if( !$result ) {
$result['status'] = 1;
$result['msg'][] = "Couldn't drop table '$table_name'";
}
}
return $result;
}
}
?>