From 69ad1c540150c061054d38f3bdafda87a7e1ce16 Mon Sep 17 00:00:00 2001 From: sualko Date: Mon, 29 Oct 2018 09:44:27 +0100 Subject: [PATCH] build v0.5.0 --- Gruntfile.js | 1 - appinfo/info.xml | 2 +- build/appinfo/Application.php | 19 ++- build/appinfo/app.php | 25 +-- build/appinfo/info.xml | 12 +- build/appinfo/routes.php | 5 +- build/js/track.js | 146 ++++++++---------- build/js/track.min.js | 2 - build/js/track.min.js.map | 1 - build/lib/Config.php | 34 ++++ build/lib/Controller/JavaScriptController.php | 52 +++++++ build/lib/Controller/SettingsController.php | 36 +---- build/lib/Settings/Admin.php | 1 + build/templates/settings/admin.php | 9 +- js/track.js | 2 +- package.json | 10 +- 16 files changed, 211 insertions(+), 146 deletions(-) delete mode 100644 build/js/track.min.js delete mode 100644 build/js/track.min.js.map create mode 100644 build/lib/Config.php create mode 100644 build/lib/Controller/JavaScriptController.php diff --git a/Gruntfile.js b/Gruntfile.js index f761e52..00135c4 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -69,7 +69,6 @@ module.exports = function(grunt) { }, files: { 'build/js/settings/admin.min.js': ['js/settings/admin.js'], - 'build/js/track.min.js': ['js/track.js'] } } }, diff --git a/appinfo/info.xml b/appinfo/info.xml index 2147689..7abef05 100755 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -4,7 +4,7 @@ Piwik/Matomo Tracking Track users with Piwik/Matomo Track users with Piwik/Matomo - 0.4.1 + 0.5.0 agpl Klaus Herberth tools diff --git a/build/appinfo/Application.php b/build/appinfo/Application.php index 72591f3..c483b35 100644 --- a/build/appinfo/Application.php +++ b/build/appinfo/Application.php @@ -1,7 +1,9 @@ getContainer(); + $container->registerService('OCA\Piwik\Config', function (IContainer $c) { + return new Config( + $c->query('AppName'), + $c->query('OCP\IConfig') + ); + }); + /** * Controllers */ @@ -22,7 +31,15 @@ public function __construct(array $urlParams = array()) return new SettingsController( $c->query('AppName'), $c->query('Request'), - $c->query('OCP\IConfig') + $c->query('OCA\Piwik\Config') + ); + }); + + $container->registerService('JavaScriptController', function (IContainer $c) { + return new JavaScriptController( + $c->query('AppName'), + $c->query('Request'), + $c->query('OCA\Piwik\Config') ); }); diff --git a/build/appinfo/app.php b/build/appinfo/app.php index 8ded64a..44b441c 100644 --- a/build/appinfo/app.php +++ b/build/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/build/appinfo/info.xml b/build/appinfo/info.xml index f187c5b..7abef05 100644 --- a/build/appinfo/info.xml +++ b/build/appinfo/info.xml @@ -4,7 +4,7 @@ Piwik/Matomo Tracking Track users with Piwik/Matomo Track users with Piwik/Matomo - 0.4.1 + 0.5.0 agpl Klaus Herberth tools @@ -14,16 +14,16 @@ https://github.com/sualko/cloud_piwik.git - + - - OCA\Piwik\Settings\Admin - - OCA\Piwik\Migration\Settings + + + OCA\Piwik\Settings\Admin + diff --git a/build/appinfo/routes.php b/build/appinfo/routes.php index 6a4e821..2fd1fa6 100644 --- a/build/appinfo/routes.php +++ b/build/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/build/js/track.js b/build/js/track.js index df124eb..94a18ca 100644 --- a/build/js/track.js +++ b/build/js/track.js @@ -1,121 +1,101 @@ /*! - * owncloud_piwik v0.4.1 - 2018-05-11 + * cloud_piwik v0.5.0 - 2018-10-29 * * Copyright (c) 2018 Klaus Herberth
* Released under the MIT license * - * Please see https://github.com/sualko/owncloud_piwik + * Please see https://github.com/sualko/cloud_piwik * * @author Klaus Herberth - * @version 0.4.1 + * @version 0.5.0 * @license MIT */ -/* global OC, oc_debug */ +/* global OC */ 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 && options.trackUser) { + // 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/build/js/track.min.js b/build/js/track.min.js deleted file mode 100644 index d5c6223..0000000 --- a/build/js/track.min.js +++ /dev/null @@ -1,2 +0,0 @@ -var _paq=_paq||[];!function(){"use strict";function track(options){var app=null,path=window.location.pathname,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){if("s"===(app=pathparts[1])){app="share";var sharevalue=$('input[name="filename"]').val();sharevalue?(sharevalue=pathparts[3]+" ("+sharevalue+")",_paq.push(["setCustomVariable","3","ShareNodes",sharevalue,"page"])):sharevalue=pathparts[3],_paq.push(["setCustomVariable","2","Shares",pathparts[3],"page"])}_paq.push(["setCustomVariable","1","App",app,"page"])}if(OC&&OC.currentUser&&_paq.push(["setUserId",OC.currentUser]),"on"!==options.trackDir&&!0!==options.trackDir||$("#app-content").delegate(">div","afterChangeDirectory",function(){_paq.push(["setDocumentTitle",document.title]),_paq.push(["setCustomUrl",window.location.href]),_paq.push(["trackPageView"])}),_paq.push(["setTrackerUrl",options.url+"piwik.php"]),_paq.push(["setSiteId",options.siteId]),"files"===app&&"on"===options.trackDir||_paq.push(["trackPageView"]),"undefined"==typeof Piwik){var d=document,g=d.createElement("script"),s=d.getElementsByTagName("script")[0];g.type="text/javascript",g.async=!0,g.defer=!0,g.src=options.url+"piwik.js",s.parentNode.insertBefore(g,s)}}var piwik;if("undefined"!=typeof localStorage){var piwikString=localStorage.getItem("piwik");try{piwik=JSON.parse(piwikString)}catch(err){}}piwik&&(piwik.validUntil||0)>(new Date).getTime()&&!oc_debug?track(piwik):$.ajax({method:"GET",url:OC.generateUrl("apps/piwik/settings")}).done(function(response){var data=response?response.data:{};data.siteId&&data.url&&(data.validUntil=(new Date).getTime()+1e3*data.validity,localStorage.setItem("piwik",JSON.stringify(data)),track(data))})}(); -//# sourceMappingURL=track.min.js.map \ No newline at end of file diff --git a/build/js/track.min.js.map b/build/js/track.min.js.map deleted file mode 100644 index cb32883..0000000 --- a/build/js/track.min.js.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"sources":["../../js/track.js"],"names":["_paq","track","options","app","path","window","location","pathname","pathparts","match","length","sharevalue","$","val","push","OC","currentUser","trackDir","delegate","document","title","href","url","siteId","Piwik","d","g","createElement","s","getElementsByTagName","type","async","defer","src","parentNode","insertBefore","piwik","localStorage","piwikString","getItem","JSON","parse","err","validUntil","Date","getTime","oc_debug","ajax","method","generateUrl","done","response","data","validity","setItem","stringify"],"mappings":"AAEA,IAAIA,KAAOA,UAEV,WACE,aAkCA,SAASC,MAAMC,SACZ,IAAIC,IAAM,KACNC,KAAOC,OAAOC,SAASC,SACvBC,UAAYJ,KAAKK,MAAM,2CAA6CL,KAAKK,MAAM,qDAEnF,GAAID,UAAUE,QAAU,EAAG,CAGxB,GAAY,OAFZP,IAAMK,UAAU,IAEC,CAEdL,IAAM,QAEN,IAAIQ,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6B,KAAK9B,MACrB8B,KAAKT,YAAa,IAAKC,MAAQC,UAA6B,IAAhBO,KAAKC,SAEjDhB,aAAaiB,QAAQ,QAASd,KAAKe,UAAUH,OAE7CnD,MAAMmD,SA9BrB","file":"track.min.js"} \ No newline at end of file diff --git a/build/lib/Config.php b/build/lib/Config.php new file mode 100644 index 0000000..0889d41 --- /dev/null +++ b/build/lib/Config.php @@ -0,0 +1,34 @@ +appName = $appName; + $this->config = $config; + } + + public function getAppValue($key, $default = null) + { + $value = $this->config->getAppValue($this->appName, $key, $default); + return (empty($value)) ? $default : $value; + } + + public function setAppValue($key, $value) + { + return $this->config->setAppValue($this->appName, $key, $value); + } + + public function getBooleanAppValue($key) + { + return $this->validateBoolean($this->getAppValue($key)); + } + + private function validateBoolean($val) + { + return $val === true || $val === 'true'; + } +} diff --git a/build/lib/Controller/JavaScriptController.php b/build/lib/Controller/JavaScriptController.php new file mode 100644 index 0000000..bc15c76 --- /dev/null +++ b/build/lib/Controller/JavaScriptController.php @@ -0,0 +1,52 @@ +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'), + 'trackUser' => $this->config->getBooleanAppValue('trackUser'), + ]; + + $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'); + } +} diff --git a/build/lib/Controller/SettingsController.php b/build/lib/Controller/SettingsController.php index 36745a5..bbfe123 100644 --- a/build/lib/Controller/SettingsController.php +++ b/build/lib/Controller/SettingsController.php @@ -2,8 +2,8 @@ namespace OCA\Piwik\Controller; use OCP\AppFramework\Controller; -use OCP\IConfig; use OCP\IRequest; +use OCA\Piwik\Config; class SettingsController extends Controller { @@ -12,7 +12,7 @@ class SettingsController extends Controller public function __construct( $appName, IRequest $request, - IConfig $config + Config $config ) { parent::__construct($appName, $request); @@ -28,51 +28,29 @@ public function index() return [ 'result' => 'success', 'data' => [ - 'url' => $this->getAppValue('url'), - 'siteId' => $this->getAppValue('siteId'), - 'trackDir' => $this->getBooleanAppValue('trackDir'), - 'validity' => 2*60, + 'url' => $this->config->getAppValue('url'), + 'siteId' => $this->config->getAppValue('siteId'), + 'trackDir' => $this->config->getBooleanAppValue('trackDir') ], ]; } public function update($key) { - if (!in_array($key, ['url', 'siteId', 'trackDir'])) { + if (!in_array($key, ['url', 'siteId', 'trackDir', 'trackUser'])) { return [ 'result' => 'error', 'message' => 'Tried to update not allowed param.', ]; } - $this->setAppValue($key, $this->getTrimParam('value')); + $this->config->setAppValue($key, $this->getTrimParam('value')); return [ 'status' => 'success', ]; } - private function getAppValue($key, $default = null) - { - $value = $this->config->getAppValue($this->appName, $key, $default); - return (empty($value)) ? $default : $value; - } - - private function setAppValue($key, $value) - { - return $this->config->setAppValue($this->appName, $key, $value); - } - - private function getBooleanAppValue($key) - { - return $this->validateBoolean($this->getAppValue($key)); - } - - private function validateBoolean($val) - { - return $val === true || $val === 'true'; - } - private function getTrimParam($key) { return trim($this->request->getParam($key)); diff --git a/build/lib/Settings/Admin.php b/build/lib/Settings/Admin.php index dcb5a6e..2478445 100644 --- a/build/lib/Settings/Admin.php +++ b/build/lib/Settings/Admin.php @@ -30,6 +30,7 @@ public function getForm() 'url' => $this->config->getAppValue('piwik', 'url'), 'siteId' => $this->config->getAppValue('piwik', 'siteId'), 'trackDir' => $this->config->getAppValue('piwik', 'trackDir'), + 'trackUser' => $this->config->getAppValue('piwik', 'trackUser'), ]; return new TemplateResponse('piwik', 'settings/admin', $parameters); diff --git a/build/templates/settings/admin.php b/build/templates/settings/admin.php index edda4b1..ed4ec9e 100644 --- a/build/templates/settings/admin.php +++ b/build/templates/settings/admin.php @@ -6,7 +6,6 @@

Piwik/Matomo Tracking

If you have no Piwik/Matomo instance, go to matomo.org for further instructions.

-

Please take into account that the old values are cached for 2 minutes in your browser.

@@ -21,7 +20,13 @@ + + +
checked="checked" /> - + +
+ checked="checked" /> +
diff --git a/js/track.js b/js/track.js index ccd9f14..62a41af 100644 --- a/js/track.js +++ b/js/track.js @@ -1,4 +1,4 @@ -/* global OC, oc_debug */ +/* global OC */ var _paq = _paq || []; diff --git a/package.json b/package.json index 0ae8c44..0bf9267 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { - "name": "owncloud_piwik", - "version": "0.4.1", + "name": "cloud_piwik", + "version": "0.5.0", "description": "Track owncloud/nextcloud users with piwik", "repository": { "type": "git", - "url": "https://github.com/sualko/owncloud_piwik.git" + "url": "https://github.com/sualko/cloud_piwik.git" }, "keywords": [ "nextcloud", @@ -15,9 +15,9 @@ "author": "Klaus Herberth ", "license": "MIT", "bugs": { - "url": "https://github.com/sualko/owncloud_piwik/issues" + "url": "https://github.com/sualko/cloud_piwik/issues" }, - "homepage": "https://github.com/sualko/owncloud_piwik", + "homepage": "https://github.com/sualko/cloud_piwik", "devDependencies": { "grunt": "^1.0.1", "grunt-banner": "^0.6.0",