C:\xampp\htdocs\landing\wp-content\plugins\better-wp-security\core\lib\log-util.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
<?php

final class ITSEC_Log_Util {
    public static function 
get_type_counts$min_timestamp ) {
        global 
$wpdb;

        
$where = [];
        
$prepare = [];

        if ( 
$min_timestamp ) {
            
$where[]        = 'init_timestamp > %s';
            
$prepare[] = date'Y-m-d H:i:s'$min_timestamp );
        }

        if ( 
$where ) {
            
$where 'WHERE ' implode' AND '$where );
        } else {
            
$where '';
        }

        
$query "SELECT type, COUNT(*) AS count FROM `{$wpdb->base_prefix}itsec_logs` {$where} GROUP BY type";

        if ( ! empty( 
$prepare ) ) {
            
$query $wpdb->prepare$query$prepare );
        }

        
$results $wpdb->get_results$queryARRAY_A );

        
$counts = array();

        foreach ( 
$results as $result ) {
            if ( 
'process-start' === $result['type'] ) {
                
$result['type'] = 'process';
            }

            if ( isset( 
$counts[$result['type']] ) ) {
                
$counts[$result['type']] += $result['count'];
            } else {
                
$counts[$result['type']] = $result['count'];
            }
        }

        return 
$counts;
    }

    public static function 
get_modules() {
        global 
$wpdb;

        
$items $wpdb->get_col"SELECT DISTINCT module FROM {$wpdb->base_prefix}itsec_logs" );

        if ( ! 
is_array$items ) ) {
            return array();
        }

        
$modules = array();

        foreach ( 
$items as $module_slug ) {
            
$labels ITSEC_Modules::get_labels$module_slug );

            if ( ! 
$labels ) {
                
$labels ITSEC_Modules::get_labelsstr_replace'_''-'$module_slug ) );
            }

            if ( 
$labels ) {
                
$modules$module_slug ] = $labels['title'];
            } else {
                
$modules$module_slug ] = $module_slug;
            }
        }

        return 
$modules;
    }

    public static function 
get_entries$filters = array(), $limit 100$page 1$sort_by_column 'timestamp'$sort_direction 'DESC'$columns false ) {
        global 
$wpdb;


        
$valid_columns = array(
            
'id',
            
'parent_id',
            
'module',
            
'type',
            
'code',
            
'timestamp',
            
'init_timestamp',
            
'remote_ip',
            
'user_id',
            
'url',
            
'memory_current',
            
'memory_peak',
        );


        
$get_count false;
        
$min_timestamp $max_timestamp false;

        if ( isset( 
$filters['__get_count'] ) ) {
            if ( 
$filters['__get_count'] ) {
                
$get_count true;
            }

            unset( 
$filters['__get_count'] );
        }

        if ( isset( 
$filters['__min_timestamp'] ) ) {
            
$min_timestamp $filters['__min_timestamp'];
            unset( 
$filters['__min_timestamp'] );
        }

        if ( isset( 
$filters['__max_timestamp'] ) ) {
            
$max_timestamp $filters['__max_timestamp'];
            unset( 
$filters['__max_timestamp'] );
        }


        
$limit max0min100intval$limit ) ) );
        
$page max1intval$page ) );

        if ( 
is_array$sort_by_column ) ) {
            
$regex_valid_columns '(?:' implode'|'$valid_columns ) . ')';

            foreach ( 
$sort_by_column as $index => $sort_by ) {
                if ( 
in_array$sort_by$valid_columns ) ) {
                    
$sort_by_column[$index] = "$sort_by DESC";
                } else if ( ! 
preg_match"/^$regex_valid_columns\s+(?:DESC|ASC)$/i"$sort_by ) ) {
                    unset( 
$sort_by_column[$index] );
                }
            }

            if ( empty( 
$sort_by_column ) ) {
                
$sort_by_column 'timestamp';
            }
        } else if ( ! 
in_array$sort_by_column$valid_columns ) ) {
            
$sort_by_column 'timestamp';
        }

        
$sort_direction strtoupper$sort_direction );
        if ( ! 
in_array$sort_direction, array( 'DESC''ASC' ) ) ) {
            
$sort_direction 'DESC';
        }

        if ( 
false === $columns ) {
            
$columns $valid_columns;
        } else if ( 
'all' === $columns ) {
            
$columns array_merge$valid_columns, array( 'data' ) );
        }


        if ( 
$get_count ) {
            
$query "SELECT COUNT(*) FROM `{$wpdb->base_prefix}itsec_logs`";
        } else {
            
$query "SELECT " implode', '$columns ) . " FROM `{$wpdb->base_prefix}itsec_logs`";
        }

        
$prepare_args = array();


        
$where_entries = array();

        foreach ( (array) 
$filters as $column => $value ) {
            if ( 
preg_match'/^(.+)_not$/'$column$match ) ) {
                
$not true;
                
$column $match[1];
            } else {
                
$not false;
            }

            if ( 
preg_match'/^(.+)_(min|max)$/'$column$match ) ) {
                if ( ! 
in_array$match[1], $valid_columns ) ) {
                    continue;
                }

                if ( 
'min' === $match[2] ) {
                    
$where_entries[] = "'$column'>=%s";
                    
$prepare_args[] = $value;
                } else {
                    
$where_entries[] = "'column'<=%s";
                    
$prepare_args[] = $value;
                }
            } else if ( ! 
in_array$column$valid_columns ) ) {
                continue;
            } else if ( 
is_array$value ) ) {
                if ( ! empty( 
$value ) ) {
                    if ( 
$not ) {
                        
$where_entries[] = "$column NOT IN (" implode', 'array_fill0count$value ), '%s' ) ) . ")";
                    } else {
                        
$where_entries[] = "$column IN (" implode', 'array_fill0count$value ), '%s' ) ) . ")";
                    }
                    
$prepare_args array_merge$prepare_args$value );
                }
            } else if ( 
false !== strpos$value'%' ) ) {
                if ( 
$not ) {
                    
$where_entries[] = "$column NOT LIKE %s";
                } else {
                    
$where_entries[] = "$column LIKE %s";
                }
                
$prepare_args[] = $value;
            } else {
                if ( 
$not ) {
                    
$where_entries[] = "$column<>%s";
                } else {
                    
$where_entries[] = "$column=%s";
                }
                
$prepare_args[] = $value;
            }
        }

        if ( 
false !== $min_timestamp ) {
            
$where_entries[] = 'timestamp>%s';
            
$prepare_args[] = date'Y-m-d H:i:s'$min_timestamp );
        }

        if ( 
false !== $max_timestamp ) {
            
$where_entries[] = 'timestamp<%s';
            
$prepare_args[] = date'Y-m-d H:i:s'$max_timestamp );
        }

        if ( 
$where_entries ) {
            
$query .= ' WHERE ' implode' AND '$where_entries );
        }

        if ( ! 
$get_count ) {
            if ( ! 
is_array$sort_by_column ) ) {
                
$sort_by_column = array( "$sort_by_column $sort_direction);
            }

            
$query .= ' ORDER BY ' implode', '$sort_by_column );


            if ( 
$limit ) {
                
$offset = ( $page ) * $limit;
                
$query .= " LIMIT $offset,$limit";
            }
        }

        if ( 
$prepare_args ) {
            
$query $wpdb->prepare$query$prepare_args );
        }

        if ( 
$get_count ) {
            return 
intval$wpdb->get_var$query ) );
        }

        
$rows $wpdb->get_results$queryARRAY_A );

        if ( 
is_null$rows ) ) {
            return new 
WP_Error'itsec-log-util-failed-query'sprintfesc_html__'A query failure prevented the log data from being accessed: %s''better-wp-security' ), $wpdb->last_error ) );
        }

        foreach ( 
$rows as $index => $row ) {
            if ( ! isset( 
$row['data'] ) ) {
                break;
            }

            
$data unserialize$row['data'] );

            if ( 
false !== $data || 'b:0;' === $row['data'] ) {
                
$rows[$index]['data'] = $data;
            }
        }

        return 
$rows;
    }

    public static function 
get_logs_page_screen_options() {
        
$defaults = array(
            
'per_page'     => 20,
            
'default_view' => 'important',
            
'color'        => true,
            
'show_debug'   => false,
            
'show_process' => false,
            
'last_seen'    => 0,
        );

        
$options get_user_option'itsec_logs_page_screen_options' );

        if ( 
is_array$options ) ) {
            
$options array_merge$defaults$options );
        } else {
            
$options $defaults;

            if ( 
$user wp_get_current_user() ) {
                
update_user_option$user->ID'itsec_logs_page_screen_options'$optionstrue );
            }
        }

        return 
$options;
    }

    public static function 
set_logs_page_screen_options$options ) {
        if ( ! 
$user wp_get_current_user() ) {
            return;
        }

        if ( isset( 
$options['per_page'] ) && ( $options['per_page'] < || $options['per_page'] > 999 ) ) {
            unset( 
$options['per_page'] );
        }
        if ( isset( 
$options['default_view'] ) && ! in_array$options['default_view'], array( 'important''all''critical-issue' ) ) ) {
            unset( 
$options['default_view'] );
        }
        if ( isset( 
$options['last_seen'] ) ) {
            
$options['last_seen'] = intval$options['last_seen'] );

            if ( 
$options['last_seen'] < || $options['last_seen'] > ITSEC_Core::get_current_time_gmt() ) {
                unset( 
$options['last_seen'] );
            }
        }

        
$options array_mergeself::get_logs_page_screen_options(), $options );

        
update_user_option$user->ID'itsec_logs_page_screen_options'$optionstrue );
    }

    public static function 
has_old_log_entries() {
        global 
$wpdb;

        if ( ! 
$wpdb->get_var"SHOW TABLES LIKE '{$wpdb->prefix}itsec_log'" ) ) {
            return 
false;
        }

        
$num_entries $wpdb->get_var"SELECT COUNT(*) FROM {$wpdb->prefix}itsec_log" );

        if ( empty( 
$num_entries ) ) {
            
$wpdb->query"DROP TABLE IF EXISTS {$wpdb->prefix}itsec_log" );
        }

        return 
true;
    }

    public static function 
migrate_old_log_entries() {
        global 
$wpdb;

        
$max 50;
        
$num_entries $wpdb->get_var"SELECT COUNT(*) FROM {$wpdb->prefix}itsec_log" );
        
$num_loops min$max$num_entries );

        for ( 
$count 1$count <= $num_loops$count++ ) {
            
$old_entry $wpdb->get_row"SELECT * FROM {$wpdb->prefix}itsec_log ORDER BY log_date_gmt LIMIT 1"ARRAY_A );
            
$entry self::get_new_log_entry_from_old$old_entry );

            
$wpdb->insert"{$wpdb->base_prefix}itsec_logs"$entry );
            
$wpdb->query$wpdb->prepare"DELETE FROM {$wpdb->prefix}itsec_log WHERE log_id=%d"$old_entry['log_id'] ) );
        }

        if ( 
$num_entries $max ) {
            return 
false;
        }

        
$wpdb->query"DROP TABLE IF EXISTS {$wpdb->prefix}itsec_log" );

        return 
true;
    }

    public static function 
get_new_log_entry_from_old$old_entry ) {
        
$old_entry['log_data'] = unserialize$old_entry['log_data'] );

        
$entry = array(
            
'module'         => $old_entry['log_type'],
            
'code'           => $old_entry['log_function'],
            
'type'           => 'notice',
            
'user_id'        => $old_entry['log_user'],
            
'timestamp'      => $old_entry['log_date_gmt'],
            
'init_timestamp' => $old_entry['log_date_gmt'],
            
'remote_ip'      => $old_entry['log_host'],
            
'url'            => $old_entry['log_url'],
        );

        if ( 
'lockout' === $old_entry['log_type'] ) {
            if ( isset( 
$old_entry['log_data']['expires'] ) ) {
                
$entry['type'] = 'action';

                if ( empty( 
$old_entry['log_host'] ) ) {
                    
$entry['code'] = 'user-lockout';
                } else {
                    
$entry['code'] = 'host-lockout';
                }
            } else {
                if ( empty( 
$old_entry['log_host'] ) ) {
                    
$entry['code'] = 'whitelisted-host-triggered-user-lockout';
                } else {
                    
$entry['code'] = 'whitelisted-host-triggered-host-lockout';
                }
            }
        } else if ( 
'file_change' === $old_entry['log_type'] ) {
            
$entry['type'] = 'warning';
            
$entry['code'] = 'changes-found';
            
$entry['data'] = $old_entry['log_data'];
        } else if ( 
'malware' === $old_entry['log_type'] ) {
            
$entry['code'] = 'scan';
            
$entry['data'] = array( 'results' => $old_entry['log_data'] );
        } else if ( 
'backup' === $old_entry['log_type'] ) {
            
$entry['code'] = 'details';
        } else if ( 
'four_oh_four' === $old_entry['log_type'] ) {
            
$entry['code'] = 'found_404';
        } else if ( 
'ipcheck' === $old_entry['log_type'] ) {
            if ( empty( 
$old_entry['log_data'] ) ) {
                
$entry['code'] = 'failed-login-by-blocked-ip';
            } else {
                
$entry['type'] = 'action';
                
$entry['code'] = 'ip-blocked';
            }
        } else if ( 
'brute_force' === $old_entry['log_type'] ) {
            if ( 
'admin' === $old_entry['log_username'] ) {
                
$entry['code'] = 'auto-ban-admin-username';
            } else {
                
$entry['code'] = 'invalid-login';
            }
        } else if ( 
'away_mode' === $old_entry['log_type'] ) {
            
$entry['code'] = 'away-mode-active';
        } else if ( 
'recaptcha' === $old_entry['log_type'] ) {
            
$entry['code'] = 'failed-validation';
        } else if ( 
'user_logging' === $old_entry['log_type'] ) {
            if ( isset( 
$old_entry['log_data']['post'] ) ) {
                
$entry['code'] = 'post-status-changed';
            } else if ( empty( 
$old_entry['log_username'] ) ) {
                
$entry['code'] = 'user-logged-out';
            } else {
                
$entry['code'] = 'user-logged-in';
            }
        }

        if ( isset( 
$entry['data'] ) ) {
            
$entry['data'] = serialize$entry['data'] );
        }

        return 
$entry;
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586