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 function securemail_settings(&$a,&$s){
47 $enable = intval(get_pconfig(local_user(),'securemail','enable'));
48 $publickey = get_pconfig(local_user(),'securemail','pkey');
50 $t = get_markup_template( "admin.tpl", "addon/securemail/" );
52 $s = replace_macros($t, array(
53 '$title' => t('"Secure Mail" Settings'),
54 '$submit' => t('Save Settings'),
55 '$test' => t('Save and send test'), //NOTE: update also in 'post'
56 '$enable' => array('securemail-enable', t('Enable Secure Mail'), $enable, ""),
57 '$publickey' => array('securemail-pkey', t('Public key'), $publickey, t("Your public PGP key, ascii armored format"), "rows='10'")
64 function securemail_settings_post(&$a, &$b){
70 if($_POST['securemail-submit']) {
71 set_pconfig(local_user(),'securemail','pkey',trim($_POST['securemail-pkey']));
72 $enable = ((x($_POST,'securemail-enable')) ? 1 : 0);
73 set_pconfig(local_user(),'securemail','enable', $enable);
74 info( t('Secure Mail Settings saved.') . EOL);
76 if ($_POST['securemail-submit'] == t('Save and send test')) {
77 $sitename = $a->config['sitename'];
79 $hostname = $a->get_hostname();
80 if (strpos($hostname, ':')){
81 $hostname = substr($hostname, 0, strpos($hostname, ':'));
84 $sender_email = $a->config['sender_email'];
85 if (empty($sender_email)){
86 $sender_email = 'noreply@'.$hostname;
89 $subject = "Friendica - Secure Mail - Test";
90 $message = "This is a test message from your Friendica Secure Mail addon.\n\nBye!";
93 'uid' => local_user(),
94 'fromName' => $sitename,
95 'fromEmail' => $sender_email,
96 'toEmail' => $a->user['email'],
97 'messageSubject' => $subject,
98 'htmlVersion' => "<p>{$message}</p>",
99 'textVersion' => $message,
102 // enable addon for test
103 set_pconfig(local_user(),'securemail','enable', 1);
105 $res = Emailer::send($params);
107 // revert to saved value
108 set_pconfig(local_user(),'securemail','enable', $enable);
111 info( t("Test email sent") . EOL);
113 notice( t("There was an error sending the test email") .EOL);
119 function securemail_emailer_send_prepare(&$a, &$b) {
126 $enable_checked = get_pconfig($uid,'securemail','enable');
127 if (!$enable_checked) {
131 $public_key_ascii = get_pconfig($uid,'securemail','pkey');
133 preg_match('/-----BEGIN ([A-Za-z ]+)-----/', $public_key_ascii, $matches);
134 $marker = (empty($matches[1])) ? 'MESSAGE' : $matches[1];
135 $public_key = OpenPGP::unarmor($public_key_ascii, $marker);
137 $key = OpenPGP_Message::parse($public_key);
139 $data = new OpenPGP_LiteralDataPacket($b['textVersion'], array(
141 'filename' => 'encrypted.gpg'
143 $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message(array($data)));
144 $armored_encrypted = wordwrap(OpenPGP::enarmor($encrypted->to_bytes(), "PGP MESSAGE"), 64, "\n", true);
146 $b['textVersion'] = $armored_encrypted;
147 $b['htmlVersion'] = null;
152 * add addon composer autoloader maps to system autoloader
154 function securemail_autoloader() {
156 $loader = require dirname(dirname(__DIR__))."/vendor/autoload.php";
158 $map = require __DIR__ . '/vendor/composer/autoload_namespaces.php';
159 foreach ($map as $namespace => $path) {
160 $loader->set($namespace, $path);
163 $map = require __DIR__ . '/vendor/composer/autoload_psr4.php';
164 foreach ($map as $namespace => $path) {
165 $loader->setPsr4($namespace, $path);
168 $classMap = require __DIR__ . '/vendor/composer/autoload_classmap.php';
170 $loader->addClassMap($classMap);
173 securemail_autoloader();