]> git.mxchange.org Git - friendica-addons.git/blob - securemail/securemail.php
modified: nitter/lang/C/messages.po
[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\Addon\securemail\SecureTestEmail;
10 use Friendica\App;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Logger;
13 use Friendica\Core\Renderer;
14 use Friendica\DI;
15 use Friendica\Object\EMail\IEmail;
16
17 require_once __DIR__ . '/vendor/autoload.php';
18
19 function securemail_install()
20 {
21         Hook::register('addon_settings', 'addon/securemail/securemail.php', 'securemail_settings');
22         Hook::register('addon_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post', 10);
23
24         Hook::register('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare', 10);
25
26         Logger::notice('installed securemail');
27 }
28
29 /**
30  * @brief Build user settings form
31  *
32  * @link  https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings 'addon_settings' hook
33  *
34  * @param App    $a App instance
35  * @param string $s output html
36  *
37  * @see   App
38  */
39 function securemail_settings(App &$a, array &$data)
40 {
41         if (!DI::userSession()->getLocalUserId()) {
42                 return;
43         }
44
45         $enabled   = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'securemail', 'enable'));
46         $publickey = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'securemail', 'pkey');
47
48         $t    = Renderer::getMarkupTemplate('settings.tpl', 'addon/securemail/');
49         $html = Renderer::replaceMacros($t, [
50                 '$enabled'   => ['securemail-enable', DI::l10n()->t('Enable Secure Mail'), $enabled],
51                 '$publickey' => ['securemail-pkey', DI::l10n()->t('Public key'), $publickey, DI::l10n()->t('Your public PGP key, ascii armored format')]
52         ]);
53
54         $data = [
55                 'addon'  => 'securemail',
56                 'title'  => DI::l10n()->t('"Secure Mail" Settings'),
57                 'html'   => $html,
58                 'submit' => [
59                         'securemail-submit' => DI::l10n()->t('Save Settings'),
60                         'securemail-test'   => DI::l10n()->t('Save and send test'),
61                 ],
62         ];
63 }
64
65 /**
66  * @brief Handle data from user settings form
67  *
68  * @link  https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings_post 'addon_settings_post' hook
69  *
70  * @param App   $a App instance
71  * @param array $b hook data
72  *
73  * @see   App
74  */
75 function securemail_settings_post(App &$a, array &$b)
76 {
77         if (!DI::userSession()->getLocalUserId()) {
78                 return;
79         }
80
81         if (!empty($_POST['securemail-submit']) || !empty($_POST['securemail-test'])) {
82                 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'securemail', 'pkey', trim($_POST['securemail-pkey']));
83                 $enable = (!empty($_POST['securemail-enable']) ? 1 : 0);
84                 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'securemail', 'enable', $enable);
85
86                 if (!empty($_POST['securemail-test'])) {
87                         $res = DI::emailer()->send(new SecureTestEmail(DI::app(), DI::config(), DI::pConfig(), DI::baseUrl()));
88
89                         // revert to saved value
90                         DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'securemail', 'enable', $enable);
91
92                         if ($res) {
93                                 DI::sysmsg()->addInfo(DI::l10n()->t('Test email sent'));
94                         } else {
95                                 DI::sysmsg()->addNotice(DI::l10n()->t('There was an error sending the test email'));
96                         }
97                 }
98         }
99 }
100
101 /**
102  * @brief Encrypt notification emails text
103  *
104  * @link  https://github.com/friendica/friendica/blob/develop/doc/Addons.md#emailer_send_prepare 'emailer_send_prepare' hook
105  *
106  * @param App   $a App instance
107  * @param IEmail $email Email
108  *
109  * @see   App
110  */
111 function securemail_emailer_send_prepare(App &$a, IEmail &$email)
112 {
113         if (empty($email->getRecipientUid())) {
114                 return;
115         }
116
117         $uid = $email->getRecipientUid();
118
119         $enable_checked = DI::pConfig()->get($uid, 'securemail', 'enable');
120         if (!$enable_checked) {
121                 DI::logger()->debug('No securemail enabled.');
122                 return;
123         }
124
125         $public_key_ascii = DI::pConfig()->get($uid, 'securemail', 'pkey');
126
127         preg_match('/-----BEGIN ([A-Za-z ]+)-----/', $public_key_ascii, $matches);
128         $marker = empty($matches[1]) ? 'MESSAGE' : $matches[1];
129         $public_key = OpenPGP::unarmor($public_key_ascii, $marker);
130
131         $key = OpenPGP_Message::parse($public_key);
132
133         $data = new OpenPGP_LiteralDataPacket($email->getMessage(true), [
134                 'format' => 'u',
135                 'filename' => 'encrypted.gpg'
136         ]);
137
138         try {
139                 $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message([$data]));
140                 $armored_encrypted = wordwrap(
141                         OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'),
142                         64,
143                         "\n",
144                         true
145                 );
146
147                 $email = $email->withMessage($armored_encrypted, null);
148
149         } catch (Exception $e) {
150                 DI::logger()->warning('Encryption failed.', ['email' => $email, 'exception' => $e]);
151         }
152 }