From 9b0f8936192bd2ea909a753a956b6884d1f475d5 Mon Sep 17 00:00:00 2001 From: Yuriy Bakhtin Date: Fri, 15 Sep 2023 16:19:10 +0200 Subject: [PATCH] Wrap URLs in email messages to "go" app --- protected/humhub/components/mail/Message.php | 27 +++++++- protected/humhub/config/common.php | 1 + protected/humhub/libs/GoAppService.php | 70 ++++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 protected/humhub/libs/GoAppService.php diff --git a/protected/humhub/components/mail/Message.php b/protected/humhub/components/mail/Message.php index d1d5297b5a..893e3eabea 100644 --- a/protected/humhub/components/mail/Message.php +++ b/protected/humhub/components/mail/Message.php @@ -8,12 +8,37 @@ namespace humhub\components\mail; +use humhub\libs\GoAppService; +use yii\symfonymailer\Message as BaseMessage; + /** * Message * * @since 1.2 * @author Luke */ -class Message extends \yii\symfonymailer\Message +class Message extends BaseMessage { + private ?GoAppService $goAppService = null; + + /** + * @inheritdoc + */ + public function setHtmlBody($html): BaseMessage + { + return BaseMessage::setHtmlBody($this->getGoAppService()->processLinks($html)); + } + + /** + * @inheritdoc + */ + public function setTextBody($text): BaseMessage + { + return BaseMessage::setTextBody($this->getGoAppService()->processUrls($text)); + } + + private function getGoAppService(): GoAppService + { + return $this->goAppService ?? new GoAppService(); + } } diff --git a/protected/humhub/config/common.php b/protected/humhub/config/common.php index 41be2ba7d1..725e3a18ab 100644 --- a/protected/humhub/config/common.php +++ b/protected/humhub/config/common.php @@ -253,6 +253,7 @@ $config = [ // Marketplace / New Version Check 'apiEnabled' => true, 'apiUrl' => 'https://api.humhub.com', + 'goUrl' => 'https://go.humhub.com', ], 'search' => [ 'zendLucenceDataDir' => '@runtime/searchdb', diff --git a/protected/humhub/libs/GoAppService.php b/protected/humhub/libs/GoAppService.php new file mode 100644 index 0000000000..65b8446323 --- /dev/null +++ b/protected/humhub/libs/GoAppService.php @@ -0,0 +1,70 @@ +appUrl = Yii::$app->params['humhub']['goUrl'] ?? null; + + $baseUrl = Yii::$app->settings->get('baseUrl'); + $this->sitePattern = is_string($baseUrl) ? preg_quote($baseUrl, '#') : null; + + $installationId = Yii::$app->getModule('admin')->settings->get('installationId'); + $this->hid = is_string($installationId) && strlen($installationId) > 5 + ? substr($installationId, 0, 3) . substr($installationId, -3) + : null; + } + + public function isEnabled(): bool + { + return is_string($this->appUrl) && is_string($this->hid) && is_string($this->sitePattern); + } + + public function processUrls(string $text): ?string + { + return $this->isEnabled() + ? preg_replace_callback('#' . $this->sitePattern . '[^\s]+#i', [$this, 'callbackReplaceUrl'], $text) + : $text; + } + + private function callbackReplaceUrl($matches) + { + return $this->buildUrl($matches[0]); + } + + public function processLinks($text): ?string + { + return $this->isEnabled() + ? preg_replace_callback('#(sitePattern . '.+?)(".*?>)#i', [$this, 'callbackReplaceLink'], $text) + : $text; + } + + private function callbackReplaceLink($matches) + { + return $matches[1] . $this->buildUrl($matches[2]) . $matches[3]; + } + + public function buildUrl(string $url): string + { + return $this->appUrl . '?url=' . urlencode($url) . '&hid=' . $this->hid; + } +}