/
sk_core_class.php
890 lines (757 loc) · 30 KB
/
sk_core_class.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
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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
<?php
/******************************************************************************
Spam Karma (c) 2009 - http://code.google.com/p/spam-karma/
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; version 2 of the License.
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.
******************************************************************************/
?><?php
define ('SK_PLUGIN_FOLDER', dirname(__FILE__) . '/sk_plugins/');
define ('SK_CORE_VERSION', 5);
define ('SK_VERSION', 4); // => 2.3
define ('SK_RELEASE', 'alpha');
if (isset($table_prefix))
define ('SK_SPAM_TABLE', $table_prefix . 'sk_spams');
else
define ('SK_SPAM_TABLE', SK_TABLE_PREFIX . 'sk_spams');
require_once('sk_util_class.php');
require_once('sk_plugin_class.php');
require_once('sk_comment_class.php');
require_once('sk_blacklist_class.php');
require_once('sk_functions.php');
global $sk_plugin_array;
$sk_plugin_array = 0;
class SK_Core
{
var $cur_comment = 0;
var $post_proc = false;
var $plugins = array();
var $version = SK_CORE_VERSION;
function SK_Core($comment_ID = 0, $post_proc = false, $load_plugins = true)
{
return $this->__construct($comment_ID, $post_proc, $load_plugins);
}
function __construct($comment_ID = 0, $post_proc = false, $load_plugins = true)
{
add_action( 'admin_notices', array( &$this, 'sanity_check' ) );
$this->set_post_proc($post_proc);
if ($comment_ID)
$this->load_comment($comment_ID);
$this->plugins = array();
if ($load_plugins)
$this->load_plugin_files();
}
function load_plugin_files()
{
global $sk_plugin_array;
if ($sk_plugin_array)
{
$this->plugins = $sk_plugin_array;
return;
}
$plugin_files = sk_get_file_list(SK_PLUGIN_FOLDER);
foreach($plugin_files as $plugin)
{
include_once(SK_PLUGIN_FOLDER . $plugin);
}
$sk_plugin_array = $this->plugins;
}
function load_comment($comment_ID)
{
if (! $comment_ID)
return false;
$this->cur_comment = new sk_comment($comment_ID, $this->post_proc, $comment_sk_info);
if ($this->post_proc)
$comment_sk_info = $this->get_comment_sk_info();
return ($this->cur_comment->ID > 0);
}
function set_post_proc($post_proc)
{
$this->post_proc = $post_proc;
}
function is_post_proc()
{
return $this->post_proc;
}
function process_comment($comment_ID = 0)
{
if ($comment_ID && ($comment_ID != @$this->cur_comment->ID))
$this->cur_comment = new sk_comment($comment_ID, $this->post_proc); // ? does PHP garbage collect? sure hope so...
global $sk_settings;
$this->filter_comment();
if ($bias = $sk_settings->get_core_settings("general_bias"))
$this->cur_comment->modify_karma($bias, "core", "Severity settings adjustment.");
$this->treat_comment();
$this->set_comment_sk_info();
}
function filter_comment ($comment_ID = 0)
{
$start = time();
if ($comment_ID && ($comment_ID != @$this->cur_comment->ID))
$this->cur_comment = new sk_comment($comment_ID, $this->post_proc); // ? does PHP garbage collect? sure hope so...
if (! $this->cur_comment)
{
$this->log_msg(__("Cannot run <code>filter_comment</code>: no valid comment provided.", 'spam-karma'), 9);
return;
}
$this->log_msg(__("Running filters on comment ID: ", 'spam-karma') . $this->cur_comment->ID, 0);
foreach ($this->plugins as $plugin)
{
if ($plugin[1]->is_filter()
&& ($plugin[1]->is_enabled())
&& ($this->post_proc
|| (((int) $this->cur_comment->karma > $plugin[1]->skip_under)
&& ((int) $this->cur_comment->karma < $plugin[1]->skip_above))))
$plugin[1]->filter_this($this->cur_comment);
}
$dur = time() - $start;
if ($dur > 1000)
$this->log_msg(sprintf(__("Filter processing on comment ID %d took %d ms.", 'spam-karma'), $this->cur_comment->ID, $dur), 3);
}
function treat_comment ($comment_ID = 0)
{
$start = time();
if ($comment_ID && ($comment_ID != @$this->cur_comment->ID))
$this->cur_comment = new sk_comment($comment_ID, $this->post_proc);
if (! $this->cur_comment)
{
$this->log_msg(__("Cannot run <code>treat_comment</code>: no valid comment provided.", 'spam-karma'), 9);
return;
}
$this->log_msg(__("Running treatments on comment ID: ", 'spam-karma') . $this->cur_comment->ID, 0);
foreach ($this->plugins as $plugin)
if ($plugin[1]->is_treatment()
&& ($plugin[1]->is_enabled()))
$plugin[1]->treat_this($this->cur_comment);
$dur = time() - $start;
if ($dur > 1000)
$this->log_msg(sprintf(__("Treatment processing on comment ID %d took %d ms.", 'spam-karma'), $this->cur_comment->ID, $dur), 3);
}
function form_insert ($post_ID = 0)
{
foreach ($this->plugins as $plugin)
if ($plugin[1]->is_enabled())
$plugin[1]->form_insert($post_ID);
}
function second_chance()
{
if (! $this->cur_comment)
return false;
$rem_attempts = (int) $this->cur_comment->remaining_attempts;
if($rem_attempts <= 0)
{
echo ("<div class=\"sk_box\">" . __("Too many unlock attempts.", 'spam-karma') . "</div>");
return false;
}
if (! $this->cur_comment->can_unlock())
{
echo ("<div class=\"sk_box\">" . __("This comment wasn't given a second chance.", 'spam-karma') . "</div>");
return false;
}
if (!empty ($_REQUEST['sk_second_chance']))
{
$class = $_REQUEST['sk_second_chance'];
$this_key = 0;
foreach ($this->cur_comment->unlock_keys as $id => $unlock_key)
$this_key = $unlock_key;
if ($this_key
&& ($this_key['expire'] > time())
&& ($my_plugin = $this->get_plugin($this_key['class']))
&& $my_plugin->is_enabled())
{
echo "<div class=\"sk_box\">";
if ($my_plugin->treat_second_chance($this->cur_comment, $this_key['key']))
{
$this->cur_comment->post_proc = true; // just making sure we don't run into loops here
// if ($anubis = $this->get_plugin('sk_anubis_plugin'))
// $anubis->treat_this($this->cur_comment);
$this->treat_comment();
$this->cur_comment->remaining_attempts = 0; // no more attempts
if ($this->cur_comment->approved == '1')
{
echo __("Thank you. Your comment has been approved.", 'spam-karma');
if ( get_option('comments_notify') )
wp_notify_postauthor($this->cur_comment->ID, $this->cur_comment->type);
//TODO redirect to comment page...
}
else
{
echo __("Thank you. Your comment will be displayed as soon as it is approved by a moderator.", 'spam-karma');
wp_notify_moderator($this->cur_comment->ID);
}
}
else
{
$this->cur_comment->remaining_attempts = --$rem_attempts;
if ($rem_attempts <= 0)
{
//if ($anubis = $this->get_plugin('sk_anubis_plugin'))
// $anubis->treat_this($this->cur_comment);
$this->treat_comment($this->cur_comment);
echo "<span class=\"sk_fail\">" . __("Too many missed attempts. Your comment's moderation has been confirmed. A log of your comment will be kept and presented to the blog admin upon his next log-on. Please contact him directly via e-mail regarding this problem.", 'spam-karma') . "</span>";
}
else
{
echo "<span class=\"sk_fail\">" . sprintf(__ngettext("Sorry, bad luck on this one, cow-boy, try again. You have %d attempt left.", "Sorry, bad luck on this one, cow-boy, try again. You have %d attempts left.", $rem_attempts, 'spam-karma'), $rem_attempts) . "</span>";
}
}
$this->set_comment_sk_info();
echo "</div>";
}
else
{
echo ("<div class=\"sk_box\">" . __("Can't use this unlock method.", 'spam-karma') . "</div>");
$this->cur_comment->remaining_attempts = 0;
$this->set_comment_sk_info();
return false;
}
}
else
{
$i = 0;
foreach($this->cur_comment->unlock_keys as $id => $unlock_key)
{
$which_plugin_obj = $this->get_plugin($unlock_key['class']);
$msg = "";
$msg_level = 0;
echo "<div class=\"sk_box\">";
if (! $which_plugin_obj)
{
$msg = "<strong>" . sprintf(__("Cannot find 2nd chance plugin file: %s", 'spam-karma'), $unlock_key['class']) . "</strong>";
$msg_level = 9;
}
elseif (! $which_plugin_obj->is_enabled())
{
$msg = "<strong>" . sprintf(__("2nd chance: %s plugin disabled.", 'spam-karma'), $which_plugin_obj->name) . "</strong>";
$msg_level = 6;
}
elseif ($unlock_key['expire'] < time())
{
$msg = "<strong>" . sprintf(__("%s: unlock key expired.", 'spam-karma'), $which_plugin_obj->name) . "</strong>";
$msg_level = 6;
}
else
{
$i++;
echo "<form name=\"sk_form_". $unlock_key['class'] . "\" id=\"sk_form_". $unlock_key['class'] . "\ method=\"post\">";
echo "<input type=\"hidden\" name=\"sk_second_chance\" id=\"sk_second_chance\" value=\"". $unlock_key['class'] . "\">";
echo "<input type=\"hidden\" name=\"c_id\" id=\"c_id\" value=\"" . $this->cur_comment->ID . "\">";
echo "<input type=\"hidden\" name=\"c_author\" id=\"c_author\" value=\"" . $this->cur_comment->author_email . "\">";
$which_plugin_obj->display_second_chance($this->cur_comment, $unlock_key['key']);
echo "</form>";
}
if ($msg_level)
{
echo $msg;
$this->log_msg($msg, $msg_level);
}
echo "</div>";
}
if ($i <= 0)
$this->cur_comment->remaining_attempts = 0;
}
$this->set_comment_sk_info();
}
function register_plugin($plugin_class, $priority = 5)
{
$i = 0;
foreach ($this->plugins as $elem)
if ( $elem[0] <= $priority )
$i++;
$this->log_msg(__("Registering plugin: ", 'spam-karma') . "<i>" . $plugin_class . "</i>, " . __("priority: ", 'spam-karma') . $priority);
array_splice( $this->plugins, $i, 0, array( array( $priority, new $plugin_class(), $plugin_class) ) );
}
function output_UI()
{
?>
<div class="wrap hide" id="sk_settings_pane"><h2><?php _e("Spam Karma Settings", 'spam-karma'); ?></h2>
<form name="sk_settings_form" id="sk_settings_form" method="post">
<fieldset class="options">
<legend><?php _e("General Settings", 'spam-karma'); ?></legend>
<ul>
<?php
echo sk_nonce_field();
global $sk_settings;
foreach($sk_settings->core_defaults as $name => $format)
{
if (@$format['auto_draw'] != true)
continue; // no UI
echo "<li";
if (@$format['advanced'])
echo " class=\"advanced\"";
echo ">\n";
if ($format['type'] == "check" || $format['type'] == "checkbox")
{
sk_plugin::output_UI_input($name, "checkbox", $sk_settings->get_core_settings($name));
if (!empty($format['caption']))
echo " " . __($format['caption'], 'spam-karma');
if (!empty($format['after']))
echo " " . __($format['after'], 'spam-karma');
}
else
{
if (! empty($format['caption']))
echo __($format['caption'], 'spam-karma') . " ";
if ($format['type'] == "menu" || $format['type'] == "select")
sk_plugin::output_UI_menu($name, $format['options'], $sk_settings->get_core_settings($name));
else
sk_plugin::output_UI_input($name, "text", $sk_settings->get_core_settings($name), @$format['size']);
if (! empty($format['after']))
echo " " . __($format['after'], 'spam-karma');
}
echo "</li>\n";
}
?>
</ul></fieldset>
<p class="submit"><input type="submit" id="sk_settings_save" name="sk_settings_save" value="<?php _e("Save new settings", 'spam-karma'); ?>"></p>
<fieldset class="options">
<legend><?php _e("Filter Plugins Settings", 'spam-karma'); ?></legend>
<?php
foreach ($this->plugins as $plugin)
if ($plugin[1]->is_filter())
$plugin[1]->output_plugin_UI();
?>
</fieldset>
<fieldset class="options">
<legend><?php _e("Treatment Plugins Settings", 'spam-karma'); ?></legend>
<?php
foreach ($this->plugins as $plugin)
if ($plugin[1]->is_treatment() && ! $plugin[1]->is_filter())
$plugin[1]->output_plugin_UI();
?>
</fieldset>
<p class="submit"><input type="submit" id="sk_settings_save" name="sk_settings_save" value="<?php _e("Save new settings", 'spam-karma'); ?>"></p>
</form></div>
<?php
}
function update_SQL_schema()
{
global $sk_settings;
$mysql_updates = $sk_settings->get_core_settings("mysql_updates");
if (@$mysql_updates['core'] < $this->version)
{
if ($this->update_core_SQL_schema (@$mysql_updates['core']))
{
$this->log_msg(sprintf(__("Updated SQL schema for the core (to version: %d).", 'spam-karma'), $this->version), 5);
$mysql_updates['core'] = $this->version;
}
else
$this->log_msg(sprintf(__("Failed to update SQL schema for the core (to version: %d).", 'spam-karma'), $this->version), 8);
}
foreach ($this->plugins as $plugin)
{
$class_name = $plugin[2];
if (@$mysql_updates[$class_name] < $plugin[1]->plugin_version)
{
if ($plugin[1]->update_SQL_schema (@$mysql_updates[$class_name]))
{
$this->log_msg(__("Updated SQL schema for plugin: ", 'spam-karma') . "<em>" . $plugin[1]->name . "</em> ". sprintf(__("(to version: %d).", 'spam-karma'), $plugin[1]->plugin_version), 5);
$mysql_updates[$class_name] = $plugin[1]->plugin_version;
}
else
$this->log_msg(__("Failed to update SQL schema for plugin: ", 'spam-karma') . "<em>" . $plugin[1]->name . "</em> ". sprintf(__("(to version: %d).", 'spam-karma'), $plugin[1]->plugin_version), 8);
}
}
$sk_settings->set_core_settings($mysql_updates, "mysql_updates");
}
function update_components()
{
global $sk_settings;
$version_updates = $sk_settings->get_core_settings("version_updates");
if (@$version_updates['core'] < $this->version)
{
if ($this->update_core (@$version_updates['core']))
{
$this->log_msg(__("Initialized the core", 'spam-karma') . " " . sprintf(__("(to version: %d).", 'spam-karma'), $this->version), 5);
$version_updates['core'] = $this->version;
}
else
$this->log_msg(__("Failed to initialize the core", 'spam-karma') . " " . sprintf(__("(to version: %d).", 'spam-karma'), $this->version), 8);
}
foreach ($this->plugins as $plugin)
{
$class_name = $plugin[2];
if (@$version_updates[$class_name] < $plugin[1]->plugin_version)
{
if ($plugin[1]->version_update (@$version_updates[$class_name]))
{
$this->log_msg(__("Updated plugin: ", 'spam-karma') . "<em>" . $plugin[1]->name . "</em> " . sprintf(__("(to version: %d).", 'spam-karma'), $plugin[1]->plugin_version), 5);
$version_updates[$plugin[2]] = $plugin[1]->plugin_version;
}
else
$this->log_msg(__("Failed to update plugin: ", 'spam-karma') . "<em>" . $plugin[1]->name . "</em> " . sprintf(__("(to version: %d).", 'spam-karma'), $plugin[1]->plugin_version), 8);
}
}
$sk_settings->set_core_settings($version_updates, "version_updates");
}
function update_core ($cur_version)
{
// doing nothing right now
return true;
}
function update_core_SQL_schema ($cur_version)
{
global $wpdb;
$success = true;
if ($cur_version == 0)
{
$query = "CREATE TABLE IF NOT EXISTS `" . SK_SPAM_TABLE . "` (
`id` int(11) NOT NULL auto_increment,
`comment_ID` int(11) NOT NULL default '0',
`karma` float(2) NOT NULL default '0',
`karma_cmts` text NOT NULL,
`last_mod` datetime NOT NULL default '0000-00-00 00:00:00',
`unlock_keys` tinytext NOT NULL,
`remaining_attempts` INT NOT NULL,
PRIMARY KEY (`id`),
KEY `comment_ID` (`comment_ID`)
) TYPE=MyISAM;";
$wpdb->query($query);
if (mysql_error())
{
$this->log_msg(__("Could not create SQL table: ", 'spam-karma') . SK_SPAM_TABLE . ".", 10, true);
$success = false;
}
$query = "CREATE TABLE IF NOT EXISTS `" . SK_LOGTABLE . "` (
`id` INT NOT NULL AUTO_INCREMENT ,
`msg` TEXT NOT NULL ,
`component` TINYTEXT NOT NULL ,
`level` TINYINT NOT NULL ,
`ts` DATETIME NOT NULL ,
PRIMARY KEY ( `id` )
) TYPE=MyISAM;";
$wpdb->query($query);
if (mysql_error())
{
$this->log_msg(__("Could not create SQL table: ", 'spam-karma') . SK_LOGTABLE . ".", 10, true);
$success = false;
}
$query = "CREATE TABLE IF NOT EXISTS `". SK_BLACKLIST_TABLE ."` (
`id` int(11) NOT NULL auto_increment,
`type` tinytext NOT NULL,
`value` text NOT NULL,
`score` int(11) NOT NULL default '0',
`trust` int(11) NOT NULL default '0',
`comments` text NOT NULL,
`added` datetime NOT NULL default '0000-00-00 00:00:00',
`added_by` tinytext NOT NULL,
`last_used` datetime NOT NULL default '0000-00-00 00:00:00',
`used_count` int(11) NOT NULL default '0',
`user_reviewed` enum('yes','no') NOT NULL default 'no',
PRIMARY KEY (`id`)
) TYPE=MyISAM ;";
$wpdb->query($query);
if (mysql_error())
{
$this->log_msg(__("Could not create SQL table: ", 'spam-karma') . SK_BLACKLIST_TABLE . ".", 10, true);
$success = false;
}
}
elseif ($cur_version == 1)
{
$query = "ALTER TABLE `" . SK_SPAM_TABLE . "` ADD INDEX ( `comment_ID` )";
$wpdb->query($query);
if (mysql_error())
{
$this->log_msg(__("Could not alter SQL table: ", 'spam-karma') . SK_SPAM_TABLE . ".", 10, true);
$success = false;
}
else
$this->log_msg(__("Successfully created index for SQL table: ", 'spam-karma') . SK_SPAM_TABLE . ".", 5, false);
}
// modifying WP's tables to work correctly...
// Still returns success on SQL failure (so it doesn't keep trying adding indices in case they are already there or the tables have been otherwise modified)
$query = "ALTER TABLE `" . $wpdb->comments . "` ADD INDEX `comment_date_gmt` ( `comment_date_gmt`)";
$wpdb->query($query);
if (! mysql_error())
$this->log_msg(__("Successfully created index for SQL table: ", 'spam-karma') . $wpdb->comments . ".", 5, false);
$query = "ALTER TABLE `" . $wpdb->posts . "` ADD INDEX `post_date` ( `post_date`)";
$wpdb->query($query);
if (! mysql_error())
$this->log_msg(__("Successfully created index for SQL table: ", 'spam-karma') . $wpdb->posts . ".", 5, false);
$query = "ALTER TABLE `" . $wpdb->posts . "` ADD INDEX `post_date_gmt` ( `post_date_gmt`)";
$wpdb->query($query);
if (! mysql_error())
$this->log_msg(__("Successfully created index for SQL table: ", 'spam-karma') . $wpdb->posts . ".", 5, false);
return $success;
}
function save_UI_settings($form_values)
{
global $sk_settings;
//print_r($form_values);
if (isset($form_values["sk_settings_save"]))
{
if (isset($form_values['sk_filter_options']))
{
array_walk($form_values['sk_filter_options'], "sk_unescape_form_string_callback");
$this->log_msg(sprintf(__ngettext("Saving new settings for %d plugin.", "Saving new settings for %d plugins.", count($form_values['sk_filter_options']), 'spam-karma'), count($form_values['sk_filter_options'])), 3, 'spam-karma');
if (isset($form_values['sk_filter_checkboxes']))
foreach($form_values['sk_filter_checkboxes'] as $check_plugin => $check_name)
if (! isset($form_values['sk_filter_options'][$check_plugin][$check_name]))
$form_values['sk_filter_options'][$check_plugin][$check_name] = false;
if (isset($form_values['sk_filter_options'][get_class($this)]))
{
foreach($form_values['sk_filter_options'][get_class($this)] as $name => $value)
$sk_settings->set_core_settings($value, $name);
unset($form_values['sk_filter_options'][get_class($this)]);
}
$sk_settings->set_plugins_settings($form_values['sk_filter_options']);
}
}
}
function get_plugin($this_one)
{
$which_plugin_obj = 0;
foreach ($this->plugins as $plugin)
if ($plugin[2] == $this_one)
$which_plugin_obj = $plugin[1];
return $which_plugin_obj;
}
function set_comment_sk_info($comment_ID = 0, $comment_sk_info = 0, $append = false)
{
// if $comment_ID != 0: must provide $comment_sk_info
global $sk_settings, $wpdb;
if (! $comment_ID)
{
if (! $this->cur_comment->ID)
{
$this->log_msg(__("Cannot update SK_SPAM_TABLE info (no comment ID provided).", 'spam-karma'), 8);
return false;
}
$comment_sk_info = array();
$comment_ID = $comment_sk_info['comment_ID'] = $this->cur_comment->ID;
$comment_sk_info['karma'] = $this->cur_comment->karma;
$comment_sk_info['karma_cmts'] = $this->cur_comment->karma_cmts;
$comment_sk_info['unlock_keys'] = $this->cur_comment->unlock_keys;
$comment_sk_info['remaining_attempts'] = $this->cur_comment->remaining_attempts;
}
$comment_sk_info_orig = $this->get_comment_sk_info($comment_ID);
if ($comment_sk_info_orig)
{
if ($append)
{
if(! is_array($comment_sk_info_orig->karma_cmts))
$comment_sk_info_orig->karma_cmts = array();
if(! is_array($comment_sk_info_orig->unlock_keys))
$comment_sk_info_orig->unlock_keys = array();
if(! is_array($comment_sk_info['karma_cmts']))
$comment_sk_info['karma_cmts'] = array();
if(! is_array($comment_sk_info['unlock_keys']))
$comment_sk_info['unlock_keys'] = array();
$comment_sk_info['karma_cmts'] = $comment_sk_info_orig->karma_cmts + $comment_sk_info['karma_cmts'];
$comment_sk_info['unlock_keys'] = $comment_sk_info_orig->unlock_keys + $comment_sk_info['unlock_keys'];
if (! isset($comment_sk_info['karma']))
$comment_sk_info['karma'] = $comment_sk_info_orig->karma;
if (! isset($comment_sk_info['remaining_attempts']))
$comment_sk_info['remaining_attempts'] = $comment_sk_info_orig->remaining_attempts;
// print_r($comment_sk_info);
}
$query = "UPDATE `". SK_SPAM_TABLE ."` SET ";
$query_end = "`last_mod` = NOW() WHERE `id` = " . $comment_sk_info_orig->id;
}
else
{
$query = "INSERT INTO `". SK_SPAM_TABLE ."` SET ";
$query_end = "`last_mod` = NOW(), `comment_ID` = $comment_ID";
}
foreach ($comment_sk_info as $key => $val)
{
if ($key == 'comment_ID')
continue;
if (is_array($val))
$val = serialize($val);
if (is_int($val) || is_float($val))
$query .= "`$key` = " . $val . ",";
else
$query .= "`$key` = '" . sk_escape_string($val) . "', ";
}
$query .= $query_end;
//echo $query;
$wpdb->query($query);
if (! mysql_error())
$this->log_msg(__("Inserted/Updated SK_SPAM_TABLE record for comment ID: ", 'spam-karma') . $comment_ID . " (". ($append ? __("mode: append", 'spam-karma') : __("mode: overwrite", 'spam-karma')) . ").", 0);
else
$this->log_msg(__("Failed inserting/updating SK_SPAM_TABLE record for comment ID:", 'spam-karma') . $comment_ID . " (". ($append ? __("mode: append", 'spam-karma') : __("mode: overwrite", 'spam-karma')) . "). <br/>" . __("Query: ", 'spam-karma') . "<code>$query</code>", 8, true);
}
function get_comment_sk_info($comment_ID = 0)
{
global $wpdb;
if (! $comment_ID)
$comment_ID = $this->cur_comment->ID;
if (! $comment_ID)
{
echo
$this->log_msg(__("get_comment_sk_info: Cannot get SK_SPAM_TABLE info (no comment ID provided).", 'spam-karma'), 8);
return false;
}
if ($comment_sk_info = $wpdb->get_row("SELECT * FROM `". SK_SPAM_TABLE ."` WHERE `comment_ID` = $comment_ID"))
{
if (! empty($comment_sk_info->karma_cmts))
$comment_sk_info->karma_cmts = unserialize($comment_sk_info->karma_cmts);
if (! is_array($comment_sk_info->karma_cmts))
$comment_sk_info->karma_cmts = array();
if (! empty($comment_sk_info->unlock_keys))
$comment_sk_info->unlock_keys = unserialize($comment_sk_info->unlock_keys);
if (! is_array($comment_sk_info->unlock_keys))
$comment_sk_info->unlock_keys = array();
return $comment_sk_info;
}
else
return false;
}
// sanity_check() makes sure we've at least been run *once*
function sanity_check()
{
global $sk_settings;
if ( isset($_REQUEST['sk_section'] ) || ( isset( $_REQUEST['page'] ) && 'spamkarma' == $_REQUEST['page'] ) )
return;
$mysql_updates = $sk_settings->get_core_settings("mysql_updates");
$version_updates = $sk_settings->get_core_settings("version_updates");
if (empty($mysql_updates['core']) || empty($version_updates['core'])) {
$message = __('It sounds like SK has been recently installed on this blog, but not configured.', 'spam-karma');
} elseif (($mysql_updates['core'] < $this->version) || ($version_updates['core'] < $this->version)) {
$message = __('It sounds like SK has recently been updated on this blog. But not fully configured.', 'spam-karma');
}
if (!empty ($message)) {
$message .= ' ' . sprintf(__("You MUST visit %sSpam Karma's admin page%s at least once before letting it filter your comments (chaos may ensue otherwise).", 'spam-karma'), "<a href=\"". get_bloginfo('wpurl') . "/wp-admin/options-general.php?page=spamkarma" . "\">", "</a>");
echo "<div class=\"error\">$message</div>";
}
}
function advanced_tools($run_tools)
{
if (! is_array($run_tools))
$run_tools = array($run_tools => true);
global $sk_settings, $wpdb;
if (isset($run_tools['force_sql_update']))
{
$sk_settings->set_core_settings("", "mysql_updates");
$this->log_msg(__("Forcing MySQL updates on core and plugins.", 'spam-karma'), 3);
}
if (isset($run_tools['reinit_plugins']))
{
$sk_settings->set_core_settings("", "version_updates");
$sk_settings->reset_plugin_settings();
$this->log_msg(__("Reinitialize all plugins.", 'spam-karma'), 3);
}
if (isset($run_tools['reinit_all']))
{
$sk_settings->reset_all_settings();
$this->log_msg(__("Reinitialize everything to factory settings.", 'spam-karma'), 3);
}
if (isset($run_tools['reset_all_tables']))
{
$this->log_msg(__("Dropping all SK Tables!", 'spam-karma'), 8);
$wpdb->query("DROP TABLE `". SK_SPAM_TABLE . "`;");
$wpdb->query("DROP TABLE `". SK_LOGTABLE . "`;");
$wpdb->query("DROP TABLE `". SK_BLACKLIST_TABLE . "`;");
$this->log_msg(__("Dropped all SK Tables!", 'spam-karma'), 7);
$sk_settings->set_core_settings("", "mysql_updates");
$this->log_msg(__("Forcing MySQL updates on core and plugins.", 'spam-karma'), 6, 0, "web_UI");
}
if(isset($run_tools['check_comment_form']))
{
$found = 0;
foreach(array("comments.php", "comments-popup.php") as $file_name)
{
$content = "";
if ($file = @fopen(TEMPLATEPATH . "/$file_name", "r"))
{
while(! feof($file))
$content .= fread($file, 4096);
if (preg_match("#do_action.*comment_form#", $content))
{
$found++;
echo "<div class=\"msg_good\">" . sprintf(__("File <code>%s</code> is OK.", 'spam-karma'), $file_name) . "</div>";
}
else
{
echo "<div class=\"msg_bad\">" . sprintf(__("File <code>%s</code>: Could not find <code>do_action</code> hook. Some plugins will fail. Please refer to <a href=\"http://wp-plugins.net/wiki/index.php?title=sk_Theme_Compatibility\">SK's documentation</a>.", 'spam-karma'), $file_name) . "</div>";
}
}
else
{
echo "<div class=\"msg_good\">" . sprintf(__("File <code>%s</code> looks OK (not present in the active theme, should be using WP default).", 'spam-karma'), $file_name) . "</div>";
$found++;
}
}
if ($found < 2)
{
if ($payload_plugin = $this->get_plugin("sk_payload_plugin"))
{
$payload_plugin->set_option_value("weight", "0.0");
echo "<div class=\"msg_bad\">" . __("Temporarily disabling form payload check.", 'spam-karma') . "</div>";
}
}
else
{
echo "<div class=\"msg_good\">" . __("Your theme appears to be compatible (but you might want to run the <strong>Advanced Theme Compatibility Check</strong> at the bottom of the General Settings screen, for extra security).", 'spam-karma') . "</div>";
}
}
if(isset($run_tools['check_comment_form_2']))
{
if (empty($run_tools['check_comment_form_2_url']))
{
echo "<div class=\"msg_bad\">" . __("You need to provide the URL address to a page on your blog that contains a comment form (e.g. a single entry permalink).", 'spam-karma') . "</div>";
}
else
{
global $sk_settings;
if ($payload_plugin = $this->get_plugin("sk_payload_plugin"))
{
$save_weight = $payload_plugin->get_option_value("weight");
$payload_plugin->set_option_value("weight", "1.0");
$sk_settings->save_settings();
}
else
{
echo "<div class=\"msg_bad\">" . __("Cannot load Payload plugin. You need to have this plugin present in your Plugins directory to use the advanced Theme check.", 'spam-karma') . "</div>";
return;
}
global $curl_error;
$content = sk_get_url_content($run_tools['check_comment_form_2_url']);
$payload_plugin->set_option_value("weight", $save_weight);
$sk_settings->save_settings();
if (empty ($content))
{
if ($curl_error == 22)
{
### Add l10n
echo "<div class=\"msg_bad\">" . __("SK was unable to open that page (an HTTP error was returned). This may be due to a bad URL or some form of anti-bot script present on your server.", 'spam-karma') . "</div>";
}
else
{
echo "<div class=\"msg_bad\">" . __("Couldn't open the page: You need to provide a valid URL address to a page on your blog that contains a comment form (e.g. a single entry permalink).", 'spam-karma') . "</div>";
}
}
else
{
if (strpos($content, "id=\"sk_payload\"") !== FALSE)
{
echo "<div class=\"msg_good\">" . sprintf(__("URL %s contains the Payload. Your theme appears to be OK.", 'spam-karma'), "<i>" . ($run_tools['check_comment_form_2_url']) . "</i>") . "</div>";
echo "<div class=\"msg_good\">" . __("Re-enabling form payload check.", 'spam-karma') . "</div>";
$payload_plugin->set_option_value("weight", 1.0);
}
else
{
echo "<div class=\"msg_bad\">" . sprintf(__("URL %s: Could not find Payload in comment form. Your theme is likely not compatible. Make sure you used a URL that contains your blog's comment form, try again and, if still getting this result, please refer to <a href=\"http://wp-plugins.net/wiki/index.php?title=sk_Theme_Compatibility\">SK's documentation</a>.", 'spam-karma'), "<i>" . ($run_tools['check_comment_form_2_url']) . "</i>") . "</div>";
echo "<div class=\"msg_bad\">" . __("Temporarily disabling form payload check.", 'spam-karma') . "</div>";
$payload_plugin->set_option_value("weight", 0.0);
}
}
}
}
}
function log_msg($msg, $level = 0, $mysql = false)
{
global $sk_log;
if ($mysql)
$sk_log->log_msg_mysql($msg, $level, 0, "core");
else
$sk_log->log_msg($msg, $level, 0, "core");
}
}
?>