xref: /plugin/statistics/helper.php (revision a8acb244f011a1b8bd9a22373e14f2819297c46f)
16b6f8822SAndreas Gohr<?php
2*a8acb244SAndreas Gohr
36b6f8822SAndreas Gohr/**
46b6f8822SAndreas Gohr * Statistics Plugin
56b6f8822SAndreas Gohr *
66b6f8822SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
76b6f8822SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
86b6f8822SAndreas Gohr */
96b6f8822SAndreas Gohr
10*a8acb244SAndreas Gohrclass helper_plugin_statistics extends Dokuwiki_Plugin
11*a8acb244SAndreas Gohr{
12*a8acb244SAndreas Gohr    private $dblink;
136b6f8822SAndreas Gohr    public $prefix;
14*a8acb244SAndreas Gohr    private $oQuery;
15*a8acb244SAndreas Gohr    private $oLogger;
16*a8acb244SAndreas Gohr    private $oGraph;
176b6f8822SAndreas Gohr
186b6f8822SAndreas Gohr    /**
19cae4a1c5SAndreas Gohr     * Constructor
20cae4a1c5SAndreas Gohr     */
21*a8acb244SAndreas Gohr    public function __construct()
22*a8acb244SAndreas Gohr    {
23cae4a1c5SAndreas Gohr        $this->prefix = $this->getConf('db_prefix');
24cae4a1c5SAndreas Gohr    }
25cae4a1c5SAndreas Gohr
26cae4a1c5SAndreas Gohr    /**
276b6f8822SAndreas Gohr     * Return an instance of the query class
286b6f8822SAndreas Gohr     *
291664ba1dSAndreas Gohr     * @return StatisticsQuery
306b6f8822SAndreas Gohr     */
31*a8acb244SAndreas Gohr    public function Query()
32*a8acb244SAndreas Gohr    {
336b6f8822SAndreas Gohr        if (is_null($this->oQuery)) {
34*a8acb244SAndreas Gohr            require __DIR__ . '/inc/StatisticsQuery.class.php';
356b6f8822SAndreas Gohr            $this->oQuery = new StatisticsQuery($this);
366b6f8822SAndreas Gohr        }
376b6f8822SAndreas Gohr        return $this->oQuery;
386b6f8822SAndreas Gohr    }
396b6f8822SAndreas Gohr
406b6f8822SAndreas Gohr    /**
416b6f8822SAndreas Gohr     * Return an instance of the logger class
426b6f8822SAndreas Gohr     *
431664ba1dSAndreas Gohr     * @return StatisticsLogger
446b6f8822SAndreas Gohr     */
45*a8acb244SAndreas Gohr    public function Logger()
46*a8acb244SAndreas Gohr    {
47b6eece2fSAndreas Gohr        $this->prefix = $this->getConf('db_prefix');
486b6f8822SAndreas Gohr        if (is_null($this->oLogger)) {
49*a8acb244SAndreas Gohr            require __DIR__ . '/inc/StatisticsLogger.class.php';
5058511ae8SAndreas Gohr            $this->oLogger = new StatisticsLogger($this);
516b6f8822SAndreas Gohr        }
526b6f8822SAndreas Gohr        return $this->oLogger;
536b6f8822SAndreas Gohr    }
546b6f8822SAndreas Gohr
556b6f8822SAndreas Gohr    /**
566b6f8822SAndreas Gohr     * Return an instance of the Graph class
576b6f8822SAndreas Gohr     *
581664ba1dSAndreas Gohr     * @return StatisticsGraph
596b6f8822SAndreas Gohr     */
60*a8acb244SAndreas Gohr    public function Graph()
61*a8acb244SAndreas Gohr    {
62b6eece2fSAndreas Gohr        $this->prefix = $this->getConf('db_prefix');
636b6f8822SAndreas Gohr        if (is_null($this->oGraph)) {
64*a8acb244SAndreas Gohr            require __DIR__ . '/inc/StatisticsGraph.class.php';
656b6f8822SAndreas Gohr            $this->oGraph = new StatisticsGraph($this);
666b6f8822SAndreas Gohr        }
676b6f8822SAndreas Gohr        return $this->oGraph;
686b6f8822SAndreas Gohr    }
696b6f8822SAndreas Gohr
706b6f8822SAndreas Gohr    /**
716b6f8822SAndreas Gohr     * Return a link to the DB, opening the connection if needed
726b6f8822SAndreas Gohr     */
73*a8acb244SAndreas Gohr    protected function dbLink()
74*a8acb244SAndreas Gohr    {
756b6f8822SAndreas Gohr        // connect to DB if needed
766b6f8822SAndreas Gohr        if (!$this->dblink) {
7784864b27SAndreas Gohr            if (!$this->getConf('db_server')) return null;
7884864b27SAndreas Gohr
79a242b522Sphl0            $this->dblink = mysqli_connect(
800863c19cSAndreas Gohr                $this->getConf('db_server'),
816b6f8822SAndreas Gohr                $this->getConf('db_user'),
820863c19cSAndreas Gohr                $this->getConf('db_password')
830863c19cSAndreas Gohr            );
846b6f8822SAndreas Gohr            if (!$this->dblink) {
856b6f8822SAndreas Gohr                msg('DB Error: connection failed', -1);
866b6f8822SAndreas Gohr                return null;
876b6f8822SAndreas Gohr            }
88a242b522Sphl0            if (!mysqli_select_db($this->dblink, $this->getConf('db_database'))) {
898dfec278SAndreas Gohr                msg('DB Error: failed to select database', -1);
908dfec278SAndreas Gohr                return null;
918dfec278SAndreas Gohr            }
928dfec278SAndreas Gohr
936b6f8822SAndreas Gohr            // set utf-8
94a242b522Sphl0            if (!mysqli_query($this->dblink, 'set names utf8')) {
95a242b522Sphl0                msg('DB Error: could not set UTF-8 (' . mysqli_error($this->dblink) . ')', -1);
966b6f8822SAndreas Gohr                return null;
976b6f8822SAndreas Gohr            }
986b6f8822SAndreas Gohr        }
996b6f8822SAndreas Gohr        return $this->dblink;
1006b6f8822SAndreas Gohr    }
1016b6f8822SAndreas Gohr
1026b6f8822SAndreas Gohr    /**
1036b6f8822SAndreas Gohr     * Simple function to run a DB query
1046b6f8822SAndreas Gohr     */
105*a8acb244SAndreas Gohr    public function runSQL($sql_string)
106*a8acb244SAndreas Gohr    {
1076b6f8822SAndreas Gohr        $link = $this->dbLink();
10884864b27SAndreas Gohr        if (!$link) return null;
1096b6f8822SAndreas Gohr
110a242b522Sphl0        $result = mysqli_query($link, $sql_string);
111d277cdd5SAndreas Gohr        if ($result === false) {
112a242b522Sphl0            dbglog('DB Error: ' . mysqli_error($link) . ' ' . hsc($sql_string), -1);
113a242b522Sphl0            msg('DB Error: ' . mysqli_error($link) . ' ' . hsc($sql_string), -1);
1146b6f8822SAndreas Gohr            return null;
1156b6f8822SAndreas Gohr        }
1166b6f8822SAndreas Gohr
117*a8acb244SAndreas Gohr        $resultarray = [];
1186b6f8822SAndreas Gohr
1196b6f8822SAndreas Gohr        //mysql_db_query returns 1 on a insert statement -> no need to ask for results
120d277cdd5SAndreas Gohr        if ($result !== true) {
121a242b522Sphl0            for ($i = 0; $i < mysqli_num_rows($result); $i++) {
122a242b522Sphl0                $temparray     = mysqli_fetch_assoc($result);
1236b6f8822SAndreas Gohr                $resultarray[] = $temparray;
1246b6f8822SAndreas Gohr            }
125a242b522Sphl0            mysqli_free_result($result);
1266b6f8822SAndreas Gohr        }
1276b6f8822SAndreas Gohr
128a242b522Sphl0        if (mysqli_insert_id($link)) {
129a242b522Sphl0            $resultarray = mysqli_insert_id($link); //give back ID on insert
1306b6f8822SAndreas Gohr        }
1316b6f8822SAndreas Gohr
1326b6f8822SAndreas Gohr        return $resultarray;
1336b6f8822SAndreas Gohr    }
1346b6f8822SAndreas Gohr
1354f41a2ccSAndreas Gohr    /**
1364f41a2ccSAndreas Gohr     * Just send a 1x1 pixel blank gif to the browser
1374f41a2ccSAndreas Gohr     *
1384f41a2ccSAndreas Gohr     * @called from log.php
1394f41a2ccSAndreas Gohr     *
1404f41a2ccSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
1414f41a2ccSAndreas Gohr     * @author Harry Fuecks <fuecks@gmail.com>
1424f41a2ccSAndreas Gohr     */
143*a8acb244SAndreas Gohr    public function sendGIF($transparent = true)
144*a8acb244SAndreas Gohr    {
145259897e1SAndreas Gohr        if ($transparent) {
1464f41a2ccSAndreas Gohr            $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7');
147259897e1SAndreas Gohr        } else {
148259897e1SAndreas Gohr            $img = base64_decode('R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=');
149259897e1SAndreas Gohr        }
1504f41a2ccSAndreas Gohr        header('Content-Type: image/gif');
1514f41a2ccSAndreas Gohr        header('Content-Length: ' . strlen($img));
1524f41a2ccSAndreas Gohr        header('Connection: Close');
153*a8acb244SAndreas Gohr        echo $img;
1544f41a2ccSAndreas Gohr        flush();
1554f41a2ccSAndreas Gohr        // Browser should drop connection after this
1564f41a2ccSAndreas Gohr        // Thinks it's got the whole image
1574f41a2ccSAndreas Gohr    }
1586b6f8822SAndreas Gohr}
159