From cd899dea267af52c3b6aee4291a69a81691f3fc6 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Sat, 23 Jun 2012 22:01:49 +0530 Subject: [PATCH 001/102] code refactoring for PMA_displayColumnPrivs function in server_privileges-php file --- server_privileges.php | 52 ++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/server_privileges.php b/server_privileges.php index 4b302af73e..21b896c93e 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -454,45 +454,47 @@ function PMA_extractPrivInfo($row = '', $enableHTML = false) /** * Displays on which column(s) a table-specific privilege is granted * - * @param array $columns - * @param array $row - * @param string $name_for_select - * @param string $priv_for_header - * @param string $name - * @param string $name_for_dfn - * @param string $name_for_current + * @param array $columns columns array + * @param array $row first row from result or boolean false + * @param string $name_for_select privilege types - Select_priv, Insert_priv + * Update_priv, References_priv + * @param string $priv_for_header privilege for header + * @param string $name privilege name - insert, select, update, references + * @param string $name_for_dfn name for dfn + * @param string $name_for_current name for current * - * @return void + * @return $html_output html snippet */ -function PMA_displayColumnPrivs($columns, $row, $name_for_select, +function PMA_getHtmlToDisplayColumnPrivileges($columns, $row, $name_for_select, $priv_for_header, $name, $name_for_dfn, $name_for_current ) { - echo '
' . "\n" - . '
' . "\n"; + return $html_output; } // end function @@ -640,22 +642,22 @@ function PMA_displayPrivTable($db = '*', $table = '*', $submit = true) // privs that are attached to a specific column - PMA_displayColumnPrivs( + echo PMA_getHtmlForDisplayColumnPrivileges( $columns, $row, 'Select_priv', 'SELECT', 'select', __('Allows reading data.'), 'Select' ); - PMA_displayColumnPrivs( + echo PMA_getHtmlForDisplayColumnPrivileges( $columns, $row, 'Insert_priv', 'INSERT', 'insert', __('Allows inserting and replacing data.'), 'Insert' ); - PMA_displayColumnPrivs( + echo PMA_getHtmlForDisplayColumnPrivileges( $columns, $row, 'Update_priv', 'UPDATE', 'update', __('Allows changing data.'), 'Update' ); - PMA_displayColumnPrivs( + echo PMA_getHtmlForDisplayColumnPrivileges( $columns, $row, 'References_priv', 'REFERENCES', 'references', __('Has no effect in this MySQL version.'), 'References' ); From 44c084069d2438495cda54411dcced07f50e3893 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Sun, 24 Jun 2012 00:45:28 +0530 Subject: [PATCH 002/102] some refactoring for PMA_displayPrivTable function in server_privileges script --- server_privileges.php | 200 ++++++++++++++++++++++-------------------- 1 file changed, 105 insertions(+), 95 deletions(-) diff --git a/server_privileges.php b/server_privileges.php index 21b896c93e..9cdfdbcc3a 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -497,7 +497,36 @@ function PMA_getHtmlToDisplayColumnPrivileges($columns, $row, $name_for_select, return $html_output; } // end function - +/** + * Get sql query for display privileges table + * + * @param string $db the database + * @param string $table the table + * + * @return string sql query + */ +function PMA_getSqlQueryForDisplayPrivTable($db, $table) +{ + $username = $GLOBALS['username']; + $hostname = $GLOBALS['hostname']; + if ($db == '*') { + return "SELECT * FROM `mysql`.`user`" + ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" + ." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "';"; + } elseif ($table == '*') { + return "SELECT * FROM `mysql`.`db`" + ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" + ." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "'" + ." AND '" . PMA_unescapeMysqlWildcards($db) . "'" + ." LIKE `Db`;"; + } + return "SELECT `Table_priv`" + ." FROM `mysql`.`tables_priv`" + ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" + ." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "'" + ." AND `Db` = '" . PMA_unescapeMysqlWildcards($db) . "'" + ." AND `Table_name` = '" . PMA_sqlAddSlashes($table) . "';"; +} /** * Displays the privileges form table * @@ -508,37 +537,19 @@ function PMA_getHtmlToDisplayColumnPrivileges($columns, $row, $name_for_select, * @global array $cfg the phpMyAdmin configuration * @global ressource $user_link the database connection * - * @return void + * @return string html snippet */ -function PMA_displayPrivTable($db = '*', $table = '*', $submit = true) +function PMA_getHtmlToDisplayPrivilegesTable($db = '*', $table = '*', $submit = true) { global $random_n; - + $html_output = ''; + if ($db == '*') { $table = '*'; } if (isset($GLOBALS['username'])) { - $username = $GLOBALS['username']; - $hostname = $GLOBALS['hostname']; - if ($db == '*') { - $sql_query = "SELECT * FROM `mysql`.`user`" - ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" - ." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "';"; - } elseif ($table == '*') { - $sql_query = "SELECT * FROM `mysql`.`db`" - ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" - ." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "'" - ." AND '" . PMA_unescapeMysqlWildcards($db) . "'" - ." LIKE `Db`;"; - } else { - $sql_query = "SELECT `Table_priv`" - ." FROM `mysql`.`tables_priv`" - ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" - ." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "'" - ." AND `Db` = '" . PMA_unescapeMysqlWildcards($db) . "'" - ." AND `Table_name` = '" . PMA_sqlAddSlashes($table) . "';"; - } + $sql_query = PMA_getSqlQueryForDisplayPrivTable($db, $table); $row = PMA_DBI_fetch_single_row($sql_query); } if (empty($row)) { @@ -632,39 +643,37 @@ function PMA_displayPrivTable($db = '*', $table = '*', $submit = true) PMA_DBI_free_result($res); unset($res, $row1, $current); - echo '' . "\n" + $html_output .= '' . "\n" . '' . "\n" . '
' . "\n" . ' ' . __('Table-specific privileges') . PMA_showHint(__('Note: MySQL privilege names are expressed in English')) . '' . "\n"; - - // privs that are attached to a specific column - echo PMA_getHtmlForDisplayColumnPrivileges( + $html_output .= PMA_getHtmlForDisplayColumnPrivileges( $columns, $row, 'Select_priv', 'SELECT', 'select', __('Allows reading data.'), 'Select' ); - echo PMA_getHtmlForDisplayColumnPrivileges( + $html_output .= PMA_getHtmlForDisplayColumnPrivileges( $columns, $row, 'Insert_priv', 'INSERT', 'insert', __('Allows inserting and replacing data.'), 'Insert' ); - echo PMA_getHtmlForDisplayColumnPrivileges( + $html_output .= PMA_getHtmlForDisplayColumnPrivileges( $columns, $row, 'Update_priv', 'UPDATE', 'update', __('Allows changing data.'), 'Update' ); - echo PMA_getHtmlForDisplayColumnPrivileges( + $html_output .= PMA_getHtmlForDisplayColumnPrivileges( $columns, $row, 'References_priv', 'REFERENCES', 'references', __('Has no effect in this MySQL version.'), 'References' ); // privs that are not attached to a specific column - echo '
' . "\n"; + $html_output .= '
' . "\n"; foreach ($row as $current_grant => $current_grant_value) { $grant_type = substr($current_grant, 0, (strlen($current_grant) - 5)); if (in_array($grant_type, array('Select', 'Insert', 'Update', 'References'))) { @@ -684,30 +693,30 @@ function PMA_displayPrivTable($db = '*', $table = '*', $submit = true) $tmp_current_grant = $current_grant; } - echo '
' . "\n" - . ' ' . "\n"; - echo ' ' . "\n" - . '
' . "\n"; + . '">' . strtoupper(substr($current_grant, 0, strlen($current_grant) - 5)) . '
' . "\n" + . '
' . "\n"; } // end foreach () - echo '
' . "\n"; + $html_output .= '' . "\n"; // for Safari 2.0.2 - echo '
' . "\n"; + $html_output .= '
' . "\n"; } else { @@ -770,84 +779,85 @@ function PMA_displayPrivTable($db = '*', $table = '*', $submit = true) $privTable[2][] = array('Repl_slave', 'REPLICATION SLAVE', __('Needed for the replication slaves.')); $privTable[2][] = array('Create_user', 'CREATE USER', __('Allows creating, dropping and renaming user accounts.')); } - echo '' . "\n" - . '
' . "\n" - . ' ' . "\n" - . ' ' + . '
' . "\n" + . '' . "\n" + . ' ' . ($db == '*' ? __('Global privileges') : ($table == '*' ? __('Database-specific privileges') : __('Table-specific privileges'))) . "\n" - . ' (' . __('Check All') . ' /' . "\n" - . ' ' . __('Uncheck All') . ')' . "\n" - . ' ' . "\n" - . '

' . __('Note: MySQL privilege names are expressed in English') . '

' . "\n"; + . '' . "\n" + . '

' . __('Note: MySQL privilege names are expressed in English') . '

' . "\n"; // Output the Global privilege tables with checkboxes foreach ($privTable as $i => $table) { - echo '
' . "\n" - . ' ' . __($privTable_names[$i]) . '' . "\n"; + $html_output .= '
' . "\n" + . '' . __($privTable_names[$i]) . '' . "\n"; foreach ($table as $priv) { - echo '
' . "\n" - . ' ' . "\n" - . ' ' . "\n" - . '
' . "\n"; + $html_output .= '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n"; } - echo '
' . "\n"; + $html_output .= '
' . "\n"; } // The "Resource limits" box is not displayed for db-specific privs if ($db == '*') { - echo '
' . "\n" - . ' ' . __('Resource limits') . '' . "\n" - . '

' . __('Note: Setting these options to 0 (zero) removes the limit.') . '

' . "\n" - . '
' . "\n" - . ' ' . "\n" - . ' ' . "\n" - . '
' . "\n" - . '
' . "\n" - . ' ' . "\n" - . ' ' . "\n" - . '
' . "\n" - . '
' . "\n" - . ' ' . "\n" - . ' ' . "\n" - . '
' . "\n" - . '
' . "\n" - . ' ' . "\n" - . ' ' . "\n" - . '
' . "\n" - . '
' . "\n"; + $html_output .= '
' . "\n" + . '' . __('Resource limits') . '' . "\n" + . '

' . __('Note: Setting these options to 0 (zero) removes the limit.') . '

' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n" + . '
' . "\n"; } // for Safari 2.0.2 - echo '
' . "\n"; + $html_output .= '
' . "\n"; } - echo '
' . "\n"; + $html_output .= '
' . "\n"; if ($submit) { - echo '' . "\n"; - PMA_displayPrivTable('*', '*', false); + echo PMA_getHtmlToDisplayPrivilegesTable('*', '*', false); echo ' ' . "\n" From 9e25fa580a8604775668b9fc4800c8f9faaa0f01 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Tue, 26 Jun 2012 00:12:02 +0530 Subject: [PATCH 003/102] function for Resource limits in server_privileges --- server_privileges.php | 69 +++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 29 deletions(-) diff --git a/server_privileges.php b/server_privileges.php index f48773b3b4..ebe5c4536a 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -696,7 +696,7 @@ function PMA_getHtmlToDisplayPrivilegesTable($db = '*', $table = '*', $submit = $html_output .= '
' . "\n" . '' . __('Resource limits') . '' . "\n" - . '

' . __('Note: Setting these options to 0 (zero) removes the limit.') . '

' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n" - . '
' . "\n"; + $html_output .= PMA_getHtmlForDisplayResourceLimits($row); } // for Safari 2.0.2 $html_output .= '
' . "\n"; @@ -860,6 +833,44 @@ function PMA_getHtmlToDisplayPrivilegesTable($db = '*', $table = '*', $submit = return $html_output; } // end of the 'PMA_displayPrivTable()' function +/** + * Get HTML for "Resource limits" + * + * @param array $row first row from result or boolean false + * + * @return string html snippet + */ +function PMA_getHtmlForDisplayResourceLimits($row) +{ + return '
' . "\n" + . '' . __('Resource limits') . '' . "\n" + . '

' . __('Note: Setting these options to 0 (zero) removes the limit.') . '

' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n" + . '
' . "\n"; +} /** * Displays the fields used by the "new user" form as well as the From 1578cee9ac0c7680337b5b483a178d7f45c57746 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Tue, 26 Jun 2012 01:20:35 +0530 Subject: [PATCH 004/102] new lib file for server_privileges --- libraries/server_privileges.lib.php | 877 ++++++++++++++++++++++++++++ server_privileges.php | 877 +--------------------------- 2 files changed, 887 insertions(+), 867 deletions(-) create mode 100644 libraries/server_privileges.lib.php diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php new file mode 100644 index 0000000000..e799940d51 --- /dev/null +++ b/libraries/server_privileges.lib.php @@ -0,0 +1,877 @@ + tag with tooltips + * + * @global resource $user_link the database connection + * + * @return array + */ +function PMA_extractPrivInfo($row = '', $enableHTML = false) +{ + $grants = array( + array( + 'Select_priv', + 'SELECT', + __('Allows reading data.')), + array( + 'Insert_priv', + 'INSERT', + __('Allows inserting and replacing data.')), + array( + 'Update_priv', + 'UPDATE', + __('Allows changing data.')), + array( + 'Delete_priv', + 'DELETE', + __('Allows deleting data.')), + array( + 'Create_priv', + 'CREATE', + __('Allows creating new databases and tables.')), + array( + 'Drop_priv', + 'DROP', + __('Allows dropping databases and tables.')), + array( + 'Reload_priv', + 'RELOAD', + __('Allows reloading server settings and flushing the server\'s caches.')), + array( + 'Shutdown_priv', + 'SHUTDOWN', + __('Allows shutting down the server.')), + array( + 'Process_priv', + 'PROCESS', + __('Allows viewing processes of all users')), + array( + 'File_priv', + 'FILE', + __('Allows importing data from and exporting data into files.')), + array( + 'References_priv', + 'REFERENCES', + __('Has no effect in this MySQL version.')), + array( + 'Index_priv', + 'INDEX', + __('Allows creating and dropping indexes.')), + array( + 'Alter_priv', + 'ALTER', + __('Allows altering the structure of existing tables.')), + array( + 'Show_db_priv', + 'SHOW DATABASES', + __('Gives access to the complete list of databases.')), + array( + 'Super_priv', + 'SUPER', + __('Allows connecting, even if maximum number of connections is reached; required for most administrative operations like setting global variables or killing threads of other users.')), + array( + 'Create_tmp_table_priv', + 'CREATE TEMPORARY TABLES', + __('Allows creating temporary tables.')), + array( + 'Lock_tables_priv', + 'LOCK TABLES', + __('Allows locking tables for the current thread.')), + array( + 'Repl_slave_priv', + 'REPLICATION SLAVE', + __('Needed for the replication slaves.')), + array( + 'Repl_client_priv', + 'REPLICATION CLIENT', + __('Allows the user to ask where the slaves / masters are.')), + array( + 'Create_view_priv', + 'CREATE VIEW', + __('Allows creating new views.')), + array( + 'Event_priv', + 'EVENT', + __('Allows to set up events for the event scheduler')), + array( + 'Trigger_priv', + 'TRIGGER', + __('Allows creating and dropping triggers')), + // for table privs: + array( + 'Create View_priv', + 'CREATE VIEW', + __('Allows creating new views.')), + array( + 'Show_view_priv', + 'SHOW VIEW', + __('Allows performing SHOW CREATE VIEW queries.')), + // for table privs: + array( + 'Show view_priv', + 'SHOW VIEW', + __('Allows performing SHOW CREATE VIEW queries.')), + array( + 'Create_routine_priv', + 'CREATE ROUTINE', + __('Allows creating stored routines.')), + array( + 'Alter_routine_priv', + 'ALTER ROUTINE', + __('Allows altering and dropping stored routines.')), + array( + 'Create_user_priv', + 'CREATE USER', + __('Allows creating, dropping and renaming user accounts.')), + array( + 'Execute_priv', + 'EXECUTE', + __('Allows executing stored routines.')), + ); + + if (! empty($row) && isset($row['Table_priv'])) { + $row1 = PMA_DBI_fetch_single_row( + 'SHOW COLUMNS FROM `mysql`.`tables_priv` LIKE \'Table_priv\';', + 'ASSOC', $GLOBALS['userlink'] + ); + $av_grants = explode( + '\',\'', + substr($row1['Type'], 5, strlen($row1['Type']) - 7) + ); + unset($row1); + $users_grants = explode(',', $row['Table_priv']); + foreach ($av_grants as $current_grant) { + $row[$current_grant . '_priv'] + = in_array($current_grant, $users_grants) ? 'Y' : 'N'; + } + unset($current_grant); + unset($av_grants); + unset($users_grants); + } + $privs = array(); + $allPrivileges = true; + foreach ($grants as $current_grant) { + if ((! empty($row) && isset($row[$current_grant[0]])) + || (empty($row) && isset($GLOBALS[$current_grant[0]])) + ) { + if ((! empty($row) && $row[$current_grant[0]] == 'Y') + || (empty($row) + && ($GLOBALS[$current_grant[0]] == 'Y' + || (is_array($GLOBALS[$current_grant[0]]) + && count($GLOBALS[$current_grant[0]]) == $GLOBALS['column_count'] + && empty($GLOBALS[$current_grant[0] . '_none'])))) + ) { + if ($enableHTML) { + $privs[] = '' + . $current_grant[1] . ''; + } else { + $privs[] = $current_grant[1]; + } + } elseif (! empty($GLOBALS[$current_grant[0]]) + && is_array($GLOBALS[$current_grant[0]]) + && empty($GLOBALS[$current_grant[0] . '_none'])) { + if ($enableHTML) { + $priv_string = '' + . $current_grant[1] . ''; + } else { + $priv_string = $current_grant[1]; + } + $privs[] = $priv_string . ' (`' + . join('`, `', $GLOBALS[$current_grant[0]]) . '`)'; + } else { + $allPrivileges = false; + } + } + } + if (empty($privs)) { + if ($enableHTML) { + $privs[] = 'USAGE'; + } else { + $privs[] = 'USAGE'; + } + } elseif ($allPrivileges + && (! isset($GLOBALS['grant_count']) + || count($privs) == $GLOBALS['grant_count']) + ) { + if ($enableHTML) { + $privs = array('ALL PRIVILEGES' + ); + } else { + $privs = array('ALL PRIVILEGES'); + } + } + return $privs; +} // end of the 'PMA_extractPrivInfo()' function + +/** + * Displays on which column(s) a table-specific privilege is granted + * + * @param array $columns columns array + * @param array $row first row from result or boolean false + * @param string $name_for_select privilege types - Select_priv, Insert_priv + * Update_priv, References_priv + * @param string $priv_for_header privilege for header + * @param string $name privilege name - insert, select, update, references + * @param string $name_for_dfn name for dfn + * @param string $name_for_current name for current + * + * @return $html_output html snippet + */ +function PMA_getHtmlToDisplayColumnPrivileges($columns, $row, $name_for_select, + $priv_for_header, $name, $name_for_dfn, $name_for_current +) { + $html_output = '
' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . __('Or') . '' . "\n" + . '' . "\n" + . '
' . "\n"; + return $html_output; +} // end function + +/** + * Get sql query for display privileges table + * + * @param string $db the database + * @param string $table the table + * + * @return string sql query + */ +function PMA_getSqlQueryForDisplayPrivTable($db, $table) +{ + $username = $GLOBALS['username']; + $hostname = $GLOBALS['hostname']; + if ($db == '*') { + return "SELECT * FROM `mysql`.`user`" + ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" + ." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "';"; + } elseif ($table == '*') { + return "SELECT * FROM `mysql`.`db`" + ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" + ." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "'" + ." AND '" . PMA_unescapeMysqlWildcards($db) . "'" + ." LIKE `Db`;"; + } + return "SELECT `Table_priv`" + ." FROM `mysql`.`tables_priv`" + ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" + ." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "'" + ." AND `Db` = '" . PMA_unescapeMysqlWildcards($db) . "'" + ." AND `Table_name` = '" . PMA_sqlAddSlashes($table) . "';"; +} +/** + * Displays the privileges form table + * + * @param string $db the database + * @param string $table the table + * @param boolean $submit wheather to display the submit button or not + * + * @global array $cfg the phpMyAdmin configuration + * @global ressource $user_link the database connection + * + * @return string html snippet + */ +function PMA_getHtmlToDisplayPrivilegesTable($db = '*', $table = '*', $submit = true) +{ + global $random_n; + $html_output = ''; + + if ($db == '*') { + $table = '*'; + } + + if (isset($GLOBALS['username'])) { + $sql_query = PMA_getSqlQueryForDisplayPrivTable($db, $table); + $row = PMA_DBI_fetch_single_row($sql_query); + } + if (empty($row)) { + if ($table == '*') { + if ($db == '*') { + $sql_query = 'SHOW COLUMNS FROM `mysql`.`user`;'; + } elseif ($table == '*') { + $sql_query = 'SHOW COLUMNS FROM `mysql`.`db`;'; + } + $res = PMA_DBI_query($sql_query); + while ($row1 = PMA_DBI_fetch_row($res)) { + if (substr($row1[0], 0, 4) == 'max_') { + $row[$row1[0]] = 0; + } else { + $row[$row1[0]] = 'N'; + } + } + PMA_DBI_free_result($res); + } else { + $row = array('Table_priv' => ''); + } + } + if (isset($row['Table_priv'])) { + $row1 = PMA_DBI_fetch_single_row( + 'SHOW COLUMNS FROM `mysql`.`tables_priv` LIKE \'Table_priv\';', + 'ASSOC', $GLOBALS['userlink'] + ); + // note: in MySQL 5.0.3 we get "Create View', 'Show view'; + // the View for Create is spelled with uppercase V + // the view for Show is spelled with lowercase v + // and there is a space between the words + + $av_grants = explode( + '\',\'', + substr( + $row1['Type'], + strpos($row1['Type'], '(') + 2, + strpos($row1['Type'], ')') - strpos($row1['Type'], '(') - 3 + ) + ); + unset($row1); + $users_grants = explode(',', $row['Table_priv']); + + foreach ($av_grants as $current_grant) { + $row[$current_grant . '_priv'] + = in_array($current_grant, $users_grants) ? 'Y' : 'N'; + } + unset($row['Table_priv'], $current_grant, $av_grants, $users_grants); + + // get collumns + $res = PMA_DBI_try_query( + 'SHOW COLUMNS FROM ' + . PMA_backquote(PMA_unescapeMysqlWildcards($db)) + . '.' . PMA_backquote($table) . ';' + ); + $columns = array(); + if ($res) { + while ($row1 = PMA_DBI_fetch_row($res)) { + $columns[$row1[0]] = array( + 'Select' => false, + 'Insert' => false, + 'Update' => false, + 'References' => false + ); + } + PMA_DBI_free_result($res); + } + unset($res, $row1); + } + // t a b l e - s p e c i f i c p r i v i l e g e s + if (! empty($columns)) { + $res = PMA_DBI_query( + 'SELECT `Column_name`, `Column_priv`' + .' FROM `mysql`.`columns_priv`' + .' WHERE `User`' + .' = \'' . PMA_sqlAddSlashes($username) . "'" + .' AND `Host`' + .' = \'' . PMA_sqlAddSlashes($hostname) . "'" + .' AND `Db`' + .' = \'' . PMA_sqlAddSlashes(PMA_unescapeMysqlWildcards($db)) . "'" + .' AND `Table_name`' + .' = \'' . PMA_sqlAddSlashes($table) . '\';' + ); + + while ($row1 = PMA_DBI_fetch_row($res)) { + $row1[1] = explode(',', $row1[1]); + foreach ($row1[1] as $current) { + $columns[$row1[0]][$current] = true; + } + } + PMA_DBI_free_result($res); + unset($res, $row1, $current); + + $html_output .= '' . "\n" + . '' . "\n" + . '
' . "\n" + . ' ' . __('Table-specific privileges') + . PMA_showHint(__('Note: MySQL privilege names are expressed in English')) + . '' . "\n"; + + // privs that are attached to a specific column + $html_output .= PMA_getHtmlForDisplayColumnPrivileges( + $columns, $row, 'Select_priv', 'SELECT', + 'select', __('Allows reading data.'), 'Select' + ); + + $html_output .= PMA_getHtmlForDisplayColumnPrivileges( + $columns, $row, 'Insert_priv', 'INSERT', + 'insert', __('Allows inserting and replacing data.'), 'Insert' + ); + + $html_output .= PMA_getHtmlForDisplayColumnPrivileges( + $columns, $row, 'Update_priv', 'UPDATE', + 'update', __('Allows changing data.'), 'Update' + ); + + $html_output .= PMA_getHtmlForDisplayColumnPrivileges( + $columns, $row, 'References_priv', 'REFERENCES', 'references', + __('Has no effect in this MySQL version.'), 'References' + ); + + // privs that are not attached to a specific column + + $html_output .= '
' . "\n"; + foreach ($row as $current_grant => $current_grant_value) { + $grant_type = substr($current_grant, 0, (strlen($current_grant) - 5)); + if (in_array($grant_type, array('Select', 'Insert', 'Update', 'References'))) { + continue; + } + // make a substitution to match the messages variables; + // also we must substitute the grant we get, because we can't generate + // a form variable containing blanks (those would get changed to + // an underscore when receiving the POST) + if ($current_grant == 'Create View_priv') { + $tmp_current_grant = 'CreateView_priv'; + $current_grant = 'Create_view_priv'; + } elseif ($current_grant == 'Show view_priv') { + $tmp_current_grant = 'ShowView_priv'; + $current_grant = 'Show_view_priv'; + } else { + $tmp_current_grant = $current_grant; + } + + $html_output .= '
' . "\n" + . '' . "\n"; + + $html_output .= '' . "\n" + . '
' . "\n"; + } // end foreach () + + $html_output .= '
' . "\n"; + // for Safari 2.0.2 + $html_output .= '
' . "\n"; + + } else { + + // g l o b a l o r d b - s p e c i f i c + // + $privTable_names = array(0 => __('Data'), 1 => __('Structure'), 2 => __('Administration')); + + // d a t a + $privTable[0] = array( + array('Select', 'SELECT', __('Allows reading data.')), + array('Insert', 'INSERT', __('Allows inserting and replacing data.')), + array('Update', 'UPDATE', __('Allows changing data.')), + array('Delete', 'DELETE', __('Allows deleting data.')) + ); + if ($db == '*') { + $privTable[0][] = array('File', 'FILE', __('Allows importing data from and exporting data into files.')); + } + + // s t r u c t u r e + $privTable[1] = array( + array('Create', 'CREATE', ($table == '*' ? __('Allows creating new databases and tables.') : __('Allows creating new tables.'))), + array('Alter', 'ALTER', __('Allows altering the structure of existing tables.')), + array('Index', 'INDEX', __('Allows creating and dropping indexes.')), + array('Drop', 'DROP', ($table == '*' ? __('Allows dropping databases and tables.') : __('Allows dropping tables.'))), + array('Create_tmp_table', 'CREATE TEMPORARY TABLES', __('Allows creating temporary tables.')), + array('Show_view', 'SHOW VIEW', __('Allows performing SHOW CREATE VIEW queries.')), + array('Create_routine', 'CREATE ROUTINE', __('Allows creating stored routines.')), + array('Alter_routine', 'ALTER ROUTINE', __('Allows altering and dropping stored routines.')), + array('Execute', 'EXECUTE', __('Allows executing stored routines.')), + ); + // this one is for a db-specific priv: Create_view_priv + if (isset($row['Create_view_priv'])) { + $privTable[1][] = array('Create_view', 'CREATE VIEW', __('Allows creating new views.')); + } + // this one is for a table-specific priv: Create View_priv + if (isset($row['Create View_priv'])) { + $privTable[1][] = array('Create View', 'CREATE VIEW', __('Allows creating new views.')); + } + if (isset($row['Event_priv'])) { + // MySQL 5.1.6 + $privTable[1][] = array('Event', 'EVENT', __('Allows to set up events for the event scheduler')); + $privTable[1][] = array('Trigger', 'TRIGGER', __('Allows creating and dropping triggers')); + } + + // a d m i n i s t r a t i o n + $privTable[2] = array( + array('Grant', 'GRANT', __('Allows adding users and privileges without reloading the privilege tables.')), + ); + if ($db == '*') { + $privTable[2][] = array('Super', 'SUPER', __('Allows connecting, even if maximum number of connections is reached; required for most administrative operations like setting global variables or killing threads of other users.')); + $privTable[2][] = array('Process', 'PROCESS', __('Allows viewing processes of all users')); + $privTable[2][] = array('Reload', 'RELOAD', __('Allows reloading server settings and flushing the server\'s caches.')); + $privTable[2][] = array('Shutdown', 'SHUTDOWN', __('Allows shutting down the server.')); + $privTable[2][] = array('Show_db', 'SHOW DATABASES', __('Gives access to the complete list of databases.')); + } + $privTable[2][] = array('Lock_tables', 'LOCK TABLES', __('Allows locking tables for the current thread.')); + $privTable[2][] = array('References', 'REFERENCES', __('Has no effect in this MySQL version.')); + if ($db == '*') { + $privTable[2][] = array('Repl_client', 'REPLICATION CLIENT', __('Allows the user to ask where the slaves / masters are.')); + $privTable[2][] = array('Repl_slave', 'REPLICATION SLAVE', __('Needed for the replication slaves.')); + $privTable[2][] = array('Create_user', 'CREATE USER', __('Allows creating, dropping and renaming user accounts.')); + } + $html_output .= '' . "\n" + . '
' . "\n" + . '' . "\n" + . ' ' + . ($db == '*' + ? __('Global privileges') + : ($table == '*' + ? __('Database-specific privileges') + : __('Table-specific privileges'))) . "\n" + . '(' + . __('Check All') . ' /' . "\n" + . '' + . __('Uncheck All') . ')' . "\n" + . '' . "\n" + . '

' . __('Note: MySQL privilege names are expressed in English') . '

' . "\n"; + + // Output the Global privilege tables with checkboxes + foreach ($privTable as $i => $table) { + $html_output .= '
' . "\n" + . '' . __($privTable_names[$i]) . '' . "\n"; + foreach ($table as $priv) { + $html_output .= '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n"; + } + $html_output .= '
' . "\n"; + } + + // The "Resource limits" box is not displayed for db-specific privs + if ($db == '*') { + $html_output .= PMA_getHtmlForDisplayResourceLimits($row); + } + // for Safari 2.0.2 + $html_output .= '
' . "\n"; + } + $html_output .= '
' . "\n"; + if ($submit) { + $html_output .= '' . "\n"; + } + return $html_output; +} // end of the 'PMA_displayPrivTable()' function + +/** + * Get HTML for "Resource limits" + * + * @param array $row first row from result or boolean false + * + * @return string html snippet + */ +function PMA_getHtmlForDisplayResourceLimits($row) +{ + return '
' . "\n" + . '' . __('Resource limits') . '' . "\n" + . '

' . __('Note: Setting these options to 0 (zero) removes the limit.') . '

' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n" + . '
' . "\n"; +} + +/** + * Displays the fields used by the "new user" form as well as the + * "change login information / copy user" form. + * + * @param string $mode are we creating a new user or are we just + * changing one? (allowed values: 'new', 'change') + * + * @global array $cfg the phpMyAdmin configuration + * @global ressource $user_link the database connection + * + * @return void + */ +function PMA_getHtmlForDisplayLoginInformationFields($mode = 'new') +{ + // Get user/host name lengths + $fields_info = PMA_DBI_get_columns('mysql', 'user', null, true); + $username_length = 16; + $hostname_length = 41; + foreach ($fields_info as $val) { + if ($val['Field'] == 'User') { + strtok($val['Type'], '()'); + $v = strtok('()'); + if (is_int($v)) { + $username_length = $v; + } + } elseif ($val['Field'] == 'Host') { + strtok($val['Type'], '()'); + $v = strtok('()'); + if (is_int($v)) { + $hostname_length = $v; + } + } + } + unset($fields_info); + + if (isset($GLOBALS['username']) && strlen($GLOBALS['username']) === 0) { + $GLOBALS['pred_username'] = 'any'; + } + $html_output = '
' . "\n" + . '' . __('Login Information') . '' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . '' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . ' ' . "\n" + . '' . "\n" + . '' . "\n" + . PMA_showHint(__('When Host table is used, this field is ignored and values stored in Host table are used instead.')) + . '
' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . '' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n" + . '
' . "\n" + . '' . "\n" + . ' ' . "\n" + . '' . "\n" + . '
' . "\n" + // Generate password added here via jQuery + . '
' . "\n"; + + return $html_output; +} // end of the 'PMA_displayUserAndHostFields()' function + + +/** + * Returns all the grants for a certain user on a certain host + * Used in the export privileges for all users section + * + * @param string $user User name + * @param string $host Host name + * + * @return string containing all the grants text + */ +function PMA_getGrants($user, $host) +{ + $grants = PMA_DBI_fetch_result("SHOW GRANTS FOR '" . PMA_sqlAddSlashes($user) . "'@'" . PMA_sqlAddSlashes($host) . "'"); + $response = ''; + foreach ($grants as $one_grant) { + $response .= $one_grant . ";\n\n"; + } + return $response; +} // end of the 'PMA_getGrants()' function + +?> diff --git a/server_privileges.php b/server_privileges.php index ebe5c4536a..243e067ed0 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -10,6 +10,11 @@ */ require_once 'libraries/common.inc.php'; +/** + * functions implementation for this script + */ +require_once 'libraries/server_privileges.lib.php'; + /** * Does the common work */ @@ -188,868 +193,6 @@ if (! $is_superuser) { // a random number that will be appended to the id of the user forms $random_n = mt_rand(0, 1000000); -/** - * Escapes wildcard in a database+table specification - * before using it in a GRANT statement. - * - * Escaping a wildcard character in a GRANT is only accepted at the global - * or database level, not at table level; this is why I remove - * the escaping character. Internally, in mysql.tables_priv.Db there are - * no escaping (for example test_db) but in mysql.db you'll see test\_db - * for a db-specific privilege. - * - * @param string $dbname Database name - * @param string $tablename Table name - * - * @return string the escaped (if necessary) database.table - */ -function PMA_wildcardEscapeForGrant($dbname, $tablename) -{ - - if (! strlen($dbname)) { - $db_and_table = '*.*'; - } else { - if (strlen($tablename)) { - $db_and_table - = PMA_backquote(PMA_unescapeMysqlWildcards($dbname)) . '.' - . PMA_backquote($tablename); - } else { - $db_and_table = PMA_backquote($dbname) . '.*'; - } - } - return $db_and_table; -} - -/** - * Generates a condition on the user name - * - * @param string $initial the user's initial - * - * @return string the generated condition - */ -function PMA_rangeOfUsers($initial = '') -{ - // strtolower() is used because the User field - // might be BINARY, so LIKE would be case sensitive - if (! empty($initial)) { - $ret = " WHERE `User` LIKE '" - . PMA_sqlAddSlashes($initial, true) . "%'" - . " OR `User` LIKE '" - . PMA_sqlAddSlashes(strtolower($initial), true) . "%'"; - } else { - $ret = ''; - } - return $ret; -} // end function - -/** - * Extracts the privilege information of a priv table row - * - * @param array $row the row - * @param boolean $enableHTML add tag with tooltips - * - * @global resource $user_link the database connection - * - * @return array - */ -function PMA_extractPrivInfo($row = '', $enableHTML = false) -{ - $grants = array( - array( - 'Select_priv', - 'SELECT', - __('Allows reading data.')), - array( - 'Insert_priv', - 'INSERT', - __('Allows inserting and replacing data.')), - array( - 'Update_priv', - 'UPDATE', - __('Allows changing data.')), - array( - 'Delete_priv', - 'DELETE', - __('Allows deleting data.')), - array( - 'Create_priv', - 'CREATE', - __('Allows creating new databases and tables.')), - array( - 'Drop_priv', - 'DROP', - __('Allows dropping databases and tables.')), - array( - 'Reload_priv', - 'RELOAD', - __('Allows reloading server settings and flushing the server\'s caches.')), - array( - 'Shutdown_priv', - 'SHUTDOWN', - __('Allows shutting down the server.')), - array( - 'Process_priv', - 'PROCESS', - __('Allows viewing processes of all users')), - array( - 'File_priv', - 'FILE', - __('Allows importing data from and exporting data into files.')), - array( - 'References_priv', - 'REFERENCES', - __('Has no effect in this MySQL version.')), - array( - 'Index_priv', - 'INDEX', - __('Allows creating and dropping indexes.')), - array( - 'Alter_priv', - 'ALTER', - __('Allows altering the structure of existing tables.')), - array( - 'Show_db_priv', - 'SHOW DATABASES', - __('Gives access to the complete list of databases.')), - array( - 'Super_priv', - 'SUPER', - __('Allows connecting, even if maximum number of connections is reached; required for most administrative operations like setting global variables or killing threads of other users.')), - array( - 'Create_tmp_table_priv', - 'CREATE TEMPORARY TABLES', - __('Allows creating temporary tables.')), - array( - 'Lock_tables_priv', - 'LOCK TABLES', - __('Allows locking tables for the current thread.')), - array( - 'Repl_slave_priv', - 'REPLICATION SLAVE', - __('Needed for the replication slaves.')), - array( - 'Repl_client_priv', - 'REPLICATION CLIENT', - __('Allows the user to ask where the slaves / masters are.')), - array( - 'Create_view_priv', - 'CREATE VIEW', - __('Allows creating new views.')), - array( - 'Event_priv', - 'EVENT', - __('Allows to set up events for the event scheduler')), - array( - 'Trigger_priv', - 'TRIGGER', - __('Allows creating and dropping triggers')), - // for table privs: - array( - 'Create View_priv', - 'CREATE VIEW', - __('Allows creating new views.')), - array( - 'Show_view_priv', - 'SHOW VIEW', - __('Allows performing SHOW CREATE VIEW queries.')), - // for table privs: - array( - 'Show view_priv', - 'SHOW VIEW', - __('Allows performing SHOW CREATE VIEW queries.')), - array( - 'Create_routine_priv', - 'CREATE ROUTINE', - __('Allows creating stored routines.')), - array( - 'Alter_routine_priv', - 'ALTER ROUTINE', - __('Allows altering and dropping stored routines.')), - array( - 'Create_user_priv', - 'CREATE USER', - __('Allows creating, dropping and renaming user accounts.')), - array( - 'Execute_priv', - 'EXECUTE', - __('Allows executing stored routines.')), - ); - - if (! empty($row) && isset($row['Table_priv'])) { - $row1 = PMA_DBI_fetch_single_row( - 'SHOW COLUMNS FROM `mysql`.`tables_priv` LIKE \'Table_priv\';', - 'ASSOC', $GLOBALS['userlink'] - ); - $av_grants = explode( - '\',\'', - substr($row1['Type'], 5, strlen($row1['Type']) - 7) - ); - unset($row1); - $users_grants = explode(',', $row['Table_priv']); - foreach ($av_grants as $current_grant) { - $row[$current_grant . '_priv'] - = in_array($current_grant, $users_grants) ? 'Y' : 'N'; - } - unset($current_grant); - unset($av_grants); - unset($users_grants); - } - $privs = array(); - $allPrivileges = true; - foreach ($grants as $current_grant) { - if ((! empty($row) && isset($row[$current_grant[0]])) - || (empty($row) && isset($GLOBALS[$current_grant[0]])) - ) { - if ((! empty($row) && $row[$current_grant[0]] == 'Y') - || (empty($row) - && ($GLOBALS[$current_grant[0]] == 'Y' - || (is_array($GLOBALS[$current_grant[0]]) - && count($GLOBALS[$current_grant[0]]) == $GLOBALS['column_count'] - && empty($GLOBALS[$current_grant[0] . '_none'])))) - ) { - if ($enableHTML) { - $privs[] = '' - . $current_grant[1] . ''; - } else { - $privs[] = $current_grant[1]; - } - } elseif (! empty($GLOBALS[$current_grant[0]]) - && is_array($GLOBALS[$current_grant[0]]) - && empty($GLOBALS[$current_grant[0] . '_none'])) { - if ($enableHTML) { - $priv_string = '' - . $current_grant[1] . ''; - } else { - $priv_string = $current_grant[1]; - } - $privs[] = $priv_string . ' (`' - . join('`, `', $GLOBALS[$current_grant[0]]) . '`)'; - } else { - $allPrivileges = false; - } - } - } - if (empty($privs)) { - if ($enableHTML) { - $privs[] = 'USAGE'; - } else { - $privs[] = 'USAGE'; - } - } elseif ($allPrivileges - && (! isset($GLOBALS['grant_count']) - || count($privs) == $GLOBALS['grant_count']) - ) { - if ($enableHTML) { - $privs = array('ALL PRIVILEGES' - ); - } else { - $privs = array('ALL PRIVILEGES'); - } - } - return $privs; -} // end of the 'PMA_extractPrivInfo()' function - -/** - * Displays on which column(s) a table-specific privilege is granted - * - * @param array $columns columns array - * @param array $row first row from result or boolean false - * @param string $name_for_select privilege types - Select_priv, Insert_priv - * Update_priv, References_priv - * @param string $priv_for_header privilege for header - * @param string $name privilege name - insert, select, update, references - * @param string $name_for_dfn name for dfn - * @param string $name_for_current name for current - * - * @return $html_output html snippet - */ -function PMA_getHtmlToDisplayColumnPrivileges($columns, $row, $name_for_select, - $priv_for_header, $name, $name_for_dfn, $name_for_current -) { - $html_output = '
' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . __('Or') . '' . "\n" - . '' . "\n" - . '
' . "\n"; - return $html_output; -} // end function - -/** - * Get sql query for display privileges table - * - * @param string $db the database - * @param string $table the table - * - * @return string sql query - */ -function PMA_getSqlQueryForDisplayPrivTable($db, $table) -{ - $username = $GLOBALS['username']; - $hostname = $GLOBALS['hostname']; - if ($db == '*') { - return "SELECT * FROM `mysql`.`user`" - ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" - ." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "';"; - } elseif ($table == '*') { - return "SELECT * FROM `mysql`.`db`" - ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" - ." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "'" - ." AND '" . PMA_unescapeMysqlWildcards($db) . "'" - ." LIKE `Db`;"; - } - return "SELECT `Table_priv`" - ." FROM `mysql`.`tables_priv`" - ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" - ." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "'" - ." AND `Db` = '" . PMA_unescapeMysqlWildcards($db) . "'" - ." AND `Table_name` = '" . PMA_sqlAddSlashes($table) . "';"; -} -/** - * Displays the privileges form table - * - * @param string $db the database - * @param string $table the table - * @param boolean $submit wheather to display the submit button or not - * - * @global array $cfg the phpMyAdmin configuration - * @global ressource $user_link the database connection - * - * @return string html snippet - */ -function PMA_getHtmlToDisplayPrivilegesTable($db = '*', $table = '*', $submit = true) -{ - global $random_n; - $html_output = ''; - - if ($db == '*') { - $table = '*'; - } - - if (isset($GLOBALS['username'])) { - $sql_query = PMA_getSqlQueryForDisplayPrivTable($db, $table); - $row = PMA_DBI_fetch_single_row($sql_query); - } - if (empty($row)) { - if ($table == '*') { - if ($db == '*') { - $sql_query = 'SHOW COLUMNS FROM `mysql`.`user`;'; - } elseif ($table == '*') { - $sql_query = 'SHOW COLUMNS FROM `mysql`.`db`;'; - } - $res = PMA_DBI_query($sql_query); - while ($row1 = PMA_DBI_fetch_row($res)) { - if (substr($row1[0], 0, 4) == 'max_') { - $row[$row1[0]] = 0; - } else { - $row[$row1[0]] = 'N'; - } - } - PMA_DBI_free_result($res); - } else { - $row = array('Table_priv' => ''); - } - } - if (isset($row['Table_priv'])) { - $row1 = PMA_DBI_fetch_single_row( - 'SHOW COLUMNS FROM `mysql`.`tables_priv` LIKE \'Table_priv\';', - 'ASSOC', $GLOBALS['userlink'] - ); - // note: in MySQL 5.0.3 we get "Create View', 'Show view'; - // the View for Create is spelled with uppercase V - // the view for Show is spelled with lowercase v - // and there is a space between the words - - $av_grants = explode( - '\',\'', - substr( - $row1['Type'], - strpos($row1['Type'], '(') + 2, - strpos($row1['Type'], ')') - strpos($row1['Type'], '(') - 3 - ) - ); - unset($row1); - $users_grants = explode(',', $row['Table_priv']); - - foreach ($av_grants as $current_grant) { - $row[$current_grant . '_priv'] - = in_array($current_grant, $users_grants) ? 'Y' : 'N'; - } - unset($row['Table_priv'], $current_grant, $av_grants, $users_grants); - - // get collumns - $res = PMA_DBI_try_query( - 'SHOW COLUMNS FROM ' - . PMA_backquote(PMA_unescapeMysqlWildcards($db)) - . '.' . PMA_backquote($table) . ';' - ); - $columns = array(); - if ($res) { - while ($row1 = PMA_DBI_fetch_row($res)) { - $columns[$row1[0]] = array( - 'Select' => false, - 'Insert' => false, - 'Update' => false, - 'References' => false - ); - } - PMA_DBI_free_result($res); - } - unset($res, $row1); - } - // t a b l e - s p e c i f i c p r i v i l e g e s - if (! empty($columns)) { - $res = PMA_DBI_query( - 'SELECT `Column_name`, `Column_priv`' - .' FROM `mysql`.`columns_priv`' - .' WHERE `User`' - .' = \'' . PMA_sqlAddSlashes($username) . "'" - .' AND `Host`' - .' = \'' . PMA_sqlAddSlashes($hostname) . "'" - .' AND `Db`' - .' = \'' . PMA_sqlAddSlashes(PMA_unescapeMysqlWildcards($db)) . "'" - .' AND `Table_name`' - .' = \'' . PMA_sqlAddSlashes($table) . '\';' - ); - - while ($row1 = PMA_DBI_fetch_row($res)) { - $row1[1] = explode(',', $row1[1]); - foreach ($row1[1] as $current) { - $columns[$row1[0]][$current] = true; - } - } - PMA_DBI_free_result($res); - unset($res, $row1, $current); - - $html_output .= '' . "\n" - . '' . "\n" - . '
' . "\n" - . ' ' . __('Table-specific privileges') - . PMA_showHint(__('Note: MySQL privilege names are expressed in English')) - . '' . "\n"; - - // privs that are attached to a specific column - $html_output .= PMA_getHtmlForDisplayColumnPrivileges( - $columns, $row, 'Select_priv', 'SELECT', - 'select', __('Allows reading data.'), 'Select' - ); - - $html_output .= PMA_getHtmlForDisplayColumnPrivileges( - $columns, $row, 'Insert_priv', 'INSERT', - 'insert', __('Allows inserting and replacing data.'), 'Insert' - ); - - $html_output .= PMA_getHtmlForDisplayColumnPrivileges( - $columns, $row, 'Update_priv', 'UPDATE', - 'update', __('Allows changing data.'), 'Update' - ); - - $html_output .= PMA_getHtmlForDisplayColumnPrivileges( - $columns, $row, 'References_priv', 'REFERENCES', 'references', - __('Has no effect in this MySQL version.'), 'References' - ); - - // privs that are not attached to a specific column - - $html_output .= '
' . "\n"; - foreach ($row as $current_grant => $current_grant_value) { - $grant_type = substr($current_grant, 0, (strlen($current_grant) - 5)); - if (in_array($grant_type, array('Select', 'Insert', 'Update', 'References'))) { - continue; - } - // make a substitution to match the messages variables; - // also we must substitute the grant we get, because we can't generate - // a form variable containing blanks (those would get changed to - // an underscore when receiving the POST) - if ($current_grant == 'Create View_priv') { - $tmp_current_grant = 'CreateView_priv'; - $current_grant = 'Create_view_priv'; - } elseif ($current_grant == 'Show view_priv') { - $tmp_current_grant = 'ShowView_priv'; - $current_grant = 'Show_view_priv'; - } else { - $tmp_current_grant = $current_grant; - } - - $html_output .= '
' . "\n" - . '' . "\n"; - - $html_output .= '' . "\n" - . '
' . "\n"; - } // end foreach () - - $html_output .= '
' . "\n"; - // for Safari 2.0.2 - $html_output .= '
' . "\n"; - - } else { - - // g l o b a l o r d b - s p e c i f i c - // - $privTable_names = array(0 => __('Data'), 1 => __('Structure'), 2 => __('Administration')); - - // d a t a - $privTable[0] = array( - array('Select', 'SELECT', __('Allows reading data.')), - array('Insert', 'INSERT', __('Allows inserting and replacing data.')), - array('Update', 'UPDATE', __('Allows changing data.')), - array('Delete', 'DELETE', __('Allows deleting data.')) - ); - if ($db == '*') { - $privTable[0][] = array('File', 'FILE', __('Allows importing data from and exporting data into files.')); - } - - // s t r u c t u r e - $privTable[1] = array( - array('Create', 'CREATE', ($table == '*' ? __('Allows creating new databases and tables.') : __('Allows creating new tables.'))), - array('Alter', 'ALTER', __('Allows altering the structure of existing tables.')), - array('Index', 'INDEX', __('Allows creating and dropping indexes.')), - array('Drop', 'DROP', ($table == '*' ? __('Allows dropping databases and tables.') : __('Allows dropping tables.'))), - array('Create_tmp_table', 'CREATE TEMPORARY TABLES', __('Allows creating temporary tables.')), - array('Show_view', 'SHOW VIEW', __('Allows performing SHOW CREATE VIEW queries.')), - array('Create_routine', 'CREATE ROUTINE', __('Allows creating stored routines.')), - array('Alter_routine', 'ALTER ROUTINE', __('Allows altering and dropping stored routines.')), - array('Execute', 'EXECUTE', __('Allows executing stored routines.')), - ); - // this one is for a db-specific priv: Create_view_priv - if (isset($row['Create_view_priv'])) { - $privTable[1][] = array('Create_view', 'CREATE VIEW', __('Allows creating new views.')); - } - // this one is for a table-specific priv: Create View_priv - if (isset($row['Create View_priv'])) { - $privTable[1][] = array('Create View', 'CREATE VIEW', __('Allows creating new views.')); - } - if (isset($row['Event_priv'])) { - // MySQL 5.1.6 - $privTable[1][] = array('Event', 'EVENT', __('Allows to set up events for the event scheduler')); - $privTable[1][] = array('Trigger', 'TRIGGER', __('Allows creating and dropping triggers')); - } - - // a d m i n i s t r a t i o n - $privTable[2] = array( - array('Grant', 'GRANT', __('Allows adding users and privileges without reloading the privilege tables.')), - ); - if ($db == '*') { - $privTable[2][] = array('Super', 'SUPER', __('Allows connecting, even if maximum number of connections is reached; required for most administrative operations like setting global variables or killing threads of other users.')); - $privTable[2][] = array('Process', 'PROCESS', __('Allows viewing processes of all users')); - $privTable[2][] = array('Reload', 'RELOAD', __('Allows reloading server settings and flushing the server\'s caches.')); - $privTable[2][] = array('Shutdown', 'SHUTDOWN', __('Allows shutting down the server.')); - $privTable[2][] = array('Show_db', 'SHOW DATABASES', __('Gives access to the complete list of databases.')); - } - $privTable[2][] = array('Lock_tables', 'LOCK TABLES', __('Allows locking tables for the current thread.')); - $privTable[2][] = array('References', 'REFERENCES', __('Has no effect in this MySQL version.')); - if ($db == '*') { - $privTable[2][] = array('Repl_client', 'REPLICATION CLIENT', __('Allows the user to ask where the slaves / masters are.')); - $privTable[2][] = array('Repl_slave', 'REPLICATION SLAVE', __('Needed for the replication slaves.')); - $privTable[2][] = array('Create_user', 'CREATE USER', __('Allows creating, dropping and renaming user accounts.')); - } - $html_output .= '' . "\n" - . '
' . "\n" - . '' . "\n" - . ' ' - . ($db == '*' - ? __('Global privileges') - : ($table == '*' - ? __('Database-specific privileges') - : __('Table-specific privileges'))) . "\n" - . '(' - . __('Check All') . ' /' . "\n" - . '' - . __('Uncheck All') . ')' . "\n" - . '' . "\n" - . '

' . __('Note: MySQL privilege names are expressed in English') . '

' . "\n"; - - // Output the Global privilege tables with checkboxes - foreach ($privTable as $i => $table) { - $html_output .= '
' . "\n" - . '' . __($privTable_names[$i]) . '' . "\n"; - foreach ($table as $priv) { - $html_output .= '
' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n"; - } - $html_output .= '
' . "\n"; - } - - // The "Resource limits" box is not displayed for db-specific privs - if ($db == '*') { - $html_output .= PMA_getHtmlForDisplayResourceLimits($row); - } - // for Safari 2.0.2 - $html_output .= '
' . "\n"; - } - $html_output .= '
' . "\n"; - if ($submit) { - $html_output .= '' . "\n"; - } - return $html_output; -} // end of the 'PMA_displayPrivTable()' function - -/** - * Get HTML for "Resource limits" - * - * @param array $row first row from result or boolean false - * - * @return string html snippet - */ -function PMA_getHtmlForDisplayResourceLimits($row) -{ - return '
' . "\n" - . '' . __('Resource limits') . '' . "\n" - . '

' . __('Note: Setting these options to 0 (zero) removes the limit.') . '

' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n" - . '
' . "\n"; -} - -/** - * Displays the fields used by the "new user" form as well as the - * "change login information / copy user" form. - * - * @param string $mode are we creating a new user or are we just - * changing one? (allowed values: 'new', 'change') - * - * @global array $cfg the phpMyAdmin configuration - * @global ressource $user_link the database connection - * - * @return void - */ -function PMA_displayLoginInformationFields($mode = 'new') -{ - // Get user/host name lengths - $fields_info = PMA_DBI_get_columns('mysql', 'user', null, true); - $username_length = 16; - $hostname_length = 41; - foreach ($fields_info as $val) { - if ($val['Field'] == 'User') { - strtok($val['Type'], '()'); - $v = strtok('()'); - if (is_int($v)) { - $username_length = $v; - } - } elseif ($val['Field'] == 'Host') { - strtok($val['Type'], '()'); - $v = strtok('()'); - if (is_int($v)) { - $hostname_length = $v; - } - } - } - unset($fields_info); - - if (isset($GLOBALS['username']) && strlen($GLOBALS['username']) === 0) { - $GLOBALS['pred_username'] = 'any'; - } - echo '
' . "\n" - . '' . __('Login Information') . '' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . ' ' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . ' ' . "\n" - . '' . "\n" - . '' . "\n" - . PMA_showHint(__('When Host table is used, this field is ignored and values stored in Host table are used instead.')) - . '
' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . ' ' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n" - . '
' . "\n" - . '' . "\n" - . ' ' . "\n" - . '' . "\n" - . '
' . "\n" - // Generate password added here via jQuery - . '
' . "\n"; -} // end of the 'PMA_displayUserAndHostFields()' function - - -/** - * Returns all the grants for a certain user on a certain host - * Used in the export privileges for all users section - * - * @param string $user User name - * @param string $host Host name - * - * @return string containing all the grants text - */ -function PMA_getGrants($user, $host) -{ - $grants = PMA_DBI_fetch_result("SHOW GRANTS FOR '" . PMA_sqlAddSlashes($user) . "'@'" . PMA_sqlAddSlashes($host) . "'"); - $response = ''; - foreach ($grants as $one_grant) { - $response .= $one_grant . ";\n\n"; - } - return $response; -} // end of the 'PMA_getGrants()' function - /** * Changes / copies a user, part I */ @@ -2058,7 +1201,7 @@ if (empty($_REQUEST['adduser']) && (! isset($checkprivs) || ! strlen($checkprivs unset($sql); if ($user_does_not_exists) { PMA_Message::error(__('The selected user was not found in the privilege table.'))->display(); - PMA_displayLoginInformationFields(); + echo PMA_getHtmlForDisplayLoginInformationFields(); //exit; } @@ -2383,8 +1526,8 @@ if (empty($_REQUEST['adduser']) && (! isset($checkprivs) || ! strlen($checkprivs . '' . "\n" . '' . "\n" . '
' . "\n" - . ' ' . __('Change Login Information / Copy User') . '' . "\n"; - PMA_displayLoginInformationFields('change'); + . ' ' . __('Change Login Information / Copy User') . '' . "\n" + . PMA_getHtmlForDisplayLoginInformationFields('change'); echo '
' . "\n" . ' ' . __('Create a new user with the same privileges and ...') . '' . "\n"; $choices = array( @@ -2411,8 +1554,8 @@ if (empty($_REQUEST['adduser']) && (! isset($checkprivs) || ! strlen($checkprivs . PMA_getIcon('b_usradd.png') . __('Add user') . "\n" . '' . "\n" . '
' . "\n" - . PMA_generate_common_hidden_inputs('', ''); - PMA_displayLoginInformationFields('new'); + . PMA_generate_common_hidden_inputs('', '') + . PMA_getHtmlForDisplayLoginInformationFields('new'); echo '
' . "\n" . '' . __('Database for user') . '' . "\n"; From 8fb4c87621fcab9a7b1d082abf1344edede7478f Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Tue, 26 Jun 2012 23:43:29 +0530 Subject: [PATCH 005/102] fixed bug in Add user panel in privileges --- libraries/server_privileges.lib.php | 16 +- server_privileges.php | 870 +++++++++++++++++++++++++++- 2 files changed, 875 insertions(+), 11 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index e799940d51..d9c8ec595f 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -9,7 +9,6 @@ if (! defined('PHPMYADMIN')) { exit; } - /** * Escapes wildcard in a database+table specification * before using it in a GRANT statement. @@ -361,9 +360,8 @@ function PMA_getSqlQueryForDisplayPrivTable($db, $table) * * @return string html snippet */ -function PMA_getHtmlToDisplayPrivilegesTable($db = '*', $table = '*', $submit = true) +function PMA_getHtmlToDisplayPrivilegesTable($random_n, $db = '*', $table = '*', $submit = true) { - global $random_n; $html_output = ''; if ($db == '*') { @@ -770,7 +768,7 @@ function PMA_getHtmlForDisplayLoginInformationFields($mode = 'new') unset($thishost); } } - $html_output = ' onchange="if (this.value == \'any\') { hostname.value = \'%\'; } else if (this.value == \'localhost\') { hostname.value = \'localhost\'; } ' + $html_output .= ' onchange="if (this.value == \'any\') { hostname.value = \'%\'; } else if (this.value == \'localhost\') { hostname.value = \'localhost\'; } ' . (empty($thishost) ? '' : 'else if (this.value == \'thishost\') { hostname.value = \'' . addslashes(htmlspecialchars($thishost)) . '\'; } ') . 'else if (this.value == \'hosttable\') { hostname.value = \'\'; } else if (this.value == \'userdefined\') { hostname.focus(); hostname.select(); }">' . "\n"; unset($_current_user); @@ -790,7 +788,7 @@ function PMA_getHtmlForDisplayLoginInformationFields($mode = 'new') break; } } - $html_output = ' ' . "\n" @@ -799,13 +797,13 @@ function PMA_getHtmlForDisplayLoginInformationFields($mode = 'new') ? ' selected="selected"' : '') . '>' . __('Local') . '' . "\n"; if (! empty($thishost)) { - $html_output = ' ' . "\n"; } unset($thishost); - $html_output = ' ' . "\n" @@ -833,9 +831,9 @@ function PMA_getHtmlForDisplayLoginInformationFields($mode = 'new') . ($mode == 'change' ? ' ' . "\n" : '') . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" diff --git a/server_privileges.php b/server_privileges.php index 243e067ed0..096f775205 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -13,7 +13,7 @@ require_once 'libraries/common.inc.php'; /** * functions implementation for this script */ -require_once 'libraries/server_privileges.lib.php'; +//require_once 'libraries/server_privileges.lib.php'; /** * Does the common work @@ -1219,6 +1219,7 @@ if (empty($_REQUEST['adduser']) && (! isset($checkprivs) || ! strlen($checkprivs echo PMA_generate_common_hidden_inputs($_params); echo PMA_getHtmlToDisplayPrivilegesTable( + $random_n, PMA_ifSetOr($dbname, '*', 'length'), PMA_ifSetOr($tablename, '*', 'length') ); @@ -1571,7 +1572,7 @@ if (empty($_REQUEST['adduser']) && (! isset($checkprivs) || ! strlen($checkprivs } echo '
' . "\n"; - echo PMA_getHtmlToDisplayPrivilegesTable('*', '*', false); + echo PMA_getHtmlToDisplayPrivilegesTable($random_n, '*', '*', false); echo ' ' . "\n" @@ -1754,4 +1755,869 @@ if (empty($_REQUEST['adduser']) && (! isset($checkprivs) || ! strlen($checkprivs } // end if (empty($_REQUEST['adduser']) && empty($checkprivs)) ... elseif ... else ... +/** + * Escapes wildcard in a database+table specification + * before using it in a GRANT statement. + * + * Escaping a wildcard character in a GRANT is only accepted at the global + * or database level, not at table level; this is why I remove + * the escaping character. Internally, in mysql.tables_priv.Db there are + * no escaping (for example test_db) but in mysql.db you'll see test\_db + * for a db-specific privilege. + * + * @param string $dbname Database name + * @param string $tablename Table name + * + * @return string the escaped (if necessary) database.table + */ +function PMA_wildcardEscapeForGrant($dbname, $tablename) +{ + + if (! strlen($dbname)) { + $db_and_table = '*.*'; + } else { + if (strlen($tablename)) { + $db_and_table + = PMA_backquote(PMA_unescapeMysqlWildcards($dbname)) . '.' + . PMA_backquote($tablename); + } else { + $db_and_table = PMA_backquote($dbname) . '.*'; + } + } + return $db_and_table; +} + +/** + * Generates a condition on the user name + * + * @param string $initial the user's initial + * + * @return string the generated condition + */ +function PMA_rangeOfUsers($initial = '') +{ + // strtolower() is used because the User field + // might be BINARY, so LIKE would be case sensitive + if (! empty($initial)) { + $ret = " WHERE `User` LIKE '" + . PMA_sqlAddSlashes($initial, true) . "%'" + . " OR `User` LIKE '" + . PMA_sqlAddSlashes(strtolower($initial), true) . "%'"; + } else { + $ret = ''; + } + return $ret; +} // end function + +/** + * Extracts the privilege information of a priv table row + * + * @param array $row the row + * @param boolean $enableHTML add tag with tooltips + * + * @global resource $user_link the database connection + * + * @return array + */ +function PMA_extractPrivInfo($row = '', $enableHTML = false) +{ + $grants = array( + array( + 'Select_priv', + 'SELECT', + __('Allows reading data.')), + array( + 'Insert_priv', + 'INSERT', + __('Allows inserting and replacing data.')), + array( + 'Update_priv', + 'UPDATE', + __('Allows changing data.')), + array( + 'Delete_priv', + 'DELETE', + __('Allows deleting data.')), + array( + 'Create_priv', + 'CREATE', + __('Allows creating new databases and tables.')), + array( + 'Drop_priv', + 'DROP', + __('Allows dropping databases and tables.')), + array( + 'Reload_priv', + 'RELOAD', + __('Allows reloading server settings and flushing the server\'s caches.')), + array( + 'Shutdown_priv', + 'SHUTDOWN', + __('Allows shutting down the server.')), + array( + 'Process_priv', + 'PROCESS', + __('Allows viewing processes of all users')), + array( + 'File_priv', + 'FILE', + __('Allows importing data from and exporting data into files.')), + array( + 'References_priv', + 'REFERENCES', + __('Has no effect in this MySQL version.')), + array( + 'Index_priv', + 'INDEX', + __('Allows creating and dropping indexes.')), + array( + 'Alter_priv', + 'ALTER', + __('Allows altering the structure of existing tables.')), + array( + 'Show_db_priv', + 'SHOW DATABASES', + __('Gives access to the complete list of databases.')), + array( + 'Super_priv', + 'SUPER', + __('Allows connecting, even if maximum number of connections is reached; required for most administrative operations like setting global variables or killing threads of other users.')), + array( + 'Create_tmp_table_priv', + 'CREATE TEMPORARY TABLES', + __('Allows creating temporary tables.')), + array( + 'Lock_tables_priv', + 'LOCK TABLES', + __('Allows locking tables for the current thread.')), + array( + 'Repl_slave_priv', + 'REPLICATION SLAVE', + __('Needed for the replication slaves.')), + array( + 'Repl_client_priv', + 'REPLICATION CLIENT', + __('Allows the user to ask where the slaves / masters are.')), + array( + 'Create_view_priv', + 'CREATE VIEW', + __('Allows creating new views.')), + array( + 'Event_priv', + 'EVENT', + __('Allows to set up events for the event scheduler')), + array( + 'Trigger_priv', + 'TRIGGER', + __('Allows creating and dropping triggers')), + // for table privs: + array( + 'Create View_priv', + 'CREATE VIEW', + __('Allows creating new views.')), + array( + 'Show_view_priv', + 'SHOW VIEW', + __('Allows performing SHOW CREATE VIEW queries.')), + // for table privs: + array( + 'Show view_priv', + 'SHOW VIEW', + __('Allows performing SHOW CREATE VIEW queries.')), + array( + 'Create_routine_priv', + 'CREATE ROUTINE', + __('Allows creating stored routines.')), + array( + 'Alter_routine_priv', + 'ALTER ROUTINE', + __('Allows altering and dropping stored routines.')), + array( + 'Create_user_priv', + 'CREATE USER', + __('Allows creating, dropping and renaming user accounts.')), + array( + 'Execute_priv', + 'EXECUTE', + __('Allows executing stored routines.')), + ); + + if (! empty($row) && isset($row['Table_priv'])) { + $row1 = PMA_DBI_fetch_single_row( + 'SHOW COLUMNS FROM `mysql`.`tables_priv` LIKE \'Table_priv\';', + 'ASSOC', $GLOBALS['userlink'] + ); + $av_grants = explode( + '\',\'', + substr($row1['Type'], 5, strlen($row1['Type']) - 7) + ); + unset($row1); + $users_grants = explode(',', $row['Table_priv']); + foreach ($av_grants as $current_grant) { + $row[$current_grant . '_priv'] + = in_array($current_grant, $users_grants) ? 'Y' : 'N'; + } + unset($current_grant); + unset($av_grants); + unset($users_grants); + } + $privs = array(); + $allPrivileges = true; + foreach ($grants as $current_grant) { + if ((! empty($row) && isset($row[$current_grant[0]])) + || (empty($row) && isset($GLOBALS[$current_grant[0]])) + ) { + if ((! empty($row) && $row[$current_grant[0]] == 'Y') + || (empty($row) + && ($GLOBALS[$current_grant[0]] == 'Y' + || (is_array($GLOBALS[$current_grant[0]]) + && count($GLOBALS[$current_grant[0]]) == $GLOBALS['column_count'] + && empty($GLOBALS[$current_grant[0] . '_none'])))) + ) { + if ($enableHTML) { + $privs[] = '' + . $current_grant[1] . ''; + } else { + $privs[] = $current_grant[1]; + } + } elseif (! empty($GLOBALS[$current_grant[0]]) + && is_array($GLOBALS[$current_grant[0]]) + && empty($GLOBALS[$current_grant[0] . '_none'])) { + if ($enableHTML) { + $priv_string = '' + . $current_grant[1] . ''; + } else { + $priv_string = $current_grant[1]; + } + $privs[] = $priv_string . ' (`' + . join('`, `', $GLOBALS[$current_grant[0]]) . '`)'; + } else { + $allPrivileges = false; + } + } + } + if (empty($privs)) { + if ($enableHTML) { + $privs[] = 'USAGE'; + } else { + $privs[] = 'USAGE'; + } + } elseif ($allPrivileges + && (! isset($GLOBALS['grant_count']) + || count($privs) == $GLOBALS['grant_count']) + ) { + if ($enableHTML) { + $privs = array('ALL PRIVILEGES' + ); + } else { + $privs = array('ALL PRIVILEGES'); + } + } + return $privs; +} // end of the 'PMA_extractPrivInfo()' function + +/** + * Displays on which column(s) a table-specific privilege is granted + * + * @param array $columns columns array + * @param array $row first row from result or boolean false + * @param string $name_for_select privilege types - Select_priv, Insert_priv + * Update_priv, References_priv + * @param string $priv_for_header privilege for header + * @param string $name privilege name - insert, select, update, references + * @param string $name_for_dfn name for dfn + * @param string $name_for_current name for current + * + * @return $html_output html snippet + */ +function PMA_getHtmlToDisplayColumnPrivileges($columns, $row, $name_for_select, + $priv_for_header, $name, $name_for_dfn, $name_for_current +) { + $html_output = '
' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . __('Or') . '' . "\n" + . '' . "\n" + . '
' . "\n"; + return $html_output; +} // end function + +/** + * Get sql query for display privileges table + * + * @param string $db the database + * @param string $table the table + * + * @return string sql query + */ +function PMA_getSqlQueryForDisplayPrivTable($db, $table) +{ + $username = $GLOBALS['username']; + $hostname = $GLOBALS['hostname']; + if ($db == '*') { + return "SELECT * FROM `mysql`.`user`" + ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" + ." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "';"; + } elseif ($table == '*') { + return "SELECT * FROM `mysql`.`db`" + ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" + ." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "'" + ." AND '" . PMA_unescapeMysqlWildcards($db) . "'" + ." LIKE `Db`;"; + } + return "SELECT `Table_priv`" + ." FROM `mysql`.`tables_priv`" + ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" + ." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "'" + ." AND `Db` = '" . PMA_unescapeMysqlWildcards($db) . "'" + ." AND `Table_name` = '" . PMA_sqlAddSlashes($table) . "';"; +} +/** + * Displays the privileges form table + * + * @param string $db the database + * @param string $table the table + * @param boolean $submit wheather to display the submit button or not + * + * @global array $cfg the phpMyAdmin configuration + * @global ressource $user_link the database connection + * + * @return string html snippet + */ +function PMA_getHtmlToDisplayPrivilegesTable($random_n, $db = '*', $table = '*', $submit = true) +{ + $html_output = ''; + + if ($db == '*') { + $table = '*'; + } + + if (isset($GLOBALS['username'])) { + $sql_query = PMA_getSqlQueryForDisplayPrivTable($db, $table); + $row = PMA_DBI_fetch_single_row($sql_query); + } + if (empty($row)) { + if ($table == '*') { + if ($db == '*') { + $sql_query = 'SHOW COLUMNS FROM `mysql`.`user`;'; + } elseif ($table == '*') { + $sql_query = 'SHOW COLUMNS FROM `mysql`.`db`;'; + } + $res = PMA_DBI_query($sql_query); + while ($row1 = PMA_DBI_fetch_row($res)) { + if (substr($row1[0], 0, 4) == 'max_') { + $row[$row1[0]] = 0; + } else { + $row[$row1[0]] = 'N'; + } + } + PMA_DBI_free_result($res); + } else { + $row = array('Table_priv' => ''); + } + } + if (isset($row['Table_priv'])) { + $row1 = PMA_DBI_fetch_single_row( + 'SHOW COLUMNS FROM `mysql`.`tables_priv` LIKE \'Table_priv\';', + 'ASSOC', $GLOBALS['userlink'] + ); + // note: in MySQL 5.0.3 we get "Create View', 'Show view'; + // the View for Create is spelled with uppercase V + // the view for Show is spelled with lowercase v + // and there is a space between the words + + $av_grants = explode( + '\',\'', + substr( + $row1['Type'], + strpos($row1['Type'], '(') + 2, + strpos($row1['Type'], ')') - strpos($row1['Type'], '(') - 3 + ) + ); + unset($row1); + $users_grants = explode(',', $row['Table_priv']); + + foreach ($av_grants as $current_grant) { + $row[$current_grant . '_priv'] + = in_array($current_grant, $users_grants) ? 'Y' : 'N'; + } + unset($row['Table_priv'], $current_grant, $av_grants, $users_grants); + + // get collumns + $res = PMA_DBI_try_query( + 'SHOW COLUMNS FROM ' + . PMA_backquote(PMA_unescapeMysqlWildcards($db)) + . '.' . PMA_backquote($table) . ';' + ); + $columns = array(); + if ($res) { + while ($row1 = PMA_DBI_fetch_row($res)) { + $columns[$row1[0]] = array( + 'Select' => false, + 'Insert' => false, + 'Update' => false, + 'References' => false + ); + } + PMA_DBI_free_result($res); + } + unset($res, $row1); + } + // t a b l e - s p e c i f i c p r i v i l e g e s + if (! empty($columns)) { + $res = PMA_DBI_query( + 'SELECT `Column_name`, `Column_priv`' + .' FROM `mysql`.`columns_priv`' + .' WHERE `User`' + .' = \'' . PMA_sqlAddSlashes($username) . "'" + .' AND `Host`' + .' = \'' . PMA_sqlAddSlashes($hostname) . "'" + .' AND `Db`' + .' = \'' . PMA_sqlAddSlashes(PMA_unescapeMysqlWildcards($db)) . "'" + .' AND `Table_name`' + .' = \'' . PMA_sqlAddSlashes($table) . '\';' + ); + + while ($row1 = PMA_DBI_fetch_row($res)) { + $row1[1] = explode(',', $row1[1]); + foreach ($row1[1] as $current) { + $columns[$row1[0]][$current] = true; + } + } + PMA_DBI_free_result($res); + unset($res, $row1, $current); + + $html_output .= '' . "\n" + . '' . "\n" + . '
' . "\n" + . ' ' . __('Table-specific privileges') + . PMA_showHint(__('Note: MySQL privilege names are expressed in English')) + . '' . "\n"; + + // privs that are attached to a specific column + $html_output .= PMA_getHtmlForDisplayColumnPrivileges( + $columns, $row, 'Select_priv', 'SELECT', + 'select', __('Allows reading data.'), 'Select' + ); + + $html_output .= PMA_getHtmlForDisplayColumnPrivileges( + $columns, $row, 'Insert_priv', 'INSERT', + 'insert', __('Allows inserting and replacing data.'), 'Insert' + ); + + $html_output .= PMA_getHtmlForDisplayColumnPrivileges( + $columns, $row, 'Update_priv', 'UPDATE', + 'update', __('Allows changing data.'), 'Update' + ); + + $html_output .= PMA_getHtmlForDisplayColumnPrivileges( + $columns, $row, 'References_priv', 'REFERENCES', 'references', + __('Has no effect in this MySQL version.'), 'References' + ); + + // privs that are not attached to a specific column + + $html_output .= '
' . "\n"; + foreach ($row as $current_grant => $current_grant_value) { + $grant_type = substr($current_grant, 0, (strlen($current_grant) - 5)); + if (in_array($grant_type, array('Select', 'Insert', 'Update', 'References'))) { + continue; + } + // make a substitution to match the messages variables; + // also we must substitute the grant we get, because we can't generate + // a form variable containing blanks (those would get changed to + // an underscore when receiving the POST) + if ($current_grant == 'Create View_priv') { + $tmp_current_grant = 'CreateView_priv'; + $current_grant = 'Create_view_priv'; + } elseif ($current_grant == 'Show view_priv') { + $tmp_current_grant = 'ShowView_priv'; + $current_grant = 'Show_view_priv'; + } else { + $tmp_current_grant = $current_grant; + } + + $html_output .= '
' . "\n" + . '' . "\n"; + + $html_output .= '' . "\n" + . '
' . "\n"; + } // end foreach () + + $html_output .= '
' . "\n"; + // for Safari 2.0.2 + $html_output .= '
' . "\n"; + + } else { + + // g l o b a l o r d b - s p e c i f i c + // + $privTable_names = array(0 => __('Data'), 1 => __('Structure'), 2 => __('Administration')); + + // d a t a + $privTable[0] = array( + array('Select', 'SELECT', __('Allows reading data.')), + array('Insert', 'INSERT', __('Allows inserting and replacing data.')), + array('Update', 'UPDATE', __('Allows changing data.')), + array('Delete', 'DELETE', __('Allows deleting data.')) + ); + if ($db == '*') { + $privTable[0][] = array('File', 'FILE', __('Allows importing data from and exporting data into files.')); + } + + // s t r u c t u r e + $privTable[1] = array( + array('Create', 'CREATE', ($table == '*' ? __('Allows creating new databases and tables.') : __('Allows creating new tables.'))), + array('Alter', 'ALTER', __('Allows altering the structure of existing tables.')), + array('Index', 'INDEX', __('Allows creating and dropping indexes.')), + array('Drop', 'DROP', ($table == '*' ? __('Allows dropping databases and tables.') : __('Allows dropping tables.'))), + array('Create_tmp_table', 'CREATE TEMPORARY TABLES', __('Allows creating temporary tables.')), + array('Show_view', 'SHOW VIEW', __('Allows performing SHOW CREATE VIEW queries.')), + array('Create_routine', 'CREATE ROUTINE', __('Allows creating stored routines.')), + array('Alter_routine', 'ALTER ROUTINE', __('Allows altering and dropping stored routines.')), + array('Execute', 'EXECUTE', __('Allows executing stored routines.')), + ); + // this one is for a db-specific priv: Create_view_priv + if (isset($row['Create_view_priv'])) { + $privTable[1][] = array('Create_view', 'CREATE VIEW', __('Allows creating new views.')); + } + // this one is for a table-specific priv: Create View_priv + if (isset($row['Create View_priv'])) { + $privTable[1][] = array('Create View', 'CREATE VIEW', __('Allows creating new views.')); + } + if (isset($row['Event_priv'])) { + // MySQL 5.1.6 + $privTable[1][] = array('Event', 'EVENT', __('Allows to set up events for the event scheduler')); + $privTable[1][] = array('Trigger', 'TRIGGER', __('Allows creating and dropping triggers')); + } + + // a d m i n i s t r a t i o n + $privTable[2] = array( + array('Grant', 'GRANT', __('Allows adding users and privileges without reloading the privilege tables.')), + ); + if ($db == '*') { + $privTable[2][] = array('Super', 'SUPER', __('Allows connecting, even if maximum number of connections is reached; required for most administrative operations like setting global variables or killing threads of other users.')); + $privTable[2][] = array('Process', 'PROCESS', __('Allows viewing processes of all users')); + $privTable[2][] = array('Reload', 'RELOAD', __('Allows reloading server settings and flushing the server\'s caches.')); + $privTable[2][] = array('Shutdown', 'SHUTDOWN', __('Allows shutting down the server.')); + $privTable[2][] = array('Show_db', 'SHOW DATABASES', __('Gives access to the complete list of databases.')); + } + $privTable[2][] = array('Lock_tables', 'LOCK TABLES', __('Allows locking tables for the current thread.')); + $privTable[2][] = array('References', 'REFERENCES', __('Has no effect in this MySQL version.')); + if ($db == '*') { + $privTable[2][] = array('Repl_client', 'REPLICATION CLIENT', __('Allows the user to ask where the slaves / masters are.')); + $privTable[2][] = array('Repl_slave', 'REPLICATION SLAVE', __('Needed for the replication slaves.')); + $privTable[2][] = array('Create_user', 'CREATE USER', __('Allows creating, dropping and renaming user accounts.')); + } + $html_output .= '' . "\n" + . '
' . "\n" + . '' . "\n" + . ' ' + . ($db == '*' + ? __('Global privileges') + : ($table == '*' + ? __('Database-specific privileges') + : __('Table-specific privileges'))) . "\n" + . '(' + . __('Check All') . ' /' . "\n" + . '' + . __('Uncheck All') . ')' . "\n" + . '' . "\n" + . '

' . __('Note: MySQL privilege names are expressed in English') . '

' . "\n"; + + // Output the Global privilege tables with checkboxes + foreach ($privTable as $i => $table) { + $html_output .= '
' . "\n" + . '' . __($privTable_names[$i]) . '' . "\n"; + foreach ($table as $priv) { + $html_output .= '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n"; + } + $html_output .= '
' . "\n"; + } + + // The "Resource limits" box is not displayed for db-specific privs + if ($db == '*') { + $html_output .= PMA_getHtmlForDisplayResourceLimits($row); + } + // for Safari 2.0.2 + $html_output .= '
' . "\n"; + } + $html_output .= '
' . "\n"; + if ($submit) { + $html_output .= '' . "\n"; + } + return $html_output; +} // end of the 'PMA_displayPrivTable()' function + +/** + * Get HTML for "Resource limits" + * + * @param array $row first row from result or boolean false + * + * @return string html snippet + */ +function PMA_getHtmlForDisplayResourceLimits($row) +{ + return '
' . "\n" + . '' . __('Resource limits') . '' . "\n" + . '

' . __('Note: Setting these options to 0 (zero) removes the limit.') . '

' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n" + . '
' . "\n"; +} + +/** + * Displays the fields used by the "new user" form as well as the + * "change login information / copy user" form. + * + * @param string $mode are we creating a new user or are we just + * changing one? (allowed values: 'new', 'change') + * + * @global array $cfg the phpMyAdmin configuration + * @global ressource $user_link the database connection + * + * @return void + */ +function PMA_getHtmlForDisplayLoginInformationFields($mode = 'new') +{ + // Get user/host name lengths + $fields_info = PMA_DBI_get_columns('mysql', 'user', null, true); + $username_length = 16; + $hostname_length = 41; + foreach ($fields_info as $val) { + if ($val['Field'] == 'User') { + strtok($val['Type'], '()'); + $v = strtok('()'); + if (is_int($v)) { + $username_length = $v; + } + } elseif ($val['Field'] == 'Host') { + strtok($val['Type'], '()'); + $v = strtok('()'); + if (is_int($v)) { + $hostname_length = $v; + } + } + } + unset($fields_info); + + if (isset($GLOBALS['username']) && strlen($GLOBALS['username']) === 0) { + $GLOBALS['pred_username'] = 'any'; + } + $html_output = '
' . "\n" + . '' . __('Login Information') . '' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . '' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . ' ' . "\n" + . '' . "\n" + . '' . "\n" + . PMA_showHint(__('When Host table is used, this field is ignored and values stored in Host table are used instead.')) + . '
' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n" + . '' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n" + . '
' . "\n" + . '' . "\n" + . ' ' . "\n" + . '' . "\n" + . '
' . "\n" + // Generate password added here via jQuery + . '
' . "\n"; + + return $html_output; +} // end of the 'PMA_displayUserAndHostFields()' function + + +/** + * Returns all the grants for a certain user on a certain host + * Used in the export privileges for all users section + * + * @param string $user User name + * @param string $host Host name + * + * @return string containing all the grants text + */ +function PMA_getGrants($user, $host) +{ + $grants = PMA_DBI_fetch_result("SHOW GRANTS FOR '" . PMA_sqlAddSlashes($user) . "'@'" . PMA_sqlAddSlashes($host) . "'"); + $response = ''; + foreach ($grants as $one_grant) { + $response .= $one_grant . ";\n\n"; + } + return $response; +} // end of the 'PMA_getGrants()' function + + + ?> From 7bb7bafc5f0f16058f28076765105e8946597292 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Wed, 27 Jun 2012 01:29:04 +0530 Subject: [PATCH 006/102] implement PMA_getGrantsArray() in server_privileges-lib --- libraries/server_privileges.lib.php | 254 ++++---- server_privileges.php | 867 +--------------------------- 2 files changed, 134 insertions(+), 987 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index d9c8ec595f..3a58f13642 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -26,7 +26,6 @@ if (! defined('PHPMYADMIN')) { */ function PMA_wildcardEscapeForGrant($dbname, $tablename) { - if (! strlen($dbname)) { $db_and_table = '*.*'; } else { @@ -75,126 +74,7 @@ function PMA_rangeOfUsers($initial = '') */ function PMA_extractPrivInfo($row = '', $enableHTML = false) { - $grants = array( - array( - 'Select_priv', - 'SELECT', - __('Allows reading data.')), - array( - 'Insert_priv', - 'INSERT', - __('Allows inserting and replacing data.')), - array( - 'Update_priv', - 'UPDATE', - __('Allows changing data.')), - array( - 'Delete_priv', - 'DELETE', - __('Allows deleting data.')), - array( - 'Create_priv', - 'CREATE', - __('Allows creating new databases and tables.')), - array( - 'Drop_priv', - 'DROP', - __('Allows dropping databases and tables.')), - array( - 'Reload_priv', - 'RELOAD', - __('Allows reloading server settings and flushing the server\'s caches.')), - array( - 'Shutdown_priv', - 'SHUTDOWN', - __('Allows shutting down the server.')), - array( - 'Process_priv', - 'PROCESS', - __('Allows viewing processes of all users')), - array( - 'File_priv', - 'FILE', - __('Allows importing data from and exporting data into files.')), - array( - 'References_priv', - 'REFERENCES', - __('Has no effect in this MySQL version.')), - array( - 'Index_priv', - 'INDEX', - __('Allows creating and dropping indexes.')), - array( - 'Alter_priv', - 'ALTER', - __('Allows altering the structure of existing tables.')), - array( - 'Show_db_priv', - 'SHOW DATABASES', - __('Gives access to the complete list of databases.')), - array( - 'Super_priv', - 'SUPER', - __('Allows connecting, even if maximum number of connections is reached; required for most administrative operations like setting global variables or killing threads of other users.')), - array( - 'Create_tmp_table_priv', - 'CREATE TEMPORARY TABLES', - __('Allows creating temporary tables.')), - array( - 'Lock_tables_priv', - 'LOCK TABLES', - __('Allows locking tables for the current thread.')), - array( - 'Repl_slave_priv', - 'REPLICATION SLAVE', - __('Needed for the replication slaves.')), - array( - 'Repl_client_priv', - 'REPLICATION CLIENT', - __('Allows the user to ask where the slaves / masters are.')), - array( - 'Create_view_priv', - 'CREATE VIEW', - __('Allows creating new views.')), - array( - 'Event_priv', - 'EVENT', - __('Allows to set up events for the event scheduler')), - array( - 'Trigger_priv', - 'TRIGGER', - __('Allows creating and dropping triggers')), - // for table privs: - array( - 'Create View_priv', - 'CREATE VIEW', - __('Allows creating new views.')), - array( - 'Show_view_priv', - 'SHOW VIEW', - __('Allows performing SHOW CREATE VIEW queries.')), - // for table privs: - array( - 'Show view_priv', - 'SHOW VIEW', - __('Allows performing SHOW CREATE VIEW queries.')), - array( - 'Create_routine_priv', - 'CREATE ROUTINE', - __('Allows creating stored routines.')), - array( - 'Alter_routine_priv', - 'ALTER ROUTINE', - __('Allows altering and dropping stored routines.')), - array( - 'Create_user_priv', - 'CREATE USER', - __('Allows creating, dropping and renaming user accounts.')), - array( - 'Execute_priv', - 'EXECUTE', - __('Allows executing stored routines.')), - ); + $grants = PMA_getGrantsArray(); if (! empty($row) && isset($row['Table_priv'])) { $row1 = PMA_DBI_fetch_single_row( @@ -272,6 +152,138 @@ function PMA_extractPrivInfo($row = '', $enableHTML = false) return $privs; } // end of the 'PMA_extractPrivInfo()' function +/** + * Get the grants array which contains all the privilege types + * and relevent grant messages + * + * @return array + */ +function PMA_getGrantsArray() +{ + return array( + array( + 'Select_priv', + 'SELECT', + __('Allows reading data.')), + array( + 'Insert_priv', + 'INSERT', + __('Allows inserting and replacing data.')), + array( + 'Update_priv', + 'UPDATE', + __('Allows changing data.')), + array( + 'Delete_priv', + 'DELETE', + __('Allows deleting data.')), + array( + 'Create_priv', + 'CREATE', + __('Allows creating new databases and tables.')), + array( + 'Drop_priv', + 'DROP', + __('Allows dropping databases and tables.')), + array( + 'Reload_priv', + 'RELOAD', + __('Allows reloading server settings and flushing the server\'s caches.')), + array( + 'Shutdown_priv', + 'SHUTDOWN', + __('Allows shutting down the server.')), + array( + 'Process_priv', + 'PROCESS', + __('Allows viewing processes of all users')), + array( + 'File_priv', + 'FILE', + __('Allows importing data from and exporting data into files.')), + array( + 'References_priv', + 'REFERENCES', + __('Has no effect in this MySQL version.')), + array( + 'Index_priv', + 'INDEX', + __('Allows creating and dropping indexes.')), + array( + 'Alter_priv', + 'ALTER', + __('Allows altering the structure of existing tables.')), + array( + 'Show_db_priv', + 'SHOW DATABASES', + __('Gives access to the complete list of databases.')), + array( + 'Super_priv', + 'SUPER', + __('Allows connecting, even if maximum number of connections is reached; + required for most administrative operations like setting global + variables or killing threads of other users.')), + array( + 'Create_tmp_table_priv', + 'CREATE TEMPORARY TABLES', + __('Allows creating temporary tables.')), + array( + 'Lock_tables_priv', + 'LOCK TABLES', + __('Allows locking tables for the current thread.')), + array( + 'Repl_slave_priv', + 'REPLICATION SLAVE', + __('Needed for the replication slaves.')), + array( + 'Repl_client_priv', + 'REPLICATION CLIENT', + __('Allows the user to ask where the slaves / masters are.')), + array( + 'Create_view_priv', + 'CREATE VIEW', + __('Allows creating new views.')), + array( + 'Event_priv', + 'EVENT', + __('Allows to set up events for the event scheduler')), + array( + 'Trigger_priv', + 'TRIGGER', + __('Allows creating and dropping triggers')), + // for table privs: + array( + 'Create View_priv', + 'CREATE VIEW', + __('Allows creating new views.')), + array( + 'Show_view_priv', + 'SHOW VIEW', + __('Allows performing SHOW CREATE VIEW queries.')), + // for table privs: + array( + 'Show view_priv', + 'SHOW VIEW', + __('Allows performing SHOW CREATE VIEW queries.')), + array( + 'Create_routine_priv', + 'CREATE ROUTINE', + __('Allows creating stored routines.')), + array( + 'Alter_routine_priv', + 'ALTER ROUTINE', + __('Allows altering and dropping stored routines.')), + array( + 'Create_user_priv', + 'CREATE USER', + __('Allows creating, dropping and renaming user accounts.')), + array( + 'Execute_priv', + 'EXECUTE', + __('Allows executing stored routines.')), + ); +} + /** * Displays on which column(s) a table-specific privilege is granted * diff --git a/server_privileges.php b/server_privileges.php index 096f775205..e84de0533b 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -13,7 +13,7 @@ require_once 'libraries/common.inc.php'; /** * functions implementation for this script */ -//require_once 'libraries/server_privileges.lib.php'; +require_once 'libraries/server_privileges.lib.php'; /** * Does the common work @@ -1755,869 +1755,4 @@ if (empty($_REQUEST['adduser']) && (! isset($checkprivs) || ! strlen($checkprivs } // end if (empty($_REQUEST['adduser']) && empty($checkprivs)) ... elseif ... else ... -/** - * Escapes wildcard in a database+table specification - * before using it in a GRANT statement. - * - * Escaping a wildcard character in a GRANT is only accepted at the global - * or database level, not at table level; this is why I remove - * the escaping character. Internally, in mysql.tables_priv.Db there are - * no escaping (for example test_db) but in mysql.db you'll see test\_db - * for a db-specific privilege. - * - * @param string $dbname Database name - * @param string $tablename Table name - * - * @return string the escaped (if necessary) database.table - */ -function PMA_wildcardEscapeForGrant($dbname, $tablename) -{ - - if (! strlen($dbname)) { - $db_and_table = '*.*'; - } else { - if (strlen($tablename)) { - $db_and_table - = PMA_backquote(PMA_unescapeMysqlWildcards($dbname)) . '.' - . PMA_backquote($tablename); - } else { - $db_and_table = PMA_backquote($dbname) . '.*'; - } - } - return $db_and_table; -} - -/** - * Generates a condition on the user name - * - * @param string $initial the user's initial - * - * @return string the generated condition - */ -function PMA_rangeOfUsers($initial = '') -{ - // strtolower() is used because the User field - // might be BINARY, so LIKE would be case sensitive - if (! empty($initial)) { - $ret = " WHERE `User` LIKE '" - . PMA_sqlAddSlashes($initial, true) . "%'" - . " OR `User` LIKE '" - . PMA_sqlAddSlashes(strtolower($initial), true) . "%'"; - } else { - $ret = ''; - } - return $ret; -} // end function - -/** - * Extracts the privilege information of a priv table row - * - * @param array $row the row - * @param boolean $enableHTML add tag with tooltips - * - * @global resource $user_link the database connection - * - * @return array - */ -function PMA_extractPrivInfo($row = '', $enableHTML = false) -{ - $grants = array( - array( - 'Select_priv', - 'SELECT', - __('Allows reading data.')), - array( - 'Insert_priv', - 'INSERT', - __('Allows inserting and replacing data.')), - array( - 'Update_priv', - 'UPDATE', - __('Allows changing data.')), - array( - 'Delete_priv', - 'DELETE', - __('Allows deleting data.')), - array( - 'Create_priv', - 'CREATE', - __('Allows creating new databases and tables.')), - array( - 'Drop_priv', - 'DROP', - __('Allows dropping databases and tables.')), - array( - 'Reload_priv', - 'RELOAD', - __('Allows reloading server settings and flushing the server\'s caches.')), - array( - 'Shutdown_priv', - 'SHUTDOWN', - __('Allows shutting down the server.')), - array( - 'Process_priv', - 'PROCESS', - __('Allows viewing processes of all users')), - array( - 'File_priv', - 'FILE', - __('Allows importing data from and exporting data into files.')), - array( - 'References_priv', - 'REFERENCES', - __('Has no effect in this MySQL version.')), - array( - 'Index_priv', - 'INDEX', - __('Allows creating and dropping indexes.')), - array( - 'Alter_priv', - 'ALTER', - __('Allows altering the structure of existing tables.')), - array( - 'Show_db_priv', - 'SHOW DATABASES', - __('Gives access to the complete list of databases.')), - array( - 'Super_priv', - 'SUPER', - __('Allows connecting, even if maximum number of connections is reached; required for most administrative operations like setting global variables or killing threads of other users.')), - array( - 'Create_tmp_table_priv', - 'CREATE TEMPORARY TABLES', - __('Allows creating temporary tables.')), - array( - 'Lock_tables_priv', - 'LOCK TABLES', - __('Allows locking tables for the current thread.')), - array( - 'Repl_slave_priv', - 'REPLICATION SLAVE', - __('Needed for the replication slaves.')), - array( - 'Repl_client_priv', - 'REPLICATION CLIENT', - __('Allows the user to ask where the slaves / masters are.')), - array( - 'Create_view_priv', - 'CREATE VIEW', - __('Allows creating new views.')), - array( - 'Event_priv', - 'EVENT', - __('Allows to set up events for the event scheduler')), - array( - 'Trigger_priv', - 'TRIGGER', - __('Allows creating and dropping triggers')), - // for table privs: - array( - 'Create View_priv', - 'CREATE VIEW', - __('Allows creating new views.')), - array( - 'Show_view_priv', - 'SHOW VIEW', - __('Allows performing SHOW CREATE VIEW queries.')), - // for table privs: - array( - 'Show view_priv', - 'SHOW VIEW', - __('Allows performing SHOW CREATE VIEW queries.')), - array( - 'Create_routine_priv', - 'CREATE ROUTINE', - __('Allows creating stored routines.')), - array( - 'Alter_routine_priv', - 'ALTER ROUTINE', - __('Allows altering and dropping stored routines.')), - array( - 'Create_user_priv', - 'CREATE USER', - __('Allows creating, dropping and renaming user accounts.')), - array( - 'Execute_priv', - 'EXECUTE', - __('Allows executing stored routines.')), - ); - - if (! empty($row) && isset($row['Table_priv'])) { - $row1 = PMA_DBI_fetch_single_row( - 'SHOW COLUMNS FROM `mysql`.`tables_priv` LIKE \'Table_priv\';', - 'ASSOC', $GLOBALS['userlink'] - ); - $av_grants = explode( - '\',\'', - substr($row1['Type'], 5, strlen($row1['Type']) - 7) - ); - unset($row1); - $users_grants = explode(',', $row['Table_priv']); - foreach ($av_grants as $current_grant) { - $row[$current_grant . '_priv'] - = in_array($current_grant, $users_grants) ? 'Y' : 'N'; - } - unset($current_grant); - unset($av_grants); - unset($users_grants); - } - $privs = array(); - $allPrivileges = true; - foreach ($grants as $current_grant) { - if ((! empty($row) && isset($row[$current_grant[0]])) - || (empty($row) && isset($GLOBALS[$current_grant[0]])) - ) { - if ((! empty($row) && $row[$current_grant[0]] == 'Y') - || (empty($row) - && ($GLOBALS[$current_grant[0]] == 'Y' - || (is_array($GLOBALS[$current_grant[0]]) - && count($GLOBALS[$current_grant[0]]) == $GLOBALS['column_count'] - && empty($GLOBALS[$current_grant[0] . '_none'])))) - ) { - if ($enableHTML) { - $privs[] = '' - . $current_grant[1] . ''; - } else { - $privs[] = $current_grant[1]; - } - } elseif (! empty($GLOBALS[$current_grant[0]]) - && is_array($GLOBALS[$current_grant[0]]) - && empty($GLOBALS[$current_grant[0] . '_none'])) { - if ($enableHTML) { - $priv_string = '' - . $current_grant[1] . ''; - } else { - $priv_string = $current_grant[1]; - } - $privs[] = $priv_string . ' (`' - . join('`, `', $GLOBALS[$current_grant[0]]) . '`)'; - } else { - $allPrivileges = false; - } - } - } - if (empty($privs)) { - if ($enableHTML) { - $privs[] = 'USAGE'; - } else { - $privs[] = 'USAGE'; - } - } elseif ($allPrivileges - && (! isset($GLOBALS['grant_count']) - || count($privs) == $GLOBALS['grant_count']) - ) { - if ($enableHTML) { - $privs = array('ALL PRIVILEGES' - ); - } else { - $privs = array('ALL PRIVILEGES'); - } - } - return $privs; -} // end of the 'PMA_extractPrivInfo()' function - -/** - * Displays on which column(s) a table-specific privilege is granted - * - * @param array $columns columns array - * @param array $row first row from result or boolean false - * @param string $name_for_select privilege types - Select_priv, Insert_priv - * Update_priv, References_priv - * @param string $priv_for_header privilege for header - * @param string $name privilege name - insert, select, update, references - * @param string $name_for_dfn name for dfn - * @param string $name_for_current name for current - * - * @return $html_output html snippet - */ -function PMA_getHtmlToDisplayColumnPrivileges($columns, $row, $name_for_select, - $priv_for_header, $name, $name_for_dfn, $name_for_current -) { - $html_output = '
' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . __('Or') . '' . "\n" - . '' . "\n" - . '
' . "\n"; - return $html_output; -} // end function - -/** - * Get sql query for display privileges table - * - * @param string $db the database - * @param string $table the table - * - * @return string sql query - */ -function PMA_getSqlQueryForDisplayPrivTable($db, $table) -{ - $username = $GLOBALS['username']; - $hostname = $GLOBALS['hostname']; - if ($db == '*') { - return "SELECT * FROM `mysql`.`user`" - ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" - ." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "';"; - } elseif ($table == '*') { - return "SELECT * FROM `mysql`.`db`" - ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" - ." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "'" - ." AND '" . PMA_unescapeMysqlWildcards($db) . "'" - ." LIKE `Db`;"; - } - return "SELECT `Table_priv`" - ." FROM `mysql`.`tables_priv`" - ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" - ." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "'" - ." AND `Db` = '" . PMA_unescapeMysqlWildcards($db) . "'" - ." AND `Table_name` = '" . PMA_sqlAddSlashes($table) . "';"; -} -/** - * Displays the privileges form table - * - * @param string $db the database - * @param string $table the table - * @param boolean $submit wheather to display the submit button or not - * - * @global array $cfg the phpMyAdmin configuration - * @global ressource $user_link the database connection - * - * @return string html snippet - */ -function PMA_getHtmlToDisplayPrivilegesTable($random_n, $db = '*', $table = '*', $submit = true) -{ - $html_output = ''; - - if ($db == '*') { - $table = '*'; - } - - if (isset($GLOBALS['username'])) { - $sql_query = PMA_getSqlQueryForDisplayPrivTable($db, $table); - $row = PMA_DBI_fetch_single_row($sql_query); - } - if (empty($row)) { - if ($table == '*') { - if ($db == '*') { - $sql_query = 'SHOW COLUMNS FROM `mysql`.`user`;'; - } elseif ($table == '*') { - $sql_query = 'SHOW COLUMNS FROM `mysql`.`db`;'; - } - $res = PMA_DBI_query($sql_query); - while ($row1 = PMA_DBI_fetch_row($res)) { - if (substr($row1[0], 0, 4) == 'max_') { - $row[$row1[0]] = 0; - } else { - $row[$row1[0]] = 'N'; - } - } - PMA_DBI_free_result($res); - } else { - $row = array('Table_priv' => ''); - } - } - if (isset($row['Table_priv'])) { - $row1 = PMA_DBI_fetch_single_row( - 'SHOW COLUMNS FROM `mysql`.`tables_priv` LIKE \'Table_priv\';', - 'ASSOC', $GLOBALS['userlink'] - ); - // note: in MySQL 5.0.3 we get "Create View', 'Show view'; - // the View for Create is spelled with uppercase V - // the view for Show is spelled with lowercase v - // and there is a space between the words - - $av_grants = explode( - '\',\'', - substr( - $row1['Type'], - strpos($row1['Type'], '(') + 2, - strpos($row1['Type'], ')') - strpos($row1['Type'], '(') - 3 - ) - ); - unset($row1); - $users_grants = explode(',', $row['Table_priv']); - - foreach ($av_grants as $current_grant) { - $row[$current_grant . '_priv'] - = in_array($current_grant, $users_grants) ? 'Y' : 'N'; - } - unset($row['Table_priv'], $current_grant, $av_grants, $users_grants); - - // get collumns - $res = PMA_DBI_try_query( - 'SHOW COLUMNS FROM ' - . PMA_backquote(PMA_unescapeMysqlWildcards($db)) - . '.' . PMA_backquote($table) . ';' - ); - $columns = array(); - if ($res) { - while ($row1 = PMA_DBI_fetch_row($res)) { - $columns[$row1[0]] = array( - 'Select' => false, - 'Insert' => false, - 'Update' => false, - 'References' => false - ); - } - PMA_DBI_free_result($res); - } - unset($res, $row1); - } - // t a b l e - s p e c i f i c p r i v i l e g e s - if (! empty($columns)) { - $res = PMA_DBI_query( - 'SELECT `Column_name`, `Column_priv`' - .' FROM `mysql`.`columns_priv`' - .' WHERE `User`' - .' = \'' . PMA_sqlAddSlashes($username) . "'" - .' AND `Host`' - .' = \'' . PMA_sqlAddSlashes($hostname) . "'" - .' AND `Db`' - .' = \'' . PMA_sqlAddSlashes(PMA_unescapeMysqlWildcards($db)) . "'" - .' AND `Table_name`' - .' = \'' . PMA_sqlAddSlashes($table) . '\';' - ); - - while ($row1 = PMA_DBI_fetch_row($res)) { - $row1[1] = explode(',', $row1[1]); - foreach ($row1[1] as $current) { - $columns[$row1[0]][$current] = true; - } - } - PMA_DBI_free_result($res); - unset($res, $row1, $current); - - $html_output .= '' . "\n" - . '' . "\n" - . '
' . "\n" - . ' ' . __('Table-specific privileges') - . PMA_showHint(__('Note: MySQL privilege names are expressed in English')) - . '' . "\n"; - - // privs that are attached to a specific column - $html_output .= PMA_getHtmlForDisplayColumnPrivileges( - $columns, $row, 'Select_priv', 'SELECT', - 'select', __('Allows reading data.'), 'Select' - ); - - $html_output .= PMA_getHtmlForDisplayColumnPrivileges( - $columns, $row, 'Insert_priv', 'INSERT', - 'insert', __('Allows inserting and replacing data.'), 'Insert' - ); - - $html_output .= PMA_getHtmlForDisplayColumnPrivileges( - $columns, $row, 'Update_priv', 'UPDATE', - 'update', __('Allows changing data.'), 'Update' - ); - - $html_output .= PMA_getHtmlForDisplayColumnPrivileges( - $columns, $row, 'References_priv', 'REFERENCES', 'references', - __('Has no effect in this MySQL version.'), 'References' - ); - - // privs that are not attached to a specific column - - $html_output .= '
' . "\n"; - foreach ($row as $current_grant => $current_grant_value) { - $grant_type = substr($current_grant, 0, (strlen($current_grant) - 5)); - if (in_array($grant_type, array('Select', 'Insert', 'Update', 'References'))) { - continue; - } - // make a substitution to match the messages variables; - // also we must substitute the grant we get, because we can't generate - // a form variable containing blanks (those would get changed to - // an underscore when receiving the POST) - if ($current_grant == 'Create View_priv') { - $tmp_current_grant = 'CreateView_priv'; - $current_grant = 'Create_view_priv'; - } elseif ($current_grant == 'Show view_priv') { - $tmp_current_grant = 'ShowView_priv'; - $current_grant = 'Show_view_priv'; - } else { - $tmp_current_grant = $current_grant; - } - - $html_output .= '
' . "\n" - . '' . "\n"; - - $html_output .= '' . "\n" - . '
' . "\n"; - } // end foreach () - - $html_output .= '
' . "\n"; - // for Safari 2.0.2 - $html_output .= '
' . "\n"; - - } else { - - // g l o b a l o r d b - s p e c i f i c - // - $privTable_names = array(0 => __('Data'), 1 => __('Structure'), 2 => __('Administration')); - - // d a t a - $privTable[0] = array( - array('Select', 'SELECT', __('Allows reading data.')), - array('Insert', 'INSERT', __('Allows inserting and replacing data.')), - array('Update', 'UPDATE', __('Allows changing data.')), - array('Delete', 'DELETE', __('Allows deleting data.')) - ); - if ($db == '*') { - $privTable[0][] = array('File', 'FILE', __('Allows importing data from and exporting data into files.')); - } - - // s t r u c t u r e - $privTable[1] = array( - array('Create', 'CREATE', ($table == '*' ? __('Allows creating new databases and tables.') : __('Allows creating new tables.'))), - array('Alter', 'ALTER', __('Allows altering the structure of existing tables.')), - array('Index', 'INDEX', __('Allows creating and dropping indexes.')), - array('Drop', 'DROP', ($table == '*' ? __('Allows dropping databases and tables.') : __('Allows dropping tables.'))), - array('Create_tmp_table', 'CREATE TEMPORARY TABLES', __('Allows creating temporary tables.')), - array('Show_view', 'SHOW VIEW', __('Allows performing SHOW CREATE VIEW queries.')), - array('Create_routine', 'CREATE ROUTINE', __('Allows creating stored routines.')), - array('Alter_routine', 'ALTER ROUTINE', __('Allows altering and dropping stored routines.')), - array('Execute', 'EXECUTE', __('Allows executing stored routines.')), - ); - // this one is for a db-specific priv: Create_view_priv - if (isset($row['Create_view_priv'])) { - $privTable[1][] = array('Create_view', 'CREATE VIEW', __('Allows creating new views.')); - } - // this one is for a table-specific priv: Create View_priv - if (isset($row['Create View_priv'])) { - $privTable[1][] = array('Create View', 'CREATE VIEW', __('Allows creating new views.')); - } - if (isset($row['Event_priv'])) { - // MySQL 5.1.6 - $privTable[1][] = array('Event', 'EVENT', __('Allows to set up events for the event scheduler')); - $privTable[1][] = array('Trigger', 'TRIGGER', __('Allows creating and dropping triggers')); - } - - // a d m i n i s t r a t i o n - $privTable[2] = array( - array('Grant', 'GRANT', __('Allows adding users and privileges without reloading the privilege tables.')), - ); - if ($db == '*') { - $privTable[2][] = array('Super', 'SUPER', __('Allows connecting, even if maximum number of connections is reached; required for most administrative operations like setting global variables or killing threads of other users.')); - $privTable[2][] = array('Process', 'PROCESS', __('Allows viewing processes of all users')); - $privTable[2][] = array('Reload', 'RELOAD', __('Allows reloading server settings and flushing the server\'s caches.')); - $privTable[2][] = array('Shutdown', 'SHUTDOWN', __('Allows shutting down the server.')); - $privTable[2][] = array('Show_db', 'SHOW DATABASES', __('Gives access to the complete list of databases.')); - } - $privTable[2][] = array('Lock_tables', 'LOCK TABLES', __('Allows locking tables for the current thread.')); - $privTable[2][] = array('References', 'REFERENCES', __('Has no effect in this MySQL version.')); - if ($db == '*') { - $privTable[2][] = array('Repl_client', 'REPLICATION CLIENT', __('Allows the user to ask where the slaves / masters are.')); - $privTable[2][] = array('Repl_slave', 'REPLICATION SLAVE', __('Needed for the replication slaves.')); - $privTable[2][] = array('Create_user', 'CREATE USER', __('Allows creating, dropping and renaming user accounts.')); - } - $html_output .= '' . "\n" - . '
' . "\n" - . '' . "\n" - . ' ' - . ($db == '*' - ? __('Global privileges') - : ($table == '*' - ? __('Database-specific privileges') - : __('Table-specific privileges'))) . "\n" - . '(' - . __('Check All') . ' /' . "\n" - . '' - . __('Uncheck All') . ')' . "\n" - . '' . "\n" - . '

' . __('Note: MySQL privilege names are expressed in English') . '

' . "\n"; - - // Output the Global privilege tables with checkboxes - foreach ($privTable as $i => $table) { - $html_output .= '
' . "\n" - . '' . __($privTable_names[$i]) . '' . "\n"; - foreach ($table as $priv) { - $html_output .= '
' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n"; - } - $html_output .= '
' . "\n"; - } - - // The "Resource limits" box is not displayed for db-specific privs - if ($db == '*') { - $html_output .= PMA_getHtmlForDisplayResourceLimits($row); - } - // for Safari 2.0.2 - $html_output .= '
' . "\n"; - } - $html_output .= '
' . "\n"; - if ($submit) { - $html_output .= '' . "\n"; - } - return $html_output; -} // end of the 'PMA_displayPrivTable()' function - -/** - * Get HTML for "Resource limits" - * - * @param array $row first row from result or boolean false - * - * @return string html snippet - */ -function PMA_getHtmlForDisplayResourceLimits($row) -{ - return '
' . "\n" - . '' . __('Resource limits') . '' . "\n" - . '

' . __('Note: Setting these options to 0 (zero) removes the limit.') . '

' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n" - . '
' . "\n"; -} - -/** - * Displays the fields used by the "new user" form as well as the - * "change login information / copy user" form. - * - * @param string $mode are we creating a new user or are we just - * changing one? (allowed values: 'new', 'change') - * - * @global array $cfg the phpMyAdmin configuration - * @global ressource $user_link the database connection - * - * @return void - */ -function PMA_getHtmlForDisplayLoginInformationFields($mode = 'new') -{ - // Get user/host name lengths - $fields_info = PMA_DBI_get_columns('mysql', 'user', null, true); - $username_length = 16; - $hostname_length = 41; - foreach ($fields_info as $val) { - if ($val['Field'] == 'User') { - strtok($val['Type'], '()'); - $v = strtok('()'); - if (is_int($v)) { - $username_length = $v; - } - } elseif ($val['Field'] == 'Host') { - strtok($val['Type'], '()'); - $v = strtok('()'); - if (is_int($v)) { - $hostname_length = $v; - } - } - } - unset($fields_info); - - if (isset($GLOBALS['username']) && strlen($GLOBALS['username']) === 0) { - $GLOBALS['pred_username'] = 'any'; - } - $html_output = '
' . "\n" - . '' . __('Login Information') . '' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . '' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . ' ' . "\n" - . '' . "\n" - . '' . "\n" - . PMA_showHint(__('When Host table is used, this field is ignored and values stored in Host table are used instead.')) - . '
' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . '' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n" - . '
' . "\n" - . '' . "\n" - . ' ' . "\n" - . '' . "\n" - . '
' . "\n" - // Generate password added here via jQuery - . '
' . "\n"; - - return $html_output; -} // end of the 'PMA_displayUserAndHostFields()' function - - -/** - * Returns all the grants for a certain user on a certain host - * Used in the export privileges for all users section - * - * @param string $user User name - * @param string $host Host name - * - * @return string containing all the grants text - */ -function PMA_getGrants($user, $host) -{ - $grants = PMA_DBI_fetch_result("SHOW GRANTS FOR '" . PMA_sqlAddSlashes($user) . "'@'" . PMA_sqlAddSlashes($host) . "'"); - $response = ''; - foreach ($grants as $one_grant) { - $response .= $one_grant . ";\n\n"; - } - return $response; -} // end of the 'PMA_getGrants()' function - - - ?> From d37b4a4255bd7f862ff8ee270559b289869bc706 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Thu, 28 Jun 2012 05:32:40 +0530 Subject: [PATCH 007/102] remove new line --- libraries/server_privileges.lib.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 3a58f13642..4000df9151 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -92,9 +92,8 @@ function PMA_extractPrivInfo($row = '', $enableHTML = false) = in_array($current_grant, $users_grants) ? 'Y' : 'N'; } unset($current_grant); - unset($av_grants); - unset($users_grants); } + $privs = array(); $allPrivileges = true; foreach ($grants as $current_grant) { @@ -221,8 +220,7 @@ function PMA_getGrantsArray() 'Super_priv', 'SUPER', __('Allows connecting, even if maximum number of connections is reached; - required for most administrative operations like setting global - variables or killing threads of other users.')), + required for most administrative operations like setting global variables or killing threads of other users.')), array( 'Create_tmp_table_priv', 'CREATE TEMPORARY TABLES', From 58908f962bd89f0e6620eb8d0683fc16f91ae958 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Sat, 30 Jun 2012 00:38:49 +0530 Subject: [PATCH 008/102] function implementations for table specific privileges --- libraries/server_privileges.lib.php | 247 ++++++++++++++++------------ 1 file changed, 146 insertions(+), 101 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 4000df9151..2997a93eac 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -333,13 +333,13 @@ function PMA_getHtmlToDisplayColumnPrivileges($columns, $row, $name_for_select, * * @param string $db the database * @param string $table the table + * @param string $username username for database connection + * @param string $hostname hostname for database connection * * @return string sql query */ -function PMA_getSqlQueryForDisplayPrivTable($db, $table) +function PMA_getSqlQueryForDisplayPrivTable($db, $table, $username, $hostname) { - $username = $GLOBALS['username']; - $hostname = $GLOBALS['hostname']; if ($db == '*') { return "SELECT * FROM `mysql`.`user`" ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" @@ -379,7 +379,9 @@ function PMA_getHtmlToDisplayPrivilegesTable($random_n, $db = '*', $table = '*', } if (isset($GLOBALS['username'])) { - $sql_query = PMA_getSqlQueryForDisplayPrivTable($db, $table); + $username = $GLOBALS['username']; + $hostname = $GLOBALS['hostname']; + $sql_query = PMA_getSqlQueryForDisplayPrivTable($db, $table, $username, $hostname); $row = PMA_DBI_fetch_single_row($sql_query); } if (empty($row)) { @@ -451,103 +453,7 @@ function PMA_getHtmlToDisplayPrivilegesTable($random_n, $db = '*', $table = '*', } // t a b l e - s p e c i f i c p r i v i l e g e s if (! empty($columns)) { - $res = PMA_DBI_query( - 'SELECT `Column_name`, `Column_priv`' - .' FROM `mysql`.`columns_priv`' - .' WHERE `User`' - .' = \'' . PMA_sqlAddSlashes($username) . "'" - .' AND `Host`' - .' = \'' . PMA_sqlAddSlashes($hostname) . "'" - .' AND `Db`' - .' = \'' . PMA_sqlAddSlashes(PMA_unescapeMysqlWildcards($db)) . "'" - .' AND `Table_name`' - .' = \'' . PMA_sqlAddSlashes($table) . '\';' - ); - - while ($row1 = PMA_DBI_fetch_row($res)) { - $row1[1] = explode(',', $row1[1]); - foreach ($row1[1] as $current) { - $columns[$row1[0]][$current] = true; - } - } - PMA_DBI_free_result($res); - unset($res, $row1, $current); - - $html_output .= '' . "\n" - . '' . "\n" - . '
' . "\n" - . ' ' . __('Table-specific privileges') - . PMA_showHint(__('Note: MySQL privilege names are expressed in English')) - . '' . "\n"; - - // privs that are attached to a specific column - $html_output .= PMA_getHtmlForDisplayColumnPrivileges( - $columns, $row, 'Select_priv', 'SELECT', - 'select', __('Allows reading data.'), 'Select' - ); - - $html_output .= PMA_getHtmlForDisplayColumnPrivileges( - $columns, $row, 'Insert_priv', 'INSERT', - 'insert', __('Allows inserting and replacing data.'), 'Insert' - ); - - $html_output .= PMA_getHtmlForDisplayColumnPrivileges( - $columns, $row, 'Update_priv', 'UPDATE', - 'update', __('Allows changing data.'), 'Update' - ); - - $html_output .= PMA_getHtmlForDisplayColumnPrivileges( - $columns, $row, 'References_priv', 'REFERENCES', 'references', - __('Has no effect in this MySQL version.'), 'References' - ); - - // privs that are not attached to a specific column - - $html_output .= '
' . "\n"; - foreach ($row as $current_grant => $current_grant_value) { - $grant_type = substr($current_grant, 0, (strlen($current_grant) - 5)); - if (in_array($grant_type, array('Select', 'Insert', 'Update', 'References'))) { - continue; - } - // make a substitution to match the messages variables; - // also we must substitute the grant we get, because we can't generate - // a form variable containing blanks (those would get changed to - // an underscore when receiving the POST) - if ($current_grant == 'Create View_priv') { - $tmp_current_grant = 'CreateView_priv'; - $current_grant = 'Create_view_priv'; - } elseif ($current_grant == 'Show view_priv') { - $tmp_current_grant = 'ShowView_priv'; - $current_grant = 'Show_view_priv'; - } else { - $tmp_current_grant = $current_grant; - } - - $html_output .= '
' . "\n" - . '' . "\n"; - - $html_output .= '' . "\n" - . '
' . "\n"; - } // end foreach () - - $html_output .= '
' . "\n"; - // for Safari 2.0.2 - $html_output .= '
' . "\n"; - + $html_output .= PMA_getHtmlForTableSpecificPrivileges($username, $hostname, $db, $table, $columns); } else { // g l o b a l o r d b - s p e c i f i c @@ -702,6 +608,145 @@ function PMA_getHtmlForDisplayResourceLimits($row) . '
' . "\n"; } +/** + * Get the HTML snippet for table specific privileges + * + * @param string $username username for database connection + * @param string $hostname hostname for database connection + * @param string $db the database + * @param string $table the table + * @param boolean $columns columns array + * + * @return string $html_output + */ +function PMA_getHtmlForTableSpecificPrivileges($username, $hostname, $db, $table, $columns) +{ + $res = PMA_DBI_query( + 'SELECT `Column_name`, `Column_priv`' + .' FROM `mysql`.`columns_priv`' + .' WHERE `User`' + .' = \'' . PMA_sqlAddSlashes($username) . "'" + .' AND `Host`' + .' = \'' . PMA_sqlAddSlashes($hostname) . "'" + .' AND `Db`' + .' = \'' . PMA_sqlAddSlashes(PMA_unescapeMysqlWildcards($db)) . "'" + .' AND `Table_name`' + .' = \'' . PMA_sqlAddSlashes($table) . '\';' + ); + + while ($row1 = PMA_DBI_fetch_row($res)) { + $row1[1] = explode(',', $row1[1]); + foreach ($row1[1] as $current) { + $columns[$row1[0]][$current] = true; + } + } + PMA_DBI_free_result($res); + unset($res, $row1, $current); + + $html_output .= '' . "\n" + . '' . "\n" + . '
' . "\n" + . ' ' . __('Table-specific privileges') + . PMA_showHint(__('Note: MySQL privilege names are expressed in English')) + . '' . "\n"; + + // privs that are attached to a specific column + $html_output .= PMA_getHtmlForAttachedPrivilegesToTableSpecificColumn($columns, $row); + + // privs that are not attached to a specific column + $html_output .= '
' . "\n" + . PMA_getHtmlForNotAttachedPrivilegesToTableSpecificColumn($row, $grant_type) + . '
' . "\n"; + + // for Safari 2.0.2 + $html_output .= '
' . "\n"; + + return $html_output; +} + +/** + * Get HTML snippet for privileges that are attached to a specific column + * + * @param string $columns columns array + * @param array $row first row from result or boolean false + * + * @return string $html_output + */ +function PMA_getHtmlForAttachedPrivilegesToTableSpecificColumn($columns, $row) +{ + $html_output = PMA_getHtmlForDisplayColumnPrivileges( + $columns, $row, 'Select_priv', 'SELECT', + 'select', __('Allows reading data.'), 'Select' + ); + + $html_output .= PMA_getHtmlForDisplayColumnPrivileges( + $columns, $row, 'Insert_priv', 'INSERT', + 'insert', __('Allows inserting and replacing data.'), 'Insert' + ); + + $html_output .= PMA_getHtmlForDisplayColumnPrivileges( + $columns, $row, 'Update_priv', 'UPDATE', + 'update', __('Allows changing data.'), 'Update' + ); + + $html_output .= PMA_getHtmlForDisplayColumnPrivileges( + $columns, $row, 'References_priv', 'REFERENCES', 'references', + __('Has no effect in this MySQL version.'), 'References' + ); + return $html_output; +} + +/** + * Get HTML for privileges that are not attached to a specific column + * + * @param array $row first row from result or boolean false + * @param array $grant_type privilrge type + * + * @return string $html_output + */ +function PMA_getHtmlForNotAttachedPrivilegesToTableSpecificColumn($row, $grant_type) +{ + foreach ($row as $current_grant => $current_grant_value) { + $grant_type = substr($current_grant, 0, (strlen($current_grant) - 5)); + if (in_array($grant_type, array('Select', 'Insert', 'Update', 'References'))) { + continue; + } + // make a substitution to match the messages variables; + // also we must substitute the grant we get, because we can't generate + // a form variable containing blanks (those would get changed to + // an underscore when receiving the POST) + if ($current_grant == 'Create View_priv') { + $tmp_current_grant = 'CreateView_priv'; + $current_grant = 'Create_view_priv'; + } elseif ($current_grant == 'Show view_priv') { + $tmp_current_grant = 'ShowView_priv'; + $current_grant = 'Show_view_priv'; + } else { + $tmp_current_grant = $current_grant; + } + + $html_output .= '
' . "\n" + . '' . "\n"; + + $html_output .= '' . "\n" + . '
' . "\n"; + } // end foreach () + return $html_output; +} /** * Displays the fields used by the "new user" form as well as the * "change login information / copy user" form. From 5a6c11259bde32114dfe1cc3aac6189432fcf2b5 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Sat, 30 Jun 2012 00:42:46 +0530 Subject: [PATCH 009/102] removing new line --- libraries/server_privileges.lib.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 2997a93eac..82e6d2e077 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -219,8 +219,7 @@ function PMA_getGrantsArray() array( 'Super_priv', 'SUPER', - __('Allows connecting, even if maximum number of connections is reached; - required for most administrative operations like setting global variables or killing threads of other users.')), + __('Allows connecting, even if maximum number of connections is reached; required for most administrative operations like setting global variables or killing threads of other users.')), array( 'Create_tmp_table_priv', 'CREATE TEMPORARY TABLES', From adec50eb2487473a26004d53cb33817076072c6b Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Sat, 30 Jun 2012 10:50:02 +0530 Subject: [PATCH 010/102] functions for global specific privs --- libraries/server_privileges.lib.php | 284 ++++++++++++++++++---------- 1 file changed, 181 insertions(+), 103 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 82e6d2e077..b5028a4b7c 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -454,110 +454,8 @@ function PMA_getHtmlToDisplayPrivilegesTable($random_n, $db = '*', $table = '*', if (! empty($columns)) { $html_output .= PMA_getHtmlForTableSpecificPrivileges($username, $hostname, $db, $table, $columns); } else { - // g l o b a l o r d b - s p e c i f i c - // - $privTable_names = array(0 => __('Data'), 1 => __('Structure'), 2 => __('Administration')); - - // d a t a - $privTable[0] = array( - array('Select', 'SELECT', __('Allows reading data.')), - array('Insert', 'INSERT', __('Allows inserting and replacing data.')), - array('Update', 'UPDATE', __('Allows changing data.')), - array('Delete', 'DELETE', __('Allows deleting data.')) - ); - if ($db == '*') { - $privTable[0][] = array('File', 'FILE', __('Allows importing data from and exporting data into files.')); - } - - // s t r u c t u r e - $privTable[1] = array( - array('Create', 'CREATE', ($table == '*' ? __('Allows creating new databases and tables.') : __('Allows creating new tables.'))), - array('Alter', 'ALTER', __('Allows altering the structure of existing tables.')), - array('Index', 'INDEX', __('Allows creating and dropping indexes.')), - array('Drop', 'DROP', ($table == '*' ? __('Allows dropping databases and tables.') : __('Allows dropping tables.'))), - array('Create_tmp_table', 'CREATE TEMPORARY TABLES', __('Allows creating temporary tables.')), - array('Show_view', 'SHOW VIEW', __('Allows performing SHOW CREATE VIEW queries.')), - array('Create_routine', 'CREATE ROUTINE', __('Allows creating stored routines.')), - array('Alter_routine', 'ALTER ROUTINE', __('Allows altering and dropping stored routines.')), - array('Execute', 'EXECUTE', __('Allows executing stored routines.')), - ); - // this one is for a db-specific priv: Create_view_priv - if (isset($row['Create_view_priv'])) { - $privTable[1][] = array('Create_view', 'CREATE VIEW', __('Allows creating new views.')); - } - // this one is for a table-specific priv: Create View_priv - if (isset($row['Create View_priv'])) { - $privTable[1][] = array('Create View', 'CREATE VIEW', __('Allows creating new views.')); - } - if (isset($row['Event_priv'])) { - // MySQL 5.1.6 - $privTable[1][] = array('Event', 'EVENT', __('Allows to set up events for the event scheduler')); - $privTable[1][] = array('Trigger', 'TRIGGER', __('Allows creating and dropping triggers')); - } - - // a d m i n i s t r a t i o n - $privTable[2] = array( - array('Grant', 'GRANT', __('Allows adding users and privileges without reloading the privilege tables.')), - ); - if ($db == '*') { - $privTable[2][] = array('Super', 'SUPER', __('Allows connecting, even if maximum number of connections is reached; required for most administrative operations like setting global variables or killing threads of other users.')); - $privTable[2][] = array('Process', 'PROCESS', __('Allows viewing processes of all users')); - $privTable[2][] = array('Reload', 'RELOAD', __('Allows reloading server settings and flushing the server\'s caches.')); - $privTable[2][] = array('Shutdown', 'SHUTDOWN', __('Allows shutting down the server.')); - $privTable[2][] = array('Show_db', 'SHOW DATABASES', __('Gives access to the complete list of databases.')); - } - $privTable[2][] = array('Lock_tables', 'LOCK TABLES', __('Allows locking tables for the current thread.')); - $privTable[2][] = array('References', 'REFERENCES', __('Has no effect in this MySQL version.')); - if ($db == '*') { - $privTable[2][] = array('Repl_client', 'REPLICATION CLIENT', __('Allows the user to ask where the slaves / masters are.')); - $privTable[2][] = array('Repl_slave', 'REPLICATION SLAVE', __('Needed for the replication slaves.')); - $privTable[2][] = array('Create_user', 'CREATE USER', __('Allows creating, dropping and renaming user accounts.')); - } - $html_output .= '' . "\n" - . '
' . "\n" - . '' . "\n" - . ' ' - . ($db == '*' - ? __('Global privileges') - : ($table == '*' - ? __('Database-specific privileges') - : __('Table-specific privileges'))) . "\n" - . '(' - . __('Check All') . ' /' . "\n" - . '' - . __('Uncheck All') . ')' . "\n" - . '' . "\n" - . '

' . __('Note: MySQL privilege names are expressed in English') . '

' . "\n"; - - // Output the Global privilege tables with checkboxes - foreach ($privTable as $i => $table) { - $html_output .= '
' . "\n" - . '' . __($privTable_names[$i]) . '' . "\n"; - foreach ($table as $priv) { - $html_output .= '
' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n"; - } - $html_output .= '
' . "\n"; - } - - // The "Resource limits" box is not displayed for db-specific privs - if ($db == '*') { - $html_output .= PMA_getHtmlForDisplayResourceLimits($row); - } - // for Safari 2.0.2 - $html_output .= '
' . "\n"; + $html_output .= PMA_getHtmlForGlobalOrDbSpecificPrivs($db, $table, $row, $random_n); } $html_output .= '
' . "\n"; if ($submit) { @@ -746,6 +644,186 @@ function PMA_getHtmlForNotAttachedPrivilegesToTableSpecificColumn($row, $grant_t } // end foreach () return $html_output; } + +/** + * Get HTML for global or database specific privileges + * + * @param string $db the database + * @param string $table the table + * @param string $row first row from result or boolean false + * @param string $random_n a random number that will be appended + * to the id of the user forms + * + * @return string $html_output + */ +function PMA_getHtmlForGlobalOrDbSpecificPrivs($db, $table, $row, $random_n) +{ + $privTable_names = array(0 => __('Data'), 1 => __('Structure'), 2 => __('Administration')); + $privTable = array(); + // d a t a + $privTable[0] = PMA_getDataPrivilegeTable($db); + + // s t r u c t u r e + $privTable[1] = PMA_getStructurePrivilegeTable($table, $row); + + // a d m i n i s t r a t i o n + $privTable[2] = PMA_getAdministrationPrivilegeTable($db); + + $html_output = '' . "\n" + . '
' . "\n" + . '' . "\n" + . ' ' + . ($db == '*' + ? __('Global privileges') + : ($table == '*' + ? __('Database-specific privileges') + : __('Table-specific privileges'))) . "\n" + . '(' + . __('Check All') . ' /' . "\n" + . '' + . __('Uncheck All') . ')' . "\n" + . '' . "\n" + . '

' . __('Note: MySQL privilege names are expressed in English') . '

' . "\n"; + + // Output the Global privilege tables with checkboxes + $html_output .= PMA_getHtmlForGlobalPrivTableWithCheckboxes($privTable, $privTable_names, $row); + + // The "Resource limits" box is not displayed for db-specific privs + if ($db == '*') { + $html_output .= PMA_getHtmlForDisplayResourceLimits($row); + } + // for Safari 2.0.2 + $html_output .= '
' . "\n"; + + return $html_output; +} + +/** + * Get data privilege table as an array + * + * @param string $db the database + * + * @return string data privilege table + */ +function PMA_getDataPrivilegeTable($db) +{ + $data_privTable = array( + array('Select', 'SELECT', __('Allows reading data.')), + array('Insert', 'INSERT', __('Allows inserting and replacing data.')), + array('Update', 'UPDATE', __('Allows changing data.')), + array('Delete', 'DELETE', __('Allows deleting data.')) + ); + if ($db == '*') { + $data_privTable[] + = array('File', + 'FILE', + __('Allows importing data from and exporting data into files.') + ); + } + return $data_privTable; +} + +/** + * Get structure privilege table as an array + * + * @param string $table the table + * @param array $row first row from result or boolean false + * + * @return string structure privilege table + */ +function PMA_getStructurePrivilegeTable($table, $row) +{ + $structure_privTable = array( + array('Create', 'CREATE', ($table == '*' ? __('Allows creating new databases and tables.') : __('Allows creating new tables.'))), + array('Alter', 'ALTER', __('Allows altering the structure of existing tables.')), + array('Index', 'INDEX', __('Allows creating and dropping indexes.')), + array('Drop', 'DROP', ($table == '*' ? __('Allows dropping databases and tables.') : __('Allows dropping tables.'))), + array('Create_tmp_table', 'CREATE TEMPORARY TABLES', __('Allows creating temporary tables.')), + array('Show_view', 'SHOW VIEW', __('Allows performing SHOW CREATE VIEW queries.')), + array('Create_routine', 'CREATE ROUTINE', __('Allows creating stored routines.')), + array('Alter_routine', 'ALTER ROUTINE', __('Allows altering and dropping stored routines.')), + array('Execute', 'EXECUTE', __('Allows executing stored routines.')), + ); + // this one is for a db-specific priv: Create_view_priv + if (isset($row['Create_view_priv'])) { + $structure_privTable[] = array('Create_view', 'CREATE VIEW', __('Allows creating new views.')); + } + // this one is for a table-specific priv: Create View_priv + if (isset($row['Create View_priv'])) { + $structure_privTable[] = array('Create View', 'CREATE VIEW', __('Allows creating new views.')); + } + if (isset($row['Event_priv'])) { + // MySQL 5.1.6 + $structure_privTable[] = array('Event', 'EVENT', __('Allows to set up events for the event scheduler')); + $structure_privTable[] = array('Trigger', 'TRIGGER', __('Allows creating and dropping triggers')); + } + return $structure_privTable; +} + +/** + * Get administration privilege table as an array + * + * @param string $db the table + * + * @return string administration privilege table + */ +function PMA_getAdministrationPrivilegeTable($db) +{ + $administration_privTable = array( + array('Grant', 'GRANT', __('Allows adding users and privileges without reloading the privilege tables.')), + ); + if ($db == '*') { + $administration_privTable[] = array('Super', 'SUPER', __('Allows connecting, even if maximum number of connections is reached; required for most administrative operations like setting global variables or killing threads of other users.')); + $administration_privTable[] = array('Process', 'PROCESS', __('Allows viewing processes of all users')); + $administration_privTable[] = array('Reload', 'RELOAD', __('Allows reloading server settings and flushing the server\'s caches.')); + $administration_privTable[] = array('Shutdown', 'SHUTDOWN', __('Allows shutting down the server.')); + $administration_privTable[] = array('Show_db', 'SHOW DATABASES', __('Gives access to the complete list of databases.')); + } + $administration_privTable[] = array('Lock_tables', 'LOCK TABLES', __('Allows locking tables for the current thread.')); + $administration_privTable[] = array('References', 'REFERENCES', __('Has no effect in this MySQL version.')); + if ($db == '*') { + $administration_privTable[] = array('Repl_client', 'REPLICATION CLIENT', __('Allows the user to ask where the slaves / masters are.')); + $administration_privTable[] = array('Repl_slave', 'REPLICATION SLAVE', __('Needed for the replication slaves.')); + $administration_privTable[] = array('Create_user', 'CREATE USER', __('Allows creating, dropping and renaming user accounts.')); + } + return $administration_privTable; +} + +/** + * Get HTML snippet for global privileges table with check boxes + * + * @param array $privTable privileges table array + * @param array $privTable_names names of the privilege tables (Data, Structure, Administration) + * @param array $row first row from result or boolean false + * + * @return string $html_output + */ +function PMA_getHtmlForGlobalPrivTableWithCheckboxes($privTable, $privTable_names, $row) +{ + $html_output = ''; + foreach ($privTable as $i => $table) { + $html_output .= '
' . "\n" + . '' . __($privTable_names[$i]) . '' . "\n"; + foreach ($table as $priv) { + $html_output .= '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n"; + } + $html_output .= '
' . "\n"; + } + return $html_output; +} + /** * Displays the fields used by the "new user" form as well as the * "change login information / copy user" form. From 21dc59ed28370c96a2dfef21787336c63d85062c Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Sat, 30 Jun 2012 10:52:38 +0530 Subject: [PATCH 011/102] correct wrong variable define --- libraries/server_privileges.lib.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index b5028a4b7c..ad7c5eafc0 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -540,7 +540,7 @@ function PMA_getHtmlForTableSpecificPrivileges($username, $hostname, $db, $table PMA_DBI_free_result($res); unset($res, $row1, $current); - $html_output .= '' . "\n" + $html_output = '' . "\n" . '' . "\n" . '
' . "\n" . ' ' . __('Table-specific privileges') @@ -603,6 +603,7 @@ function PMA_getHtmlForAttachedPrivilegesToTableSpecificColumn($columns, $row) */ function PMA_getHtmlForNotAttachedPrivilegesToTableSpecificColumn($row, $grant_type) { + $html_output = ''; foreach ($row as $current_grant => $current_grant_value) { $grant_type = substr($current_grant, 0, (strlen($current_grant) - 5)); if (in_array($grant_type, array('Select', 'Insert', 'Update', 'References'))) { From e953deca3c50ee424d7597ce890a8a2641c05ed8 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Sat, 30 Jun 2012 14:05:32 +0530 Subject: [PATCH 012/102] rplace use of PMA_showMessage with PMA_getMessage in insert_edit-lib and tbl_change --- libraries/insert_edit.lib.php | 2 +- tbl_change.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/insert_edit.lib.php b/libraries/insert_edit.lib.php index c6d94b441e..823ff37251 100644 --- a/libraries/insert_edit.lib.php +++ b/libraries/insert_edit.lib.php @@ -140,7 +140,7 @@ function PMA_showEmptyResultMessageOrSetUniqueCondition($rows, $key_id, // No row returned if (! $rows[$key_id]) { unset($rows[$key_id], $where_clause_array[$key_id]); - PMA_showMessage( + echo PMA_getMessage( __('MySQL returned an empty result set (i.e. zero rows).'), $local_query ); diff --git a/tbl_change.php b/tbl_change.php index cbea2058cb..a33b438601 100644 --- a/tbl_change.php +++ b/tbl_change.php @@ -126,7 +126,7 @@ if (! empty($disp_message)) { if (! isset($disp_query)) { $disp_query = null; } - PMA_showMessage($disp_message, $disp_query); + echo PMA_getMessage($disp_message, $disp_query); } /** From 121333d51fbc17e5926d286e1963120670b8a893 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Sat, 30 Jun 2012 18:54:13 +0530 Subject: [PATCH 013/102] implement PMA_getUsernameAndHostnameLength() in server_privilegs-lib --- libraries/server_privileges.lib.php | 48 +++++++++++++++++------------ 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index ad7c5eafc0..c066e28cef 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -839,26 +839,7 @@ function PMA_getHtmlForGlobalPrivTableWithCheckboxes($privTable, $privTable_name */ function PMA_getHtmlForDisplayLoginInformationFields($mode = 'new') { - // Get user/host name lengths - $fields_info = PMA_DBI_get_columns('mysql', 'user', null, true); - $username_length = 16; - $hostname_length = 41; - foreach ($fields_info as $val) { - if ($val['Field'] == 'User') { - strtok($val['Type'], '()'); - $v = strtok('()'); - if (is_int($v)) { - $username_length = $v; - } - } elseif ($val['Field'] == 'Host') { - strtok($val['Type'], '()'); - $v = strtok('()'); - if (is_int($v)) { - $hostname_length = $v; - } - } - } - unset($fields_info); + list($username_length, $hostname_length) = PMA_getUsernameAndHostnameLength(); if (isset($GLOBALS['username']) && strlen($GLOBALS['username']) === 0) { $GLOBALS['pred_username'] = 'any'; @@ -985,6 +966,33 @@ function PMA_getHtmlForDisplayLoginInformationFields($mode = 'new') return $html_output; } // end of the 'PMA_displayUserAndHostFields()' function +/** + * Get username and hostname length + * + * @return array username length and hostname length + */ +function PMA_getUsernameAndHostnameLength() +{ + $fields_info = PMA_DBI_get_columns('mysql', 'user', null, true); + $username_length = 16; + $hostname_length = 41; + foreach ($fields_info as $val) { + if ($val['Field'] == 'User') { + strtok($val['Type'], '()'); + $value = strtok('()'); + if (is_int($value)) { + $username_length = $value; + } + } elseif ($val['Field'] == 'Host') { + strtok($val['Type'], '()'); + $value = strtok('()'); + if (is_int($value)) { + $hostname_length = $value; + } + } + } + return array($username_length, $hostname_length); +} /** * Returns all the grants for a certain user on a certain host From 9ca636fe21cf6fc63f359715447b654be1395ae0 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Sat, 30 Jun 2012 23:00:30 +0530 Subject: [PATCH 014/102] modify a doc comment --- libraries/server_privileges.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index c066e28cef..90e814756d 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -835,7 +835,7 @@ function PMA_getHtmlForGlobalPrivTableWithCheckboxes($privTable, $privTable_name * @global array $cfg the phpMyAdmin configuration * @global ressource $user_link the database connection * - * @return void + * @return string $html_output a HTML snippet */ function PMA_getHtmlForDisplayLoginInformationFields($mode = 'new') { From e4631d837e5251fb39c33f24e4de5df5b5e7cba2 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Sun, 1 Jul 2012 13:56:53 +0530 Subject: [PATCH 015/102] function for update password in privileges --- libraries/server_privileges.lib.php | 46 +++++++++++++++++++++++++++++ server_privileges.php | 31 ++----------------- 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 90e814756d..cdd151d3a3 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1013,4 +1013,50 @@ function PMA_getGrants($user, $host) return $response; } // end of the 'PMA_getGrants()' function +/** + * Update password and get message for password updating + * + * @param string $pma_pw password that user entered for change, comming from request + * @param string $pma_pw2 Re typed password, comming from request + * @param string $err_url error url + * @param string $username username + * @param string $hostname hostname + * + * @return string $message success or error message after updating password + */ +function PMA_getMessageForUpdatePassword($pma_pw, $pma_pw2, $err_url, $username, $hostname) +{ + // similar logic in user_password.php + $message = ''; + + if (empty($_REQUEST['nopass']) && isset($pma_pw) && isset($pma_pw2)) { + if ($pma_pw != $pma_pw2) { + $message = PMA_Message::error(__('The passwords aren\'t the same!')); + } elseif (empty($pma_pw) || empty($pma_pw2)) { + $message = PMA_Message::error(__('The password is empty!')); + } + } + + // here $nopass could be == 1 + if (empty($message)) { + + $hashing_function = (! empty($_REQUEST['pw_hash']) && $_REQUEST['pw_hash'] == 'old' ? 'OLD_' : '') + . 'PASSWORD'; + + // in $sql_query which will be displayed, hide the password + $sql_query = 'SET PASSWORD FOR \'' . PMA_sqlAddSlashes($username) + . '\'@\'' . PMA_sqlAddSlashes($hostname) . '\' = ' + . (($pma_pw == '') ? '\'\'' : $hashing_function . '(\'' . preg_replace('@.@s', '*', $pma_pw) . '\')'); + $local_query = 'SET PASSWORD FOR \'' . PMA_sqlAddSlashes($username) + . '\'@\'' . PMA_sqlAddSlashes($hostname) . '\' = ' + . (($pma_pw == '') ? '\'\'' : $hashing_function . '(\'' . PMA_sqlAddSlashes($pma_pw) . '\')'); + + PMA_DBI_try_query($local_query) + or PMA_mysqlDie(PMA_DBI_getError(), $sql_query, false, $err_url); + $message = PMA_Message::success(__('The password for %s was changed successfully.')); + $message->addParam('\'' . htmlspecialchars($username) . '\'@\'' . htmlspecialchars($hostname) . '\''); + } + return $message; +} + ?> diff --git a/server_privileges.php b/server_privileges.php index e84de0533b..4534c736cc 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -140,7 +140,6 @@ $strPrivDescUsage = __('No privileges.'); */ if (PMA_isValid($_REQUEST['pred_tablename'])) { $tablename = $_REQUEST['pred_tablename']; - unset($pred_tablename); } elseif (PMA_isValid($_REQUEST['tablename'])) { $tablename = $_REQUEST['tablename']; } else { @@ -586,39 +585,15 @@ if (isset($_REQUEST['revokeall'])) { } } - /** * Updates the password */ if (isset($_REQUEST['change_pw'])) { - // similar logic in user_password.php - $message = ''; - - if (empty($_REQUEST['nopass']) && isset($pma_pw) && isset($pma_pw2)) { - if ($pma_pw != $pma_pw2) { - $message = PMA_Message::error(__('The passwords aren\'t the same!')); - } elseif (empty($pma_pw) || empty($pma_pw2)) { - $message = PMA_Message::error(__('The password is empty!')); - } - } // end if - - // here $nopass could be == 1 - if (empty($message)) { - - $hashing_function = (! empty($pw_hash) && $pw_hash == 'old' ? 'OLD_' : '') - . 'PASSWORD'; - - // in $sql_query which will be displayed, hide the password - $sql_query = 'SET PASSWORD FOR \'' . PMA_sqlAddSlashes($username) . '\'@\'' . PMA_sqlAddSlashes($hostname) . '\' = ' . (($pma_pw == '') ? '\'\'' : $hashing_function . '(\'' . preg_replace('@.@s', '*', $pma_pw) . '\')'); - $local_query = 'SET PASSWORD FOR \'' . PMA_sqlAddSlashes($username) . '\'@\'' . PMA_sqlAddSlashes($hostname) . '\' = ' . (($pma_pw == '') ? '\'\'' : $hashing_function . '(\'' . PMA_sqlAddSlashes($pma_pw) . '\')'); - PMA_DBI_try_query($local_query) - or PMA_mysqlDie(PMA_DBI_getError(), $sql_query, false, $err_url); - $message = PMA_Message::success(__('The password for %s was changed successfully.')); - $message->addParam('\'' . htmlspecialchars($username) . '\'@\'' . htmlspecialchars($hostname) . '\''); - } + $message = PMA_getMessageForUpdatePassword( + $pma_pw, $pma_pw2, $err_url, $username, $hostname + ); } - /** * Deletes users * (Changes / copies a user, part IV) From 8f2af471a46311208d5bdfca852592a7694fd679 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Sun, 1 Jul 2012 15:18:20 +0530 Subject: [PATCH 016/102] a function for privileges revoke --- libraries/server_privileges.lib.php | 34 +++++++++++++++++++++++++++++ server_privileges.php | 25 ++++----------------- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index cdd151d3a3..ac54c66d22 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1059,4 +1059,38 @@ function PMA_getMessageForUpdatePassword($pma_pw, $pma_pw2, $err_url, $username, return $message; } +/** + * Revokes privileges and get message and SQL query for privileges revokes + * + * @param string $db_and_table wildcard Escaped database+table specification + * @param string $dbname database name + * @param string $tablename table name + * @param string $sql_query0 sql query + * @param string $sql_query1 sql query + * @param string $username username + * @param string $hostname hostname + * @return array ($message, $sql_query) + */ +function PMA_getMessageAndSqlQueryForPrivilegesRevoke($db_and_table, $dbname, + $tablename, $sql_query0, $sql_query1, $username, $hostname +) { + $db_and_table = PMA_wildcardEscapeForGrant($dbname, isset($tablename) ? $tablename : ''); + + $sql_query0 = 'REVOKE ALL PRIVILEGES ON ' . $db_and_table + . ' FROM \'' . PMA_sqlAddSlashes($username) . '\'@\'' . PMA_sqlAddSlashes($hostname) . '\';'; + $sql_query1 = 'REVOKE GRANT OPTION ON ' . $db_and_table + . ' FROM \'' . PMA_sqlAddSlashes($username) . '\'@\'' . PMA_sqlAddSlashes($hostname) . '\';'; + + PMA_DBI_query($sql_query0); + if (! PMA_DBI_try_query($sql_query1)) { + // this one may fail, too... + $sql_query1 = ''; + } + $sql_query = $sql_query0 . ' ' . $sql_query1; + $message = PMA_Message::success(__('You have revoked the privileges for %s')); + $message->addParam('\'' . htmlspecialchars($username) . '\'@\'' . htmlspecialchars($hostname) . '\''); + + return array($message, $sql_query); +} + ?> diff --git a/server_privileges.php b/server_privileges.php index 4534c736cc..921c41c6e5 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -563,26 +563,10 @@ if (! empty($update_privs)) { * Revokes Privileges */ if (isset($_REQUEST['revokeall'])) { - $db_and_table = PMA_wildcardEscapeForGrant($dbname, isset($tablename) ? $tablename : ''); - - $sql_query0 = 'REVOKE ALL PRIVILEGES ON ' . $db_and_table - . ' FROM \'' . PMA_sqlAddSlashes($username) . '\'@\'' . PMA_sqlAddSlashes($hostname) . '\';'; - $sql_query1 = 'REVOKE GRANT OPTION ON ' . $db_and_table - . ' FROM \'' . PMA_sqlAddSlashes($username) . '\'@\'' . PMA_sqlAddSlashes($hostname) . '\';'; - - PMA_DBI_query($sql_query0); - if (! PMA_DBI_try_query($sql_query1)) { - // this one may fail, too... - $sql_query1 = ''; - } - $sql_query = $sql_query0 . ' ' . $sql_query1; - $message = PMA_Message::success(__('You have revoked the privileges for %s')); - $message->addParam('\'' . htmlspecialchars($username) . '\'@\'' . htmlspecialchars($hostname) . '\''); - if (! isset($tablename)) { - unset($dbname); - } else { - unset($tablename); - } + list ($message, $sql_query) = PMA_getMessageAndSqlQueryForPrivilegesRevoke( + $db_and_table, $dbname, $tablename, $sql_query0, $sql_query1, $username, + $hostname + ); } /** @@ -650,7 +634,6 @@ if (isset($_REQUEST['delete']) || (isset($_REQUEST['change_copy']) && $_REQUEST[ } } - /** * Changes / copies a user, part V */ From c09e857763c6ab995862bdc211b76e7b816adfb5 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Sun, 1 Jul 2012 17:09:23 +0530 Subject: [PATCH 017/102] remove duplicate code snippet from server_privileges script --- libraries/server_privileges.lib.php | 37 +++++++++++++++++ server_privileges.php | 64 +++++------------------------ 2 files changed, 47 insertions(+), 54 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index ac54c66d22..85aea4ad16 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1093,4 +1093,41 @@ function PMA_getMessageAndSqlQueryForPrivilegesRevoke($db_and_table, $dbname, return array($message, $sql_query); } +/**' + * Get a common SQL query for 'update privileges' and 'add user' + * + * @param type $Grant_priv grant privileges + * @param type $max_questions maximum questions + * @param type $max_connections maximum connections + * @param type $max_updates maximum updates + * @param type $max_user_connections maximum userconnections + * + * @return string $sql_query + */ +function PMA_getCommonSQlQueryForAddUserAndUpdatePrivs($Grant_priv, $max_questions, $max_connections, + $max_updates, $max_user_connections +) { + $sql_query = 'WITH'; + if (isset($Grant_priv) && $Grant_priv == 'Y') { + $sql_query .= ' GRANT OPTION'; + } + if (isset($Grant_priv)) { + $max_questions = max(0, (int)$max_questions); + $sql_query .= ' MAX_QUERIES_PER_HOUR ' . $max_questions; + } + if (isset($max_connections)) { + $max_connections = max(0, (int)$max_connections); + $sql_query .= ' MAX_CONNECTIONS_PER_HOUR ' . $max_connections; + } + if (isset($max_updates)) { + $max_updates = max(0, (int)$max_updates); + $sql_query .= ' MAX_UPDATES_PER_HOUR ' . $max_updates; + } + if (isset($max_user_connections)) { + $max_user_connections = max(0, (int)$max_user_connections); + $sql_query .= ' MAX_USER_CONNECTIONS ' . $max_user_connections; + } + return $sql_query; +} + ?> diff --git a/server_privileges.php b/server_privileges.php index 921c41c6e5..04779c5f6b 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -216,7 +216,6 @@ if (isset($_REQUEST['change_copy'])) { } } - /** * Adds a user * (Changes / copies a user, part II) @@ -275,40 +274,16 @@ if (isset($_REQUEST['adduser_submit']) || isset($_REQUEST['change_copy'])) { $create_user_show = $create_user_real; } } - /** - * @todo similar code appears twice in this script - */ + if ((isset($Grant_priv) && $Grant_priv == 'Y') || (isset($max_questions) || isset($max_connections) || isset($max_updates) || isset($max_user_connections)) ) { - $real_sql_query .= ' WITH'; - $sql_query .= ' WITH'; - if (isset($Grant_priv) && $Grant_priv == 'Y') { - $real_sql_query .= ' GRANT OPTION'; - $sql_query .= ' GRANT OPTION'; - } - if (isset($max_questions)) { - // avoid negative values - $max_questions = max(0, (int)$max_questions); - $real_sql_query .= ' MAX_QUERIES_PER_HOUR ' . $max_questions; - $sql_query .= ' MAX_QUERIES_PER_HOUR ' . $max_questions; - } - if (isset($max_connections)) { - $max_connections = max(0, (int)$max_connections); - $real_sql_query .= ' MAX_CONNECTIONS_PER_HOUR ' . $max_connections; - $sql_query .= ' MAX_CONNECTIONS_PER_HOUR ' . $max_connections; - } - if (isset($max_updates)) { - $max_updates = max(0, (int)$max_updates); - $real_sql_query .= ' MAX_UPDATES_PER_HOUR ' . $max_updates; - $sql_query .= ' MAX_UPDATES_PER_HOUR ' . $max_updates; - } - if (isset($max_user_connections)) { - $max_user_connections = max(0, (int)$max_user_connections); - $real_sql_query .= ' MAX_USER_CONNECTIONS ' . $max_user_connections; - $sql_query .= ' MAX_USER_CONNECTIONS ' . $max_user_connections; - } + $real_sql_query .= PMA_getCommonSQlQueryForAddUserAndUpdatePrivs( + $Grant_priv, $max_questions, $max_connections,$max_updates, + $max_user_connections + ); + $sql_query .= $real_sql_query; } if (isset($create_user_real)) { $create_user_real .= ';'; @@ -508,34 +483,15 @@ if (! empty($update_privs)) { . ' ON ' . $db_and_table . ' TO \'' . PMA_sqlAddSlashes($username) . '\'@\'' . PMA_sqlAddSlashes($hostname) . '\''; - /** - * @todo similar code appears twice in this script - */ if ((isset($Grant_priv) && $Grant_priv == 'Y') || (! isset($dbname) && (isset($max_questions) || isset($max_connections) || isset($max_updates) || isset($max_user_connections))) ) { - $sql_query2 .= 'WITH'; - if (isset($Grant_priv) && $Grant_priv == 'Y') { - $sql_query2 .= ' GRANT OPTION'; - } - if (isset($max_questions)) { - $max_questions = max(0, (int)$max_questions); - $sql_query2 .= ' MAX_QUERIES_PER_HOUR ' . $max_questions; - } - if (isset($max_connections)) { - $max_connections = max(0, (int)$max_connections); - $sql_query2 .= ' MAX_CONNECTIONS_PER_HOUR ' . $max_connections; - } - if (isset($max_updates)) { - $max_updates = max(0, (int)$max_updates); - $sql_query2 .= ' MAX_UPDATES_PER_HOUR ' . $max_updates; - } - if (isset($max_user_connections)) { - $max_user_connections = max(0, (int)$max_user_connections); - $sql_query2 .= ' MAX_USER_CONNECTIONS ' . $max_user_connections; - } + $sql_query2 .= PMA_getCommonSQlQueryForAddUserAndUpdatePrivs( + $Grant_priv, $max_questions, $max_connections, $max_updates, + $max_user_connections + ); } $sql_query2 .= ';'; } From d96b0aa7242cf58e15d35f019e6723e59dcf3b74 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Sun, 1 Jul 2012 19:08:14 +0530 Subject: [PATCH 018/102] remove unwanted function parameters --- libraries/server_privileges.lib.php | 6 ++---- server_privileges.php | 3 +-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 85aea4ad16..86f598699d 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1065,14 +1065,12 @@ function PMA_getMessageForUpdatePassword($pma_pw, $pma_pw2, $err_url, $username, * @param string $db_and_table wildcard Escaped database+table specification * @param string $dbname database name * @param string $tablename table name - * @param string $sql_query0 sql query - * @param string $sql_query1 sql query * @param string $username username - * @param string $hostname hostname + * @param string $hostname host name * @return array ($message, $sql_query) */ function PMA_getMessageAndSqlQueryForPrivilegesRevoke($db_and_table, $dbname, - $tablename, $sql_query0, $sql_query1, $username, $hostname + $tablename, $username, $hostname ) { $db_and_table = PMA_wildcardEscapeForGrant($dbname, isset($tablename) ? $tablename : ''); diff --git a/server_privileges.php b/server_privileges.php index 04779c5f6b..e999df2de4 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -520,8 +520,7 @@ if (! empty($update_privs)) { */ if (isset($_REQUEST['revokeall'])) { list ($message, $sql_query) = PMA_getMessageAndSqlQueryForPrivilegesRevoke( - $db_and_table, $dbname, $tablename, $sql_query0, $sql_query1, $username, - $hostname + $db_and_table, $dbname, $tablename, $username, $hostname ); } From 39cb681f265246296a5fbb02bee70e707b98e5b4 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Sun, 1 Jul 2012 23:58:05 +0530 Subject: [PATCH 019/102] modify PMA_getCommonSQlQueryForAddUserAndUpdatePrivs() function --- libraries/server_privileges.lib.php | 36 +++++++++++++---------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 86f598699d..d183708f57 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1091,7 +1091,7 @@ function PMA_getMessageAndSqlQueryForPrivilegesRevoke($db_and_table, $dbname, return array($message, $sql_query); } -/**' +/** * Get a common SQL query for 'update privileges' and 'add user' * * @param type $Grant_priv grant privileges @@ -1102,29 +1102,25 @@ function PMA_getMessageAndSqlQueryForPrivilegesRevoke($db_and_table, $dbname, * * @return string $sql_query */ -function PMA_getCommonSQlQueryForAddUserAndUpdatePrivs($Grant_priv, $max_questions, $max_connections, - $max_updates, $max_user_connections +function PMA_getCommonSQlQueryForAddUserAndUpdatePrivs($Grant_priv, $max_questions, + $max_connections, $max_updates, $max_user_connections ) { $sql_query = 'WITH'; - if (isset($Grant_priv) && $Grant_priv == 'Y') { + if ($Grant_priv == 'Y') { $sql_query .= ' GRANT OPTION'; } - if (isset($Grant_priv)) { - $max_questions = max(0, (int)$max_questions); - $sql_query .= ' MAX_QUERIES_PER_HOUR ' . $max_questions; - } - if (isset($max_connections)) { - $max_connections = max(0, (int)$max_connections); - $sql_query .= ' MAX_CONNECTIONS_PER_HOUR ' . $max_connections; - } - if (isset($max_updates)) { - $max_updates = max(0, (int)$max_updates); - $sql_query .= ' MAX_UPDATES_PER_HOUR ' . $max_updates; - } - if (isset($max_user_connections)) { - $max_user_connections = max(0, (int)$max_user_connections); - $sql_query .= ' MAX_USER_CONNECTIONS ' . $max_user_connections; - } + $max_questions = max(0, (int)$max_questions); + $sql_query .= ' MAX_QUERIES_PER_HOUR ' . $max_questions; + + $max_connections = max(0, (int)$max_connections); + $sql_query .= ' MAX_CONNECTIONS_PER_HOUR ' . $max_connections; + + $max_updates = max(0, (int)$max_updates); + $sql_query .= ' MAX_UPDATES_PER_HOUR ' . $max_updates; + + $max_user_connections = max(0, (int)$max_user_connections); + $sql_query .= ' MAX_USER_CONNECTIONS ' . $max_user_connections; + return $sql_query; } From ac4a01ca790648b475ba60a2ad803aba6ceae641 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Tue, 3 Jul 2012 09:50:56 +0530 Subject: [PATCH 020/102] correct wrong common function calls in sever_privileges-lib --- libraries/server_privileges.lib.php | 77 +++++++++++++++++------------ 1 file changed, 45 insertions(+), 32 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index d183708f57..6860e49cd9 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -31,10 +31,10 @@ function PMA_wildcardEscapeForGrant($dbname, $tablename) } else { if (strlen($tablename)) { $db_and_table - = PMA_backquote(PMA_unescapeMysqlWildcards($dbname)) . '.' - . PMA_backquote($tablename); + = PMA_CommonFunctions::getInstance()->backquote(PMA_unescapeMysqlWildcards($dbname)) . '.' + . PMA_CommonFunctions::getInstance()->backquote($tablename); } else { - $db_and_table = PMA_backquote($dbname) . '.*'; + $db_and_table = PMA_CommonFunctions::getInstance()->backquote($dbname) . '.*'; } } return $db_and_table; @@ -53,9 +53,9 @@ function PMA_rangeOfUsers($initial = '') // might be BINARY, so LIKE would be case sensitive if (! empty($initial)) { $ret = " WHERE `User` LIKE '" - . PMA_sqlAddSlashes($initial, true) . "%'" + . PMA_CommonFunctions::getInstance()->sqlAddSlashes($initial, true) . "%'" . " OR `User` LIKE '" - . PMA_sqlAddSlashes(strtolower($initial), true) . "%'"; + . PMA_CommonFunctions::getInstance()->sqlAddSlashes(strtolower($initial), true) . "%'"; } else { $ret = ''; } @@ -339,23 +339,24 @@ function PMA_getHtmlToDisplayColumnPrivileges($columns, $row, $name_for_select, */ function PMA_getSqlQueryForDisplayPrivTable($db, $table, $username, $hostname) { + $common_functions = PMA_CommonFunctions::getInstance(); if ($db == '*') { return "SELECT * FROM `mysql`.`user`" - ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" - ." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "';"; + ." WHERE `User` = '" . $common_functions->sqlAddSlashes($username) . "'" + ." AND `Host` = '" . $common_functions->sqlAddSlashes($hostname) . "';"; } elseif ($table == '*') { return "SELECT * FROM `mysql`.`db`" - ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" - ." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "'" - ." AND '" . PMA_unescapeMysqlWildcards($db) . "'" + ." WHERE `User` = '" . $common_functions->sqlAddSlashes($username) . "'" + ." AND `Host` = '" . $common_functions->sqlAddSlashes($hostname) . "'" + ." AND '" . $common_functions->unescapeMysqlWildcards($db) . "'" ." LIKE `Db`;"; } return "SELECT `Table_priv`" ." FROM `mysql`.`tables_priv`" - ." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'" - ." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "'" - ." AND `Db` = '" . PMA_unescapeMysqlWildcards($db) . "'" - ." AND `Table_name` = '" . PMA_sqlAddSlashes($table) . "';"; + ." WHERE `User` = '" . $common_functions->sqlAddSlashes($username) . "'" + ." AND `Host` = '" . $common_functions->sqlAddSlashes($hostname) . "'" + ." AND `Db` = '" . $common_functions->unescapeMysqlWildcards($db) . "'" + ." AND `Table_name` = '" . $common_functions->sqlAddSlashes($table) . "';"; } /** * Displays the privileges form table @@ -522,13 +523,13 @@ function PMA_getHtmlForTableSpecificPrivileges($username, $hostname, $db, $table 'SELECT `Column_name`, `Column_priv`' .' FROM `mysql`.`columns_priv`' .' WHERE `User`' - .' = \'' . PMA_sqlAddSlashes($username) . "'" + .' = \'' . PMA_CommonFunctions::getInstance()->sqlAddSlashes($username) . "'" .' AND `Host`' - .' = \'' . PMA_sqlAddSlashes($hostname) . "'" + .' = \'' . PMA_CommonFunctions::getInstance()->sqlAddSlashes($hostname) . "'" .' AND `Db`' - .' = \'' . PMA_sqlAddSlashes(PMA_unescapeMysqlWildcards($db)) . "'" + .' = \'' . PMA_CommonFunctions::getInstance()->sqlAddSlashes(PMA_unescapeMysqlWildcards($db)) . "'" .' AND `Table_name`' - .' = \'' . PMA_sqlAddSlashes($table) . '\';' + .' = \'' . PMA_CommonFunctions::getInstance()->sqlAddSlashes($table) . '\';' ); while ($row1 = PMA_DBI_fetch_row($res)) { @@ -544,7 +545,9 @@ function PMA_getHtmlForTableSpecificPrivileges($username, $hostname, $db, $table . '' . "\n" . '
' . "\n" . ' ' . __('Table-specific privileges') - . PMA_showHint(__('Note: MySQL privilege names are expressed in English')) + . PMA_CommonFunctions::getInstance()->showHint( + __('Note: MySQL privilege names are expressed in English') + ) . '' . "\n"; // privs that are attached to a specific column @@ -932,7 +935,7 @@ function PMA_getHtmlForDisplayLoginInformationFields($mode = 'new') . htmlspecialchars(isset($GLOBALS['hostname']) ? $GLOBALS['hostname'] : '') . '" title="' . __('Host') . '" onchange="pred_hostname.value = \'userdefined\';" />' . "\n" - . PMA_showHint(__('When Host table is used, this field is ignored and values stored in Host table are used instead.')) + . PMA_CommonFunctions::getInstance()->showHint(__('When Host table is used, this field is ignored and values stored in Host table are used instead.')) . '' . "\n" . '
' . "\n" . '
' @@ -1397,8 +1397,8 @@ function PMA_getUserForm($checkprivs, $link_edit, $conditional_class) . '' . __('New') . '' . "\n"; $html_output .= '' . "\n" . $common_functions->getIcon('b_usradd.png') @@ -1410,18 +1410,18 @@ function PMA_getUserForm($checkprivs, $link_edit, $conditional_class) } /** - * Get HTML snippet for table body of user form + * Get HTML snippet for table body of specific database privileges * * @param boolean $found whether user found or not * @param array $row array of rows from mysql , db table with list of privileges * @param boolean $odd_row whether odd or not * @param string $link_edit standard link for edit * @param string $res ran sql query - * @param string $checkprivs check privileges + * @param string $dbToCheck check privileges * * @return string $html_output */ -function PMA_getUserFormTableBody($found, $row, $odd_row, $link_edit, $res, $checkprivs) +function PMA_getHtmlTableBodyForSpecificDbPrivs($found, $row, $odd_row, $link_edit, $res, $dbToCheck) { $html_output = '' . "\n"; if ($found) { @@ -1457,7 +1457,7 @@ function PMA_getUserFormTableBody($found, $row, $odd_row, $link_edit, $res, $che . ' '; if (! isset($current['Db']) || $current['Db'] == '*') { $html_output .= __('global'); - } elseif ($current['Db'] == PMA_CommonFunctions::getInstance()->escapeMysqlWildcards($checkprivs)) { + } elseif ($current['Db'] == PMA_CommonFunctions::getInstance()->escapeMysqlWildcards($dbToCheck)) { $html_output .= __('database-specific'); } else { $html_output .= __('wildcard'). ': ' . htmlspecialchars($current['Db']) . ''; diff --git a/server_privileges.php b/server_privileges.php index 6707cb82b4..5ffa7ed4b2 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -647,7 +647,7 @@ if (empty($_REQUEST['adduser']) && (! isset($checkprivs) || ! strlen($checkprivs } else { // check the privileges for a particular database. $response->addHTML( - PMA_getUserForm($checkprivs, $link_edit, $conditional_class) + PMA_getHtmlForSpecificDbPrivileges($checkprivs, $link_edit, $conditional_class) ); } // end if (empty($_REQUEST['adduser']) && empty($checkprivs)) ... elseif ... else ... From 4efa2156887185a3d54152b6caaa07d014b6bca1 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Wed, 11 Jul 2012 20:23:13 +0530 Subject: [PATCH 068/102] improve doc comment --- libraries/server_privileges.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 8c3b903870..7840396a76 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1179,7 +1179,7 @@ function PMA_getMessageAndSqlQueryForPrivilegesRevoke($db_and_table, $dbname, } /** - * Get a common SQL query for 'update privileges' and 'add user' + * Get a WITH clause for 'update privileges' and 'add user' * * @return string $sql_query */ From 6f67701831497c2fa362a261a5a06115d6f1ee75 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Wed, 11 Jul 2012 22:51:19 +0530 Subject: [PATCH 069/102] improve doc comment --- libraries/server_privileges.lib.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 7840396a76..0e9e3c0f48 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1325,7 +1325,7 @@ function PMA_getListOfPrivilegesAndComparedPrivileges() /** * Get the HTML for user form and check the privileges for a particular database. * - * @param string $dbToCheck check privileges + * @param string $dbToCheck database to check for privileges * @param string $link_edit standard link for edit * @param string $conditional_class if ajaxable 'Ajax' otherwise '' * @@ -1417,7 +1417,7 @@ function PMA_getHtmlForSpecificDbPrivileges($dbToCheck, $link_edit, $conditional * @param boolean $odd_row whether odd or not * @param string $link_edit standard link for edit * @param string $res ran sql query - * @param string $dbToCheck check privileges + * @param string $dbToCheck database to check for privileges * * @return string $html_output */ From 519ff99ff6ab3cf42b4e3d179560c6efa7f6d7fe Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Thu, 12 Jul 2012 23:46:53 +0530 Subject: [PATCH 070/102] functions implement for get queries for Db speicific privs --- libraries/server_privileges.lib.php | 119 ++++++++++++++++++++++++++++ server_privileges.php | 79 +----------------- 2 files changed, 121 insertions(+), 77 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 0e9e3c0f48..67b2e53ecf 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -2813,4 +2813,123 @@ function PMA_getHtmlForDisplayUserProperties($dbname_is_wildcard,$url_dbname, return $html_output; } + +/** + * Get queries for Table privileges to change or copy user + * + * @param string $user_host_condition user host condition to select relevent table privileges + * @param string $queries queries array + * @param string $username username + * @param string $hostname host name + * + * @return array $queries + */ +function PMA_getTablePrivsQueriesForChangeOrCopyUser($user_host_condition, + $queries, $username, $hostname) +{ + $common_functions = PMA_CommonFunctions::getInstance(); + $res = PMA_DBI_query( + 'SELECT `Db`, `Table_name`, `Table_priv` FROM `mysql`.`tables_priv`' . $user_host_condition, + $GLOBALS['userlink'], + PMA_DBI_QUERY_STORE + ); + while ($row = PMA_DBI_fetch_assoc($res)) { + + $res2 = PMA_DBI_QUERY( + 'SELECT `Column_name`, `Column_priv`' + .' FROM `mysql`.`columns_priv`' + .' WHERE `User`' + .' = \'' . $common_functions->sqlAddSlashes($_REQUEST['old_username']) . "'" + .' AND `Host`' + .' = \'' . $common_functions->sqlAddSlashes($_REQUEST['old_username']) . '\'' + .' AND `Db`' + .' = \'' . $common_functions->sqlAddSlashes($row['Db']) . "'" + .' AND `Table_name`' + .' = \'' . $common_functions->sqlAddSlashes($row['Table_name']) . "'" + .';', + null, + PMA_DBI_QUERY_STORE + ); + + $tmp_privs1 = PMA_extractPrivInfo($row); + $tmp_privs2 = array( + 'Select' => array(), + 'Insert' => array(), + 'Update' => array(), + 'References' => array() + ); + + while ($row2 = PMA_DBI_fetch_assoc($res2)) { + $tmp_array = explode(',', $row2['Column_priv']); + if (in_array('Select', $tmp_array)) { + $tmp_privs2['Select'][] = $row2['Column_name']; + } + if (in_array('Insert', $tmp_array)) { + $tmp_privs2['Insert'][] = $row2['Column_name']; + } + if (in_array('Update', $tmp_array)) { + $tmp_privs2['Update'][] = $row2['Column_name']; + } + if (in_array('References', $tmp_array)) { + $tmp_privs2['References'][] = $row2['Column_name']; + } + } + if (count($tmp_privs2['Select']) > 0 && ! in_array('SELECT', $tmp_privs1)) { + $tmp_privs1[] = 'SELECT (`' . join('`, `', $tmp_privs2['Select']) . '`)'; + } + if (count($tmp_privs2['Insert']) > 0 && ! in_array('INSERT', $tmp_privs1)) { + $tmp_privs1[] = 'INSERT (`' . join('`, `', $tmp_privs2['Insert']) . '`)'; + } + if (count($tmp_privs2['Update']) > 0 && ! in_array('UPDATE', $tmp_privs1)) { + $tmp_privs1[] = 'UPDATE (`' . join('`, `', $tmp_privs2['Update']) . '`)'; + } + if (count($tmp_privs2['References']) > 0 && ! in_array('REFERENCES', $tmp_privs1)) { + $tmp_privs1[] = 'REFERENCES (`' . join('`, `', $tmp_privs2['References']) . '`)'; + } + + $queries[] = 'GRANT ' . join(', ', $tmp_privs1) + . ' ON ' . $common_functions->backquote($row['Db']) . '.' + . $common_functions->backquote($row['Table_name']) + . ' TO \'' . $common_functions->sqlAddSlashes($username) + . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\'' + . (in_array('Grant', explode(',', $row['Table_priv'])) ? ' WITH GRANT OPTION;' : ';'); + } + return $queries; +} + +/** + * Get queries for database speicific privileges foe change or copy user + * + * @param array $queries queries array with string + * @param string $username username + * @param string $hostname host name + * + * @return array $queries + */ +function PMA_getDbSpecificPrivsQueriesForChangeOrCopyUser($queries, $username, $hostname) +{ + $common_functions = PMA_CommonFunctions::getInstance(); + + $user_host_condition = ' WHERE `User`' + .' = \'' . $common_functions->sqlAddSlashes($_REQUEST['old_username']) . "'" + .' AND `Host`' + .' = \'' . $common_functions->sqlAddSlashes($_REQUEST['old_username']) . '\';'; + + $res = PMA_DBI_query('SELECT * FROM `mysql`.`db`' . $user_host_condition); + + while ($row = PMA_DBI_fetch_assoc($res)) { + $queries[] = 'GRANT ' . join(', ', PMA_extractPrivInfo($row)) + .' ON ' . $common_functions->backquote($row['Db']) . '.*' + .' TO \'' . $common_functions->sqlAddSlashes($username) + . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\'' + . ($row['Grant_priv'] == 'Y' ? ' WITH GRANT OPTION;' : ';'); + } + PMA_DBI_free_result($res); + + $queries = PMA_getTablePrivsQueriesForChangeOrCopyUser( + $user_host_condition, $queries, $username, $hostname + ); + + return $queries; +} ?> diff --git a/server_privileges.php b/server_privileges.php index 5ffa7ed4b2..71455f282d 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -372,88 +372,13 @@ if (isset($_REQUEST['adduser_submit']) || isset($_REQUEST['change_copy'])) { } } - /** * Changes / copies a user, part III */ if (isset($_REQUEST['change_copy'])) { - $user_host_condition = ' WHERE `User`' - .' = \'' . $common_functions->sqlAddSlashes($old_username) . "'" - .' AND `Host`' - .' = \'' . $common_functions->sqlAddSlashes($old_hostname) . '\';'; - $res = PMA_DBI_query('SELECT * FROM `mysql`.`db`' . $user_host_condition); - while ($row = PMA_DBI_fetch_assoc($res)) { - $queries[] = 'GRANT ' . join(', ', PMA_extractPrivInfo($row)) - .' ON ' . $common_functions->backquote($row['Db']) . '.*' - .' TO \'' . $common_functions->sqlAddSlashes($username) . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\'' - . ($row['Grant_priv'] == 'Y' ? ' WITH GRANT OPTION;' : ';'); - } - PMA_DBI_free_result($res); - $res = PMA_DBI_query( - 'SELECT `Db`, `Table_name`, `Table_priv` FROM `mysql`.`tables_priv`' . $user_host_condition, - $GLOBALS['userlink'], - PMA_DBI_QUERY_STORE + $queries = PMA_getDbSpecificPrivsQueriesForChangeOrCopyUser( + $queries, $username, $hostname ); - while ($row = PMA_DBI_fetch_assoc($res)) { - - $res2 = PMA_DBI_QUERY( - 'SELECT `Column_name`, `Column_priv`' - .' FROM `mysql`.`columns_priv`' - .' WHERE `User`' - .' = \'' . $common_functions->sqlAddSlashes($old_username) . "'" - .' AND `Host`' - .' = \'' . $common_functions->sqlAddSlashes($old_hostname) . '\'' - .' AND `Db`' - .' = \'' . $common_functions->sqlAddSlashes($row['Db']) . "'" - .' AND `Table_name`' - .' = \'' . $common_functions->sqlAddSlashes($row['Table_name']) . "'" - .';', - null, - PMA_DBI_QUERY_STORE - ); - - $tmp_privs1 = PMA_extractPrivInfo($row); - $tmp_privs2 = array( - 'Select' => array(), - 'Insert' => array(), - 'Update' => array(), - 'References' => array() - ); - - while ($row2 = PMA_DBI_fetch_assoc($res2)) { - $tmp_array = explode(',', $row2['Column_priv']); - if (in_array('Select', $tmp_array)) { - $tmp_privs2['Select'][] = $row2['Column_name']; - } - if (in_array('Insert', $tmp_array)) { - $tmp_privs2['Insert'][] = $row2['Column_name']; - } - if (in_array('Update', $tmp_array)) { - $tmp_privs2['Update'][] = $row2['Column_name']; - } - if (in_array('References', $tmp_array)) { - $tmp_privs2['References'][] = $row2['Column_name']; - } - unset($tmp_array); - } - if (count($tmp_privs2['Select']) > 0 && ! in_array('SELECT', $tmp_privs1)) { - $tmp_privs1[] = 'SELECT (`' . join('`, `', $tmp_privs2['Select']) . '`)'; - } - if (count($tmp_privs2['Insert']) > 0 && ! in_array('INSERT', $tmp_privs1)) { - $tmp_privs1[] = 'INSERT (`' . join('`, `', $tmp_privs2['Insert']) . '`)'; - } - if (count($tmp_privs2['Update']) > 0 && ! in_array('UPDATE', $tmp_privs1)) { - $tmp_privs1[] = 'UPDATE (`' . join('`, `', $tmp_privs2['Update']) . '`)'; - } - if (count($tmp_privs2['References']) > 0 && ! in_array('REFERENCES', $tmp_privs1)) { - $tmp_privs1[] = 'REFERENCES (`' . join('`, `', $tmp_privs2['References']) . '`)'; - } - unset($tmp_privs2); - $queries[] = 'GRANT ' . join(', ', $tmp_privs1) - . ' ON ' . $common_functions->backquote($row['Db']) . '.' . $common_functions->backquote($row['Table_name']) - . ' TO \'' . $common_functions->sqlAddSlashes($username) . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\'' - . (in_array('Grant', explode(',', $row['Table_priv'])) ? ' WITH GRANT OPTION;' : ';'); - } } /** From 2bb169e1d63082f5a1883d3b635afaffc937fac1 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Fri, 13 Jul 2012 01:02:10 +0530 Subject: [PATCH 071/102] PMA_addUser() function implementation --- libraries/server_privileges.lib.php | 68 ++++++++++++++++++++++++++++- server_privileges.php | 64 +++------------------------ 2 files changed, 72 insertions(+), 60 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 67b2e53ecf..34ef4cb1c8 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -2898,7 +2898,7 @@ function PMA_getTablePrivsQueriesForChangeOrCopyUser($user_host_condition, } /** - * Get queries for database speicific privileges foe change or copy user + * Get queries for database specific privileges for change or copy user * * @param array $queries queries array with string * @param string $username username @@ -2932,4 +2932,70 @@ function PMA_getDbSpecificPrivsQueriesForChangeOrCopyUser($queries, $username, $ return $queries; } + +function PMA_addUser($_error, $real_sql_query, $sql_query, $username, $hostname) +{ + $common_functions = PMA_CommonFunctions::getInstance(); + + if ($_error || ! PMA_DBI_try_query($real_sql_query)) { + $_REQUEST['createdb-1'] = $_REQUEST['createdb-2'] = $_REQUEST['createdb-3'] = false; + $message = PMA_Message::rawError(PMA_DBI_getError()); + } else { + $message = PMA_Message::success(__('You have added a new user.')); + } + + if (isset($_REQUEST['createdb-1'])) { + // Create database with same name and grant all privileges + $q = 'CREATE DATABASE IF NOT EXISTS ' + . $common_functions->backquote($common_functions->sqlAddSlashes($username)) . ';'; + $sql_query .= $q; + if (! PMA_DBI_try_query($q)) { + $message = PMA_Message::rawError(PMA_DBI_getError()); + } + + /** + * If we are not in an Ajax request, we can't reload navigation now + */ + if ($GLOBALS['is_ajax_request'] != true) { + // this is needed in case tracking is on: + $GLOBALS['db'] = $username; + $GLOBALS['reload'] = true; + echo $common_functions->getReloadNavigationScript(); + } + + $q = 'GRANT ALL PRIVILEGES ON ' + . $common_functions->backquote( + $common_functions->escapeMysqlWildcards($common_functions->sqlAddSlashes($username)) + ) . '.* TO \'' + . $common_functions->sqlAddSlashes($username) + . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\';'; + $sql_query .= $q; + if (! PMA_DBI_try_query($q)) { + $message = PMA_Message::rawError(PMA_DBI_getError()); + } + } + + if (isset($_REQUEST['createdb-2'])) { + // Grant all privileges on wildcard name (username\_%) + $q = 'GRANT ALL PRIVILEGES ON ' + . $common_functions->backquote($common_functions->sqlAddSlashes($username) . '\_%') . '.* TO \'' + . $common_functions->sqlAddSlashes($username) . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\';'; + $sql_query .= $q; + if (! PMA_DBI_try_query($q)) { + $message = PMA_Message::rawError(PMA_DBI_getError()); + } + } + + if (isset($_REQUEST['createdb-3'])) { + // Grant all privileges on the specified database to the new user + $q = 'GRANT ALL PRIVILEGES ON ' + . $common_functions->backquote($common_functions->sqlAddSlashes($dbname)) . '.* TO \'' + . $common_functions->sqlAddSlashes($username) . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\';'; + $sql_query .= $q; + if (! PMA_DBI_try_query($q)) { + $message = PMA_Message::rawError(PMA_DBI_getError()); + } + } + return array($sql_query, $message); +} ?> diff --git a/server_privileges.php b/server_privileges.php index 71455f282d..8ba598e071 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -296,69 +296,15 @@ if (isset($_REQUEST['adduser_submit']) || isset($_REQUEST['change_copy'])) { } $sql_query = $create_user_show . $sql_query; } + list($sql_query, $message) = PMA_addUser($_error, $real_sql_query, + $sql_query, $username, $hostname + ); - if ($_error || ! PMA_DBI_try_query($real_sql_query)) { - $_REQUEST['createdb-1'] = $_REQUEST['createdb-2'] = $_REQUEST['createdb-3'] = false; - $message = PMA_Message::rawError(PMA_DBI_getError()); - } else { - $message = PMA_Message::success(__('You have added a new user.')); - } - - if (isset($_REQUEST['createdb-1'])) { - // Create database with same name and grant all privileges - $q = 'CREATE DATABASE IF NOT EXISTS ' - . $common_functions->backquote($common_functions->sqlAddSlashes($username)) . ';'; - $sql_query .= $q; - if (! PMA_DBI_try_query($q)) { - $message = PMA_Message::rawError(PMA_DBI_getError()); - } - - - /** - * If we are not in an Ajax request, we can't reload navigation now - */ - if ($GLOBALS['is_ajax_request'] != true) { - // this is needed in case tracking is on: - $GLOBALS['db'] = $username; - $GLOBALS['reload'] = true; - echo $common_functions->getReloadNavigationScript(); - } - - $q = 'GRANT ALL PRIVILEGES ON ' - . $common_functions->backquote($common_functions->escapeMysqlWildcards($common_functions->sqlAddSlashes($username))) . '.* TO \'' - . $common_functions->sqlAddSlashes($username) . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\';'; - $sql_query .= $q; - if (! PMA_DBI_try_query($q)) { - $message = PMA_Message::rawError(PMA_DBI_getError()); - } - } - - if (isset($_REQUEST['createdb-2'])) { - // Grant all privileges on wildcard name (username\_%) - $q = 'GRANT ALL PRIVILEGES ON ' - . $common_functions->backquote($common_functions->sqlAddSlashes($username) . '\_%') . '.* TO \'' - . $common_functions->sqlAddSlashes($username) . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\';'; - $sql_query .= $q; - if (! PMA_DBI_try_query($q)) { - $message = PMA_Message::rawError(PMA_DBI_getError()); - } - } - - if (isset($_REQUEST['createdb-3'])) { - // Grant all privileges on the specified database to the new user - $q = 'GRANT ALL PRIVILEGES ON ' - . $common_functions->backquote($common_functions->sqlAddSlashes($dbname)) . '.* TO \'' - . $common_functions->sqlAddSlashes($username) . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\';'; - $sql_query .= $q; - if (! PMA_DBI_try_query($q)) { - $message = PMA_Message::rawError(PMA_DBI_getError()); - } - } } else { if (isset($create_user_real)) { - $queries[] = $create_user_real; + $queries[] = $create_user_real; } - $queries[] = $real_sql_query; + $queries[] = $real_sql_query; // we put the query containing the hidden password in // $queries_for_display, at the same position occupied // by the real query in $queries From bb84591a1de1abefa3c2f1ecb6456fa31d31b5c1 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Fri, 13 Jul 2012 01:40:22 +0530 Subject: [PATCH 072/102] add doc comment and change function name --- libraries/server_privileges.lib.php | 13 ++++++++++++- server_privileges.php | 4 ++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 34ef4cb1c8..4289316657 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -2933,7 +2933,18 @@ function PMA_getDbSpecificPrivsQueriesForChangeOrCopyUser($queries, $username, $ return $queries; } -function PMA_addUser($_error, $real_sql_query, $sql_query, $username, $hostname) +/** + * Prepares queries for adding users and also create database and return query and message + * + * @param boolean $_error whether use create or not + * @param string $real_sql_query real sql query + * @param string $sql_query sql query + * @param string $username username + * @param string $hostname host name + * + * @return array $sql_query, $message + */ +function PMA_getQueryAndMessageForAddUser($_error, $real_sql_query, $sql_query, $username, $hostname) { $common_functions = PMA_CommonFunctions::getInstance(); diff --git a/server_privileges.php b/server_privileges.php index 8ba598e071..62f9eae851 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -296,8 +296,8 @@ if (isset($_REQUEST['adduser_submit']) || isset($_REQUEST['change_copy'])) { } $sql_query = $create_user_show . $sql_query; } - list($sql_query, $message) = PMA_addUser($_error, $real_sql_query, - $sql_query, $username, $hostname + list($sql_query, $message) = PMA_getQueryAndMessageForAddUser( + $_error, $real_sql_query, $sql_query, $username, $hostname ); } else { From 30cf9d5e3f63e6ad2e68e50bf1de56d7e196b94d Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Fri, 13 Jul 2012 02:01:43 +0530 Subject: [PATCH 073/102] remove invalid SQL query --- libraries/server_privileges.lib.php | 2 +- server_privileges.php | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 4289316657..d382d1d09e 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1185,7 +1185,7 @@ function PMA_getMessageAndSqlQueryForPrivilegesRevoke($db_and_table, $dbname, */ function PMA_getWithClauseForAddUserAndUpdatePrivs() { - $sql_query = 'WITH'; + $sql_query = ''; if (isset($_POST['Grant_priv']) && $_POST['Grant_priv'] == 'Y') { $sql_query .= ' GRANT OPTION'; } diff --git a/server_privileges.php b/server_privileges.php index 62f9eae851..cdab88f04b 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -278,8 +278,11 @@ if (isset($_REQUEST['adduser_submit']) || isset($_REQUEST['change_copy'])) { || (isset($max_questions) || isset($max_connections) || isset($max_updates) || isset($max_user_connections)) ) { - $real_sql_query .= PMA_getWithClauseForAddUserAndUpdatePrivs(); - $sql_query .= $real_sql_query; + $with_clause = PMA_getWithClauseForAddUserAndUpdatePrivs(); + } + if (!empty ($with_clause)) { + $real_sql_query .= 'WITH' . $with_clause; + $sql_query .= 'WITH' . $with_clause; } if (isset($create_user_real)) { $create_user_real .= ';'; From f0dbcd47334276a3c839045f18f53015d9f76058 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Fri, 13 Jul 2012 20:43:07 +0530 Subject: [PATCH 074/102] function name improvement and correct spelling mistake --- libraries/server_privileges.lib.php | 4 ++-- server_privileges.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index d382d1d09e..e26ec18085 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -2936,7 +2936,7 @@ function PMA_getDbSpecificPrivsQueriesForChangeOrCopyUser($queries, $username, $ /** * Prepares queries for adding users and also create database and return query and message * - * @param boolean $_error whether use create or not + * @param boolean $_error whether user create or not * @param string $real_sql_query real sql query * @param string $sql_query sql query * @param string $username username @@ -2944,7 +2944,7 @@ function PMA_getDbSpecificPrivsQueriesForChangeOrCopyUser($queries, $username, $ * * @return array $sql_query, $message */ -function PMA_getQueryAndMessageForAddUser($_error, $real_sql_query, $sql_query, $username, $hostname) +function PMA_addUserAndCreateDatabas($_error, $real_sql_query, $sql_query, $username, $hostname) { $common_functions = PMA_CommonFunctions::getInstance(); diff --git a/server_privileges.php b/server_privileges.php index cdab88f04b..a64eaa96cf 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -299,7 +299,7 @@ if (isset($_REQUEST['adduser_submit']) || isset($_REQUEST['change_copy'])) { } $sql_query = $create_user_show . $sql_query; } - list($sql_query, $message) = PMA_getQueryAndMessageForAddUser( + list($sql_query, $message) = PMA_addUserAndCreateDatabas( $_error, $real_sql_query, $sql_query, $username, $hostname ); From 800308a5f117381dee30ab7ca35557fb41937133 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Fri, 13 Jul 2012 21:10:09 +0530 Subject: [PATCH 075/102] improve PMA_getWithClauseForAddUserAndUpdatePrivs() function --- libraries/server_privileges.lib.php | 8 +++++++- server_privileges.php | 7 +++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index e26ec18085..c3afa08b50 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1185,27 +1185,33 @@ function PMA_getMessageAndSqlQueryForPrivilegesRevoke($db_and_table, $dbname, */ function PMA_getWithClauseForAddUserAndUpdatePrivs() { + $isWith = false; $sql_query = ''; if (isset($_POST['Grant_priv']) && $_POST['Grant_priv'] == 'Y') { $sql_query .= ' GRANT OPTION'; + $isWith = true; } if (isset($_POST['max_questions'])) { $max_questions = max(0, (int)$_POST['max_questions']); $sql_query .= ' MAX_QUERIES_PER_HOUR ' . $max_questions; + $isWith = true; } if (isset($_POST['max_connections'])) { $max_connections = max(0, (int)$_POST['max_connections']); $sql_query .= ' MAX_CONNECTIONS_PER_HOUR ' . $max_connections; + $isWith = true; } if (isset($_POST['max_updates'])) { $max_updates = max(0, (int)$_POST['max_updates']); $sql_query .= ' MAX_UPDATES_PER_HOUR ' . $max_updates; + $isWith = true; } if (isset($_POST['max_user_connections'])) { $max_user_connections = max(0, (int)$_POST['max_user_connections']); $sql_query .= ' MAX_USER_CONNECTIONS ' . $max_user_connections; + $isWith = true; } - return $sql_query; + return ($isWith ? 'WITH' . $sql_query : $sql_query); } /** diff --git a/server_privileges.php b/server_privileges.php index a64eaa96cf..51c375f5a1 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -279,11 +279,10 @@ if (isset($_REQUEST['adduser_submit']) || isset($_REQUEST['change_copy'])) { || isset($max_updates) || isset($max_user_connections)) ) { $with_clause = PMA_getWithClauseForAddUserAndUpdatePrivs(); + $real_sql_query .= $with_clause; + $sql_query .= $with_clause; } - if (!empty ($with_clause)) { - $real_sql_query .= 'WITH' . $with_clause; - $sql_query .= 'WITH' . $with_clause; - } + if (isset($create_user_real)) { $create_user_real .= ';'; $create_user_show .= ';'; From 630b50074a40a6919c9a4d0281bf3212c0b153da Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Fri, 13 Jul 2012 21:24:46 +0530 Subject: [PATCH 076/102] improve doc comment --- libraries/server_privileges.lib.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index c3afa08b50..25877b0675 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -2943,8 +2943,8 @@ function PMA_getDbSpecificPrivsQueriesForChangeOrCopyUser($queries, $username, $ * Prepares queries for adding users and also create database and return query and message * * @param boolean $_error whether user create or not - * @param string $real_sql_query real sql query - * @param string $sql_query sql query + * @param string $real_sql_query SQL query for add a user + * @param string $sql_query SQL query for display * @param string $username username * @param string $hostname host name * From c28619d85fe8cdfac8ec4699e3d34d0949a9df3d Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Fri, 13 Jul 2012 21:44:07 +0530 Subject: [PATCH 077/102] remove PHP notice --- libraries/server_privileges.lib.php | 12 +++--------- server_privileges.php | 6 ++++++ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 25877b0675..f5f4f4679a 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -2561,10 +2561,11 @@ function PMA_getAddUserHtmlFieldset($conditional_class) * Get HTML header for display User's properties * * @param boolean $dbname_is_wildcard whether database name is wildcard or not + * @param type $url_dbname url database name that urlencode() string * * @return string $html_output */ -function PMA_getHtmlHeaderForDisplayUserProperties($dbname_is_wildcard) +function PMA_getHtmlHeaderForDisplayUserProperties($dbname_is_wildcard, $url_dbname) { $html_output = '

' . "\n" . PMA_CommonFunctions::getInstance()->getIcon('b_usredit.png') @@ -2579,13 +2580,6 @@ function PMA_getHtmlHeaderForDisplayUserProperties($dbname_is_wildcard) . '&dbname=&tablename=">\'' . htmlspecialchars($_REQUEST['username']) . '\'@\'' . htmlspecialchars($_REQUEST['hostname']) . '\'' . "\n"; - - $url_dbname = urlencode( - str_replace( - array('\_', '\%'), - array('_', '%'), $_REQUEST['dbname'] - ) - ); $html_output .= ' - ' . ($dbname_is_wildcard ? __('Databases') : __('Database') ); if (isset($_REQUEST['tablename'])) { @@ -2739,7 +2733,7 @@ function PMA_getHtmlForDisplayUserProperties($dbname_is_wildcard,$url_dbname, } elseif (PMA_isValid($_REQUEST['tablename'])) { $tablename = $_REQUEST['tablename']; } - $html_output = PMA_getHtmlHeaderForDisplayUserProperties($dbname_is_wildcard); + $html_output = PMA_getHtmlHeaderForDisplayUserProperties($dbname_is_wildcard, $url_dbname); $sql = "SELECT '1' FROM `mysql`.`user`" . " WHERE `User` = '" . PMA_CommonFunctions::getInstance()->sqlAddSlashes($username) . "'" diff --git a/server_privileges.php b/server_privileges.php index 51c375f5a1..2581d9aa4d 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -506,6 +506,12 @@ if (empty($_REQUEST['adduser']) && (! isset($checkprivs) || ! strlen($checkprivs if ($GLOBALS['is_ajax_request'] == true) { header('Cache-Control: no-cache'); } + $url_dbname = urlencode( + str_replace( + array('\_', '\%'), + array('_', '%'), $_REQUEST['dbname'] + ) + ); $response->addHTML( PMA_getHtmlForDisplayUserProperties($dbname_is_wildcard,$url_dbname, $random_n, $username, $hostname, $link_edit, $link_revoke From e00cf973c001854002e0a40b125ef83a850fe6a6 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Fri, 13 Jul 2012 22:02:38 +0530 Subject: [PATCH 078/102] remove unwanted parameter from PMA_getHtmlForNotAttachedPrivilegesToTableSpecificColumn() --- libraries/server_privileges.lib.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index f5f4f4679a..6b6c981930 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -560,7 +560,7 @@ function PMA_getHtmlForTableSpecificPrivileges($username, $hostname, $db, $table // privs that are not attached to a specific column $html_output .= '
' . "\n" - . PMA_getHtmlForNotAttachedPrivilegesToTableSpecificColumn($row, $grant_type) + . PMA_getHtmlForNotAttachedPrivilegesToTableSpecificColumn($row) . '
' . "\n"; // for Safari 2.0.2 @@ -605,11 +605,10 @@ function PMA_getHtmlForAttachedPrivilegesToTableSpecificColumn($columns, $row) * Get HTML for privileges that are not attached to a specific column * * @param array $row first row from result or boolean false - * @param array $grant_type privilrge type * * @return string $html_output */ -function PMA_getHtmlForNotAttachedPrivilegesToTableSpecificColumn($row, $grant_type) +function PMA_getHtmlForNotAttachedPrivilegesToTableSpecificColumn($row) { $html_output = ''; foreach ($row as $current_grant => $current_grant_value) { From 57cc90e00e1c4606fb2f1567f253a548d896aebe Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Fri, 13 Jul 2012 23:04:39 +0530 Subject: [PATCH 079/102] correct wrong variable declaration --- libraries/server_privileges.lib.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 6b6c981930..f7b2e65eeb 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1852,10 +1852,11 @@ function PMA_getHtmlForDisplayUserRightsInRows($db_rights, $link_edit, } elseif (PMA_isValid($_REQUEST['dbname'])) { $dbname = $_REQUEST['dbname']; } + $html_output = ''; $found_rows = array(); // display rows if (count($db_rights) < 1) { - $html_output = '' . "\n" + $html_output .= '' . "\n" . '
' . __('None') . '
' . "\n" . '' . "\n"; } else { @@ -1864,7 +1865,7 @@ function PMA_getHtmlForDisplayUserRightsInRows($db_rights, $link_edit, foreach ($db_rights as $row) { $found_rows[] = (! isset($dbname)) ? $row['Db'] : $row['Table_name']; - $html_output = '' . "\n" + $html_output .= '' . "\n" . '' . htmlspecialchars((! isset($dbname)) ? $row['Db'] From 8ee8a5ffbce0da41007f9042111d15d34c83d359 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Sat, 14 Jul 2012 17:18:28 +0530 Subject: [PATCH 080/102] spelling mistakes --- libraries/server_privileges.lib.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index f7b2e65eeb..06dd2ae593 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -2938,13 +2938,13 @@ function PMA_getDbSpecificPrivsQueriesForChangeOrCopyUser($queries, $username, $ * * @param boolean $_error whether user create or not * @param string $real_sql_query SQL query for add a user - * @param string $sql_query SQL query for display + * @param string $sql_query SQL query to be displayed * @param string $username username * @param string $hostname host name * * @return array $sql_query, $message */ -function PMA_addUserAndCreateDatabas($_error, $real_sql_query, $sql_query, $username, $hostname) +function PMA_addUserAndCreateDatabase($_error, $real_sql_query, $sql_query, $username, $hostname) { $common_functions = PMA_CommonFunctions::getInstance(); From 9aa636b766fc0fca0f56f4c08f4c90f848a4f32b Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Sat, 14 Jul 2012 17:23:14 +0530 Subject: [PATCH 081/102] improve PMA_getWithClauseForAddUserAndUpdatePrivs() --- libraries/server_privileges.lib.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 06dd2ae593..0b75df714d 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1184,33 +1184,27 @@ function PMA_getMessageAndSqlQueryForPrivilegesRevoke($db_and_table, $dbname, */ function PMA_getWithClauseForAddUserAndUpdatePrivs() { - $isWith = false; $sql_query = ''; if (isset($_POST['Grant_priv']) && $_POST['Grant_priv'] == 'Y') { $sql_query .= ' GRANT OPTION'; - $isWith = true; } if (isset($_POST['max_questions'])) { $max_questions = max(0, (int)$_POST['max_questions']); $sql_query .= ' MAX_QUERIES_PER_HOUR ' . $max_questions; - $isWith = true; } if (isset($_POST['max_connections'])) { $max_connections = max(0, (int)$_POST['max_connections']); $sql_query .= ' MAX_CONNECTIONS_PER_HOUR ' . $max_connections; - $isWith = true; } if (isset($_POST['max_updates'])) { $max_updates = max(0, (int)$_POST['max_updates']); $sql_query .= ' MAX_UPDATES_PER_HOUR ' . $max_updates; - $isWith = true; } if (isset($_POST['max_user_connections'])) { $max_user_connections = max(0, (int)$_POST['max_user_connections']); $sql_query .= ' MAX_USER_CONNECTIONS ' . $max_user_connections; - $isWith = true; } - return ($isWith ? 'WITH' . $sql_query : $sql_query); + return ((!empty($sql_query)) ? 'WITH' . $sql_query : $sql_query); } /** From a4e4544f3397301007f5fef89410490f64018ff9 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Sun, 15 Jul 2012 21:38:14 +0530 Subject: [PATCH 082/102] remove unwanted function parameter --- libraries/server_privileges.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 0b75df714d..8bde755829 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -2774,7 +2774,7 @@ function PMA_getHtmlForDisplayUserProperties($dbname_is_wildcard,$url_dbname, $html_output .= '
' . "\n"; list($html_rightsTable, $found_rows) = PMA_getTableForDisplayAllTableSpecificRights( - $username, $hostname, $dbname, $link_edit, $link_revoke + $username, $hostname, $link_edit, $link_revoke ); $html_output .= $html_rightsTable; From 89c68ed98a34e8b147f3dc7e25f92abd185d85b9 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Sun, 15 Jul 2012 21:40:42 +0530 Subject: [PATCH 083/102] improve PMA_getWithClauseForAddUserAndUpdatePrivs() --- libraries/server_privileges.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 8bde755829..38d7b749e2 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1204,7 +1204,7 @@ function PMA_getWithClauseForAddUserAndUpdatePrivs() $max_user_connections = max(0, (int)$_POST['max_user_connections']); $sql_query .= ' MAX_USER_CONNECTIONS ' . $max_user_connections; } - return ((!empty($sql_query)) ? 'WITH' . $sql_query : $sql_query); + return ((!empty($sql_query)) ? 'WITH' . $sql_query : ''); } /** From 42860cf4f0b1c4c0b8bf7a67b4e3a00b11daff11 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Mon, 16 Jul 2012 02:21:09 +0530 Subject: [PATCH 084/102] change some functions signatures --- libraries/server_privileges.lib.php | 107 +++++++++------------------- server_privileges.php | 16 ++++- 2 files changed, 48 insertions(+), 75 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 38d7b749e2..215dab14e2 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1155,7 +1155,7 @@ function PMA_getMessageForUpdatePassword($pma_pw, $pma_pw2, $err_url, $username, function PMA_getMessageAndSqlQueryForPrivilegesRevoke($db_and_table, $dbname, $tablename, $username, $hostname ) { - $db_and_table = PMA_wildcardEscapeForGrant($dbname, isset($tablename) ? $tablename : ''); + $db_and_table = PMA_wildcardEscapeForGrant($dbname, $tablename); $sql_query0 = 'REVOKE ALL PRIVILEGES ON ' . $db_and_table . ' FROM \'' . PMA_CommonFunctions::getInstance()->sqlAddSlashes($username) . '\'@\'' @@ -1710,7 +1710,7 @@ function PMA_getLinkToDbAndTable($url_dbname, $dbname, $tablename) . PMA_CommonFunctions::getInstance()->getTitleForTarget($GLOBALS['cfg']['DefaultTabDatabase']) . " ]\n"; - if (isset($tablename)) { + if (strlen($tablename)) { $html_output .= ' [ ' . __('Table') . ' ' . "\n" . '' - . htmlspecialchars((! isset($dbname)) + . htmlspecialchars((! strlen($dbname)) ? $row['Db'] : $row['Table_name']) . '' . "\n" @@ -1869,8 +1859,8 @@ function PMA_getHtmlForDisplayUserRightsInRows($db_rights, $link_edit, . ' ' . join(',' . "\n" . ' ', PMA_extractPrivInfo($row, true)) . "\n" . '' . "\n" . '' - . ((((! isset($dbname)) && $row['Grant_priv'] == 'Y') - || (isset($dbname) && in_array('Grant', explode(',', $row['Table_priv'])))) + . ((((! strlen($dbname)) && $row['Grant_priv'] == 'Y') + || (strlen($dbname) && in_array('Grant', explode(',', $row['Table_priv'])))) ? __('Yes') : __('No')) . '' . "\n" @@ -1886,8 +1876,8 @@ function PMA_getHtmlForDisplayUserRightsInRows($db_rights, $link_edit, $link_edit, htmlspecialchars(urlencode($username)), urlencode(htmlspecialchars($hostname)), - urlencode((! isset($dbname)) ? $row['Db'] : htmlspecialchars($dbname)), - urlencode((! isset($dbname)) ? '' : $row['Table_name']) + urlencode((! strlen($dbname)) ? $row['Db'] : htmlspecialchars($dbname)), + urlencode((! strlen($dbname)) ? '' : $row['Table_name']) ); $html_output .= '' . "\n" . ' '; @@ -1899,8 +1889,8 @@ function PMA_getHtmlForDisplayUserRightsInRows($db_rights, $link_edit, $link_revoke, htmlspecialchars(urlencode($username)), urlencode(htmlspecialchars($hostname)), - urlencode((! isset($dbname)) ? $row['Db'] : htmlspecialchars($dbname)), - urlencode((! isset($dbname)) ? '' : $row['Table_name']) + urlencode((! strlen($dbname)) ? $row['Db'] : htmlspecialchars($dbname)), + urlencode((! strlen($dbname)) ? '' : $row['Table_name']) ); } $html_output .= '' . "\n" @@ -1923,28 +1913,23 @@ function PMA_getHtmlForDisplayUserRightsInRows($db_rights, $link_edit, * @return array $html_output, $found_rows */ function PMA_getTableForDisplayAllTableSpecificRights($username, $hostname - , $link_edit, $link_revoke + , $link_edit, $link_revoke, $dbname ) { - if (PMA_isValid($_REQUEST['pred_dbname'])) { - $dbname = $_REQUEST['pred_dbname']; - } elseif (PMA_isValid($_REQUEST['dbname'])) { - $dbname = $_REQUEST['dbname']; - } // table header $html_output = PMA_generate_common_hidden_inputs('', '') . '' . "\n" . '' . "\n" . '
' . "\n" . '' - . (! isset($dbname) ? __('Database-specific privileges') : __('Table-specific privileges')) + . (! strlen($dbname) ? __('Database-specific privileges') : __('Table-specific privileges')) . '' . "\n" . '' . "\n" . '' . "\n" - . '' . "\n" + . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" @@ -1965,14 +1950,14 @@ function PMA_getTableForDisplayAllTableSpecificRights($username, $hostname * no db name given, so we want all privs for the given user * db name was given, so we want all user specific rights for this db */ - $db_rights = PMA_getUserSpecificRights($tables, $user_host_condition); + $db_rights = PMA_getUserSpecificRights($tables, $user_host_condition, $dbname); ksort($db_rights); $html_output .= '' . "\n"; // display rows list ($found_rows, $html_out) = PMA_getHtmlForDisplayUserRightsInRows( - $db_rights, $link_edit, $link_revoke, $hostname, $username + $db_rights, $link_edit, $dbname, $link_revoke, $hostname, $username ); $html_output .= $html_out; @@ -2417,23 +2402,11 @@ function PMA_deleteUser($queries) * * @return PMA_message success message or error message for update */ -function PMA_updatePrivileges($username, $hostname) +function PMA_updatePrivileges($username, $hostname, $tablename, $dbname) { $common_functions = PMA_CommonFunctions::getInstance(); - - if (PMA_isValid($_REQUEST['pred_tablename'])) { - $tablename = $_REQUEST['pred_tablename']; - } elseif (PMA_isValid($_REQUEST['tablename'])) { - $tablename = $_REQUEST['tablename']; - } - if (PMA_isValid($_REQUEST['pred_dbname'])) { - $dbname = $_REQUEST['pred_dbname']; - } elseif (PMA_isValid($_REQUEST['dbname'])) { - $dbname = $_REQUEST['dbname']; - } - $db_and_table = PMA_wildcardEscapeForGrant( - $dbname, (isset($tablename) ? $tablename : '') - ); + + $db_and_table = PMA_wildcardEscapeForGrant($dbname, $tablename); $sql_query0 = 'REVOKE ALL PRIVILEGES ON ' . $db_and_table . ' FROM \'' . $common_functions->sqlAddSlashes($username) @@ -2449,14 +2422,14 @@ function PMA_updatePrivileges($username, $hostname) // Should not do a GRANT USAGE for a table-specific privilege, it // causes problems later (cannot revoke it) - if (! (isset($tablename) && 'USAGE' == implode('', PMA_extractPrivInfo()))) { + if (! (strlen($tablename) && 'USAGE' == implode('', PMA_extractPrivInfo()))) { $sql_query2 = 'GRANT ' . join(', ', PMA_extractPrivInfo()) . ' ON ' . $db_and_table . ' TO \'' . $common_functions->sqlAddSlashes($username) . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\''; if ((isset($_POST['Grant_priv']) && $_POST['Grant_priv'] == 'Y') - || (! isset($dbname) + || (! strlen($dbname) && (isset($_POST['max_questions']) || isset($_POST['max_connections']) || isset($_POST['max_updates']) || isset($_POST['max_user_connections']))) ) { @@ -2715,18 +2688,8 @@ function PMA_getHtmlForDisplayUserOverviewPage($link_edit, $pmaThemeImage, * @return string $html_output */ function PMA_getHtmlForDisplayUserProperties($dbname_is_wildcard,$url_dbname, - $random_n, $username, $hostname, $link_edit, $link_revoke + $random_n, $username, $hostname, $link_edit, $link_revoke, $dbname, $tablename ) { - if (PMA_isValid($_REQUEST['pred_dbname'])) { - $dbname = $_REQUEST['pred_dbname']; - } elseif (PMA_isValid($_REQUEST['dbname'])) { - $dbname = $_REQUEST['dbname']; - } - if (PMA_isValid($_REQUEST['pred_tablename'])) { - $tablename = $_REQUEST['pred_tablename']; - } elseif (PMA_isValid($_REQUEST['tablename'])) { - $tablename = $_REQUEST['tablename']; - } $html_output = PMA_getHtmlHeaderForDisplayUserProperties($dbname_is_wildcard, $url_dbname); $sql = "SELECT '1' FROM `mysql`.`user`" @@ -2750,9 +2713,9 @@ function PMA_getHtmlForDisplayUserProperties($dbname_is_wildcard,$url_dbname, 'username' => $username, 'hostname' => $hostname, ); - if (isset($dbname)) { + if (strlen($dbname)) { $_params['dbname'] = $dbname; - if (isset($tablename)) { + if (strlen($tablename)) { $_params['tablename'] = $tablename; } } @@ -2766,7 +2729,7 @@ function PMA_getHtmlForDisplayUserProperties($dbname_is_wildcard,$url_dbname, $html_output .= '' . "\n"; - if (! isset($tablename) && empty($dbname_is_wildcard)) { + if (! strlen($tablename) && empty($dbname_is_wildcard)) { // no table name was given, display all table specific rights // but only if $dbname contains no wildcards @@ -2774,11 +2737,11 @@ function PMA_getHtmlForDisplayUserProperties($dbname_is_wildcard,$url_dbname, $html_output .= '' . "\n"; list($html_rightsTable, $found_rows) = PMA_getTableForDisplayAllTableSpecificRights( - $username, $hostname, $link_edit, $link_revoke + $username, $hostname, $link_edit, $link_revoke, $dbname ); $html_output .= $html_rightsTable; - if (! isset($dbname)) { + if (! strlen($dbname)) { // no database name was given, display select db $html_output .= PMA_getHTmlForDisplaySelectDbInEditPrivs($found_rows); @@ -2794,12 +2757,12 @@ function PMA_getHtmlForDisplayUserProperties($dbname_is_wildcard,$url_dbname, } // Provide a line with links to the relevant database and table - if (isset($dbname) && empty($dbname_is_wildcard)) { + if (strlen($dbname) && empty($dbname_is_wildcard)) { $html_output .= PMA_getLinkToDbAndTable($url_dbname, $dbname, $tablename); } - if (! isset($dbname) && ! $user_does_not_exists) { + if (! strlen($dbname) && ! $user_does_not_exists) { //change login information include_once 'libraries/display_change_password.lib.php'; $html_output .= PMA_getChangeLoginInformationHtmlForm($username, $hostname); diff --git a/server_privileges.php b/server_privileges.php index 2581d9aa4d..386b444e69 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -333,7 +333,12 @@ if (isset($_REQUEST['change_copy'])) { * Updates privileges */ if (! empty($update_privs)) { - list($sql_query, $message) = PMA_updatePrivileges($username, $hostname); + list($sql_query, $message) = PMA_updatePrivileges( + $username, + $hostname, + (isset($tablename) ? $tablename : ''), + (isset($dbname) ? $dbname : '') + ); } /** @@ -341,7 +346,10 @@ if (! empty($update_privs)) { */ if (isset($_REQUEST['revokeall'])) { list ($message, $sql_query) = PMA_getMessageAndSqlQueryForPrivilegesRevoke( - $db_and_table, $dbname, $tablename, $username, $hostname + $db_and_table, + (isset($dbename) ? $dbname : ''), + (isset($tablename) ? $tablename : ''), + $username, $hostname ); } @@ -514,7 +522,9 @@ if (empty($_REQUEST['adduser']) && (! isset($checkprivs) || ! strlen($checkprivs ); $response->addHTML( PMA_getHtmlForDisplayUserProperties($dbname_is_wildcard,$url_dbname, - $random_n, $username, $hostname, $link_edit, $link_revoke + $random_n, $username, $hostname, $link_edit, $link_revoke, + (isset($dbename) ? $dbname : ''), + (isset($tablename) ? $tablename : '') ) ); } From 45f35c6906ae9f564c19e81bd078ccbcfb3f31d8 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Mon, 16 Jul 2012 02:21:49 +0530 Subject: [PATCH 085/102] remove a php notice --- libraries/server_privileges.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 215dab14e2..ed737b5d8a 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1340,7 +1340,7 @@ function PMA_getHtmlForSpecificDbPrivileges($dbToCheck, $link_edit, $conditional . ' ' . sprintf( __('Users having access to "%s"'), - '' . htmlspecialchars($checkprivs) . '' + '' . htmlspecialchars($dbToCheck) . '' ) . "\n" . '' . "\n"; From d8233fcbb96ab2b897ba87a6ccc4cbbf2576d44e Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Mon, 16 Jul 2012 18:00:39 +0530 Subject: [PATCH 086/102] improve coding style --- libraries/server_privileges.lib.php | 166 ++++++++++++++++++++-------- 1 file changed, 117 insertions(+), 49 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index ed737b5d8a..6c71446ed1 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1119,14 +1119,20 @@ function PMA_getMessageForUpdatePassword($pma_pw, $pma_pw2, $err_url, $username, if (empty($message)) { $common_functions = PMA_CommonFunctions::getInstance(); - $hashing_function = (! empty($_REQUEST['pw_hash']) && $_REQUEST['pw_hash'] == 'old' ? 'OLD_' : '') - . 'PASSWORD'; + $hashing_function = + (! empty($_REQUEST['pw_hash']) && $_REQUEST['pw_hash'] == 'old' + ? 'OLD_' + : '' + ) + . 'PASSWORD'; // in $sql_query which will be displayed, hide the password $sql_query = 'SET PASSWORD FOR \'' . $common_functions->sqlAddSlashes($username) . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\' = ' - . (($pma_pw == '') ? '\'\'' : $hashing_function . '(\'' . preg_replace('@.@s', '*', $pma_pw) . '\')'); + . (($pma_pw == '') + ? '\'\'' + : $hashing_function . '(\'' . preg_replace('@.@s', '*', $pma_pw) . '\')'); $local_query = 'SET PASSWORD FOR \'' . $common_functions->sqlAddSlashes($username) @@ -1137,7 +1143,9 @@ function PMA_getMessageForUpdatePassword($pma_pw, $pma_pw2, $err_url, $username, PMA_DBI_try_query($local_query) or $common_functions->mysqlDie(PMA_DBI_getError(), $sql_query, false, $err_url); $message = PMA_Message::success(__('The password for %s was changed successfully.')); - $message->addParam('\'' . htmlspecialchars($username) . '\'@\'' . htmlspecialchars($hostname) . '\''); + $message->addParam( + '\'' . htmlspecialchars($username) . '\'@\'' . htmlspecialchars($hostname) . '\'' + ); } return $message; } @@ -1158,7 +1166,8 @@ function PMA_getMessageAndSqlQueryForPrivilegesRevoke($db_and_table, $dbname, $db_and_table = PMA_wildcardEscapeForGrant($dbname, $tablename); $sql_query0 = 'REVOKE ALL PRIVILEGES ON ' . $db_and_table - . ' FROM \'' . PMA_CommonFunctions::getInstance()->sqlAddSlashes($username) . '\'@\'' + . ' FROM \'' + . PMA_CommonFunctions::getInstance()->sqlAddSlashes($username) . '\'@\'' . PMA_CommonFunctions::getInstance()->sqlAddSlashes($hostname) . '\';'; $sql_query1 = 'REVOKE GRANT OPTION ON ' . $db_and_table @@ -1172,7 +1181,9 @@ function PMA_getMessageAndSqlQueryForPrivilegesRevoke($db_and_table, $dbname, } $sql_query = $sql_query0 . ' ' . $sql_query1; $message = PMA_Message::success(__('You have revoked the privileges for %s')); - $message->addParam('\'' . htmlspecialchars($username) . '\'@\'' . htmlspecialchars($hostname) . '\''); + $message->addParam( + '\'' . htmlspecialchars($username) . '\'@\'' . htmlspecialchars($hostname) . '\'' + ); return array($message, $sql_query); } @@ -1223,7 +1234,8 @@ function PMA_getHtmlForAddUser($random_n, $dbname) $html_output = '

' . "\n" . $common_functions->getIcon('b_usradd.png') . __('Add user') . "\n" . '

' . "\n" - . '' . "\n" + . '' . "\n" . PMA_generate_common_hidden_inputs('', '') . PMA_getHtmlForDisplayLoginInformationFields('new'); @@ -1340,7 +1352,8 @@ function PMA_getHtmlForSpecificDbPrivileges($dbToCheck, $link_edit, $conditional . ' ' . sprintf( __('Users having access to "%s"'), - '' . htmlspecialchars($dbToCheck) . '' + '' + . htmlspecialchars($dbToCheck) . '' ) . "\n" . '' . "\n"; @@ -1456,17 +1469,21 @@ function PMA_getHtmlTableBodyForSpecificDbPrivs($found, $row, $odd_row, $link_ed . ' '; if (! isset($current['Db']) || $current['Db'] == '*') { $html_output .= __('global'); - } elseif ($current['Db'] == PMA_CommonFunctions::getInstance()->escapeMysqlWildcards($dbToCheck)) { + } elseif ( + $current['Db'] == PMA_CommonFunctions::getInstance()->escapeMysqlWildcards($dbToCheck) + ) { $html_output .= __('database-specific'); } else { - $html_output .= __('wildcard'). ': ' . htmlspecialchars($current['Db']) . ''; + $html_output .= __('wildcard'). ': ' + . '' . htmlspecialchars($current['Db']) . ''; } $html_output .= "\n" . '' . "\n"; $html_output .='
' . "\n"; @@ -1478,7 +1495,9 @@ function PMA_getHtmlTableBodyForSpecificDbPrivs($found, $row, $odd_row, $link_ed $link_edit, urlencode($current_user), urlencode($current_host), - urlencode(! isset($current['Db']) || $current['Db'] == '*' ? '' : $current['Db']), + urlencode( + ! isset($current['Db']) || $current['Db'] == '*' ? '' : $current['Db'] + ), '' ); $html_output .= '' . "\n" @@ -1517,7 +1536,8 @@ function PMA_getStandardLinks($conditional_class) $common_functions = PMA_CommonFunctions::getInstance(); $link_edit = ''; $link_revoke = ''; $link_export = ''; $link_export_all = ' ' + . htmlspecialchars($_REQUEST['username']) + . '&#27;' . htmlspecialchars($_REQUEST['hostname']) . '" />' . '' . "\n" . ''."\n"; @@ -1635,7 +1661,8 @@ function PMA_getExtraDataForAjaxBehavior( $password, $sql_query,$link_edit, */ $new_user_initial = strtoupper(substr($_REQUEST['username'], 0, 1)); $new_user_initial_string = '' . $new_user_initial . ''; + . $GLOBALS['url_query'] . '&initial=' . $new_user_initial .'">' + . $new_user_initial . ''; $extra_data['new_user_initial'] = $new_user_initial; $extra_data['new_user_initial_string'] = $new_user_initial_string; } @@ -1668,16 +1695,22 @@ function PMA_getChangeLoginInformationHtmlForm($username, $hostname) '2' => __('... revoke all active privileges from the old one and delete it afterwards.'), '3' => __('... delete the old one from the user tables and reload the privileges afterwards.')); - $html_output = '' . "\n" + $html_output = '' . "\n" . PMA_generate_common_hidden_inputs('', '') - . '' . "\n" - . '' . "\n" + . '' . "\n" + . '' . "\n" . '
' . "\n" - . '' . __('Change Login Information / Copy User') . '' . "\n" + . '' . __('Change Login Information / Copy User') + . '' . "\n" . PMA_getHtmlForDisplayLoginInformationFields('change'); $html_output .= '
' . "\n" - . ' ' . __('Create a new user with the same privileges and ...') . '' . "\n"; + . ' ' + . __('Create a new user with the same privileges and ...') + . '' . "\n"; $html_output .= PMA_CommonFunctions::getInstance()->getRadioFields( 'mode', $choices, '4', true ); @@ -1707,13 +1740,16 @@ function PMA_getLinkToDbAndTable($url_dbname, $dbname, $tablename) . ' ' . htmlspecialchars($dbname) . ': ' - . PMA_CommonFunctions::getInstance()->getTitleForTarget($GLOBALS['cfg']['DefaultTabDatabase']) + . PMA_CommonFunctions::getInstance()->getTitleForTarget( + $GLOBALS['cfg']['DefaultTabDatabase'] + ) . " ]\n"; if (strlen($tablename)) { $html_output .= ' [ ' . __('Table') . ' ' . htmlspecialchars($tablename) . ': ' . PMA_CommonFunctions::getInstance()->getTitleForTarget( $GLOBALS['cfg']['DefaultTabTable'] @@ -1746,7 +1782,8 @@ function PMA_getUserSpecificRights($tables, $user_host_condition, $dbname) } else { $user_host_condition .= ' AND `Db`' - .' LIKE \'' . $common_functions->sqlAddSlashes($dbname, true) . "'"; + .' LIKE \'' + . $common_functions->sqlAddSlashes($dbname, true) . "'"; $tables_to_search_for_users = array('columns_priv',); $dbOrTableName = 'Table_name'; } @@ -1792,7 +1829,8 @@ function PMA_getUserSpecificRights($tables, $user_host_condition, $dbname) PMA_DBI_free_result($db_rights_result); if (! strlen($dbname)) { - $sql_query = 'SELECT * FROM `mysql`.`db`' . $user_host_condition . ' ORDER BY `Db` ASC'; + $sql_query = 'SELECT * FROM `mysql`.`db`' + . $user_host_condition . ' ORDER BY `Db` ASC'; } else { $sql_query = 'SELECT `Table_name`,' .' `Table_priv`,' @@ -1808,7 +1846,8 @@ function PMA_getUserSpecificRights($tables, $user_host_condition, $dbname) while ($row = PMA_DBI_fetch_assoc($result)) { if (isset($db_rights[$row[$dbOrTableName]])) { - $db_rights[$row[$dbOrTableName]] = array_merge($db_rights[$row[$dbOrTableName]], $row); + $db_rights[$row[$dbOrTableName]] + = array_merge($db_rights[$row[$dbOrTableName]], $row); } else { $db_rights[$row[$dbOrTableName]] = $row; } @@ -1856,7 +1895,8 @@ function PMA_getHtmlForDisplayUserRightsInRows($db_rights, $link_edit, $dbname, : $row['Table_name']) . '' . "\n" . '
' . "\n" . '
' . (! isset($dbname) ? __('Database') : __('Table')) . '
' . (! strlen($dbname) ? __('Database') : __('Table')) . '' . __('Privileges') . '' . __('Grant') . '' - . (! isset($dbname) ? __('Table-specific privileges') : __('Column-specific privileges')) + . (! strlen($dbname) ? __('Table-specific privileges') : __('Column-specific privileges')) . '' . __('Action') . '
' . "\n" . '' . "\n" - . '' . join(',' . "\n" . ' ', PMA_extractPrivInfo($current, true)) . "\n" + . '' + . join(',' . "\n" . ' ', PMA_extractPrivInfo($current, true)) . "\n" . '' . "\n" . '' . "\n" - . ' ' . join(',' . "\n" . ' ', PMA_extractPrivInfo($row, true)) . "\n" + . ' ' + . join(',' . "\n" . ' ', PMA_extractPrivInfo($row, true)) . "\n" . '' . ((((! strlen($dbname)) && $row['Grant_priv'] == 'Y') @@ -1917,19 +1957,29 @@ function PMA_getTableForDisplayAllTableSpecificRights($username, $hostname ) { // table header $html_output = PMA_generate_common_hidden_inputs('', '') - . '' . "\n" - . '' . "\n" + . '' . "\n" + . '' . "\n" . '
' . "\n" . '' - . (! strlen($dbname) ? __('Database-specific privileges') : __('Table-specific privileges')) + . (! strlen($dbname) + ? __('Database-specific privileges') + : __('Table-specific privileges') + ) . '' . "\n" . '' . "\n" . '' . "\n" - . '' . "\n" + . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" @@ -2015,8 +2065,10 @@ function PMA_displayTablesInEditPrivs($dbname, $found_rows) { $common_functions = PMA_CommonFunctions::getInstance(); - $html_output = '' . "\n" - . '' . "\n"; + $html_output = '' . "\n" + $html_output .= '' . "\n"; $result = @PMA_DBI_try_query( 'SHOW TABLES FROM ' . $common_functions->backquote( @@ -2113,11 +2165,15 @@ function PMA_getUsersOverview($result, $db_rights, $link_edit, $pmaThemeImage, 'submit_mult', 'mult_submit', 'submit_mult_export', __('Export'), 'b_tblexport.png', 'export' ); - $html_output .= ''; + $html_output .= ''; $html_output .= '' . '
' . '
' - . sprintf($link_export_all, urlencode('%'), urlencode('%'), (isset($_GET['initial']) ? $_GET['initial'] : '')); + . sprintf($link_export_all, + urlencode('%'), urlencode('%'), + (isset($_GET['initial']) ? $_GET['initial'] : '') + ); $html_output .= '
' . '' . '
'; @@ -2151,7 +2207,9 @@ function PMA_getTableBodyForUserRightsTable($db_rights, $link_edit, $link_export foreach ($user as $host) { $index_checkbox++; $html_output .= '' . "\n"; - $html_output .= '' . "\n" - . '' . "\n" + . '' . "\n" . '' . "\n"; if ($found) { while (true) { @@ -1589,7 +1595,10 @@ function PMA_getHtmlTableBodyForSpecificDbPrivs($found, $row, $odd_row, $link_ed $current_privileges = array(); $current_user = $row['User']; $current_host = $row['Host']; - while ($row && $current_user == $row['User'] && $current_host == $row['Host']) { + while ($row + && $current_user == $row['User'] + && $current_host == $row['Host'] + ) { $current_privileges[] = $row; $row = PMA_DBI_fetch_assoc($res); } @@ -2286,7 +2295,9 @@ function PMA_getUsersOverview($result, $db_rights, $link_edit, $pmaThemeImage, . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" @@ -2294,7 +2305,9 @@ function PMA_getUsersOverview($result, $db_rights, $link_edit, $pmaThemeImage, . '' . "\n"; $html_output .= '' . "\n"; - $html_output .= PMA_getTableBodyForUserRightsTable($db_rights, $link_edit, $link_export); + $html_output .= PMA_getTableBodyForUserRightsTable( + $db_rights, $link_edit, $link_export + ); $html_output .= '' . '
' . (! strlen($dbname) ? __('Database') : __('Table')) . '
' + . (! strlen($dbname) ? __('Database') : __('Table')) + . '' . __('Privileges') . '' . __('Grant') . '' - . (! strlen($dbname) ? __('Table-specific privileges') : __('Column-specific privileges')) + . (! strlen($dbname) + ? __('Table-specific privileges') + : __('Column-specific privileges') + ) . '' . __('Action') . '
' . ($host['Grant_priv'] == 'Y' ? __('Yes') : __('No')) . '' + . ($host['Grant_priv'] == 'Y' ? __('Yes') : __('No')) + . '' . sprintf($link_edit, urlencode($host['User']), urlencode($host['Host']), '', '' @@ -2496,7 +2556,8 @@ function PMA_getHtmlForExportUserDefinition($username, $hostname) } } else { // export privileges for a single user - $title = __('User') . ' `' . htmlspecialchars($username) . '`@`' . htmlspecialchars($hostname) . '`'; + $title = __('User') . ' `' . htmlspecialchars($username) + . '`@`' . htmlspecialchars($hostname) . '`'; $export .= PMA_getGrants($username, $hostname); } // remove trailing whitespace @@ -2645,10 +2706,14 @@ function PMA_getHtmlForDisplayUserOverviewPage($link_edit, $pmaThemeImage, * Display the user overview * (if less than 50 users, display them immediately) */ - if (isset($_REQUEST['initial']) || isset($_REQUEST['showall']) || PMA_DBI_num_rows($res) < 50) { - $html_output .= PMA_getUsersOverview($res, $db_rights, $link_edit,$pmaThemeImage, - $text_dir, $conditional_class, $link_export, $link_export_all - ); + if (isset($_REQUEST['initial']) + || isset($_REQUEST['showall']) + || PMA_DBI_num_rows($res) < 50 + ) { + $html_output .= PMA_getUsersOverview($res, $db_rights, + $link_edit,$pmaThemeImage, $text_dir, $conditional_class, + $link_export, $link_export_all + ); } else { $html_output .= PMA_getAddUserHtmlFieldset($conditional_class); } // end if (display overview) @@ -2734,7 +2799,8 @@ function PMA_getHtmlForDisplayUserProperties($dbname_is_wildcard,$url_dbname, // no table name was given, display all table specific rights // but only if $dbname contains no wildcards - $html_output .= '' . "\n"; + $html_output .= '' . "\n"; list($html_rightsTable, $found_rows) = PMA_getTableForDisplayAllTableSpecificRights( $username, $hostname, $link_edit, $link_revoke, $dbname @@ -2947,7 +3013,8 @@ function PMA_addUserAndCreateDatabase($_error, $real_sql_query, $sql_query, $use // Grant all privileges on wildcard name (username\_%) $q = 'GRANT ALL PRIVILEGES ON ' . $common_functions->backquote($common_functions->sqlAddSlashes($username) . '\_%') . '.* TO \'' - . $common_functions->sqlAddSlashes($username) . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\';'; + . $common_functions->sqlAddSlashes($username) + . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\';'; $sql_query .= $q; if (! PMA_DBI_try_query($q)) { $message = PMA_Message::rawError(PMA_DBI_getError()); @@ -2958,7 +3025,8 @@ function PMA_addUserAndCreateDatabase($_error, $real_sql_query, $sql_query, $use // Grant all privileges on the specified database to the new user $q = 'GRANT ALL PRIVILEGES ON ' . $common_functions->backquote($common_functions->sqlAddSlashes($dbname)) . '.* TO \'' - . $common_functions->sqlAddSlashes($username) . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\';'; + . $common_functions->sqlAddSlashes($username) + . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\';'; $sql_query .= $q; if (! PMA_DBI_try_query($q)) { $message = PMA_Message::rawError(PMA_DBI_getError()); From 7f0ef97ddebd6853e6567966f0427be24e74d268 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Mon, 16 Jul 2012 18:04:30 +0530 Subject: [PATCH 087/102] Spelling mistake --- server_privileges.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server_privileges.php b/server_privileges.php index 386b444e69..3fe8815a79 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -298,7 +298,7 @@ if (isset($_REQUEST['adduser_submit']) || isset($_REQUEST['change_copy'])) { } $sql_query = $create_user_show . $sql_query; } - list($sql_query, $message) = PMA_addUserAndCreateDatabas( + list($sql_query, $message) = PMA_addUserAndCreateDatabase( $_error, $real_sql_query, $sql_query, $username, $hostname ); From 18459099240d67b47951175e175e97d93cf823ea Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Mon, 16 Jul 2012 21:14:53 +0530 Subject: [PATCH 088/102] missing semicolon --- libraries/server_privileges.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 6c71446ed1..0aa1240fdc 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -2066,7 +2066,7 @@ function PMA_displayTablesInEditPrivs($dbname, $found_rows) $common_functions = PMA_CommonFunctions::getInstance(); $html_output = '' . "\n" + '. 'value="' . htmlspecialchars($dbname) . '"/>' . "\n"; $html_output .= '' . "\n"; From a0db338b70345d32cd5d1eee335e0911b90d212e Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Tue, 17 Jul 2012 00:02:49 +0530 Subject: [PATCH 089/102] change function signature --- libraries/server_privileges.lib.php | 6 +++--- server_privileges.php | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 0aa1240fdc..928ff6599b 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1588,10 +1588,10 @@ function PMA_getStandardLinks($conditional_class) * * @return array $extra_data */ -function PMA_getExtraDataForAjaxBehavior( $password, $sql_query,$link_edit, - $dbname_is_wildcard, $link_export +function PMA_getExtraDataForAjaxBehavior($password, $link_export, $sql_query, + $link_edit, $dbname_is_wildcard ) { - if (isset($sql_query)) { + if (strlen($sql_query)) { $extra_data['sql_query'] = PMA_CommonFunctions::getInstance()->getMessage(null, $sql_query); } diff --git a/server_privileges.php b/server_privileges.php index 3fe8815a79..02d5ebc9c3 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -446,8 +446,8 @@ if ($GLOBALS['is_ajax_request'] if (isset($password)) { $isPass = true; } - $extra_data = PMA_getExtraDataForAjaxBehavior( $isPass, - $sql_query, $link_edit, $dbname_is_wildcard, $link_export + $extra_data = PMA_getExtraDataForAjaxBehavior($isPass, $link_export, + (isset($sql_query) ? $sql_query : ''), $link_edit, $dbname_is_wildcard ); if ($message instanceof PMA_Message) { From b984bc397bcd958407174aac2bb3322568063e8c Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Tue, 17 Jul 2012 18:25:59 +0530 Subject: [PATCH 090/102] Improve coding style --- libraries/server_privileges.lib.php | 87 +++++++++++++++++++---------- 1 file changed, 59 insertions(+), 28 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 928ff6599b..3d96bd2ccc 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -436,7 +436,9 @@ function PMA_getHtmlToDisplayPrivilegesTable($random_n, $db = '*', $table = '*', // get columns $res = PMA_DBI_try_query( 'SHOW COLUMNS FROM ' - . PMA_CommonFunctions::getInstance()->backquote(PMA_CommonFunctions::getInstance()->unescapeMysqlWildcards($db)) + . PMA_CommonFunctions::getInstance()->backquote( + PMA_CommonFunctions::getInstance()->unescapeMysqlWildcards($db) + ) . '.' . PMA_CommonFunctions::getInstance()->backquote($table) . ';' ); $columns = array(); @@ -480,34 +482,63 @@ function PMA_getHtmlToDisplayPrivilegesTable($random_n, $db = '*', $table = '*', */ function PMA_getHtmlForDisplayResourceLimits($row) { - return '
' . "\n" + $html_output = '
' . "\n" . '' . __('Resource limits') . '' . "\n" - . '

' . __('Note: Setting these options to 0 (zero) removes the limit.') . '

' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n" - . '
' . "\n"; + . '

' + . '' . __('Note: Setting these options to 0 (zero) removes the limit.') + . '

' . "\n"; + + $html_output .= '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n"; + + $html_output .= '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n"; + + $html_output .= '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n"; + + $html_output .= '
' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n"; + + $html_output .= '
' . "\n"; + + return $html_output; } /** From 9199e0262e3c6ad0bd16b635913c8dec39f38ffd Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Tue, 17 Jul 2012 20:57:27 +0530 Subject: [PATCH 091/102] remove php parse error --- libraries/server_privileges.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 3d96bd2ccc..3f1ede7642 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -504,7 +504,7 @@ function PMA_getHtmlForDisplayResourceLimits($row) $html_output .= '
' . "\n" . '' . "\n" . ' Date: Tue, 17 Jul 2012 23:16:14 +0530 Subject: [PATCH 092/102] improve coding style --- libraries/server_privileges.lib.php | 276 ++++++++++++++++++++-------- 1 file changed, 196 insertions(+), 80 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 3f1ede7642..c2ffefe3e4 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -372,8 +372,9 @@ function PMA_getSqlQueryForDisplayPrivTable($db, $table, $username, $hostname) * * @return string html snippet */ -function PMA_getHtmlToDisplayPrivilegesTable($random_n, $db = '*', $table = '*', $submit = true) -{ +function PMA_getHtmlToDisplayPrivilegesTable($random_n, $db = '*', + $table = '*', $submit = true +) { $html_output = ''; if ($db == '*') { @@ -383,7 +384,9 @@ function PMA_getHtmlToDisplayPrivilegesTable($random_n, $db = '*', $table = '*', if (isset($GLOBALS['username'])) { $username = $GLOBALS['username']; $hostname = $GLOBALS['hostname']; - $sql_query = PMA_getSqlQueryForDisplayPrivTable($db, $table, $username, $hostname); + $sql_query = PMA_getSqlQueryForDisplayPrivTable( + $db, $table, $username, $hostname + ); $row = PMA_DBI_fetch_single_row($sql_query); } if (empty($row)) { @@ -552,8 +555,9 @@ function PMA_getHtmlForDisplayResourceLimits($row) * * @return string $html_output */ -function PMA_getHtmlForTableSpecificPrivileges($username, $hostname, $db, $table, $columns,$row) -{ +function PMA_getHtmlForTableSpecificPrivileges($username, $hostname, $db + , $table, $columns,$row +) { $common_functions = PMA_CommonFunctions::getInstance(); $res = PMA_DBI_query( 'SELECT `Column_name`, `Column_priv`' @@ -563,7 +567,9 @@ function PMA_getHtmlForTableSpecificPrivileges($username, $hostname, $db, $table .' AND `Host`' .' = \'' . $common_functions->sqlAddSlashes($hostname) . "'" .' AND `Db`' - .' = \'' . $common_functions->sqlAddSlashes($common_functions->unescapeMysqlWildcards($db)) . "'" + .' = \'' . $common_functions->sqlAddSlashes( + $common_functions->unescapeMysqlWildcards($db) + ) . "'" .' AND `Table_name`' .' = \'' . $common_functions->sqlAddSlashes($table) . '\';' ); @@ -669,16 +675,32 @@ function PMA_getHtmlForNotAttachedPrivilegesToTableSpecificColumn($row) . ($current_grant_value == 'Y' ? 'checked="checked" ' : '') . 'title="'; - $html_output .= (isset($GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5))]) - ? $GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5))] - : $GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5)) . 'Tbl']) . '"/>' . "\n"; + $html_output .= (isset($GLOBALS[ + 'strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5)) + ] ) + ? $GLOBALS[ + 'strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5)) + ] + : $GLOBALS[ + 'strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5)) . 'Tbl' + ] + ) + . '"/>' . "\n"; $html_output .= '' . "\n" + . (isset($GLOBALS[ + 'strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5)) + ]) + ? $GLOBALS[ + 'strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5)) + ] + : $GLOBALS[ + 'strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5)) . 'Tbl' + ] + ) + . '">' . strtoupper(substr($current_grant, 0, strlen($current_grant) - 5)) + . '' . "\n" . '
' . "\n"; } // end foreach () return $html_output; @@ -697,7 +719,10 @@ function PMA_getHtmlForNotAttachedPrivilegesToTableSpecificColumn($row) */ function PMA_getHtmlForGlobalOrDbSpecificPrivs($db, $table, $row, $random_n) { - $privTable_names = array(0 => __('Data'), 1 => __('Structure'), 2 => __('Administration')); + $privTable_names = array(0 => __('Data'), + 1 => __('Structure'), + 2 => __('Administration') + ); $privTable = array(); // d a t a $privTable[0] = PMA_getDataPrivilegeTable($db); @@ -709,7 +734,11 @@ function PMA_getHtmlForGlobalOrDbSpecificPrivs($db, $table, $row, $random_n) $privTable[2] = PMA_getAdministrationPrivilegeTable($db); $html_output = '' . "\n" . '
' . "\n" . '' . "\n" @@ -720,16 +749,22 @@ function PMA_getHtmlForGlobalOrDbSpecificPrivs($db, $table, $row, $random_n) ? __('Database-specific privileges') : __('Table-specific privileges'))) . "\n" . '(' + . $GLOBALS['url_query'] . '&checkall=1" ' + . 'onclick="setCheckboxes(\'addUsersForm_' . $random_n . '\', true); return false;">' . __('Check All') . ' /' . "\n" . '' + . $GLOBALS['url_query'] . '" ' + . 'onclick="setCheckboxes(\'addUsersForm_' . $random_n . '\', false); return false;">' . __('Uncheck All') . ')' . "\n" . '' . "\n" - . '

' . __('Note: MySQL privilege names are expressed in English') . '

' . "\n"; + . '

' + . __('Note: MySQL privilege names are expressed in English') + . '

' . "\n"; // Output the Global privilege tables with checkboxes - $html_output .= PMA_getHtmlForGlobalPrivTableWithCheckboxes($privTable, $privTable_names, $row); + $html_output .= PMA_getHtmlForGlobalPrivTableWithCheckboxes( + $privTable, $privTable_names, $row + ); // The "Resource limits" box is not displayed for db-specific privs if ($db == '*') { @@ -852,7 +887,10 @@ function PMA_getStructurePrivilegeTable($table, $row) function PMA_getAdministrationPrivilegeTable($db) { $administration_privTable = array( - array('Grant', 'GRANT', __('Allows adding users and privileges without reloading the privilege tables.')), + array('Grant', + 'GRANT', + __('Allows adding users and privileges without reloading the privilege tables.') + ), ); if ($db == '*') { $administration_privTable[] = array('Super', @@ -921,7 +959,10 @@ function PMA_getHtmlForGlobalPrivTableWithCheckboxes($privTable, $privTable_name . '' . "\n" . '' . "\n" @@ -952,46 +993,90 @@ function PMA_getHtmlForDisplayLoginInformationFields($mode = 'new') $GLOBALS['pred_username'] = 'any'; } $html_output = '
' . "\n" - . '' . __('Login Information') . '' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . '' . "\n" - . '' . "\n" - . '' . __('Login Information') . '' . "\n" + . '
' . "\n" + . '' . "\n" + . '' . "\n"; + + $html_output .= '' . "\n" + . '' . "\n"; + + $html_output .= '' . "\n" - . '
' . "\n" - . '
' . "\n" - . '' . "\n" - . '' . "\n" - . ' ' . "\n"; + $html_output .= ' onchange="' + . 'if (this.value == \'any\') { ' + . ' hostname.value = \'%\'; ' + . '} else if (this.value == \'localhost\') { ' + . ' hostname.value = \'localhost\'; ' + . '} ' + . (empty($thishost) + ? '' + : 'else if (this.value == \'thishost\') { ' + . ' hostname.value = \'' . addslashes(htmlspecialchars($thishost)) . '\'; ' + . '} ' + ) + . 'else if (this.value == \'hosttable\') { ' + . ' hostname.value = \'\'; ' + . '} else if (this.value == \'userdefined\') {' + . ' hostname.focus(); hostname.select(); ' + . '}">' . "\n"; unset($_current_user); // when we start editing a user, $GLOBALS['pred_hostname'] is not defined @@ -1009,64 +1094,95 @@ function PMA_getHtmlForDisplayLoginInformationFields($mode = 'new') break; } } - $html_output .= ' ' . "\n" . '' . "\n"; if (! empty($thishost)) { - $html_output .= ' ' . "\n"; } unset($thishost); - $html_output .= ' ' . "\n" - . ' ' . "\n"; + + $html_output .= '' . "\n" + ? ' selected="selected"' + : '') . '>' + . __('Use text field') . ':' . "\n" . '' . "\n" - . '' . "\n" - . '' . "\n" - . PMA_CommonFunctions::getInstance()->showHint(__('When Host table is used, this field is ignored and values stored in Host table are used instead.')) - . '
' . "\n" - . '
' . "\n" + . PMA_CommonFunctions::getInstance()->showHint( + __('When Host table is used, this field is ignored and values stored in Host table are used instead.') + ) + . '
' . "\n"; + + $html_output .= '
' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" - . '' . "\n" - . '' . "\n" - . '
' . "\n" - . '
' . "\n" - . '' . "\n" - . ' ' . "\n" - . '' . "\n" - . '
' . "\n" + . '' . "\n" + . '' . "\n" + . '' . "\n" + . '' . "\n" + . '
' . "\n"; + + $html_output .= '
' . "\n" + . '' . "\n" + . ' ' . "\n" + . '' . "\n" + . '
' . "\n" // Generate password added here via jQuery . '
' . "\n"; From f998b4c620819befc195f13851829637172cafb5 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Tue, 17 Jul 2012 23:26:46 +0530 Subject: [PATCH 093/102] improve coding style --- libraries/server_privileges.lib.php | 37 +++++++++++++++++++---------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index c2ffefe3e4..55eae0c0f5 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1249,8 +1249,9 @@ function PMA_getGrants($user, $host) * * @return string $message success or error message after updating password */ -function PMA_getMessageForUpdatePassword($pma_pw, $pma_pw2, $err_url, $username, $hostname) -{ +function PMA_getMessageForUpdatePassword($pma_pw, $pma_pw2, $err_url + , $username, $hostname +) { // similar logic in user_password.php $message = ''; @@ -1498,9 +1499,11 @@ function PMA_getHtmlForSpecificDbPrivileges($dbToCheck, $link_edit, $conditional . $common_functions->getIcon('b_usrcheck.png') . ' ' . sprintf( - __('Users having access to "%s"'), - '' - . htmlspecialchars($dbToCheck) . '' + __('Users having access to "%s"'), + '' + . htmlspecialchars($dbToCheck) + . '' ) . "\n" . '' . "\n"; @@ -1556,8 +1559,10 @@ function PMA_getHtmlForSpecificDbPrivileges($dbToCheck, $link_edit, $conditional . '' . __('New') . '' . "\n"; $html_output .= '' . "\n" . $common_functions->getIcon('b_usradd.png') @@ -1580,8 +1585,9 @@ function PMA_getHtmlForSpecificDbPrivileges($dbToCheck, $link_edit, $conditional * * @return string $html_output */ -function PMA_getHtmlTableBodyForSpecificDbPrivs($found, $row, $odd_row, $link_edit, $res, $dbToCheck) -{ +function PMA_getHtmlTableBodyForSpecificDbPrivs($found, $row, $odd_row, + $link_edit, $res, $dbToCheck +) { $html_output = '
' . __('Host') . '' . __('Password') . '' . __('Global privileges') . ' ' - . $common_functions->showHint(__('Note: MySQL privilege names are expressed in English')) + . $common_functions->showHint( + __('Note: MySQL privilege names are expressed in English') + ) . '' . __('Grant') . '' . __('Action') . '
' . "\n"; From 8d43951bcd8466e810ecc205c1afb1944d5425db Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Wed, 18 Jul 2012 07:28:58 +0530 Subject: [PATCH 094/102] improve coding style --- libraries/server_privileges.lib.php | 25 ++++++++++++++++++------- server_privileges.php | 27 ++++++++++++++++++++------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 55eae0c0f5..1198d52e64 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -3127,12 +3127,15 @@ function PMA_getDbSpecificPrivsQueriesForChangeOrCopyUser($queries, $username, $ * * @return array $sql_query, $message */ -function PMA_addUserAndCreateDatabase($_error, $real_sql_query, $sql_query, $username, $hostname) -{ +function PMA_addUserAndCreateDatabase($_error, $real_sql_query, $sql_query, + $username, $hostname +) { $common_functions = PMA_CommonFunctions::getInstance(); if ($_error || ! PMA_DBI_try_query($real_sql_query)) { - $_REQUEST['createdb-1'] = $_REQUEST['createdb-2'] = $_REQUEST['createdb-3'] = false; + $_REQUEST['createdb-1'] = $_REQUEST['createdb-2'] + = $_REQUEST['createdb-3'] + = false; $message = PMA_Message::rawError(PMA_DBI_getError()); } else { $message = PMA_Message::success(__('You have added a new user.')); @@ -3141,7 +3144,9 @@ function PMA_addUserAndCreateDatabase($_error, $real_sql_query, $sql_query, $use if (isset($_REQUEST['createdb-1'])) { // Create database with same name and grant all privileges $q = 'CREATE DATABASE IF NOT EXISTS ' - . $common_functions->backquote($common_functions->sqlAddSlashes($username)) . ';'; + . $common_functions->backquote( + $common_functions->sqlAddSlashes($username) + ) . ';'; $sql_query .= $q; if (! PMA_DBI_try_query($q)) { $message = PMA_Message::rawError(PMA_DBI_getError()); @@ -3159,7 +3164,9 @@ function PMA_addUserAndCreateDatabase($_error, $real_sql_query, $sql_query, $use $q = 'GRANT ALL PRIVILEGES ON ' . $common_functions->backquote( - $common_functions->escapeMysqlWildcards($common_functions->sqlAddSlashes($username)) + $common_functions->escapeMysqlWildcards( + $common_functions->sqlAddSlashes($username) + ) ) . '.* TO \'' . $common_functions->sqlAddSlashes($username) . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\';'; @@ -3172,7 +3179,9 @@ function PMA_addUserAndCreateDatabase($_error, $real_sql_query, $sql_query, $use if (isset($_REQUEST['createdb-2'])) { // Grant all privileges on wildcard name (username\_%) $q = 'GRANT ALL PRIVILEGES ON ' - . $common_functions->backquote($common_functions->sqlAddSlashes($username) . '\_%') . '.* TO \'' + . $common_functions->backquote( + $common_functions->sqlAddSlashes($username) . '\_%' + ) . '.* TO \'' . $common_functions->sqlAddSlashes($username) . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\';'; $sql_query .= $q; @@ -3184,7 +3193,9 @@ function PMA_addUserAndCreateDatabase($_error, $real_sql_query, $sql_query, $use if (isset($_REQUEST['createdb-3'])) { // Grant all privileges on the specified database to the new user $q = 'GRANT ALL PRIVILEGES ON ' - . $common_functions->backquote($common_functions->sqlAddSlashes($dbname)) . '.* TO \'' + . $common_functions->backquote( + $common_functions->sqlAddSlashes($dbname) + ) . '.* TO \'' . $common_functions->sqlAddSlashes($username) . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\';'; $sql_query .= $q; diff --git a/server_privileges.php b/server_privileges.php index 02d5ebc9c3..56d4faa5d9 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -250,16 +250,22 @@ if (isset($_REQUEST['adduser_submit']) || isset($_REQUEST['change_copy'])) { $_add_user_error = true; } else { - $create_user_real = 'CREATE USER \'' . $common_functions->sqlAddSlashes($username) . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\''; + $create_user_real = 'CREATE USER \'' + . $common_functions->sqlAddSlashes($username) . '\'@\'' + . $common_functions->sqlAddSlashes($hostname) . '\''; $real_sql_query = 'GRANT ' . join(', ', PMA_extractPrivInfo()) . ' ON *.* TO \'' - . $common_functions->sqlAddSlashes($username) . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\''; + . $common_functions->sqlAddSlashes($username) . '\'@\'' + . $common_functions->sqlAddSlashes($hostname) . '\''; + if ($pred_password != 'none' && $pred_password != 'keep') { $sql_query = $real_sql_query . ' IDENTIFIED BY \'***\''; - $real_sql_query .= ' IDENTIFIED BY \'' . $common_functions->sqlAddSlashes($pma_pw) . '\''; + $real_sql_query .= ' IDENTIFIED BY \'' + . $common_functions->sqlAddSlashes($pma_pw) . '\''; if (isset($create_user_real)) { $create_user_show = $create_user_real . ' IDENTIFIED BY \'***\''; - $create_user_real .= ' IDENTIFIED BY \'' . $common_functions->sqlAddSlashes($pma_pw) . '\''; + $create_user_real .= ' IDENTIFIED BY \'' + . $common_functions->sqlAddSlashes($pma_pw) . '\''; } } else { if ($pred_password == 'keep' && ! empty($password)) { @@ -370,15 +376,22 @@ if (isset($_REQUEST['delete']) || (isset($_REQUEST['change_copy']) && $_REQUEST['mode'] < 4) ) { if (isset($_REQUEST['change_copy'])) { - $selected_usr = array($_REQUEST['old_username'] . '&#27;' . $_REQUEST['old_hostname']); + $selected_usr = array( + $_REQUEST['old_username'] . '&#27;' . $_REQUEST['old_hostname'] + ); } else { $selected_usr = $_REQUEST['selected_usr']; $queries = array(); } foreach ($selected_usr as $each_user) { list($this_user, $this_host) = explode('&#27;', $each_user); - $queries[] = '# ' . sprintf(__('Deleting %s'), '\'' . $this_user . '\'@\'' . $this_host . '\'') . ' ...'; - $queries[] = 'DROP USER \'' . $common_functions->sqlAddSlashes($this_user) . '\'@\'' . $common_functions->sqlAddSlashes($this_host) . '\';'; + $queries[] = '# ' + . sprintf(__('Deleting %s'), + '\'' . $this_user . '\'@\'' . $this_host . '\'' + ) . ' ...'; + $queries[] = 'DROP USER \'' + . $common_functions->sqlAddSlashes($this_user) + . '\'@\'' . $common_functions->sqlAddSlashes($this_host) . '\';'; if (isset($_REQUEST['drop_users_db'])) { $queries[] = 'DROP DATABASE IF EXISTS ' . $common_functions->backquote($this_user) . ';'; From 9210a1acc95a3dc7956d4addfdede75efae84c21 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Wed, 18 Jul 2012 07:48:44 +0530 Subject: [PATCH 095/102] remove global variables --- libraries/server_privileges.lib.php | 20 ++++++++++---------- server_privileges.php | 9 ++++++--- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 1198d52e64..2b83f07cd5 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1484,7 +1484,6 @@ function PMA_getListOfPrivilegesAndComparedPrivileges() /** * Get the HTML for user form and check the privileges for a particular database. * - * @param string $dbToCheck database to check for privileges * @param string $link_edit standard link for edit * @param string $conditional_class if ajaxable 'Ajax' otherwise '' * @@ -1501,8 +1500,8 @@ function PMA_getHtmlForSpecificDbPrivileges($dbToCheck, $link_edit, $conditional . sprintf( __('Users having access to "%s"'), '' - . htmlspecialchars($dbToCheck) + . PMA_generate_common_url($_REQUEST['checkprivs']) . '">' + . htmlspecialchars($_REQUEST['checkprivs']) . '' ) . "\n" @@ -1525,7 +1524,7 @@ function PMA_getHtmlForSpecificDbPrivileges($dbToCheck, $link_edit, $conditional $sql_query = '(SELECT ' . $list_of_privileges . ', `Db`' .' FROM `mysql`.`db`' - .' WHERE \'' . $common_functions->sqlAddSlashes($dbToCheck) . "'" + .' WHERE \'' . $common_functions->sqlAddSlashes($_REQUEST['checkprivs']) . "'" .' LIKE `Db`' .' AND NOT (' . $list_of_compared_privileges. ')) ' .'UNION ' @@ -1541,7 +1540,7 @@ function PMA_getHtmlForSpecificDbPrivileges($dbToCheck, $link_edit, $conditional $found = true; } $html_output .= PMA_getHtmlTableBodyForSpecificDbPrivs( - $found, $row, $odd_row, $link_edit, $res, $dbToCheck + $found, $row, $odd_row, $link_edit, $res ); $html_output .= '
' . '
' @@ -1560,9 +1559,9 @@ function PMA_getHtmlForSpecificDbPrivileges($dbToCheck, $link_edit, $conditional $html_output .= '' . "\n" . $common_functions->getIcon('b_usradd.png') @@ -1581,12 +1580,11 @@ function PMA_getHtmlForSpecificDbPrivileges($dbToCheck, $link_edit, $conditional * @param boolean $odd_row whether odd or not * @param string $link_edit standard link for edit * @param string $res ran sql query - * @param string $dbToCheck database to check for privileges * * @return string $html_output */ function PMA_getHtmlTableBodyForSpecificDbPrivs($found, $row, $odd_row, - $link_edit, $res, $dbToCheck + $link_edit, $res ) { $html_output = '' . "\n"; if ($found) { @@ -1626,7 +1624,9 @@ function PMA_getHtmlTableBodyForSpecificDbPrivs($found, $row, $odd_row, if (! isset($current['Db']) || $current['Db'] == '*') { $html_output .= __('global'); } elseif ( - $current['Db'] == PMA_CommonFunctions::getInstance()->escapeMysqlWildcards($dbToCheck) + $current['Db'] == PMA_CommonFunctions::getInstance()->escapeMysqlWildcards( + $_REQUEST['checkprivs'] + ) ) { $html_output .= __('database-specific'); } else { diff --git a/server_privileges.php b/server_privileges.php index 56d4faa5d9..8f5c23e70a 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -476,7 +476,7 @@ if ($GLOBALS['is_ajax_request'] * Displays the links */ if (isset($viewing_mode) && $viewing_mode == 'db') { - $db = $checkprivs; + $db = $_REQUEST['checkprivs']; $url_query .= '&goto=db_operations.php'; // Gets the database structure @@ -512,7 +512,10 @@ if (isset($_REQUEST['export']) } } -if (empty($_REQUEST['adduser']) && (! isset($checkprivs) || ! strlen($checkprivs))) { +if (empty($_REQUEST['adduser']) + && (! isset($_REQUEST['checkprivs']) + || ! strlen($_REQUEST['checkprivs'])) +) { if (! isset($username)) { // No username is given --> display the overview $response->addHTML( @@ -549,7 +552,7 @@ if (empty($_REQUEST['adduser']) && (! isset($checkprivs) || ! strlen($checkprivs } else { // check the privileges for a particular database. $response->addHTML( - PMA_getHtmlForSpecificDbPrivileges($checkprivs, $link_edit, $conditional_class) + PMA_getHtmlForSpecificDbPrivileges($link_edit, $conditional_class) ); } // end if (empty($_REQUEST['adduser']) && empty($checkprivs)) ... elseif ... else ... From 37981416a9fff37b6982b685647a2d8c5cfdf287 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Wed, 18 Jul 2012 07:51:10 +0530 Subject: [PATCH 096/102] remove global variable db --- server_privileges.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server_privileges.php b/server_privileges.php index 8f5c23e70a..079341b917 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -476,7 +476,7 @@ if ($GLOBALS['is_ajax_request'] * Displays the links */ if (isset($viewing_mode) && $viewing_mode == 'db') { - $db = $_REQUEST['checkprivs']; + $_REQUEST['db'] = $_REQUEST['checkprivs']; $url_query .= '&goto=db_operations.php'; // Gets the database structure From 7245209eae1a466c9eafc6afafd78fde1dfe73cd Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Wed, 18 Jul 2012 07:58:50 +0530 Subject: [PATCH 097/102] remove global variable --- server_privileges.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server_privileges.php b/server_privileges.php index 079341b917..55eb7349a2 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -196,9 +196,9 @@ $random_n = mt_rand(0, 1000000); */ if (isset($_REQUEST['change_copy'])) { $user_host_condition = ' WHERE `User`' - .' = \'' . $common_functions->sqlAddSlashes($old_username) . "'" + .' = \'' . $common_functions->sqlAddSlashes($_REQUEST['old_username']) . "'" .' AND `Host`' - .' = \'' . $common_functions->sqlAddSlashes($old_hostname) . '\';'; + .' = \'' . $common_functions->sqlAddSlashes($_REQUEST['old_hostname']) . '\';'; $row = PMA_DBI_fetch_single_row('SELECT * FROM `mysql`.`user` ' . $user_host_condition); if (! $row) { PMA_Message::notice(__('No user found.'))->display(); @@ -475,7 +475,7 @@ if ($GLOBALS['is_ajax_request'] /** * Displays the links */ -if (isset($viewing_mode) && $viewing_mode == 'db') { +if (isset($_REQUEST['viewing_mode']) && $_REQUEST['viewing_mode'] == 'db') { $_REQUEST['db'] = $_REQUEST['checkprivs']; $url_query .= '&goto=db_operations.php'; From c6e265c236e3b7d954cf2b88cb51b24a33f65c98 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Wed, 18 Jul 2012 08:30:59 +0530 Subject: [PATCH 098/102] remove global variable --- libraries/server_privileges.lib.php | 16 ++++++++-------- server_privileges.php | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 2b83f07cd5..1d1166fa5d 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1249,16 +1249,16 @@ function PMA_getGrants($user, $host) * * @return string $message success or error message after updating password */ -function PMA_getMessageForUpdatePassword($pma_pw, $pma_pw2, $err_url +function PMA_getMessageForUpdatePassword($pma_pw2, $err_url , $username, $hostname ) { // similar logic in user_password.php $message = ''; - if (empty($_REQUEST['nopass']) && isset($pma_pw) && isset($pma_pw2)) { - if ($pma_pw != $pma_pw2) { + if (empty($_REQUEST['nopass']) && isset($_POST['pma_pw']) && isset($pma_pw2)) { + if ($_POST['pma_pw'] != $pma_pw2) { $message = PMA_Message::error(__('The passwords aren\'t the same!')); - } elseif (empty($pma_pw) || empty($pma_pw2)) { + } elseif (empty($_POST['pma_pw']) || empty($pma_pw2)) { $message = PMA_Message::error(__('The password is empty!')); } } @@ -1278,15 +1278,15 @@ function PMA_getMessageForUpdatePassword($pma_pw, $pma_pw2, $err_url $sql_query = 'SET PASSWORD FOR \'' . $common_functions->sqlAddSlashes($username) . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\' = ' - . (($pma_pw == '') + . (($_POST['pma_pw'] == '') ? '\'\'' - : $hashing_function . '(\'' . preg_replace('@.@s', '*', $pma_pw) . '\')'); + : $hashing_function . '(\'' . preg_replace('@.@s', '*', $_POST['pma_pw']) . '\')'); $local_query = 'SET PASSWORD FOR \'' . $common_functions->sqlAddSlashes($username) . '\'@\'' . $common_functions->sqlAddSlashes($hostname) . '\' = ' - . (($pma_pw == '') ? '\'\'' : $hashing_function - . '(\'' . $common_functions->sqlAddSlashes($pma_pw) . '\')'); + . (($_POST['pma_pw'] == '') ? '\'\'' : $hashing_function + . '(\'' . $common_functions->sqlAddSlashes($_POST['pma_pw']) . '\')'); PMA_DBI_try_query($local_query) or $common_functions->mysqlDie(PMA_DBI_getError(), $sql_query, false, $err_url); diff --git a/server_privileges.php b/server_privileges.php index 55eb7349a2..bfad2fcc29 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -261,11 +261,11 @@ if (isset($_REQUEST['adduser_submit']) || isset($_REQUEST['change_copy'])) { if ($pred_password != 'none' && $pred_password != 'keep') { $sql_query = $real_sql_query . ' IDENTIFIED BY \'***\''; $real_sql_query .= ' IDENTIFIED BY \'' - . $common_functions->sqlAddSlashes($pma_pw) . '\''; + . $common_functions->sqlAddSlashes($_POST['pma_pw']) . '\''; if (isset($create_user_real)) { $create_user_show = $create_user_real . ' IDENTIFIED BY \'***\''; $create_user_real .= ' IDENTIFIED BY \'' - . $common_functions->sqlAddSlashes($pma_pw) . '\''; + . $common_functions->sqlAddSlashes($_POST['pma_pw']) . '\''; } } else { if ($pred_password == 'keep' && ! empty($password)) { @@ -364,7 +364,7 @@ if (isset($_REQUEST['revokeall'])) { */ if (isset($_REQUEST['change_pw'])) { $message = PMA_getMessageForUpdatePassword( - $pma_pw, $pma_pw2, $err_url, $username, $hostname + $pma_pw2, $err_url, $username, $hostname ); } From 6ec2e514b6cc2b740aada33a33998ed8a4a161a5 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Wed, 18 Jul 2012 08:32:52 +0530 Subject: [PATCH 099/102] remove global variable pma_pw2 --- libraries/server_privileges.lib.php | 15 ++++++--------- server_privileges.php | 2 +- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 1d1166fa5d..db63aa1f30 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1240,25 +1240,22 @@ function PMA_getGrants($user, $host) /** * Update password and get message for password updating - * - * @param string $pma_pw password that user entered for change, comming from request - * @param string $pma_pw2 Re typed password, comming from request + * * @param string $err_url error url * @param string $username username * @param string $hostname hostname * * @return string $message success or error message after updating password */ -function PMA_getMessageForUpdatePassword($pma_pw2, $err_url - , $username, $hostname -) { +function PMA_getMessageForUpdatePassword($err_url, $username, $hostname) +{ // similar logic in user_password.php $message = ''; - if (empty($_REQUEST['nopass']) && isset($_POST['pma_pw']) && isset($pma_pw2)) { - if ($_POST['pma_pw'] != $pma_pw2) { + if (empty($_REQUEST['nopass']) && isset($_POST['pma_pw']) && isset($_POST['pma_pw2'])) { + if ($_POST['pma_pw'] != $_POST['pma_pw2']) { $message = PMA_Message::error(__('The passwords aren\'t the same!')); - } elseif (empty($_POST['pma_pw']) || empty($pma_pw2)) { + } elseif (empty($_POST['pma_pw']) || empty($_POST['pma_pw2'])) { $message = PMA_Message::error(__('The password is empty!')); } } diff --git a/server_privileges.php b/server_privileges.php index bfad2fcc29..644ab8ff5e 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -364,7 +364,7 @@ if (isset($_REQUEST['revokeall'])) { */ if (isset($_REQUEST['change_pw'])) { $message = PMA_getMessageForUpdatePassword( - $pma_pw2, $err_url, $username, $hostname + $err_url, $username, $hostname ); } From f03376076d9b4184e4d7fe16cd5e86b836238582 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Wed, 18 Jul 2012 22:51:24 +0530 Subject: [PATCH 100/102] remove php notice --- libraries/server_privileges.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index db63aa1f30..73b59a6d1c 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1486,7 +1486,7 @@ function PMA_getListOfPrivilegesAndComparedPrivileges() * * @return string $html_output */ -function PMA_getHtmlForSpecificDbPrivileges($dbToCheck, $link_edit, $conditional_class) +function PMA_getHtmlForSpecificDbPrivileges($link_edit, $conditional_class) { $common_functions = PMA_CommonFunctions::getInstance(); // check the privileges for a particular database. From b05ee596f951efd7f42d6943b41bf9bf9f37461b Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Thu, 19 Jul 2012 01:19:38 +0530 Subject: [PATCH 101/102] remove global variables --- libraries/server_privileges.lib.php | 4 +- server_privileges.php | 57 +++++------------------------ 2 files changed, 12 insertions(+), 49 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 73b59a6d1c..ce0df26a44 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -138,8 +138,8 @@ function PMA_extractPrivInfo($row = '', $enableHTML = false) $privs[] = 'USAGE'; } } elseif ($allPrivileges - && (! isset($GLOBALS['grant_count']) - || count($privs) == $GLOBALS['grant_count']) + && (! isset($_POST['grant_count']) + || count($privs) == $_POST['grant_count']) ) { if ($enableHTML) { $privs = array('