Birthday link fixed
[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         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // SQL queries
41 function SQL_QUERY($sql_string, $F, $L) {
42         global $link, $CSS, $_CONFIG, $OK;
43
44         // Remove \t, \n and \r from queries they may confuse some MySQL version I have heard
45         $sql_string = str_replace("\t", " ", str_replace("\n", " ", str_replace("\r", " ", $sql_string)));
46
47         // Run SQL command
48         $result = @mysql_query($sql_string, $link)
49          or ADD_FATAL($F." (".$L."):".mysql_error()."<br />
50 ".MYSQL_QUERY_STRING."<br />
51 ".$sql_string);
52
53          // Count this query
54          if (!isset($_CONFIG['sql_count'])) $_CONFIG['sql_count'] = 0;
55          $_CONFIG['sql_count']++;
56
57          // Debug output
58          //* DEBUG: */ print "Query=<pre>".$sql_string."</pre>, affected=<b>".SQL_AFFECTEDROWS()."</b>, numrows=<b>".SQL_NUMROWS($result)."</b><br />\n";
59
60         if (($CSS != "1") && ($CSS != "-1") && (isBooleanConstantAndTrue('DEBUG_MODE')) && (DEBUG_SQL)) {
61                 //
62                 // Debugging stuff...
63                 //
64                 $fp = @fopen(PATH."debug.log", 'a') or mxchange_die("Cannot write debug.log!");
65                 if (!isset($OK)) {
66                         // Write first entry
67                         fwrite($fp, "Module=".$GLOBALS['module']."\n");
68                         $OK = true;
69                 }
70                 fwrite($fp, $F."(LINE=".$L."|NUM=".SQL_NUMROWS($result)."|AFFECTED=".SQL_AFFECTEDROWS()."): ".str_replace('\r', "", str_replace('\n', " ", $sql_string))."\n");
71                 fclose($fp);
72         }
73
74         // Count DB hits
75         if (!isset($_CONFIG['db_hits'])) {
76                 // Count in dummy variable
77                 $_CONFIG['db_hits'] = 0;
78         } else {
79                 // Count to config array
80                 $_CONFIG['db_hits']++;
81         }
82         return $result;
83 }
84
85 // SQL num rows
86 function SQL_NUMROWS($result) {
87         if ($result != false) {
88                 $lines = @mysql_num_rows($result);
89                 if (empty($lines)) $lines = "0";
90
91         } else {
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         global $link;
101         // $lnk will be ignored for now!
102         $lines = @mysql_affected_rows($link);
103         return $lines;
104 }
105
106 // SQL fetch row
107 function SQL_FETCHROW($result) {
108         $DATA = array();
109         $DATA = @mysql_fetch_row($result);
110         return $DATA;
111 }
112
113 // SQL fetch array
114 function SQL_FETCHARRAY($res=false, $nr=0, $remove_numerical=true) {
115         // Is a result resource set?
116         if (!$res) return false;
117
118         // Initialize array
119         $row = array();
120
121         // Load row from database
122         $row = @mysql_fetch_array($res);
123
124         // Return only arrays here
125         if (is_array($row)) {
126                 // Shall we remove numerical data here automatically?
127                 if ($remove_numerical) {
128                                  // So let's remove all numerical elements to save memory!
129                         $max = count($row);
130                         for ($idx = 0; $idx < ($max / 2); $idx++) {
131                                 // Remove entry
132                                 unset($row[$idx]);
133                         }
134                 }
135
136                 // Return row
137                 return $row;
138         } else {
139                 // Return a false here...
140                 return false;
141         }
142 }
143
144 // SQL result
145 function SQL_RESULT($res, $row, $field) {
146         $result = @mysql_result($res, $row, $field);
147         return $result;
148 }
149 // SQL connect
150 function SQL_CONNECT($host, $login, $password, $F, $L) {
151         $connect = @mysql_connect($host, $login, $password) or ADD_FATAL($F." (".$L."):".mysql_error());
152         return $connect;
153 }
154 // SQL select database
155 function SQL_SELECT_DB($dbName, $link, $F, $L) {
156         $select = false;
157         if (is_resource($link)) {
158                 $select = @mysql_select_db($dbName, $link) or ADD_FATAL($F." (".$L."):".mysql_error());
159         }
160         return $select;
161 }
162 // SQL close link
163 function SQL_CLOSE($link, $F, $L) {
164         global $_CONFIG, $cacheInstance, $cacheArray;
165         if ((GET_EXT_VERSION("cache") >= "0.0.7") && (isset($_CONFIG['db_hits'])) && (isset($_CONFIG['cache_hits'])) && (is_object($cacheInstance))) {
166                 // Update counter for db/cache
167                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_config SET db_hits=%d, cache_hits=%d WHERE config=0 LIMIT 1",
168                         array(bigintval($_CONFIG['db_hits']), bigintval($_CONFIG['cache_hits'])), __FILE__, __LINE__);
169
170                 // Update cache here
171                 if (GET_EXT_VERSION("cache") >= "0.1.2") {
172                         if ($cacheInstance->cache_file("config", true)) {
173                                 // Replace data
174                                 $cacheInstance->cache_replace("cache_hits", $_CONFIG['cache_hits'], "0", $cacheArray);
175                                 $cacheInstance->cache_replace("db_hits"   , $_CONFIG['db_hits']   , "0", $cacheArray);
176                         }
177                 }
178         }
179
180         // Close database link
181         $close = @mysql_close($link) or ADD_FATAL($F." (".$L."):".mysql_error());
182         return $close;
183 }
184 // SQL free result
185 function SQL_FREERESULT($result) {
186         $res = @mysql_free_result($result);
187         return $res;
188 }
189 // SQL string escaping
190 function SQL_QUERY_ESC($qstring, $data, $file, $line, $run=true, $strip=true) {
191         global $link;
192         $eval = "\$query = sprintf(\"".$qstring."\"";
193         foreach ($data as $var) {
194                 if (!empty($var)) {
195                         if ($strip) {
196                                 $eval .= ", SQL_ESCAPE(\"".strip_tags($var)."\")";
197                         } else {
198                                 $eval .= ", SQL_ESCAPE(\"".$var."\")";
199                         }
200                 } else {
201                         $eval .= ", ''";
202                 }
203         }
204         $eval .= ");";
205         //
206         // Debugging
207         //
208         //$fp = fopen(PATH."escape_debug.log", 'a') or mxchange_die("Cannot write debug.log!");
209         //fwrite($fp, $file."(".$line."): ".str_replace('\r', "", str_replace('\n', " ", $eval))."\n");
210         //fclose($fp);
211         eval($eval);
212         if ($run) {
213                 // Run SQL query (default)
214                 return SQL_QUERY($query, $file, $line);
215         } else {
216                 // Return secured string
217                 return $query;
218         }
219 }
220 // Get ID from last INSERT command
221 function SQL_INSERTID() {
222         return @mysql_insert_id();
223 }
224 // Escape a string for the database
225 function SQL_ESCAPE($str) {
226         global $link;
227         if (!is_resource($link)) {
228                 // Fall-back to addslashes() when there is no link
229                 return addslashes($str);
230         }
231
232         if (function_exists('mysql_real_escape_string')) {
233                 // The new and improved version
234                 return mysql_real_escape_string($str, $link);
235         } elseif (function_exists('mysql_escape_string')) {
236                 // The obsulete function
237                 return mysql_escape_string($str, $link);
238         } else {
239                 // If nothing else works
240                 return addslashes($str);
241         }
242 }
243 // SELECT query string from table, columns and so on... ;-)
244 function SQL_RESULT_FROM_ARRAY ($table, $columns, $idRow, $id) {
245         // Prepare the SQL statement
246         $SQL = "SELECT ".implode(", ", $columns)." FROM "._MYSQL_PREFIX."_".$table." WHERE ".$idRow."=%d LIMIT 1";
247
248         // Return the result
249         return SQL_QUERY_ESC($SQL, array(bigintval($id)), __FILE__, __LINE__);
250 }
251 //
252 ?>