4 * Description: Send notification mail encrypted with user-defined public GPG key
6 * Author: Fabio Comuni <http://kirgroup.com/profile/fabrixxm>
9 require_once 'include/Emailer.php';
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);
19 require_once 'openpgp.php';
20 require_once 'openpgp_crypt_symmetric.php';
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');
27 register_hook('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
29 logger('installed securemail');
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');
36 unregister_hook('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
38 logger('removed securemail');
42 * @brief Build user settings form
44 * @link https://github.com/friendica/friendica/blob/develop/doc/Plugins.md#plugin_settings 'plugin_settings' hook
46 * @param App $a App instance
47 * @param string $s output html
51 function securemail_settings(App &$a, string &$s){
56 $enable = intval(get_pconfig(local_user(), 'securemail', 'enable'));
57 $publickey = get_pconfig(local_user(), 'securemail', 'pkey');
59 $t = get_markup_template('admin.tpl', 'addon/securemail/');
61 $s = replace_macros($t, array(
62 '$title' => t('"Secure Mail" Settings'),
63 '$submit' => t('Save Settings'),
64 '$test' => t('Save and send test'), //NOTE: update also in 'post'
65 '$enable' => array('securemail-enable', t('Enable Secure Mail'), $enable, ''),
66 '$publickey' => array('securemail-pkey', t('Public key'), $publickey, t('Your public PGP key, ascii armored format'), 'rows="10"')
71 * @brief Handle data from user settings form
73 * @link https://github.com/friendica/friendica/blob/develop/doc/Plugins.md#plugin_settings_post 'plugin_settings_post' hook
75 * @param App $a App instance
76 * @param array $b hook data
80 function securemail_settings_post(App &$a, array &$b){
86 if ($_POST['securemail-submit']) {
87 set_pconfig(local_user(), 'securemail', 'pkey', trim($_POST['securemail-pkey']));
88 $enable = ((x($_POST, 'securemail-enable')) ? 1 : 0);
89 set_pconfig(local_user(), 'securemail', 'enable', $enable);
90 info(t('Secure Mail Settings saved.') . EOL);
92 if ($_POST['securemail-submit'] == t('Save and send test')) {
93 $sitename = $a->config['sitename'];
95 $hostname = $a->get_hostname();
96 if (strpos($hostname, ':')) {
97 $hostname = substr($hostname, 0, strpos($hostname, ':'));
100 $sender_email = $a->config['sender_email'];
101 if (empty($sender_email)) {
102 $sender_email = 'noreply@' . $hostname;
105 $subject = 'Friendica - Secure Mail - Test';
106 $message = 'This is a test message from your Friendica Secure Mail addon.\n\nBye!';
109 'uid' => local_user(),
110 'fromName' => $sitename,
111 'fromEmail' => $sender_email,
112 'toEmail' => $a->user['email'],
113 'messageSubject' => $subject,
114 'htmlVersion' => "<p>{$message}</p>",
115 'textVersion' => $message,
118 // enable addon for test
119 set_pconfig(local_user(), 'securemail', 'enable', 1);
121 $res = Emailer::send($params);
123 // revert to saved value
124 set_pconfig(local_user(), 'securemail', 'enable', $enable);
127 info(t('Test email sent') . EOL);
129 notice(t('There was an error sending the test email') . EOL);
136 * @brief Encrypt notification emails text
138 * @link https://github.com/friendica/friendica/blob/develop/doc/Plugins.md#emailer_send_prepare 'emailer_send_prepare' hook
140 * @param App $a App instance
141 * @param array $b hook data
145 function securemail_emailer_send_prepare(App &$a, array &$b) {
152 $enable_checked = get_pconfig($uid, 'securemail', 'enable');
153 if (!$enable_checked) {
157 $public_key_ascii = get_pconfig($uid, 'securemail', 'pkey');
159 preg_match('/-----BEGIN ([A-Za-z ]+)-----/', $public_key_ascii, $matches);
160 $marker = (empty($matches[1])) ? 'MESSAGE' : $matches[1];
161 $public_key = OpenPGP::unarmor($public_key_ascii, $marker);
163 $key = OpenPGP_Message::parse($public_key);
165 $data = new OpenPGP_LiteralDataPacket($b['textVersion'], array(
167 'filename' => 'encrypted.gpg'
169 $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message(array($data)));
170 $armored_encrypted = wordwrap(
171 OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'),
177 $b['textVersion'] = $armored_encrypted;
178 $b['htmlVersion'] = null;
183 * add addon composer autoloader maps to system autoloader
185 function securemail_autoloader() {
187 $loader = require dirname(dirname(__DIR__)) . '/vendor/autoload.php';
189 $map = require __DIR__ . '/vendor/composer/autoload_namespaces.php';
190 foreach ($map as $namespace => $path) {
191 $loader->set($namespace, $path);
194 $map = require __DIR__ . '/vendor/composer/autoload_psr4.php';
195 foreach ($map as $namespace => $path) {
196 $loader->setPsr4($namespace, $path);
199 $classMap = require __DIR__ . '/vendor/composer/autoload_classmap.php';
201 $loader->addClassMap($classMap);
204 securemail_autoloader();