forked from 10up/wpcli-vulnerability-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-vulnerability-scanner.php
More file actions
900 lines (743 loc) · 21.3 KB
/
wp-vulnerability-scanner.php
File metadata and controls
900 lines (743 loc) · 21.3 KB
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
891
892
893
894
895
896
897
898
899
900
<?php
/**
* Plugin Name: WP Vulnerability Scanner
* Plugin URI: https://github.com/10up/wp-vulnerability-scanner
* Description: WP-CLI command only.
* Check WordPress code, installed plugins and themes for vulnerabilities reported by wpvulndb.com.
* Version: 0.0.2
* Author: 10up
* Author URI: http://10up.com
* License: MIT
*/
if ( ! defined( 'WP_CLI' ) ) {
return;
}
/**
* WP Vulnerability Scanner
*/
class Vulnerability_CLI extends WP_CLI_Command {
/**
*/
var $porcelain;
/**
*/
var $test;
/**
*/
var $update_list;
/**
*/
var $assoc_args_wordpress;
/**
*/
var $assoc_args_plugin;
/**
*/
var $assoc_args_theme;
/**
* Flag for nagios
*/
var $nagios_op;
/**
* Flag for mail
*/
var $mail;
/**
* Check plugins and themes for reported vulnerabilities.
*
* ## OPTIONS
*
* [--test]
* : Load test plugin/theme data
*
* [--nagios]
* : Output for nagios
*
* [--mail]
* : Mail nagios output if any vulnerability found
*
* [--format=<format>]
* : Accepted values: table, csv, json, count, ids, yaml. Default: table
*
* ## EXAMPLES
*
* wp vuln status
* wp vuln status --not-themes
*
* @subcommand status
*/
function status( $args, $assoc_args ) {
$this->porcelain = false;
$this->test = isset( $assoc_args['test'] );
$this->nagios_op = isset( $assoc_args['nagios'] );
$this->mail = isset( $assoc_args['mail'] ) ? $assoc_args['mail'] : '';
$this->update_list = array();
// need a copy because it's passed by ref and destroyed in the formatter
$this->assoc_args_plugin = $assoc_args;
$this->assoc_args_theme = $assoc_args;
$this->assoc_args_wordpress = $assoc_args;
global $wp_version;
if( $this->nagios_op ) {
$this->_do_nagios_op( array( 'wordpress', 'plugin', 'theme' ) );
}
WP_CLI::log( WP_CLI::colorize( '%GWordPress ' . $wp_version . ' %n' ) );
$this->_do_wordpress();
WP_CLI::log( WP_CLI::colorize( '%GPlugins%n' ) );
$this->_do_plugins();
WP_CLI::log( WP_CLI::colorize( '%GThemes%n' ) );
$this->_do_themes();
}
/**
* Check WordPress core for reported vulnerabilities.
*
* ## OPTIONS
*
* [--nagios]
* : Output for nagios
*
* [--mail]
* : Mail nagios output if any vulnerability found
*
* [--format=<format>]
* : Accepted values: table, csv, json, count, ids, yaml. Default: table
*
* ## EXAMPLES
*
* wp vuln core-status
*
* @subcommand core-status
*/
function core_status( $args, $assoc_args ) {
$this->porcelain = isset( $assoc_args['porcelain'] );
$this->test = isset( $assoc_args['test'] );
$this->nagios_op = isset( $assoc_args['nagios'] );
$this->mail = isset( $assoc_args['mail'] ) ? $assoc_args['mail'] : '';
$this->update_list = array();
// need a copy because it's passed by ref and destroyed in the formatter
$this->assoc_args_wordpress = $assoc_args;
if( $this->nagios_op ) {
$this->_do_nagios_op( array( 'wordpress' ) );
}
$this->_do_wordpress();
}
/**
* Check plugins for reported vulnerabilities.
*
* ## OPTIONS
*
* [--porcelain]
* : Print only slugs of plugins with updates
*
* [--test]
* : Load test plugin/theme data
*
* [--nagios]
* : Output for nagios
*
* [--mail]
* : Mail nagios output if any vulnerability found
*
* [--format=<format>]
* : Accepted values: table, csv, json, count, ids, yaml. Default: table
*
* ## EXAMPLES
*
* wp vuln plugin-status
*
* @subcommand plugin-status
*/
function plugin_status( $args, $assoc_args ) {
$this->porcelain = isset( $assoc_args['porcelain'] );
$this->test = isset( $assoc_args['test'] );
$this->nagios_op = isset( $assoc_args['nagios'] );
$this->mail = isset( $assoc_args['mail'] ) ? $assoc_args['mail'] : '';
$this->update_list = array();
// need a copy because it's passed by ref and destroyed in the formatter
$this->assoc_args_plugin = $assoc_args;
if( $this->nagios_op ) {
$this->_do_nagios_op( array( 'plugin' ) );
}
$this->_do_plugins();
}
/**
* Check themes for reported vulnerabilities.
*
* ## OPTIONS
*
* [--porcelain]
* : Print only slugs of themes with updates
*
* [--test]
* : Load test theme/theme data
*
* [--nagios]
* : Output for nagios
*
* [--format=<format>]
* : Accepted values: table, csv, json, count, ids, yaml. Default: table
*
* ## EXAMPLES
*
* wp vuln theme-status
*
* @subcommand theme-status
*/
function theme_status( $args, $assoc_args ) {
$this->porcelain = isset( $assoc_args['porcelain'] );
$this->test = isset( $assoc_args['test'] );
$this->nagios_op = isset( $assoc_args['nagios'] );
$this->update_list = array();
// need a copy because it's passed by ref and destroyed in the formatter
$this->assoc_args_theme = $assoc_args;
if( $this->nagios_op ) {
$this->_do_nagios_op( array( 'theme' ) );
}
$this->_do_themes();
}
/**
* Check any given theme.
*
* ## OPTIONS
*
* [<slug>...]
* : theme slug to check
*
* [--version]
* : Version if other than latest. Only applies if one slug provided
*
* [--format=<format>]
* : Accepted values: table, csv, json, count, ids, yaml. Default: table
*
* ## EXAMPLES
*
* wp vuln theme-check
*
* @subcommand theme-check
*/
function theme_check( $args, $assoc_args ) {
if ( count( $args ) > 1 ) {
$version = 0;
} else {
$version = isset( $assoc_args['version'] ) ? $assoc_args['version'] : 0;
}
$display = array();
foreach( $args as $slug ) {
$status = $this->_check_status( $slug, $version, 'theme' );
$display = array_merge ( $display, $status );
}
$formatter = new \WP_CLI\Formatter( $assoc_args, array(
'name',
'installed version',
'status',
'fix'
), 'themes' );
$formatter->display_items( $display );
}
/**
* Check any given plugin.
*
* ## OPTIONS
*
* [<slug>...]
* : plugin slug to check
*
* [--version]
* : Version if other than latest. Only applies if one slug provided
*
* [--format=<format>]
* : Accepted values: table, csv, json, count, ids, yaml. Default: table
*
* ## EXAMPLES
*
* wp vuln plugin-check
*
* @subcommand plugin-check
*/
function plugin_check( $args, $assoc_args ) {
if ( count( $args ) > 1 ) {
$version = 0;
} else {
$version = isset( $assoc_args['version'] ) ? $assoc_args['version'] : 0;
}
$display = array();
foreach( $args as $slug ) {
$status = $this->_check_status( $slug, $version, 'plugin' );
$display = array_merge ( $display, $status );
}
$formatter = new \WP_CLI\Formatter( $assoc_args, array(
'name',
'installed version',
'status',
'fix'
), 'plugins' );
$formatter->display_items( $display );
}
/**
* Do WordPress core check
*
* @return void
*/
private function _do_wordpress() {
$singular_type = 'wordpress';
$plural_type = 'wordpresses';
$assoc_args = "assoc_args_{$singular_type}";
$display = $this->_check_wordpress();
$update_list = $this->_extract_updatable_items( $display );
// Pretty print
if ( ! $this->porcelain ) {
$formatter = new \WP_CLI\Formatter( $this->$assoc_args, array(
'name',
'installed version',
'status',
'fix',
), $plural_type );
$formatter->display_items( $display );
} elseif ( $update_list ) {
WP_CLI::log( implode( ' ', $update_list ) );
die;
}
}
/**
* Helper. Call worker for plugin tasks
*
* @return void
*/
private function _do_plugins() {
$this->_do_thing( 'plugin' );
}
/**
* Helper. Call worker for theme tasks
*
* @return void
*/
private function _do_themes() {
$this->_do_thing( 'theme' );
}
/**
* Worker. Do what's needed for plugins/themes
* Display table, or if --porcelain, display only updatable slugs
*
* @param string|array $type The "thing" we're checking
* If string, it's pluralized with an "s"
* If array, should be [ single, plural ]
*
* @return void
*/
private function _do_thing( $type ) {
list( $singular_type, $plural_type ) = $this->_get_slugs( $type );
$assoc_args = "assoc_args_{$singular_type}";
$display = $this->_check_thing( $singular_type );
$update_list = $this->_extract_updatable_items( $display );
// Pretty print
if ( ! $this->porcelain ) {
$formatter = new \WP_CLI\Formatter( $this->$assoc_args, array(
'name',
'installed version',
'status',
'fix',
), $plural_type );
$formatter->display_items( $display );
// if plugins need updating, do or tell the user
if ( $update_list ) {
// it would be nice to show this, but we'd need to rewrite the unit test
// $update_list = implode( ' ', $update_list );
// WP_CLI::log( "Run `wp $singular_type update $update_list`" );
} else {
WP_CLI::log( 'Nothing to update' );
}
} elseif ( $update_list ) {
WP_CLI::log( implode( ' ', $update_list ) );
die;
}
}
/**
* Worker. Run through checking the status of a plugin/theme
*
* @param string|array $type The "thing" we're checking
* If string, it's pluralized with an "s"
* If array, should be [ single, plural ]
*
* @return array Statuses for all themes
*/
private function _check_thing( $type ) {
list( $singular_type ) = $this->_get_slugs( $type );
$list = WP_CLI::launch_self( $singular_type, array( 'list' ), array(
'format' => 'csv',
'fields' => 'name,version',
), true, true );
$list = $this->parse_list( $list );
# test list
if ( isset( $this->test ) && $this->test ) {
switch ( $type ) {
case 'plugin':
$list = array(
array( 'name' => 'relevant', 'version' => '1.0.2' ), // fixed vulns
// array( 'name' => 'wordpress-seo', 'version' => '3.2.5' ), // no vulns
// array( 'name' => 'revslider', 'version' => '1.0.0' ), // fixed vulns
// array( 'name' => 'fluid-respnsive-slideshow', 'version' => '2.2.6' ), // ongoing vulns
);
break;
case 'theme':
$list = array(
array( 'name' => 'digital-store', 'version' => '1.3' ), // fixed vulns
// array( 'name' => 'twentyten', 'version' => '1.0.0' ), // no vulns
// array( 'name' => 'twentyfifteen', 'version' => '1.0.0' ), // fixed vulns
// array( 'name' => 'epic', 'version' => '1.0.0' ), // ongoing vulns
);
break;
}
}
#/
$data = array();
foreach ( $list as $thing ) {
$status = $this->_check_status( $thing['name'], $thing['version'], $singular_type );
$data = array_merge( $data, $status );
}
return $data;
}
/**
* Worker for WordPress vulnerability checks
*
* @return array
*/
private function _check_wordpress() {
$singular_type = 'wordpress';
$plural_type = 'wordpresses';
global $wp_version;
$version = str_replace( '.', '', $wp_version );
$url = "https://wpvulndb.com/api/v1/{$plural_type}/{$version}";
$key = 'vuln_check-' . md5( $url );
if ( ! ( $response = get_transient( $key ) ) ) {
$response = wp_remote_get( $url );
set_transient( $key, $response, HOUR_IN_SECONDS );
} else {
WP_CLI::debug( "Use response cache for $url" );
}
$code = wp_remote_retrieve_response_code( $response );
$body = wp_remote_retrieve_body( $response );
$assoc_args = "assoc_args_{$singular_type}";
$table_format = false;
if ( ! isset( $this->{$assoc_args}['format'] ) || 'table' == $this->{$assoc_args}['format'] ) {
$table_format = true;
}
$report = array();
if ( 404 == $code ) {
$report[] = array(
'title' => "No reported vulnerabilities for WordPress " . $wp_version,
'fix' => 'n/a',
);
} else {
// let's analyse the report!
$vulndb = json_decode( $body );
$vulnerabilities = $vulndb->$singular_type->vulnerabilities;
if( is_array( $vulnerabilities ) ) {
foreach ( $vulnerabilities as $k => $vuln ) {
// vulnerability that hasn't been fixed :(
if ( ! isset( $vuln->fixed_in ) ) {
$report[] = array(
'title' => $vuln->title,
'fix' => 'Not fixed',
'action' => 'watch',
);
// vuln version, fix available
} elseif ( version_compare( $wp_version, $vuln->fixed_in, '<' ) ) {
$report[] = array(
'title' => $vuln->title,
'fix' => "Fixed in {$vuln->fixed_in}",
'action' => 'update',
);
// if installed plugin version is greater than a fixed version,
// unset that vuln entry, we don't need it
} else {
// This leaves us with an array of relevant vulns
// not currently used :/
unset( $vulnerabilities[ $k ] );
}
}
}
$total = count( $report );
if ( $total <= 0 ) {
$report[] = array(
'title' => "No vulnerabilities reported for this version of WordPress",
'fix' => 'n/a',
);
}
}
$data = array();
foreach ( $report as $index => $stat ) {
if ( ! isset( $stat['action'] ) ) {
$stat['action'] = '';
}
$name = ( $table_format && 0 != $index ? '' : 'WordPress' );
if ( $table_format ) {
switch ( $stat['action'] ) {
case 'update' :
$name = WP_CLI::colorize( "%r$name%n" );
break;
case 'watch' :
$name = WP_CLI::colorize( "%y$name%n" );
break;
default;
}
}
// these keys must match the column headings in the formatter (extras ok)
$data[] = array(
'name' => $name,
'slug' => 'wordpress',
'installed version' => $wp_version,
'status' => $stat['title'],
'fix' => $stat['fix'],
'action' => isset( $stat['action'] ) ? $stat['action'] : '',
);
}
return $data;
}
/**
* Pull updatable items from Formatter-ready array
*
* @param array $data Formatter-ready data
*
* @return array Simple array of plugin/theme slugs needing to be updated
*/
private function _extract_updatable_items( $data ) {
$update_list = wp_list_pluck( wp_list_filter( $data, array( 'action' => 'update' ) ), 'action', 'slug' );
$update_list = array_keys( $update_list );
return $update_list;
}
/**
* Check wpvulndb.com for reports
* Total how many are relevant
*
* @param string $slug Installed plugin/theme slug
* @param string $version Installed plugin/theme version
* @param string|array $type The "thing" we're checking, "plugin" or "theme"
* If string, it's pluralized with an "s"
* If array, should be [ single, plural ]
*
* @return array Data array
*/
private function _check_status( $slug, $version, $type ) {
list( $singular_type, $plural_type ) = $this->_get_slugs( $type );
$url = "https://wpvulndb.com/api/v1/{$plural_type}/{$slug}";
$key = 'vuln_check-' . md5( $url );
if ( ! ( $response = get_transient( $key ) ) ) {
$response = wp_remote_get( $url );
set_transient( $key, $response, HOUR_IN_SECONDS );
} else {
WP_CLI::debug( "Use response cache for $url" );
}
$code = wp_remote_retrieve_response_code( $response );
$body = wp_remote_retrieve_body( $response );
$assoc_args = "assoc_args_{$singular_type}";
$table_format = false;
if ( ! isset( $this->{$assoc_args}['format'] ) || 'table' == $this->{$assoc_args}['format'] ) {
$table_format = true;
}
$report = array();
if ( 404 == $code ) {
$report[] = array(
'title' => "No vulnerabilities reported for $slug",
'fix' => 'n/a',
);
} else {
// let's analyse the report!
$vulndb = json_decode( $body );
$vulnerabilities = array();
if( isset( $vulndb->$singular_type ) && isset( $vulndb->$singular_type->vulnerabilities ) ) {
$vulnerabilities = $vulndb->$singular_type->vulnerabilities;
}
if( is_array( $vulnerabilities ) && ! empty( $vulnerabilities ) ) {
foreach ( $vulnerabilities as $k => $vuln ) {
// vulnerability that hasn't been fixed :(
if ( ! isset( $vuln->fixed_in ) ) {
$report[] = array(
'title' => $vuln->title,
'fix' => 'Not fixed',
'action' => 'watch',
);
// vuln version, fix available
} elseif ( version_compare( $version, $vuln->fixed_in, '<' ) ) {
$report[] = array(
'title' => $vuln->title,
'fix' => "Fixed in {$vuln->fixed_in}",
'action' => 'update',
);
// if installed plugin version is greater than a fixed version,
// unset that vuln entry, we don't need it
} else {
// This leaves us with an array of relevant vulns
// not currently used :/
unset( $vulnerabilities[ $k ] );
}
}
}
$total = count( $report );
if ( $total <= 0 ) {
$report[] = array(
'title' => "No vulnerabilities reported for this version of $slug",
'fix' => 'n/a',
);
}
}
$data = array();
$last_item = '';
foreach ( $report as $stat ) {
if ( ! isset( $stat['action'] ) ) {
$stat['action'] = '';
}
$name = ( $table_format && $slug == $last_item ? '' : $slug );
if ( $table_format ) {
switch ( $stat['action'] ) {
case 'update' :
$name = WP_CLI::colorize( "%r$name%n" );
break;
case 'watch' :
$name = WP_CLI::colorize( "%y$name%n" );
break;
default;
}
}
// these keys must match the column headings in the formatter (extras ok)
$data[] = array(
'name' => $name,
'slug' => $slug,
'installed version' => $version,
'status' => $stat['title'],
'fix' => $stat['fix'],
'action' => isset( $stat['action'] ) ? $stat['action'] : '',
);
$last_item = $slug;
}
return $data;
}
/**
* Get singular and plural slugs for given string or array
*
* @param string|array $singular_or_array If string, it's pluralized with an "s"
* If array, should be [ single, plural ]
*
* @return array
*/
private function _get_slugs( $singular_or_array ) {
if ( is_array( $singular_or_array ) ) {
$singular_type = $singular_or_array[0];
$plural_type = $singular_or_array[1];
} else {
$singular_type = $singular_or_array;
$plural_type = "{$singular_or_array}s";
}
return array( $singular_type, $plural_type );
}
/**
* Parse list string into item array
*
* @param string $list
*
* @return array
*/
private function parse_list( $list ) {
$list = explode( "\n", $list );
$newlist = array();
foreach ( $list as $line ) {
// skip printed command
if ( '$ ' === substr( $line, 0, 2 ) ) {
continue;
}
// skip output header
if ( 'name,version' === $line ) {
continue;
}
// skip output footer
if ( empty( $line ) ) {
break;
}
$newlist[] = array_combine(
array( 'name', 'version' ),
explode( ',', $line )
);
}
return $newlist;
}
/**
* Do nagios output
*
* @param $things
*/
private function _do_nagios_op( $things ) {
if( ! empty( $things ) && is_array( $things ) ) {
$wp_list = $pl_list = $th_list = 0;
if( in_array( 'wordpress', $things ) ) {
$wp_vulns = $this->_check_wordpress();
if( ! empty( $wp_vulns ) && is_array( $wp_vulns ) ) {
foreach( $wp_vulns as $wp_vuln ) {
if ( isset( $wp_vuln[ 'fix' ] ) && 'n/a' != $wp_vuln[ 'fix' ] ) {
$wp_list++;
}
}
}
}
if( in_array( 'plugin', $things ) ) {
$plugins = $this->_check_thing( 'plugin' );
if( ! empty( $plugins ) && is_array( $plugins ) ) {
foreach( $plugins as $plugin ) {
if ( isset( $plugin[ 'fix' ] ) && 'n/a' != $plugin[ 'fix' ] ) {
$pl_list++;
}
}
}
}
if( in_array( 'theme', $things ) ) {
$themes = $this->_check_thing( 'theme' );
if( ! empty( $themes ) && is_array( $themes ) ) {
foreach( $themes as $theme ) {
if ( isset( $theme[ 'fix' ] ) && 'n/a' != $theme[ 'fix' ] ) {
$th_list++;
}
}
}
}
if( empty( $wp_list ) && empty( $pl_list ) && empty( $th_list ) ) {
WP_CLI::line( 'OK - no vulnerabilities found' );
exit(0);
} else {
$message = sprintf(
'CRITICAL - %d core, %d plugin and %d theme vulnerabilities found',
$wp_list,
$pl_list,
$th_list
);
WP_CLI::line( $message );
//Notify via mail
if( ! empty( $this->mail ) ) {
$site_url = site_url();
$headers = array();
/*
* In CLI if $_SERVER['SERVER_NAME'] isn't set than it fails to send mail.
* Reason being, this property is used to create "From" mail header.
* If it's empty, we need to pass that in header.
*/
if( empty( $_SERVER['SERVER_NAME'] ) ) {
$sitename = $site_url;
if ( substr( $sitename, 0, 8 ) == 'https://' ) {
$sitename = substr( $sitename, 8 );
}
if ( substr( $sitename, 0, 7 ) == 'http://' ) {
$sitename = substr( $sitename, 7 );
}
if ( substr( $sitename, 0, 4 ) == 'www.' ) {
$sitename = substr( $sitename, 4 );
}
$from_email = 'wordpress@' . $sitename;
$headers[] = "From:{$from_email}";
}
$subject = 'Vulnerabilities found in ' . $site_url;
if( ! wp_mail( $this->mail, $subject, $message, $headers ) ) {
WP_CLI::line( 'Not able to send mail.' );
}
}
exit(2);
}
}
}
}
WP_CLI::add_command( 'vuln', 'Vulnerability_CLI' );