xref: /plugin/statistics/helper.php (revision d277cdd575718009ab7c5d3391859fdd835b5ea4)
16b6f8822SAndreas Gohr<?php
26b6f8822SAndreas Gohr/**
36b6f8822SAndreas Gohr * Statistics Plugin
46b6f8822SAndreas Gohr *
56b6f8822SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
66b6f8822SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
76b6f8822SAndreas Gohr */
86b6f8822SAndreas Gohr
96b6f8822SAndreas Gohrclass helper_plugin_statistics extends Dokuwiki_Plugin {
106b6f8822SAndreas Gohr
116b6f8822SAndreas Gohr    private $dblink = null;
126b6f8822SAndreas Gohr    public $prefix;
136b6f8822SAndreas Gohr    private $oQuery = null;
146b6f8822SAndreas Gohr    private $oLogger = null;
156b6f8822SAndreas Gohr    private $oGraph = null;
166b6f8822SAndreas Gohr
176b6f8822SAndreas Gohr    /**
18cae4a1c5SAndreas Gohr     * Constructor
19cae4a1c5SAndreas Gohr     */
20cae4a1c5SAndreas Gohr    public function __construct() {
21cae4a1c5SAndreas Gohr        $this->prefix = $this->getConf('db_prefix');
22cae4a1c5SAndreas Gohr    }
23cae4a1c5SAndreas Gohr
24cae4a1c5SAndreas Gohr    /**
256b6f8822SAndreas Gohr     * Return an instance of the query class
266b6f8822SAndreas Gohr     *
271664ba1dSAndreas Gohr     * @return StatisticsQuery
286b6f8822SAndreas Gohr     */
296b6f8822SAndreas Gohr    public function Query() {
306b6f8822SAndreas Gohr        if(is_null($this->oQuery)) {
316b6f8822SAndreas Gohr            require dirname(__FILE__) . '/inc/StatisticsQuery.class.php';
326b6f8822SAndreas Gohr            $this->oQuery = new StatisticsQuery($this);
336b6f8822SAndreas Gohr        }
346b6f8822SAndreas Gohr        return $this->oQuery;
356b6f8822SAndreas Gohr    }
366b6f8822SAndreas Gohr
376b6f8822SAndreas Gohr    /**
386b6f8822SAndreas Gohr     * Return an instance of the logger class
396b6f8822SAndreas Gohr     *
401664ba1dSAndreas Gohr     * @return StatisticsLogger
416b6f8822SAndreas Gohr     */
426b6f8822SAndreas Gohr    public function Logger() {
43b6eece2fSAndreas Gohr        $this->prefix = $this->getConf('db_prefix');
446b6f8822SAndreas Gohr        if(is_null($this->oLogger)) {
456b6f8822SAndreas Gohr            require dirname(__FILE__) . '/inc/StatisticsLogger.class.php';
4658511ae8SAndreas Gohr            $this->oLogger = new StatisticsLogger($this);
476b6f8822SAndreas Gohr        }
486b6f8822SAndreas Gohr        return $this->oLogger;
496b6f8822SAndreas Gohr    }
506b6f8822SAndreas Gohr
516b6f8822SAndreas Gohr    /**
526b6f8822SAndreas Gohr     * Return an instance of the Graph class
536b6f8822SAndreas Gohr     *
541664ba1dSAndreas Gohr     * @return StatisticsGraph
556b6f8822SAndreas Gohr     */
566b6f8822SAndreas Gohr    public function Graph() {
57b6eece2fSAndreas Gohr        $this->prefix = $this->getConf('db_prefix');
586b6f8822SAndreas Gohr        if(is_null($this->oGraph)) {
596b6f8822SAndreas Gohr            require dirname(__FILE__) . '/inc/StatisticsGraph.class.php';
606b6f8822SAndreas Gohr            $this->oGraph = new StatisticsGraph($this);
616b6f8822SAndreas Gohr        }
626b6f8822SAndreas Gohr        return $this->oGraph;
636b6f8822SAndreas Gohr    }
646b6f8822SAndreas Gohr
656b6f8822SAndreas Gohr    /**
666b6f8822SAndreas Gohr     * Return a link to the DB, opening the connection if needed
676b6f8822SAndreas Gohr     */
686b6f8822SAndreas Gohr    protected function dbLink() {
696b6f8822SAndreas Gohr        // connect to DB if needed
706b6f8822SAndreas Gohr        if(!$this->dblink) {
7184864b27SAndreas Gohr            if(!$this->getConf('db_server')) return null;
7284864b27SAndreas Gohr
73a242b522Sphl0            $this->dblink = mysqli_connect(
740863c19cSAndreas Gohr                $this->getConf('db_server'),
756b6f8822SAndreas Gohr                $this->getConf('db_user'),
760863c19cSAndreas Gohr                $this->getConf('db_password')
770863c19cSAndreas Gohr            );
786b6f8822SAndreas Gohr            if(!$this->dblink) {
796b6f8822SAndreas Gohr                msg('DB Error: connection failed', -1);
806b6f8822SAndreas Gohr                return null;
816b6f8822SAndreas Gohr            }
82a242b522Sphl0            if(!mysqli_select_db($this->dblink, $this->getConf('db_database'))) {
838dfec278SAndreas Gohr                msg('DB Error: failed to select database', -1);
848dfec278SAndreas Gohr                return null;
858dfec278SAndreas Gohr            }
868dfec278SAndreas Gohr
876b6f8822SAndreas Gohr            // set utf-8
88a242b522Sphl0            if(!mysqli_query($this->dblink, 'set names utf8')) {
89a242b522Sphl0                msg('DB Error: could not set UTF-8 (' . mysqli_error($this->dblink) . ')', -1);
906b6f8822SAndreas Gohr                return null;
916b6f8822SAndreas Gohr            }
926b6f8822SAndreas Gohr        }
936b6f8822SAndreas Gohr        return $this->dblink;
946b6f8822SAndreas Gohr    }
956b6f8822SAndreas Gohr
966b6f8822SAndreas Gohr    /**
976b6f8822SAndreas Gohr     * Simple function to run a DB query
986b6f8822SAndreas Gohr     */
996b6f8822SAndreas Gohr    public function runSQL($sql_string) {
1006b6f8822SAndreas Gohr        $link = $this->dbLink();
10184864b27SAndreas Gohr        if(!$link) return null;
1026b6f8822SAndreas Gohr
103a242b522Sphl0        $result = mysqli_query($link, $sql_string);
104*d277cdd5SAndreas Gohr        if($result === false) {
105a242b522Sphl0            dbglog('DB Error: ' . mysqli_error($link) . ' ' . hsc($sql_string), -1);
106a242b522Sphl0            msg('DB Error: ' . mysqli_error($link) . ' ' . hsc($sql_string), -1);
1076b6f8822SAndreas Gohr            return null;
1086b6f8822SAndreas Gohr        }
1096b6f8822SAndreas Gohr
1106b6f8822SAndreas Gohr        $resultarray = array();
1116b6f8822SAndreas Gohr
1126b6f8822SAndreas Gohr        //mysql_db_query returns 1 on a insert statement -> no need to ask for results
113*d277cdd5SAndreas Gohr        if($result !== true) {
114a242b522Sphl0            for($i = 0; $i < mysqli_num_rows($result); $i++) {
115a242b522Sphl0                $temparray     = mysqli_fetch_assoc($result);
1166b6f8822SAndreas Gohr                $resultarray[] = $temparray;
1176b6f8822SAndreas Gohr            }
118a242b522Sphl0            mysqli_free_result($result);
1196b6f8822SAndreas Gohr        }
1206b6f8822SAndreas Gohr
121a242b522Sphl0        if(mysqli_insert_id($link)) {
122a242b522Sphl0            $resultarray = mysqli_insert_id($link); //give back ID on insert
1236b6f8822SAndreas Gohr        }
1246b6f8822SAndreas Gohr
1256b6f8822SAndreas Gohr        return $resultarray;
1266b6f8822SAndreas Gohr    }
1276b6f8822SAndreas Gohr
1284f41a2ccSAndreas Gohr    /**
1294f41a2ccSAndreas Gohr     * Just send a 1x1 pixel blank gif to the browser
1304f41a2ccSAndreas Gohr     *
1314f41a2ccSAndreas Gohr     * @called from log.php
1324f41a2ccSAndreas Gohr     *
1334f41a2ccSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
1344f41a2ccSAndreas Gohr     * @author Harry Fuecks <fuecks@gmail.com>
1354f41a2ccSAndreas Gohr     */
136259897e1SAndreas Gohr    function sendGIF($transparent = true) {
137259897e1SAndreas Gohr        if($transparent) {
1384f41a2ccSAndreas Gohr            $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7');
139259897e1SAndreas Gohr        } else {
140259897e1SAndreas Gohr            $img = base64_decode('R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=');
141259897e1SAndreas Gohr        }
1424f41a2ccSAndreas Gohr        header('Content-Type: image/gif');
1434f41a2ccSAndreas Gohr        header('Content-Length: ' . strlen($img));
1444f41a2ccSAndreas Gohr        header('Connection: Close');
1454f41a2ccSAndreas Gohr        print $img;
1464f41a2ccSAndreas Gohr        flush();
1474f41a2ccSAndreas Gohr        // Browser should drop connection after this
1484f41a2ccSAndreas Gohr        // Thinks it's got the whole image
1494f41a2ccSAndreas Gohr    }
1506b6f8822SAndreas Gohr}
151