avoid going back to login page on slave configuration
[phpmyadmin/crack.git] / libraries / dbi / mysqli.dbi.lib.php
blob913bce6279f0481c3f2f239f725e28ef8774a5c0
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Interface to the improved MySQL extension (MySQLi)
6 * @package phpMyAdmin-DBI-MySQLi
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('.', mysqli_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 /**
25 * some PHP versions are reporting extra messages like "No index used in query"
28 mysqli_report(MYSQLI_REPORT_OFF);
30 /**
31 * some older mysql client libs are missing these constants ...
33 if (! defined('MYSQLI_BINARY_FLAG')) {
34 define('MYSQLI_BINARY_FLAG', 128);
37 /**
38 * @see http://bugs.php.net/36007
40 if (! defined('MYSQLI_TYPE_NEWDECIMAL')) {
41 define('MYSQLI_TYPE_NEWDECIMAL', 246);
43 if (! defined('MYSQLI_TYPE_BIT')) {
44 define('MYSQLI_TYPE_BIT', 16);
47 /**
48 * connects to the database server
50 * @uses $GLOBALS['cfg']['Server']
51 * @uses PMA_auth_fails()
52 * @uses PMA_DBI_postConnect()
53 * @uses MYSQLI_CLIENT_COMPRESS
54 * @uses MYSQLI_OPT_LOCAL_INFILE
55 * @uses strtolower()
56 * @uses mysqli_init()
57 * @uses mysqli_options()
58 * @uses mysqli_real_connect()
59 * @uses defined()
60 * @param string $user mysql user name
61 * @param string $password mysql user password
62 * @param boolean $is_controluser
63 * @param array $server host/port/socket
64 * @param boolean $auxiliary_connection (when true, don't go back to login if connection fails)
65 * @return mixed false on error or a mysqli object on success
67 function PMA_DBI_connect($user, $password, $is_controluser = false, $server = null, $auxiliary_connection = false)
69 if ($server) {
70 $server_port = (empty($server['port']))
71 ? ''
72 : (int)$server['port'];
73 $server_socket = (empty($server['socket']))
74 ? ''
75 : $server['socket'];
76 $server['host'] = (empty($server['host']))
77 ? 'localhost'
78 : $server['host'];
79 } else {
80 $server_port = (empty($GLOBALS['cfg']['Server']['port']))
81 ? false
82 : (int) $GLOBALS['cfg']['Server']['port'];
83 $server_socket = (empty($GLOBALS['cfg']['Server']['socket']))
84 ? null
85 : $GLOBALS['cfg']['Server']['socket'];
89 if (strtolower($GLOBALS['cfg']['Server']['connect_type']) == 'tcp') {
90 $GLOBALS['cfg']['Server']['socket'] = '';
93 // NULL enables connection to the default socket
95 $link = mysqli_init();
97 mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, true);
99 $client_flags = 0;
101 /* Optionally compress connection */
102 if ($GLOBALS['cfg']['Server']['compress'] && defined('MYSQLI_CLIENT_COMPRESS')) {
103 $client_flags |= MYSQLI_CLIENT_COMPRESS;
106 /* Optionally enable SSL */
107 if ($GLOBALS['cfg']['Server']['ssl'] && defined('MYSQLI_CLIENT_SSL')) {
108 $client_flags |= MYSQLI_CLIENT_SSL;
111 if (!$server) {
112 $return_value = @mysqli_real_connect($link, $GLOBALS['cfg']['Server']['host'], $user, $password, false, $server_port, $server_socket, $client_flags);
114 // Retry with empty password if we're allowed to
115 if ($return_value == false && isset($cfg['Server']['nopassword']) && $cfg['Server']['nopassword'] && !$is_controluser) {
116 $return_value = @mysqli_real_connect($link, $GLOBALS['cfg']['Server']['host'], $user, '', false, $server_port, $server_socket, $client_flags);
118 } else {
119 $return_value = @mysqli_real_connect($link, $server['host'], $user, $password, false, $server_port, $server_socket);
122 if ($return_value == false) {
123 if ($is_controluser) {
124 trigger_error($GLOBALS['strControluserFailed'], E_USER_WARNING);
125 return false;
127 // we could be calling PMA_DBI_connect() to connect to another
128 // server, for example in the Synchronize feature, so do not
129 // go back to main login if it fails
130 if (! $auxiliary_connection) {
131 PMA_log_user($user, 'mysql-denied');
132 PMA_auth_fails();
133 } else {
134 return false;
136 } else {
137 PMA_DBI_postConnect($link, $is_controluser);
140 return $link;
144 * selects given database
146 * @uses $GLOBALS['userlink']
147 * @uses PMA_convert_charset()
148 * @uses mysqli_select_db()
149 * @param string $dbname database name to select
150 * @param object mysqli $link the mysqli object
151 * @return boolean true or false
153 function PMA_DBI_select_db($dbname, $link = null)
155 if (empty($link)) {
156 if (isset($GLOBALS['userlink'])) {
157 $link = $GLOBALS['userlink'];
158 } else {
159 return false;
162 return mysqli_select_db($link, $dbname);
166 * runs a query and returns the result
168 * @uses PMA_DBI_QUERY_STORE
169 * @uses PMA_DBI_QUERY_UNBUFFERED
170 * @uses $GLOBALS['userlink']
171 * @uses PMA_convert_charset()
172 * @uses MYSQLI_STORE_RESULT
173 * @uses MYSQLI_USE_RESULT
174 * @uses mysqli_query()
175 * @uses defined()
176 * @param string $query query to execute
177 * @param object mysqli $link mysqli object
178 * @param integer $options
179 * @return mixed true, false or result object
181 function PMA_DBI_try_query($query, $link = null, $options = 0)
183 if ($options == ($options | PMA_DBI_QUERY_STORE)) {
184 $method = MYSQLI_STORE_RESULT;
185 } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED)) {
186 $method = MYSQLI_USE_RESULT;
187 } else {
188 $method = 0;
191 if (empty($link)) {
192 if (isset($GLOBALS['userlink'])) {
193 $link = $GLOBALS['userlink'];
194 } else {
195 return false;
199 if ($GLOBALS['cfg']['DBG']['sql']) {
200 $time = microtime(true);
202 $r = mysqli_query($link, $query, $method);
203 if ($GLOBALS['cfg']['DBG']['sql']) {
204 $time = microtime(true) - $time;
206 $hash = md5($query);
208 if (isset($_SESSION['debug']['queries'][$hash])) {
209 $_SESSION['debug']['queries'][$hash]['count']++;
210 } else {
211 $_SESSION['debug']['queries'][$hash] = array();
212 $_SESSION['debug']['queries'][$hash]['count'] = 1;
213 $_SESSION['debug']['queries'][$hash]['query'] = $query;
214 $_SESSION['debug']['queries'][$hash]['time'] = $time;
217 $trace = array();
218 foreach (debug_backtrace() as $trace_step) {
219 $trace[] = PMA_Error::relPath($trace_step['file']) . '#'
220 . $trace_step['line'] . ': '
221 . (isset($trace_step['class']) ? $trace_step['class'] : '')
222 //. (isset($trace_step['object']) ? get_class($trace_step['object']) : '')
223 . (isset($trace_step['type']) ? $trace_step['type'] : '')
224 . (isset($trace_step['function']) ? $trace_step['function'] : '')
225 . '('
226 . (isset($trace_step['params']) ? implode(', ', $trace_step['params']) : '')
227 . ')'
230 $_SESSION['debug']['queries'][$hash]['trace'][] = $trace;
233 if ($r != FALSE && PMA_Tracker::isActive() == TRUE ) {
234 PMA_Tracker::handleQuery($query);
237 return $r;
239 // From the PHP manual:
240 // "note: returns true on success or false on failure. For SELECT,
241 // SHOW, DESCRIBE or EXPLAIN, mysqli_query() will return a result object"
242 // so, do not use the return value to feed mysqli_num_rows() if it's
243 // a boolean
247 * returns array of rows with associative and numeric keys from $result
249 * @uses mysqli_fetch_array()
250 * @uses MYSQLI_BOTH
251 * @param object mysqli result $result
252 * @return array result rows
254 function PMA_DBI_fetch_array($result)
256 return mysqli_fetch_array($result, MYSQLI_BOTH);
260 * returns array of rows with associative keys from $result
262 * @uses mysqli_fetch_array()
263 * @uses MYSQLI_ASSOC
264 * @param object mysqli result $result
265 * @return array result rows
267 function PMA_DBI_fetch_assoc($result)
269 return mysqli_fetch_array($result, MYSQLI_ASSOC);
273 * returns array of rows with numeric keys from $result
275 * @uses mysqli_fetch_array()
276 * @uses MYSQLI_NUM
277 * @param object mysqli result $result
278 * @return array result rows
280 function PMA_DBI_fetch_row($result)
282 return mysqli_fetch_array($result, MYSQLI_NUM);
286 * Adjusts the result pointer to an arbitrary row in the result
288 * @uses mysqli_data_seek()
289 * @param $result
290 * @param $offset
291 * @return boolean true on success, false on failure
293 function PMA_DBI_data_seek($result, $offset)
295 return mysqli_data_seek($result, $offset);
299 * Frees the memory associated with the results
301 * @uses mysqli_result
302 * @uses func_get_args()
303 * @uses mysqli_free_result()
304 * @param result $result,... one or more mysql result resources
306 function PMA_DBI_free_result()
308 foreach (func_get_args() as $result) {
309 if ($result instanceof mysqli_result) {
310 mysqli_free_result($result);
316 * Returns a string representing the type of connection used
317 * @uses mysqli_get_host_info()
318 * @uses $GLOBALS['userlink'] as default for $link
319 * @param resource $link mysql link
320 * @return string type of connection used
322 function PMA_DBI_get_host_info($link = null)
324 if (null === $link) {
325 if (isset($GLOBALS['userlink'])) {
326 $link = $GLOBALS['userlink'];
327 } else {
328 return false;
331 return mysqli_get_host_info($link);
335 * Returns the version of the MySQL protocol used
336 * @uses mysqli_get_proto_info()
337 * @uses $GLOBALS['userlink'] as default for $link
338 * @param resource $link mysql link
339 * @return integer version of the MySQL protocol used
341 function PMA_DBI_get_proto_info($link = null)
343 if (null === $link) {
344 if (isset($GLOBALS['userlink'])) {
345 $link = $GLOBALS['userlink'];
346 } else {
347 return false;
350 return mysqli_get_proto_info($link);
354 * returns a string that represents the client library version
355 * @uses mysqli_get_client_info()
356 * @return string MySQL client library version
358 function PMA_DBI_get_client_info()
360 return mysqli_get_client_info();
364 * returns last error message or false if no errors occured
366 * @uses PMA_DBI_convert_message()
367 * @uses $GLOBALS['errno']
368 * @uses $GLOBALS['userlink']
369 * @uses $GLOBALS['strServerNotResponding']
370 * @uses $GLOBALS['strSocketProblem']
371 * @uses mysqli_errno()
372 * @uses mysqli_error()
373 * @uses mysqli_connect_errno()
374 * @uses mysqli_connect_error()
375 * @uses defined()
376 * @param resource $link mysql link
377 * @return string|boolean $error or false
379 function PMA_DBI_getError($link = null)
381 $GLOBALS['errno'] = 0;
383 if (null === $link && isset($GLOBALS['userlink'])) {
384 $link =& $GLOBALS['userlink'];
385 // Do not stop now. We still can get the error code
386 // with mysqli_connect_errno()
387 // } else {
388 // return false;
391 if (null !== $link) {
392 $error_number = mysqli_errno($link);
393 $error_message = mysqli_error($link);
394 } else {
395 $error_number = mysqli_connect_errno();
396 $error_message = mysqli_connect_error();
398 if (0 == $error_number) {
399 return false;
402 // keep the error number for further check after the call to PMA_DBI_getError()
403 $GLOBALS['errno'] = $error_number;
405 if (! empty($error_message)) {
406 $error_message = PMA_DBI_convert_message($error_message);
409 if ($error_number == 2002) {
410 $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'] . ' ' . $GLOBALS['strSocketProblem'];
411 } else {
412 $error = '#' . ((string) $error_number) . ' - ' . $error_message;
414 return $error;
419 * @param object mysqli result $result
421 function PMA_DBI_num_rows($result)
423 // see the note for PMA_DBI_try_query();
424 if (!is_bool($result)) {
425 return @mysqli_num_rows($result);
426 } else {
427 return 0;
432 * returns last inserted auto_increment id for given $link or $GLOBALS['userlink']
434 * @uses $GLOBALS['userlink']
435 * @uses mysqli_insert_id()
436 * @param object mysqli $link the mysqli object
437 * @return string ineteger
439 function PMA_DBI_insert_id($link = '')
441 if (empty($link)) {
442 if (isset($GLOBALS['userlink'])) {
443 $link = $GLOBALS['userlink'];
444 } else {
445 return false;
448 return mysqli_insert_id($link);
452 * returns the number of rows affected by last query
454 * @uses $GLOBALS['userlink']
455 * @uses mysqli_affected_rows()
456 * @param object mysqli $link the mysqli object
457 * @return string integer
459 function PMA_DBI_affected_rows($link = null)
461 if (empty($link)) {
462 if (isset($GLOBALS['userlink'])) {
463 $link = $GLOBALS['userlink'];
464 } else {
465 return false;
468 return mysqli_affected_rows($link);
472 * returns metainfo for fields in $result
474 * @todo preserve orignal flags value
475 * @uses PMA_DBI_field_flags()
476 * @uses MYSQLI_TYPE_*
477 * @uses MYSQLI_MULTIPLE_KEY_FLAG
478 * @uses MYSQLI_PRI_KEY_FLAG
479 * @uses MYSQLI_UNIQUE_KEY_FLAG
480 * @uses MYSQLI_NOT_NULL_FLAG
481 * @uses MYSQLI_UNSIGNED_FLAG
482 * @uses MYSQLI_ZEROFILL_FLAG
483 * @uses MYSQLI_NUM_FLAG
484 * @uses MYSQLI_TYPE_BLOB
485 * @uses MYSQLI_BLOB_FLAG
486 * @uses defined()
487 * @uses mysqli_fetch_fields()
488 * @uses is_array()
489 * @param object mysqli result $result
490 * @return array meta info for fields in $result
492 function PMA_DBI_get_fields_meta($result)
494 // Build an associative array for a type look up
495 $typeAr = array();
496 $typeAr[MYSQLI_TYPE_DECIMAL] = 'real';
497 $typeAr[MYSQLI_TYPE_NEWDECIMAL] = 'real';
498 $typeAr[MYSQLI_TYPE_BIT] = 'int';
499 $typeAr[MYSQLI_TYPE_TINY] = 'int';
500 $typeAr[MYSQLI_TYPE_SHORT] = 'int';
501 $typeAr[MYSQLI_TYPE_LONG] = 'int';
502 $typeAr[MYSQLI_TYPE_FLOAT] = 'real';
503 $typeAr[MYSQLI_TYPE_DOUBLE] = 'real';
504 $typeAr[MYSQLI_TYPE_NULL] = 'null';
505 $typeAr[MYSQLI_TYPE_TIMESTAMP] = 'timestamp';
506 $typeAr[MYSQLI_TYPE_LONGLONG] = 'int';
507 $typeAr[MYSQLI_TYPE_INT24] = 'int';
508 $typeAr[MYSQLI_TYPE_DATE] = 'date';
509 $typeAr[MYSQLI_TYPE_TIME] = 'time';
510 $typeAr[MYSQLI_TYPE_DATETIME] = 'datetime';
511 $typeAr[MYSQLI_TYPE_YEAR] = 'year';
512 $typeAr[MYSQLI_TYPE_NEWDATE] = 'date';
513 $typeAr[MYSQLI_TYPE_ENUM] = 'unknown';
514 $typeAr[MYSQLI_TYPE_SET] = 'unknown';
515 $typeAr[MYSQLI_TYPE_TINY_BLOB] = 'blob';
516 $typeAr[MYSQLI_TYPE_MEDIUM_BLOB] = 'blob';
517 $typeAr[MYSQLI_TYPE_LONG_BLOB] = 'blob';
518 $typeAr[MYSQLI_TYPE_BLOB] = 'blob';
519 $typeAr[MYSQLI_TYPE_VAR_STRING] = 'string';
520 $typeAr[MYSQLI_TYPE_STRING] = 'string';
521 // MySQL returns MYSQLI_TYPE_STRING for CHAR
522 // and MYSQLI_TYPE_CHAR === MYSQLI_TYPE_TINY
523 // so this would override TINYINT and mark all TINYINT as string
524 // https://sf.net/tracker/?func=detail&aid=1532111&group_id=23067&atid=377408
525 //$typeAr[MYSQLI_TYPE_CHAR] = 'string';
526 $typeAr[MYSQLI_TYPE_GEOMETRY] = 'unknown';
527 $typeAr[MYSQLI_TYPE_BIT] = 'bit';
529 $fields = mysqli_fetch_fields($result);
531 // this happens sometimes (seen under MySQL 4.0.25)
532 if (!is_array($fields)) {
533 return false;
536 foreach ($fields as $k => $field) {
537 $fields[$k]->_type = $field->type;
538 $fields[$k]->type = $typeAr[$field->type];
539 $fields[$k]->_flags = $field->flags;
540 $fields[$k]->flags = PMA_DBI_field_flags($result, $k);
542 // Enhance the field objects for mysql-extension compatibilty
543 //$flags = explode(' ', $fields[$k]->flags);
544 //array_unshift($flags, 'dummy');
545 $fields[$k]->multiple_key
546 = (int) (bool) ($fields[$k]->_flags & MYSQLI_MULTIPLE_KEY_FLAG);
547 $fields[$k]->primary_key
548 = (int) (bool) ($fields[$k]->_flags & MYSQLI_PRI_KEY_FLAG);
549 $fields[$k]->unique_key
550 = (int) (bool) ($fields[$k]->_flags & MYSQLI_UNIQUE_KEY_FLAG);
551 $fields[$k]->not_null
552 = (int) (bool) ($fields[$k]->_flags & MYSQLI_NOT_NULL_FLAG);
553 $fields[$k]->unsigned
554 = (int) (bool) ($fields[$k]->_flags & MYSQLI_UNSIGNED_FLAG);
555 $fields[$k]->zerofill
556 = (int) (bool) ($fields[$k]->_flags & MYSQLI_ZEROFILL_FLAG);
557 $fields[$k]->numeric
558 = (int) (bool) ($fields[$k]->_flags & MYSQLI_NUM_FLAG);
559 $fields[$k]->blob
560 = (int) (bool) ($fields[$k]->_flags & MYSQLI_BLOB_FLAG);
562 return $fields;
566 * return number of fields in given $result
568 * @param object mysqli result $result
569 * @return integer field count
571 function PMA_DBI_num_fields($result)
573 return mysqli_num_fields($result);
577 * returns the length of the given field $i in $result
579 * @uses mysqli_fetch_field_direct()
580 * @param object mysqli result $result
581 * @param integer $i field
582 * @return integer length of field
584 function PMA_DBI_field_len($result, $i)
586 return mysqli_fetch_field_direct($result, $i)->length;
590 * returns name of $i. field in $result
592 * @uses mysqli_fetch_field_direct()
593 * @param object mysqli result $result
594 * @param integer $i field
595 * @return string name of $i. field in $result
597 function PMA_DBI_field_name($result, $i)
599 return mysqli_fetch_field_direct($result, $i)->name;
603 * returns concatenated string of human readable field flags
605 * @uses MYSQLI_UNIQUE_KEY_FLAG
606 * @uses MYSQLI_NUM_FLAG
607 * @uses MYSQLI_PART_KEY_FLAG
608 * @uses MYSQLI_TYPE_SET
609 * @uses MYSQLI_TIMESTAMP_FLAG
610 * @uses MYSQLI_AUTO_INCREMENT_FLAG
611 * @uses MYSQLI_TYPE_ENUM
612 * @uses MYSQLI_ZEROFILL_FLAG
613 * @uses MYSQLI_UNSIGNED_FLAG
614 * @uses MYSQLI_BLOB_FLAG
615 * @uses MYSQLI_MULTIPLE_KEY_FLAG
616 * @uses MYSQLI_UNIQUE_KEY_FLAG
617 * @uses MYSQLI_PRI_KEY_FLAG
618 * @uses MYSQLI_NOT_NULL_FLAG
619 * @uses MYSQLI_TYPE_BLOB
620 * @uses MYSQLI_TYPE_MEDIUM_BLOB
621 * @uses MYSQLI_TYPE_LONG_BLOB
622 * @uses MYSQLI_TYPE_VAR_STRING
623 * @uses MYSQLI_TYPE_STRING
624 * @uses mysqli_fetch_field_direct()
625 * @param object mysqli result $result
626 * @param integer $i field
627 * @return string field flags
629 function PMA_DBI_field_flags($result, $i)
631 // This is missing from PHP 5.2.5, see http://bugs.php.net/bug.php?id=44846
632 if (! defined('MYSQLI_ENUM_FLAG')) {
633 define('MYSQLI_ENUM_FLAG', 256); // see MySQL source include/mysql_com.h
635 $f = mysqli_fetch_field_direct($result, $i);
636 $type = $f->type;
637 $charsetnr = $f->charsetnr;
638 $f = $f->flags;
639 $flags = '';
640 if ($f & MYSQLI_UNIQUE_KEY_FLAG) { $flags .= 'unique ';}
641 if ($f & MYSQLI_NUM_FLAG) { $flags .= 'num ';}
642 if ($f & MYSQLI_PART_KEY_FLAG) { $flags .= 'part_key ';}
643 if ($f & MYSQLI_SET_FLAG) { $flags .= 'set ';}
644 if ($f & MYSQLI_TIMESTAMP_FLAG) { $flags .= 'timestamp ';}
645 if ($f & MYSQLI_AUTO_INCREMENT_FLAG) { $flags .= 'auto_increment ';}
646 if ($f & MYSQLI_ENUM_FLAG) { $flags .= 'enum ';}
647 // See http://dev.mysql.com/doc/refman/6.0/en/c-api-datatypes.html:
648 // to determine if a string is binary, we should not use MYSQLI_BINARY_FLAG
649 // but instead the charsetnr member of the MYSQL_FIELD
650 // structure. Watch out: some types like DATE returns 63 in charsetnr
651 // so we have to check also the type.
652 // Unfortunately there is no equivalent in the mysql extension.
653 if (($type == MYSQLI_TYPE_TINY_BLOB || $type == MYSQLI_TYPE_BLOB || $type == MYSQLI_TYPE_MEDIUM_BLOB || $type == MYSQLI_TYPE_LONG_BLOB || $type == MYSQLI_TYPE_VAR_STRING || $type == MYSQLI_TYPE_STRING) && 63 == $charsetnr) { $flags .= 'binary ';}
654 if ($f & MYSQLI_ZEROFILL_FLAG) { $flags .= 'zerofill ';}
655 if ($f & MYSQLI_UNSIGNED_FLAG) { $flags .= 'unsigned ';}
656 if ($f & MYSQLI_BLOB_FLAG) { $flags .= 'blob ';}
657 if ($f & MYSQLI_MULTIPLE_KEY_FLAG) { $flags .= 'multiple_key ';}
658 if ($f & MYSQLI_UNIQUE_KEY_FLAG) { $flags .= 'unique_key ';}
659 if ($f & MYSQLI_PRI_KEY_FLAG) { $flags .= 'primary_key ';}
660 if ($f & MYSQLI_NOT_NULL_FLAG) { $flags .= 'not_null ';}
661 return trim($flags);