query debug output removed
[mailer.git] / inc / db / lib-mysql3.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 08/29/2004 *
4  * ===============                              Last change: 08/29/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : lib.php                                          *
8  * -------------------------------------------------------------------- *
9  * Short description :                                                  *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  :                                                  *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
36 {
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
38         require($INC);
39 }
40
41 // SQL queries
42 function SQL_QUERY($sql_string, $F, $L, $compile=false)
43 {
44         global $link, $CSS, $CONFIG, $OK;
45
46         // Run SQL command
47         $result = @mysql_query($sql_string, $link)
48          or ADD_FATAL($F." (".$L."):".mysql_error()."<BR>
49 ".MYSQL_QUERY_STRING."<BR>
50 ".$sql_string);
51
52         if (($CSS != "1") && ($CSS != "-1") && (DEBUG_MODE) && (DEBUG_SQL))
53         {
54                 //
55                 // Debugging stuff...
56                 //
57                 $fp = @fopen(PATH."debug.log", 'a') or mxchange_die("Cannot write debug.log!");
58                 if (!isset($OK)) {
59                         // Write first entry
60                         fwrite($fp, "Module=".$GLOBALS['module']."\n");
61                         $OK = true;
62                 }
63                 fwrite($fp, $F."(LINE=".$L."|NUM=".SQL_NUMROWS($result)."|AFFECTED=".SQL_AFFECTEDROWS()."): ".str_replace('\r', '', str_replace('\n', " ", $sql_string))."\n");
64                 fclose($fp);
65         }
66
67         // Count DB hits
68         if (!isset($CONFIG['db_hits']))
69         {
70                 // Count in dummy variable
71                 $CONFIG['db_hits'] = 0;
72         }
73          else
74         {
75                 // Count to config array
76                 $CONFIG['db_hits']++;
77         }
78         return $result;
79 }
80
81 // SQL num rows
82 function SQL_NUMROWS($result)
83 {
84         if ($result != false)
85         {
86                 $lines = @mysql_num_rows($result);
87                 if (empty($lines)) $lines = "0";
88
89         }
90          else
91         {
92                 // No resource given, no lines found!
93                 $lines = "0";
94         }
95         return $lines;
96 }
97
98 // SQL affected rows
99 function SQL_AFFECTEDROWS($lnk="x", $F="dummy", $L="dummy")
100 {
101         global $link;
102         // $lnk will be ignored for now!
103         $lines = @mysql_affected_rows($link);
104         return $lines;
105 }
106
107 // SQL fetch row
108 function SQL_FETCHROW($result)
109 {
110         $DATA = array();
111         $DATA = @mysql_fetch_row($result);
112         return $DATA;
113 }
114
115 // SQL fetch array
116 function SQL_FETCHARRAY($res=false, $nr=0, $remove_numerical=true)
117 {
118         // Is a result resource set?
119         if (!$res) return false;
120
121         // Initialize array
122         $row = array();
123
124         // Load row from database
125         $row = @mysql_fetch_array($res);
126
127         // Return only arrays here
128         if (is_array($row))
129         {
130                 // Shall we remove numerical data here automatically?
131                 if ($remove_numerical)
132                 {
133                         // So let's remove all numerical elements to save memory!
134                         $max = count($row);
135                         for ($idx = 0; $idx < ($max / 2); $idx++)
136                         {
137                                 // Remove entry
138                                 unset($row[$idx]);
139                         }
140                 }
141
142                 // Return row
143                 return $row;
144         }
145          else
146         {
147                 // Return a false here...
148                 return false;
149         }
150 }
151
152 // SQL result
153 function SQL_RESULT($res, $row, $field)
154 {
155         $result = @mysql_result($res, $row, $field);
156         return $result;
157 }
158 // SQL connect
159 function SQL_CONNECT($host, $login, $password, $F, $L)
160 {
161         $connect = @mysql_connect($host, $login, $password) or ADD_FATAL($F." (".$L."):".mysql_error());
162         return $connect;
163 }
164 // SQL select database
165 function SQL_SELECT_DB($DB, $link, $F, $L)
166 {
167         $DB = @mysql_select_db($DB, $link) or ADD_FATAL($F." (".$L."):".mysql_error());
168         return $DB;
169 }
170 // SQL close link
171 function SQL_CLOSE($link, $F, $L)
172 {
173         global $CONFIG, $CACHE, $CFG_CACHE;
174         if ((GET_EXT_VERSION("cache") >= "0.0.7") && (isset($CONFIG['db_hits'])) && (isset($CONFIG['cache_hits'])) && (is_object($CACHE)))
175         {
176                 // Update counter for db/cache
177                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_config SET db_hits=%d, cache_hits=%d WHERE config='0' LIMIT 1",
178                         array(bigintval($CONFIG['db_hits']), bigintval($CONFIG['cache_hits'])), __FILE__, __LINE__);
179
180                 // Update cache here
181                 if (GET_EXT_VERSION("cache") >= "0.1.2")
182                 {
183                         if ($CACHE->cache_file("config", true))
184                         {
185                                 // Replace data
186                                 $CACHE->cache_replace("cache_hits", $CONFIG['cache_hits'], "0", $CFG_CACHE);
187                                 $CACHE->cache_replace("db_hits"   , $CONFIG['db_hits']   , "0", $CFG_CACHE);
188                         }
189                 }
190         }
191
192         // Close database link
193         $close = @mysql_close($link) or ADD_FATAL($F." (".$L."):".mysql_error());
194         return $close;
195 }
196 // SQL free result
197 function SQL_FREERESULT($result)
198 {
199         $res = @mysql_free_result($result);
200         return $res;
201 }
202 // SQL string escaping
203 function SQL_QUERY_ESC($qstring, $data, $file, $line, $run=true, $strip=true)
204 {
205         global $link;
206         $eval = "\$query = sprintf(\"".$qstring."\"";
207         foreach ($data as $var)
208         {
209                 if (!empty($var))
210                 {
211                         if ($strip) {
212                                 $eval .= ", SQL_ESCAPE(\"".strip_tags($var)."\")";
213                         } else {
214                                 $eval .= ", SQL_ESCAPE(\"".$var."\")";
215                         }
216                 }
217                  else
218                 {
219                         $eval .= ", ''";
220                 }
221         }
222         $eval .= ");";
223         //
224         // Debugging
225         //
226         //$fp = fopen(PATH."escape_debug.log", 'a') or mxchange_die("Cannot write debug.log!");
227         //fwrite($fp, $file."(".$line."): ".str_replace('\r', '', str_replace('\n', " ", $eval))."\n");
228         //fclose($fp);
229         eval($eval);
230         if ($run)
231         {
232                 // Run SQL query (default)
233                 return SQL_QUERY($query, $file, $line);
234         }
235          else
236         {
237                 // Return secured string
238                 return $query;
239         }
240 }
241 // Get ID from last INSERT command
242 function SQL_INSERTID()
243 {
244         return @mysql_insert_id();
245 }
246 // Escape a string for the database
247 function SQL_ESCAPE($str)
248 {
249         global $link;
250         if (!is_resource($link)) {
251                 // Fall-back to addslashes() when there is no link
252                 return addslashes($str);
253         }
254
255         if (function_exists('mysql_real_escape_string')) {
256                 // The new and improved version
257                 return mysql_real_escape_string($str, $link);
258         } elseif (function_exists('mysql_escape_string')) {
259                 // The obsulete function
260                 return mysql_escape_string($str, $link);
261         } else {
262                 // If nothing else works
263                 return addslashes($str);
264         }
265 }
266 // SELECT query string from table, columns and so on... ;-)
267 function SQL_RESULT_FROM_ARRAY ($table, $columns, $idRow, $id) {
268         // Prepare the SQL statement
269         $SQL = "SELECT ".implode(", ", $columns)." FROM "._MYSQL_PREFIX."_".$table." WHERE ".$idRow."=%d LIMIT 1";
270
271         // Return the result
272         return SQL_QUERY_ESC($SQL, array(bigintval($id)), __FILE__, __LINE__);
273 }
274 //
275 ?>