webmaster@1
|
1 <?php |
webmaster@1
|
2 // $Id: database.mysql.inc,v 1.89 2008/01/24 10:46:54 goba Exp $ |
webmaster@1
|
3 |
webmaster@1
|
4 /** |
webmaster@1
|
5 * @file |
webmaster@1
|
6 * Database interface code for MySQL database servers. |
webmaster@1
|
7 */ |
webmaster@1
|
8 |
webmaster@1
|
9 /** |
webmaster@1
|
10 * @ingroup database |
webmaster@1
|
11 * @{ |
webmaster@1
|
12 */ |
webmaster@1
|
13 |
webmaster@1
|
14 // Include functions shared between mysql and mysqli. |
webmaster@1
|
15 require_once './includes/database.mysql-common.inc'; |
webmaster@1
|
16 |
webmaster@1
|
17 /** |
webmaster@1
|
18 * Report database status. |
webmaster@1
|
19 */ |
webmaster@1
|
20 function db_status_report($phase) { |
webmaster@1
|
21 $t = get_t(); |
webmaster@1
|
22 |
webmaster@1
|
23 $version = db_version(); |
webmaster@1
|
24 |
webmaster@1
|
25 $form['mysql'] = array( |
webmaster@1
|
26 'title' => $t('MySQL database'), |
webmaster@1
|
27 'value' => ($phase == 'runtime') ? l($version, 'admin/reports/status/sql') : $version, |
webmaster@1
|
28 ); |
webmaster@1
|
29 |
webmaster@1
|
30 if (version_compare($version, DRUPAL_MINIMUM_MYSQL) < 0) { |
webmaster@1
|
31 $form['mysql']['severity'] = REQUIREMENT_ERROR; |
webmaster@1
|
32 $form['mysql']['description'] = $t('Your MySQL Server is too old. Drupal requires at least MySQL %version.', array('%version' => DRUPAL_MINIMUM_MYSQL)); |
webmaster@1
|
33 } |
webmaster@1
|
34 |
webmaster@1
|
35 return $form; |
webmaster@1
|
36 } |
webmaster@1
|
37 |
webmaster@1
|
38 /** |
webmaster@1
|
39 * Returns the version of the database server currently in use. |
webmaster@1
|
40 * |
webmaster@1
|
41 * @return Database server version |
webmaster@1
|
42 */ |
webmaster@1
|
43 function db_version() { |
webmaster@1
|
44 list($version) = explode('-', mysql_get_server_info()); |
webmaster@1
|
45 return $version; |
webmaster@1
|
46 } |
webmaster@1
|
47 |
webmaster@1
|
48 /** |
webmaster@1
|
49 * Initialize a database connection. |
webmaster@1
|
50 */ |
webmaster@1
|
51 function db_connect($url) { |
webmaster@1
|
52 $url = parse_url($url); |
webmaster@1
|
53 |
webmaster@1
|
54 // Check if MySQL support is present in PHP |
webmaster@1
|
55 if (!function_exists('mysql_connect')) { |
webmaster@1
|
56 _db_error_page('Unable to use the MySQL database because the MySQL extension for PHP is not installed. Check your <code>php.ini</code> to see how you can enable it.'); |
webmaster@1
|
57 } |
webmaster@1
|
58 |
webmaster@1
|
59 // Decode url-encoded information in the db connection string |
webmaster@1
|
60 $url['user'] = urldecode($url['user']); |
webmaster@1
|
61 // Test if database url has a password. |
webmaster@1
|
62 $url['pass'] = isset($url['pass']) ? urldecode($url['pass']) : ''; |
webmaster@1
|
63 $url['host'] = urldecode($url['host']); |
webmaster@1
|
64 $url['path'] = urldecode($url['path']); |
webmaster@1
|
65 |
webmaster@1
|
66 // Allow for non-standard MySQL port. |
webmaster@1
|
67 if (isset($url['port'])) { |
webmaster@1
|
68 $url['host'] = $url['host'] .':'. $url['port']; |
webmaster@1
|
69 } |
webmaster@1
|
70 |
webmaster@1
|
71 // - TRUE makes mysql_connect() always open a new link, even if |
webmaster@1
|
72 // mysql_connect() was called before with the same parameters. |
webmaster@1
|
73 // This is important if you are using two databases on the same |
webmaster@1
|
74 // server. |
webmaster@1
|
75 // - 2 means CLIENT_FOUND_ROWS: return the number of found |
webmaster@1
|
76 // (matched) rows, not the number of affected rows. |
webmaster@1
|
77 $connection = @mysql_connect($url['host'], $url['user'], $url['pass'], TRUE, 2); |
webmaster@1
|
78 if (!$connection || !mysql_select_db(substr($url['path'], 1))) { |
webmaster@1
|
79 // Show error screen otherwise |
webmaster@1
|
80 _db_error_page(mysql_error()); |
webmaster@1
|
81 } |
webmaster@1
|
82 |
webmaster@1
|
83 // Force UTF-8. |
webmaster@1
|
84 mysql_query('SET NAMES "utf8"', $connection); |
webmaster@1
|
85 return $connection; |
webmaster@1
|
86 } |
webmaster@1
|
87 |
webmaster@1
|
88 /** |
webmaster@1
|
89 * Helper function for db_query(). |
webmaster@1
|
90 */ |
webmaster@1
|
91 function _db_query($query, $debug = 0) { |
webmaster@1
|
92 global $active_db, $queries, $user; |
webmaster@1
|
93 |
webmaster@1
|
94 if (variable_get('dev_query', 0)) { |
webmaster@1
|
95 list($usec, $sec) = explode(' ', microtime()); |
webmaster@1
|
96 $timer = (float)$usec + (float)$sec; |
webmaster@1
|
97 // If devel.module query logging is enabled, prepend a comment with the username and calling function |
webmaster@1
|
98 // to the SQL string. This is useful when running mysql's SHOW PROCESSLIST to learn what exact |
webmaster@1
|
99 // code is issueing the slow query. |
webmaster@1
|
100 $bt = debug_backtrace(); |
webmaster@1
|
101 // t() may not be available yet so we don't wrap 'Anonymous'. |
webmaster@1
|
102 $name = $user->uid ? $user->name : variable_get('anonymous', 'Anonymous'); |
webmaster@1
|
103 // str_replace() to prevent SQL injection via username or anonymous name. |
webmaster@1
|
104 $name = str_replace(array('*', '/'), '', $name); |
webmaster@1
|
105 $query = '/* '. $name .' : '. $bt[2]['function'] .' */ '. $query; |
webmaster@1
|
106 } |
webmaster@1
|
107 |
webmaster@1
|
108 $result = mysql_query($query, $active_db); |
webmaster@1
|
109 |
webmaster@1
|
110 if (variable_get('dev_query', 0)) { |
webmaster@1
|
111 $query = $bt[2]['function'] ."\n". $query; |
webmaster@1
|
112 list($usec, $sec) = explode(' ', microtime()); |
webmaster@1
|
113 $stop = (float)$usec + (float)$sec; |
webmaster@1
|
114 $diff = $stop - $timer; |
webmaster@1
|
115 $queries[] = array($query, $diff); |
webmaster@1
|
116 } |
webmaster@1
|
117 |
webmaster@1
|
118 if ($debug) { |
webmaster@1
|
119 print '<p>query: '. $query .'<br />error:'. mysql_error($active_db) .'</p>'; |
webmaster@1
|
120 } |
webmaster@1
|
121 |
webmaster@1
|
122 if (!mysql_errno($active_db)) { |
webmaster@1
|
123 return $result; |
webmaster@1
|
124 } |
webmaster@1
|
125 else { |
webmaster@1
|
126 // Indicate to drupal_error_handler that this is a database error. |
webmaster@1
|
127 ${DB_ERROR} = TRUE; |
webmaster@1
|
128 trigger_error(check_plain(mysql_error($active_db) ."\nquery: ". $query), E_USER_WARNING); |
webmaster@1
|
129 return FALSE; |
webmaster@1
|
130 } |
webmaster@1
|
131 } |
webmaster@1
|
132 |
webmaster@1
|
133 /** |
webmaster@1
|
134 * Fetch one result row from the previous query as an object. |
webmaster@1
|
135 * |
webmaster@1
|
136 * @param $result |
webmaster@1
|
137 * A database query result resource, as returned from db_query(). |
webmaster@1
|
138 * @return |
webmaster@1
|
139 * An object representing the next row of the result, or FALSE. The attributes |
webmaster@1
|
140 * of this object are the table fields selected by the query. |
webmaster@1
|
141 */ |
webmaster@1
|
142 function db_fetch_object($result) { |
webmaster@1
|
143 if ($result) { |
webmaster@1
|
144 return mysql_fetch_object($result); |
webmaster@1
|
145 } |
webmaster@1
|
146 } |
webmaster@1
|
147 |
webmaster@1
|
148 /** |
webmaster@1
|
149 * Fetch one result row from the previous query as an array. |
webmaster@1
|
150 * |
webmaster@1
|
151 * @param $result |
webmaster@1
|
152 * A database query result resource, as returned from db_query(). |
webmaster@1
|
153 * @return |
webmaster@1
|
154 * An associative array representing the next row of the result, or FALSE. |
webmaster@1
|
155 * The keys of this object are the names of the table fields selected by the |
webmaster@1
|
156 * query, and the values are the field values for this result row. |
webmaster@1
|
157 */ |
webmaster@1
|
158 function db_fetch_array($result) { |
webmaster@1
|
159 if ($result) { |
webmaster@1
|
160 return mysql_fetch_array($result, MYSQL_ASSOC); |
webmaster@1
|
161 } |
webmaster@1
|
162 } |
webmaster@1
|
163 |
webmaster@1
|
164 /** |
webmaster@1
|
165 * Return an individual result field from the previous query. |
webmaster@1
|
166 * |
webmaster@1
|
167 * Only use this function if exactly one field is being selected; otherwise, |
webmaster@1
|
168 * use db_fetch_object() or db_fetch_array(). |
webmaster@1
|
169 * |
webmaster@1
|
170 * @param $result |
webmaster@1
|
171 * A database query result resource, as returned from db_query(). |
webmaster@1
|
172 * @return |
webmaster@1
|
173 * The resulting field or FALSE. |
webmaster@1
|
174 */ |
webmaster@1
|
175 function db_result($result) { |
webmaster@1
|
176 if ($result && mysql_num_rows($result) > 0) { |
webmaster@1
|
177 // The mysql_fetch_row function has an optional second parameter $row |
webmaster@1
|
178 // but that can't be used for compatibility with Oracle, DB2, etc. |
webmaster@1
|
179 $array = mysql_fetch_row($result); |
webmaster@1
|
180 return $array[0]; |
webmaster@1
|
181 } |
webmaster@1
|
182 return FALSE; |
webmaster@1
|
183 } |
webmaster@1
|
184 |
webmaster@1
|
185 /** |
webmaster@1
|
186 * Determine whether the previous query caused an error. |
webmaster@1
|
187 */ |
webmaster@1
|
188 function db_error() { |
webmaster@1
|
189 global $active_db; |
webmaster@1
|
190 return mysql_errno($active_db); |
webmaster@1
|
191 } |
webmaster@1
|
192 |
webmaster@1
|
193 /** |
webmaster@1
|
194 * Determine the number of rows changed by the preceding query. |
webmaster@1
|
195 */ |
webmaster@1
|
196 function db_affected_rows() { |
webmaster@1
|
197 global $active_db; |
webmaster@1
|
198 return mysql_affected_rows($active_db); |
webmaster@1
|
199 } |
webmaster@1
|
200 |
webmaster@1
|
201 /** |
webmaster@1
|
202 * Runs a limited-range query in the active database. |
webmaster@1
|
203 * |
webmaster@1
|
204 * Use this as a substitute for db_query() when a subset of the query is to be |
webmaster@1
|
205 * returned. |
webmaster@1
|
206 * User-supplied arguments to the query should be passed in as separate parameters |
webmaster@1
|
207 * so that they can be properly escaped to avoid SQL injection attacks. |
webmaster@1
|
208 * |
webmaster@1
|
209 * @param $query |
webmaster@1
|
210 * A string containing an SQL query. |
webmaster@1
|
211 * @param ... |
webmaster@1
|
212 * A variable number of arguments which are substituted into the query |
webmaster@1
|
213 * using printf() syntax. The query arguments can be enclosed in one |
webmaster@1
|
214 * array instead. |
webmaster@1
|
215 * Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose |
webmaster@1
|
216 * in '') and %%. |
webmaster@1
|
217 * |
webmaster@1
|
218 * NOTE: using this syntax will cast NULL and FALSE values to decimal 0, |
webmaster@1
|
219 * and TRUE values to decimal 1. |
webmaster@1
|
220 * |
webmaster@1
|
221 * @param $from |
webmaster@1
|
222 * The first result row to return. |
webmaster@1
|
223 * @param $count |
webmaster@1
|
224 * The maximum number of result rows to return. |
webmaster@1
|
225 * @return |
webmaster@1
|
226 * A database query result resource, or FALSE if the query was not executed |
webmaster@1
|
227 * correctly. |
webmaster@1
|
228 */ |
webmaster@1
|
229 function db_query_range($query) { |
webmaster@1
|
230 $args = func_get_args(); |
webmaster@1
|
231 $count = array_pop($args); |
webmaster@1
|
232 $from = array_pop($args); |
webmaster@1
|
233 array_shift($args); |
webmaster@1
|
234 |
webmaster@1
|
235 $query = db_prefix_tables($query); |
webmaster@1
|
236 if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax |
webmaster@1
|
237 $args = $args[0]; |
webmaster@1
|
238 } |
webmaster@1
|
239 _db_query_callback($args, TRUE); |
webmaster@1
|
240 $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query); |
webmaster@1
|
241 $query .= ' LIMIT '. (int)$from .', '. (int)$count; |
webmaster@1
|
242 return _db_query($query); |
webmaster@1
|
243 } |
webmaster@1
|
244 |
webmaster@1
|
245 /** |
webmaster@1
|
246 * Runs a SELECT query and stores its results in a temporary table. |
webmaster@1
|
247 * |
webmaster@1
|
248 * Use this as a substitute for db_query() when the results need to stored |
webmaster@1
|
249 * in a temporary table. Temporary tables exist for the duration of the page |
webmaster@1
|
250 * request. |
webmaster@1
|
251 * User-supplied arguments to the query should be passed in as separate parameters |
webmaster@1
|
252 * so that they can be properly escaped to avoid SQL injection attacks. |
webmaster@1
|
253 * |
webmaster@1
|
254 * Note that if you need to know how many results were returned, you should do |
webmaster@1
|
255 * a SELECT COUNT(*) on the temporary table afterwards. db_affected_rows() does |
webmaster@1
|
256 * not give consistent result across different database types in this case. |
webmaster@1
|
257 * |
webmaster@1
|
258 * @param $query |
webmaster@1
|
259 * A string containing a normal SELECT SQL query. |
webmaster@1
|
260 * @param ... |
webmaster@1
|
261 * A variable number of arguments which are substituted into the query |
webmaster@1
|
262 * using printf() syntax. The query arguments can be enclosed in one |
webmaster@1
|
263 * array instead. |
webmaster@1
|
264 * Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose |
webmaster@1
|
265 * in '') and %%. |
webmaster@1
|
266 * |
webmaster@1
|
267 * NOTE: using this syntax will cast NULL and FALSE values to decimal 0, |
webmaster@1
|
268 * and TRUE values to decimal 1. |
webmaster@1
|
269 * |
webmaster@1
|
270 * @param $table |
webmaster@1
|
271 * The name of the temporary table to select into. This name will not be |
webmaster@1
|
272 * prefixed as there is no risk of collision. |
webmaster@1
|
273 * @return |
webmaster@1
|
274 * A database query result resource, or FALSE if the query was not executed |
webmaster@1
|
275 * correctly. |
webmaster@1
|
276 */ |
webmaster@1
|
277 function db_query_temporary($query) { |
webmaster@1
|
278 $args = func_get_args(); |
webmaster@1
|
279 $tablename = array_pop($args); |
webmaster@1
|
280 array_shift($args); |
webmaster@1
|
281 |
webmaster@1
|
282 $query = preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE '. $tablename .' Engine=HEAP SELECT', db_prefix_tables($query)); |
webmaster@1
|
283 if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax |
webmaster@1
|
284 $args = $args[0]; |
webmaster@1
|
285 } |
webmaster@1
|
286 _db_query_callback($args, TRUE); |
webmaster@1
|
287 $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query); |
webmaster@1
|
288 return _db_query($query); |
webmaster@1
|
289 } |
webmaster@1
|
290 |
webmaster@1
|
291 /** |
webmaster@1
|
292 * Returns a properly formatted Binary Large OBject value. |
webmaster@1
|
293 * |
webmaster@1
|
294 * @param $data |
webmaster@1
|
295 * Data to encode. |
webmaster@1
|
296 * @return |
webmaster@1
|
297 * Encoded data. |
webmaster@1
|
298 */ |
webmaster@1
|
299 function db_encode_blob($data) { |
webmaster@1
|
300 global $active_db; |
webmaster@1
|
301 return "'". mysql_real_escape_string($data, $active_db) ."'"; |
webmaster@1
|
302 } |
webmaster@1
|
303 |
webmaster@1
|
304 /** |
webmaster@1
|
305 * Returns text from a Binary Large Object value. |
webmaster@1
|
306 * |
webmaster@1
|
307 * @param $data |
webmaster@1
|
308 * Data to decode. |
webmaster@1
|
309 * @return |
webmaster@1
|
310 * Decoded data. |
webmaster@1
|
311 */ |
webmaster@1
|
312 function db_decode_blob($data) { |
webmaster@1
|
313 return $data; |
webmaster@1
|
314 } |
webmaster@1
|
315 |
webmaster@1
|
316 /** |
webmaster@1
|
317 * Prepare user input for use in a database query, preventing SQL injection attacks. |
webmaster@1
|
318 */ |
webmaster@1
|
319 function db_escape_string($text) { |
webmaster@1
|
320 global $active_db; |
webmaster@1
|
321 return mysql_real_escape_string($text, $active_db); |
webmaster@1
|
322 } |
webmaster@1
|
323 |
webmaster@1
|
324 /** |
webmaster@1
|
325 * Lock a table. |
webmaster@1
|
326 */ |
webmaster@1
|
327 function db_lock_table($table) { |
webmaster@1
|
328 db_query('LOCK TABLES {'. db_escape_table($table) .'} WRITE'); |
webmaster@1
|
329 } |
webmaster@1
|
330 |
webmaster@1
|
331 /** |
webmaster@1
|
332 * Unlock all locked tables. |
webmaster@1
|
333 */ |
webmaster@1
|
334 function db_unlock_tables() { |
webmaster@1
|
335 db_query('UNLOCK TABLES'); |
webmaster@1
|
336 } |
webmaster@1
|
337 |
webmaster@1
|
338 /** |
webmaster@1
|
339 * Check if a table exists. |
webmaster@1
|
340 */ |
webmaster@1
|
341 function db_table_exists($table) { |
webmaster@1
|
342 return (bool) db_fetch_object(db_query("SHOW TABLES LIKE '{". db_escape_table($table) ."}'")); |
webmaster@1
|
343 } |
webmaster@1
|
344 |
webmaster@1
|
345 /** |
webmaster@1
|
346 * Check if a column exists in the given table. |
webmaster@1
|
347 */ |
webmaster@1
|
348 function db_column_exists($table, $column) { |
webmaster@1
|
349 return (bool) db_fetch_object(db_query("SHOW COLUMNS FROM {". db_escape_table($table) ."} LIKE '". db_escape_table($column) ."'")); |
webmaster@1
|
350 } |
webmaster@1
|
351 |
webmaster@1
|
352 /** |
webmaster@1
|
353 * Wraps the given table.field entry with a DISTINCT(). The wrapper is added to |
webmaster@1
|
354 * the SELECT list entry of the given query and the resulting query is returned. |
webmaster@1
|
355 * This function only applies the wrapper if a DISTINCT doesn't already exist in |
webmaster@1
|
356 * the query. |
webmaster@1
|
357 * |
webmaster@1
|
358 * @param $table Table containing the field to set as DISTINCT |
webmaster@1
|
359 * @param $field Field to set as DISTINCT |
webmaster@1
|
360 * @param $query Query to apply the wrapper to |
webmaster@1
|
361 * @return SQL query with the DISTINCT wrapper surrounding the given table.field. |
webmaster@1
|
362 */ |
webmaster@1
|
363 function db_distinct_field($table, $field, $query) { |
webmaster@1
|
364 $field_to_select = 'DISTINCT('. $table .'.'. $field .')'; |
webmaster@1
|
365 // (?<!text) is a negative look-behind (no need to rewrite queries that already use DISTINCT). |
webmaster@1
|
366 return preg_replace('/(SELECT.*)(?:'. $table .'\.|\s)(?<!DISTINCT\()(?<!DISTINCT\('. $table .'\.)'. $field .'(.*FROM )/AUsi', '\1 '. $field_to_select .'\2', $query); |
webmaster@1
|
367 } |
webmaster@1
|
368 |
webmaster@1
|
369 /** |
webmaster@1
|
370 * @} End of "ingroup database". |
webmaster@1
|
371 */ |