rfe #1694104 Allow logging of user status with Apache.
[phpmyadmin/crack.git] / libraries / dbi / mysql.dbi.lib.php
blob1539614dadf2739d90f48e494092fc6d3c18216b
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Interface to the classic MySQL extension
6 * @package phpMyAdmin-DBI-MySQL
7 * @version $Id$
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 require_once './libraries/logging.lib.php';
15 /**
16 * MySQL client API
18 if (! defined('PMA_MYSQL_CLIENT_API')) {
19 $client_api = explode('.', mysql_get_client_info());
20 define('PMA_MYSQL_CLIENT_API', (int)sprintf('%d%02d%02d', $client_api[0], $client_api[1], intval($client_api[2])));
21 unset($client_api);
24 function PMA_DBI_real_connect($server, $user, $password, $client_flags)
26 global $cfg;
28 if (empty($client_flags)) {
29 if ($cfg['PersistentConnections']) {
30 $link = @mysql_pconnect($server, $user, $password);
31 } else {
32 $link = @mysql_connect($server, $user, $password);
34 } else {
35 if ($cfg['PersistentConnections']) {
36 $link = @mysql_pconnect($server, $user, $password, $client_flags);
37 } else {
38 $link = @mysql_connect($server, $user, $password, false, $client_flags);
42 return $link;
45 function PMA_DBI_connect($user, $password, $is_controluser = false)
47 global $cfg, $php_errormsg;
49 $server_port = (empty($cfg['Server']['port']))
50 ? ''
51 : ':' . $cfg['Server']['port'];
53 if (strtolower($cfg['Server']['connect_type']) == 'tcp') {
54 $cfg['Server']['socket'] = '';
57 $server_socket = (empty($cfg['Server']['socket']))
58 ? ''
59 : ':' . $cfg['Server']['socket'];
61 $client_flags = 0;
63 // always use CLIENT_LOCAL_FILES as defined in mysql_com.h
64 // for the case where the client library was not compiled
65 // with --enable-local-infile
66 $client_flags |= 128;
68 /* Optionally compress connection */
69 if (defined('MYSQL_CLIENT_COMPRESS') && $cfg['Server']['compress']) {
70 $client_flags |= MYSQL_CLIENT_COMPRESS;
73 /* Optionally enable SSL */
74 if (defined('MYSQL_CLIENT_SSL') && $cfg['Server']['ssl']) {
75 $client_flags |= MYSQL_CLIENT_SSL;
78 $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, $password, empty($client_flags) ? NULL : $client_flags);
80 // Retry with empty password if we're allowed to
81 if (empty($link) && $cfg['Server']['nopassword'] && !$is_controluser) {
82 $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, '', empty($client_flags) ? NULL : $client_flags);
85 if (empty($link)) {
86 if ($is_controluser) {
87 trigger_error($GLOBALS['strControluserFailed'], E_USER_WARNING);
88 return false;
90 PMA_log_user($user, 'mysql-denied');
91 PMA_auth_fails();
92 } // end if
94 PMA_DBI_postConnect($link, $is_controluser);
96 return $link;
99 /**
100 * select a db
102 * @param string $dbname name of db to select
103 * @param resource $link mysql link resource
104 * @return boolean success
106 function PMA_DBI_select_db($dbname, $link = null)
108 if (empty($link)) {
109 if (isset($GLOBALS['userlink'])) {
110 $link = $GLOBALS['userlink'];
111 } else {
112 return false;
115 return mysql_select_db($dbname, $link);
119 * runs a query and returns the result
121 * @param string $query query to run
122 * @param resource $link mysql link resource
123 * @param integer $options
124 * @return mixed
126 function PMA_DBI_try_query($query, $link = null, $options = 0)
128 if (empty($link)) {
129 if (isset($GLOBALS['userlink'])) {
130 $link = $GLOBALS['userlink'];
131 } else {
132 return false;
136 if ($GLOBALS['cfg']['DBG']['sql']) {
137 $time = microtime(true);
139 if ($options == ($options | PMA_DBI_QUERY_STORE)) {
140 $r = mysql_query($query, $link);
141 } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED)) {
142 $r = mysql_unbuffered_query($query, $link);
143 } else {
144 $r = mysql_query($query, $link);
147 if ($GLOBALS['cfg']['DBG']['sql']) {
148 $time = microtime(true) - $time;
150 $hash = md5($query);
152 if (isset($_SESSION['debug']['queries'][$hash])) {
153 $_SESSION['debug']['queries'][$hash]['count']++;
154 } else {
155 $_SESSION['debug']['queries'][$hash] = array();
156 $_SESSION['debug']['queries'][$hash]['count'] = 1;
157 $_SESSION['debug']['queries'][$hash]['query'] = $query;
158 $_SESSION['debug']['queries'][$hash]['time'] = $time;
161 $trace = array();
162 foreach (debug_backtrace() as $trace_step) {
163 $trace[] = PMA_Error::relPath($trace_step['file']) . '#'
164 . $trace_step['line'] . ': '
165 . (isset($trace_step['class']) ? $trace_step['class'] : '')
166 //. (isset($trace_step['object']) ? get_class($trace_step['object']) : '')
167 . (isset($trace_step['type']) ? $trace_step['type'] : '')
168 . (isset($trace_step['function']) ? $trace_step['function'] : '')
169 . '('
170 . (isset($trace_step['params']) ? implode(', ', $trace_step['params']) : '')
171 . ')'
174 $_SESSION['debug']['queries'][$hash]['trace'][] = $trace;
177 return $r;
180 function PMA_DBI_fetch_array($result)
182 return mysql_fetch_array($result, MYSQL_BOTH);
185 function PMA_DBI_fetch_assoc($result) {
186 return mysql_fetch_array($result, MYSQL_ASSOC);
189 function PMA_DBI_fetch_row($result)
191 return mysql_fetch_array($result, MYSQL_NUM);
195 * Adjusts the result pointer to an arbitrary row in the result
197 * @uses mysql_data_seek()
198 * @param $result
199 * @param $offset
200 * @return boolean true on success, false on failure
202 function PMA_DBI_data_seek($result, $offset)
204 return mysql_data_seek($result, $offset);
208 * Frees the memory associated with the results
210 * @param result $result,... one or more mysql result resources
212 function PMA_DBI_free_result()
214 foreach (func_get_args() as $result) {
215 if (is_resource($result)
216 && get_resource_type($result) === 'mysql result') {
217 mysql_free_result($result);
223 * Returns a string representing the type of connection used
224 * @uses mysql_get_host_info()
225 * @uses $GLOBALS['userlink'] as default for $link
226 * @param resource $link mysql link
227 * @return string type of connection used
229 function PMA_DBI_get_host_info($link = null)
231 if (null === $link) {
232 if (isset($GLOBALS['userlink'])) {
233 $link = $GLOBALS['userlink'];
234 } else {
235 return false;
238 return mysql_get_host_info($link);
242 * Returns the version of the MySQL protocol used
243 * @uses mysql_get_proto_info()
244 * @uses $GLOBALS['userlink'] as default for $link
245 * @param resource $link mysql link
246 * @return integer version of the MySQL protocol used
248 function PMA_DBI_get_proto_info($link = null)
250 if (null === $link) {
251 if (isset($GLOBALS['userlink'])) {
252 $link = $GLOBALS['userlink'];
253 } else {
254 return false;
257 return mysql_get_proto_info($link);
261 * returns a string that represents the client library version
262 * @uses mysql_get_client_info()
263 * @return string MySQL client library version
265 function PMA_DBI_get_client_info()
267 return mysql_get_client_info();
271 * returns last error message or false if no errors occured
273 * @uses PMA_DBI_convert_message()
274 * @uses $GLOBALS['errno']
275 * @uses $GLOBALS['userlink']
276 * @uses $GLOBALS['strServerNotResponding']
277 * @uses $GLOBALS['strSocketProblem']
278 * @uses $GLOBALS['strDetails']
279 * @uses mysql_errno()
280 * @uses mysql_error()
281 * @uses defined()
282 * @uses PMA_generate_common_url()
283 * @param resource $link mysql link
284 * @return string|boolean $error or false
286 function PMA_DBI_getError($link = null)
288 $GLOBALS['errno'] = 0;
289 if (null === $link && isset($GLOBALS['userlink'])) {
290 $link =& $GLOBALS['userlink'];
292 // Do not stop now. On the initial connection, we don't have a $link,
293 // we don't have a $GLOBALS['userlink'], but we can catch the error code
294 // } else {
295 // return false;
298 if (null !== $link && false !== $link) {
299 $error_number = mysql_errno($link);
300 $error_message = mysql_error($link);
301 } else {
302 $error_number = mysql_errno();
303 $error_message = mysql_error();
305 if (0 == $error_number) {
306 return false;
309 // keep the error number for further check after the call to PMA_DBI_getError()
310 $GLOBALS['errno'] = $error_number;
312 if (! empty($error_message)) {
313 $error_message = PMA_DBI_convert_message($error_message);
316 // Some errors messages cannot be obtained by mysql_error()
317 if ($error_number == 2002) {
318 $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'] . ' ' . $GLOBALS['strSocketProblem'];
319 } elseif ($error_number == 2003) {
320 $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'];
321 } elseif ($error_number == 1005) {
322 /* InnoDB contraints, see
323 * http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
325 $error = '#' . ((string) $error_number) . ' - ' . $error_message .
326 ' (<a href="server_engines.php' . PMA_generate_common_url(array('engine' => 'InnoDB', 'page' => 'Status')).
327 '">' . $GLOBALS['strDetails'] . '</a>)';
328 } else {
329 $error = '#' . ((string) $error_number) . ' - ' . $error_message;
331 return $error;
334 function PMA_DBI_num_rows($result)
336 if (!is_bool($result)) {
337 return mysql_num_rows($result);
338 } else {
339 return 0;
343 function PMA_DBI_insert_id($link = null)
345 if (empty($link)) {
346 if (isset($GLOBALS['userlink'])) {
347 $link = $GLOBALS['userlink'];
348 } else {
349 return false;
352 //$insert_id = mysql_insert_id($link);
353 // if the primary key is BIGINT we get an incorrect result
354 // (sometimes negative, sometimes positive)
355 // and in the present function we don't know if the PK is BIGINT
356 // so better play safe and use LAST_INSERT_ID()
358 // by the way, no problem with mysqli_insert_id()
359 return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
362 function PMA_DBI_affected_rows($link = null)
364 if (empty($link)) {
365 if (isset($GLOBALS['userlink'])) {
366 $link = $GLOBALS['userlink'];
367 } else {
368 return false;
371 return mysql_affected_rows($link);
375 * @todo add missing keys like in from mysqli_query (orgname, orgtable, flags, decimals)
377 function PMA_DBI_get_fields_meta($result)
379 $fields = array();
380 $num_fields = mysql_num_fields($result);
381 for ($i = 0; $i < $num_fields; $i++) {
382 $fields[] = mysql_fetch_field($result, $i);
384 return $fields;
387 function PMA_DBI_num_fields($result)
389 return mysql_num_fields($result);
392 function PMA_DBI_field_len($result, $i)
394 return mysql_field_len($result, $i);
397 function PMA_DBI_field_name($result, $i)
399 return mysql_field_name($result, $i);
402 function PMA_DBI_field_flags($result, $i)
404 return mysql_field_flags($result, $i);