68fc24ff845d0e19d9f95af9fce437fd048d1fb6
[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 use Friendica\App;
10 use Friendica\Core\PConfig;
11 use Friendica\Util\Emailer;
12
13 /* because the fraking openpgp-php is in composer, require libs in composer
14  * and then don't use autoloader to load classes... */
15 $path = __DIR__ . '/vendor/phpseclib/phpseclib/phpseclib/';
16 set_include_path(get_include_path() . PATH_SEPARATOR . $path);
17 /* so, we don't use the autoloader and include what we need */
18 $path = __DIR__ . '/vendor/singpolyma/openpgp-php/lib';
19 set_include_path(get_include_path() . PATH_SEPARATOR . $path);
20
21 require_once 'openpgp.php';
22 require_once 'openpgp_crypt_symmetric.php';
23
24
25 function securemail_install() {
26     register_hook('plugin_settings', 'addon/securemail/securemail.php', 'securemail_settings');
27     register_hook('plugin_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
28
29     register_hook('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
30
31     logger('installed securemail');
32 }
33
34 function securemail_uninstall() {
35     unregister_hook('plugin_settings', 'addon/securemail/securemail.php', 'securemail_settings');
36     unregister_hook('plugin_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
37
38     unregister_hook('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
39
40     logger('removed securemail');
41 }
42
43 /**
44  * @brief Build user settings form
45  *
46  * @link https://github.com/friendica/friendica/blob/develop/doc/Plugins.md#plugin_settings 'plugin_settings' hook
47  *
48  * @param App $a App instance
49  * @param string $s output html
50  *
51  * @see App
52  */
53 function securemail_settings(App &$a, &$s){
54     if (!local_user()) {
55         return;
56     }
57
58     $enable = intval(PConfig::get(local_user(), 'securemail', 'enable'));
59     $publickey = PConfig::get(local_user(), 'securemail', 'pkey');
60
61     $t = get_markup_template('admin.tpl', 'addon/securemail/');
62
63     $s .= replace_macros($t, [
64         '$title' => t('"Secure Mail" Settings'),
65         '$submit' => t('Save Settings'),
66         '$test' => t('Save and send test'), //NOTE: update also in 'post'
67         '$enable' => ['securemail-enable', t('Enable Secure Mail'), $enable, ''],
68         '$publickey' => ['securemail-pkey', t('Public key'), $publickey, t('Your public PGP key, ascii armored format'), 'rows="10"']
69     ]);
70 }
71
72 /**
73  * @brief Handle data from user settings form
74  *
75  * @link https://github.com/friendica/friendica/blob/develop/doc/Plugins.md#plugin_settings_post 'plugin_settings_post' hook
76  *
77  * @param App $a App instance
78  * @param array $b hook data
79  *
80  * @see App
81  */
82 function securemail_settings_post(App &$a, array &$b){
83
84     if (!local_user()) {
85         return;
86     }
87
88     if ($_POST['securemail-submit']) {
89         PConfig::set(local_user(), 'securemail', 'pkey', trim($_POST['securemail-pkey']));
90         $enable = ((x($_POST, 'securemail-enable')) ? 1 : 0);
91         PConfig::set(local_user(), 'securemail', 'enable', $enable);
92         info(t('Secure Mail Settings saved.') . EOL);
93
94         if ($_POST['securemail-submit'] == t('Save and send test')) {
95             $sitename = $a->config['sitename'];
96
97             $hostname = $a->get_hostname();
98             if (strpos($hostname, ':')) {
99                 $hostname = substr($hostname, 0, strpos($hostname, ':'));
100             }
101
102             $sender_email = $a->config['sender_email'];
103             if (empty($sender_email)) {
104                 $sender_email = 'noreply@' . $hostname;
105             }
106
107             $subject = 'Friendica - Secure Mail - Test';
108             $message = 'This is a test message from your Friendica Secure Mail addon.';
109
110             $params = [
111                 'uid' => local_user(),
112                 'fromName' => $sitename,
113                 'fromEmail' => $sender_email,
114                 'toEmail' => $a->user['email'],
115                 'messageSubject' => $subject,
116                 'htmlVersion' => "<p>{$message}</p>",
117                 'textVersion' => $message,
118             ];
119
120             // enable addon for test
121             PConfig::set(local_user(), 'securemail', 'enable', 1);
122
123             $res = Emailer::send($params);
124
125             // revert to saved value
126             PConfig::set(local_user(), 'securemail', 'enable', $enable);
127
128             if ($res) {
129                 info(t('Test email sent') . EOL);
130             } else {
131                 notice(t('There was an error sending the test email') . EOL);
132             }
133         }
134     }
135 }
136
137 /**
138  * @brief Encrypt notification emails text
139  *
140  * @link https://github.com/friendica/friendica/blob/develop/doc/Plugins.md#emailer_send_prepare 'emailer_send_prepare' hook
141  *
142  * @param App $a App instance
143  * @param array $b hook data
144  *
145  * @see App
146  */
147 function securemail_emailer_send_prepare(App &$a, array &$b) {
148     if (!x($b, 'uid')) {
149         return;
150     }
151
152     $uid = $b['uid'];
153
154     $enable_checked = PConfig::get($uid, 'securemail', 'enable');
155     if (!$enable_checked) {
156         return;
157     }
158
159     $public_key_ascii = PConfig::get($uid, 'securemail', 'pkey');
160
161     preg_match('/-----BEGIN ([A-Za-z ]+)-----/', $public_key_ascii, $matches);
162     $marker = (empty($matches[1])) ? 'MESSAGE' : $matches[1];
163     $public_key = OpenPGP::unarmor($public_key_ascii, $marker);
164
165     $key = OpenPGP_Message::parse($public_key);
166
167     $data = new OpenPGP_LiteralDataPacket($b['textVersion'], [
168         'format' => 'u',
169         'filename' => 'encrypted.gpg'
170     ]);
171     $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message([$data]));
172     $armored_encrypted = wordwrap(
173         OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'),
174         64,
175         "\n",
176         true
177     );
178
179     $b['textVersion'] = $armored_encrypted;
180     $b['htmlVersion'] = null;
181 }
182
183
184 /**
185  * add addon composer autoloader maps to system autoloader
186
187 function securemail_autoloader() {
188
189     $loader = require dirname(dirname(__DIR__)) . '/vendor/autoload.php';
190
191     $map = require __DIR__ . '/vendor/composer/autoload_namespaces.php';
192     foreach ($map as $namespace => $path) {
193         $loader->set($namespace, $path);
194     }
195
196     $map = require __DIR__ . '/vendor/composer/autoload_psr4.php';
197     foreach ($map as $namespace => $path) {
198         $loader->setPsr4($namespace, $path);
199     }
200
201     $classMap = require __DIR__ . '/vendor/composer/autoload_classmap.php';
202     if ($classMap) {
203         $loader->addClassMap($classMap);
204     }
205 }
206 securemail_autoloader();
207
208 */