]> git.mxchange.org Git - friendica-addons.git/blobdiff - securemail/securemail.php
dwpost addon added HU translation THX Balázs Úr
[friendica-addons.git] / securemail / securemail.php
index 5662714bc2f98f0ef1cf25baf3c2930c75cda452..fb801a4ce84bc8f03e88aab4d7fe438908631f12 100644 (file)
@@ -6,14 +6,13 @@
  * Author: Fabio Comuni <http://kirgroup.com/profile/fabrixxm>
  */
 
+use Friendica\Addon\securemail\SecureTestEmail;
 use Friendica\App;
-use Friendica\Core\Config;
 use Friendica\Core\Hook;
-use Friendica\Core\L10n;
 use Friendica\Core\Logger;
-use Friendica\Core\PConfig;
 use Friendica\Core\Renderer;
-use Friendica\Util\Emailer;
+use Friendica\DI;
+use Friendica\Object\EMail\IEmail;
 
 require_once __DIR__ . '/vendor/autoload.php';
 
@@ -27,16 +26,6 @@ function securemail_install()
        Logger::log('installed securemail');
 }
 
-function securemail_uninstall()
-{
-       Hook::unregister('addon_settings', 'addon/securemail/securemail.php', 'securemail_settings');
-       Hook::unregister('addon_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
-
-       Hook::unregister('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
-
-       Logger::log('removed securemail');
-}
-
 /**
  * @brief Build user settings form
  *
@@ -53,17 +42,17 @@ function securemail_settings(App &$a, &$s)
                return;
        }
 
-       $enable = intval(PConfig::get(local_user(), 'securemail', 'enable'));
-       $publickey = PConfig::get(local_user(), 'securemail', 'pkey');
+       $enable = intval(DI::pConfig()->get(local_user(), 'securemail', 'enable'));
+       $publickey = DI::pConfig()->get(local_user(), 'securemail', 'pkey');
 
        $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/securemail/');
 
        $s .= Renderer::replaceMacros($t, [
-               '$title' => L10n::t('"Secure Mail" Settings'),
-               '$submit' => L10n::t('Save Settings'),
-               '$test' => L10n::t('Save and send test'), //NOTE: update also in 'post'
-               '$enable' => ['securemail-enable', L10n::t('Enable Secure Mail'), $enable, ''],
-               '$publickey' => ['securemail-pkey', L10n::t('Public key'), $publickey, L10n::t('Your public PGP key, ascii armored format'), 'rows="10"']
+               '$title' => DI::l10n()->t('"Secure Mail" Settings'),
+               '$submit' => DI::l10n()->t('Save Settings'),
+               '$test' => DI::l10n()->t('Save and send test'), //NOTE: update also in 'post'
+               '$enable' => ['securemail-enable', DI::l10n()->t('Enable Secure Mail'), $enable, ''],
+               '$publickey' => ['securemail-pkey', DI::l10n()->t('Public key'), $publickey, DI::l10n()->t('Your public PGP key, ascii armored format'), 'rows="10"']
        ]);
 }
 
@@ -84,49 +73,21 @@ function securemail_settings_post(App &$a, array &$b)
        }
 
        if ($_POST['securemail-submit']) {
-               PConfig::set(local_user(), 'securemail', 'pkey', trim($_POST['securemail-pkey']));
+               DI::pConfig()->set(local_user(), 'securemail', 'pkey', trim($_POST['securemail-pkey']));
                $enable = (!empty($_POST['securemail-enable']) ? 1 : 0);
-               PConfig::set(local_user(), 'securemail', 'enable', $enable);
-               info(L10n::t('Secure Mail Settings saved.') . EOL);
+               DI::pConfig()->set(local_user(), 'securemail', 'enable', $enable);
 
-               if ($_POST['securemail-submit'] == L10n::t('Save and send test')) {
-                       $sitename = Config::get('config', 'sitename');
-
-                       $hostname = $a->getHostName();
-                       if (strpos($hostname, ':')) {
-                               $hostname = substr($hostname, 0, strpos($hostname, ':'));
-                       }
-
-                       $sender_email = Config::get('config', 'sender_email');
-                       if (empty($sender_email)) {
-                               $sender_email = 'noreply@' . $hostname;
-                       }
+               if ($_POST['securemail-submit'] == DI::l10n()->t('Save and send test')) {
 
-                       $subject = 'Friendica - Secure Mail - Test';
-                       $message = 'This is a test message from your Friendica Secure Mail addon.';
-
-                       $params = [
-                               'uid' => local_user(),
-                               'fromName' => $sitename,
-                               'fromEmail' => $sender_email,
-                               'toEmail' => $a->user['email'],
-                               'messageSubject' => $subject,
-                               'htmlVersion' => "<p>{$message}</p>",
-                               'textVersion' => $message,
-                       ];
-
-                       // enable addon for test
-                       PConfig::set(local_user(), 'securemail', 'enable', 1);
-
-                       $res = Emailer::send($params);
+                       $res = DI::emailer()->send(new SecureTestEmail(DI::app(), DI::config(), DI::pConfig(), DI::baseUrl()));
 
                        // revert to saved value
-                       PConfig::set(local_user(), 'securemail', 'enable', $enable);
+                       DI::pConfig()->set(local_user(), 'securemail', 'enable', $enable);
 
                        if ($res) {
-                               info(L10n::t('Test email sent') . EOL);
+                               info(DI::l10n()->t('Test email sent') . EOL);
                        } else {
-                               notice(L10n::t('There was an error sending the test email') . EOL);
+                               notice(DI::l10n()->t('There was an error sending the test email') . EOL);
                        }
                }
        }
@@ -138,24 +99,25 @@ 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 = PConfig::get($uid, 'securemail', 'enable');
+       $enable_checked = DI::pConfig()->get($uid, 'securemail', 'enable');
        if (!$enable_checked) {
+               DI::logger()->debug('No securemail enabled.');
                return;
        }
 
-       $public_key_ascii = PConfig::get($uid, 'securemail', 'pkey');
+       $public_key_ascii = DI::pConfig()->get($uid, 'securemail', 'pkey');
 
        preg_match('/-----BEGIN ([A-Za-z ]+)-----/', $public_key_ascii, $matches);
        $marker = empty($matches[1]) ? 'MESSAGE' : $matches[1];
@@ -163,18 +125,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]);
+       }
 }