3c8d2fbb6d9a91a553e732d45a61ceed76c70536
[mailer.git] / 0.2.1 / inc / db / lib-mysql3.php
1 <?php\r
2 /************************************************************************\r
3  * MXChange v0.2.1                                    Start: 08/29/2004 *\r
4  * ===============                              Last change: 08/29/2004 *\r
5  *                                                                      *\r
6  * -------------------------------------------------------------------- *\r
7  * File              : lib.php                                          *\r
8  * -------------------------------------------------------------------- *\r
9  * Short description :                                                  *\r
10  * -------------------------------------------------------------------- *\r
11  * Kurzbeschreibung  :                                                  *\r
12  * -------------------------------------------------------------------- *\r
13  *                                                                      *\r
14  * -------------------------------------------------------------------- *\r
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *\r
16  * For more information visit: http://www.mxchange.org                  *\r
17  *                                                                      *\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
22  *                                                                      *\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
27  *                                                                      *\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
33 \r
34 // Some security stuff...\r
35 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))\r
36 {\r
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4)."/security.php";\r
38         require($INC);\r
39 }\r
40 \r
41 // SQL queries\r
42 function SQL_QUERY($sql_string, $F, $L, $compile=false)\r
43 {\r
44         global $link, $CSS, $CONFIG, $OK;\r
45 \r
46         // Run SQL command\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
50 ".$sql_string);\r
51 \r
52         if (($CSS != "1") && ($CSS != "-1") && (DEBUG_MODE) && (DEBUG_SQL))\r
53         {\r
54                 //\r
55                 // Debugging stuff...\r
56                 //\r
57                 $fp = @fopen(PATH."debug.log", 'a') or mxchange_die("Cannot write debug.log!");\r
58                 if (!isset($OK)) {\r
59                         // Write first entry\r
60                         fwrite($fp, "Module=".$GLOBALS['module']."\n");\r
61                         $OK = true;\r
62                 }\r
63                 fwrite($fp, $F."(LINE=".$L."|NUM=".SQL_NUMROWS($result)."|AFFECTED=".SQL_AFFECTEDROWS()."): ".str_replace('\r', '', str_replace('\n', " ", $sql_string))."\n");\r
64                 fclose($fp);\r
65         }\r
66 \r
67         // Count DB hits\r
68         if (!isset($CONFIG['db_hits']))\r
69         {\r
70                 // Count in dummy variable\r
71                 $CONFIG['db_hits'] = 0;\r
72         }\r
73          else\r
74         {\r
75                 // Count to config array\r
76                 $CONFIG['db_hits']++;\r
77         }\r
78         return $result;\r
79 }\r
80 \r
81 // SQL num rows\r
82 function SQL_NUMROWS($result)\r
83 {\r
84         if ($result != false)\r
85         {\r
86                 $lines = @mysql_num_rows($result);\r
87                 if (empty($lines)) $lines = "0";\r
88 \r
89         }\r
90          else\r
91         {\r
92                 // No resource given, no lines found!\r
93                 $lines = "0";\r
94         }\r
95         return $lines;\r
96 }\r
97 \r
98 // SQL affected rows\r
99 function SQL_AFFECTEDROWS($lnk="x", $F="dummy", $L="dummy")\r
100 {\r
101         global $link;\r
102         // $lnk will be ignored for now!\r
103         $lines = @mysql_affected_rows($link);\r
104         return $lines;\r
105 }\r
106 \r
107 // SQL fetch row\r
108 function SQL_FETCHROW($result)\r
109 {\r
110         $DATA = array();\r
111         $DATA = @mysql_fetch_row($result);\r
112         return $DATA;\r
113 }\r
114 \r
115 // SQL fetch array\r
116 function SQL_FETCHARRAY($res=false, $nr=0, $remove_numerical=true)\r
117 {\r
118         // Is a result resource set?\r
119         if (!$res) return false;\r
120 \r
121         // Initialize array\r
122         $row = array();\r
123 \r
124         // Load row from database\r
125         $row = @mysql_fetch_array($res);\r
126 \r
127         // Return only arrays here\r
128         if (is_array($row))\r
129         {\r
130                 // Shall we remove numerical data here automatically?\r
131                 if ($remove_numerical)\r
132                 {\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
136                         {\r
137                                 // Remove entry\r
138                                 unset($row[$idx]);\r
139                         }\r
140                 }\r
141 \r
142                 // Return row\r
143                 return $row;\r
144         }\r
145          else\r
146         {\r
147                 // Return a false here...\r
148                 return false;\r
149         }\r
150 }\r
151 \r
152 // SQL result\r
153 function SQL_RESULT($res, $row, $field)\r
154 {\r
155         $result = @mysql_result($res, $row, $field);\r
156         return $result;\r
157 }\r
158 // SQL connect\r
159 function SQL_CONNECT($host, $login, $password, $F, $L)\r
160 {\r
161         $connect = @mysql_connect($host, $login, $password) or ADD_FATAL($F." (".$L."):".mysql_error());\r
162         return $connect;\r
163 }\r
164 // SQL select database\r
165 function SQL_SELECT_DB($DB, $link, $F, $L)\r
166 {\r
167         $DB = @mysql_select_db($DB, $link) or ADD_FATAL($F." (".$L."):".mysql_error());\r
168         return $DB;\r
169 }\r
170 // SQL close link\r
171 function SQL_CLOSE($link, $F, $L)\r
172 {\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
175         {\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
178 \r
179                 // Update cache here\r
180                 if (GET_EXT_VERSION("cache") >= "0.1.2")\r
181                 {\r
182                         if ($CACHE->cache_file("config", true))\r
183                         {\r
184                                 // Replace data\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
187                         }\r
188                 }\r
189         }\r
190 \r
191         // Close database link\r
192         $close = @mysql_close($link) or ADD_FATAL($F." (".$L."):".mysql_error());\r
193         return $close;\r
194 }\r
195 // SQL free result\r
196 function SQL_FREERESULT($result)\r
197 {\r
198         $res = @mysql_free_result($result);\r
199         return $res;\r
200 }\r
201 // SQL string escaping\r
202 function SQL_QUERY_ESC($qstring, $data, $file, $line, $run=true, $strip=true)\r
203 {\r
204         global $link;\r
205         $eval = "\$query = sprintf(\"".$qstring."\"";\r
206         foreach ($data as $var)\r
207         {\r
208                 if (!empty($var))\r
209                 {\r
210                         if ($strip) {\r
211                                 $eval .= ", SQL_ESCAPE(\"".strip_tags($var)."\")";\r
212                         } else {\r
213                                 $eval .= ", SQL_ESCAPE(\"".$var."\")";\r
214                         }\r
215                 }\r
216                  else\r
217                 {\r
218                         $eval .= ", ''";\r
219                 }\r
220         }\r
221         $eval .= ");";\r
222         //\r
223         // Debugging\r
224         //\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
227         //fclose($fp);\r
228         eval($eval);\r
229         if ($run)\r
230         {\r
231                 // Run SQL query (default)\r
232                 return SQL_QUERY($query, $file, $line);\r
233         }\r
234          else\r
235         {\r
236                 // Return secured string\r
237                 return $query;\r
238         }\r
239 }\r
240 // Get ID from last INSERT command\r
241 function SQL_INSERTID()\r
242 {\r
243         return @mysql_insert_id();\r
244 }\r
245 // Escape a string for the database\r
246 function SQL_ESCAPE($str)\r
247 {\r
248         global $link;\r
249         if (!is_resource($link)) {\r
250                 // Fall-back to addslashes() when there is no link\r
251                 return addslashes($str);\r
252         }\r
253 \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
260         } else {\r
261                 // If nothing else works\r
262                 return addslashes($str);\r
263         }\r
264 }\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
269 \r
270         // Return the result\r
271         return SQL_QUERY_ESC($SQL, array(bigintval($id)), __FILE__, __LINE__);\r
272 }\r
273 //\r
274 ?>\r