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){ 656b6f8822SAndreas Gohr $this->dblink = mysql_connect($this->getConf('db_server'), 666b6f8822SAndreas Gohr $this->getConf('db_user'), 676b6f8822SAndreas Gohr $this->getConf('db_password')); 686b6f8822SAndreas Gohr if(!$this->dblink){ 696b6f8822SAndreas Gohr msg('DB Error: connection failed',-1); 706b6f8822SAndreas Gohr return null; 716b6f8822SAndreas Gohr } 72*8dfec278SAndreas Gohr if(!mysql_select_db($this->getConf('db_database'))) { 73*8dfec278SAndreas Gohr msg('DB Error: failed to select database',-1); 74*8dfec278SAndreas Gohr return null; 75*8dfec278SAndreas Gohr } 76*8dfec278SAndreas Gohr 776b6f8822SAndreas Gohr // set utf-8 78*8dfec278SAndreas Gohr if(!mysql_query('set names utf8', $this->dblink)){ 796b6f8822SAndreas Gohr msg('DB Error: could not set UTF-8 ('.mysql_error($this->dblink).')',-1); 806b6f8822SAndreas Gohr return null; 816b6f8822SAndreas Gohr } 826b6f8822SAndreas Gohr } 836b6f8822SAndreas Gohr return $this->dblink; 846b6f8822SAndreas Gohr } 856b6f8822SAndreas Gohr 866b6f8822SAndreas Gohr /** 876b6f8822SAndreas Gohr * Simple function to run a DB query 886b6f8822SAndreas Gohr */ 896b6f8822SAndreas Gohr public function runSQL($sql_string) { 906b6f8822SAndreas Gohr $link = $this->dbLink(); 916b6f8822SAndreas Gohr 92*8dfec278SAndreas Gohr $result = mysql_query($sql_string,$link); 936b6f8822SAndreas Gohr if(!$result){ 94099a30bcSAndreas Gohr dbglog('DB Error: '.mysql_error($link).' '.hsc($sql_string),-1); 956b6f8822SAndreas Gohr msg('DB Error: '.mysql_error($link).' '.hsc($sql_string),-1); 966b6f8822SAndreas Gohr return null; 976b6f8822SAndreas Gohr } 986b6f8822SAndreas Gohr 996b6f8822SAndreas Gohr $resultarray = array(); 1006b6f8822SAndreas Gohr 1016b6f8822SAndreas Gohr //mysql_db_query returns 1 on a insert statement -> no need to ask for results 1026b6f8822SAndreas Gohr if ($result != 1) { 1036b6f8822SAndreas Gohr for($i=0; $i< mysql_num_rows($result); $i++) { 1046b6f8822SAndreas Gohr $temparray = mysql_fetch_assoc($result); 1056b6f8822SAndreas Gohr $resultarray[]=$temparray; 1066b6f8822SAndreas Gohr } 1076b6f8822SAndreas Gohr mysql_free_result($result); 1086b6f8822SAndreas Gohr } 1096b6f8822SAndreas Gohr 1106b6f8822SAndreas Gohr if (mysql_insert_id($link)) { 1116b6f8822SAndreas Gohr $resultarray = mysql_insert_id($link); //give back ID on insert 1126b6f8822SAndreas Gohr } 1136b6f8822SAndreas Gohr 1146b6f8822SAndreas Gohr return $resultarray; 1156b6f8822SAndreas Gohr } 1166b6f8822SAndreas Gohr 1174f41a2ccSAndreas Gohr 1184f41a2ccSAndreas Gohr /** 1194f41a2ccSAndreas Gohr * Just send a 1x1 pixel blank gif to the browser 1204f41a2ccSAndreas Gohr * 1214f41a2ccSAndreas Gohr * @called from log.php 1224f41a2ccSAndreas Gohr * 1234f41a2ccSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1244f41a2ccSAndreas Gohr * @author Harry Fuecks <fuecks@gmail.com> 1254f41a2ccSAndreas Gohr */ 126259897e1SAndreas Gohr function sendGIF($transparent = true){ 127259897e1SAndreas Gohr if($transparent){ 1284f41a2ccSAndreas Gohr $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7'); 129259897e1SAndreas Gohr }else{ 130259897e1SAndreas Gohr $img = base64_decode('R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs='); 131259897e1SAndreas Gohr } 1324f41a2ccSAndreas Gohr header('Content-Type: image/gif'); 1334f41a2ccSAndreas Gohr header('Content-Length: '.strlen($img)); 1344f41a2ccSAndreas Gohr header('Connection: Close'); 1354f41a2ccSAndreas Gohr print $img; 1364f41a2ccSAndreas Gohr flush(); 1374f41a2ccSAndreas Gohr // Browser should drop connection after this 1384f41a2ccSAndreas Gohr // Thinks it's got the whole image 1394f41a2ccSAndreas Gohr } 1406b6f8822SAndreas Gohr} 141