16b6f8822SAndreas Gohr<?php 2a8acb244SAndreas Gohr 3d5ef99ddSAndreas Gohruse dokuwiki\Extension\Plugin; 4d5ef99ddSAndreas Gohruse dokuwiki\plugin\sqlite\SQLiteDB; 5*aab59130SAndreas Gohruse dokuwiki\plugin\statistics\Logger; 6d5ef99ddSAndreas Gohr 76b6f8822SAndreas Gohr/** 86b6f8822SAndreas Gohr * Statistics Plugin 96b6f8822SAndreas Gohr * 106b6f8822SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 116b6f8822SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 126b6f8822SAndreas Gohr */ 13d5ef99ddSAndreas Gohrclass helper_plugin_statistics extends Plugin 14a8acb244SAndreas Gohr{ 15*aab59130SAndreas Gohr protected $dblink; 166b6f8822SAndreas Gohr public $prefix; 17*aab59130SAndreas Gohr protected $oQuery; 18*aab59130SAndreas Gohr protected ?Logger $oLogger; 19*aab59130SAndreas Gohr protected $oGraph; 20*aab59130SAndreas Gohr protected ?SQLiteDB $db; 216b6f8822SAndreas Gohr 226b6f8822SAndreas Gohr /** 23cae4a1c5SAndreas Gohr * Constructor 24cae4a1c5SAndreas Gohr */ 25a8acb244SAndreas Gohr public function __construct() 26a8acb244SAndreas Gohr { 27cae4a1c5SAndreas Gohr } 28cae4a1c5SAndreas Gohr 29cae4a1c5SAndreas Gohr /** 30d5ef99ddSAndreas Gohr * Get SQLiteDB instance 31d5ef99ddSAndreas Gohr * 32d5ef99ddSAndreas Gohr * @return SQLiteDB|null 33d5ef99ddSAndreas Gohr */ 34d5ef99ddSAndreas Gohr public function getDB() 35d5ef99ddSAndreas Gohr { 36d5ef99ddSAndreas Gohr if ($this->db === null) { 37d5ef99ddSAndreas Gohr $this->db = new SQLiteDB('example', DOKU_PLUGIN . 'example/db/'); 38d5ef99ddSAndreas Gohr } 39d5ef99ddSAndreas Gohr return $this->db; 40d5ef99ddSAndreas Gohr } 41d5ef99ddSAndreas Gohr 42d5ef99ddSAndreas Gohr 43d5ef99ddSAndreas Gohr /** 446b6f8822SAndreas Gohr * Return an instance of the query class 456b6f8822SAndreas Gohr * 461664ba1dSAndreas Gohr * @return StatisticsQuery 476b6f8822SAndreas Gohr */ 48a8acb244SAndreas Gohr public function Query() 49a8acb244SAndreas Gohr { 506b6f8822SAndreas Gohr if (is_null($this->oQuery)) { 51a8acb244SAndreas Gohr require __DIR__ . '/inc/StatisticsQuery.class.php'; 526b6f8822SAndreas Gohr $this->oQuery = new StatisticsQuery($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 * 60*aab59130SAndreas 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)) { 66a8acb244SAndreas Gohr require __DIR__ . '/inc/StatisticsLogger.class.php'; 67*aab59130SAndreas Gohr $this->oLogger = new Logger($this); 686b6f8822SAndreas Gohr } 696b6f8822SAndreas Gohr return $this->oLogger; 706b6f8822SAndreas Gohr } 716b6f8822SAndreas Gohr 726b6f8822SAndreas Gohr /** 736b6f8822SAndreas Gohr * Return an instance of the Graph class 746b6f8822SAndreas Gohr * 751664ba1dSAndreas Gohr * @return StatisticsGraph 766b6f8822SAndreas Gohr */ 77a8acb244SAndreas Gohr public function Graph() 78a8acb244SAndreas Gohr { 79b6eece2fSAndreas Gohr $this->prefix = $this->getConf('db_prefix'); 806b6f8822SAndreas Gohr if (is_null($this->oGraph)) { 81a8acb244SAndreas Gohr require __DIR__ . '/inc/StatisticsGraph.class.php'; 826b6f8822SAndreas Gohr $this->oGraph = new StatisticsGraph($this); 836b6f8822SAndreas Gohr } 846b6f8822SAndreas Gohr return $this->oGraph; 856b6f8822SAndreas Gohr } 866b6f8822SAndreas Gohr 876b6f8822SAndreas Gohr /** 886b6f8822SAndreas Gohr * Return a link to the DB, opening the connection if needed 896b6f8822SAndreas Gohr */ 90a8acb244SAndreas Gohr protected function dbLink() 91a8acb244SAndreas Gohr { 926b6f8822SAndreas Gohr // connect to DB if needed 936b6f8822SAndreas Gohr if (!$this->dblink) { 9484864b27SAndreas Gohr if (!$this->getConf('db_server')) return null; 9584864b27SAndreas Gohr 96a242b522Sphl0 $this->dblink = mysqli_connect( 970863c19cSAndreas Gohr $this->getConf('db_server'), 986b6f8822SAndreas Gohr $this->getConf('db_user'), 990863c19cSAndreas Gohr $this->getConf('db_password') 1000863c19cSAndreas Gohr ); 1016b6f8822SAndreas Gohr if (!$this->dblink) { 1026b6f8822SAndreas Gohr msg('DB Error: connection failed', -1); 1036b6f8822SAndreas Gohr return null; 1046b6f8822SAndreas Gohr } 105a242b522Sphl0 if (!mysqli_select_db($this->dblink, $this->getConf('db_database'))) { 1068dfec278SAndreas Gohr msg('DB Error: failed to select database', -1); 1078dfec278SAndreas Gohr return null; 1088dfec278SAndreas Gohr } 1098dfec278SAndreas Gohr 1106b6f8822SAndreas Gohr // set utf-8 111a242b522Sphl0 if (!mysqli_query($this->dblink, 'set names utf8')) { 112a242b522Sphl0 msg('DB Error: could not set UTF-8 (' . mysqli_error($this->dblink) . ')', -1); 1136b6f8822SAndreas Gohr return null; 1146b6f8822SAndreas Gohr } 1156b6f8822SAndreas Gohr } 1166b6f8822SAndreas Gohr return $this->dblink; 1176b6f8822SAndreas Gohr } 1186b6f8822SAndreas Gohr 1196b6f8822SAndreas Gohr /** 1206b6f8822SAndreas Gohr * Simple function to run a DB query 1216b6f8822SAndreas Gohr */ 122a8acb244SAndreas Gohr public function runSQL($sql_string) 123a8acb244SAndreas Gohr { 1246b6f8822SAndreas Gohr $link = $this->dbLink(); 12584864b27SAndreas Gohr if (!$link) return null; 1266b6f8822SAndreas Gohr 127a242b522Sphl0 $result = mysqli_query($link, $sql_string); 128d277cdd5SAndreas Gohr if ($result === false) { 129a242b522Sphl0 dbglog('DB Error: ' . mysqli_error($link) . ' ' . hsc($sql_string), -1); 130a242b522Sphl0 msg('DB Error: ' . mysqli_error($link) . ' ' . hsc($sql_string), -1); 1316b6f8822SAndreas Gohr return null; 1326b6f8822SAndreas Gohr } 1336b6f8822SAndreas Gohr 134a8acb244SAndreas Gohr $resultarray = []; 1356b6f8822SAndreas Gohr 1366b6f8822SAndreas Gohr //mysql_db_query returns 1 on a insert statement -> no need to ask for results 137d277cdd5SAndreas Gohr if ($result !== true) { 138a242b522Sphl0 for ($i = 0; $i < mysqli_num_rows($result); $i++) { 139a242b522Sphl0 $temparray = mysqli_fetch_assoc($result); 1406b6f8822SAndreas Gohr $resultarray[] = $temparray; 1416b6f8822SAndreas Gohr } 142a242b522Sphl0 mysqli_free_result($result); 1436b6f8822SAndreas Gohr } 1446b6f8822SAndreas Gohr 145a242b522Sphl0 if (mysqli_insert_id($link)) { 146a242b522Sphl0 $resultarray = mysqli_insert_id($link); //give back ID on insert 1476b6f8822SAndreas Gohr } 1486b6f8822SAndreas Gohr 1496b6f8822SAndreas Gohr return $resultarray; 1506b6f8822SAndreas Gohr } 1516b6f8822SAndreas Gohr 1524f41a2ccSAndreas Gohr /** 1534f41a2ccSAndreas Gohr * Just send a 1x1 pixel blank gif to the browser 1544f41a2ccSAndreas Gohr * 1554f41a2ccSAndreas Gohr * @called from log.php 1564f41a2ccSAndreas Gohr * 1574f41a2ccSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1584f41a2ccSAndreas Gohr * @author Harry Fuecks <fuecks@gmail.com> 1594f41a2ccSAndreas Gohr */ 160a8acb244SAndreas Gohr public function sendGIF($transparent = true) 161a8acb244SAndreas Gohr { 162259897e1SAndreas Gohr if ($transparent) { 1634f41a2ccSAndreas Gohr $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7'); 164259897e1SAndreas Gohr } else { 165259897e1SAndreas Gohr $img = base64_decode('R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs='); 166259897e1SAndreas Gohr } 1674f41a2ccSAndreas Gohr header('Content-Type: image/gif'); 1684f41a2ccSAndreas Gohr header('Content-Length: ' . strlen($img)); 1694f41a2ccSAndreas Gohr header('Connection: Close'); 170a8acb244SAndreas Gohr echo $img; 1714f41a2ccSAndreas Gohr flush(); 1724f41a2ccSAndreas Gohr // Browser should drop connection after this 1734f41a2ccSAndreas Gohr // Thinks it's got the whole image 1744f41a2ccSAndreas Gohr } 1756b6f8822SAndreas Gohr} 176