-
Notifications
You must be signed in to change notification settings - Fork 19
/
functions.inc
827 lines (692 loc) · 26.7 KB
/
functions.inc
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
<?php
// +---------------------------------------------------------------------------+
// | Spam-X Plugin 1.3 |
// +---------------------------------------------------------------------------+
// | functions.inc |
// | |
// | This file provides helper functions for the automatic plugin install. |
// +---------------------------------------------------------------------------+
// | Copyright (C) 2004-2017 by the following authors: |
// | |
// | Authors: Tom Willett - tomw AT pigstye DOT net |
// | Dirk Haun - dirk AT haun-online DOT de |
// +---------------------------------------------------------------------------+
// | |
// | 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; either version 2 |
// | of the License, or (at your option) any later version. |
// | |
// | 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
// | |
// +---------------------------------------------------------------------------+
if (stripos($_SERVER['PHP_SELF'], basename(__FILE__)) !== false) {
die ('This file can not be used on its own.');
}
// Language file include
$plugin_path = $_CONF['path'] . 'plugins/spamx/';
$langfile = $plugin_path . 'language/' . $_CONF['language'] . '.php';
if (file_exists($langfile)) {
include_once $langfile;
} else {
include_once $plugin_path . 'language/english.php';
}
Language::override(array(
'LANG_SX00', 'PLG_spamx_MESSAGE128', 'PLG_spamx_MESSAGE8',
));
// Check and see if we need to load the plugin configuration
if (!isset($_SPX_CONF['timeout'])) {
require_once $_CONF['path_system'] . 'classes/config.class.php';
$spx_config = config::get_instance();
$_SPX_CONF = $spx_config->get_config('spamx');
}
// +---------------------------------------------------------------------------+
// | Geeklog Plugin API Implementations |
// +---------------------------------------------------------------------------+
/**
* Shows the statistics for the plugin on stats.php. If $showsitestats is 1
* then we are to only print the overall stats in the 'site statistics' box
* otherwise we show the detailed stats for the plugin
*
* @param int $showSiteStats Flag to let us know which stats to get
* @return string returns formatted HTML to insert in stats page
* @see plugin_statssummary_spamx
*/
function plugin_showstats_spamx($showSiteStats)
{
global $_CONF, $_TABLES, $LANG_SX00;
$retval = '';
if (SEC_hasRights('spamx.admin')) {
// detailed stats are only visible to Spam-X admins
require_once($_CONF['path_system'] . 'lib-admin.php');
$header_arr = array(
array(
'text' => $LANG_SX00['stats_page_title'],
'field' => 'label',
'header_class' => 'stats-header-title'
),
array(
'text' => $LANG_SX00['stats_entries'],
'field' => 'stats',
'header_class' => 'stats-header-count',
'field_class' => 'stats-list-count'
),
);
$data_arr = array();
$text_arr = array(
'has_menu' => false,
'title' => $LANG_SX00['stats_headline'],
'form_url' => $_CONF['site_url'] . '/stats.php',
);
$data_arr = array(
array(
'label' => $LANG_SX00['stats_pblacklist'],
'stats' => COM_numberFormat(DB_count($_TABLES['spamx'], 'name', 'Personal')),
),
array(
'label' => $LANG_SX00['stats_ip'],
'stats' => COM_numberFormat(DB_count($_TABLES['spamx'], 'name', 'IP')),
),
array(
'label' => $LANG_SX00['stats_ipofurl'],
'stats' => COM_numberFormat(DB_count($_TABLES['spamx'], 'name', 'IPofUrl')),
),
array(
'label' => $LANG_SX00['stats_header'],
'stats' => COM_numberFormat(DB_count($_TABLES['spamx'], 'name', 'HTTPHeader')),
),
array(
'label' => $LANG_SX00['slvwhitelist'],
'stats' => COM_numberFormat(DB_count($_TABLES['spamx'], 'name', 'SLVwhitelist')),
),
);
$retval .= ADMIN_simpleList("", $header_arr, $text_arr, $data_arr);
}
return $retval;
}
/**
* New stats plugin API function for proper integration with the site stats
*
* @return array(item text, item count);
* @see plugin_showstats_spamx
*/
function plugin_statssummary_spamx()
{
global $_TABLES, $LANG_SX00;
$counter = DB_getItem($_TABLES['vars'], 'value', "name = 'spamx.counter'");
return array($LANG_SX00['stats_deleted'], COM_numberFormat($counter));
}
/**
* This will put an option for the plugin in the command and control block on moderation.php
* Add the plugin name, icon and link to the command and control block in moderation.php
*
* @return array Array containing (plugin name, admin url, url of plugin icon)
*/
function plugin_cclabel_spamx()
{
global $_CONF, $LANG_SX00;
$retval = array();
if (SEC_hasRights('spamx.admin')) {
$retval = array(
$LANG_SX00['plugin_name'],
$_CONF['site_admin_url'] . '/plugins/spamx/index.php',
plugin_geticon_spamx(), 'tools'
);
}
return $retval;
}
/**
* Returns the administrative option for this plugin
* Adds the plugin to the Admin menu
*
* @return array Array containing (plugin name, plugin admin url, # of items in plugin or '')
*/
function plugin_getadminoption_spamx()
{
global $_CONF, $LANG_SX00, $_TABLES;
if (SEC_hasRights('spamx.admin')) {
$result = DB_query("SELECT count(*) AS cnt FROM {$_TABLES['spamx']}");
$A = DB_fetchArray($result);
$count = $A['cnt'];
return array($LANG_SX00['plugin_name'], $_CONF['site_admin_url'] . '/plugins/spamx/index.php', $count, 'tools');
} else {
return array();
}
}
/**
* Returns the current plugin code version
*
* @return string plugin version
*/
function plugin_chkVersion_spamx()
{
global $_CONF;
require_once $_CONF['path'] . 'plugins/spamx/autoinstall.php';
$inst_parms = plugin_autoinstall_spamx('spamx');
return $inst_parms['info']['pi_version'];
}
/**
* Update the Spam-X plugin
*
* @return int Number of message to display
*/
function plugin_upgrade_spamx()
{
global $_CONF, $_TABLES, $_DB_dbms;
$installed_version = DB_getItem($_TABLES['plugins'], 'pi_version', "pi_name = 'spamx'");
$code_version = plugin_chkVersion_spamx();
if ($installed_version == $code_version) {
// nothing to do
return true;
}
require_once $_CONF['path'] . 'plugins/spamx/autoinstall.php';
if (!plugin_compatible_with_this_version_spamx('spamx')) {
return 3002;
}
$inst_parms = plugin_autoinstall_spamx('spamx');
$pi_gl_version = $inst_parms['info']['pi_gl_version'];
require_once $_CONF['path'] . 'plugins/spamx/sql/' . $_DB_dbms . '_updates.php';
require_once $_CONF['path'] . 'plugins/spamx/install_updates.php';
$current_version = $installed_version;
$done = false;
$current_config = false;
while (!$done) {
switch ($current_version) {
case '1.1.0':
require_once $_CONF['path_system'] . 'classes/config.class.php';
$plugin_path = $_CONF['path'] . 'plugins/spamx/';
require_once $plugin_path . 'install_defaults.php';
if (file_exists($plugin_path . 'config.php')) {
global $_DB_table_prefix, $_SPX_CONF;
require_once $plugin_path . 'config.php';
}
if (!plugin_initconfig_spamx()) {
echo 'There was an error upgrading the Spam-X plugin';
return false;
}
$current_config = true;
$current_version = '1.1.1';
break;
case '1.1.1':
// no db changes
$current_version = '1.1.2';
break;
case '1.1.2':
// no db changes
$current_version = '1.2.0';
break;
case '1.2.0':
if (isset($_UPDATES[$current_version])) {
$_SQL = $_UPDATES[$current_version];
foreach ($_SQL as $sql) {
DB_query($sql);
}
}
if (!$current_config) {
// Remove admin override since not needed anymore
$c = config::get_instance();
$c->del('admin_override', 'spamx');
// late fix: ensure 'notification_email' option can be disabled
$result = DB_query("SELECT value, default_value FROM {$_TABLES['conf_values']} WHERE name = 'notification_email' AND group_name = 'spamx'");
list($value, $default_value) = DB_fetchArray($result);
if ($value != 'unset') {
if (substr($default_value, 0, 6) != 'unset:') {
$unset = DB_escapeString('unset:' . $default_value);
DB_query("UPDATE {$_TABLES['conf_values']} SET default_value = '$unset' WHERE name = 'notification_email' AND group_name = 'spamx'");
}
}
}
$current_version = '1.2.1';
break;
case '1.2.1':
if (isset($_UPDATES[$current_version])) {
$_SQL = $_UPDATES[$current_version];
foreach ($_SQL as $sql) {
DB_query($sql);
}
}
if (!$current_config) {
spamx_update_ConfValues_1_2_1();
}
spamx_update_ConfigSecurity_1_2_1();
$current_version = '1.2.2';
break;
case '1.2.2':
if (isset($_UPDATES[$current_version])) {
$_SQL = $_UPDATES[$current_version];
foreach ($_SQL as $sql) {
DB_query($sql);
}
}
if (!$current_config) {
// Update to Config Tables must be performed here and not in regular SQL update array since if config is current then they shouldn't be run
// Set new Tab column to whatever fieldset is
$sql = "UPDATE {$_TABLES['conf_values']} SET tab = fieldset WHERE group_name = 'spamx'";
DB_query($sql);
// Rename the action config option since it is causes JavaScript issues in the config and IE 8
$sql = "UPDATE {$_TABLES['conf_values']} SET name = 'spamx_action' WHERE name = 'action' AND group_name = 'spamx'";
DB_query($sql);
// in an earlier update we accidentally renamed the 'action' option to 'spamx_name' - fix that
$sql = "UPDATE {$_TABLES['conf_values']} SET name = 'spamx_action' WHERE name = 'spamx_name' AND group_name = 'spamx'";
DB_query($sql);
spamx_update_ConfValues_1_2_2();
}
spamx_update_ConfigSecurity_1_2_2();
$current_version = '1.3.0';
break;
case '1.3.0': // Shipped with Geeklog-2.0.0
if (isset($_UPDATES[$current_version])) {
$_SQL = $_UPDATES[$current_version];
foreach ($_SQL as $sql) {
DB_query($sql);
}
}
spamx_update_ConfValues_1_3_0();
$current_version = '1.3.1';
break;
case '1.3.1':
if (isset($_UPDATES[$current_version])) {
$_SQL = $_UPDATES[$current_version];
foreach ($_SQL as $sql) {
DB_query($sql);
}
}
$current_version = '1.3.2'; // Shipped with Geeklog-2.1.1
break;
case '1.3.2':
if (isset($_UPDATES[$current_version])) {
$_SQL = $_UPDATES[$current_version];
foreach ($_SQL as $sql) {
DB_query($sql);
}
}
$current_version = '1.3.3'; // Shipped with Geeklog-2.1.2
break;
case '1.3.3':
if (isset($_UPDATES[$current_version])) {
$_SQL = $_UPDATES[$current_version];
foreach ($_SQL as $sql) {
DB_query($sql);
}
}
$current_version = '1.3.4'; // Shipped with Geeklog-2.1.3
break;
case '1.3.4':
if (isset($_UPDATES[$current_version])) {
$_SQL = $_UPDATES[$current_version];
foreach ($_SQL as $sql) {
DB_query($sql);
}
}
spamx_update_ConfValues_1_3_4();
$current_version = '1.3.5'; // Shipped with Geeklog-2.2.0
break;
default:
$done = true;
}
}
DB_query("UPDATE {$_TABLES['plugins']} SET pi_version = '$code_version', pi_gl_version = '$pi_gl_version' WHERE pi_name = 'spamx'");
return true;
}
/**
* Called during site migration - handle changed URLs or paths
*
* @param array $old_conf contents of the $_CONF array on the old site
* @return boolean true on success, otherwise false
*/
function plugin_migrate_spamx($old_conf)
{
global $_CONF, $_TABLES;
// only update the SLV whitelist, so we don't use Installer::updateSiteUrl
$old_url = DB_escapeString($old_conf['site_url']);
$result = DB_query("SELECT name, value FROM {$_TABLES['spamx']} WHERE name = 'SLVwhitelist' AND value LIKE '{$old_url}%'");
$num = DB_numRows($result);
for ($i = 0; $i < $num; $i++) {
list($name, $value) = DB_fetchArray($result);
$new_value = DB_escapeString(str_replace($old_conf['site_url'], $_CONF['site_url'], $value));
$old_value = DB_escapeString($value);
DB_query("UPDATE {$_TABLES['spamx']} SET value = '$new_value' WHERE name = 'SLVwhitelist' AND value = '$old_value'");
}
COM_errorLog('Successfully migrated the "spamx" plugin');
return true;
}
/**
* Actual Plugin Functions here.
*/
/**
* Check a post for spam
*
* @param string $comment comment text
* @param int $action (former spam action - not used any more)
* @param string $permanentLink (since GL 2.2.0)
* @param string $commentType (since GL 2.2.0)
* @param string $commentAuthor (since GL 2.2.0)
* @param string $commentAuthorEmail (since GL 2.2.0)
* @param string $commentAuthorURL (since GL 2.2.0)
* @return int either PLG_SPAM_NOT_FOUND, PLG_SPAM_FOUND or PLG_SPAM_UNSURE
* @note As for valid value for $commentType, see system/classes/Akismet.php
*/
function plugin_checkforSpam_spamx($comment, $action = -1, $permanentLink,
$commentType = Geeklog\Akismet::COMMENT_TYPE_COMMENT,
$commentAuthor = null, $commentAuthorEmail = null, $commentAuthorURL = null)
{
global $_CONF, $_SPX_CONF, $_TABLES;
// skip spam check for members of a group that has the spamx.skip right.
if (SEC_hasRights('spamx.skip')) {
return PLG_SPAM_NOT_FOUND;
}
if (!isset($_SPX_CONF['max_age'])) {
$_SPX_CONF['max_age'] = 0;
}
// Clear out any old records
$deleteList = '';
if (is_array($_SPX_CONF['records_delete'])) {
foreach ($_SPX_CONF['records_delete'] as $record) {
if (!empty($record)) {
$record = DB_escapeString($record);
if ($deleteList == '') {
$deleteList = "'$record'";
} else {
$deleteList .= ", '$record'";
}
}
}
}
if ($_SPX_CONF['max_age'] > 1 && $deleteList != '') {
$timestamp = DB_escapeString(date('Y-m-d'));
$sql = "DELETE FROM {$_TABLES['spamx']}
WHERE DATE_ADD(regdate,INTERVAL {$_SPX_CONF['max_age']} DAY) < '$timestamp'
AND name IN ($deleteList)";
DB_query($sql);
}
$spamx_path = $_CONF['path'] . 'plugins/spamx/';
// Set up SpamxExaminer array
$spamxExaminerSources = array();
if ($dir = @opendir($spamx_path)) {
while (($file = readdir($dir)) !== false) {
if (is_file($spamx_path . $file)) {
if (substr($file, -18) == '.Examine.class.php') {
$sourceFile = str_replace('.Examine.class.php', '', $file);
$spamxExaminerSources[] = $sourceFile;
}
}
}
closedir($dir);
}
// Sort the files. IP Class needs to execute before Stop Forum Spam class yo save on calls to stopforumspam.com
asort($spamxExaminerSources);
$answer = PLG_SPAM_NOT_FOUND;
foreach ($spamxExaminerSources as $spamxExaminerSource) {
$filename = $spamxExaminerSource . '.Examine.class.php';
require_once $spamx_path . $filename;
$examiner = new $spamxExaminerSource;
$answer = $examiner->execute(
$comment, $permanentLink, $commentType, $commentAuthor, $commentAuthorEmail, $commentAuthorURL
);
if ($answer != PLG_SPAM_NOT_FOUND) {
break;
}
}
return $answer;
}
/**
* Perform action after spam has been detected
*
* @param string $comment comment text
* @param int $action which action modules to call (sum of module numbers)
* @return int number of message to display to the spammer
*/
function plugin_spamaction_spamx($comment, $action)
{
global $_CONF, $_SPX_CONF;
$res = PLG_SPAM_NOT_FOUND;
$spamx_path = $_CONF['path'] . 'plugins/spamx/';
if (($action == -1) || ($action == '')) {
$action = $_SPX_CONF['spamx_action'];
}
// Set up Spamx_Action array
$Spamx_Action = array();
if ($dir = @opendir($spamx_path)) {
while (($file = readdir($dir)) !== false) {
if (is_file($spamx_path . $file)) {
if (substr($file, -17) == '.Action.class.php') {
$sfile = str_replace('.Action.class.php', '', $file);
require_once $spamx_path . $file;
$CM = new $sfile;
$Spamx_Action[$sfile] = $CM->getActionCode();
}
}
}
closedir($dir);
}
foreach ($Spamx_Action as $Act => $actionCode) {
if (($action & $actionCode) == $actionCode) {
$AC = new $Act;
$AC->execute($comment);
$res = max($res, $AC->getResult());
}
}
return $res;
}
/**
* Logs message to spamx.log
* This will print a message to the spamx log
*
* @param string $logEntry Message to write to log
*/
function SPAMX_log($logEntry)
{
global $_CONF, $LANG01, $_SPX_CONF;
if ((!isset($_SPX_CONF['logging']) || ($_SPX_CONF['logging'] === true)) && !empty($logEntry)) {
$logEntry = str_replace(array('<?', '?>'), array('(@', '@)'), $logEntry);
$timestamp = strftime('%c');
$logfile = $_CONF['path_log'] . 'spamx.log';
if (!$file = fopen($logfile, 'a')) {
COM_errorLog($LANG01[33] . $logfile . ' (' . $timestamp . ')', 1);
}
fputs($file, "$timestamp - $logEntry \n");
fclose($file);
}
}
/**
* Returns the URL of the plugin's icon
*
* @return string URL of the icon
*/
function plugin_geticon_spamx()
{
global $_CONF;
return $_CONF['site_admin_url'] . '/plugins/spamx/images/spamx.png';
}
/**
* Automatic uninstall function for plugins
* This code is automatically uninstalling the plugin.
* It passes an array to the core code function that removes
* tables, groups, features and php blocks from the tables.
* Additionally, this code can perform special actions that cannot be
* foreseen by the core code (interactions with other plugins for example)
*
* @return array Plugin information
*/
function plugin_autouninstall_spamx()
{
$out = array(
/* give the name of the tables, without $_TABLES[] */
'tables' => array('spamx'),
/* give the full name of the group, as in the db */
'groups' => array('spamx Admin'),
/* give the full name of the feature, as in the db */
'features' => array('spamx.admin',
'spamx.view',
'spamx.skip',
'config.spamx.tab_main'),
/* give the full name of the block, including 'phpblock_', etc */
'php_blocks' => array(),
/* give all vars with their name */
'vars' => array('spamx_gid', 'spamx.counter'),
);
return $out;
}
/**
* Provide URL of a documentation file
*
* @param string $file documentation file being requested, e.g. 'config'
* @return mixed URL or false when not available
*/
function plugin_getdocumentationurl_spamx($file)
{
static $docUrl;
switch ($file) {
case 'index':
case 'config':
if (isset($docUrl)) {
$retval = $docUrl;
} else {
$retval = COM_getDocumentUrl('docs', 'spamx.html');
$docUrl = $retval;
}
break;
default:
$retval = false;
break;
}
return $retval;
}
/**
* Provides text for a Configuration tooltip
*
* @param string $id Id of config value
* @return mixed Text to use regular tooltip, NULL to use config
* tooltip hack, or empty string when not available
*/
function plugin_getconfigtooltip_spamx($id)
{
// Use config tooltip hack where tooltip is read from the config documentation
return;
}
/**
* Checks user registrations against stopforumspam.com to validate email and IP addresses
*
* @param string $type Type of item
* @param string $username Type of item
* @return string
*/
function plugin_itemPreSave_spamx($type, $username)
{
global $_CONF, $LANG_SX00;
if ($type == 'registration') {
require_once $_CONF['path'] . '/plugins/spamx/SFS.Misc.class.php';
$EX = new SFS;
$res = $EX->execute(null, null, Geeklog\Akismet::COMMENT_TYPE_COMMENT, null, Geeklog\Input::post('email'));
if ($res > 0) {
return $LANG_SX00['invalid_email_or_ip'];
}
}
return '';
}
/**
* Public APIs to provide the information the Spam-X has stored or to manipulate the data it has
*
* @since v2.2.0
*/
/**
* @param string $ipToCheck
* @return bool
*/
function SPAMX_isIPBanned($ipToCheck)
{
global $_TABLES;
$ipToCheck = trim($ipToCheck);
$ipToCheck = strtolower($ipToCheck);
if ($ipToCheck !== '') {
$sql = "SELECT value FROM {$_TABLES['spamx']} WHERE (name = 'IP')";
$result = DB_query($sql);
if (!DB_error()) {
while (($A = DB_fetchArray($result, false)) !== false) {
$target = strtolower($A['value']);
if (strpos($target, '/') !== false) {
$check = Geeklog\IP::matchCIDR($ipToCheck, $target);
} elseif (strpos($target, '-') !== false) {
$check = Geeklog\IP::matchRange($ipToCheck, $target);
} else {
$target = '/' . preg_quote($target, '/') . '/';
$check = (preg_match($target, $ipToCheck) == 1);
}
if ($check) {
return true;
}
}
}
}
return false;
}
/**
* Return an array of banned IPs
*
* @return array of string representing an IP
*/
function SPAMX_getBannedIPs()
{
global $_TABLES;
$retval = array();
$sql = "SELECT value FROM {$_TABLES['spamx']} WHERE name = 'IP'";
$result = DB_query($sql);
if (!DB_error()) {
while (($A = DB_fetchArray($result, false)) !== false) {
$retval[] = $A['value'];
}
}
return $retval;
}
/**
* Register or update banned IPs
*
* @param string|array $ips
*/
function SPAMX_registerBannedIPs($ips)
{
global $_TABLES;
$ips = (array) $ips;
foreach ($ips as $ip) {
$ip = trim($ip);
if ($ip !== '') {
$ip = DB_escapeString($ip);
if (DB_count($_TABLES['spamx'], array('name', 'value'), array('IP', $ip)) == 1) {
// The IP given already exists
$sql = "UPDATE {$_TABLES['spamx']} SET counter = counter + 1 "
. "WHERE (name = 'IP') AND (value = '{$ip}')";
} else {
// The IP given not found
$timestamp = DB_escapeString(date('Y-m-d H:i:s'));
$sql = "INSERT INTO {$_TABLES['spamx']} (name, value, counter, regdate) "
. "VALUES('IP', '{$ip}', 1, '{$timestamp}')";
}
DB_query($sql);
}
}
}
/**
* Delete IPs from the database
*
* @param string|array $ips
*/
function SPAMX_deleteBannedIPs($ips)
{
global $_TABLES;
$ips = (array) $ips;
foreach ($ips as $ip) {
$ip = trim($ip);
if ($ip !== '') {
$ip = DB_escapeString($ip);
$sql = "DELETE FROM {$_TABLES['spamx']} WHERE (name = 'IP') AND (value = '{$ip}') LIMIT 1";
DB_query($sql);
}
}
}