xref: /plugin/statistics/action.php (revision e25286da54587c65a30049fc7e655d674bf6a14e)
1<?php
2/**
3 *
4 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
5 * @author     Andreas Gohr <gohr@cosmocode.de>
6 */
7
8// must be run within Dokuwiki
9if(!defined('DOKU_INC')) die();
10
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'action.php');
13
14class action_plugin_statistics extends DokuWiki_Action_Plugin {
15
16    /**
17     * return some info
18     */
19    function getInfo(){
20        return confToHash(dirname(__FILE__).'/info.txt');
21    }
22
23    /**
24     * register the eventhandlers and initialize some options
25     */
26    function register(&$controller){
27
28        $controller->register_hook('TPL_METAHEADER_OUTPUT',
29                                   'BEFORE',
30                                   $this,
31                                   'handle_metaheaders',
32                                   array());
33    }
34
35    /**
36     * Extend the meta headers
37     */
38    function handle_metaheaders(&$event, $param){
39        global $ACT;
40        global $ID;
41        if($ACT != 'show') return; //only log page views for now
42
43        $base = DOKU_BASE.'lib/plugins/statistics/log.php?rnd='.time();
44        $view = $base.'&p='.rawurlencode($ID);
45
46        // we create an image object and load the logger here, we also attach the same logger
47        // to all external links - we won't use the JS dispatcher because we only want all this
48        // on the 'show' action
49        $data = "var plugin_statistics_image = new Image();
50                 var plugin_statistics_uid   = DokuCookie.getValue('plgstats');
51                 if(!plugin_statistics_uid){
52                    plugin_statistics_uid = new Date().getTime()+'-'+Math.floor(Math.random()*32000);
53                    DokuCookie.setValue('plgstats',plugin_statistics_uid);
54                    if(!DokuCookie.getCookie(DokuCookie.name)){
55                        plugin_statistics_uid = '';
56                    }
57                 }
58                 plugin_statistics_image.src = '$view&r='+encodeURIComponent(document.referrer)+
59                                                    '&sx='+screen.width+
60                                                    '&sy='+screen.height+
61                                                    '&vx='+window.innerWidth+
62                                                    '&vy='+window.innerHeight+
63                                                    '&uid='+plugin_statistics_uid+
64                                                    '&js=1';
65                addInitEvent(function(){
66                    var links = getElementsByClass('urlextern',null,'a');
67                    for(var i=0; i<links.length; i++){
68                        addEvent(links[i],'click',function(){
69                            var img = new Image();
70                            img.src = '$base&ol='+encodeURIComponent(this.href);
71                            return true;
72                        });
73                    }
74                });
75                ";
76        $event->data['script'][] = array( 'type'=>'text/javascript', 'charset'=>'utf-8', '_data'=>$data);
77    }
78
79    /**
80     * @fixme call this in the webbug call
81     */
82    function putpixel(){
83        global $ID;
84        $url = DOKU_BASE.'lib/plugins/statistics/log.php?p='.rawurlencode($ID).
85               '&amp;r='.rawurlencode($_SERVER['HTTP_REFERER']).'&rnd='.time();
86
87        echo '<noscript><img src="'.$url.'" width="1" height="1" /></noscript>';
88    }
89}
90
91