]> git.mxchange.org Git - friendica-addons.git/blob - securemail/securemail.php
Fix coding standards issues
[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 require_once 'include/Emailer.php';
10
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);
18
19 require_once 'openpgp.php';
20 require_once 'openpgp_crypt_symmetric.php';
21
22
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');
26
27     register_hook('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
28
29     logger('installed securemail');
30 }
31
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');
35
36     unregister_hook('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
37
38     logger('removed securemail');
39 }
40
41 /**
42  * @brief Build user settings form
43  *
44  * @link https://github.com/friendica/friendica/blob/develop/doc/Plugins.md#plugin_settings 'plugin_settings' hook
45  *
46  * @param App $a App instance
47  * @param string $s output html
48  *
49  * @see App
50  */
51 function securemail_settings(App &$a, string &$s){
52     if (!local_user()) {
53         return;
54     }
55
56     $enable = intval(get_pconfig(local_user(), 'securemail', 'enable'));
57     $publickey = get_pconfig(local_user(), 'securemail', 'pkey');
58
59     $t = get_markup_template('admin.tpl', 'addon/securemail/');
60
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"')
67     ));
68 }
69
70 /**
71  * @brief Handle data from user settings form
72  *
73  * @link https://github.com/friendica/friendica/blob/develop/doc/Plugins.md#plugin_settings_post 'plugin_settings_post' hook
74  *
75  * @param App $a App instance
76  * @param array $b hook data
77  *
78  * @see App
79  */
80 function securemail_settings_post(App &$a, array &$b){
81
82     if (!local_user()) {
83         return;
84     }
85
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);
91
92         if ($_POST['securemail-submit'] == t('Save and send test')) {
93             $sitename = $a->config['sitename'];
94
95             $hostname = $a->get_hostname();
96             if (strpos($hostname, ':')) {
97                 $hostname = substr($hostname, 0, strpos($hostname, ':'));
98             }
99
100             $sender_email = $a->config['sender_email'];
101             if (empty($sender_email)) {
102                 $sender_email = 'noreply@' . $hostname;
103             }
104
105             $subject = 'Friendica - Secure Mail - Test';
106             $message = 'This is a test message from your Friendica Secure Mail addon.\n\nBye!';
107
108             $params = array(
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,
116             );
117
118             // enable addon for test
119             set_pconfig(local_user(), 'securemail', 'enable', 1);
120
121             $res = Emailer::send($params);
122
123             // revert to saved value
124             set_pconfig(local_user(), 'securemail', 'enable', $enable);
125
126             if ($res) {
127                 info(t('Test email sent') . EOL);
128             } else {
129                 notice(t('There was an error sending the test email') . EOL);
130             }
131         }
132     }
133 }
134
135 /**
136  * @brief Encrypt notification emails text
137  *
138  * @link https://github.com/friendica/friendica/blob/develop/doc/Plugins.md#emailer_send_prepare 'emailer_send_prepare' hook
139  *
140  * @param App $a App instance
141  * @param array $b hook data
142  *
143  * @see App
144  */
145 function securemail_emailer_send_prepare(App &$a, array &$b) {
146     if (!x($b,'uid')) {
147         return;
148     }
149
150     $uid = $b['uid'];
151
152     $enable_checked = get_pconfig($uid, 'securemail', 'enable');
153     if (!$enable_checked) {
154         return;
155     }
156
157     $public_key_ascii = get_pconfig($uid, 'securemail', 'pkey');
158
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);
162
163     $key = OpenPGP_Message::parse($public_key);
164
165     $data = new OpenPGP_LiteralDataPacket($b['textVersion'], array(
166         'format' => 'u',
167         'filename' => 'encrypted.gpg'
168     ));
169     $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message(array($data)));
170     $armored_encrypted = wordwrap(
171         OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'),
172         64,
173         '\n',
174         true
175     );
176
177     $b['textVersion'] = $armored_encrypted;
178     $b['htmlVersion'] = null;
179 }
180
181
182 /**
183  * add addon composer autoloader maps to system autoloader
184
185 function securemail_autoloader() {
186
187     $loader = require dirname(dirname(__DIR__)) . '/vendor/autoload.php';
188
189     $map = require __DIR__ . '/vendor/composer/autoload_namespaces.php';
190     foreach ($map as $namespace => $path) {
191         $loader->set($namespace, $path);
192     }
193
194     $map = require __DIR__ . '/vendor/composer/autoload_psr4.php';
195     foreach ($map as $namespace => $path) {
196         $loader->setPsr4($namespace, $path);
197     }
198
199     $classMap = require __DIR__ . '/vendor/composer/autoload_classmap.php';
200     if ($classMap) {
201         $loader->addClassMap($classMap);
202     }
203 }
204 securemail_autoloader();
205
206 */