/
sk_util_class.php
270 lines (221 loc) · 9.77 KB
/
sk_util_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
<?php
/**********************************************************************************************
Spam Karma (c) 2015 - http://github.com/strider72/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
include_once('sk_functions.php');
if (isset($table_prefix))
define ('SK_LOGTABLE', $table_prefix . 'sk_logs');
else
define ('SK_LOGTABLE', SK_TABLE_PREFIX . 'sk_logs');
global $sk_log;
if (! isset($sk_log))
$sk_log = new sk_log;
global $sk_settings;
if (! isset($sk_settings))
$sk_settings = new sk_settings;
$sk_log->db_threshold = $sk_settings->get_core_settings('log_threshold');
class sk_settings
{
var $core_settings;
var $plugins_settings;
var $stats;
var $need_to_save = false;
var $core_defaults =
array(
'general_bias' => array('auto_draw' => true, 'type' => 'menu', 'value' => 0, 'caption' => 'Severity: ', 'options' => array(-2 => 'Total Beeatch', -1 => 'Kinda Mean', 0 => 'Normal', 1 => 'Nice', 2 => 'Lovey Dovey')),
'log_threshold' => array('auto_draw' => true, 'advanced' => true, 'type' => 'text', 'value' => 4, 'size' => 2, 'caption' => 'Only write logs level ', 'after' => ' and above <em>(logs with a level under this threshold will be displayed in the log dump if they are issued when using the Admin tools, but not written to the database)</em>.'),
'max_attempts' => array('auto_draw' => true, 'advanced' => true, 'type' => 'text', 'value' => 5, 'size' => 3, 'caption' => 'How many total backup attempts should be allowed:', 'after' => ' <i>(you need to have backup methods, such as captcha, enabled for this to be taken in account).</i>'),
'hover_in_tables' => array('auto_draw' => true, 'advanced' => true, 'type' => 'checkbox', 'value' => true, 'caption' => "Enable 'hovering' in spam report tables", 'after' => ' <i>(if using MSIE. or otherwise css-impaired browsers, you should disable this feature).</i>'),
'display_sk_footer' => array('auto_draw' => true, 'advanced' => false, 'type' => 'checkbox', 'value' => true, 'caption' => 'Automatically insert SK\'s "X spam eaten" message in your blog\'s footer (enable Advanced Options to customize it).'),
'sk_footer_msg_0' => array('auto_draw' => true, 'advanced' => true, 'type' => 'text', 'value' => '<div id="sk-footer" style="color:#FFF; background-color:#444; padding: 3px 2px 3px 2px; border-top: #888 solid 1px;">Spammers: Beware of the <a href="http://github.com/strider72/spam-karma" title="Spam Karma" style="color:#C77; background-color:transparent;">Dog</a>.</div>', 'caption' => 'SK footer message (until there are at least two spams in the DB): ', 'size' => 50),
'sk_footer_msg_n' => array('auto_draw' => true, 'advanced' => true, 'type' => 'text', 'value' => '<div id="sk-footer" style="color:#FFF; background-color:#444; padding: 3px 2px 3px 2px; border-top: #888 solid 1px;">This blog is protected by <strong><a href="http://github.com/strider72/spam-karma" style="color:#C77; background-color:transparent;">Spam Karma</a></strong>: <strong>{hell}</strong> Spams eaten and counting...</div>', 'caption' => 'Standard SK footer message: ', 'after' => '<em>The following variables will be replaced by their numeric values: {hell} {purgatory} {paradise} {hell_total_karma} and {paradise_total_karma}</em>', 'size' => 50),
// no default UI:
'auto_purge_spamlist' => array('value' => false, 'type' => 'checkbox'),
'purge_spamlist_duration' => array('value' => 30, 'type' => 'text', 'size' => 3),
'purge_spamlist_unit' => array('value' => 'DAY', 'type' => 'menu', 'options' => array('DAY' => 'days', 'HOUR' => 'hours', 'MINUTE' => 'minutes')),
'auto_purge_blacklist' => array('value' => true, 'type' => 'checkbox'),
'purge_blacklist_duration' => array('value' => 30, 'type' => 'text', 'size' => 3),
'purge_blacklist_unit' => array('value' => 'DAY', 'type' => 'menu', 'options' => array('DAY' => 'days', 'HOUR' => 'hours', 'MINUTE' => 'minutes')),
'purge_blacklist_score' => array('value' => 99, 'type' => 'text', 'size' => 3),
'purge_blacklist_criterion' => array('value' => 'last_used', 'type' => 'menu', 'options' => array('added' => 'added', 'last_used' => 'last used')),
'auto_purge_logs' => array('value' => true, 'type' => 'checkbox'),
'purge_logs_duration' => array('value' => 7, 'type' => 'text', 'size' => 3),
'purge_logs_unit' => array('value' => 'DAY', 'type' => 'menu', 'options' => array('DAY' => 'days', 'HOUR' => 'hours', 'MINUTE' => 'minutes')),
'purge_logs_level' => array('value' => 8, 'type' => 'text', 'size' => 3),
);
function __construct()
{
$this->refresh_settings();
}
function refresh_settings()
{
foreach (array('plugins_settings', 'core_settings', 'stats') as $this_group)
{
$this->$this_group = get_option('sk_' . $this_group);
if (! is_array($this->$this_group))
$this->$this_group = array();
}
}
function save_settings()
{
if (! $this->need_to_save)
return;
$this->need_to_save = false;
$this->log_msg(__('Saved all settings to DB.', 'spam-karma'), 1);
update_option('sk_core_settings', $this->core_settings);
update_option('sk_plugins_settings', $this->plugins_settings);
update_option('sk_stats', $this->stats);
}
function get_core_settings( $section = 0 )
{
if ( $section )
{
if ( isset( $this->core_settings[$section] ) )
return $this->core_settings[$section];
else if ( isset( $this->core_defaults[$section]['value'] ) )
return $this->core_defaults[$section]['value'];
else
return $this->core_settings;
}
else
return $this->core_settings;
}
function get_plugin_settings($plugin)
{
if ( isset( $this->plugins_settings[$plugin] ) )
return $this->plugins_settings[$plugin];
else
return null;
}
function get_stats($section = 0)
{
if ($section)
return @$this->stats[$section];
else
return @$this->stats;
}
function increment_stats($section, $value = 1)
{
$this->need_to_save = true;
$this->stats[$section] = (int) @$this->stats[$section] + $value;
}
function set_core_settings ($settings, $section = 0)
{
$this->need_to_save = true;
if ($section)
$this->core_settings[$section] = $settings;
else
$this->core_settings = $settings;
}
function reset_plugin_settings ()
{
$this->need_to_save = true;
$this->plugins_settings = array();
}
function reset_all_settings ()
{
$this->need_to_save = true;
foreach (array('plugins_settings', 'core_settings', 'stats') as $this_group)
{
$this->$this_group = array();
}
}
function set_plugin_settings ($plugin_settings, $plugin_name)
{
$this->need_to_save = true;
$this->plugins_settings[$plugin_name] = $plugin_settings;
}
function set_plugins_settings ($plugins_settings)
{
$this->need_to_save = true;
$this->plugins_settings = $plugins_settings;
}
function set_stats ($stats, $section = 0)
{
$this->need_to_save = true;
if($section)
$this->stats['section'] = $stats;
else
$this->stats = $stats;
}
function log_msg($msg, $level = 0)
{
global $sk_log;
$sk_log->log_msg($msg, $level, 0, 'sk_settings');
}
function is_wp20 ()
{
// Merged from sk_wp2compatibility_plugin by Drac (http://lair.fierydragon.org)
global $wp_db_version;
// this function will differentiate between a 1.5 and previous codebase and a 1.6 - 2.0 codebase. The $wp_db_version variable is new to 1.6-2.0
/* the $wp_db_version variable was introduced in changeset 2966 but the comment_count field was only added in changeset 3104. So a further check is required for anyone still running an older 1.6-alpha-do-not-use */
if (isset ($wp_db_version) )
{
if ($wp_db_version >= 3104) {
return true;
}
}
return false;
}
}
class sk_log
{
var $logs = array();
var $db_threshold;
var $live_threshold;
var $live_output = true;
function __construct($db_threshold = 5, $live_threshold = 7)
{
global $wpdb;
$wpdb->hide_errors();
$this->db_threshold = $db_threshold;
$this->live_threshold = $live_threshold;
}
function log_msg($msg, $level = 0, $comment_id = 0, $component = "", $live = false, $div_wrapper = true)
{
global $wpdb;
if ($this->live_output && ($level >= $this->live_threshold || $live))
{
if ($div_wrapper)
echo "<div class=\"wrap sk_first\">\n";
echo "<div class=\"sk_log sk_level_$level\">$msg</div>";
if ($div_wrapper)
echo '</div>';
$echoed = true;
}
else
$echoed = false;
$this->logs[] = array($msg, $level, $comment_id, time(), $echoed);
if ($level >= $this->db_threshold)
@$wpdb->query("INSERT INTO `". SK_LOGTABLE ."` SET `msg` = '" . sk_escape_string($msg) . "', `component` = '" . sk_escape_string($component) . "', `level` = $level, `ts` = NOW()" );
}
function log_msg_mysql($msg, $level = 0, $comment_id = 0, $component = "")
{
$msg .= "<br/>\n" . __('SQL error: ', 'spam-karma') . '<code>'. mysql_error() . '</code>';
$this->log_msg($msg, $level, $comment_id, $component);
}
function dump_logs($threshold = 0)
{
foreach ($this->logs as $log)
if ($log[1] >= $threshold)
echo "<div class=\"sk_log sk_level_$log[1]\">" . __($log[3], 'spam-karma') . ' - ' . $log[0] . "</div>\n";
}
function echo_logs($threshold = 0)
{
$output = "";
foreach ($this->logs as $log)
if (! $log[4] && ($log[1] >= $threshold))
$output .= "<div class=\"sk_log sk_level_$log[1]\">" . __($log[3], 'spam-karma') . " - " . $log[0] . "</div>\n";
if ($output)
echo "<div class=\"wrap sk_first\">\n$output<div>";
}
}
?>