-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from humhub/enh/go-app-service
Wrap URLs in email messages to "go" app
- Loading branch information
Showing
7 changed files
with
161 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/** | ||
* @link https://www.humhub.org/ | ||
* @copyright Copyright (c) HumHub GmbH & Co. KG | ||
* @license https://www.humhub.com/licences | ||
*/ | ||
|
||
namespace humhub\modules\fcmPush\components; | ||
|
||
use humhub\components\mail\Message; | ||
use humhub\modules\fcmPush\Module; | ||
use Yii; | ||
|
||
/** | ||
* Message | ||
* | ||
* @author Luke | ||
*/ | ||
class MailerMessage extends Message | ||
{ | ||
protected ?Module $module = null; | ||
|
||
public function init() | ||
{ | ||
parent::init(); | ||
$this->module = Yii::$app->getModule('fcm-push'); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function setHtmlBody($html): Message | ||
{ | ||
return Message::setHtmlBody($this->module->getGoService()->processLinks($html)); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function setTextBody($text): Message | ||
{ | ||
return Message::setTextBody($this->module->getGoService()->processUrls($text)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
/** | ||
* @link https://www.humhub.org/ | ||
* @copyright Copyright (c) HumHub GmbH & Co. KG | ||
* @license https://www.humhub.com/licences | ||
*/ | ||
|
||
namespace humhub\modules\fcmPush\services; | ||
|
||
use humhub\modules\fcmPush\models\ConfigureForm; | ||
use Yii; | ||
|
||
/** | ||
* Service to handle URLs to "go" app in order to open then in mobile app | ||
* | ||
* @author Luke | ||
*/ | ||
class GoService | ||
{ | ||
private ?string $appUrl; | ||
private ?string $sitePattern; | ||
private ?string $hid = null; | ||
private bool $isEnabled = false; | ||
|
||
public function __construct(string $appUrl) | ||
{ | ||
$this->appUrl = $appUrl; | ||
|
||
$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; | ||
|
||
$this->isEnabled = (bool) ConfigureForm::getInstance()->enableEmailGoService; | ||
} | ||
|
||
public function isConfigured(): bool | ||
{ | ||
return $this->isEnabled && | ||
is_string($this->appUrl) && | ||
is_string($this->hid) && | ||
is_string($this->sitePattern); | ||
} | ||
|
||
public function processUrls(string $text): ?string | ||
{ | ||
return $this->isConfigured() | ||
? 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->isConfigured() | ||
? preg_replace_callback('#(<a.+?href=")(' . $this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters