xref: /plugin/statistics/helper.php (revision cae4a1c5d0ac2520d0d70293c31b44783a9dea09)
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    /**
18*cae4a1c5SAndreas Gohr     * Constructor
19*cae4a1c5SAndreas Gohr     */
20*cae4a1c5SAndreas Gohr    public function __construct() {
21*cae4a1c5SAndreas Gohr        $this->prefix = $this->getConf('db_prefix');
22*cae4a1c5SAndreas Gohr    }
23*cae4a1c5SAndreas Gohr
24*cae4a1c5SAndreas 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) {
710863c19cSAndreas Gohr            $this->dblink = mysql_connect(
720863c19cSAndreas Gohr                $this->getConf('db_server'),
736b6f8822SAndreas Gohr                $this->getConf('db_user'),
740863c19cSAndreas Gohr                $this->getConf('db_password')
750863c19cSAndreas Gohr            );
766b6f8822SAndreas Gohr            if(!$this->dblink) {
776b6f8822SAndreas Gohr                msg('DB Error: connection failed', -1);
786b6f8822SAndreas Gohr                return null;
796b6f8822SAndreas Gohr            }
808dfec278SAndreas Gohr            if(!mysql_select_db($this->getConf('db_database'))) {
818dfec278SAndreas Gohr                msg('DB Error: failed to select database', -1);
828dfec278SAndreas Gohr                return null;
838dfec278SAndreas Gohr            }
848dfec278SAndreas Gohr
856b6f8822SAndreas Gohr            // set utf-8
868dfec278SAndreas Gohr            if(!mysql_query('set names utf8', $this->dblink)) {
876b6f8822SAndreas Gohr                msg('DB Error: could not set UTF-8 (' . mysql_error($this->dblink) . ')', -1);
886b6f8822SAndreas Gohr                return null;
896b6f8822SAndreas Gohr            }
906b6f8822SAndreas Gohr        }
916b6f8822SAndreas Gohr        return $this->dblink;
926b6f8822SAndreas Gohr    }
936b6f8822SAndreas Gohr
946b6f8822SAndreas Gohr    /**
956b6f8822SAndreas Gohr     * Simple function to run a DB query
966b6f8822SAndreas Gohr     */
976b6f8822SAndreas Gohr    public function runSQL($sql_string) {
986b6f8822SAndreas Gohr        $link = $this->dbLink();
996b6f8822SAndreas Gohr
1008dfec278SAndreas Gohr        $result = mysql_query($sql_string, $link);
1016b6f8822SAndreas Gohr        if(!$result) {
102099a30bcSAndreas Gohr            dbglog('DB Error: ' . mysql_error($link) . ' ' . hsc($sql_string), -1);
1036b6f8822SAndreas Gohr            msg('DB Error: ' . mysql_error($link) . ' ' . hsc($sql_string), -1);
1046b6f8822SAndreas Gohr            return null;
1056b6f8822SAndreas Gohr        }
1066b6f8822SAndreas Gohr
1076b6f8822SAndreas Gohr        $resultarray = array();
1086b6f8822SAndreas Gohr
1096b6f8822SAndreas Gohr        //mysql_db_query returns 1 on a insert statement -> no need to ask for results
1106b6f8822SAndreas Gohr        if($result != 1) {
1116b6f8822SAndreas Gohr            for($i = 0; $i < mysql_num_rows($result); $i++) {
1126b6f8822SAndreas Gohr                $temparray     = mysql_fetch_assoc($result);
1136b6f8822SAndreas Gohr                $resultarray[] = $temparray;
1146b6f8822SAndreas Gohr            }
1156b6f8822SAndreas Gohr            mysql_free_result($result);
1166b6f8822SAndreas Gohr        }
1176b6f8822SAndreas Gohr
1186b6f8822SAndreas Gohr        if(mysql_insert_id($link)) {
1196b6f8822SAndreas Gohr            $resultarray = mysql_insert_id($link); //give back ID on insert
1206b6f8822SAndreas Gohr        }
1216b6f8822SAndreas Gohr
1226b6f8822SAndreas Gohr        return $resultarray;
1236b6f8822SAndreas Gohr    }
1246b6f8822SAndreas Gohr
1254f41a2ccSAndreas Gohr    /**
1264f41a2ccSAndreas Gohr     * Just send a 1x1 pixel blank gif to the browser
1274f41a2ccSAndreas Gohr     *
1284f41a2ccSAndreas Gohr     * @called from log.php
1294f41a2ccSAndreas Gohr     *
1304f41a2ccSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
1314f41a2ccSAndreas Gohr     * @author Harry Fuecks <fuecks@gmail.com>
1324f41a2ccSAndreas Gohr     */
133259897e1SAndreas Gohr    function sendGIF($transparent = true) {
134259897e1SAndreas Gohr        if($transparent) {
1354f41a2ccSAndreas Gohr            $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7');
136259897e1SAndreas Gohr        } else {
137259897e1SAndreas Gohr            $img = base64_decode('R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=');
138259897e1SAndreas Gohr        }
1394f41a2ccSAndreas Gohr        header('Content-Type: image/gif');
1404f41a2ccSAndreas Gohr        header('Content-Length: ' . strlen($img));
1414f41a2ccSAndreas Gohr        header('Connection: Close');
1424f41a2ccSAndreas Gohr        print $img;
1434f41a2ccSAndreas Gohr        flush();
1444f41a2ccSAndreas Gohr        // Browser should drop connection after this
1454f41a2ccSAndreas Gohr        // Thinks it's got the whole image
1464f41a2ccSAndreas Gohr    }
1476b6f8822SAndreas Gohr}
148