2 /************************************************************************
\r
3 * MXChange v0.2.1 Start: 08/29/2004 *
\r
4 * =============== Last change: 08/29/2004 *
\r
6 * -------------------------------------------------------------------- *
\r
8 * -------------------------------------------------------------------- *
\r
9 * Short description : *
\r
10 * -------------------------------------------------------------------- *
\r
11 * Kurzbeschreibung : *
\r
12 * -------------------------------------------------------------------- *
\r
14 * -------------------------------------------------------------------- *
\r
15 * Copyright (c) 2003 - 2008 by Roland Haeder *
\r
16 * For more information visit: http://www.mxchange.org *
\r
18 * This program is free software; you can redistribute it and/or modify *
\r
19 * it under the terms of the GNU General Public License as published by *
\r
20 * the Free Software Foundation; either version 2 of the License, or *
\r
21 * (at your option) any later version. *
\r
23 * This program is distributed in the hope that it will be useful, *
\r
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
\r
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
\r
26 * GNU General Public License for more details. *
\r
28 * You should have received a copy of the GNU General Public License *
\r
29 * along with this program; if not, write to the Free Software *
\r
30 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
\r
31 * MA 02110-1301 USA *
\r
32 ************************************************************************/
\r
34 // Some security stuff...
\r
35 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
\r
37 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
\r
42 function SQL_QUERY($sql_string, $F, $L, $compile=false)
\r
44 global $link, $CSS, $CONFIG, $OK;
\r
47 $result = @mysql_query($sql_string, $link)
\r
48 or ADD_FATAL($F." (".$L."):".mysql_error()."<BR>
\r
49 ".MYSQL_QUERY_STRING."<BR>
\r
52 if (($CSS != "1") && ($CSS != "-1") && (DEBUG_MODE) && (DEBUG_SQL))
\r
55 // Debugging stuff...
\r
57 $fp = @fopen(PATH."debug.log", 'a') or mxchange_die("Cannot write debug.log!");
\r
59 // Write first entry
\r
60 fwrite($fp, "Module=".$GLOBALS['module']."\n");
\r
63 fwrite($fp, $F."(LINE=".$L."|NUM=".SQL_NUMROWS($result)."|AFFECTED=".SQL_AFFECTEDROWS()."): ".str_replace('\r', '', str_replace('\n', " ", $sql_string))."\n");
\r
68 if (!isset($CONFIG['db_hits']))
\r
70 // Count in dummy variable
\r
71 $CONFIG['db_hits'] = 0;
\r
75 // Count to config array
\r
76 $CONFIG['db_hits']++;
\r
82 function SQL_NUMROWS($result)
\r
84 if ($result != false)
\r
86 $lines = @mysql_num_rows($result);
\r
87 if (empty($lines)) $lines = "0";
\r
92 // No resource given, no lines found!
\r
98 // SQL affected rows
\r
99 function SQL_AFFECTEDROWS($lnk="x", $F="dummy", $L="dummy")
\r
102 // $lnk will be ignored for now!
\r
103 $lines = @mysql_affected_rows($link);
\r
108 function SQL_FETCHROW($result)
\r
111 $DATA = @mysql_fetch_row($result);
\r
116 function SQL_FETCHARRAY($res=false, $nr=0, $remove_numerical=true)
\r
118 // Is a result resource set?
\r
119 if (!$res) return false;
\r
121 // Initialize array
\r
124 // Load row from database
\r
125 $row = @mysql_fetch_array($res);
\r
127 // Return only arrays here
\r
128 if (is_array($row))
\r
130 // Shall we remove numerical data here automatically?
\r
131 if ($remove_numerical)
\r
133 // So let's remove all numerical elements to save memory!
\r
134 $max = count($row);
\r
135 for ($idx = 0; $idx < ($max / 2); $idx++)
\r
147 // Return a false here...
\r
153 function SQL_RESULT($res, $row, $field)
\r
155 $result = @mysql_result($res, $row, $field);
\r
159 function SQL_CONNECT($host, $login, $password, $F, $L)
\r
161 $connect = @mysql_connect($host, $login, $password) or ADD_FATAL($F." (".$L."):".mysql_error());
\r
164 // SQL select database
\r
165 function SQL_SELECT_DB($DB, $link, $F, $L)
\r
167 $DB = @mysql_select_db($DB, $link) or ADD_FATAL($F." (".$L."):".mysql_error());
\r
171 function SQL_CLOSE($link, $F, $L)
\r
173 global $CONFIG, $CACHE, $CFG_CACHE;
\r
174 if ((GET_EXT_VERSION("cache") >= "0.0.7") && (isset($CONFIG['db_hits'])) && (isset($CONFIG['cache_hits'])) && (is_object($CACHE)))
\r
176 // Update counter for db/cache
\r
177 $result = SQL_QUERY("UPDATE "._MYSQL_PREFIX."_config SET db_hits='".$CONFIG['db_hits']."', cache_hits='".$CONFIG['cache_hits']."' WHERE config='0' LIMIT 1", __FILE__, __LINE__);
\r
179 // Update cache here
\r
180 if (GET_EXT_VERSION("cache") >= "0.1.2")
\r
182 if ($CACHE->cache_file("config", true))
\r
185 $CACHE->cache_replace("cache_hits", $CONFIG['cache_hits'], "0", $CFG_CACHE);
\r
186 $CACHE->cache_replace("db_hits" , $CONFIG['db_hits'] , "0", $CFG_CACHE);
\r
191 // Close database link
\r
192 $close = @mysql_close($link) or ADD_FATAL($F." (".$L."):".mysql_error());
\r
196 function SQL_FREERESULT($result)
\r
198 $res = @mysql_free_result($result);
\r
201 // SQL string escaping
\r
202 function SQL_QUERY_ESC($qstring, $data, $file, $line, $run=true, $strip=true)
\r
205 $eval = "\$query = sprintf(\"".$qstring."\"";
\r
206 foreach ($data as $var)
\r
211 $eval .= ", SQL_ESCAPE(\"".strip_tags($var)."\")";
\r
213 $eval .= ", SQL_ESCAPE(\"".$var."\")";
\r
225 //$fp = fopen(PATH."escape_debug.log", 'a') or mxchange_die("Cannot write debug.log!");
\r
226 //fwrite($fp, $file."(".$line."): ".str_replace('\r', '', str_replace('\n', " ", $eval))."\n");
\r
231 // Run SQL query (default)
\r
232 return SQL_QUERY($query, $file, $line);
\r
236 // Return secured string
\r
240 // Get ID from last INSERT command
\r
241 function SQL_INSERTID()
\r
243 return @mysql_insert_id();
\r
245 // Escape a string for the database
\r
246 function SQL_ESCAPE($str)
\r
249 if (!is_resource($link)) {
\r
250 // Fall-back to addslashes() when there is no link
\r
251 return addslashes($str);
\r
254 if (function_exists('mysql_real_escape_string')) {
\r
255 // The new and improved version
\r
256 return mysql_real_escape_string($str, $link);
\r
257 } elseif (function_exists('mysql_escape_string')) {
\r
258 // The obsulete function
\r
259 return mysql_escape_string($str, $link);
\r
261 // If nothing else works
\r
262 return addslashes($str);
\r
265 // SELECT query string from table, columns and so on... ;-)
\r
266 function SQL_RESULT_FROM_ARRAY ($table, array $columns, $idRow, $id) {
\r
267 // Prepare the SQL statement
\r
268 $SQL = "SELECT ".implode(", ", $columns)." FROM "._MYSQL_PREFIX."_".$table." WHERE ".$idRow."=%d LIMIT 1";
\r
270 // Return the result
\r
271 return SQL_QUERY_ESC($SQL, array(bigintval($id)), __FILE__, __LINE__);
\r