1<?php
2/**
3 * DokuWiki plugin for Piwik
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Heikki Hokkanen <hoxu@users.sf.net>
7 */
8require_once(DOKU_INC .'inc/auth.php');
9
10// Old plugin version instructed people to modify tpl/default/main.php and call
11// this function
12function piwik_code() {
13	//msg('Piwik plugin: old version required template modification, you should remove piwik call from tpl/default/main.php now.');
14}
15
16/**
17 * Prints the snippet needed for Piwik.
18 */
19function piwik_code_new()
20{
21	global $conf;
22
23	if (isset($conf['plugin']['piwik']['piwik_idsite'])) {
24		// Config does not contain keys if they are default;
25		// so check whether they are set & to non-default value
26
27		// default 0, so check if it's not set or 0
28		if (!isset($conf['plugin']['piwik']['count_admins']) || $conf['plugin']['piwik']['count_admins'] == 0) {
29			if (isset($_SERVER['REMOTE_USER']) && auth_isadmin()) { return; }
30		}
31
32		// default 1, so check if it's set and 0
33		if (isset($conf['plugin']['piwik']['count_users']) && $conf['plugin']['piwik']['count_users'] == 0) {
34			if (isset($_SERVER['REMOTE_USER'])) { return; }
35		}
36
37		$idsite = $conf['plugin']['piwik']['piwik_idsite'];
38		$piwik_url = $conf['plugin']['piwik']['piwik_url'];
39		$piwik_url = str_replace('http://', '', $piwik_url); // Remove 'http://' if any
40		$piwik_url = rtrim($piwik_url, '/') . '/'; // Make sure the URL has '/' in the end
41
42		ptln(
43'<script type="text/javascript">
44var pkBaseURL = (("https:" == document.location.protocol) ? "https://'. $piwik_url .'" : "http://'. $piwik_url .'");
45document.write(unescape("%3Cscript src=\'" + pkBaseURL + "piwik.js\' type=\'text/javascript\'%3E%3C/script%3E"));
46</script><script type="text/javascript">
47try {
48var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", '. $idsite .');
49piwikTracker.trackPageView();
50piwikTracker.enableLinkTracking();
51} catch( err ) {}
52</script><noscript><p><img src="http://'. $piwik_url .'piwik.php?idsite='. $idsite .'" style="border:0" alt=""/></p></noscript>
53');
54	} else {
55		// Show configuration tip for admin
56		if (isset($_SERVER['REMOTE_USER']) && auth_isadmin()) {
57			msg('Please configure the piwik plugin');
58		}
59	}
60}
61
62