-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphpguard-lite.php
More file actions
1357 lines (1184 loc) · 58.1 KB
/
phpguard-lite.php
File metadata and controls
1357 lines (1184 loc) · 58.1 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
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin Name: PHPGuard Lite
* Description: Check WordPress plugins and PHP code for syntax errors before activating or using them.
* Version: 1.0.0
* Author: M. Wouterse
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: phpguard-lite
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class PHPGuard_Preinstall_Plugin {
const VERSION = '1.0.0';
const OPTION_VERSION = 'phpguard_preinstall_version';
const OPTION_HISTORY = 'phpguard_version_history';
const OPTION_ACTIVATION_REDIRECT = 'phpguard_preinstall_activation_redirect';
public function __construct() {
// Lifecycle
register_activation_hook( __FILE__, array( $this, 'on_activate' ) );
add_action( 'plugins_loaded', array( $this, 'maybe_record_version_bump' ) );
add_action( 'admin_init', array( $this, 'maybe_redirect_after_activation' ) );
// Admin UI
add_action( 'admin_menu', array( $this, 'register_menus' ) );
// Ajax scan
add_action( 'wp_ajax_phpguard_run_scan', array( $this, 'ajax_run_scan' ) );
add_action( 'wp_ajax_phpguard_run_snippet_scan', array( $this, 'ajax_run_snippet_scan' ) );
// Assets
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
}
public function on_activate() {
$this->maybe_record_version_bump();
// Flag for one-time redirect to the main PHPGuard screen after activation.
update_option( self::OPTION_ACTIVATION_REDIRECT, true );
}
public function maybe_redirect_after_activation() {
// Only run in admin for users who can manage options.
if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) {
return;
}
// Only redirect once, immediately after activation.
$flag = get_option( self::OPTION_ACTIVATION_REDIRECT );
if ( ! $flag ) {
return;
}
// Do not hijack bulk activations.
if ( isset( $_GET['activate-multi'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
delete_option( self::OPTION_ACTIVATION_REDIRECT );
return;
}
delete_option( self::OPTION_ACTIVATION_REDIRECT );
wp_safe_redirect( admin_url( 'admin.php?page=phpguard-lite' ) );
exit;
}
/**
* Record a version bump into shared history option.
*/
public function maybe_record_version_bump() {
$stored = get_option( self::OPTION_VERSION );
if ( $stored === self::VERSION ) {
return;
}
update_option( self::OPTION_VERSION, self::VERSION );
$history = get_option( self::OPTION_HISTORY );
if ( ! is_array( $history ) ) {
$history = array();
}
$notes = $this->get_version_notes( self::VERSION );
$history[] = array(
'component' => 'preinstall_free',
'version' => self::VERSION,
'timestamp' => time(),
'notes' => $notes,
);
update_option( self::OPTION_HISTORY, $history );
}
/**
* Notes for known versions.
*
* @param string $version Version string.
* @return string
*/
protected function get_version_notes( $version ) {
switch ( $version ) {
case '1.0.0':
return __( 'Initial public release of PHPGuard Pre-Install Safety Checker.', 'phpguard-lite' );
}
}
public function register_menus() {
// Top-level PHPGuard menu
add_menu_page(
__( 'PHPGuard – Lite', 'phpguard-lite' ),
__( 'PHPGuard', 'phpguard-lite' ),
'manage_options',
'phpguard-lite',
array( $this, 'render_main_page' ),
'dashicons-shield-alt',
59
);
}
public function enqueue_assets( $hook ) {
if ( strpos( $hook, 'phpguard-lite' ) === false ) {
return;
}
wp_enqueue_style(
'phpguard-lite-admin',
plugin_dir_url( __FILE__ ) . 'assets/phpguard-admin.css',
array(),
self::VERSION
);
wp_enqueue_script(
'phpguard-lite-admin',
plugin_dir_url( __FILE__ ) . 'assets/phpguard-admin.js',
array( 'jquery' ),
self::VERSION,
true
);
wp_localize_script(
'phpguard-lite-admin',
'PHPGuardFree',
array(
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'phpguard_run_scan' ),
)
);
}
public function render_main_page() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$zip_install_result = null;
$zip_scan_result = null;
$auto_install_disabled = (bool) get_option( 'phpguard_auto_install_disabled', false );
if ( defined( 'PHPGUARD_DISABLE_INSTALL' ) && PHPGUARD_DISABLE_INSTALL ) {
$auto_install_disabled = true;
}
// Handle "Install Now" for a previously scanned ZIP.
if ( isset( $_POST['phpguard_install_zip_submit'] ) ) {
if ( ! isset( $_POST['phpguard_install_zip_nonce'] ) ) {
$zip_install_result = array(
'error' => __( 'Security check failed for ZIP install.', 'phpguard-lite' ),
);
} else {
$nonce = sanitize_text_field( wp_unslash( $_POST['phpguard_install_zip_nonce'] ) );
if ( ! wp_verify_nonce( $nonce, 'phpguard_install_zip' ) ) {
$zip_install_result = array(
'error' => __( 'Security check failed for ZIP install.', 'phpguard-lite' ),
);
} elseif ( ! current_user_can( 'install_plugins' ) ) {
$zip_install_result = array(
'error' => __( 'You do not have permission to install plugins.', 'phpguard-lite' ),
);
} else {
$zip_path_raw = isset( $_POST['phpguard_zip_path'] ) ? sanitize_text_field( wp_unslash( $_POST['phpguard_zip_path'] ) ) : '';
if ( '' === $zip_path_raw ) {
$zip_install_result = array(
'error' => __( 'No ZIP path was provided for install.', 'phpguard-lite' ),
);
} else {
$zip_path = realpath( $zip_path_raw );
$uploads = wp_upload_dir();
$base_dir = realpath( $uploads['basedir'] );
if ( ! $zip_path || ! $base_dir || strpos( $zip_path, $base_dir ) !== 0 || substr( $zip_path, -4 ) !== '.zip' ) {
$zip_install_result = array(
'error' => __( 'The ZIP file location was invalid.', 'phpguard-lite' ),
);
} else {
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/plugin.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
$skin = new Automatic_Upgrader_Skin();
$upgrader = new Plugin_Upgrader( $skin );
$result = $upgrader->install( $zip_path );
$skin_errors = method_exists( $skin, 'get_errors' ) ? $skin->get_errors() : null;
if ( is_wp_error( $result ) ) {
$zip_install_result = array(
'error' => sprintf(
/* translators: %s: error message */
__( 'Install failed: %s', 'phpguard-lite' ),
$result->get_error_message()
),
);
} elseif ( $skin_errors instanceof WP_Error && $skin_errors->has_errors() ) {
$messages = $skin_errors->get_error_messages();
$zip_install_result = array(
'error' => sprintf(
/* translators: %s: error message */
__( 'Install failed: %s', 'phpguard-lite' ),
implode( ' ', $messages )
),
);
} elseif ( ! $result ) {
// Try to give a more helpful hint when the upgrader returns false with no explicit error.
$filesystem_method = function_exists( 'get_filesystem_method' ) ? get_filesystem_method() : '';
$zip_install_result = array(
'error' => __( 'Install failed. Please try installing this ZIP via the standard Plugins → Add New → Upload Plugin screen.', 'phpguard-lite' ),
);
wp_delete_file( $zip_path );
}
}
}
}
}
}
if ( isset( $zip_install_result['error'] ) ) {
update_option( 'phpguard_auto_install_disabled', true );
$auto_install_disabled = true;
}
// Handle new ZIP uploads to scan.
if ( isset( $_POST['phpguard_upload_zip_submit'] ) ) {
if ( ! isset( $_POST['phpguard_upload_zip_nonce'] ) ) {
$zip_scan_result = array(
'error' => __( 'Security check failed for ZIP upload.', 'phpguard-lite' ),
);
} else {
$nonce = sanitize_text_field( wp_unslash( $_POST['phpguard_upload_zip_nonce'] ) );
if ( ! wp_verify_nonce( $nonce, 'phpguard_upload_zip' ) ) {
$zip_scan_result = array(
'error' => __( 'Security check failed for ZIP upload.', 'phpguard-lite' ),
);
} else {
$zip_scan_result = $this->handle_zip_upload_scan();
}
}
}
$plugins = get_plugins();
// Free runs in Safe Parser Mode and does not rely on PHP CLI.
// Pro may optionally enable CLI-based deep checks when available.
$environment_ok = false;
$environment_msg = __( 'PHPGuard is running in Safe Parser Mode. Advanced CLI scanning is not enabled in PHPGuard Lite.', 'phpguard-lite' );
?>
<div class="wrap phpguard-lite-wrap">
<h1><img src="<?php echo esc_url( plugins_url( 'assets/phpguard-logo.webp', __FILE__ ) ); ?>" style="height: 110px;margin-right:10px;vertical-align:middle;"> <?php esc_html_e( 'PHPGuard – Lite', 'phpguard-lite' ); ?></h1>
<style>
/* PHPGuard header slogan */
.phpguard-lite-wrap{
position: relative;
}
.phpguard-slogan{
position: absolute;
top: 16px;
left: 450px;
max-width: 420px;
font-size: 15px;
font-style: italic;
font-family: 'Segoe Script','Brush Script MT','Comic Sans MS', cursive;
color: #5a6b7a;
transform: rotate(-7deg);
}
/* On smaller screens, fall back to normal flow so layout doesn't break */
@media (max-width: 900px){
.phpguard-slogan{
position: static;
transform: none;
margin-top: 4px;
margin-bottom: 18px;
}
}
</style>
<p class="phpguard-slogan"><?php esc_html_e( 'Scan plugins for PHP syntax errors before you activate them, to reduce the risk of crashes and white screens.', 'phpguard-lite' ); ?></p>
<hr />
<h2 class="nav-tab-wrapper phpguard-tabs-nav">
<a href="#phpguard-tab-plugin" class="nav-tab nav-tab-active" data-phpguard-tab="plugin">
<?php esc_html_e( 'Quick Plugin Scan', 'phpguard-lite' ); ?>
</a>
<a href="#phpguard-tab-snippet" class="nav-tab" data-phpguard-tab="snippet">
<?php esc_html_e( 'Check Raw PHP Code Snippet', 'phpguard-lite' ); ?>
</a>
<a href="#phpguard-tab-zip" class="nav-tab" data-phpguard-tab="zip">
<?php esc_html_e( 'Scan Plugin ZIP Before Install', 'phpguard-lite' ); ?>
</a>
<a href="#phpguard-tab-env" class="nav-tab" data-phpguard-tab="env">
<?php esc_html_e( 'Environment Check', 'phpguard-lite' ); ?>
</a>
<a href="#phpguard-tab-docs" class="nav-tab" data-phpguard-tab="docs">
<?php esc_html_e( 'Documentation', 'phpguard-lite' ); ?>
</a>
</h2>
<div class="phpguard-tabs-container">
<div id="phpguard-tab-plugin" class="phpguard-tab-panel phpguard-tab-active">
<p><?php esc_html_e( 'Select an installed plugin and let PHPGuard run a syntax check on all of its PHP files using safe parser (if available).', 'phpguard-lite' ); ?></p>
<form id="phpguard-lite-form">
<label for="phpguard-plugin-select"><?php esc_html_e( 'Plugin to scan:', 'phpguard-lite' ); ?></label>
<select id="phpguard-plugin-select" name="plugin">
<?php foreach ( $plugins as $file => $data ) : ?>
<option value="<?php echo esc_attr( $file ); ?>">
<?php echo esc_html( $data['Name'] ); ?>
</option>
<?php endforeach; ?>
</select>
<button type="button" class="button button-primary" id="phpguard-run-scan">
<?php esc_html_e( 'Run Scan', 'phpguard-lite' ); ?>
</button>
</form>
<div id="phpguard-scan-result" style="margin-top: 1em;"></div>
</div>
<div id="phpguard-tab-snippet" class="phpguard-tab-panel" style="display:none;">
<p><?php esc_html_e( 'Paste PHP code below to check it for syntax errors before using it in your site.', 'phpguard-lite' ); ?></p>
<form id="phpguard-snippet-form" method="post" action="">
<?php wp_nonce_field( 'phpguard_run_scan', 'phpguard_snippet_nonce' ); ?>
<textarea id="phpguard-snippet" name="phpguard_snippet" rows="8" style="width:100%; max-width:900px;"></textarea>
<p style="margin-top: 0.5em;">
<button type="button" class="button button-secondary" id="phpguard-run-snippet-scan">
<?php esc_html_e( 'Check Pasted Code', 'phpguard-lite' ); ?>
</button>
</p>
</form>
<div id="phpguard-snippet-result" style="margin-top: 1em;"></div>
</div>
<div id="phpguard-tab-zip" class="phpguard-tab-panel" style="display:none;">
<p><?php esc_html_e( 'Upload a plugin ZIP file to run a pre-install syntax scan. The ZIP will be scanned in a temporary folder and then removed.', 'phpguard-lite' ); ?></p>
<?php if ( ! empty( $zip_scan_result ) && is_array( $zip_scan_result ) ) : ?>
<div id="phpguard-zip-result" style="margin-top: 1em;">
<?php if ( isset( $zip_scan_result['error'] ) ) : ?>
<div class="notice notice-error"><p><?php echo esc_html( $zip_scan_result['error'] ); ?></p></div>
<?php else : ?>
<p><strong><?php echo esc_html( isset( $zip_scan_result['message'] ) ? $zip_scan_result['message'] : '' ); ?></strong></p>
<?php if ( isset( $zip_scan_result['filesChecked'] ) ) : ?>
<p><?php esc_html_e( 'Files checked:', 'phpguard-lite' ); ?> <strong><?php echo esc_html( $zip_scan_result['filesChecked'] ); ?></strong></p>
<?php endif; ?>
<?php if ( ! empty( $zip_scan_result['errors'] ) && is_array( $zip_scan_result['errors'] ) ) : ?>
<ol>
<?php foreach ( $zip_scan_result['errors'] as $error ) : ?>
<li>
<strong><code><?php echo esc_html( isset( $error['file'] ) ? $error['file'] : '' ); ?></code></strong><br />
<?php echo esc_html( isset( $error['message'] ) ? $error['message'] : '' ); ?>
</li>
<?php endforeach; ?>
</ol>
<?php endif; ?>
<?php if ( ! empty( $zip_scan_result['indicators'] ) && is_array( $zip_scan_result['indicators'] ) ) : ?>
<h4 style="margin-top:18px;"><?php esc_html_e( 'Suspicious indicators', 'phpguard-lite' ); ?></h4>
<p style="margin-top:6px;"><?php esc_html_e( 'These are informational only. Nothing is executed.', 'phpguard-lite' ); ?></p>
<div class="phpguard-indicators" style="overflow:auto;">
<table class="widefat striped" style="margin-top:8px;">
<thead>
<tr>
<th><?php esc_html_e( 'Severity', 'phpguard-lite' ); ?></th>
<th><?php esc_html_e( 'Indicator', 'phpguard-lite' ); ?></th>
<th><?php esc_html_e( 'File', 'phpguard-lite' ); ?></th>
<th><?php esc_html_e( 'Line', 'phpguard-lite' ); ?></th>
<th><?php esc_html_e( 'What / Next', 'phpguard-lite' ); ?></th>
<th><?php esc_html_e( 'Excerpt', 'phpguard-lite' ); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ( $zip_scan_result['indicators'] as $ind ) : ?>
<tr>
<td><strong><?php echo esc_html( isset( $ind['severity'] ) ? $ind['severity'] : '' ); ?></strong></td>
<td><?php echo esc_html( isset( $ind['indicator'] ) ? $ind['indicator'] : '' ); ?></td>
<td><code><?php echo esc_html( isset( $ind['file'] ) ? $ind['file'] : '' ); ?></code></td>
<td><?php echo esc_html( isset( $ind['line'] ) ? $ind['line'] : '' ); ?></td>
<td>
<?php
$what = isset( $ind['what'] ) ? (string) $ind['what'] : '';
$next = isset( $ind['next'] ) ? (string) $ind['next'] : '';
echo esc_html( $what );
if ( $next ) {
echo '<br /><em>' . esc_html__( 'Next:', 'phpguard-lite' ) . '</em> ' . esc_html( $next );
}
?>
</td>
<td><code><?php echo esc_html( isset( $ind['excerpt'] ) ? $ind['excerpt'] : '' ); ?></code></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
<?php endif; ?>
</div>
<?php endif; ?>
<?php if ( ! empty( $zip_install_result ) && is_array( $zip_install_result ) ) : ?>
<div class="notice notice-<?php echo isset( $zip_install_result['error'] ) ? 'error' : 'success'; ?>">
<p>
<?php
if ( isset( $zip_install_result['error'] ) ) {
echo esc_html( $zip_install_result['error'] );
} else {
echo esc_html( $zip_install_result['message'] );
}
?>
</p>
</div>
<?php endif; ?>
<?php
$phpguard_can_install_zip = (
! empty( $zip_scan_result )
&& is_array( $zip_scan_result )
&& empty( $zip_scan_result['error'] )
&& empty( $zip_scan_result['errors'] )
&& ! empty( $zip_scan_result['zip_path'] )
);
?>
<?php
$phpguard_env_allows_install = false; // Automatic installs disabled – always direct to manual Upload Plugin page.
if ( ! $phpguard_env_allows_install && $phpguard_can_install_zip ) :
?>
<div class="phpguard-host-warning" style="display:none;">
<p><?php esc_html_e( 'Your hosting environment prevents automated plugin installs. PHPGuard can safely scan ZIP files, but you will need to install the plugin manually via Plugins → Add New → Upload Plugin.', 'phpguard-lite' ); ?></p>
</div>
<?php endif; ?>
<?php if ( $phpguard_can_install_zip && $phpguard_env_allows_install ) : ?>
<form method="post" style="margin-bottom: 1em;" id="phpguard-install-zip-form">
<?php wp_nonce_field( 'phpguard_install_zip', 'phpguard_install_zip_nonce' ); ?>
<input type="hidden" name="phpguard_zip_path" value="<?php echo esc_attr( $zip_scan_result['zip_path'] ); ?>" />
<?php submit_button( __( 'Install This Plugin Now', 'phpguard-lite' ), 'primary', 'phpguard_install_zip_submit', false ); ?>
</form>
<?php endif; ?>
<div id="phpguard-install-meta"
data-can-install="<?php echo $phpguard_can_install_zip ? '1' : '0'; ?>"
data-env-allows="<?php echo $phpguard_env_allows_install ? '1' : '0'; ?>"
data-upload-url="<?php echo esc_url( admin_url( 'plugin-install.php?tab=upload' ) ); ?>">
</div>
<form method="post" enctype="multipart/form-data">
<?php wp_nonce_field( 'phpguard_upload_zip', 'phpguard_upload_zip_nonce' ); ?>
<input type="file" name="phpguard_plugin_zip" accept=".zip" />
<?php submit_button( __( 'Upload and Scan ZIP', 'phpguard-lite' ), 'secondary', 'phpguard_upload_zip_submit', false ); ?>
</form>
</div></div>
</div>
<div id="phpguard-tab-env" class="phpguard-tab-panel" style="display:none;">
<h2><?php esc_html_e( 'Environment & Host Check', 'phpguard-lite' ); ?></h2>
<p><?php esc_html_e( 'This tab shows which scan engine PHPGuard is using on this server.', 'phpguard-lite' ); ?></p>
<p><strong><?php esc_html_e( 'Current status:', 'phpguard-lite' ); ?></strong> <?php echo esc_html( $environment_msg ); ?></p>
<p><?php esc_html_e( 'Safe Parser Mode performs syntax checks without executing code. PHPGuard Pro may optionally enable advanced CLI-based scanning on hosts that support it.', 'phpguard-lite' ); ?></p>
</div>
<div id="phpguard-tab-docs" class="phpguard-tab-panel" style="display:none;">
<h2><?php esc_html_e( 'How PHPGuard Works', 'phpguard-lite' ); ?></h2>
<p><?php esc_html_e( 'Use these tabs to run different kinds of checks before you install or activate plugins on a live site.', 'phpguard-lite' ); ?></p>
<h3><?php esc_html_e( 'Quick Plugin Scan', 'phpguard-lite' ); ?></h3>
<p><?php esc_html_e( 'Select any installed plugin and run a fast syntax scan on its PHP files. PHPGuard uses safe parser (if available) to detect fatal errors that would cause a white screen or crash when the plugin runs.', 'phpguard-lite' ); ?></p>
<h3><?php esc_html_e( 'Check Raw PHP Code Snippet', 'phpguard-lite' ); ?></h3>
<p><?php esc_html_e( 'Paste a single PHP snippet or small block of code and let PHPGuard check it for syntax errors before you drop it into functions.php, a custom plugin, or a code snippets plugin.', 'phpguard-lite' ); ?></p>
<h3><?php esc_html_e( 'Scan Plugin ZIP Before Install', 'phpguard-lite' ); ?></h3>
<p><?php esc_html_e( 'Upload a plugin ZIP file before you install it. PHPGuard unpacks the archive in a temporary folder, runs syntax checks, and then cleans up. This helps catch broken downloads or bad third‑party code before it ever touches your Plugins list.', 'phpguard-lite' ); ?></p>
<h3><?php esc_html_e( 'Environment Check', 'phpguard-lite' ); ?></h3>
<p><?php esc_html_e( 'This tab shows which scan engine PHPGuard is using. In PHPGuard Lite, scans run in Safe Parser Mode (no code is executed). PHPGuard Pro may optionally enable advanced CLI-based scanning when a host supports it.', 'phpguard-lite' ); ?></p>
<h3><?php esc_html_e( 'PHPGuard Pro — Coming Soon', 'phpguard-lite' ); ?></h3>
<p><?php esc_html_e( 'PHPGuard Pro will build on the free safety checks with powerful automation and protection features designed for serious WordPress professionals, agencies, and mission-critical sites.', 'phpguard-lite' ); ?></p>
<ul class="ul-disc">
<li><?php esc_html_e( 'Real-time crash prevention to stop bad code before it takes down a live site.', 'phpguard-lite' ); ?></li>
<li><?php esc_html_e( 'Automatic SMART rollback safety nets when a plugin update or custom code breaks a site.', 'phpguard-lite' ); ?></li>
<li><?php esc_html_e( 'Backup and SMART rollback of database (per plugin) or GLOBAL.', 'phpguard-lite' ); ?></li>
<li><?php esc_html_e( 'Deeper and smarter analysis than basic safe parser syntax checks.', 'phpguard-lite' ); ?></li>
<li><?php esc_html_e( 'Professional-grade workflows built for agencies and power users.', 'phpguard-lite' ); ?></li>
</ul>
<h3><?php esc_html_e( 'Support', 'phpguard-lite' ); ?></h3>
<p><?php esc_html_e( 'Prefer a one‑time way to say thank you? You can support PHPGuard development.', 'phpguard-lite' ); ?></p>
<p>
<a class="button button-primary phpguard-donate-button" href="https://www.paypal.com/donate/?business=info%40phpguard.dev&item_name=Support+PHPGuard¤cy_code=USD" target="_blank" rel="noopener">
<?php esc_html_e( 'Buy me a coffee', 'phpguard-lite' ); ?>
</a>
</p>
</div>
<div class="phpguard-pro-panel">
<h2><?php esc_html_e( 'PHPGuard Pro', 'phpguard-lite' ); ?></h2>
<p style="margin:12px 0;">
<strong><?php esc_html_e( 'PHPGuard Pro is coming.', 'phpguard-lite' ); ?></strong>
<?php esc_html_e( 'Learn what Pro will include and follow updates on the project site.', 'phpguard-lite' ); ?>
<a href="<?php echo esc_url( 'https://phpguard.dev/prevent-wordpress-plugin-errors/' ); ?>" target="_blank" rel="noopener noreferrer">
<?php esc_html_e( 'Learn more', 'phpguard-lite' ); ?>
</a>
</p>
<?php
printf(
/* translators: %s: version number */
esc_html__( 'PHPGuard Pre-Install Safety Checker version %s', 'phpguard-lite' ),
esc_html( self::VERSION )
);
?>
</div>
<?php
}
/**
* Render the shared version history view.
*/
public function render_version_history_page() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$history = get_option( self::OPTION_HISTORY );
if ( ! is_array( $history ) ) {
$history = array();
}
// Newest first.
usort(
$history,
function ( $a, $b ) {
$ta = isset( $a['timestamp'] ) ? (int) $a['timestamp'] : 0;
$tb = isset( $b['timestamp'] ) ? (int) $b['timestamp'] : 0;
return $tb - $ta;
}
);
?>
<div class="wrap phpguard-lite-wrap">
<h1><img src="<?php echo esc_url( plugins_url( 'assets/phpguard-logo.webp', __FILE__ ) ); ?>" style="height: 110px;margin-right:10px;vertical-align:middle;"> <?php esc_html_e( 'PHPGuard Version History', 'phpguard-lite' ); ?></h1>
<p><?php esc_html_e( 'This table shows recorded updates for the PHPGuard Pre-Install Safety Checker (free plugin, and related admin tools).', 'phpguard-lite' ); ?></p>
<?php if ( empty( $history ) ) : ?>
<p><?php esc_html_e( 'No version history has been recorded yet.', 'phpguard-lite' ); ?></p>
<?php else : ?>
<table class="widefat fixed striped">
<thead>
<tr>
<th><?php esc_html_e( 'Date', 'phpguard-lite' ); ?></th>
<th><?php esc_html_e( 'Component', 'phpguard-lite' ); ?></th>
<th><?php esc_html_e( 'Version', 'phpguard-lite' ); ?></th>
<th><?php esc_html_e( 'Notes', 'phpguard-lite' ); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ( $history as $entry ) : ?>
<tr>
<td>
<?php
$ts = isset( $entry['timestamp'] ) ? (int) $entry['timestamp'] : 0;
if ( $ts ) {
echo esc_html( date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $ts ) );
} else {
esc_html_e( '(unknown)', 'phpguard-lite' );
}
?>
</td>
<td><?php echo esc_html( isset( $entry['component'] ) ? $entry['component'] : '' ); ?></td>
<td><?php echo esc_html( isset( $entry['version'] ) ? $entry['version'] : '' ); ?></td>
<td><?php echo esc_html( isset( $entry['notes'] ) ? $entry['notes'] : '' ); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
<?php
}
/**
* Ajax: run scan for selected plugin.
*/
public function ajax_run_scan() {
if ( ! check_ajax_referer( 'phpguard_run_scan', 'nonce', false ) ) {
wp_send_json_error( array( 'message' => __( 'Security check failed. Please refresh and try again.', 'phpguard-lite' ) ) );
}
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( array( 'message' => __( 'Permission denied.', 'phpguard-lite' ) ) );
}
$plugin = isset( $_POST['plugin'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) : '';
if ( empty( $plugin ) ) {
wp_send_json_error( array( 'message' => __( 'No plugin specified.', 'phpguard-lite' ) ) );
}
$plugins = get_plugins();
if ( ! isset( $plugins[ $plugin ] ) ) {
wp_send_json_error( array( 'message' => __( 'Unknown plugin.', 'phpguard-lite' ) ) );
}
$plugin_dir = WP_PLUGIN_DIR . '/' . dirname( $plugin );
if ( ! is_dir( $plugin_dir ) ) {
wp_send_json_error( array( 'message' => __( 'Plugin folder not found.', 'phpguard-lite' ) ) );
}
$results = $this->scan_directory_for_php_errors( $plugin_dir );
wp_send_json_success( $results );
}
/**
* Ajax: run syntax scan for pasted PHP code.
*/
public
function ajax_run_snippet_scan() {
if ( ! check_ajax_referer( 'phpguard_run_scan', 'nonce', false ) ) {
wp_send_json_error( array( 'message' => __( 'Security check failed. Please refresh and try again.', 'phpguard-lite' ) ) );
}
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error(
array(
'message' => __( 'Permission denied.', 'phpguard-lite' ),
)
);
}
$snippet = '';
if ( isset( $_POST['snippet'] ) ) {
// Plain text mode (older JS or fallback)
$snippet = isset( $_POST['snippet'] ) ? sanitize_textarea_field( wp_unslash( $_POST['snippet'] ) ) : '';
} elseif ( isset( $_POST['snippet_b64'] ) ) {
// Base64 mode (current JS)
$raw = sanitize_textarea_field( wp_unslash( $_POST['snippet_b64'] ) );
$decoded = base64_decode( $raw, true );
if ( false === $decoded ) {
wp_send_json_error(
array(
'message' => __( 'Invalid snippet encoding.', 'phpguard-lite' ),
)
);
}
$snippet = $decoded;
}
if ( '' === trim( $snippet ) ) {
wp_send_json_error(
array(
'message' => __( 'No code was provided.', 'phpguard-lite' ),
)
);
}
// Ensure the code is wrapped in PHP tags so safe parser can lint it reliably.
if ( false === strpos( $snippet, '<?' ) ) {
$snippet = "<?php\n" . $snippet;
}
$tmp_file = wp_tempnam( 'phpguard_snippet.php' );
if ( ! $tmp_file ) {
wp_send_json_error(
array(
'message' => __( 'Could not create a temporary file for the snippet scan.', 'phpguard-lite' ),
)
);
}
file_put_contents( $tmp_file, $snippet );
$results = array(
'message' => '',
'filesChecked' => 1,
'errors' => array(),
// Suspicious patterns (informational; nothing is executed).
'indicators' => array(),
);
$output_text = '';
$exit_code = 0;
// Non-executing syntax check using nikic/php-parser.
$result = $this->basic_php_syntax_check( $tmp_file );
if ( true !== $result ) {
$output_text = (string) $result;
$exit_code = 1;
}
if ( $output_text ) {
$output_text = $this->normalize_php_lint_output( $output_text, $tmp_file );
$output_text = $this->clean_php_error_message( $output_text );
}
wp_delete_file( $tmp_file );
$has_error = false;
if ( 0 !== $exit_code ) {
$has_error = true;
} elseif ( $output_text && preg_match( '/(Errors parsing|Parse error|syntax error|Fatal error|PHP Parse error)/i', $output_text ) ) {
$has_error = true;
}
if ( $has_error ) {
$results['message'] = __( 'Detected syntax issues in the pasted code. Review details below.', 'phpguard-lite' );
$results['errors'][] = array(
'file' => __( 'Pasted code snippet', 'phpguard-lite' ),
'message' => $output_text ? $output_text : __( 'Unknown parse error reported by PHP.', 'phpguard-lite' ),
);
} else {
$results['message'] = __( 'No syntax errors detected in the pasted code.', 'phpguard-lite' );
}
// Add informational suspicious-pattern indicators (even if syntax is clean).
$results['indicators'] = $this->detect_indicators_in_code( $snippet );
wp_send_json_success( $results );
}
/**
* Detect suspicious patterns in a code string without executing anything.
* This is intentionally heuristic and informational only.
*/
protected function detect_indicators_in_code( $code ) {
$out = array();
if ( ! is_string( $code ) || '' === trim( $code ) ) {
return $out;
}
// Functions that are commonly abused in malicious PHP code.
// Used only for detection and for labeling scan results.
$fn_eval = 'eval';
$fn_assert = 'assert';
$fn_exec = 'exec';
$fn_system = 'system';
$fn_shell_exec = 'shell_exec';
$fn_passthru = 'passthru';
$fn_proc_open = 'proc_open';
$fn_popen = 'popen';
$fn_create_func = 'create_function';
$danger_words = array(
$fn_eval,
$fn_assert,
$fn_system,
$fn_exec,
$fn_shell_exec,
$fn_passthru,
$fn_proc_open,
$fn_popen,
$fn_create_func,
);
$obf_word = function( $w ) { return (string) $w; };
$obf_text = function( $t ) { return (string) $t; };
// Token-based detection.
$tokens = token_get_all( $code );
if ( is_array( $tokens ) ) {
$danger_map = array(
( defined( 'T_EVAL' ) ? T_EVAL : -1 ) => array( 'severity' => 'HIGH', 'indicator' => $fn_eval, 'what' => 'eval() language construct', 'next' => 'Never run untrusted code; prefer parsing only.' ),
);
$count_tokens = count( $tokens );
for ( $i = 0; $i < $count_tokens; $i++ ) {
$t = $tokens[ $i ];
if ( is_array( $t ) ) {
$tid = $t[0];
$tval = $t[1];
$tline = isset( $t[2] ) ? (int) $t[2] : 0;
if ( isset( $danger_map[ $tid ] ) ) {
$info = $danger_map[ $tid ];
$out[] = array(
'severity' => $info['severity'],
'indicator' => $obf_word($info['indicator']),
'line' => max( 1, $tline ),
'excerpt' => trim( $tval ),
'what' => $obf_text($info['what']),
'next' => $info['next'],
);
continue;
}
// Function-call style: system(), exec(), shell_exec(), passthru(), proc_open(), popen(), create_function(), assert()
if ( T_STRING === $tid ) {
$name = strtolower( $tval );
$watch = array(
$fn_assert,
$fn_exec,
$fn_shell_exec,
$fn_system,
$fn_passthru,
$fn_proc_open,
$fn_popen,
$fn_create_func,
);
if ( in_array( $name, $watch, true ) ) {
// Look ahead for "(" ignoring whitespace/comments.
$j = $i + 1;
while ( $j < $count_tokens ) {
$n = $tokens[ $j ];
if ( is_array( $n ) ) {
if ( in_array( $n[0], array( T_WHITESPACE, T_COMMENT, T_DOC_COMMENT ), true ) ) {
$j++;
continue;
}
break;
} else {
break;
}
}
if ( $j < $count_tokens && '(' === $tokens[ $j ] ) {
// Avoid duplicates if regex also catches it later.
$out[] = array(
'severity' => ( $name === $fn_create_func ? 'MEDIUM' : 'HIGH' ),
'indicator' => $name,
'line' => max( 1, $tline ),
'excerpt' => trim( $name . '(' ),
'what' => $name . '() call',
'next' => 'Confirm no user input reaches this call; review for webshell patterns.',
);
}
}
}
}
}
}
$rules = array(
array(
'severity' => 'HIGH',
'indicator' => $fn_eval,
'pattern' => '/\b' . preg_quote( $fn_eval, '/' ) . '\s*\(/i',
'what' => $fn_eval . '() call',
'next' => 'Do not execute user-controlled strings; remove or replace with safer logic.',
),
array(
'severity' => 'HIGH',
'indicator' => $fn_assert,
'pattern' => '/\b' . preg_quote( $fn_assert, '/' ) . '\s*\(/i',
'what' => $fn_assert . '() call',
'next' => 'In some contexts this can execute code; avoid with untrusted input.',
),
array(
'severity' => 'HIGH',
'indicator' => $fn_system,
'pattern' => '/\b' . preg_quote( $fn_system, '/' ) . '\s*\(/i',
'what' => $fn_system . '() call',
'next' => 'Runs OS commands. Confirm no user input reaches it; review for webshell patterns.',
),
array(
'severity' => 'HIGH',
'indicator' => $fn_exec,
'pattern' => '/\b' . preg_quote( $fn_exec, '/' ) . '\s*\(/i',
'what' => $fn_exec . '() call',
'next' => 'Runs OS commands. Confirm no user input reaches it; review for webshell patterns.',
),
array(
'severity' => 'HIGH',
'indicator' => $fn_shell_exec,
'pattern' => '/\b' . preg_quote( $fn_shell_exec, '/' ) . '\s*\(/i',
'what' => $fn_shell_exec . '() call',
'next' => 'Runs OS commands. Confirm no user input reaches it; review for webshell patterns.',
),
array(
'severity' => 'HIGH',
'indicator' => $fn_passthru,
'pattern' => '/\b' . preg_quote( $fn_passthru, '/' ) . '\s*\(/i',
'what' => $fn_passthru . '() call',
'next' => 'Runs OS commands. Confirm no user input reaches it; review for webshell patterns.',
),
array(
'severity' => 'HIGH',
'indicator' => $fn_proc_open,
'pattern' => '/\b' . preg_quote( $fn_proc_open, '/' ) . '\s*\(/i',
'what' => $fn_proc_open . '() call',
'next' => 'Spawns processes. Confirm no user input reaches it; review for webshell patterns.',
),
array(
'severity' => 'HIGH',
'indicator' => $fn_popen,
'pattern' => '/\b' . preg_quote( $fn_popen, '/' ) . '\s*\(/i',
'what' => $fn_popen . '() call',
'next' => 'Spawns processes. Confirm no user input reaches it; review for webshell patterns.',
),
array(
'severity' => 'MEDIUM',
'indicator' => $fn_create_func,
'pattern' => '/\b' . preg_quote( $fn_create_func, '/' ) . '\s*\(/i',
'what' => $fn_create_func . '() call',
'next' => 'Deprecated and risky. Prefer closures.',
),
array(
'severity' => 'MEDIUM',
'indicator' => 'preg_replace_e',
'pattern' => '/preg_replace\s*\(\s*[^,]*\/e[\"\']?\s*,/i',
'what' => 'preg_replace() with /e',
'next' => 'Historically dangerous. Remove /e; use preg_replace_callback.',
),
);
$lines = preg_split( "/\r\n|\r|\n/", $code );
foreach ( $rules as $r ) {
if ( ! isset( $r['pattern'] ) ) {
continue;
}
if ( preg_match_all( $r['pattern'], $code, $m, PREG_OFFSET_CAPTURE ) ) {
foreach ( $m[0] as $hit ) {
$pos = (int) $hit[1];
$line = 1 + substr_count( substr( $code, 0, max( 0, $pos ) ), "\n" );
$excerpt = '';
if ( isset( $lines[ $line - 1 ] ) ) {
$excerpt = trim( $lines[ $line - 1 ] );
}
$out[] = array(
'severity' => $r['severity'],
'indicator' => $r['indicator'],
'line' => $line,
'excerpt' => $excerpt,
'what' => $r['what'],
'next' => $r['next'],
);
}
}
}
$merged = array();
foreach ( $out as $row ) {
$key = strtolower( $row['indicator'] ) . '|' . (int) $row['line'];
if ( ! isset( $merged[ $key ] ) ) {
$merged[ $key ] = $row;
continue;
}
// Merge "what" text
if ( false === stripos( $merged[ $key ]['what'], $row['what'] ) ) {
$merged[ $key ]['what'] .= ' / ' . $row['what'];
}
// Merge "next" guidance
if ( false === stripos( $merged[ $key ]['next'], $row['next'] ) ) {
$merged[ $key ]['next'] .= ' ' . $row['next'];
}
}
return array_values( $merged );
}
/**
* Detect indicators in a file and annotate each hit with a relative file path.
*
* @param string $file Absolute path.
* @param string $base_dir Base directory for relative paths.
* @return array<int,array{severity:string,indicator:string,line:int,excerpt:string,what:string,next:string,file:string}>
*/
protected function detect_indicators_in_file( $file, $base_dir ) {
$hits = array();
if ( ! is_string( $file ) || '' === $file || ! file_exists( $file ) ) {
return $hits;
}
$code = file_get_contents( $file );
if ( false === $code ) {
return $hits;
}
$rel = $file;
if ( is_string( $base_dir ) && '' !== $base_dir ) {