· 8 years ago · Dec 26, 2017, 02:24 AM
1<?php
2/*
3HLstatsX Community Edition - Real-time player and clan rankings and statistics
4Copyleft (L) 2008-20XX Nicholas Hastings (nshastings@gmail.com)
5http://www.hlxcommunity.com
6
7HLstatsX Community Edition is a continuation of
8ELstatsNEO - Real-time player and clan rankings and statistics
9Copyleft (L) 2008-20XX Malte Bayer (steam@neo-soft.org)
10http://ovrsized.neo-soft.org/
11
12ELstatsNEO is an very improved & enhanced - so called Ultra-Humongus Edition of HLstatsX
13HLstatsX - Real-time player and clan rankings and statistics for Half-Life 2
14http://www.hlstatsx.com/
15Copyright (C) 2005-2007 Tobias Oetzel (Tobi@hlstatsx.com)
16
17HLstatsX is an enhanced version of HLstats made by Simon Garner
18HLstats - Real-time player and clan rankings and statistics for Half-Life
19http://sourceforge.net/projects/hlstats/
20Copyright (C) 2001 Simon Garner
21
22This program is free software; you can redistribute it and/or
23modify it under the terms of the GNU General Public License
24as published by the Free Software Foundation; either version 2
25of the License, or (at your option) any later version.
26
27This program is distributed in the hope that it will be useful,
28but WITHOUT ANY WARRANTY; without even the implied warranty of
29MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30GNU General Public License for more details.
31
32You should have received a copy of the GNU General Public License
33along with this program; if not, write to the Free Software
34Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35
36For support and installation notes visit http://www.hlxcommunity.com
37*/
38
39
40/* Profile support:
41
42To see SQL run counts and run times, set the $profile variable below to something
43that evaluates as true.
44
45Add the following table to your database:
46CREATE TABLE IF NOT EXISTS `hlstats_sql_web_profile` (
47`queryid` int(11) NOT NULL AUTO_INCREMENT,
48`source` tinytext NOT NULL,
49`run_count` int(11) NOT NULL,
50`run_time` float NOT NULL,
51PRIMARY KEY (`queryid`),
52UNIQUE KEY `source` (`source`(64))
53) ENGINE=MyISAM;
54*/
55
56if ( !defined('IN_HLSTATS') ) { die('Do not access this file directly.'); }
57
58class DB_mysql
59{
60 var $db_addr;
61 var $db_user;
62 var $db_pass;
63 var $db_name;
64
65 var $link;
66 var $last_result;
67 var $last_query;
68 var $last_insert_id;
69 var $profile = 0;
70 var $querycount = 0;
71 var $last_calc_rows = 0;
72
73 function DB_mysql($db_addr, $db_user, $db_pass, $db_name, $use_pconnect = false)
74 {
75 $this->db_addr = $db_addr;
76 $this->db_user = $db_user;
77 $this->db_pass = $db_pass;
78
79 $this->querycount = 0;
80
81 if ( $use_pconnect )
82 {
83 $this->link = @mysqli_connect("p:".$db_addr, $db_user, $db_pass);
84 }
85 else
86 {
87 $this->link = @mysqli_connect($db_addr, $db_user, $db_pass);
88 }
89
90 if ( $this->link )
91 {
92 $q = @mysqli_query($this->link, "SET NAMES 'utf8'");
93 @mysqli_free_result($q);
94 if ( $db_name != '' )
95 {
96 $this->db_name = $db_name;
97 if ( !@mysqli_select_db($this->link, $db_name) )
98 {
99 @mysqli_close($this->db_connect_id);
100 $this->error("Could not select database '$db_name'. Check that the value of DB_NAME in config.php is set correctly.");
101 }
102 }
103 return $this->link;
104 }
105 else
106 {
107 $this->error('Could not connect to database server. Check that the values of DB_ADDR, DB_USER and DB_PASS in config.php are set correctly.');
108 }
109 }
110
111 function data_seek($row_number, $query_id = 0)
112 {
113 if ( !$query_id )
114 {
115 $result = $this->last_result;
116 }
117 if ( $query_id )
118 {
119 return @mysqli_data_seek($query_id, $row_number);
120 }
121 return false;
122 }
123
124 function fetch_array($query_id = 0)
125 {
126 if ( !$query_id )
127 {
128 $query_id = $this->last_result;
129 }
130 if ( $query_id )
131 {
132 return @mysqli_fetch_array($query_id);
133 }
134 return false;
135 }
136
137 function fetch_row($query_id = 0)
138 {
139 if ( !$query_id )
140 {
141 $query_id = $this->last_result;
142 }
143 if ( $query_id )
144 {
145 return @mysqli_fetch_row($query_id);
146 }
147 return false;
148 }
149
150 function fetch_row_set($query_id = 0)
151 {
152 if ( !$query_id )
153 {
154 $query_id = $this->last_result;
155 }
156 if ( $query_id )
157 {
158 $rowset = array();
159 while ( $row = $this->fetch_array($query_id) )
160 $rowset[] = $row;
161
162 return $rowset;
163 }
164 return false;
165 }
166
167 function free_result($query_id = 0)
168 {
169 if ( !$query_id )
170 {
171 $query_id = $this->last_result;
172 }
173 if ( $query_id )
174 {
175 return @mysqli_free_result($query_id);
176 }
177 return false;
178 }
179
180 function insert_id()
181 {
182 return $this->last_insert_id;
183 }
184
185 function num_rows($query_id = 0)
186 {
187 if ( !$query_id )
188 {
189 $query_id = $this->last_result;
190 }
191 if ( $query_id )
192 {
193 return @mysqli_num_rows($query_id);
194 }
195 return false;
196 }
197
198 function calc_rows()
199 {
200 return $this->last_calc_rows;
201 }
202
203 function query($query, $showerror=true, $calcrows=false)
204 {
205 $this->last_query = $query;
206 $starttime = microtime(true);
207 if($calcrows == true)
208 {
209 /* Add sql_calc_found_rows to this query */
210 $query = preg_replace('/select/i', 'select sql_calc_found_rows', $query, 1);
211 }
212 $this->last_result = @mysqli_query($this->link, $query);
213 $endtime = microtime(true);
214
215 $this->last_insert_id = @mysqli_insert_id($this->link);
216
217 if($calcrows == true)
218 {
219 $calc_result = @mysqli_query("select found_rows() as rowcount");
220 if($row = mysqli_fetch_assoc($calc_result))
221 {
222 $this->last_calc_rows = $row['rowcount'];
223 }
224 }
225
226 $this->querycount++;
227
228 if ( defined('DB_DEBUG') && DB_DEBUG == true )
229 {
230 echo "<p><pre>$query</pre><hr /></p>";
231 }
232
233 if ( $this->last_result )
234 {
235 if($this->profile)
236 {
237 $backtrace = debug_backtrace();
238 $profilequery = "insert into hlstats_sql_web_profile (source, run_count, run_time) values ".
239 "('".basename($backtrace[0]['file']).':'.$backtrace[0]['line']."',1,'".($endtime-$starttime)."')"
240 ."ON DUPLICATE KEY UPDATE run_count = run_count+1, run_time=run_time+".($endtime-$starttime);
241 @mysqli_query($this->link, $profilequery);
242 }
243 return $this->last_result;
244 }
245 else
246 {
247 if ($showerror)
248 {
249 $this->error('Bad query.');
250 }
251 else
252 {
253 return false;
254 }
255 }
256 }
257
258 function result($row, $field, $query_id = 0)
259 {
260 if ( !$query_id )
261 {
262 $query_id = $this->last_result;
263 }
264 if ( $query_id )
265 {
266 return @mysqli_result($query_id, $row, $field);
267 }
268 return false;
269 }
270
271 function escape($string)
272 {
273 if ( $this->link )
274 {
275 return @mysqli_real_escape_string($this->link, $string);
276 }
277 else
278 {
279 return @mysqli_real_escape_string($string);
280 }
281 }
282
283 function error($message, $exit=true)
284 {
285 error(
286 "<b>Database Error</b><br />\n<br />\n" .
287 "<i>Server Address:</i> $this->db_addr<br />\n" .
288 "<i>Server Username:</i> $this->db_user<br /><br />\n" .
289 "<i>Error Diagnostic:</i><br />\n$message<br /><br />\n" .
290 "<i>Server Error:</i> (" . @mysqli_errno() . ") " . @mysqli_error() . "<br /><br />\n" .
291 "<i>Last SQL Query:</i><br />\n<pre style=\"font-size:2px;\">$this->last_query</pre>",
292 $exit
293 );
294 }
295}
296?>