1 | <?
|
---|
2 |
|
---|
3 | /***************************************************************************
|
---|
4 | * Dolphin Smart Community Builder
|
---|
5 | * -------------------
|
---|
6 | * begin : Mon Mar 23 2006
|
---|
7 | * copyright : (C) 2007 BoonEx Group
|
---|
8 | * website : http://www.boonex.com
|
---|
9 | * This file is part of Dolphin - Smart Community Builder
|
---|
10 | *
|
---|
11 | * Dolphin is free software; you can redistribute it and/or modify it under
|
---|
12 | * the terms of the GNU General Public License as published by the
|
---|
13 | * Free Software Foundation; either version 2 of the
|
---|
14 | * License, or any later version.
|
---|
15 | *
|
---|
16 | * Dolphin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
---|
17 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
18 | * See the GNU General Public License for more details.
|
---|
19 | * You should have received a copy of the GNU General Public License along with Dolphin,
|
---|
20 | * see license.txt file; if not, write to marketing@boonex.com
|
---|
21 | ***************************************************************************/
|
---|
22 |
|
---|
23 | define( 'BX_UPGRADE_DB_FULL_VISUAL_PROCESSING', true );
|
---|
24 | define( 'BX_UPGRADE_DB_FULL_DEBUG_MODE', true );
|
---|
25 |
|
---|
26 | class BxDolUpgradeDb
|
---|
27 | {
|
---|
28 | var $error_checking = true;
|
---|
29 | var $host, $port, $socket, $dbname, $user, $password, $link;
|
---|
30 | var $current_res, $current_arr_type;
|
---|
31 |
|
---|
32 | var $oParams;
|
---|
33 |
|
---|
34 | /*
|
---|
35 | *set database parameters and connect to it
|
---|
36 | */
|
---|
37 | function BxDolUpgradeDb(){
|
---|
38 |
|
---|
39 | $this->host = DATABASE_HOST;
|
---|
40 | $this->port = DATABASE_PORT;
|
---|
41 | $this->socket = DATABASE_SOCK;
|
---|
42 | $this->dbname = DATABASE_NAME;
|
---|
43 | $this->user = DATABASE_USER;
|
---|
44 | $this->password = DATABASE_PASS;
|
---|
45 | $this->current_arr_type = MYSQL_ASSOC;
|
---|
46 |
|
---|
47 | $this->connect();
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * connect to database with appointed parameters
|
---|
52 | */
|
---|
53 | function connect()
|
---|
54 | {
|
---|
55 | $full_host = $this->host;
|
---|
56 | $full_host .= $this->port ? ':'.$this->port : '';
|
---|
57 | $full_host .= $this->socket ? ':'.$this->socket : '';
|
---|
58 |
|
---|
59 | $this->link = @mysql_pconnect($full_host, $this->user, $this->password);
|
---|
60 | if (!$this->link)
|
---|
61 | $this->error('Database connect failed', true);
|
---|
62 |
|
---|
63 | if (!$this->select_db())
|
---|
64 | $this->error('Database select failed', true);
|
---|
65 |
|
---|
66 | $this->res("SET NAMES 'utf8'");
|
---|
67 | $this->res("SET sql_mode = ''");
|
---|
68 | }
|
---|
69 |
|
---|
70 | function select_db()
|
---|
71 | {
|
---|
72 | return @mysql_select_db($this->dbname, $this->link) or $this->error('Cannot complete query (select_db)');
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * close mysql connection
|
---|
77 | */
|
---|
78 | function close()
|
---|
79 | {
|
---|
80 | mysql_close($this->link);
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * execute sql query and return one row result
|
---|
86 | */
|
---|
87 | function getRow($query, $arr_type = MYSQL_ASSOC)
|
---|
88 | {
|
---|
89 | if(!$query)
|
---|
90 | return array();
|
---|
91 | if($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM && $arr_type != MYSQL_BOTH)
|
---|
92 | $arr_type = MYSQL_ASSOC;
|
---|
93 | $res = $this->res ($query);
|
---|
94 | $arr_res = array();
|
---|
95 | if($res && mysql_num_rows($res))
|
---|
96 | {
|
---|
97 | $arr_res = mysql_fetch_array($res, $arr_type);
|
---|
98 | mysql_free_result($res);
|
---|
99 | }
|
---|
100 | return $arr_res;
|
---|
101 | }
|
---|
102 | /**
|
---|
103 | * execute sql query and return a column as result
|
---|
104 | */
|
---|
105 | function getColumn($sQuery) {
|
---|
106 | if(!$sQuery)
|
---|
107 | return array();
|
---|
108 |
|
---|
109 | $rResult = $this->res($sQuery);
|
---|
110 |
|
---|
111 | $aResult = array();
|
---|
112 | if($rResult) {
|
---|
113 | while($aRow = mysql_fetch_array($rResult, MYSQL_NUM))
|
---|
114 | $aResult[] = $aRow[0];
|
---|
115 | mysql_free_result($rResult);
|
---|
116 | }
|
---|
117 | return $aResult;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * execute sql query and return one value result
|
---|
122 | */
|
---|
123 | function getOne($query, $index = 0)
|
---|
124 | {
|
---|
125 | if(!$query)
|
---|
126 | return false;
|
---|
127 | $res = $this->res ($query);
|
---|
128 | $arr_res = array();
|
---|
129 | if($res && mysql_num_rows($res))
|
---|
130 | $arr_res = mysql_fetch_array($res);
|
---|
131 | if(count($arr_res))
|
---|
132 | return $arr_res[$index];
|
---|
133 | else
|
---|
134 | return false;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * execute sql query and return the first row of result
|
---|
139 | * and keep $array type and poiter to all data
|
---|
140 | */
|
---|
141 | function getFirstRow($query, $arr_type = MYSQL_ASSOC)
|
---|
142 | {
|
---|
143 | if(!$query)
|
---|
144 | return array();
|
---|
145 | if($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM)
|
---|
146 | $this->current_arr_type = MYSQL_ASSOC;
|
---|
147 | else
|
---|
148 | $this->current_arr_type = $arr_type;
|
---|
149 | $this->current_res = $this->res ($query);
|
---|
150 | $arr_res = array();
|
---|
151 | if($this->current_res && mysql_num_rows($this->current_res))
|
---|
152 | $arr_res = mysql_fetch_array($this->current_res, $this->current_arr_type);
|
---|
153 | return $arr_res;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * return next row of pointed last getFirstRow calling data
|
---|
158 | */
|
---|
159 | function getNextRow()
|
---|
160 | {
|
---|
161 | $arr_res = mysql_fetch_array($this->current_res, $this->current_arr_type);
|
---|
162 | if($arr_res)
|
---|
163 | return $arr_res;
|
---|
164 | else
|
---|
165 | {
|
---|
166 | mysql_free_result($this->current_res);
|
---|
167 | $this->current_arr_type = MYSQL_ASSOC;
|
---|
168 | return array();
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * return number of affected rows in current mysql result
|
---|
174 | */
|
---|
175 | function getNumRows($res = false)
|
---|
176 | {
|
---|
177 | if ($res)
|
---|
178 | return (int)@mysql_num_rows($res);
|
---|
179 | elseif (!$this->current_res)
|
---|
180 | return (int)@mysql_num_rows($this->current_res);
|
---|
181 | else
|
---|
182 | return 0;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * execute any query return number of rows affected/false
|
---|
187 | */
|
---|
188 | function getAffectedRows()
|
---|
189 | {
|
---|
190 | return mysql_affected_rows($this->link);
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * execute any query return number of rows affected/false
|
---|
195 | */
|
---|
196 | function query($query)
|
---|
197 | {
|
---|
198 | $res = $this->res($query);
|
---|
199 | if($res)
|
---|
200 | return mysql_affected_rows($this->link);
|
---|
201 | return false;
|
---|
202 | }
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * execute any query
|
---|
206 | */
|
---|
207 | function res($query, $error_checking = true)
|
---|
208 | {
|
---|
209 | if(!$query)
|
---|
210 | return false;
|
---|
211 | if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginQuery($query);
|
---|
212 | $res = mysql_query($query, $this->link);
|
---|
213 | if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endQuery($res);
|
---|
214 | if (!$res && $error_checking)
|
---|
215 | $this->error('Database query error', false, $query);
|
---|
216 | return $res;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * execute sql query and return table of records as result
|
---|
221 | */
|
---|
222 | function getAll($query, $arr_type = MYSQL_ASSOC)
|
---|
223 | {
|
---|
224 | if(!$query)
|
---|
225 | return array();
|
---|
226 |
|
---|
227 | if($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM && $arr_type != MYSQL_BOTH)
|
---|
228 | $arr_type = MYSQL_ASSOC;
|
---|
229 |
|
---|
230 | $res = $this->res ($query);
|
---|
231 | $arr_res = array();
|
---|
232 | if($res)
|
---|
233 | {
|
---|
234 | while($row = mysql_fetch_array($res, $arr_type))
|
---|
235 | $arr_res[] = $row;
|
---|
236 | mysql_free_result($res);
|
---|
237 | }
|
---|
238 | return $arr_res;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * execute sql query and return table of records as result
|
---|
243 | */
|
---|
244 | function fillArray($res, $arr_type = MYSQL_ASSOC)
|
---|
245 | {
|
---|
246 | if(!$res)
|
---|
247 | return array();
|
---|
248 |
|
---|
249 | if($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM && $arr_type != MYSQL_BOTH)
|
---|
250 | $arr_type = MYSQL_ASSOC;
|
---|
251 |
|
---|
252 | $arr_res = array();
|
---|
253 | while($row = mysql_fetch_array($res, $arr_type))
|
---|
254 | $arr_res[] = $row;
|
---|
255 | mysql_free_result($res);
|
---|
256 |
|
---|
257 | return $arr_res;
|
---|
258 | }
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * execute sql query and return table of records as result
|
---|
262 | */
|
---|
263 | function getAllWithKey($query, $sFieldKey)
|
---|
264 | {
|
---|
265 | if(!$query)
|
---|
266 | return array();
|
---|
267 |
|
---|
268 | $res = $this->res ($query);
|
---|
269 | $arr_res = array();
|
---|
270 | if($res)
|
---|
271 | {
|
---|
272 | while($row = mysql_fetch_array($res, MYSQL_ASSOC))
|
---|
273 | {
|
---|
274 | $arr_res[$row[$sFieldKey]] = $row;
|
---|
275 | }
|
---|
276 | mysql_free_result($res);
|
---|
277 | }
|
---|
278 | return $arr_res;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * execute sql query and return table of records as result
|
---|
283 | */
|
---|
284 | function getPairs($query, $sFieldKey, $sFieldValue, $arr_type = MYSQL_ASSOC)
|
---|
285 | {
|
---|
286 | if(!$query)
|
---|
287 | return array();
|
---|
288 |
|
---|
289 | $res = $this->res ($query);
|
---|
290 | $arr_res = array();
|
---|
291 | if($res)
|
---|
292 | {
|
---|
293 | while($row = mysql_fetch_array($res, MYSQL_ASSOC))
|
---|
294 | {
|
---|
295 | $arr_res[$row[$sFieldKey]] = $row[$sFieldValue];
|
---|
296 | }
|
---|
297 | mysql_free_result($res);
|
---|
298 | }
|
---|
299 | return $arr_res;
|
---|
300 | }
|
---|
301 |
|
---|
302 | function lastId()
|
---|
303 | {
|
---|
304 | return mysql_insert_id($this->link);
|
---|
305 | }
|
---|
306 |
|
---|
307 | function error($text, $isForceErrorChecking = false, $sSqlQuery = '')
|
---|
308 | {
|
---|
309 | if ($this->error_checking || $isForceErrorChecking)
|
---|
310 | $this->genMySQLErr ($text, $sSqlQuery);
|
---|
311 | else
|
---|
312 | $this->log($text.': '.mysql_error($this->link));
|
---|
313 | }
|
---|
314 |
|
---|
315 | function listTables() {
|
---|
316 | return mysql_list_tables($GLOBALS['db']['db'], $this->link);
|
---|
317 | //return mysql_list_tables($GLOBALS['db']['db'], $this->link) or $this->error('Database get encoding error');
|
---|
318 | }
|
---|
319 |
|
---|
320 | function getEncoding() {
|
---|
321 | return mysql_client_encoding($this->link) or $this->error('Database get encoding error');
|
---|
322 | }
|
---|
323 |
|
---|
324 | function genMySQLErr( $out, $query ='' )
|
---|
325 | {
|
---|
326 | global $site;
|
---|
327 |
|
---|
328 | $aBackTrace = debug_backtrace();
|
---|
329 | unset( $aBackTrace[0] );
|
---|
330 |
|
---|
331 | if( $query )
|
---|
332 | {
|
---|
333 | //try help to find error
|
---|
334 |
|
---|
335 | $aFoundError = array();
|
---|
336 |
|
---|
337 | foreach( $aBackTrace as $aCall )
|
---|
338 | {
|
---|
339 | foreach( $aCall['args'] as $argNum => $argVal )
|
---|
340 | {
|
---|
341 | if( is_string($argVal) and strcmp( $argVal, $query ) == 0 )
|
---|
342 | {
|
---|
343 | $aFoundError['file'] = $aCall['file'];
|
---|
344 | $aFoundError['line'] = $aCall['line'];
|
---|
345 | $aFoundError['function'] = $aCall['function'];
|
---|
346 | $aFoundError['arg'] = $argNum;
|
---|
347 | }
|
---|
348 | }
|
---|
349 | }
|
---|
350 |
|
---|
351 | if( $aFoundError )
|
---|
352 | {
|
---|
353 | $sFoundError = <<<EOJ
|
---|
354 | Found error in the file '<b>{$aFoundError['file']}</b>' at line <b>{$aFoundError['line']}</b>.<br />
|
---|
355 | Called '<b>{$aFoundError['function']}</b>' function with erroneous argument #<b>{$aFoundError['arg']}</b>.<br /><br />
|
---|
356 | EOJ;
|
---|
357 | }
|
---|
358 | }
|
---|
359 |
|
---|
360 |
|
---|
361 | if( BX_UPGRADE_DB_FULL_VISUAL_PROCESSING )
|
---|
362 | {
|
---|
363 | ?>
|
---|
364 | <div style="border:2px solid red;padding:4px;width:600px;margin:0px auto;">
|
---|
365 | <div style="text-align:center;background-color:red;color:white;font-weight:bold;">Error</div>
|
---|
366 | <div style="text-align:center;"><?=$out?></div>
|
---|
367 | <?
|
---|
368 | if( BX_UPGRADE_DB_FULL_DEBUG_MODE )
|
---|
369 | {
|
---|
370 | if( strlen( $query ) )
|
---|
371 | echo "<div><b>Query:</b><br />{$query}</div>";
|
---|
372 |
|
---|
373 | echo '<div><b>Mysql error:</b><br />'.mysql_error($this->link).'</div>';
|
---|
374 | echo '<div style="overflow:scroll;height:300px;border:1px solid gray;">';
|
---|
375 | echo $sFoundError;
|
---|
376 | echo "<b>Debug backtrace:</b><br />";
|
---|
377 | echoDbg( $aBackTrace );
|
---|
378 |
|
---|
379 | echo "<b>Called script:</b> {$_SERVER['PHP_SELF']}<br />";
|
---|
380 | echo "<b>Request parameters:</b><br />";
|
---|
381 | echoDbg( $_REQUEST );
|
---|
382 | echo '</div>';
|
---|
383 | }
|
---|
384 | ?>
|
---|
385 | </div>
|
---|
386 | <?
|
---|
387 | }
|
---|
388 | else
|
---|
389 | echo $out;
|
---|
390 |
|
---|
391 | exit;
|
---|
392 | }
|
---|
393 |
|
---|
394 | function setErrorChecking ($b) {
|
---|
395 | $this->error_checking = $b;
|
---|
396 | }
|
---|
397 |
|
---|
398 | function escape ($s) {
|
---|
399 | return mysql_real_escape_string($s);
|
---|
400 | }
|
---|
401 |
|
---|
402 | function executeSQL($sPath, $aReplace = array (), $isBreakOnError = true) {
|
---|
403 |
|
---|
404 | if(!file_exists($sPath) || !($rHandler = fopen($sPath, "r")))
|
---|
405 | return array ('query' => "fopen($sPath, 'r')", 'error' => 'file not found or permission denied');
|
---|
406 |
|
---|
407 | $sQuery = "";
|
---|
408 | $sDelimiter = ';';
|
---|
409 | $aResult = array();
|
---|
410 | while(!feof($rHandler)) {
|
---|
411 | $sStr = trim(fgets($rHandler));
|
---|
412 |
|
---|
413 | if(empty($sStr) || $sStr[0] == "" || $sStr[0] == "#" || ($sStr[0] == "-" && $sStr[1] == "-"))
|
---|
414 | continue;
|
---|
415 |
|
---|
416 | //--- Change delimiter ---//
|
---|
417 | if(strpos($sStr, "DELIMITER //") !== false || strpos($sStr, "DELIMITER ;") !== false) {
|
---|
418 | $sDelimiter = trim(str_replace('DELIMITER', '', $sStr));
|
---|
419 | continue;
|
---|
420 | }
|
---|
421 |
|
---|
422 | $sQuery .= $sStr;
|
---|
423 |
|
---|
424 | //--- Check for multiline query ---//
|
---|
425 | if(substr($sStr, -strlen($sDelimiter)) != $sDelimiter)
|
---|
426 | continue;
|
---|
427 |
|
---|
428 | //--- Execute query ---//
|
---|
429 | if ($aReplace)
|
---|
430 | $sQuery = str_replace($aReplace['from'], $aReplace['to'], $sQuery);
|
---|
431 | if($sDelimiter != ';')
|
---|
432 | $sQuery = str_replace($sDelimiter, "", $sQuery);
|
---|
433 | $rResult = $this->res(trim($sQuery), false);
|
---|
434 | if(!$rResult) {
|
---|
435 | $aResult[] = array('query' => $sQuery, 'error' => mysql_error($this->link));
|
---|
436 | if ($isBreakOnError)
|
---|
437 | break;
|
---|
438 | }
|
---|
439 |
|
---|
440 | $sQuery = "";
|
---|
441 | }
|
---|
442 | fclose($rHandler);
|
---|
443 |
|
---|
444 | return empty($aResult) ? true : $aResult;
|
---|
445 | }
|
---|
446 | }
|
---|
447 | ?>
|
---|