From 6a634c4e7e90a220091c965f6d1748a128db1ea8 Mon Sep 17 00:00:00 2001 From: Mauro Date: Thu, 25 Apr 2024 09:40:53 +0200 Subject: [PATCH] added mobile clients check in BeforeTemplateRenderedListener --- README.md | 9 +++ appinfo/info.xml | 2 +- .../BeforeTemplateRenderedListener.php | 63 +++++++++++++------ 3 files changed, 53 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index d2c06ac..d081e20 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,15 @@ Add below in server config 'trusted_font_urls'=>array(0 => 'https://ebs10.telekom.de/opt-in/',), 'trusted_image_urls'=>array(0 => 'https://pix.telekom.de/',1=>'http://fbc.wcfbc.net/',) +```php +// config/config.php + ..., + // In order to deactivate the consent layer for the mobile clients we have to configure the identifiable user agents of those clients + 'nmc_marketing.mobile_user_agents' => [ + '/^Mozilla\/5\.0 \(Android\) Nextcloud\-android\/(?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)).*$/', + '/^Mozilla\/5\.0 \(iOS\) Nextcloud\-iOS\/(?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)).*$/', + ] +``` ### App Repository https://github.com/nextmcloud/nmc_marketing/tree/nmcfeat/master diff --git a/appinfo/info.xml b/appinfo/info.xml index c8dfe59..a3aa664 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -13,7 +13,7 @@ customization https://github.com/nextmcloud/nmc_marketing/issues - + diff --git a/lib/Listener/BeforeTemplateRenderedListener.php b/lib/Listener/BeforeTemplateRenderedListener.php index cc466bc..56af226 100644 --- a/lib/Listener/BeforeTemplateRenderedListener.php +++ b/lib/Listener/BeforeTemplateRenderedListener.php @@ -14,39 +14,62 @@ use OC\Security\CSP\ContentSecurityPolicyNonceManager; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; - use OCP\IConfig; +use OCP\IRequest; class BeforeTemplateRenderedListener implements IEventListener { private IConfig $config; + private IRequest $request; private ContentSecurityPolicyNonceManager $nonceManager; + private array $mobileUserAgents; public function __construct( - IConfig $config, - ContentSecurityPolicyNonceManager $nonceManager + IConfig $config, + IRequest $request, + ContentSecurityPolicyNonceManager $nonceManager ) { - $this->config = $config; - $this->nonceManager = $nonceManager; + $this->config = $config; + $this->request = $request; + $this->nonceManager = $nonceManager; + $this->mobileUserAgents = $config->getSystemValue('nmc_marketing.mobile_user_agents', [ + '/^Mozilla\/5\.0 \(Android\) Nextcloud\-android\/(?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)).*$/', + '/^Mozilla\/5\.0 \(iOS\) Nextcloud\-iOS\/(?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)).*$/', + ]); } public function handle(Event $event): void { $response = $event->getResponse(); + $userAgent = $this->request->getHeader('USER_AGENT'); + + // no consent layer for mobile clients + if (!$this->isMobileUserAgent($userAgent)) { + $marketing_config = $this->config->getSystemValue("nmc_marketing"); + $utagUrl = $marketing_config['url']; + + // the marketing tooling is controlled by CSP, so save nonce is mandatory + $nonce = $this->nonceManager->getNonce(); + // we want to invalidate script url remotely with cachebuster + $cacheBusterVal = $this->config->getAppValue('theming', 'cachebuster', '0'); + + // add utag from external CDN + \OCP\Util::addHeader("script", [ 'nonce' => $nonce, 'src' => $utagUrl . '?nmcv=' . $cacheBusterVal], ''); // the empty text is needed to generate HTML5 valid tags + + // add marketing tracking magic + \OCP\Util::addScript("nmc_marketing", "consent"); + } + } - $marketing_config = $this->config->getSystemValue("nmc_marketing"); - $utagUrl = $marketing_config['url']; - // the marketing tooling is controlled by CSP, so save nonce is mandatory - $nonce = $this->nonceManager->getNonce(); - // we want to invalidate script url remotely with cachebuster - $cacheBusterVal = $this->config->getAppValue('theming', 'cachebuster', '0'); - - // add utag from external CDN - \OCP\Util::addHeader("script", - [ 'nonce' => $nonce, - 'src' => $utagUrl . '?nmcv=' . $cacheBusterVal], - ''); // the empty text is needed to generate HTML5 valid tags - - // add marketing tracking magic - \OCP\Util::addScript("nmc_marketing", "consent"); + /** + * Check whether request comes from a mobile client + */ + private function isMobileUserAgent(string $userAgent): bool { + foreach ($this->mobileUserAgents as $mobileUserAgent) { + + if (preg_match($mobileUserAgent, $userAgent, $matches)) { + return true; + } + } + return false; } }