]> git.mxchange.org Git - friendica-addons.git/blobdiff - securemail/securemail.php
[various] Add rel="noopener noreferrer" to all target="_blank" links
[friendica-addons.git] / securemail / securemail.php
index e25e96695c05b5cc6864aa5a23a6a5fd4869d9b2..115ba47290e2cb2f6d7ed948edb726b33220ce8b 100644 (file)
@@ -6,12 +6,13 @@
  * Author: Fabio Comuni <http://kirgroup.com/profile/fabrixxm>
  */
 
-use Friendica\Addon\securemail\SecureTestEMail;
+use Friendica\Addon\securemail\SecureTestEmail;
 use Friendica\App;
 use Friendica\Core\Hook;
 use Friendica\Core\Logger;
 use Friendica\Core\Renderer;
 use Friendica\DI;
+use Friendica\Object\EMail\IEmail;
 
 require_once __DIR__ . '/vendor/autoload.php';
 
@@ -89,7 +90,7 @@ function securemail_settings_post(App &$a, array &$b)
 
                if ($_POST['securemail-submit'] == DI::l10n()->t('Save and send test')) {
 
-                       $res = DI::emailer()->send(new SecureTestEMail(DI::app(), DI::config(), DI::pConfig(), DI::baseUrl()));
+                       $res = DI::emailer()->send(new SecureTestEmail(DI::app(), DI::config(), DI::pConfig(), DI::baseUrl()));
 
                        // revert to saved value
                        DI::pConfig()->set(local_user(), 'securemail', 'enable', $enable);
@@ -109,20 +110,21 @@ function securemail_settings_post(App &$a, array &$b)
  * @link  https://github.com/friendica/friendica/blob/develop/doc/Addons.md#emailer_send_prepare 'emailer_send_prepare' hook
  *
  * @param App   $a App instance
- * @param array $b hook data
+ * @param IEmail $email Email
  *
  * @see   App
  */
-function securemail_emailer_send_prepare(App &$a, array &$b)
+function securemail_emailer_send_prepare(App &$a, IEmail &$email)
 {
-       if (empty($b['uid'])) {
+       if (empty($email->getRecipientUid())) {
                return;
        }
 
-       $uid = $b['uid'];
+       $uid = $email->getRecipientUid();
 
        $enable_checked = DI::pConfig()->get($uid, 'securemail', 'enable');
        if (!$enable_checked) {
+               DI::logger()->debug('No securemail enabled.');
                return;
        }
 
@@ -134,18 +136,23 @@ function securemail_emailer_send_prepare(App &$a, array &$b)
 
        $key = OpenPGP_Message::parse($public_key);
 
-       $data = new OpenPGP_LiteralDataPacket($b['textVersion'], [
+       $data = new OpenPGP_LiteralDataPacket($email->getMessage(true), [
                'format' => 'u',
                'filename' => 'encrypted.gpg'
        ]);
-       $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message([$data]));
-       $armored_encrypted = wordwrap(
-               OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'),
-               64,
-               "\n",
-               true
-       );
-
-       $b['textVersion'] = $armored_encrypted;
-       $b['htmlVersion'] = null;
+
+       try {
+               $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message([$data]));
+               $armored_encrypted = wordwrap(
+                       OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'),
+                       64,
+                       "\n",
+                       true
+               );
+
+               $email = $email->withMessage($armored_encrypted, null);
+
+       } catch (Exception $e) {
+               DI::logger()->warning('Encryption failed.', ['email' => $email, 'exception' => $e]);
+       }
 }