From 6a6953899e01cefedd57d464c8cc85bb99cb0271 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Sat, 17 Jan 2004 23:04:34 +0000 Subject: [PATCH] Freed main.php from all direct calls to mysql_wrappers or the MySQL API. --- ChangeLog | 9 +++-- libraries/common.lib.php | 16 +++----- libraries/dbi/mysql.dbi.lib.php | 68 ++++++++++++++++++++++++++++++++ libraries/defines_mysql.lib.php | 40 ------------------- libraries/mysql_wrappers.lib.php | 37 ----------------- main.php | 44 +++++++++++---------- 6 files changed, 102 insertions(+), 112 deletions(-) delete mode 100644 libraries/defines_mysql.lib.php diff --git a/ChangeLog b/ChangeLog index a7d6dd107..aa37e600b 100755 --- a/ChangeLog +++ b/ChangeLog @@ -7,15 +7,16 @@ $Source$ 2004-01-17 Michal Cihar * lang/czech: Updated. - + 2004-01-17 Alexander M. Turek * config.inc.php, Documentation.html, footer.inc.php, lang/*.inc.php, libraries/common.lib.php, libraries/config_import.lib.php, libraries/database_interface.lib.php, libraries/defines.lib.php, + libraries/defines_mysql.lib.php, libraries/mysql_wrappers.lib.php, libraries/auth/config.auth.lib.php, libraries/auth/cookie.lib.php, - libraries/dbi/mysql.dbi.lib.php: Database abstraction, part I - Added - new procedures for connecting and querying. - * main.php: Changed all calls to PMA_mysql_query by the new functions. + libraries/dbi/mysql.dbi.lib.php: Database abstraction. + * main.php: This script now accesses MySQL exclusivly through the new DBI + functions. 2004-01-14 Marc Delisle * mult_submits.inc.php: bug 876805, dropping a field with the diff --git a/libraries/common.lib.php b/libraries/common.lib.php index 3ec6c2dd2..a8dca8e23 100644 --- a/libraries/common.lib.php +++ b/libraries/common.lib.php @@ -1007,27 +1007,23 @@ if ($is_minimum_common == FALSE) { // scripts) if ($cfg['Server']['controluser'] != '') { $dbh = PMA_DBI_connect($cfg['Server']['controluser'], $cfg['Server']['controlpass']); - } // end if + } // end if ... else // Pass #1 of DB-Config to read in master level DB-Config will go here // Robbat2 - May 11, 2002 // Connects to the server (validates user's login) $userlink = PMA_DBI_connect($cfg['Server']['user'], $cfg['Server']['password']); + + if (empty($dbh)) { + $dbh = $userlink; + } // Pass #2 of DB-Config to read in user level DB-Config will go here // Robbat2 - May 11, 2002 @ini_set('track_errors', $bkp_track_err); - - // If controluser isn't defined, use the current user settings to get - // his rights - if ($cfg['Server']['controluser'] == '') { - $dbh = $userlink; - } - - // Gets the mysql release number - require_once('./libraries/defines_mysql.lib.php'); + unset($bkp_track_err); /** * SQL Parser code diff --git a/libraries/dbi/mysql.dbi.lib.php b/libraries/dbi/mysql.dbi.lib.php index 1305075cb..d4c07f671 100644 --- a/libraries/dbi/mysql.dbi.lib.php +++ b/libraries/dbi/mysql.dbi.lib.php @@ -63,6 +63,23 @@ function PMA_DBI_connect($user, $password) { PMA_auth_fails(); } // end if + if (!defined('PMA_MYSQL_INT_VERSION')) { + $result = PMA_DBI_try_query('SELECT VERSION() AS version', $link); + if ($result != FALSE && @mysql_num_rows($result) > 0) { + $row = PMA_DBI_fetch_assoc($result); + $match = explode('.', $row['version']); + mysql_free_result($result); + } + if (!isset($row)) { + define('PMA_MYSQL_INT_VERSION', 32332); + define('PMA_MYSQL_STR_VERSION', '3.23.32'); + } else{ + define('PMA_MYSQL_INT_VERSION', (int)sprintf('%d%02d%02d', $match[0], $match[1], intval($match[2]))); + define('PMA_MYSQL_STR_VERSION', $row['version']); + unset($result, $row, $match); + } + } + return $link; } @@ -77,6 +94,57 @@ function PMA_DBI_try_query($query, $link = '') { return mysql_query(PMA_convert_charset($query), $link); } +// The following function is meant for internal use only. +// Do not call it from outside this library! +function PMA_mysql_fetch_array($result, $type = FALSE) { + global $cfg, $allow_recoding, $charset, $convcharset; + + if ($type != FALSE) { + $data = mysql_fetch_array($result, $type); + } else { + $data = mysql_fetch_array($result); + } + if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) { + /* No recoding -> return data as we got them */ + return $data; + } else { + $ret = array(); + $num = mysql_num_fields($result); + $i = 0; + for($i = 0; $i < $num; $i++) { + $meta = mysql_fetch_field($result); + $name = mysql_field_name($result, $i); + if (!$meta) { + /* No meta information available -> we guess that it should be converted */ + if (isset($data[$i])) $ret[$i] = PMA_convert_display_charset($data[$i]); + if (isset($data[$name])) $ret[PMA_convert_display_charset($name)] = PMA_convert_display_charset($data[$name]); + } else { + /* Meta information available -> check type of field and convert it according to the type */ + if ($meta->blob || stristr($meta->type, 'BINARY')) { + if (isset($data[$i])) $ret[$i] = $data[$i]; + if (isset($data[$name])) $ret[PMA_convert_display_charset($name)] = $data[$name]; + } else { + if (isset($data[$i])) $ret[$i] = PMA_convert_display_charset($data[$i]); + if (isset($data[$name])) $ret[PMA_convert_display_charset($name)] = PMA_convert_display_charset($data[$name]); + } + } + } + return $ret; + } +} + +function PMA_DBI_fetch_assoc($result) { + return PMA_mysql_fetch_array($result, MYSQL_ASSOC); +} + +function PMA_DBI_fetch_row($result) { + return PMA_mysql_fetch_array($result, MYSQL_NUM); +} + +function PMA_DBI_free_result($result) { + return mysql_free_result($result); +} + function PMA_DBI_getError($link = '') { if (empty($link)) { if (isset($GLOBALS['userlink'])) { diff --git a/libraries/defines_mysql.lib.php b/libraries/defines_mysql.lib.php deleted file mode 100644 index 58f27899a..000000000 --- a/libraries/defines_mysql.lib.php +++ /dev/null @@ -1,40 +0,0 @@ - 0) { - $row = PMA_mysql_fetch_array($result); - $match = explode('.', $row['version']); - mysql_free_result($result); - } - } // end server id is defined case - - if (!isset($match) || !isset($match[0])) { - $match[0] = 3; - } - if (!isset($match[1])) { - $match[1] = 23; - } - if (!isset($match[2])) { - $match[2] = 32; - } - - if(!isset($row)) { - $row['version'] = '3.23.32'; - } - - define('PMA_MYSQL_INT_VERSION', (int)sprintf('%d%02d%02d', $match[0], $match[1], intval($match[2]))); - define('PMA_MYSQL_STR_VERSION', $row['version']); - unset($result, $row, $match); -} - -?> diff --git a/libraries/mysql_wrappers.lib.php b/libraries/mysql_wrappers.lib.php index e273edafd..a31154137 100644 --- a/libraries/mysql_wrappers.lib.php +++ b/libraries/mysql_wrappers.lib.php @@ -29,43 +29,6 @@ function PMA_mysql_error($id = FALSE) { return FALSE; } -function PMA_mysql_fetch_array($result, $type = FALSE) { - global $cfg, $allow_recoding, $charset, $convcharset; - - if ($type != FALSE) { - $data = mysql_fetch_array($result, $type); - } else { - $data = mysql_fetch_array($result); - } - if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) { - /* No recoding -> return data as we got them */ - return $data; - } else { - $ret = array(); - $num = mysql_num_fields($result); - $i = 0; - for($i = 0; $i < $num; $i++) { - $meta = mysql_fetch_field($result); - $name = mysql_field_name($result, $i); - if (!$meta) { - /* No meta information available -> we guess that it should be converted */ - if (isset($data[$i])) $ret[$i] = PMA_convert_display_charset($data[$i]); - if (isset($data[$name])) $ret[PMA_convert_display_charset($name)] = PMA_convert_display_charset($data[$name]); - } else { - /* Meta information available -> check type of field and convert it according to the type */ - if ($meta->blob || stristr($meta->type, 'BINARY')) { - if (isset($data[$i])) $ret[$i] = $data[$i]; - if (isset($data[$name])) $ret[PMA_convert_display_charset($name)] = $data[$name]; - } else { - if (isset($data[$i])) $ret[$i] = PMA_convert_display_charset($data[$i]); - if (isset($data[$name])) $ret[PMA_convert_display_charset($name)] = PMA_convert_display_charset($data[$name]); - } - } - } - return $ret; - } -} - function PMA_mysql_fetch_field($result , $field_offset = FALSE) { if ($field_offset != FALSE) { return PMA_convert_display_charset(mysql_fetch_field($result, $field_offset)); diff --git a/main.php b/main.php index e40c469a0..816763622 100644 --- a/main.php +++ b/main.php @@ -71,12 +71,15 @@ if ($server > 0) { // if (!empty($cfg['Server']['socket']) && PMA_PHP_INT_VERSION >= 30010) { // $server_info .= ':' . $cfg['Server']['socket']; // } - $local_query = 'SELECT VERSION() as version, USER() as user'; - $res = PMA_DBI_query($local_query); - $mysql_cur_user_and_host = PMA_mysql_result($res, 0, 'user'); + $res = PMA_DBI_query('SELECT USER();'); + $row = PMA_DBI_fetch_row($res); + $mysql_cur_user_and_host = $row[0]; $mysql_cur_user = substr($mysql_cur_user_and_host, 0, strrpos($mysql_cur_user_and_host, '@')); - $full_string = str_replace('%pma_s1%', PMA_mysql_result($res, 0, 'version'), $strMySQLServerProcess); + PMA_DBI_free_result($res); + unset($res, $row); + + $full_string = str_replace('%pma_s1%', PMA_MYSQL_STR_VERSION, $strMySQLServerProcess); $full_string = str_replace('%pma_s2%', $server_info, $full_string); $full_string = str_replace('%pma_s3%', $mysql_cur_user_and_host, $full_string); @@ -88,13 +91,14 @@ if ($server > 0) { * Reload mysql (flush privileges) */ if (($server > 0) && isset($mode) && ($mode == 'reload')) { - $result = PMA_DBI_query('FLUSH PRIVILEGES'); // Debug: or PMA_mysqlDie('', 'FLUSH PRIVILEGES', FALSE, 'main.php?' . PMA_generate_common_url()); + $result = PMA_DBI_query('FLUSH PRIVILEGES'); echo '

'; if ($result != 0) { echo $strMySQLReloaded; } else { echo $strReloadFailed; } + unset($result); echo '

' . "\n\n"; } @@ -179,24 +183,19 @@ if ($server > 0) { // (even if they cannot see the tables) $is_superuser = PMA_DBI_try_query('SELECT COUNT(*) FROM mysql.user', $userlink); if ($dbh) { - $local_query = 'SELECT Create_priv, Process_priv, Reload_priv FROM mysql.user WHERE User = \'' . PMA_sqlAddslashes($mysql_cur_user) . '\''; + $local_query = 'SELECT Create_priv, Reload_priv FROM mysql.user WHERE User = \'' . PMA_sqlAddslashes($mysql_cur_user) . '\''; $rs_usr = PMA_DBI_try_query($local_query, $dbh); // Debug: or PMA_mysqlDie('', $local_query, FALSE); if ($rs_usr) { - while ($result_usr = PMA_mysql_fetch_array($rs_usr)) { + while ($result_usr = PMA_DBI_fetch_assoc($rs_usr)) { if (!$is_create_priv) { $is_create_priv = ($result_usr['Create_priv'] == 'Y'); } - /* 02-12-09 rabus: Every user has access to the process list - - at least to its own :-) - if (!$is_process_priv) { - $is_process_priv = ($result_usr['Process_priv'] == 'Y'); - } - */ if (!$is_reload_priv) { $is_reload_priv = ($result_usr['Reload_priv'] == 'Y'); } } // end while - mysql_free_result($rs_usr); + PMA_DBI_free_result($rs_usr); + unset($rs_usr, $result_usr); } // end if } // end if // If the user has Create priv on a inexistant db, show him in the dialog @@ -208,16 +207,17 @@ if ($server > 0) { if ($rs_usr) { $re0 = '(^|(\\\\\\\\)+|[^\])'; // non-escaped wildcards $re1 = '(^|[^\])(\\\)+'; // escaped wildcards - while ($row = PMA_mysql_fetch_array($rs_usr)) { + while ($row = PMA_DBI_fetch_assoc($rs_usr)) { if (ereg($re0 . '(%|_)', $row['Db']) - || (!PMA_mysql_select_db(ereg_replace($re1 . '(%|_)', '\\1\\3', $row['Db']), $userlink) && @mysql_errno() != 1044)) { + || (!PMA_DBI_try_query('USE ' . ereg_replace($re1 . '(%|_)', '\\1\\3', $row['Db'])) && substr(PMA_DBI_getError(), 1, 4) != 1044)) { $db_to_create = ereg_replace($re0 . '%', '\\1...', ereg_replace($re0 . '_', '\\1?', $row['Db'])); $db_to_create = ereg_replace($re1 . '(%|_)', '\\1\\3', $db_to_create); $is_create_priv = TRUE; break; } // end if } // end while - mysql_free_result($rs_usr); + PMA_DBI_free_result($rs_usr); + unset($rs_usr, $row, $re0, $re1); } // end if else { // Finally, let's try to get the user's privileges by using SHOW @@ -231,10 +231,11 @@ if ($server > 0) { $local_query = 'SHOW GRANTS FOR ' . $mysql_cur_user; $rs_usr = PMA_DBI_try_query($local_query, $dbh); } + unset($local_query); if ($rs_usr) { $re0 = '(^|(\\\\\\\\)+|[^\])'; // non-escaped wildcards $re1 = '(^|[^\])(\\\)+'; // escaped wildcards - while ($row = PMA_mysql_fetch_row($rs_usr)) { + while ($row = PMA_DBI_fetch_row($rs_usr)) { $show_grants_dbname = substr($row[0], strpos($row[0], ' ON ') + 4,(strpos($row[0], '.', strpos($row[0], ' ON ')) - strpos($row[0], ' ON ') - 4)); $show_grants_str = substr($row[0],6,(strpos($row[0],' ON ')-6)); if (($show_grants_str == 'ALL') || ($show_grants_str == 'ALL PRIVILEGES') || ($show_grants_str == 'CREATE') || strpos($show_grants_str, 'CREATE')) { @@ -243,7 +244,7 @@ if ($server > 0) { $db_to_create = ''; break; } // end if - else if (ereg($re0 . '%|_', $show_grants_dbname) || !PMA_mysql_select_db($show_grants_dbname, $userlink) && @mysql_errno() != 1044) { + else if (ereg($re0 . '%|_', $show_grants_dbname) || !PMA_DBI_try_query('USE ' . $show_grants_dbname) && substr(PMA_DBI_getError(), 1, 4) != 1044) { $db_to_create = ereg_replace($re0 . '%', '\\1...', ereg_replace($re0 . '_', '\\1?', $show_grants_dbname)); $db_to_create = ereg_replace($re1 . '(%|_)', '\\1\\3', $db_to_create); // and remove backquotes @@ -253,8 +254,9 @@ if ($server > 0) { } // end elseif } // end if } // end while - unset($show_grants_dbname, $show_grants_str); - mysql_free_result($rs_usr); + unset($show_grants_dbname, $show_grants_str, $re0, $re1); + PMA_DBI_free_result($rs_usr); + unset($rs_usr); } // end if } // end elseif } // end if