Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/sualko/cloud_piwik.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsualko <klaus@jsxc.org>2018-10-28 16:26:08 +0300
committersualko <klaus@jsxc.org>2018-10-28 16:26:08 +0300
commitbd2b33a8ea76a7f132bde132f95984075717c9e9 (patch)
tree8df2996bf912770fcce6ec87878d054d98001dcf
parente9edc1224ba15589ae5e7ba9717eef57d8dd11c8 (diff)
convert to dynamic tracking script
this saves one request and removes any client-side cache
-rw-r--r--appinfo/Application.php9
-rwxr-xr-xappinfo/app.php25
-rw-r--r--appinfo/routes.php5
-rw-r--r--js/track.js138
-rw-r--r--lib/Controller/JavaScriptController.php51
5 files changed, 135 insertions, 93 deletions
diff --git a/appinfo/Application.php b/appinfo/Application.php
index fed6193..c483b35 100644
--- a/appinfo/Application.php
+++ b/appinfo/Application.php
@@ -3,6 +3,7 @@ namespace OCA\Piwik\AppInfo;
use OCA\Piwik\Config;
use OCA\Piwik\Controller\SettingsController;
+use OCA\Piwik\Controller\JavaScriptController;
use OCA\Piwik\Migration\Settings as SettingsMigration;
use OCP\AppFramework\App;
use OCP\IContainer;
@@ -34,6 +35,14 @@ class Application extends App
);
});
+ $container->registerService('JavaScriptController', function (IContainer $c) {
+ return new JavaScriptController(
+ $c->query('AppName'),
+ $c->query('Request'),
+ $c->query('OCA\Piwik\Config')
+ );
+ });
+
/**
* Migrations
*/
diff --git a/appinfo/app.php b/appinfo/app.php
index 8ded64a..44b441c 100755
--- a/appinfo/app.php
+++ b/appinfo/app.php
@@ -2,20 +2,21 @@
$url = \OC::$server->getConfig()->getAppValue('piwik', 'url');
if (!empty($url)) {
- OCP\Util::addScript('piwik', 'track');
+ \OCP\Util::addHeader(
+ 'script',
+ [
+ 'src' => \OC::$server->getURLGenerator()->linkToRoute('piwik.JavaScript.tracking'),
+ 'nonce' => \OC::$server->getContentSecurityPolicyNonceManager()->getNonce(),
+ ], ''
+ );
- if (class_exists('\\OCP\\AppFramework\\Http\\ContentSecurityPolicy')) {
- $url = parse_url($url, PHP_URL_HOST);
+ $url = parse_url($url, PHP_URL_HOST);
+ $policy = new OCP\AppFramework\Http\ContentSecurityPolicy();
- $policy = new OCP\AppFramework\Http\ContentSecurityPolicy();
- $policy->addAllowedScriptDomain('\'self\' ');
- $policy->addAllowedImageDomain('\'self\' ');
-
- if ($url !== false && array_key_exists('HTTP_HOST', $_SERVER)
- && $_SERVER['HTTP_HOST'] !== $url && !empty($url)) {
- $policy->addAllowedScriptDomain($url);
- $policy->addAllowedImageDomain($url);
- }
+ if ($url !== false && array_key_exists('HTTP_HOST', $_SERVER)
+ && $_SERVER['HTTP_HOST'] !== $url && !empty($url)) {
+ $policy->addAllowedScriptDomain($url);
+ $policy->addAllowedImageDomain($url);
\OC::$server->getContentSecurityPolicyManager()->addDefaultPolicy($policy);
}
diff --git a/appinfo/routes.php b/appinfo/routes.php
index 6a4e821..2fd1fa6 100644
--- a/appinfo/routes.php
+++ b/appinfo/routes.php
@@ -3,7 +3,8 @@
return [
'routes' => [
['name' => 'settings#index', 'url' => '/settings', 'verb' => 'GET'],
- ['name' => 'settings#update', 'url' => '/settings/{key}', 'verb' => 'PUT']
+ ['name' => 'settings#update', 'url' => '/settings/{key}', 'verb' => 'PUT'],
+ ['name' => 'JavaScript#tracking', 'url' => '/js/tracking.js', 'verb' => 'GET']
],
];
-?>
+?> \ No newline at end of file
diff --git a/js/track.js b/js/track.js
index ca9bca0..0b35beb 100644
--- a/js/track.js
+++ b/js/track.js
@@ -5,104 +5,84 @@ var _paq = _paq || [];
(function() {
"use strict";
- var piwik;
+ var options;
- if (typeof localStorage !== 'undefined') {
- var piwikString = localStorage.getItem('piwik');
+ try {
+ options = JSON.parse('%OPTIONS%');
+ } catch (err) {}
- try {
- piwik = JSON.parse(piwikString);
- } catch (err) {}
+ if (!options || !options.url || !options.siteId) {
+ return;
}
- if (piwik && (piwik.validUntil || 0) > (new Date()).getTime() && !oc_debug) {
- // use cached options
- track(piwik);
- } else {
- // load options
- $.ajax({
- method: 'GET',
- url: OC.generateUrl('apps/piwik/settings'),
- })
- .done(function(response) {
- var data = response ? response.data : {};
-
- if (data.siteId && data.url) {
- data.validUntil = (new Date()).getTime() + (data.validity * 1000);
-
- localStorage.setItem('piwik', JSON.stringify(data));
-
- track(data);
- }
- });
+ if (options.url[options.url.length - 1] !== '/') {
+ options.url += '/';
}
- function track(options) {
- var app = null;
- var path = window.location.pathname;
- var pathparts = path.match(/(?:index\.php\/)?apps\/([a-z0-9]+)\/?/i) || path.match(/(?:index\.php\/)?([a-z0-9]+)(\/([a-z0-9]+))?/i) || [];
+ var app = null;
+ var path = window.location.pathname;
+ var pathParts = path.match(/(?:index\.php\/)?apps\/([a-z0-9]+)\/?/i) || path.match(/(?:index\.php\/)?([a-z0-9]+)(\/([a-z0-9]+))?/i) || [];
- if (pathparts.length >= 2) {
- app = pathparts[1];
+ if (pathParts.length >= 2) {
+ app = pathParts[1];
- if (app === 's') {
- // rewrite app name
- app = 'share';
+ if (app === 's') {
+ // rewrite app name
+ app = 'share';
- var sharevalue = $('input[name="filename"]').val();
+ var shareValue = $('input[name="filename"]').val();
- if (sharevalue) {
- sharevalue = pathparts[3] + ' (' + sharevalue + ')';
+ if (shareValue) {
+ shareValue = pathParts[3] + ' (' + shareValue + ')';
- // save share id + share name in slot 3
- _paq.push(['setCustomVariable', '3', 'ShareNodes', sharevalue, 'page']);
- } else {
- sharevalue = pathparts[3];
- }
-
- // save share id in slot 2
- _paq.push(['setCustomVariable', '2', 'Shares', pathparts[3], 'page']);
+ // save share id + share name in slot 3
+ _paq.push(['setCustomVariable', '3', 'ShareNodes', shareValue, 'page']);
+ } else {
+ shareValue = pathParts[3];
}
- // save app name in slot 1
- _paq.push(['setCustomVariable', '1', 'App', app, 'page']);
- }
-
- if (OC && OC.currentUser) {
- // set user id
- _paq.push(['setUserId', OC.currentUser]);
+ // save share id in slot 2
+ _paq.push(['setCustomVariable', '2', 'Shares', pathParts[3], 'page']);
}
- if (options.trackDir === 'on' || options.trackDir === true) {
- // track file browsing
+ // save app name in slot 1
+ _paq.push(['setCustomVariable', '1', 'App', app, 'page']);
+ }
- $('#app-content').delegate('>div', 'afterChangeDirectory', function() {
- // update title and url for next page view
- _paq.push(['setDocumentTitle', document.title]);
- _paq.push(['setCustomUrl', window.location.href]);
- _paq.push(['trackPageView']);
- });
- }
+ if (OC && OC.currentUser) {
+ // set user id
+ _paq.push(['setUserId', OC.currentUser]);
+ }
- // set piwik options
- _paq.push(['setTrackerUrl', options.url + 'piwik.php']);
- _paq.push(['setSiteId', options.siteId]);
+ if (options.trackDir === 'on' || options.trackDir === true) {
+ // track file browsing
- if (app !== 'files' || options.trackDir !== 'on') {
- // track page view
+ $('#app-content').delegate('>div', 'afterChangeDirectory', function() {
+ // update title and url for next page view
+ _paq.push(['setDocumentTitle', document.title]);
+ _paq.push(['setCustomUrl', window.location.href]);
_paq.push(['trackPageView']);
- }
+ });
+ }
- if (typeof Piwik === 'undefined') {
- // load piwik library
- var d = document,
- g = d.createElement('script'),
- s = d.getElementsByTagName('script')[0];
- g.type = 'text/javascript';
- g.async = true;
- g.defer = true;
- g.src = options.url + 'piwik.js';
- s.parentNode.insertBefore(g, s);
- }
+ // set piwik options
+ _paq.push(['setTrackerUrl', options.url + 'piwik.php']);
+ _paq.push(['setSiteId', options.siteId]);
+
+ if (app !== 'files' || options.trackDir !== 'on') {
+ // track page view
+ _paq.push(['trackPageView']);
+ }
+
+ if (typeof Piwik === 'undefined') {
+ // load piwik library
+ var d = document,
+ g = d.createElement('script'),
+ s = d.getElementsByTagName('script')[0];
+ g.type = 'text/javascript';
+ g.async = true;
+ g.defer = true;
+ g.src = options.url + 'piwik.js';
+ s.parentNode.insertBefore(g, s);
}
}());
diff --git a/lib/Controller/JavaScriptController.php b/lib/Controller/JavaScriptController.php
new file mode 100644
index 0000000..eeeab9c
--- /dev/null
+++ b/lib/Controller/JavaScriptController.php
@@ -0,0 +1,51 @@
+<?php
+namespace OCA\Piwik\Controller;
+
+use OCP\AppFramework\Controller;
+use OCP\AppFramework\Http\DataDownloadResponse;
+use OCP\AppFramework\Http\Response;
+use OCP\IConfig;
+use OCP\IRequest;
+use OCA\Piwik\Config;
+
+class JavaScriptController extends Controller
+{
+ /** @var \OCP\IConfig */
+ protected $config;
+
+ /**
+ * constructor of the controller
+ *
+ * @param string $appName
+ * @param IRequest $request
+ * @param IConfig $config
+ */
+ public function __construct($appName,
+ IRequest $request,
+ Config $config) {
+ parent::__construct($appName, $request);
+ $this->config = $config;
+ }
+
+ /**
+ * @NoAdminRequired
+ * @NoCSRFRequired
+ * @PublicPage
+ *
+ * @return Response
+ */
+ public function tracking()
+ {
+ $options = [
+ 'url' => $this->config->getAppValue('url'),
+ 'siteId' => $this->config->getAppValue('siteId'),
+ 'trackDir' => $this->config->getBooleanAppValue('trackDir'),
+ ];
+
+ $script = "var cloudPiwikOptions = '".json_encode($options)."';";
+ $script = file_get_contents(__DIR__ . '/../../js/track.js');
+ $script = str_replace('%OPTIONS%', json_encode($options), $script);
+
+ return new DataDownloadResponse($script, 'script', 'text/javascript');
+ }
+}