4 * Description: Send notification mail encrypted with user-defined public GPG key
6 * Author: Fabio Comuni <http://kirgroup.com/profile/fabrixxm>
11 require_once 'include/Emailer.php';
13 /* because the fraking openpgp-php is in composer, require libs in composer
14 * and then don't use autoloader to load classes... */
15 $path = __DIR__ . '/vendor/phpseclib/phpseclib/phpseclib/';
16 set_include_path(get_include_path() . PATH_SEPARATOR . $path);
17 /* so, we don't use the autoloader and include what we need */
18 $path = __DIR__ . '/vendor/singpolyma/openpgp-php/lib';
19 set_include_path(get_include_path() . PATH_SEPARATOR . $path);
21 require_once 'openpgp.php';
22 require_once 'openpgp_crypt_symmetric.php';
25 function securemail_install() {
26 register_hook('plugin_settings', 'addon/securemail/securemail.php', 'securemail_settings');
27 register_hook('plugin_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
29 register_hook('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
31 logger('installed securemail');
34 function securemail_uninstall() {
35 unregister_hook('plugin_settings', 'addon/securemail/securemail.php', 'securemail_settings');
36 unregister_hook('plugin_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
38 unregister_hook('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
40 logger('removed securemail');
44 * @brief Build user settings form
46 * @link https://github.com/friendica/friendica/blob/develop/doc/Plugins.md#plugin_settings 'plugin_settings' hook
48 * @param App $a App instance
49 * @param string $s output html
53 function securemail_settings(App &$a, &$s){
58 $enable = intval(get_pconfig(local_user(), 'securemail', 'enable'));
59 $publickey = get_pconfig(local_user(), 'securemail', 'pkey');
61 $t = get_markup_template('admin.tpl', 'addon/securemail/');
63 $s .= replace_macros($t, array(
64 '$title' => t('"Secure Mail" Settings'),
65 '$submit' => t('Save Settings'),
66 '$test' => t('Save and send test'), //NOTE: update also in 'post'
67 '$enable' => array('securemail-enable', t('Enable Secure Mail'), $enable, ''),
68 '$publickey' => array('securemail-pkey', t('Public key'), $publickey, t('Your public PGP key, ascii armored format'), 'rows="10"')
73 * @brief Handle data from user settings form
75 * @link https://github.com/friendica/friendica/blob/develop/doc/Plugins.md#plugin_settings_post 'plugin_settings_post' hook
77 * @param App $a App instance
78 * @param array $b hook data
82 function securemail_settings_post(App &$a, array &$b){
88 if ($_POST['securemail-submit']) {
89 set_pconfig(local_user(), 'securemail', 'pkey', trim($_POST['securemail-pkey']));
90 $enable = ((x($_POST, 'securemail-enable')) ? 1 : 0);
91 set_pconfig(local_user(), 'securemail', 'enable', $enable);
92 info(t('Secure Mail Settings saved.') . EOL);
94 if ($_POST['securemail-submit'] == t('Save and send test')) {
95 $sitename = $a->config['sitename'];
97 $hostname = $a->get_hostname();
98 if (strpos($hostname, ':')) {
99 $hostname = substr($hostname, 0, strpos($hostname, ':'));
102 $sender_email = $a->config['sender_email'];
103 if (empty($sender_email)) {
104 $sender_email = 'noreply@' . $hostname;
107 $subject = 'Friendica - Secure Mail - Test';
108 $message = 'This is a test message from your Friendica Secure Mail addon.';
111 'uid' => local_user(),
112 'fromName' => $sitename,
113 'fromEmail' => $sender_email,
114 'toEmail' => $a->user['email'],
115 'messageSubject' => $subject,
116 'htmlVersion' => "<p>{$message}</p>",
117 'textVersion' => $message,
120 // enable addon for test
121 set_pconfig(local_user(), 'securemail', 'enable', 1);
123 $res = Emailer::send($params);
125 // revert to saved value
126 set_pconfig(local_user(), 'securemail', 'enable', $enable);
129 info(t('Test email sent') . EOL);
131 notice(t('There was an error sending the test email') . EOL);
138 * @brief Encrypt notification emails text
140 * @link https://github.com/friendica/friendica/blob/develop/doc/Plugins.md#emailer_send_prepare 'emailer_send_prepare' hook
142 * @param App $a App instance
143 * @param array $b hook data
147 function securemail_emailer_send_prepare(App &$a, array &$b) {
154 $enable_checked = get_pconfig($uid, 'securemail', 'enable');
155 if (!$enable_checked) {
159 $public_key_ascii = get_pconfig($uid, 'securemail', 'pkey');
161 preg_match('/-----BEGIN ([A-Za-z ]+)-----/', $public_key_ascii, $matches);
162 $marker = (empty($matches[1])) ? 'MESSAGE' : $matches[1];
163 $public_key = OpenPGP::unarmor($public_key_ascii, $marker);
165 $key = OpenPGP_Message::parse($public_key);
167 $data = new OpenPGP_LiteralDataPacket($b['textVersion'], array(
169 'filename' => 'encrypted.gpg'
171 $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message(array($data)));
172 $armored_encrypted = wordwrap(
173 OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'),
179 $b['textVersion'] = $armored_encrypted;
180 $b['htmlVersion'] = null;
185 * add addon composer autoloader maps to system autoloader
187 function securemail_autoloader() {
189 $loader = require dirname(dirname(__DIR__)) . '/vendor/autoload.php';
191 $map = require __DIR__ . '/vendor/composer/autoload_namespaces.php';
192 foreach ($map as $namespace => $path) {
193 $loader->set($namespace, $path);
196 $map = require __DIR__ . '/vendor/composer/autoload_psr4.php';
197 foreach ($map as $namespace => $path) {
198 $loader->setPsr4($namespace, $path);
201 $classMap = require __DIR__ . '/vendor/composer/autoload_classmap.php';
203 $loader->addClassMap($classMap);
206 securemail_autoloader();