1<?php 2 3use dokuwiki\Extension\Plugin; 4use dokuwiki\plugin\sqlite\SQLiteDB; 5use dokuwiki\plugin\statistics\Logger; 6 7/** 8 * Statistics Plugin 9 * 10 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 11 * @author Andreas Gohr <andi@splitbrain.org> 12 */ 13class helper_plugin_statistics extends Plugin 14{ 15 protected $dblink; 16 public $prefix; 17 protected $oQuery; 18 protected ?Logger $oLogger; 19 protected $oGraph; 20 protected ?SQLiteDB $db; 21 22 /** 23 * Constructor 24 */ 25 public function __construct() 26 { 27 } 28 29 /** 30 * Get SQLiteDB instance 31 * 32 * @return SQLiteDB|null 33 */ 34 public function getDB() 35 { 36 if ($this->db === null) { 37 $this->db = new SQLiteDB('example', DOKU_PLUGIN . 'example/db/'); 38 } 39 return $this->db; 40 } 41 42 43 /** 44 * Return an instance of the query class 45 * 46 * @return StatisticsQuery 47 */ 48 public function Query() 49 { 50 if (is_null($this->oQuery)) { 51 require __DIR__ . '/inc/StatisticsQuery.class.php'; 52 $this->oQuery = new StatisticsQuery($this); 53 } 54 return $this->oQuery; 55 } 56 57 /** 58 * Return an instance of the logger class 59 * 60 * @return Logger 61 */ 62 public function Logger() 63 { 64 $this->prefix = $this->getConf('db_prefix'); 65 if (is_null($this->oLogger)) { 66 require __DIR__ . '/inc/StatisticsLogger.class.php'; 67 $this->oLogger = new Logger($this); 68 } 69 return $this->oLogger; 70 } 71 72 /** 73 * Return an instance of the Graph class 74 * 75 * @return StatisticsGraph 76 */ 77 public function Graph() 78 { 79 $this->prefix = $this->getConf('db_prefix'); 80 if (is_null($this->oGraph)) { 81 require __DIR__ . '/inc/StatisticsGraph.class.php'; 82 $this->oGraph = new StatisticsGraph($this); 83 } 84 return $this->oGraph; 85 } 86 87 /** 88 * Return a link to the DB, opening the connection if needed 89 */ 90 protected function dbLink() 91 { 92 // connect to DB if needed 93 if (!$this->dblink) { 94 if (!$this->getConf('db_server')) return null; 95 96 $this->dblink = mysqli_connect( 97 $this->getConf('db_server'), 98 $this->getConf('db_user'), 99 $this->getConf('db_password') 100 ); 101 if (!$this->dblink) { 102 msg('DB Error: connection failed', -1); 103 return null; 104 } 105 if (!mysqli_select_db($this->dblink, $this->getConf('db_database'))) { 106 msg('DB Error: failed to select database', -1); 107 return null; 108 } 109 110 // set utf-8 111 if (!mysqli_query($this->dblink, 'set names utf8')) { 112 msg('DB Error: could not set UTF-8 (' . mysqli_error($this->dblink) . ')', -1); 113 return null; 114 } 115 } 116 return $this->dblink; 117 } 118 119 /** 120 * Simple function to run a DB query 121 */ 122 public function runSQL($sql_string) 123 { 124 $link = $this->dbLink(); 125 if (!$link) return null; 126 127 $result = mysqli_query($link, $sql_string); 128 if ($result === false) { 129 dbglog('DB Error: ' . mysqli_error($link) . ' ' . hsc($sql_string), -1); 130 msg('DB Error: ' . mysqli_error($link) . ' ' . hsc($sql_string), -1); 131 return null; 132 } 133 134 $resultarray = []; 135 136 //mysql_db_query returns 1 on a insert statement -> no need to ask for results 137 if ($result !== true) { 138 for ($i = 0; $i < mysqli_num_rows($result); $i++) { 139 $temparray = mysqli_fetch_assoc($result); 140 $resultarray[] = $temparray; 141 } 142 mysqli_free_result($result); 143 } 144 145 if (mysqli_insert_id($link)) { 146 $resultarray = mysqli_insert_id($link); //give back ID on insert 147 } 148 149 return $resultarray; 150 } 151 152 /** 153 * Just send a 1x1 pixel blank gif to the browser 154 * 155 * @called from log.php 156 * 157 * @author Andreas Gohr <andi@splitbrain.org> 158 * @author Harry Fuecks <fuecks@gmail.com> 159 */ 160 public function sendGIF($transparent = true) 161 { 162 if ($transparent) { 163 $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7'); 164 } else { 165 $img = base64_decode('R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs='); 166 } 167 header('Content-Type: image/gif'); 168 header('Content-Length: ' . strlen($img)); 169 header('Connection: Close'); 170 echo $img; 171 flush(); 172 // Browser should drop connection after this 173 // Thinks it's got the whole image 174 } 175} 176