4 * Description: Send notification mail encrypted with user-defined public GPG key
6 * Author: Fabio Comuni <http://kirgroup.com/profile/fabrixxm>
10 use Friendica\Core\Addon;
11 use Friendica\Core\PConfig;
12 use Friendica\Util\Emailer;
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);
22 require_once 'openpgp.php';
23 require_once 'openpgp_crypt_symmetric.php';
26 function securemail_install() {
27 Addon::registerHook('addon_settings', 'addon/securemail/securemail.php', 'securemail_settings');
28 Addon::registerHook('addon_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
30 Addon::registerHook('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
32 logger('installed securemail');
35 function securemail_uninstall() {
36 Addon::unregisterHook('addon_settings', 'addon/securemail/securemail.php', 'securemail_settings');
37 Addon::unregisterHook('addon_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
39 Addon::unregisterHook('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
41 logger('removed securemail');
45 * @brief Build user settings form
47 * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings 'addon_settings' hook
49 * @param App $a App instance
50 * @param string $s output html
54 function securemail_settings(App &$a, &$s){
59 $enable = intval(PConfig::get(local_user(), 'securemail', 'enable'));
60 $publickey = PConfig::get(local_user(), 'securemail', 'pkey');
62 $t = get_markup_template('admin.tpl', 'addon/securemail/');
64 $s .= replace_macros($t, [
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' => ['securemail-enable', t('Enable Secure Mail'), $enable, ''],
69 '$publickey' => ['securemail-pkey', t('Public key'), $publickey, t('Your public PGP key, ascii armored format'), 'rows="10"']
74 * @brief Handle data from user settings form
76 * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings_post 'addon_settings_post' hook
78 * @param App $a App instance
79 * @param array $b hook data
83 function securemail_settings_post(App &$a, array &$b){
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);
95 if ($_POST['securemail-submit'] == t('Save and send test')) {
96 $sitename = $a->config['sitename'];
98 $hostname = $a->get_hostname();
99 if (strpos($hostname, ':')) {
100 $hostname = substr($hostname, 0, strpos($hostname, ':'));
103 $sender_email = $a->config['sender_email'];
104 if (empty($sender_email)) {
105 $sender_email = 'noreply@' . $hostname;
108 $subject = 'Friendica - Secure Mail - Test';
109 $message = 'This is a test message from your Friendica Secure Mail addon.';
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,
121 // enable addon for test
122 PConfig::set(local_user(), 'securemail', 'enable', 1);
124 $res = Emailer::send($params);
126 // revert to saved value
127 PConfig::set(local_user(), 'securemail', 'enable', $enable);
130 info(t('Test email sent') . EOL);
132 notice(t('There was an error sending the test email') . EOL);
139 * @brief Encrypt notification emails text
141 * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#emailer_send_prepare 'emailer_send_prepare' hook
143 * @param App $a App instance
144 * @param array $b hook data
148 function securemail_emailer_send_prepare(App &$a, array &$b) {
155 $enable_checked = PConfig::get($uid, 'securemail', 'enable');
156 if (!$enable_checked) {
160 $public_key_ascii = PConfig::get($uid, 'securemail', 'pkey');
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);
166 $key = OpenPGP_Message::parse($public_key);
168 $data = new OpenPGP_LiteralDataPacket($b['textVersion'], [
170 'filename' => 'encrypted.gpg'
172 $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message([$data]));
173 $armored_encrypted = wordwrap(
174 OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'),
180 $b['textVersion'] = $armored_encrypted;
181 $b['htmlVersion'] = null;
186 * add addon composer autoloader maps to system autoloader
188 function securemail_autoloader() {
190 $loader = require dirname(dirname(__DIR__)) . '/vendor/autoload.php';
192 $map = require __DIR__ . '/vendor/composer/autoload_namespaces.php';
193 foreach ($map as $namespace => $path) {
194 $loader->set($namespace, $path);
197 $map = require __DIR__ . '/vendor/composer/autoload_psr4.php';
198 foreach ($map as $namespace => $path) {
199 $loader->setPsr4($namespace, $path);
202 $classMap = require __DIR__ . '/vendor/composer/autoload_classmap.php';
204 $loader->addClassMap($classMap);
207 securemail_autoloader();