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