xref: /plugin/statistics/helper.php (revision 0863c19c7bf8b95c6f464efd1fa1fd2c4f9b4e94)
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    /**
186b6f8822SAndreas Gohr     * Return an instance of the query class
196b6f8822SAndreas Gohr     *
206b6f8822SAndreas Gohr     * @return object
216b6f8822SAndreas Gohr     */
226b6f8822SAndreas Gohr    public function Query() {
23b6eece2fSAndreas Gohr        $this->prefix = $this->getConf('db_prefix');
246b6f8822SAndreas Gohr        if(is_null($this->oQuery)) {
256b6f8822SAndreas Gohr            require dirname(__FILE__) . '/inc/StatisticsQuery.class.php';
266b6f8822SAndreas Gohr            $this->oQuery = new StatisticsQuery($this);
276b6f8822SAndreas Gohr        }
286b6f8822SAndreas Gohr        return $this->oQuery;
296b6f8822SAndreas Gohr    }
306b6f8822SAndreas Gohr
316b6f8822SAndreas Gohr    /**
326b6f8822SAndreas Gohr     * Return an instance of the logger class
336b6f8822SAndreas Gohr     *
346b6f8822SAndreas Gohr     * @return object
356b6f8822SAndreas Gohr     */
366b6f8822SAndreas Gohr    public function Logger() {
37b6eece2fSAndreas Gohr        $this->prefix = $this->getConf('db_prefix');
386b6f8822SAndreas Gohr        if(is_null($this->oLogger)) {
396b6f8822SAndreas Gohr            require dirname(__FILE__) . '/inc/StatisticsLogger.class.php';
4058511ae8SAndreas Gohr            $this->oLogger = new StatisticsLogger($this);
416b6f8822SAndreas Gohr        }
426b6f8822SAndreas Gohr        return $this->oLogger;
436b6f8822SAndreas Gohr    }
446b6f8822SAndreas Gohr
456b6f8822SAndreas Gohr    /**
466b6f8822SAndreas Gohr     * Return an instance of the Graph class
476b6f8822SAndreas Gohr     *
486b6f8822SAndreas Gohr     * @return object
496b6f8822SAndreas Gohr     */
506b6f8822SAndreas Gohr    public function Graph() {
51b6eece2fSAndreas Gohr        $this->prefix = $this->getConf('db_prefix');
526b6f8822SAndreas Gohr        if(is_null($this->oGraph)) {
536b6f8822SAndreas Gohr            require dirname(__FILE__) . '/inc/StatisticsGraph.class.php';
546b6f8822SAndreas Gohr            $this->oGraph = new StatisticsGraph($this);
556b6f8822SAndreas Gohr        }
566b6f8822SAndreas Gohr        return $this->oGraph;
576b6f8822SAndreas Gohr    }
586b6f8822SAndreas Gohr
596b6f8822SAndreas Gohr    /**
606b6f8822SAndreas Gohr     * Return a link to the DB, opening the connection if needed
616b6f8822SAndreas Gohr     */
626b6f8822SAndreas Gohr    protected function dbLink() {
636b6f8822SAndreas Gohr        // connect to DB if needed
646b6f8822SAndreas Gohr        if(!$this->dblink) {
65*0863c19cSAndreas Gohr            $this->dblink = mysql_connect(
66*0863c19cSAndreas Gohr                $this->getConf('db_server'),
676b6f8822SAndreas Gohr                $this->getConf('db_user'),
68*0863c19cSAndreas Gohr                $this->getConf('db_password')
69*0863c19cSAndreas Gohr            );
706b6f8822SAndreas Gohr            if(!$this->dblink) {
716b6f8822SAndreas Gohr                msg('DB Error: connection failed', -1);
726b6f8822SAndreas Gohr                return null;
736b6f8822SAndreas Gohr            }
748dfec278SAndreas Gohr            if(!mysql_select_db($this->getConf('db_database'))) {
758dfec278SAndreas Gohr                msg('DB Error: failed to select database', -1);
768dfec278SAndreas Gohr                return null;
778dfec278SAndreas Gohr            }
788dfec278SAndreas Gohr
796b6f8822SAndreas Gohr            // set utf-8
808dfec278SAndreas Gohr            if(!mysql_query('set names utf8', $this->dblink)) {
816b6f8822SAndreas Gohr                msg('DB Error: could not set UTF-8 (' . mysql_error($this->dblink) . ')', -1);
826b6f8822SAndreas Gohr                return null;
836b6f8822SAndreas Gohr            }
846b6f8822SAndreas Gohr        }
856b6f8822SAndreas Gohr        return $this->dblink;
866b6f8822SAndreas Gohr    }
876b6f8822SAndreas Gohr
886b6f8822SAndreas Gohr    /**
896b6f8822SAndreas Gohr     * Simple function to run a DB query
906b6f8822SAndreas Gohr     */
916b6f8822SAndreas Gohr    public function runSQL($sql_string) {
926b6f8822SAndreas Gohr        $link = $this->dbLink();
936b6f8822SAndreas Gohr
948dfec278SAndreas Gohr        $result = mysql_query($sql_string, $link);
956b6f8822SAndreas Gohr        if(!$result) {
96099a30bcSAndreas Gohr            dbglog('DB Error: ' . mysql_error($link) . ' ' . hsc($sql_string), -1);
976b6f8822SAndreas Gohr            msg('DB Error: ' . mysql_error($link) . ' ' . hsc($sql_string), -1);
986b6f8822SAndreas Gohr            return null;
996b6f8822SAndreas Gohr        }
1006b6f8822SAndreas Gohr
1016b6f8822SAndreas Gohr        $resultarray = array();
1026b6f8822SAndreas Gohr
1036b6f8822SAndreas Gohr        //mysql_db_query returns 1 on a insert statement -> no need to ask for results
1046b6f8822SAndreas Gohr        if($result != 1) {
1056b6f8822SAndreas Gohr            for($i = 0; $i < mysql_num_rows($result); $i++) {
1066b6f8822SAndreas Gohr                $temparray     = mysql_fetch_assoc($result);
1076b6f8822SAndreas Gohr                $resultarray[] = $temparray;
1086b6f8822SAndreas Gohr            }
1096b6f8822SAndreas Gohr            mysql_free_result($result);
1106b6f8822SAndreas Gohr        }
1116b6f8822SAndreas Gohr
1126b6f8822SAndreas Gohr        if(mysql_insert_id($link)) {
1136b6f8822SAndreas Gohr            $resultarray = mysql_insert_id($link); //give back ID on insert
1146b6f8822SAndreas Gohr        }
1156b6f8822SAndreas Gohr
1166b6f8822SAndreas Gohr        return $resultarray;
1176b6f8822SAndreas Gohr    }
1186b6f8822SAndreas Gohr
1194f41a2ccSAndreas Gohr    /**
1204f41a2ccSAndreas Gohr     * Just send a 1x1 pixel blank gif to the browser
1214f41a2ccSAndreas Gohr     *
1224f41a2ccSAndreas Gohr     * @called from log.php
1234f41a2ccSAndreas Gohr     *
1244f41a2ccSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
1254f41a2ccSAndreas Gohr     * @author Harry Fuecks <fuecks@gmail.com>
1264f41a2ccSAndreas Gohr     */
127259897e1SAndreas Gohr    function sendGIF($transparent = true) {
128259897e1SAndreas Gohr        if($transparent) {
1294f41a2ccSAndreas Gohr            $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7');
130259897e1SAndreas Gohr        } else {
131259897e1SAndreas Gohr            $img = base64_decode('R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=');
132259897e1SAndreas Gohr        }
1334f41a2ccSAndreas Gohr        header('Content-Type: image/gif');
1344f41a2ccSAndreas Gohr        header('Content-Length: ' . strlen($img));
1354f41a2ccSAndreas Gohr        header('Connection: Close');
1364f41a2ccSAndreas Gohr        print $img;
1374f41a2ccSAndreas Gohr        flush();
1384f41a2ccSAndreas Gohr        // Browser should drop connection after this
1394f41a2ccSAndreas Gohr        // Thinks it's got the whole image
1404f41a2ccSAndreas Gohr    }
1416b6f8822SAndreas Gohr}
142