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