aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--application/Updater.php17
-rw-r--r--plugins/piwik/piwik.html15
-rw-r--r--plugins/piwik/piwik.php23
-rw-r--r--tests/Updater/UpdaterTest.php41
4 files changed, 80 insertions, 16 deletions
diff --git a/application/Updater.php b/application/Updater.php
index 27cb2f0a..fd7e2073 100644
--- a/application/Updater.php
+++ b/application/Updater.php
@@ -1,6 +1,7 @@
1<?php 1<?php
2use Shaarli\Config\ConfigJson; 2use Shaarli\Config\ConfigJson;
3use Shaarli\Config\ConfigPhp; 3use Shaarli\Config\ConfigPhp;
4use Shaarli\Config\ConfigManager;
4 5
5/** 6/**
6 * Class Updater. 7 * Class Updater.
@@ -363,6 +364,22 @@ class Updater
363 364
364 return true; 365 return true;
365 } 366 }
367
368 /**
369 * Add 'http://' to Piwik URL the setting is set.
370 *
371 * @return bool true if the update is successful, false otherwise.
372 */
373 public function updateMethodPiwikUrl()
374 {
375 if (! $this->conf->exists('plugins.PIWIK_URL') || startsWith($this->conf->get('plugins.PIWIK_URL'), 'http')) {
376 return true;
377 }
378
379 $this->conf->set('plugins.PIWIK_URL', 'http://'. $this->conf->get('plugins.PIWIK_URL'));
380 $this->conf->write($this->isLoggedIn);
381 return true;
382 }
366} 383}
367 384
368/** 385/**
diff --git a/plugins/piwik/piwik.html b/plugins/piwik/piwik.html
new file mode 100644
index 00000000..0881d7c8
--- /dev/null
+++ b/plugins/piwik/piwik.html
@@ -0,0 +1,15 @@
1<!-- Piwik -->
2<script type="text/javascript">
3 var _paq = _paq || [];
4 _paq.push(['trackPageView']);
5 _paq.push(['enableLinkTracking']);
6 (function() {
7 var u="%s/";
8 _paq.push(['setTrackerUrl', u+'piwik.php']);
9 _paq.push(['setSiteId', '%s']);
10 var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
11 g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
12 })();
13</script>
14<noscript><p><img src="%s/piwik.php?idsite=%s" style="border:0;" alt="" /></p></noscript>
15<!-- End Piwik Code --> \ No newline at end of file
diff --git a/plugins/piwik/piwik.php b/plugins/piwik/piwik.php
index 7c44909c..4a2b48a1 100644
--- a/plugins/piwik/piwik.php
+++ b/plugins/piwik/piwik.php
@@ -50,22 +50,13 @@ function hook_piwik_render_footer($data, $conf)
50 } 50 }
51 51
52 // Free elements at the end of the page. 52 // Free elements at the end of the page.
53 $data['endofpage'][] = '<!-- Piwik -->' . 53 $data['endofpage'][] = sprintf(
54'<script type="text/javascript">' . 54 file_get_contents(PluginManager::$PLUGINS_PATH . '/piwik/piwik.html'),
55' var _paq = _paq || [];' . 55 $piwikUrl,
56' _paq.push([\'trackPageView\']);' . 56 $piwikSiteid,
57' _paq.push([\'enableLinkTracking\']);' . 57 $piwikUrl,
58' (function() {' . 58 $piwikSiteid
59' var u="//' . $piwikUrl . '/";' . 59 );
60' _paq.push([\'setTrackerUrl\', u+\'piwik.php\']);' .
61' _paq.push([\'setSiteId\', \'' . $piwikSiteid . '\']);' .
62' var d=document, g=d.createElement(\'script\'), s=d.getElementsByTagName(\'script\')[0];' .
63' g.type=\'text/javascript\'; g.async=true; g.defer=true; g.src=u+\'piwik.js\'; s.parentNode.insertBefore(g,s);' .
64' })();' .
65'</script>' .
66'<noscript><p><img src="//' . $piwikUrl . '/piwik.php?idsite=' . $piwikSiteid . '" style="border:0;" alt="" /></p></noscript>' .
67'<!-- End Piwik Code -->';
68 60
69 return $data; 61 return $data;
70} 62}
71
diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php
index 448405a3..b522d616 100644
--- a/tests/Updater/UpdaterTest.php
+++ b/tests/Updater/UpdaterTest.php
@@ -574,4 +574,45 @@ $GLOBALS[\'privateLinkByDefault\'] = true;';
574 $this->assertTrue($updater->updateMethodEscapeMarkdown()); 574 $this->assertTrue($updater->updateMethodEscapeMarkdown());
575 $this->assertFalse($this->conf->get('security.markdown_escape')); 575 $this->assertFalse($this->conf->get('security.markdown_escape'));
576 } 576 }
577
578 /**
579 * Test updateMethodPiwikUrl with valid data
580 */
581 public function testUpdatePiwikUrlValid()
582 {
583 $sandboxConf = 'sandbox/config';
584 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
585 $this->conf = new ConfigManager($sandboxConf);
586 $url = 'mypiwik.tld';
587 $this->conf->set('plugins.PIWIK_URL', $url);
588 $updater = new Updater([], [], $this->conf, true);
589 $this->assertTrue($updater->updateMethodPiwikUrl());
590 $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL'));
591
592 // reload from file
593 $this->conf = new ConfigManager($sandboxConf);
594 $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL'));
595 }
596
597 /**
598 * Test updateMethodPiwikUrl without setting
599 */
600 public function testUpdatePiwikUrlEmpty()
601 {
602 $updater = new Updater([], [], $this->conf, true);
603 $this->assertTrue($updater->updateMethodPiwikUrl());
604 $this->assertEmpty($this->conf->get('plugins.PIWIK_URL'));
605 }
606
607 /**
608 * Test updateMethodPiwikUrl: valid URL, nothing to do
609 */
610 public function testUpdatePiwikUrlNothingToDo()
611 {
612 $url = 'https://mypiwik.tld';
613 $this->conf->set('plugins.PIWIK_URL', $url);
614 $updater = new Updater([], [], $this->conf, true);
615 $this->assertTrue($updater->updateMethodPiwikUrl());
616 $this->assertEquals($url, $this->conf->get('plugins.PIWIK_URL'));
617 }
577} 618}