xref: /plugin/statistics/admin.php (revision 94171ff351e70c912a4f81373f8fa47a37b105ea)
1<?php
2/**
3 * statistics plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <gohr@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13require_once(DOKU_PLUGIN.'admin.php');
14
15/**
16 * All DokuWiki plugins to extend the admin function
17 * need to inherit from this class
18 */
19class admin_plugin_statistics extends DokuWiki_Admin_Plugin {
20    var $dblink = null;
21
22    /**
23     * return some info
24     */
25    function getInfo(){
26        return confToHash(dirname(__FILE__).'/info.txt');
27    }
28
29    /**
30     * Access for managers allowed
31     */
32    function forAdminOnly(){
33        return false;
34    }
35
36    /**
37     * return sort order for position in admin menu
38     */
39    function getMenuSort() {
40        return 140;
41    }
42
43    /**
44     * handle user request
45     */
46    function handle() {
47    }
48
49    /**
50     * fixme build statistics here
51     */
52    function html() {
53        echo 'fixme';
54    }
55
56
57    /**
58     * Simple function to run a DB query
59     */
60    function runSQL($sql_string) {
61        // connect to DB if needed
62        if(!$this->dblink){
63            $this->dblink = mysql_connect($this->getConf('db_server'),
64                                          $this->getConf('db_user'),
65                                          $this->getConf('db_password'));
66            if(!$this->dblink){
67                msg('DB Error: connection failed',-1);
68                return null;
69            }
70            // set utf-8
71            if(!mysql_db_query($this->getConf('db_database'),'set names utf8',$this->dblink)){
72                msg('DB Error: could not set UTF-8 ('.mysql_error($this->dblink).')',-1);
73                return null;
74            }
75        }
76
77        $result = mysql_db_query($this->conf['db_database'],$sql_string,$this->dblink);
78        if(!$result){
79            msg('DB Error: '.mysql_error($this->dblink),-1);
80            return null;
81        }
82
83        $resultarray = array();
84
85        //mysql_db_query returns 1 on a insert statement -> no need to ask for results
86        if ($result != 1) {
87            for($i=0; $i< mysql_num_rows($result); $i++) {
88                $temparray = mysql_fetch_assoc($result);
89                $resultarray[]=$temparray;
90            }
91            mysql_free_result($result);
92        }
93
94        if (mysql_insert_id($this->dblink)) {
95            $resultarray = mysql_insert_id($this->dblink); //give back ID on insert
96        }
97
98        return $resultarray;
99    }
100
101    /**
102     * Returns a short name for a User Agent
103     *
104     * @fixme: needs to be implemented
105     */
106    function ua_info($ua){
107        return '';
108    }
109
110    /**
111     * log a page access
112     *
113     * called from log.php
114     */
115    function log_access(){
116        if(!$_REQUEST['p']) return;
117
118        $page    = addslashes($_REQUEST['p']);
119        $ip      = addslashes($_SERVER['REMOTE_ADDR']);
120        $ua      = addslashes($_SERVER['HTTP_USER_AGENT']);
121        $ua_info = addslashes($this->ua_info($_SERVER['HTTP_USER_AGENT']));
122        $ref     = addslashes($_REQUEST['r']);
123        $ref_md5 = ($ref) ? md5($ref) : '';
124        $sx      = (int) $_REQUEST['sx'];
125        $sy      = (int) $_REQUEST['sy'];
126        $vx      = (int) $_REQUEST['vx'];
127        $vy      = (int) $_REQUEST['vy'];
128        $user    = addslashes($_SERVER['REMOTE_USER']);
129        $session = addslashes(session_id());
130
131        $sql  = "INSERT DELAYED INTO ".$this->getConf('db_prefix')."access
132                    SET page     = '$page',
133                        ip       = '$ip',
134                        ua       = '$ua',
135                        ua_info  = '$ua_info',
136                        ref      = '$ref',
137                        ref_md5  = '$ref_md5',
138                        screen_x = '$sx',
139                        screen_y = '$sy',
140                        view_x   = '$vx',
141                        view_y   = '$vy',
142                        user     = '$user',
143                        session  = '$session'";
144        $ok = $this->runSQL($sql);
145        if(is_null($ok)){
146            global $MSG;
147            print_r($MSG);
148        }
149    }
150
151    /**
152     * Just send a 1x1 pixel blank gif to the browser
153     *
154     * @called from log.php
155     *
156     * @author Andreas Gohr <andi@splitbrain.org>
157     * @author Harry Fuecks <fuecks@gmail.com>
158     */
159    function sendGIF(){
160        $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7');
161        header('Content-Type: image/gif');
162        header('Content-Length: '.strlen($img));
163        header('Connection: Close');
164        print $img;
165        flush();
166        // Browser should drop connection after this
167        // Thinks it's got the whole image
168    }
169
170}
171