Skip to content

Commit

Permalink
added mobile clients check in BeforeTemplateRenderedListener
Browse files Browse the repository at this point in the history
  • Loading branch information
memurats committed Apr 25, 2024
1 parent 719c2a6 commit 6a634c4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 21 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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\/(?<version>(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)).*$/',
'/^Mozilla\/5\.0 \(iOS\) Nextcloud\-iOS\/(?<version>(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)).*$/',
]
```

### App Repository
https://github.com/nextmcloud/nmc_marketing/tree/nmcfeat/master
Expand Down
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<category>customization</category>
<bugs>https://github.com/nextmcloud/nmc_marketing/issues</bugs>
<dependencies>
<nextcloud min-version="15" max-version="27"/>
<nextcloud min-version="15" max-version="28"/>
</dependencies>
</info>

63 changes: 43 additions & 20 deletions lib/Listener/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\/(?<version>(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)).*$/',
'/^Mozilla\/5\.0 \(iOS\) Nextcloud\-iOS\/(?<version>(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;
}
}

0 comments on commit 6a634c4

Please sign in to comment.