2 /************************************************************************
3 * MXChange v0.2.1 Start: 08/29/2004 *
4 * =============== Last change: 08/29/2004 *
6 * -------------------------------------------------------------------- *
7 * File : lib-mysql3.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Database layer for MySQL +3.x server *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Datenbankschicht fuer MySQL +3.x Server *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * Needs to be in all Files and every File needs "svn propset *
18 * svn:keywords Date Revision" (autoprobset!) at least!!!!!! *
19 * -------------------------------------------------------------------- *
20 * Copyright (c) 2003 - 2008 by Roland Haeder *
21 * For more information visit: http://www.mxchange.org *
23 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
28 * This program is distributed in the hope that it will be useful, *
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
31 * GNU General Public License for more details. *
33 * You should have received a copy of the GNU General Public License *
34 * along with this program; if not, write to the Free Software *
35 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
37 ************************************************************************/
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
46 function SQL_QUERY ($sql_string, $F, $L) {
48 if (!SQL_IS_LINK_UP()) {
49 // We should not quietly ignore this!
50 trigger_error(sprintf("Cannot query database: sql_string=%s,file=%s,line=%s",
56 // Return 'false' because it has failed
60 // Remove \t, \n and \r from queries they may confuse some MySQL version I have heard
61 $sql_string = str_replace("\t", " ", str_replace("\n", " ", str_replace("\r", " ", $sql_string)));
63 // Replace {!_MYSQL_PREFIX!} with constant, closes #84. Thanks to profi-concept
64 $sql_string = str_replace("{!_MYSQL_PREFIX!}", getConfig('_MYSQL_PREFIX'), $sql_string);
66 // Replace {!_TABLE_TYPE!} with constant
67 $sql_string = str_replace("{!_TABLE_TYPE!}", getConfig('_TABLE_TYPE'), $sql_string);
70 $querytimeBefore = array_sum(explode(' ', microtime()));
73 //* DEBUG: */ print $sql_string."<br />\n";
74 $result = mysql_query($sql_string, SQL_GET_LINK())
75 or addFatalMessage(__FUNCTION__, __LINE__, $F." (".$L."):".mysql_error()."<br />
78 //* DEBUG: */ print __LINE__ . ': numRows=' . SQL_NUMROWS($result) . ',affected=' . SQL_AFFECTEDROWS() . "<br />\n";
81 $querytimeAfter = array_sum(explode(' ', microtime()));
83 // Calculate query time
84 $queryTime = $querytimeAfter - $querytimeBefore;
86 // Save last successfull query
87 setConfigEntry('db_last_query', $sql_string);
90 incrementConfigEntry('sql_count');
93 //* DEBUG: */ print "Query=<pre>".$sql_string."</pre>, affected=<strong>".SQL_AFFECTEDROWS()."</strong>, numrows=<strong>".SQL_NUMROWS($result)."</strong><br />\n";
94 if ((getOutputMode() != '1') && (getOutputMode() != '-1') && (isDebugModeEnabled()) && (getConfig('DEBUG_SQL') == 'Y')) {
98 $fp = fopen(constant('PATH') . 'inc/cache/mysql.log', 'a') or app_die(__FILE__, __LINE__, "Cannot write mysql.log!");
99 if (!isset($GLOBALS['sql_first_entry'])) {
101 fwrite($fp, 'Module=' . getModule() . "\n");
102 $GLOBALS['sql_first_entry'] = true;
104 fwrite($fp, $F."(LINE=".$L."|NUM=".SQL_NUMROWS($result)."|AFFECTED=".SQL_AFFECTEDROWS()."|QUERYTIME:".$queryTime."): ".str_replace("\r", '', str_replace("\n", " ", $sql_string))."\n");
109 if (!isConfigEntrySet('db_hits_run')) {
110 // Count in dummy variable
111 setConfigEntry('db_hits_run', 1);
113 // Count to config array
114 incrementConfigEntry('db_hits_run');
122 function SQL_NUMROWS ($result) {
123 // Is the result a valid resource?
124 if (is_resource($result)) {
125 // Get the count of rows from database
126 $lines = mysql_num_rows($result);
128 // Is the result empty? Then we have an error!
129 if (empty($lines)) $lines = 0;
131 // No resource given, no lines found!
138 function SQL_AFFECTEDROWS() {
139 // Valid link resource?
140 if (!SQL_IS_LINK_UP()) return false;
143 $lines = mysql_affected_rows(SQL_GET_LINK());
150 function SQL_FETCHROW ($result) {
151 // Is a result resource set?
152 if (!is_resource($result)) return false;
154 // Fetch the data and return it
155 return mysql_fetch_row($result);
159 function SQL_FETCHARRAY($res, $nr=0, $remove_numerical=true) {
160 // Is a result resource set?
161 if (!is_resource($res)) return false;
166 // Load row from database
167 $row = mysql_fetch_array($res);
169 // Return only arrays here
170 if (is_array($row)) {
171 // Shall we remove numerical data here automatically?
172 if ($remove_numerical) {
173 // So let's remove all numerical elements to save memory!
175 for ($idx = 0; $idx < ($max / 2); $idx++) {
184 // Return a false here...
190 function SQL_RESULT ($res, $row, $field = 0) {
192 if (!is_resource($res)) return false;
194 // Run the result command and return the result
195 $result = mysql_result($res, $row, $field);
200 function SQL_CONNECT ($host, $login, $password, $F, $L) {
202 $connect = mysql_connect($host, $login, $password) or addFatalMessage(__FUNCTION__, __LINE__, $F." (".$L."):".mysql_error());
204 // Set the link resource
205 SQL_SET_LINK($connect);
208 // SQL select database
209 function SQL_SELECT_DB ($dbName, $F, $L) {
210 // Is there still a valid link? If not, skip it.
211 if (!SQL_IS_LINK_UP()) return false;
214 return mysql_select_db($dbName, SQL_GET_LINK()) or addFatalMessage(__FUNCTION__, __LINE__, $F." (".$L."):".mysql_error());
218 function SQL_CLOSE ($F, $L) {
219 if (!SQL_IS_LINK_UP()) {
224 // Do we need to update cache/db counter?
225 //* DEBUG: */ echo "DB=".getConfig('db_hits').",CACHE=".getConfig('cache_hits')."<br />\n";
226 if ((GET_EXT_VERSION('cache') >= '0.0.7') && (getConfig('db_hits') > 0) && (getConfig('cache_hits') > 0) && (isCacheInstanceValid())) {
228 incrementConfigEntry('db_hits', getConfig('db_hits_run'));
230 // Update counter for db/cache
231 updateConfiguration(array('db_hits', 'cache_hits'), array(getConfig('db_hits'), getConfig('cache_hits')));
234 // Close database link and forget the link
235 $close = mysql_close(SQL_GET_LINK())
236 or addFatalMessage(__FUNCTION__, __LINE__, $F . ' (' . $L . '):'.mysql_error());
246 function SQL_FREERESULT ($result) {
247 if (!is_resource($result)) {
252 $res = mysql_free_result($result);
256 // SQL string escaping
257 function SQL_QUERY_ESC ($qstring, $data, $F, $L, $run=true, $strip=true, $secure=true) {
259 if (!SQL_IS_LINK_UP()) return false;
264 if ($strip === true) {
270 if ($secure === true) {
276 $eval = "\$query = sprintf(\"".$qstring."\"";
277 foreach ($data as $var) {
278 if ((!empty($var)) || ($var === 0)) {
279 $eval .= ", SQL_ESCAPE(\"".$var."\", ".$secure.", ".$strip.")";
288 //* DEBUG: */ $fp = fopen(constant('PATH')."inc/cache/escape_debug.log", 'a') or app_die(__FILE__, __LINE__, "Cannot write debug.log!");
289 //* DEBUG: */ fwrite($fp, $F.'('.$L."): ".str_replace("\r", '', str_replace("\n", " ", $eval))."\n");
290 //* DEBUG: */ fclose($fp);
295 // Was the eval() command fine?
296 if ($query == 'failed') {
297 // Something went wrong?
298 debug_report_bug('eval=' . $eval);
302 // Run SQL query (default)
303 return SQL_QUERY($query, $F, $L);
305 // Return secured string
310 // Get ID from last INSERT command
311 function SQL_INSERTID () {
312 if (!SQL_IS_LINK_UP()) return false;
313 return mysql_insert_id();
316 // Escape a string for the database
317 function SQL_ESCAPE ($str, $secureString=true, $strip=true) {
318 // Secure string first? (which is the default behaviour!)
321 $str = secureString($str, $strip);
324 if (!SQL_IS_LINK_UP()) {
325 // Fall-back to smartAddSlashes() when there is no link
326 return smartAddSlashes($str);
327 } elseif (function_exists('mysql_real_escape_string')) {
328 // The new and improved version
329 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):str={$str}<br />\n";
330 return mysql_real_escape_string($str, SQL_GET_LINK());
331 } elseif (function_exists('mysql_escape_string')) {
332 // The obsolete function
333 return mysql_escape_string($str, SQL_GET_LINK());
335 // If nothing else works, fall back to smartAddSlashes()
336 return smartAddSlashes($str);
340 // SELECT query string from table, columns and so on... ;-)
341 function SQL_RESULT_FROM_ARRAY ($table, $columns, $idRow, $id, $F, $L) {
342 // Is columns an array?
343 if (!is_array($columns)) {
345 trigger_error(sprintf("columns is not array. %s!=array", gettype($columns)));
348 // Prepare the SQL statement
349 $sql = "SELECT `".implode("`,`", $columns)."` FROM `{!_MYSQL_PREFIX!}_%s` WHERE `%s`='%s' LIMIT 1";
352 return SQL_QUERY_ESC($sql,
361 // ALTER TABLE wrapper function
362 function SQL_ALTER_TABLE ($sql, $F, $L) {
363 // This is the default result...
366 // Determine index/fulltext/unique word
369 strpos($sql, 'INDEX') === false
371 strpos($sql, 'FULLTEXT') === false
373 strpos($sql, 'UNIQUE') === false
377 // Extract table name
378 $tableArray = explode(" ", $sql);
379 $tableName = str_replace('`', '', $tableArray[2]);
381 // Shall we add/drop?
382 if (((strpos($sql, 'ADD') !== false) || (strpos($sql, 'DROP') !== false)) && ($noIndex === true)) {
383 // And column name as well
384 $columnName = str_replace('`', '', $tableArray[4]);
386 // Get column information
387 $result = SQL_QUERY_ESC("SHOW COLUMNS FROM `%s` LIKE '%s'",
388 array($tableName, $columnName), __FILE__, __LINE__);
390 // Do we have no entry on ADD or an entry on DROP?
391 // 123 4 4 3 3 4 4 32 23 4 4 3 3 4 4 321
392 if (((SQL_NUMROWS($result) == 0) && (strpos($sql, 'ADD') !== false)) || ((SQL_NUMROWS($result) == 1) && (strpos($sql, 'DROP') !== false))) {
394 //* DEBUG: */ print __LINE__.':'.$sql."<br />\n";
395 $result = SQL_QUERY($sql, $F, $L, false);
397 } elseif ((getConfig('_TABLE_TYPE') == 'InnoDB') && (strpos($sql, 'FULLTEXT') !== false)) {
398 // Skip this query silently
399 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Skipped FULLTEXT: sql=%s,file=%s,line=%s", $sql, $F, $L));
400 } elseif (!$noIndex) {
401 // And column name as well
402 $columnName = str_replace('`', '', $tableArray[4]);
404 // Is this "UNIQUE" or so? FULLTEXT has been handled the elseif() block above
405 if (in_array(strtoupper($columnName), array('INDEX', 'UNIQUE', 'KEY', 'FULLTEXT'))) {
407 $begin = 1; $columnName = ',';
408 while (strpos($columnName, ',') !== false) {
410 $columnName = str_replace('`', '', $tableArray[count($tableArray) - $begin]);
411 //* DEBUG: */ print __LINE__.':'.$columnName."----------------".$begin."<br />\n";
414 $columnName = str_replace('(', '', str_replace(')', '', $columnName));
415 //* DEBUG: */ print __LINE__.':'.$columnName."----------------".$begin."<br />\n";
423 $result = SQL_QUERY_ESC("SHOW INDEX FROM `%s`",
424 array($tableName), __FILE__, __LINE__);
428 while ($content = SQL_FETCHARRAY($result)) {
430 //* DEBUG: */ print "<pre>".print_r($content, true)."</pre>";
431 if (($content['Column_name'] == $columnName) || ($content['Key_name'] == $columnName)) {
433 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Skiped: %s", $sql));
440 SQL_FREERESULT($result);
444 // Send it to the SQL_QUERY() function
445 //* DEBUG: */ print __LINE__.':'.$sql."<br />\n";
446 $result = SQL_QUERY($sql, $F, $L, false);
449 // Other ALTER TABLE query
450 //* DEBUG: */ print __LINE__.':'.$sql."<br />\n";
451 $result = SQL_QUERY($sql, $F, $L, false);
458 // Getter for SQL link
459 function SQL_GET_LINK () {
463 // Is it in the globals?
464 if (isset($GLOBALS['sql_link'])) {
466 $link = $GLOBALS['sql_link'];
474 function SQL_SET_LINK ($link) {
475 // Is this a resource or null?
476 if ((!is_resource($link)) && (!is_null($link))) {
477 // This should never happen!
478 trigger_error(sprintf("link is not resource or null. Type: %s", gettype($link)));
482 $GLOBALS['sql_link'] = $link;
485 // Checks if the link is up
486 function SQL_IS_LINK_UP () {
488 $link = SQL_GET_LINK();
491 return (is_resource($link));