xref: /plugin/statistics/helper.php (revision b6632b6ef30a6f5a10a0a925445db5529b84c915)
16b6f8822SAndreas Gohr<?php
2a8acb244SAndreas Gohr
3d5ef99ddSAndreas Gohruse dokuwiki\Extension\Plugin;
4d5ef99ddSAndreas Gohruse dokuwiki\plugin\sqlite\SQLiteDB;
5aab59130SAndreas Gohruse dokuwiki\plugin\statistics\Logger;
6*b6632b6eSAndreas Gohruse dokuwiki\plugin\statistics\Query;
7d5ef99ddSAndreas Gohr
86b6f8822SAndreas Gohr/**
96b6f8822SAndreas Gohr * Statistics Plugin
106b6f8822SAndreas Gohr *
116b6f8822SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
126b6f8822SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
136b6f8822SAndreas Gohr */
14d5ef99ddSAndreas Gohrclass helper_plugin_statistics extends Plugin
15a8acb244SAndreas Gohr{
16aab59130SAndreas Gohr    protected $dblink;
176b6f8822SAndreas Gohr    public $prefix;
18aab59130SAndreas Gohr    protected $oQuery;
19762f4807SAndreas Gohr    protected ?Logger $oLogger = null;
20aab59130SAndreas Gohr    protected $oGraph;
21762f4807SAndreas Gohr    protected ?SQLiteDB $db = null;
226b6f8822SAndreas Gohr
236b6f8822SAndreas Gohr    /**
24cae4a1c5SAndreas Gohr     * Constructor
25cae4a1c5SAndreas Gohr     */
26a8acb244SAndreas Gohr    public function __construct()
27a8acb244SAndreas Gohr    {
28cae4a1c5SAndreas Gohr    }
29cae4a1c5SAndreas Gohr
30cae4a1c5SAndreas Gohr    /**
31d5ef99ddSAndreas Gohr     * Get SQLiteDB instance
32d5ef99ddSAndreas Gohr     *
33d5ef99ddSAndreas Gohr     * @return SQLiteDB|null
34d5ef99ddSAndreas Gohr     */
35d5ef99ddSAndreas Gohr    public function getDB()
36d5ef99ddSAndreas Gohr    {
37d5ef99ddSAndreas Gohr        if ($this->db === null) {
38762f4807SAndreas Gohr            $this->db = new SQLiteDB('statistics', DOKU_PLUGIN . 'statistics/db/');
39d5ef99ddSAndreas Gohr        }
40d5ef99ddSAndreas Gohr        return $this->db;
41d5ef99ddSAndreas Gohr    }
42d5ef99ddSAndreas Gohr
43d5ef99ddSAndreas Gohr
44d5ef99ddSAndreas Gohr    /**
456b6f8822SAndreas Gohr     * Return an instance of the query class
466b6f8822SAndreas Gohr     *
47*b6632b6eSAndreas Gohr     * @return Query
486b6f8822SAndreas Gohr     */
49a8acb244SAndreas Gohr    public function Query()
50a8acb244SAndreas Gohr    {
516b6f8822SAndreas Gohr        if (is_null($this->oQuery)) {
52*b6632b6eSAndreas Gohr            $this->oQuery = new Query($this);
536b6f8822SAndreas Gohr        }
546b6f8822SAndreas Gohr        return $this->oQuery;
556b6f8822SAndreas Gohr    }
566b6f8822SAndreas Gohr
576b6f8822SAndreas Gohr    /**
586b6f8822SAndreas Gohr     * Return an instance of the logger class
596b6f8822SAndreas Gohr     *
60aab59130SAndreas Gohr     * @return Logger
616b6f8822SAndreas Gohr     */
62a8acb244SAndreas Gohr    public function Logger()
63a8acb244SAndreas Gohr    {
64b6eece2fSAndreas Gohr        $this->prefix = $this->getConf('db_prefix');
656b6f8822SAndreas Gohr        if (is_null($this->oLogger)) {
66aab59130SAndreas Gohr            $this->oLogger = new Logger($this);
676b6f8822SAndreas Gohr        }
686b6f8822SAndreas Gohr        return $this->oLogger;
696b6f8822SAndreas Gohr    }
706b6f8822SAndreas Gohr
716b6f8822SAndreas Gohr    /**
726b6f8822SAndreas Gohr     * Return an instance of the Graph class
736b6f8822SAndreas Gohr     *
741664ba1dSAndreas Gohr     * @return StatisticsGraph
756b6f8822SAndreas Gohr     */
76a8acb244SAndreas Gohr    public function Graph()
77a8acb244SAndreas Gohr    {
78b6eece2fSAndreas Gohr        $this->prefix = $this->getConf('db_prefix');
796b6f8822SAndreas Gohr        if (is_null($this->oGraph)) {
80a8acb244SAndreas Gohr            require __DIR__ . '/inc/StatisticsGraph.class.php';
816b6f8822SAndreas Gohr            $this->oGraph = new StatisticsGraph($this);
826b6f8822SAndreas Gohr        }
836b6f8822SAndreas Gohr        return $this->oGraph;
846b6f8822SAndreas Gohr    }
856b6f8822SAndreas Gohr
866b6f8822SAndreas Gohr    /**
876b6f8822SAndreas Gohr     * Return a link to the DB, opening the connection if needed
886b6f8822SAndreas Gohr     */
89a8acb244SAndreas Gohr    protected function dbLink()
90a8acb244SAndreas Gohr    {
916b6f8822SAndreas Gohr        // connect to DB if needed
926b6f8822SAndreas Gohr        if (!$this->dblink) {
9384864b27SAndreas Gohr            if (!$this->getConf('db_server')) return null;
9484864b27SAndreas Gohr
95a242b522Sphl0            $this->dblink = mysqli_connect(
960863c19cSAndreas Gohr                $this->getConf('db_server'),
976b6f8822SAndreas Gohr                $this->getConf('db_user'),
980863c19cSAndreas Gohr                $this->getConf('db_password')
990863c19cSAndreas Gohr            );
1006b6f8822SAndreas Gohr            if (!$this->dblink) {
1016b6f8822SAndreas Gohr                msg('DB Error: connection failed', -1);
1026b6f8822SAndreas Gohr                return null;
1036b6f8822SAndreas Gohr            }
104a242b522Sphl0            if (!mysqli_select_db($this->dblink, $this->getConf('db_database'))) {
1058dfec278SAndreas Gohr                msg('DB Error: failed to select database', -1);
1068dfec278SAndreas Gohr                return null;
1078dfec278SAndreas Gohr            }
1088dfec278SAndreas Gohr
1096b6f8822SAndreas Gohr            // set utf-8
110a242b522Sphl0            if (!mysqli_query($this->dblink, 'set names utf8')) {
111a242b522Sphl0                msg('DB Error: could not set UTF-8 (' . mysqli_error($this->dblink) . ')', -1);
1126b6f8822SAndreas Gohr                return null;
1136b6f8822SAndreas Gohr            }
1146b6f8822SAndreas Gohr        }
1156b6f8822SAndreas Gohr        return $this->dblink;
1166b6f8822SAndreas Gohr    }
1176b6f8822SAndreas Gohr
1186b6f8822SAndreas Gohr    /**
1196b6f8822SAndreas Gohr     * Simple function to run a DB query
1206b6f8822SAndreas Gohr     */
121a8acb244SAndreas Gohr    public function runSQL($sql_string)
122a8acb244SAndreas Gohr    {
1236b6f8822SAndreas Gohr        $link = $this->dbLink();
12484864b27SAndreas Gohr        if (!$link) return null;
1256b6f8822SAndreas Gohr
126a242b522Sphl0        $result = mysqli_query($link, $sql_string);
127d277cdd5SAndreas Gohr        if ($result === false) {
128a242b522Sphl0            dbglog('DB Error: ' . mysqli_error($link) . ' ' . hsc($sql_string), -1);
129a242b522Sphl0            msg('DB Error: ' . mysqli_error($link) . ' ' . hsc($sql_string), -1);
1306b6f8822SAndreas Gohr            return null;
1316b6f8822SAndreas Gohr        }
1326b6f8822SAndreas Gohr
133a8acb244SAndreas Gohr        $resultarray = [];
1346b6f8822SAndreas Gohr
1356b6f8822SAndreas Gohr        //mysql_db_query returns 1 on a insert statement -> no need to ask for results
136d277cdd5SAndreas Gohr        if ($result !== true) {
137a242b522Sphl0            for ($i = 0; $i < mysqli_num_rows($result); $i++) {
138a242b522Sphl0                $temparray = mysqli_fetch_assoc($result);
1396b6f8822SAndreas Gohr                $resultarray[] = $temparray;
1406b6f8822SAndreas Gohr            }
141a242b522Sphl0            mysqli_free_result($result);
1426b6f8822SAndreas Gohr        }
1436b6f8822SAndreas Gohr
144a242b522Sphl0        if (mysqli_insert_id($link)) {
145a242b522Sphl0            $resultarray = mysqli_insert_id($link); //give back ID on insert
1466b6f8822SAndreas Gohr        }
1476b6f8822SAndreas Gohr
1486b6f8822SAndreas Gohr        return $resultarray;
1496b6f8822SAndreas Gohr    }
1506b6f8822SAndreas Gohr
1514f41a2ccSAndreas Gohr    /**
1524f41a2ccSAndreas Gohr     * Just send a 1x1 pixel blank gif to the browser
1534f41a2ccSAndreas Gohr     *
1544f41a2ccSAndreas Gohr     * @called from log.php
1554f41a2ccSAndreas Gohr     *
1564f41a2ccSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
1574f41a2ccSAndreas Gohr     * @author Harry Fuecks <fuecks@gmail.com>
1584f41a2ccSAndreas Gohr     */
159a8acb244SAndreas Gohr    public function sendGIF($transparent = true)
160a8acb244SAndreas Gohr    {
161259897e1SAndreas Gohr        if ($transparent) {
1624f41a2ccSAndreas Gohr            $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7');
163259897e1SAndreas Gohr        } else {
164259897e1SAndreas Gohr            $img = base64_decode('R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=');
165259897e1SAndreas Gohr        }
1664f41a2ccSAndreas Gohr        header('Content-Type: image/gif');
1674f41a2ccSAndreas Gohr        header('Content-Length: ' . strlen($img));
1684f41a2ccSAndreas Gohr        header('Connection: Close');
169a8acb244SAndreas Gohr        echo $img;
1704f41a2ccSAndreas Gohr        flush();
1714f41a2ccSAndreas Gohr        // Browser should drop connection after this
1724f41a2ccSAndreas Gohr        // Thinks it's got the whole image
1734f41a2ccSAndreas Gohr    }
1746b6f8822SAndreas Gohr}
175