-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
wp-pgp-encrypted-emails.php
1513 lines (1400 loc) · 54.4 KB
/
wp-pgp-encrypted-emails.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
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
/**
* The WP PGP Encrypted Emails plugin for WordPress.
*
* Despite the name, this plugin supports both OpenPGP-compatible and
* S/MIME email protection schemes. It also provides an API in the
* style familiar to WordPress developers to both encrpytion formats.
* See the `class-wp-openpgp.php` and `class-wp-smime.php` files for
* more information about the API's implementation.
*
* WordPress plugin header information:
*
* * Plugin Name: WP PGP Encrypted Emails
* * Plugin URI: https://github.com/fabacab/wp-pgp-encrypted-emails
* * Description: Encrypts email sent to users who opt-in to OpenPGP- and/or S/MIME-compatible protection. <strong>Like this plugin? Please <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=TJLPJYXHSRBEE&lc=US&item_name=WP%20PGP%20Encrypted%20Emails&item_number=wp-pgp-encrypted-emails&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHosted" title="Send a donation to the developer of WP PGP Encrypted Emails">donate</a>. ♥ Thank you!</strong>
* * Version: 0.8.0
* * License: GPL-3.0
* * License URI: https://www.gnu.org/licenses/gpl-3.0.en.html
* * Text Domain: wp-pgp-encrypted-emails
* * Domain Path: /languages
*
* @link https://developer.wordpress.org/plugins/the-basics/header-requirements/
*
* @license https://www.gnu.org/licenses/gpl-3.0.en.html
*
* @package WordPress\Plugin\WP_PGP_Encrypted_Emails
*/
if ( ! defined( 'ABSPATH' ) ) { exit; } // Disallow direct HTTP access.
if ( ! defined( 'WP_PGP_ENCRYPTED_EMAILS_MIN_PHP_VERSION' ) ) {
/**
* The minimum version of PHP needed to run the plugin.
*
* This is explicit because WordPress supports even older versions
* of PHP, so we check the running version on plugin activation.
*
* @link https://secure.php.net/manual/en/language.oop5.late-static-bindings.php
*/
define( 'WP_PGP_ENCRYPTED_EMAILS_MIN_PHP_VERSION', '5.4' );
}
/**
* Base class that WordPress uses to register and initialize plugin.
*/
class WP_PGP_Encrypted_Emails {
/**
* Meta key where PGP private/public keypair is stored.
*
* This is intended to be the PGP private key used by the plugin
* for signing outgoing emails. It is *not* intended to store any
* user's private key material nor is it intended to be used for
* saving any key material for any other purpose other than this
* plugin's own use. **Do not**, under any circumstances, copy a key
* used in any other application to this field.
*
* @var string
*/
const meta_keypair = 'pgp_keypair';
/**
* Meta key where PGP public key is stored.
*
* @var string
*/
const meta_key = 'pgp_public_key';
/**
* Meta key where S/MIME public certificate is stored.
*
* @var string
*/
const meta_smime_certificate = 'smime_certificate';
/**
* Meta key where subject line toggle is stored.
*
* @var string
*/
const meta_encryption_method = 'email_encryption_method';
/**
* Meta key where subject line toggle is stored.
*
* @var string
*/
const meta_key_empty_subject_line = 'pgp_empty_subject_line';
/**
* Meta key where unknown recipient signing toggle is stored.
*
* @var string
*/
const meta_key_sign_for_unknown_recipients = 'pgp_sign_for_unknown_recipients';
/**
* Whether a user should receive signed email or not.
*
* @var string
*/
const meta_key_receive_signed_email = 'openpgp_receive_signed_email';
/**
* Meta key where toggle to purge options on uninstall is stored.
*
* @var string
*/
const meta_key_purge_all = 'pgp_purge_all';
/**
* Entry point for the WordPress framework into plugin code.
*
* This is the method called when WordPress loads the plugin file.
* It is responsible for "registering" the plugin's main functions
* with the {@see https://codex.wordpress.org/Plugin_API WordPress Plugin API}.
*
* @uses add_action()
* @uses add_filter()
* @uses remove_filter()
* @uses register_activation_hook()
* @uses register_deactivation_hook()
*
* @return void
*/
public static function register () {
add_action( 'plugins_loaded', array( __CLASS__, 'registerL10n' ) );
add_action( 'init', array( __CLASS__, 'initialize' ) );
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueueStyles' ) );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueueAdminStyles') );
add_action( 'wp_ajax_nopriv_download_pgp_signing_public_key', array( __CLASS__, 'downloadSigningPublicKey' ) );
add_action( 'wp_ajax_download_pgp_signing_public_key', array( __CLASS__, 'downloadSigningPublicKey' ) );
add_action( 'wp_ajax_openpgp_regen_keypair', array( __CLASS__, 'regenerateKeypair' ) );
add_action( 'wp_ajax_wp_pgp_encrypted_emails_send_test_email', array( __CLASS__, 'sendTestEmail' ) );
if ( is_admin() ) {
add_action( 'admin_menu', array( __CLASS__, 'registerOptionsPage') );
add_action( 'admin_init', array( __CLASS__, 'registerAdminSettings' ) );
add_action( 'admin_notices', array( __CLASS__, 'adminNoticeBadUserKey' ) );
add_action( 'admin_notices', array( __CLASS__, 'adminNoticeBadAdminKey' ) );
add_action( 'admin_notices', array( __CLASS__, 'adminNoticeBadUserCert' ) );
add_action( 'admin_notices', array( __CLASS__, 'adminNoticeBadAdminCert' ) );
add_action( 'show_user_profile', array( __CLASS__, 'renderProfile' ) );
add_action( 'personal_options_update', array( __CLASS__, 'saveProfile' ) );
$kp = get_option( self::meta_keypair );
if ( ! $kp || empty( $kp['privatekey'] ) ) {
add_action( 'admin_notices', array( __CLASS__, 'showMissingSigningKeyNotice' ) );
}
} else {
remove_filter( 'comment_text', 'wptexturize' ); // we do wptexturize() ourselves
add_filter( 'comment_text', array( __CLASS__, 'commentText' ));
add_filter( 'comment_form_submit_field', array( __CLASS__, 'renderCommentFormFields' ) );
add_filter( 'comment_class', array( __CLASS__, 'commentClass' ), 10, 4 );
add_filter( 'preprocess_comment', array( __CLASS__, 'preprocessComment' ) );
}
add_filter( 'wp_openpgp_user_key', array( __CLASS__, 'getUserKey' ) );
add_filter( 'wp_smime_user_certificate', array( __CLASS__, 'getUserCert' ) );
add_filter( 'wp_user_encryption_method', array( __CLASS__, 'getUserEncryptionMethod' ) );
add_filter( 'wp_mail', array( __CLASS__, 'wp_mail' ) );
register_activation_hook( __FILE__, array( __CLASS__, 'activate' ) );
}
/**
* Loads localization files from plugin's languages directory.
*
* @uses load_plugin_textdomain()
*
* @return void
*/
public static function registerL10n () {
load_plugin_textdomain( 'wp-pgp-encrypted-emails', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Loads plugin componentry. Called at the WordPress `init` hook.
*
* @return void
*/
public static function initialize () {
require_once plugin_dir_path( __FILE__ ) . '/includes/class-wp-openpgp.php';
WP_OpenPGP::register();
if ( function_exists( 'openssl_x509_read' ) ) {
require_once plugin_dir_path( __FILE__ ) . '/includes/class-wp-smime.php';
WP_SMIME::register();
}
// Integrations.
//
// This allows an end-user to create a file in their
// current theme directory with the name of a plugin
// slug and `-functions.php` appended to it in order
// to automatically override any defaults defined by
// this plugin. It's "pluggable," in WP jargon.
require_once ABSPATH . '/wp-admin/includes/plugin.php';
$plugins = array(
// Whitelist of allowed plugins.
'woocommerce',
);
$tpl_dir = get_template_directory();
$our_dir = plugin_dir_path( __FILE__ ) . '/includes';
foreach ( $plugins as $p ) {
if ( is_plugin_active( "$p/$p.php" ) ) {
if ( is_readable( "{$tpl_dir}/{$p}-functions.php" ) ) {
include_once "{$tpl_dir}/{$p}-functions.php";
} else if ( is_readable( "{$our_dir}/$p-functions.php" ) ) {
include_once "{$our_dir}/{$p}-functions.php";
}
}
}
}
/**
* Enqueues the plugin's front-end stylesheet.
*
* @see https://developer.wordpress.org/reference/hooks/wp_enqueue_scripts/
*/
public static function enqueueStyles () {
// Nothing to do, yet.
}
/**
* Enqueues the plugin's admin area stylesheet.
*
* @see https://developer.wordpress.org/reference/hooks/admin_enqueue_scripts/
*/
public static function enqueueAdminStyles () {
wp_enqueue_style(
'wp-pgp-encrypted-emails',
plugin_dir_url( __FILE__ ) . 'admin/style.css'
);
}
/**
* Method to run when the plugin is activated by a user in the
* WordPress Dashboard admin screen.
*
* @uses WP_PGP_Encrypted_Emails::checkPrereqs()
*
* @return void
*/
public static function activate () {
self::checkPrereqs();
}
/**
* Checks system requirements and exits if they are not met.
*
* This first checks to ensure minimum WordPress and PHP versions
* have been satisfied. If not, the plugin deactivates and exits.
*
* @global $wp_version
*
* @uses $wp_version
* @uses WP_PGP_Encrypted_Emails::get_minimum_wordpress_version()
* @uses deactivate_plugins()
* @uses plugin_basename()
*
* @return void
*/
public static function checkPrereqs () {
global $wp_version;
$min_wp_version = self::get_minimum_wordpress_version();
if ( version_compare( WP_PGP_ENCRYPTED_EMAILS_MIN_PHP_VERSION, PHP_VERSION ) > 0 ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
wp_die( sprintf(
__( 'WP PGP Encrypted Emails requires at least PHP version %1$s. You have PHP version %2$s.', 'wp-pgp-encrypted-emails' ),
WP_PGP_ENCRYPTED_EMAILS_MIN_PHP_VERSION, PHP_VERSION
) );
}
if ( version_compare( $min_wp_version, $wp_version ) > 0 ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
wp_die( sprintf(
__( 'WP PGP Encrypted Emails requires at least WordPress version %1$s. You have WordPress version %2$s.', 'wp-pgp-encrypted-emails' ),
$min_wp_version, $wp_version
) );
}
}
/**
* Returns the "Requires at least" value from plugin's readme.txt.
*
* @link https://wordpress.org/plugins/about/readme.txt WordPress readme.txt standard
*
* @return string
*/
public static function get_minimum_wordpress_version () {
$lines = @file( plugin_dir_path( __FILE__ ) . 'readme.txt' );
foreach ( $lines as $line ) {
preg_match( '/^Requires at least: ([0-9.]+)$/', $line, $m );
if ( $m ) {
return $m[1];
}
}
}
/**
* Shows a notice on the Plugins screen that a signing key is missing.
*
* @return void
*/
public static function showMissingSigningKeyNotice () {
$screen = get_current_screen();
if ( $screen->base === 'plugins' ) {
?>
<div class="updated">
<p><a href="<?php print esc_attr( self::getKeypairRegenURL() );?>" class="button"><?php esc_html_e( 'Generate PGP signing key', 'wp-pgp-encrypted-emails' );?></a> — <?php print sprintf( esc_html__( 'Almost done! Generate an OpenPGP keypair for %s to sign outgoing emails.', 'wp-pgp-encrypted-emails' ), get_bloginfo( 'name' ) );?></p>
</div>
<?php
}
}
/**
* Gets a user's PGP public key.
*
* @param WP_User|int|string $user
*
* @return OpenPGP_Message|false
*/
public static function getUserKey ( $user = null ) {
$wp_user = false;
$ascii_key = false;
if ( $user instanceof WP_User ) {
$wp_user = $user;
} else if ( get_user_by( 'email', $user ) ) {
$wp_user = get_user_by( 'email', $user );
} else if ( get_userdata( $user ) ) {
$wp_user = get_userdata( $user );
} else {
$wp_user = wp_get_current_user();
}
if ( $wp_user ) {
$ascii_key = $wp_user->{self::meta_key};
}
return apply_filters( 'openpgp_key', $ascii_key );
}
/**
* Gets the admin's PGP public key.
*
* @return OpenPGP_Message|false
*/
public static function getAdminKey () {
return apply_filters( 'openpgp_key', get_option( self::meta_key ) );
}
/**
* Gets a user's S/MIME public certificate.
*
* @param WP_User|int|string $user
*
* @return resource|FALSE
*/
public static function getUserCert ( $user = null ) {
$wp_user = false;
$ascii_cert = false;
if ( $user instanceof WP_User ) {
$wp_user = $user;
} else if ( get_user_by('email', $user ) ) {
$wp_user = get_user_by( 'email', $user );
} else if ( get_userdata( $user ) ) {
$wp_user = get_userdata( $user );
} else {
$wp_user = wp_get_current_user();
}
if ( $wp_user ) {
$ascii_cert = $wp_user->{self::meta_smime_certificate};
}
return apply_filters( 'smime_certificate', $ascii_cert );
}
/**
* Gets the admin's S/MIME public certificate.
*
* @return resource|FALSE
*/
public static function getAdminCert () {
return apply_filters( 'smime_certificate', get_option( self::meta_smime_certificate ) );
}
/**
* Gets the admin's preferred encryption method.
*
* @return string
*/
public static function getAdminEncryptionMethod () {
return get_option( self::meta_encryption_method, 'pgp' );
}
/**
* Gets the user's preferred encryption method.
*
* @param WP_User|int|string $user
*
* @return string
*/
public static function getUserEncryptionMethod( $user ) {
$wp_user = false;
if ( $user instanceof WP_User ) {
$wp_user = $user;
} else if ( get_user_by('email', $user ) ) {
$wp_user = get_user_by( 'email', $user );
} else if ( get_userdata( $user ) ) {
$wp_user = get_userdata( $user );
} else {
$wp_user = wp_get_current_user();
}
if ( $wp_user ) {
$method = $wp_user->{self::meta_encryption_method};
}
return ( ! empty( $method ) ) ? $method : 'pgp';
}
/**
* Registers the options page as a Settings menu item.
*
* @link https://codex.wordpress.org/Settings_API
*
* @uses add_options_page()
* @uses register_setting()
*
* @return void
*/
public static function registerOptionsPage () {
add_options_page(
esc_html__( 'Email Encryption', 'wp-pgp-encrypted-emails' ),
esc_html__( 'Email Encryption', 'wp-pgp-encrypted-emails' ),
'manage_options',
'wp-pgp-encrypted-emails',
array( __CLASS__, 'renderOptionsPage' )
);
}
/**
* Registers plugin's settings and their sections with WordPress.
*
* @link https://codex.wordpress.org/Settings_API
*
* @uses add_settings_section()
* @uses add_settings_field()
* @uses register_setting()
*
* @return void
*/
public static function registerAdminSettings () {
// Register sections for the following, in order:
// 1. OpenPGP
// 1. S/MIME (if OpenSSL is available)
// 1. Delivery Options
// 1. Plugin Extras
add_settings_section(
'wp-pgp-encrypted-emails-pgp-settings',
__( 'PGP/GPG Encryption and Signing', 'wp-pgp-encrypted-emails' ),
array( __CLASS__, 'renderPGPSettingSection' ),
'wp-pgp-encrypted-emails'
);
if ( function_exists( 'openssl_x509_read' ) ) {
add_settings_section(
'wp-pgp-encrypted-emails-smime-settings',
__( 'S/MIME Encryption', 'wp-pgp-encrypted-emails' ),
array( __CLASS__, 'renderSMIMESettingSection' ),
'wp-pgp-encrypted-emails'
);
}
add_settings_section(
'wp-pgp-encrypted-emails-delivery-settings',
__( 'Delivery Options', 'wp-pgp-encrypted-emails' ),
array( __CLASS__, 'renderDeliverySettingSection' ),
'wp-pgp-encrypted-emails'
);
add_settings_section(
'wp-pgp-encrypted-emails-plugin-extras-settings',
__( 'Plugin Extras', 'wp-pgp-encrypted-emails' ),
array( __CLASS__, 'renderDeliverySettingSection' ),
'wp-pgp-encrypted-emails'
);
// ************************************************ //
// Individual settings belong in specific sections. //
// ************************************************ //
// PGP public key
add_settings_field(
self::meta_key,
__( 'Admin Email PGP Public Key', 'wp-pgp-encrypted-emails' ),
array( __CLASS__, 'renderAdminPGPKeySetting' ),
'wp-pgp-encrypted-emails',
'wp-pgp-encrypted-emails-pgp-settings', // PGP section
array(
'label_for' => self::meta_key
)
);
register_setting(
'wp-pgp-encrypted-emails',
self::meta_key,
array( __CLASS__, 'sanitizeKeyASCII' )
);
// PGP signing keypair
add_settings_field(
self::meta_keypair,
__( 'PGP Signing Keypair', 'wp-pgp-encrypted-emails' ),
array( __CLASS__, 'renderSigningKeypairSetting' ),
'wp-pgp-encrypted-emails',
'wp-pgp-encrypted-emails-pgp-settings', // PGP section
array(
'label_for' => self::meta_keypair.'_publickey'
)
);
register_setting(
'wp-pgp-encrypted-emails',
self::meta_keypair,
array( __CLASS__, 'sanitizeSigningKeypair' )
);
// S/MIME Public Certificate
if ( function_exists( 'openssl_x509_read' ) ) {
add_settings_field(
self::meta_smime_certificate,
__( 'Admin Email S/MIME Public Certificate', 'wp-pgp-encrypted-emails' ),
array( __CLASS__, 'renderAdminSMIMEKeySetting' ),
'wp-pgp-encrypted-emails',
'wp-pgp-encrypted-emails-smime-settings', // S/MIME section
array(
'label_for' => self::meta_smime_certificate
)
);
register_setting(
'wp-pgp-encrypted-emails',
self::meta_smime_certificate,
array( __CLASS__, 'sanitizeTextArea' )
);
}
// Encryption method preference, when both are available
if ( self::getAdminKey() && self::getAdminCert() ) {
add_settings_field(
self::meta_encryption_method,
__( 'Encryption method preference', 'wp-pgp-encrypted-emails' ),
array( __CLASS__, 'renderEncryptionMethodSetting' ),
'wp-pgp-encrypted-emails',
'wp-pgp-encrypted-emails-delivery-settings', // Delivery section
array(
'label_for' => self::meta_encryption_method
)
);
register_setting(
'wp-pgp-encrypted-emails',
self::meta_encryption_method,
array( 'sanitize_text_field' )
);
}
// Empty subject line?
add_settings_field(
self::meta_key_empty_subject_line,
__( 'Always empty subject lines for encrypted emails', 'wp-pgp-encrypted-emails' ),
array( __CLASS__, 'renderEmptySubjectLineSetting' ),
'wp-pgp-encrypted-emails',
'wp-pgp-encrypted-emails-delivery-settings', // Delivery section
array(
'label_for' => self::meta_key_empty_subject_line
)
);
register_setting(
'wp-pgp-encrypted-emails',
self::meta_key_empty_subject_line,
array( __CLASS__, 'sanitizeCheckBox' )
);
// Unrecognized recipient signing toggle.
add_settings_field(
self::meta_key_sign_for_unknown_recipients,
__( 'Sign email sent to unrecognized addresses', 'wp-pgp-encrypted-emails' ),
array( __CLASS__, 'renderSignForUnknownRecipients' ),
'wp-pgp-encrypted-emails',
'wp-pgp-encrypted-emails-delivery-settings', // Delivery section
array(
'label_for' => self::meta_key_sign_for_unknown_recipients
)
);
register_setting(
'wp-pgp-encrypted-emails',
self::meta_key_sign_for_unknown_recipients,
array( __CLASS__, 'sanitizeCheckBox' )
);
// Toggle to purge all data, including private key material.
add_settings_field(
self::meta_key_purge_all,
__( 'Delete private key material on uninstall', 'wp-pgp-encrypted-emails' ),
array( __CLASS__, 'renderPurgeAllSetting' ),
'wp-pgp-encrypted-emails',
'wp-pgp-encrypted-emails-plugin-extras-settings', // Plugin Extras section
array(
'label_for' => self::meta_key_purge_all
)
);
register_setting(
'wp-pgp-encrypted-emails',
self::meta_key_purge_all,
array( __CLASS__, 'sanitizeCheckBox' )
);
}
/**
* Sanitizes the signing keypair.
*
* @param string[] $input
*
* @uses wp_parse_args()
*
* @return string[]
*/
public static function sanitizeSigningKeypair ( $input ) {
$old_keypair = get_option( self::meta_keypair );
return wp_parse_args( self::sanitizeKeypairASCII( $input ), $old_keypair );
}
/**
* Sanitizes a PGP private/public keypair.
*
* @param string[] $keypair
*
* @return string[]
*/
public static function sanitizeKeypairASCII ( $keypair ) {
$safe_input = array();
foreach ( $keypair as $k => $v ) {
$safe_input[ $k ] = self::sanitizeKeyASCII( $v );
}
return $safe_input;
}
/**
* Sanitizes a PGP public key block.
*
* @param string $ascii_key
*
* @return string
*/
public static function sanitizeKeyASCII ( $ascii_key ) {
// TODO: Be a bit smarter about this being a PGP public key.
return self::sanitizeTextArea( $ascii_key );
}
/**
* A helper function that sanitizes multi-line inputs.
*
* @param string $input
*
* @return string
*/
public static function sanitizeTextArea ( $input ) {
return implode( "\n", array_map( 'sanitize_text_field', explode( "\n", $input ) ) );
}
/**
* A helper function that sanitizes check boxes.
*
* @param mixed $input
*
* @return bool
*/
public static function sanitizeCheckBox ( $input ) {
return isset( $input );
}
/**
* Prints a warning to the user if their PGP public key can't be used.
*
* @uses WP_PGP_Encrypted_Emails::getUserKey()
* @uses wp_get_current_user()
* @uses admin_url()
*
* @return void
*/
public static function adminNoticeBadUserKey () {
$wp_user = wp_get_current_user();
if ( ! empty( $wp_user->{self::meta_key} ) && ! apply_filters( 'wp_openpgp_user_key', $wp_user ) ) {
?>
<div class="notice error is-dismissible">
<p><strong><?php esc_html_e( 'There is a problem with your PGP public key.', 'wp-pgp-encrypted-emails' );?></strong></p>
<p class="description"><?php print sprintf(
esc_html__( 'Your PGP public key is what WordPress uses to encrypt emails it sends to you so that only you can read them. Unfortunately, something is wrong or missing in %1$sthe public key saved in your profile%2$s.', 'wp-pgp-encrypted-emails' ),
'<a href="' . admin_url( 'profile.php#' . self::meta_key ) . '">', '</a>'
);?></p>
</div>
<?php
}
}
/**
* Prints a warning to the admin if their PGP public key can't be used.
*
* @uses get_option()
* @uses WP_PGP_Encrypted_Emails::getAdminKey()
* @uses admin_url()
*
* @return void
*/
public static function adminNoticeBadAdminKey () {
$options = get_option( self::meta_key );
if ( current_user_can( 'manage_options' )
&& ! empty( $options )
&& ! self::getAdminKey() )
{
?>
<div class="notice error is-dismissible">
<p><strong><?php esc_html_e( 'There is a problem with your admin email PGP public key.', 'wp-pgp-encrypted-emails' );?></strong></p>
<p class="description"><?php print sprintf(
esc_html__( 'Your PGP public key is what WordPress uses to encrypt emails it sends to you so that only you can read them. Unfortunately, something is wrong or missing in %1$sthe admin email public key option%2$s.', 'wp-pgp-encrypted-emails' ),
'<a href="' . admin_url( 'options-general.php?page=wp-pgp-encrypted-emails#' . self::meta_key ) . '">', '</a>'
);?></p>
</div>
<?php
}
}
/**
* Prints a warning to the user if their S/MIME public certificate can't be used.
*
* @uses WP_PGP_Encrypted_Emails::getUserCert()
* @uses wp_get_current_user()
* @uses admin_url()
*
* @return void
*/
public static function adminNoticeBadUserCert () {
$wp_user = wp_get_current_user();
if ( ! empty( $wp_user->{self::meta_smime_certificate} ) && ! apply_filters( 'wp_smime_user_certificate', $wp_user ) ) {
?>
<div class="notice error is-dismissible">
<p><strong><?php esc_html_e( 'There is a problem with your S/MIME public certificate.', 'wp-pgp-encrypted-emails' );?></strong></p>
<p class="description"><?php print sprintf(
esc_html__( 'Your S/MIME public certifcate is what WordPress uses to encrypt emails it sends to you so that only you can read them. Unfortunately, something is wrong or missing in %1$sthe public key saved in your profile%2$s.', 'wp-pgp-encrypted-emails' ),
'<a href="' . admin_url( 'profile.php#'.self::meta_smime_certificate ) . '">', '</a>'
);?></p>
</div>
<?php
}
}
/**
* Prints a warning to the admin if their S/MIME public certificate can't be used.
*
* @uses get_option()
* @uses WP_PGP_Encrypted_Emails::getAdminCert()
* @uses admin_url()
*
* @return void
*/
public static function adminNoticeBadAdminCert () {
$options = get_option( self::meta_smime_certificate );
if ( current_user_can( 'manage_options' )
&& ! empty( $options )
&& ! self::getAdminCert() )
{
?>
<div class="notice error is-dismissible">
<p><strong><?php esc_html_e( 'There is a problem with your admin email S/MIME public certificate.', 'wp-pgp-encrypted-emails' );?></strong></p>
<p class="description"><?php print sprintf(
esc_html__( 'Your S/MIME public certificate is what WordPress uses to encrypt emails it sends to you so that only you can read them. Unfortunately, something is wrong or missing in %1$sthe admin email public key option%2$s.', 'wp-pgp-encrypted-emails' ),
'<a href="' . admin_url( 'options-general.php?page=wp-pgp-encrypted-emails#' . self::meta_key ) . '">', '</a>'
);?></p>
</div>
<?php
}
}
/**
* Prints the HTML for the custom profile fields.
*
* @param WP_User $profileuser
*
* @return void
*/
public static function renderProfile ( $profileuser ) {
require_once 'admin/profile.php';
}
/**
* Prints HTML for admin submenu settings page heading.
*
* @return void
*/
public static function renderOptionsPage ( $args ) {
?>
<div class="wrap">
<h1><?php esc_html_e( 'Email Encryption', 'wp-pgp-encrypted-emails' ); ?></h1>
<form action="options.php" method="POST">
<?php
settings_fields( 'wp-pgp-encrypted-emails' );
do_settings_sections( 'wp-pgp-encrypted-emails' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Prints the header for the PGP settings section.
*
* @see self::renderOptionsPage()
*
* @return void
*/
public static function renderPGPSettingSection ( $args ) {
// TODO: Add section header text?
}
/**
* Prints the header for the S/MIME settings section.
*
* @see self::renderOptionsPage()
*
* @return void
*/
public static function renderSMIMESettingSection ( $args ) {
// TODO: Add section header text?
}
/**
* Prints the header for the Plugin Extras settings section.
*
* @see self::renderOptionsPage()
*
* @return void
*/
public static function renderDeliverySettingSection ( $args ) {
// TODO: Add section header text?
}
/**
* Prints the header for the Plugin Extras settings section.
*
* @see self::renderOptionsPage()
*
* @return void
*/
public static function renderPluginExtrasSettingSection ( $args ) {
// TODO: Add section header text?
}
/**
* Prints the HTML for the plugin's admin PGP key setting.
*
* @return void
*/
public static function renderAdminPGPKeySetting () {
?>
<textarea
id="<?php print esc_attr(self::meta_key);?>"
name="<?php print esc_attr(self::meta_key);?>"
class="large-text code"
rows="5"
><?php print esc_textarea(get_option(self::meta_key));?></textarea>
<p class="description">
<?php print sprintf(
esc_html__('Paste the PGP public key for the admin email here to have WordPress encrypt admin emails it sends. Leave this blank if you do not want to get or know how to decrypt encrypted emails.', 'wp-pgp-encrypted-emails')
);?>
</p>
<?php
}
/**
* Prints the HTML for the plugin's admin S/MIME cert setting.
*
* @return void
*/
public static function renderAdminSMIMEKeySetting () {
?>
<textarea
id="<?php print esc_attr(self::meta_smime_certificate);?>"
name="<?php print esc_attr(self::meta_smime_certificate);?>"
class="large-text code"
rows="5"
><?php print esc_textarea(get_option(self::meta_smime_certificate));?></textarea>
<p class="description">
<?php print sprintf(
esc_html__('Paste the S/MIME public certificate for the admin email here to have WordPress encrypt admin emails it sends. Leave this blank if you do not want to get or know how to decrypt encrypted emails.', 'wp-pgp-encrypted-emails')
);?>
</p>
<?php
}
/**
* Prints the HTML for the plugin's encryption type setting.
*
* This is only registered if both options are available. If they
* are not both available, the choice is obvious. ;)
*
* @return void
*/
public static function renderEncryptionMethodSetting () {
$method = self::getAdminEncryptionMethod();
?>
<select
id="<?php print esc_attr( self::meta_encryption_method ); ?>"
name="<?php print esc_attr( self::meta_encryption_method ); ?>">
<option
value="pgp"
<?php selected( $method, 'pgp' ); ?>
>
<?php esc_html_e( 'PGP/GPG', 'wp-pgp-encrypted-emails' ); ?>
</option>
<option
value="smime"
<?php selected( $method, 'smime' ); ?>
>
<?php esc_html_e( 'S/MIME', 'wp-pgp-encrypted-emails' ); ?>
</option>
</select>
<p class="description">
<?php esc_html_e( 'When both PGP and S/MIME encryption are available, this option instructs the plugin which method to attempt first. If the chosen method fails, the other method is attempted.', 'wp-pgp-encrypted-emails' ); ?>
</p>
<?php
}
/**
* Prints the HTML for the plugin's admin subject line setting.
*
* @return void
*/
public static function renderEmptySubjectLineSetting () {
?>
<input type="checkbox"
id="<?php print esc_attr(self::meta_key_empty_subject_line);?>"
name="<?php print esc_attr(self::meta_key_empty_subject_line);?>"
<?php checked(get_option(self::meta_key_empty_subject_line));?>
value="1"
/>
<span class="description">
<?php print sprintf(
esc_html__('Email encryption cannot encrypt envelope information (such as the subject) of an email, so if you want maximum privacy, make sure this option is enabled to always erase the subject line from encrypted emails you receive.', 'wp-pgp-encrypted-emails')
);?>
</span>
<?php
}
/**
* Prints the HTML for the plugin's toggle to include signatures
* when emailing unrecognized addresses.
*
* @return void
*/
public static function renderSignForUnknownRecipients () {
?>
<input type="checkbox"
id="<?php print esc_attr(self::meta_key_sign_for_unknown_recipients);?>"
name="<?php print esc_attr(self::meta_key_sign_for_unknown_recipients);?>"
<?php checked(get_option(self::meta_key_sign_for_unknown_recipients));?>
value="1"
/>
<span class="description">
<?php print sprintf(
esc_html__('When enabled, all outbound emails will be signed with the PGP signing keypair. This includes email destined for addresses without an associated user account.', 'wp-pgp-encrypted-emails')
);?>
</span>
<?php
}
/**
* Prints the HTML for the plugin's purge-on-uninstall toggle.
*
* @return void
*/
public static function renderPurgeAllSetting () {
?>
<input type="checkbox"
id="<?php print esc_attr( self::meta_key_purge_all ); ?>"
name="<?php print esc_attr( self::meta_key_purge_all ); ?>"
<?php checked( get_option( self::meta_key_purge_all ) ); ?>
value="1"
/>
<span class="description">
<?php print sprintf(
esc_html__( 'When enabled, all private key material will be deleted from the database when the %1$sWP PGP Encrypted Emails plugin%2$s is uninstalled.', 'wp-pgp-encrypted-emails' ),
'<a href="' . admin_url( esc_url( 'plugins.php?s=wp-pgp-encrypted-emails&plugin_status=all' ) ) . '">', '</a>'
); ?>
</span>
<?php
}
/**