]> git.mxchange.org Git - friendica-addons.git/blob - securemail/securemail.php
c4dc6c93c7af429b60103e1008e1fbdf60e9f1f0
[friendica-addons.git] / securemail / securemail.php
1 <?php
2 /**
3  * Name: Secure Mail
4  * Description: Send notification mail encrypted with user-defined public GPG key
5  * Version: 2.0
6  * Author: Fabio Comuni <http://kirgroup.com/profile/fabrixxm>
7  */
8
9 require_once "include/Emailer.php";
10
11 /* because the fraking openpgp-php is in composer, require libs in composer
12  * and then don't use autoloader to load classes... */
13 $path = __DIR__."/vendor/phpseclib/phpseclib/phpseclib/";
14 set_include_path(get_include_path() . PATH_SEPARATOR . $path);
15 /* so, we don't use the autoloader and include what we need */
16 $path = __DIR__."/vendor/singpolyma/openpgp-php/lib";
17 set_include_path(get_include_path() . PATH_SEPARATOR . $path);
18
19 require_once "openpgp.php";
20 require_once "openpgp_crypt_symmetric.php";
21
22
23 function securemail_install() {
24     register_hook('plugin_settings', 'addon/securemail/securemail.php', 'securemail_settings');
25     register_hook('plugin_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
26
27     register_hook('emailer_send_prepare',  'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
28
29     logger("installed securemail");
30 }
31
32 function securemail_uninstall() {
33     unregister_hook('plugin_settings', 'addon/securemail/securemail.php', 'securemail_settings');
34     unregister_hook('plugin_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
35
36     unregister_hook('emailer_send_prepare',  'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
37
38     logger("removed securemail");
39 }
40
41
42 function securemail_settings(&$a,&$s){
43     if(! local_user()) {
44         return;
45     }
46
47     $enable = intval(get_pconfig(local_user(),'securemail','enable'));
48     $publickey = get_pconfig(local_user(),'securemail','pkey');
49
50     $t = get_markup_template( "admin.tpl", "addon/securemail/" );
51
52     $s = replace_macros($t, array(
53         '$title' => t('"Secure Mail" Settings'),
54         '$submit' => t('Save Settings'),
55         '$test' => t('Save and send test'), //NOTE: update also in 'post'
56         '$enable' => array('securemail-enable', t('Enable Secure Mail'), $enable, ""),
57         '$publickey' => array('securemail-pkey', t('Public key'), $publickey, t("Your public PGP key, ascii armored format"), "rows='10'")
58     ));
59
60
61
62     return;
63 }
64 function securemail_settings_post(&$a, &$b){
65
66     if(! local_user()) {
67         return;
68     }
69
70     if($_POST['securemail-submit']) {
71         set_pconfig(local_user(),'securemail','pkey',trim($_POST['securemail-pkey']));
72         $enable = ((x($_POST,'securemail-enable')) ? 1 : 0);
73         set_pconfig(local_user(),'securemail','enable', $enable);
74         info( t('Secure Mail Settings saved.') . EOL);
75
76         if ($_POST['securemail-submit'] == t('Save and send test')) {
77             $sitename = $a->config['sitename'];
78
79             $hostname = $a->get_hostname();
80             if (strpos($hostname, ':')){
81                 $hostname = substr($hostname, 0, strpos($hostname, ':'));
82             }
83
84             $sender_email = $a->config['sender_email'];
85             if (empty($sender_email)){
86                 $sender_email = 'noreply@'.$hostname;
87             }
88
89             $subject = "Friendica - Secure Mail - Test";
90             $message = "This is a test message from your Friendica Secure Mail addon.\n\nBye!";
91
92             $params = array(
93                 'uid' => local_user(),
94                 'fromName' => $sitename,
95                 'fromEmail' => $sender_email,
96                 'toEmail' => $a->user['email'],
97                 'messageSubject' => $subject,
98                 'htmlVersion' => "<p>{$message}</p>",
99                 'textVersion' => $message,
100             );
101
102             // enable addon for test
103             set_pconfig(local_user(),'securemail','enable', 1);
104
105             $res = Emailer::send($params);
106
107             // revert to saved value
108             set_pconfig(local_user(),'securemail','enable', $enable);
109
110             if ($res) {
111                 info( t("Test email sent") . EOL);
112             } else {
113                 notice( t("There was an error sending the test email") .EOL);
114             }
115         }
116     }
117 }
118
119 function securemail_emailer_send_prepare(&$a, &$b) {
120     if (!x($b,'uid')) {
121         return;
122     }
123
124     $uid = $b['uid'];
125
126     $enable_checked = get_pconfig($uid,'securemail','enable');
127     if (!$enable_checked) {
128         return;
129     }
130
131     $public_key_ascii = get_pconfig($uid,'securemail','pkey');
132
133     preg_match('/-----BEGIN ([A-Za-z ]+)-----/', $public_key_ascii, $matches);
134     $marker = (empty($matches[1])) ? 'MESSAGE' : $matches[1];
135     $public_key = OpenPGP::unarmor($public_key_ascii, $marker);
136
137     $key = OpenPGP_Message::parse($public_key);
138
139     $data = new OpenPGP_LiteralDataPacket($b['textVersion'], array(
140         'format' => 'u',
141         'filename' => 'encrypted.gpg'
142     ));
143     $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message(array($data)));
144     $armored_encrypted = wordwrap(OpenPGP::enarmor($encrypted->to_bytes(), "PGP MESSAGE"), 64, "\n", true);
145
146     $b['textVersion'] = $armored_encrypted;
147     $b['htmlVersion'] = null;
148 }
149
150
151 /**
152  * add addon composer autoloader maps to system autoloader
153
154 function securemail_autoloader() {
155
156     $loader = require dirname(dirname(__DIR__))."/vendor/autoload.php";
157
158     $map = require __DIR__ . '/vendor/composer/autoload_namespaces.php';
159     foreach ($map as $namespace => $path) {
160         $loader->set($namespace, $path);
161     }
162
163     $map = require __DIR__ . '/vendor/composer/autoload_psr4.php';
164     foreach ($map as $namespace => $path) {
165         $loader->setPsr4($namespace, $path);
166     }
167
168     $classMap = require __DIR__ . '/vendor/composer/autoload_classmap.php';
169     if ($classMap) {
170         $loader->addClassMap($classMap);
171     }
172 }
173 securemail_autoloader();
174
175 */