From a2b08bae7b9979dee6a9f195e55481bf1d7e882d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Mon, 11 Sep 2017 16:27:39 -0300 Subject: [PATCH 1/4] Refactor check_user_privileges to static methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refactor check_user_privileges.lib.php functions to static methods and move logic to check_user_privileges.inc.php. Signed-off-by: Maurício Meneghini Fauth --- db_operations.php | 2 +- db_routines.php | 2 +- index.php | 2 +- libraries/check_user_privileges.inc.php | 29 ++ libraries/check_user_privileges.lib.php | 322 ------------------ libraries/classes/CheckUserPrivileges.php | 310 +++++++++++++++++ .../Server/ServerDatabasesController.php | 2 +- .../Table/TableStructureController.php | 4 +- libraries/classes/Display/CreateTable.php | 2 +- libraries/classes/Import.php | 2 +- libraries/classes/ListDatabase.php | 2 +- .../classes/Navigation/NavigationTree.php | 2 +- .../Nodes/NodeDatabaseContainer.php | 2 +- server_privileges.php | 2 +- sql.php | 2 +- tbl_operations.php | 2 +- .../CheckUserPrivilegesTest.php} | 37 +- test/classes/ImportTest.php | 2 +- .../classes/navigation/NavigationTreeTest.php | 4 +- test/classes/plugin/import/ImportCsvTest.php | 2 +- test/classes/plugin/import/ImportLdiTest.php | 2 +- .../plugin/import/ImportMediawikiTest.php | 2 +- test/classes/plugin/import/ImportOdsTest.php | 2 +- test/classes/plugin/import/ImportShpTest.php | 2 +- test/classes/plugin/import/ImportSqlTest.php | 2 +- test/classes/plugin/import/ImportXmlTest.php | 2 +- 26 files changed, 381 insertions(+), 365 deletions(-) create mode 100644 libraries/check_user_privileges.inc.php delete mode 100644 libraries/check_user_privileges.lib.php create mode 100644 libraries/classes/CheckUserPrivileges.php rename test/{libraries/PMA_check_user_privileges_test.php => classes/CheckUserPrivilegesTest.php} (86%) diff --git a/db_operations.php b/db_operations.php index 19744a7075..63d0123bbf 100644 --- a/db_operations.php +++ b/db_operations.php @@ -28,7 +28,7 @@ require_once 'libraries/common.inc.php'; /** * functions implementation for this script */ -require_once 'libraries/check_user_privileges.lib.php'; +require_once 'libraries/check_user_privileges.inc.php'; // add a javascript file for jQuery functions to handle Ajax actions $response = Response::getInstance(); diff --git a/db_routines.php b/db_routines.php index 7e5c3ba093..5740c35aee 100644 --- a/db_routines.php +++ b/db_routines.php @@ -14,7 +14,7 @@ require_once 'libraries/common.inc.php'; /** * Include all other files */ -require_once 'libraries/check_user_privileges.lib.php'; +require_once 'libraries/check_user_privileges.inc.php'; /** * Do the magic diff --git a/index.php b/index.php index b67f5402fa..bb14dce20f 100644 --- a/index.php +++ b/index.php @@ -192,7 +192,7 @@ if ($server > 0 || count($cfg['Servers']) > 1 * Displays the mysql server related links */ if ($server > 0) { - include_once 'libraries/check_user_privileges.lib.php'; + include_once 'libraries/check_user_privileges.inc.php'; // Logout for advanced authentication if ($cfg['Server']['auth_type'] != 'config') { diff --git a/libraries/check_user_privileges.inc.php b/libraries/check_user_privileges.inc.php new file mode 100644 index 0000000000..976776676c --- /dev/null +++ b/libraries/check_user_privileges.inc.php @@ -0,0 +1,29 @@ +isSuperuser(); + +list($username, $hostname) = $GLOBALS['dbi']->getCurrentUserAndHost(); +if ($username === '') { // MySQL is started with --skip-grant-tables + $GLOBALS['is_create_db_priv'] = true; + $GLOBALS['is_reload_priv'] = true; + $GLOBALS['db_to_create'] = ''; + $GLOBALS['dbs_where_create_table_allowed'] = array('*'); + $GLOBALS['dbs_to_test'] = false; + $GLOBALS['db_priv'] = true; + $GLOBALS['col_priv'] = true; + $GLOBALS['table_priv'] = true; + $GLOBALS['proc_priv'] = true; +} else { + CheckUserPrivileges::analyseShowGrant(); +} diff --git a/libraries/check_user_privileges.lib.php b/libraries/check_user_privileges.lib.php deleted file mode 100644 index e2e4afa517..0000000000 --- a/libraries/check_user_privileges.lib.php +++ /dev/null @@ -1,322 +0,0 @@ -isSuperuser(); - -/** - * Extracts details from a result row of a SHOW GRANT query - * - * @param string $row grant row - * - * @return array - */ -function PMA_getItemsFromShowGrantsRow($row) -{ - $db_name_offset = mb_strpos($row, ' ON ') + 4; - $show_grants_dbname = mb_substr( - $row, $db_name_offset, - mb_strpos($row, '.', $db_name_offset) - $db_name_offset - ); - - $show_grants_dbname = PhpMyAdmin\Util::unQuote($show_grants_dbname, '`'); - - $show_grants_str = mb_substr( - $row, - 6, - (mb_strpos($row, ' ON ') - 6) - ); - - // extrac table from GRANT sytax - $tblname_start_offset = mb_strpos($row, '.') + 1; - $tblname_end_offset = mb_strpos($row, ' TO '); - - $show_grants_tblname = mb_substr( - $row, $tblname_start_offset, - $tblname_end_offset - $tblname_start_offset - ); - $show_grants_tblname = PhpMyAdmin\Util::unQuote($show_grants_tblname, '`'); - - return array( - $show_grants_str, - $show_grants_dbname, - $show_grants_tblname - ); -} - -/** - * Check if user has required privileges for - * performing 'Adjust privileges' operations - * - * @param string $show_grants_str string containing grants for user - * @param string $show_grants_dbname name of db extracted from grant string - * @param string $show_grants_tblname name of table extracted from grant string - * - * @return void - */ -function PMA_checkRequiredPrivilegesForAdjust( - $show_grants_str, - $show_grants_dbname, - $show_grants_tblname -) { - // '... ALL PRIVILEGES ON *.* ...' OR '... ALL PRIVILEGES ON `mysql`.* ..' - // OR - // SELECT, INSERT, UPDATE, DELETE .... ON *.* OR `mysql`.* - if ($show_grants_str == 'ALL' - || $show_grants_str == 'ALL PRIVILEGES' - || (mb_strpos( - $show_grants_str, 'SELECT, INSERT, UPDATE, DELETE' - ) !== false) - ) { - if ($show_grants_dbname == '*' - && $show_grants_tblname == '*' - ) { - $GLOBALS['col_priv'] = true; - $GLOBALS['db_priv'] = true; - $GLOBALS['proc_priv'] = true; - $GLOBALS['table_priv'] = true; - - if ($show_grants_str == 'ALL PRIVILEGES' - || $show_grants_str == 'ALL' - ) { - $GLOBALS['is_reload_priv'] = true; - } - } - - // check for specific tables in `mysql` db - // Ex. '... ALL PRIVILEGES on `mysql`.`columns_priv` .. ' - if ($show_grants_dbname == 'mysql') { - switch ($show_grants_tblname) { - case "columns_priv": - $GLOBALS['col_priv'] = true; - break; - case "db": - $GLOBALS['db_priv'] = true; - break; - case "procs_priv": - $GLOBALS['proc_priv'] = true; - break; - case "tables_priv": - $GLOBALS['table_priv'] = true; - break; - case "*": - $GLOBALS['col_priv'] = true; - $GLOBALS['db_priv'] = true; - $GLOBALS['proc_priv'] = true; - $GLOBALS['table_priv'] = true; - break; - default: - } - } - } -} - -/** - * sets privilege information extracted from SHOW GRANTS result - * - * Detection for some CREATE privilege. - * - * Since MySQL 4.1.2, we can easily detect current user's grants using $userlink - * (no control user needed) and we don't have to try any other method for - * detection - * - * @todo fix to get really all privileges, not only explicitly defined for this user - * from MySQL manual: (https://dev.mysql.com/doc/refman/5.0/en/show-grants.html) - * SHOW GRANTS displays only the privileges granted explicitly to the named - * account. Other privileges might be available to the account, but they are not - * displayed. For example, if an anonymous account exists, the named account - * might be able to use its privileges, but SHOW GRANTS will not display them. - * - * @return void - */ -function PMA_analyseShowGrant() -{ - if (PhpMyAdmin\Util::cacheExists('is_create_db_priv')) { - $GLOBALS['is_create_db_priv'] = PhpMyAdmin\Util::cacheGet( - 'is_create_db_priv' - ); - $GLOBALS['is_reload_priv'] = PhpMyAdmin\Util::cacheGet( - 'is_reload_priv' - ); - $GLOBALS['db_to_create'] = PhpMyAdmin\Util::cacheGet( - 'db_to_create' - ); - $GLOBALS['dbs_where_create_table_allowed'] = PhpMyAdmin\Util::cacheGet( - 'dbs_where_create_table_allowed' - ); - $GLOBALS['dbs_to_test'] = PhpMyAdmin\Util::cacheGet( - 'dbs_to_test' - ); - - $GLOBALS['db_priv'] = PhpMyAdmin\Util::cacheGet( - 'db_priv' - ); - $GLOBALS['col_priv'] = PhpMyAdmin\Util::cacheGet( - 'col_priv' - ); - $GLOBALS['table_priv'] = PhpMyAdmin\Util::cacheGet( - 'table_priv' - ); - $GLOBALS['proc_priv'] = PhpMyAdmin\Util::cacheGet( - 'proc_priv' - ); - - return; - } - - // defaults - $GLOBALS['is_create_db_priv'] = false; - $GLOBALS['is_reload_priv'] = false; - $GLOBALS['db_to_create'] = ''; - $GLOBALS['dbs_where_create_table_allowed'] = array(); - $GLOBALS['dbs_to_test'] = $GLOBALS['dbi']->getSystemSchemas(); - $GLOBALS['proc_priv'] = false; - $GLOBALS['db_priv'] = false; - $GLOBALS['col_priv'] = false; - $GLOBALS['table_priv'] = false; - - $rs_usr = $GLOBALS['dbi']->tryQuery('SHOW GRANTS'); - - if (! $rs_usr) { - return; - } - - $re0 = '(^|(\\\\\\\\)+|[^\\\\])'; // non-escaped wildcards - $re1 = '(^|[^\\\\])(\\\)+'; // escaped wildcards - - while ($row = $GLOBALS['dbi']->fetchRow($rs_usr)) { - list( - $show_grants_str, - $show_grants_dbname, - $show_grants_tblname - ) = PMA_getItemsFromShowGrantsRow($row[0]); - - if ($show_grants_dbname == '*') { - if ($show_grants_str != 'USAGE') { - $GLOBALS['dbs_to_test'] = false; - } - } elseif ($GLOBALS['dbs_to_test'] !== false) { - $GLOBALS['dbs_to_test'][] = $show_grants_dbname; - } - - if ( - mb_strpos($show_grants_str,'RELOAD') !== false - ) { - $GLOBALS['is_reload_priv'] = true; - } - - // check for the required privileges for adjust - PMA_checkRequiredPrivilegesForAdjust( - $show_grants_str, - $show_grants_dbname, - $show_grants_tblname - ); - - /** - * @todo if we find CREATE VIEW but not CREATE, do not offer - * the create database dialog box - */ - if ($show_grants_str == 'ALL' - || $show_grants_str == 'ALL PRIVILEGES' - || $show_grants_str == 'CREATE' - || strpos($show_grants_str, 'CREATE,') !== false - ) { - if ($show_grants_dbname == '*') { - // a global CREATE privilege - $GLOBALS['is_create_db_priv'] = true; - $GLOBALS['is_reload_priv'] = true; - $GLOBALS['db_to_create'] = ''; - $GLOBALS['dbs_where_create_table_allowed'][] = '*'; - // @todo we should not break here, cause GRANT ALL *.* - // could be revoked by a later rule like GRANT SELECT ON db.* - break; - } else { - // this array may contain wildcards - $GLOBALS['dbs_where_create_table_allowed'][] = $show_grants_dbname; - - $dbname_to_test = PhpMyAdmin\Util::backquote($show_grants_dbname); - - if ($GLOBALS['is_create_db_priv']) { - // no need for any more tests if we already know this - continue; - } - - // does this db exist? - if ((preg_match('/' . $re0 . '%|_/', $show_grants_dbname) - && ! preg_match('/\\\\%|\\\\_/', $show_grants_dbname)) - || (! $GLOBALS['dbi']->tryQuery( - 'USE ' . preg_replace( - '/' . $re1 . '(%|_)/', '\\1\\3', $dbname_to_test - ) - ) - && mb_substr($GLOBALS['dbi']->getError(), 1, 4) != 1044) - ) { - /** - * Do not handle the underscore wildcard - * (this case must be rare anyway) - */ - $GLOBALS['db_to_create'] = preg_replace( - '/' . $re0 . '%/', '\\1', - $show_grants_dbname - ); - $GLOBALS['db_to_create'] = preg_replace( - '/' . $re1 . '(%|_)/', '\\1\\3', - $GLOBALS['db_to_create'] - ); - $GLOBALS['is_create_db_priv'] = true; - - /** - * @todo collect $GLOBALS['db_to_create'] into an array, - * to display a drop-down in the "Create database" dialog - */ - // we don't break, we want all possible databases - //break; - } // end if - } // end elseif - } // end if - - } // end while - - $GLOBALS['dbi']->freeResult($rs_usr); - - // must also cacheUnset() them in - // PhpMyAdmin\Plugins\Auth\AuthenticationCookie - PhpMyAdmin\Util::cacheSet('is_create_db_priv', $GLOBALS['is_create_db_priv']); - PhpMyAdmin\Util::cacheSet('is_reload_priv', $GLOBALS['is_reload_priv']); - PhpMyAdmin\Util::cacheSet('db_to_create', $GLOBALS['db_to_create']); - PhpMyAdmin\Util::cacheSet( - 'dbs_where_create_table_allowed', - $GLOBALS['dbs_where_create_table_allowed'] - ); - PhpMyAdmin\Util::cacheSet('dbs_to_test', $GLOBALS['dbs_to_test']); - - PhpMyAdmin\Util::cacheSet('proc_priv', $GLOBALS['proc_priv']); - PhpMyAdmin\Util::cacheSet('table_priv', $GLOBALS['table_priv']); - PhpMyAdmin\Util::cacheSet('col_priv', $GLOBALS['col_priv']); - PhpMyAdmin\Util::cacheSet('db_priv', $GLOBALS['db_priv']); -} // end function - -list($username, $hostname) = $GLOBALS['dbi']->getCurrentUserAndHost(); -if ($username === '') { // MySQL is started with --skip-grant-tables - $GLOBALS['is_create_db_priv'] = true; - $GLOBALS['is_reload_priv'] = true; - $GLOBALS['db_to_create'] = ''; - $GLOBALS['dbs_where_create_table_allowed'] = array('*'); - $GLOBALS['dbs_to_test'] = false; - $GLOBALS['db_priv'] = true; - $GLOBALS['col_priv'] = true; - $GLOBALS['table_priv'] = true; - $GLOBALS['proc_priv'] = true; -} else { - PMA_analyseShowGrant(); -} diff --git a/libraries/classes/CheckUserPrivileges.php b/libraries/classes/CheckUserPrivileges.php new file mode 100644 index 0000000000..732e892f47 --- /dev/null +++ b/libraries/classes/CheckUserPrivileges.php @@ -0,0 +1,310 @@ +getSystemSchemas(); + $GLOBALS['proc_priv'] = false; + $GLOBALS['db_priv'] = false; + $GLOBALS['col_priv'] = false; + $GLOBALS['table_priv'] = false; + + $rs_usr = $GLOBALS['dbi']->tryQuery('SHOW GRANTS'); + + if (! $rs_usr) { + return; + } + + $re0 = '(^|(\\\\\\\\)+|[^\\\\])'; // non-escaped wildcards + $re1 = '(^|[^\\\\])(\\\)+'; // escaped wildcards + + while ($row = $GLOBALS['dbi']->fetchRow($rs_usr)) { + list( + $show_grants_str, + $show_grants_dbname, + $show_grants_tblname + ) = self::getItemsFromShowGrantsRow($row[0]); + + if ($show_grants_dbname == '*') { + if ($show_grants_str != 'USAGE') { + $GLOBALS['dbs_to_test'] = false; + } + } elseif ($GLOBALS['dbs_to_test'] !== false) { + $GLOBALS['dbs_to_test'][] = $show_grants_dbname; + } + + if ( + mb_strpos($show_grants_str,'RELOAD') !== false + ) { + $GLOBALS['is_reload_priv'] = true; + } + + // check for the required privileges for adjust + self::checkRequiredPrivilegesForAdjust( + $show_grants_str, + $show_grants_dbname, + $show_grants_tblname + ); + + /** + * @todo if we find CREATE VIEW but not CREATE, do not offer + * the create database dialog box + */ + if ($show_grants_str == 'ALL' + || $show_grants_str == 'ALL PRIVILEGES' + || $show_grants_str == 'CREATE' + || strpos($show_grants_str, 'CREATE,') !== false + ) { + if ($show_grants_dbname == '*') { + // a global CREATE privilege + $GLOBALS['is_create_db_priv'] = true; + $GLOBALS['is_reload_priv'] = true; + $GLOBALS['db_to_create'] = ''; + $GLOBALS['dbs_where_create_table_allowed'][] = '*'; + // @todo we should not break here, cause GRANT ALL *.* + // could be revoked by a later rule like GRANT SELECT ON db.* + break; + } else { + // this array may contain wildcards + $GLOBALS['dbs_where_create_table_allowed'][] = $show_grants_dbname; + + $dbname_to_test = Util::backquote($show_grants_dbname); + + if ($GLOBALS['is_create_db_priv']) { + // no need for any more tests if we already know this + continue; + } + + // does this db exist? + if ((preg_match('/' . $re0 . '%|_/', $show_grants_dbname) + && ! preg_match('/\\\\%|\\\\_/', $show_grants_dbname)) + || (! $GLOBALS['dbi']->tryQuery( + 'USE ' . preg_replace( + '/' . $re1 . '(%|_)/', '\\1\\3', $dbname_to_test + ) + ) + && mb_substr($GLOBALS['dbi']->getError(), 1, 4) != 1044) + ) { + /** + * Do not handle the underscore wildcard + * (this case must be rare anyway) + */ + $GLOBALS['db_to_create'] = preg_replace( + '/' . $re0 . '%/', '\\1', + $show_grants_dbname + ); + $GLOBALS['db_to_create'] = preg_replace( + '/' . $re1 . '(%|_)/', '\\1\\3', + $GLOBALS['db_to_create'] + ); + $GLOBALS['is_create_db_priv'] = true; + + /** + * @todo collect $GLOBALS['db_to_create'] into an array, + * to display a drop-down in the "Create database" dialog + */ + // we don't break, we want all possible databases + //break; + } // end if + } // end elseif + } // end if + + } // end while + + $GLOBALS['dbi']->freeResult($rs_usr); + + // must also cacheUnset() them in + // PhpMyAdmin\Plugins\Auth\AuthenticationCookie + Util::cacheSet('is_create_db_priv', $GLOBALS['is_create_db_priv']); + Util::cacheSet('is_reload_priv', $GLOBALS['is_reload_priv']); + Util::cacheSet('db_to_create', $GLOBALS['db_to_create']); + Util::cacheSet( + 'dbs_where_create_table_allowed', + $GLOBALS['dbs_where_create_table_allowed'] + ); + Util::cacheSet('dbs_to_test', $GLOBALS['dbs_to_test']); + + Util::cacheSet('proc_priv', $GLOBALS['proc_priv']); + Util::cacheSet('table_priv', $GLOBALS['table_priv']); + Util::cacheSet('col_priv', $GLOBALS['col_priv']); + Util::cacheSet('db_priv', $GLOBALS['db_priv']); + } // end function +} diff --git a/libraries/classes/Controllers/Server/ServerDatabasesController.php b/libraries/classes/Controllers/Server/ServerDatabasesController.php index 456b29fbae..97f6fd7ffd 100644 --- a/libraries/classes/Controllers/Server/ServerDatabasesController.php +++ b/libraries/classes/Controllers/Server/ServerDatabasesController.php @@ -57,7 +57,7 @@ class ServerDatabasesController extends Controller */ public function indexAction() { - include_once 'libraries/check_user_privileges.lib.php'; + include_once 'libraries/check_user_privileges.inc.php'; $response = Response::getInstance(); diff --git a/libraries/classes/Controllers/Table/TableStructureController.php b/libraries/classes/Controllers/Table/TableStructureController.php index 95438221df..e5110a78ad 100644 --- a/libraries/classes/Controllers/Table/TableStructureController.php +++ b/libraries/classes/Controllers/Table/TableStructureController.php @@ -112,7 +112,7 @@ class TableStructureController extends TableController /** * Function implementations for this script */ - include_once 'libraries/check_user_privileges.lib.php'; + include_once 'libraries/check_user_privileges.inc.php'; $this->response->getHeader()->getScripts()->addFiles( array( @@ -512,7 +512,7 @@ class TableStructureController extends TableController /** * Form for changing properties. */ - include_once 'libraries/check_user_privileges.lib.php'; + include_once 'libraries/check_user_privileges.inc.php'; include 'libraries/tbl_columns_definition_form.inc.php'; } diff --git a/libraries/classes/Display/CreateTable.php b/libraries/classes/Display/CreateTable.php index 649083b44b..5114d6292c 100644 --- a/libraries/classes/Display/CreateTable.php +++ b/libraries/classes/Display/CreateTable.php @@ -28,7 +28,7 @@ namespace PhpMyAdmin\Display; use PhpMyAdmin\Template; -require_once './libraries/check_user_privileges.lib.php'; +require_once './libraries/check_user_privileges.inc.php'; /** * PhpMyAdmin\Display\CreateTable class diff --git a/libraries/classes/Import.php b/libraries/classes/Import.php index fad14cdf00..579836a956 100644 --- a/libraries/classes/Import.php +++ b/libraries/classes/Import.php @@ -24,7 +24,7 @@ use PhpMyAdmin\Util; * We need to know something about user */ $GLOBALS['cfg']['Server']['DisableIS'] = false; -require_once './libraries/check_user_privileges.lib.php'; +require_once './libraries/check_user_privileges.inc.php'; /** * PhpMyAdmin\Import class diff --git a/libraries/classes/ListDatabase.php b/libraries/classes/ListDatabase.php index dc2d36b2ca..1cf8520ed8 100644 --- a/libraries/classes/ListDatabase.php +++ b/libraries/classes/ListDatabase.php @@ -10,7 +10,7 @@ namespace PhpMyAdmin; use PhpMyAdmin\ListAbstract; use PhpMyAdmin\Util; -require_once './libraries/check_user_privileges.lib.php'; +require_once './libraries/check_user_privileges.inc.php'; /** * handles database lists diff --git a/libraries/classes/Navigation/NavigationTree.php b/libraries/classes/Navigation/NavigationTree.php index e1aa71fb7d..3bd55539af 100644 --- a/libraries/classes/Navigation/NavigationTree.php +++ b/libraries/classes/Navigation/NavigationTree.php @@ -17,7 +17,7 @@ use PhpMyAdmin\Response; use PhpMyAdmin\Util; use PhpMyAdmin\Url; -require_once 'libraries/check_user_privileges.lib.php'; +require_once 'libraries/check_user_privileges.inc.php'; /** * Displays a collapsible of database objects in the navigation frame diff --git a/libraries/classes/Navigation/Nodes/NodeDatabaseContainer.php b/libraries/classes/Navigation/Nodes/NodeDatabaseContainer.php index d5a08d9a85..fd6859a839 100644 --- a/libraries/classes/Navigation/Nodes/NodeDatabaseContainer.php +++ b/libraries/classes/Navigation/Nodes/NodeDatabaseContainer.php @@ -10,7 +10,7 @@ namespace PhpMyAdmin\Navigation\Nodes; use PhpMyAdmin\Navigation\NodeFactory; use PhpMyAdmin\Util; -require_once './libraries/check_user_privileges.lib.php'; +require_once './libraries/check_user_privileges.inc.php'; /** * Represents a container for database nodes in the navigation tree diff --git a/server_privileges.php b/server_privileges.php index 767a2b21f8..7643365c08 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -22,7 +22,7 @@ require_once 'libraries/common.inc.php'; /** * functions implementation for this script */ -require_once 'libraries/check_user_privileges.lib.php'; +require_once 'libraries/check_user_privileges.inc.php'; $cfgRelation = Relation::getRelationsParam(); diff --git a/sql.php b/sql.php index 685dc5e2ec..7867577f52 100644 --- a/sql.php +++ b/sql.php @@ -17,7 +17,7 @@ use PhpMyAdmin\Util; * Gets some core libraries */ require_once 'libraries/common.inc.php'; -require_once 'libraries/check_user_privileges.lib.php'; +require_once 'libraries/check_user_privileges.inc.php'; PageSettings::showGroup('Browse'); diff --git a/tbl_operations.php b/tbl_operations.php index 9738fb262d..36f6868239 100644 --- a/tbl_operations.php +++ b/tbl_operations.php @@ -22,7 +22,7 @@ require_once 'libraries/common.inc.php'; /** * functions implementation for this script */ -require_once 'libraries/check_user_privileges.lib.php'; +require_once 'libraries/check_user_privileges.inc.php'; $pma_table = new Table($GLOBALS['table'], $GLOBALS['db']); diff --git a/test/libraries/PMA_check_user_privileges_test.php b/test/classes/CheckUserPrivilegesTest.php similarity index 86% rename from test/libraries/PMA_check_user_privileges_test.php rename to test/classes/CheckUserPrivilegesTest.php index 0c6d827ce8..d7bc41cbfb 100644 --- a/test/libraries/PMA_check_user_privileges_test.php +++ b/test/classes/CheckUserPrivilegesTest.php @@ -1,27 +1,27 @@ Date: Mon, 11 Sep 2017 20:11:26 -0300 Subject: [PATCH 2/4] Refactor browse_foreigners functions to static methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- browse_foreigners.php | 7 +- libraries/browse_foreigners.lib.php | 341 ----------------- libraries/classes/BrowseForeigners.php | 352 ++++++++++++++++++ .../BrowseForeignersTest.php} | 60 ++- 4 files changed, 385 insertions(+), 375 deletions(-) delete mode 100644 libraries/browse_foreigners.lib.php create mode 100644 libraries/classes/BrowseForeigners.php rename test/{libraries/PMA_browse_foreigners_test.php => classes/BrowseForeignersTest.php} (79%) diff --git a/browse_foreigners.php b/browse_foreigners.php index b7c194f2e7..2a4dd24566 100644 --- a/browse_foreigners.php +++ b/browse_foreigners.php @@ -5,12 +5,13 @@ * * @package PhpMyAdmin */ + +use PhpMyAdmin\BrowseForeigners; use PhpMyAdmin\Relation; use PhpMyAdmin\Response; use PhpMyAdmin\Util; require_once 'libraries/common.inc.php'; -require_once 'libraries/browse_foreigners.lib.php'; /** * Sets globals from $_REQUEST @@ -39,7 +40,7 @@ $header->setBodyId('body_browse_foreigners'); */ $foreigners = Relation::getForeigners($db, $table); -$foreign_limit = PMA_getForeignLimit( +$foreign_limit = BrowseForeigners::getForeignLimit( isset($_REQUEST['foreign_showAll']) ? $_REQUEST['foreign_showAll'] : null ); @@ -53,7 +54,7 @@ $foreignData = Relation::getForeignData( ); // HTML output -$html = PMA_getHtmlForRelationalFieldSelection( +$html = BrowseForeigners::getHtmlForRelationalFieldSelection( $db, $table, $_REQUEST['field'], $foreignData, isset($fieldkey) ? $fieldkey : null, isset($data) ? $data : null diff --git a/libraries/browse_foreigners.lib.php b/libraries/browse_foreigners.lib.php deleted file mode 100644 index cc24abdced..0000000000 --- a/libraries/browse_foreigners.lib.php +++ /dev/null @@ -1,341 +0,0 @@ - 0 - && $horizontal_count > $GLOBALS['cfg']['RepeatCells'] - ) { - $output .= $header; - $horizontal_count = 0; - } - - // key names and descriptions for the left section, - // sorted by key names - $leftKeyname = $keys[$indexByKeyname]; - list( - $leftDescription, - $leftDescriptionTitle - ) = PMA_getDescriptionAndTitle($descriptions[$indexByKeyname]); - - // key names and descriptions for the right section, - // sorted by descriptions - $rightKeyname = $keys[$indexByDescription]; - list( - $rightDescription, - $rightDescriptionTitle - ) = PMA_getDescriptionAndTitle($descriptions[$indexByDescription]); - - $indexByDescription++; - - if (! empty($current_value)) { - $rightKeynameIsSelected = $rightKeyname == $current_value; - $leftKeynameIsSelected = $leftKeyname == $current_value; - } - - $output .= ''; - - $output .= PMA_getHtmlForColumnElement( - 'class="nowrap"', $leftKeynameIsSelected, - $leftKeyname, $leftDescription, - $leftDescriptionTitle - ); - - $output .= PMA_getHtmlForColumnElement( - '', $leftKeynameIsSelected, $leftKeyname, - $leftDescription, $leftDescriptionTitle - ); - - $output .= '' - . ''; - - $output .= PMA_getHtmlForColumnElement( - '', $rightKeynameIsSelected, $rightKeyname, - $rightDescription, $rightDescriptionTitle - ); - - $output .= PMA_getHtmlForColumnElement( - 'class="nowrap"', $rightKeynameIsSelected, - $rightKeyname, $rightDescription, - $rightDescriptionTitle - ); - $output .= ''; - - return array($output, $horizontal_count, $indexByDescription); -} - -/** - * Function to get html for relational field selection - * - * @param string $db current database - * @param string $table current table - * @param string $field field - * @param array $foreignData foreign column data - * @param string $fieldkey field key - * @param string $current_value current columns's value - * - * @return string - */ -function PMA_getHtmlForRelationalFieldSelection($db, $table, $field, $foreignData, - $fieldkey, $current_value -) { - $gotopage = PMA_getHtmlForGotoPage($foreignData); - $showall = PMA_getHtmlForShowAll($foreignData); - - $output = '
' - . '
' - . Url::getHiddenInputs($db, $table) - . '' - . ''; - - if (isset($_REQUEST['rownumber'])) { - $output .= ''; - } - $filter_value = (isset($_REQUEST['foreign_filter']) - ? htmlspecialchars($_REQUEST['foreign_filter']) - : ''); - $output .= '' - . '' - . '' - . '' - . '' - . '' . $gotopage . '' - . '' . $showall . '' - . '
' - . '
'; - - $output .= ''; - - if (!is_array($foreignData['disp_row'])) { - $output .= '' - . '
'; - - return $output; - } - - $header = ' - ' . __('Keyname') . ' - ' . __('Description') . ' - - ' . __('Description') . ' - ' . __('Keyname') . ' - '; - - $output .= '' . $header . '' . "\n" - . '' . $header . '' . "\n" - . '' . "\n"; - - $descriptions = array(); - $keys = array(); - foreach ($foreignData['disp_row'] as $relrow) { - if ($foreignData['foreign_display'] != false) { - $descriptions[] = $relrow[$foreignData['foreign_display']]; - } else { - $descriptions[] = ''; - } - - $keys[] = $relrow[$foreignData['foreign_field']]; - } - - asort($keys); - - $horizontal_count = 0; - $indexByDescription = 0; - - foreach ($keys as $indexByKeyname => $value) { - list( - $html, - $horizontal_count, - $indexByDescription - ) = PMA_getHtmlForOneKey( - $horizontal_count, $header, $keys, $indexByKeyname, - $descriptions, $indexByDescription, $current_value - ); - $output .= $html; - } - - $output .= '' - . ''; - - return $output; -} - -/** - * Get the description (possibly truncated) and the title - * - * @param string $description the key name's description - * - * @return array the new description and title - */ -function PMA_getDescriptionAndTitle($description) -{ - $limitChars = $GLOBALS['cfg']['LimitChars']; - if (mb_strlen($description) <= $limitChars) { - $description = htmlspecialchars( - $description - ); - $descriptionTitle = ''; - } else { - $descriptionTitle = htmlspecialchars( - $description - ); - $description = htmlspecialchars( - mb_substr( - $description, 0, $limitChars - ) - . '...' - ); - } - return array($description, $descriptionTitle); -} - -/** - * Function to get html for each column element - * - * @param string $cssClass class="nowrap" or '' - * @param bool $isSelected whether current equals form's value - * @param string $keyname current key - * @param string $description current value - * @param string $title current title - * - * @return string - */ -function PMA_getHtmlForColumnElement($cssClass, $isSelected, $keyname, - $description, $title -) { - $keyname = htmlspecialchars($keyname); - $output = '' : '') - . ''; - if ($cssClass !== '') { - $output .= $keyname; - } else { - $output .= $description; - } - - $output .= '' . ($isSelected ? '' : '') . ''; - - return $output; -} - -/** - * Function to get html for show all case - * - * @param array $foreignData foreign data - * - * @return string - */ -function PMA_getHtmlForShowAll($foreignData) -{ - $showall = ''; - if (is_array($foreignData['disp_row'])) { - if ($GLOBALS['cfg']['ShowAll'] - && ($foreignData['the_total'] > $GLOBALS['cfg']['MaxRows']) - ) { - $showall = ''; - } - } - - return $showall; -} - -/** - * Function to get html for the goto page option - * - * @param array $foreignData foreign data - * - * @return string - */ -function PMA_getHtmlForGotoPage($foreignData) -{ - $gotopage = ''; - isset($_REQUEST['pos']) ? $pos = $_REQUEST['pos'] : $pos = 0; - if (!is_array($foreignData['disp_row'])) { - return $gotopage; - } - - $session_max_rows = $GLOBALS['cfg']['MaxRows']; - $pageNow = @floor($pos / $session_max_rows) + 1; - $nbTotalPage = @ceil($foreignData['the_total'] / $session_max_rows); - - if ($foreignData['the_total'] > $GLOBALS['cfg']['MaxRows']) { - $gotopage = PhpMyAdmin\Util::pageselector( - 'pos', - $session_max_rows, - $pageNow, - $nbTotalPage, - 200, - 5, - 5, - 20, - 10, - __('Page number:') - ); - } - - return $gotopage; -} - -/** - * Function to get foreign limit - * - * @param string $foreign_showAll foreign navigation - * - * @return string - */ -function PMA_getForeignLimit($foreign_showAll) -{ - if (isset($foreign_showAll) && $foreign_showAll == __('Show all')) { - return null; - } - isset($_REQUEST['pos']) ? $pos = $_REQUEST['pos'] : $pos = 0; - return 'LIMIT ' . $pos . ', ' . intval($GLOBALS['cfg']['MaxRows']) . ' '; -} diff --git a/libraries/classes/BrowseForeigners.php b/libraries/classes/BrowseForeigners.php new file mode 100644 index 0000000000..a24dea96ec --- /dev/null +++ b/libraries/classes/BrowseForeigners.php @@ -0,0 +1,352 @@ + 0 + && $horizontal_count > $GLOBALS['cfg']['RepeatCells'] + ) { + $output .= $header; + $horizontal_count = 0; + } + + // key names and descriptions for the left section, + // sorted by key names + $leftKeyname = $keys[$indexByKeyname]; + list( + $leftDescription, + $leftDescriptionTitle + ) = self::getDescriptionAndTitle($descriptions[$indexByKeyname]); + + // key names and descriptions for the right section, + // sorted by descriptions + $rightKeyname = $keys[$indexByDescription]; + list( + $rightDescription, + $rightDescriptionTitle + ) = self::getDescriptionAndTitle($descriptions[$indexByDescription]); + + $indexByDescription++; + + if (! empty($current_value)) { + $rightKeynameIsSelected = $rightKeyname == $current_value; + $leftKeynameIsSelected = $leftKeyname == $current_value; + } + + $output .= ''; + + $output .= self::getHtmlForColumnElement( + 'class="nowrap"', $leftKeynameIsSelected, + $leftKeyname, $leftDescription, + $leftDescriptionTitle + ); + + $output .= self::getHtmlForColumnElement( + '', $leftKeynameIsSelected, $leftKeyname, + $leftDescription, $leftDescriptionTitle + ); + + $output .= '' + . ''; + + $output .= self::getHtmlForColumnElement( + '', $rightKeynameIsSelected, $rightKeyname, + $rightDescription, $rightDescriptionTitle + ); + + $output .= self::getHtmlForColumnElement( + 'class="nowrap"', $rightKeynameIsSelected, + $rightKeyname, $rightDescription, + $rightDescriptionTitle + ); + $output .= ''; + + return array($output, $horizontal_count, $indexByDescription); + } + + /** + * Function to get html for relational field selection + * + * @param string $db current database + * @param string $table current table + * @param string $field field + * @param array $foreignData foreign column data + * @param string $fieldkey field key + * @param string $current_value current columns's value + * + * @return string + */ + public static function getHtmlForRelationalFieldSelection($db, $table, $field, $foreignData, + $fieldkey, $current_value + ) { + $gotopage = self::getHtmlForGotoPage($foreignData); + $showall = self::getHtmlForShowAll($foreignData); + + $output = '
' + . '
' + . Url::getHiddenInputs($db, $table) + . '' + . ''; + + if (isset($_REQUEST['rownumber'])) { + $output .= ''; + } + $filter_value = (isset($_REQUEST['foreign_filter']) + ? htmlspecialchars($_REQUEST['foreign_filter']) + : ''); + $output .= '' + . '' + . '' + . '' + . '' + . '' . $gotopage . '' + . '' . $showall . '' + . '
' + . '
'; + + $output .= ''; + + if (!is_array($foreignData['disp_row'])) { + $output .= '' + . '
'; + + return $output; + } + + $header = ' + ' . __('Keyname') . ' + ' . __('Description') . ' + + ' . __('Description') . ' + ' . __('Keyname') . ' + '; + + $output .= '' . $header . '' . "\n" + . '' . $header . '' . "\n" + . '' . "\n"; + + $descriptions = array(); + $keys = array(); + foreach ($foreignData['disp_row'] as $relrow) { + if ($foreignData['foreign_display'] != false) { + $descriptions[] = $relrow[$foreignData['foreign_display']]; + } else { + $descriptions[] = ''; + } + + $keys[] = $relrow[$foreignData['foreign_field']]; + } + + asort($keys); + + $horizontal_count = 0; + $indexByDescription = 0; + + foreach ($keys as $indexByKeyname => $value) { + list( + $html, + $horizontal_count, + $indexByDescription + ) = self::getHtmlForOneKey( + $horizontal_count, $header, $keys, $indexByKeyname, + $descriptions, $indexByDescription, $current_value + ); + $output .= $html; + } + + $output .= '' + . ''; + + return $output; + } + + /** + * Get the description (possibly truncated) and the title + * + * @param string $description the key name's description + * + * @return array the new description and title + */ + public static function getDescriptionAndTitle($description) + { + $limitChars = $GLOBALS['cfg']['LimitChars']; + if (mb_strlen($description) <= $limitChars) { + $description = htmlspecialchars( + $description + ); + $descriptionTitle = ''; + } else { + $descriptionTitle = htmlspecialchars( + $description + ); + $description = htmlspecialchars( + mb_substr( + $description, 0, $limitChars + ) + . '...' + ); + } + return array($description, $descriptionTitle); + } + + /** + * Function to get html for each column element + * + * @param string $cssClass class="nowrap" or '' + * @param bool $isSelected whether current equals form's value + * @param string $keyname current key + * @param string $description current value + * @param string $title current title + * + * @return string + */ + public static function getHtmlForColumnElement($cssClass, $isSelected, $keyname, + $description, $title + ) { + $keyname = htmlspecialchars($keyname); + $output = '' : '') + . ''; + if ($cssClass !== '') { + $output .= $keyname; + } else { + $output .= $description; + } + + $output .= '' . ($isSelected ? '' : '') . ''; + + return $output; + } + + /** + * Function to get html for show all case + * + * @param array $foreignData foreign data + * + * @return string + */ + public static function getHtmlForShowAll($foreignData) + { + $showall = ''; + if (is_array($foreignData['disp_row'])) { + if ($GLOBALS['cfg']['ShowAll'] + && ($foreignData['the_total'] > $GLOBALS['cfg']['MaxRows']) + ) { + $showall = ''; + } + } + + return $showall; + } + + /** + * Function to get html for the goto page option + * + * @param array $foreignData foreign data + * + * @return string + */ + public static function getHtmlForGotoPage($foreignData) + { + $gotopage = ''; + isset($_REQUEST['pos']) ? $pos = $_REQUEST['pos'] : $pos = 0; + if (!is_array($foreignData['disp_row'])) { + return $gotopage; + } + + $session_max_rows = $GLOBALS['cfg']['MaxRows']; + $pageNow = @floor($pos / $session_max_rows) + 1; + $nbTotalPage = @ceil($foreignData['the_total'] / $session_max_rows); + + if ($foreignData['the_total'] > $GLOBALS['cfg']['MaxRows']) { + $gotopage = Util::pageselector( + 'pos', + $session_max_rows, + $pageNow, + $nbTotalPage, + 200, + 5, + 5, + 20, + 10, + __('Page number:') + ); + } + + return $gotopage; + } + + /** + * Function to get foreign limit + * + * @param string $foreign_showAll foreign navigation + * + * @return string + */ + public static function getForeignLimit($foreign_showAll) + { + if (isset($foreign_showAll) && $foreign_showAll == __('Show all')) { + return null; + } + isset($_REQUEST['pos']) ? $pos = $_REQUEST['pos'] : $pos = 0; + return 'LIMIT ' . $pos . ', ' . intval($GLOBALS['cfg']['MaxRows']) . ' '; + } +} diff --git a/test/libraries/PMA_browse_foreigners_test.php b/test/classes/BrowseForeignersTest.php similarity index 79% rename from test/libraries/PMA_browse_foreigners_test.php rename to test/classes/BrowseForeignersTest.php index 3d1917b331..999adada4e 100644 --- a/test/libraries/PMA_browse_foreigners_test.php +++ b/test/classes/BrowseForeignersTest.php @@ -1,22 +1,20 @@ assertNull( - PMA_getForeignLimit('Show all') + BrowseForeigners::getForeignLimit('Show all') ); $this->assertEquals( 'LIMIT 0, 25 ', - PMA_getForeignLimit(null) + BrowseForeigners::getForeignLimit(null) ); $_REQUEST['pos'] = 10; $this->assertEquals( 'LIMIT 10, 25 ', - PMA_getForeignLimit(null) + BrowseForeigners::getForeignLimit(null) ); $GLOBALS['cfg']['MaxRows'] = 50; $this->assertEquals( 'LIMIT 10, 50 ', - PMA_getForeignLimit(null) + BrowseForeigners::getForeignLimit(null) ); $this->assertEquals( 'LIMIT 10, 50 ', - PMA_getForeignLimit('xyz') + BrowseForeigners::getForeignLimit('xyz') ); } /** - * Test for PMA_getHtmlForShowAll + * Test for BrowseForeigners::getHtmlForShowAll * * @return void */ @@ -73,7 +71,7 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase { $this->assertEquals( '', - PMA_getHtmlForShowAll(null) + BrowseForeigners::getHtmlForShowAll(null) ); $foreignData = array(); @@ -82,7 +80,7 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase $this->assertEquals( '', - PMA_getHtmlForShowAll($foreignData) + BrowseForeigners::getHtmlForShowAll($foreignData) ); $GLOBALS['cfg']['ShowAll'] = true; @@ -90,7 +88,7 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase $this->assertEquals( '', - PMA_getHtmlForShowAll($foreignData) + BrowseForeigners::getHtmlForShowAll($foreignData) ); $foreignData['the_total'] = 30; @@ -99,12 +97,12 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase '', - PMA_getHtmlForShowAll($foreignData) + BrowseForeigners::getHtmlForShowAll($foreignData) ); } /** - * Test for PMA_getHtmlForGotoPage + * Test for BrowseForeigners::getHtmlForGotoPage * * @return void */ @@ -112,7 +110,7 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase { $this->assertEquals( '', - PMA_getHtmlForGotoPage(null) + BrowseForeigners::getHtmlForGotoPage(null) ); $_REQUEST['pos'] = 15; @@ -122,11 +120,11 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase $this->assertEquals( '', - PMA_getHtmlForGotoPage($foreignData) + BrowseForeigners::getHtmlForGotoPage($foreignData) ); $foreignData['the_total'] = 30; - $result = PMA_getHtmlForGotoPage($foreignData); + $result = BrowseForeigners::getHtmlForGotoPage($foreignData); $this->assertStringStartsWith( 'Page number:', @@ -156,7 +154,7 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase } /** - * Test for PMA_getHtmlForColumnElement + * Test for BrowseForeigners::getHtmlForColumnElement * * @return void */ @@ -167,7 +165,7 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase $keyname = ''; $description = 'foo'; $title = ''; - $result = PMA_getHtmlForColumnElement( + $result = BrowseForeigners::getHtmlForColumnElement( $cssClass, $isSelected, $keyname, $description, $title ); @@ -187,7 +185,7 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase $isSelected = true; $keyname = 'bar'; $title = 'foo'; - $result = PMA_getHtmlForColumnElement( + $result = BrowseForeigners::getHtmlForColumnElement( $cssClass, $isSelected, $keyname, $description, $title ); @@ -210,7 +208,7 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase } /** - * Test for PMA_getDescriptionAndTitle + * Test for BrowseForeigners::getDescriptionAndTitle * * @return void */ @@ -221,19 +219,19 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase $this->assertEquals( array('foobar<baz', ''), - PMA_getDescriptionAndTitle($desc) + BrowseForeigners::getDescriptionAndTitle($desc) ); $GLOBALS['cfg']['LimitChars'] = 5; $this->assertEquals( array('fooba...', 'foobar<baz'), - PMA_getDescriptionAndTitle($desc) + BrowseForeigners::getDescriptionAndTitle($desc) ); } /** - * Test for PMA_getHtmlForRelationalFieldSelection + * Test for BrowseForeigners::getHtmlForRelationalFieldSelection * * @return void */ @@ -248,7 +246,7 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase $current_value = ''; $_REQUEST['rownumber'] = 1; $_REQUEST['foreign_filter'] = '5'; - $result = PMA_getHtmlForRelationalFieldSelection( + $result = BrowseForeigners::getHtmlForRelationalFieldSelection( $db, $table, $field, $foreignData, $fieldkey, $current_value ); @@ -315,7 +313,7 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase $foreignData['disp_row'] = array(); $foreignData['the_total'] = 5; $GLOBALS['cfg']['ShowAll'] = false; - $result = PMA_getHtmlForRelationalFieldSelection( + $result = BrowseForeigners::getHtmlForRelationalFieldSelection( $db, $table, $field, $foreignData, $fieldkey, $current_value ); From 360d0ad62e490c15c6892f1696744fe7fa27ee63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Mon, 11 Sep 2017 22:43:07 -0300 Subject: [PATCH 3/4] Refactor create_addfield functions to static methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- .../Table/TableStructureController.php | 5 +- libraries/classes/CreateAddField.php | 505 ++++++++++++++++++ libraries/create_addfield.lib.php | 495 ----------------- tbl_addfield.php | 5 +- tbl_create.php | 15 +- 5 files changed, 517 insertions(+), 508 deletions(-) create mode 100644 libraries/classes/CreateAddField.php delete mode 100644 libraries/create_addfield.lib.php diff --git a/libraries/classes/Controllers/Table/TableStructureController.php b/libraries/classes/Controllers/Table/TableStructureController.php index e5110a78ad..3f85a95791 100644 --- a/libraries/classes/Controllers/Table/TableStructureController.php +++ b/libraries/classes/Controllers/Table/TableStructureController.php @@ -11,6 +11,7 @@ use PhpMyAdmin\CentralColumns; use PhpMyAdmin\Config\PageSettings; use PhpMyAdmin\Controllers\TableController; use PhpMyAdmin\Core; +use PhpMyAdmin\CreateAddField; use PhpMyAdmin\Index; use PhpMyAdmin\Message; use PhpMyAdmin\Relation; @@ -710,10 +711,8 @@ class TableStructureController extends TableController */ protected function updatePartitioning() { - include_once 'libraries/create_addfield.lib.php'; - $sql_query = "ALTER TABLE " . Util::backquote($this->table) . " " - . PMA_getPartitionsDefinition(); + . CreateAddField::getPartitionsDefinition(); // Execute alter query $result = $this->dbi->tryQuery($sql_query); diff --git a/libraries/classes/CreateAddField.php b/libraries/classes/CreateAddField.php new file mode 100644 index 0000000000..e7a9424f09 --- /dev/null +++ b/libraries/classes/CreateAddField.php @@ -0,0 +1,505 @@ + $column) { + $index_fields[$key] = Util::backquote( + $_REQUEST['field_name'][$column['col_index']] + ); + if ($column['size']) { + $index_fields[$key] .= '(' . $column['size'] . ')'; + } + } // end while + + $sql_query .= ' (' . implode(', ', $index_fields) . ')'; + + $keyBlockSizes = $index['Key_block_size']; + if (! empty($keyBlockSizes)) { + $sql_query .= " KEY_BLOCK_SIZE = " + . $GLOBALS['dbi']->escapeString($keyBlockSizes); + } + + // specifying index type is allowed only for primary, unique and index only + $type = $index['Index_type']; + if ($index['Index_choice'] != 'SPATIAL' + && $index['Index_choice'] != 'FULLTEXT' + && in_array($type, Index::getIndexTypes()) + ) { + $sql_query .= ' USING ' . $type; + } + + $parser = $index['Parser']; + if ($index['Index_choice'] == 'FULLTEXT' && ! empty($parser)) { + $sql_query .= " WITH PARSER " . $GLOBALS['dbi']->escapeString($parser); + } + + $comment = $index['Index_comment']; + if (! empty($comment)) { + $sql_query .= " COMMENT '" . $GLOBALS['dbi']->escapeString($comment) + . "'"; + } + + $statement[] = $sql_query; + + return $statement; + } + + /** + * Statement prefix for the self::buildColumnCreationStatement() + * + * @param boolean $is_create_tbl true if requirement is to get the statement + * for table creation + * + * @return string $sql_prefix prefix + */ + public static function getStatementPrefix($is_create_tbl = true) + { + $sql_prefix = " "; + if (! $is_create_tbl) { + $sql_prefix = ' ADD '; + } + return $sql_prefix; + } + + /** + * Merge index definitions for one type of index + * + * @param array $definitions the index definitions to merge to + * @param boolean $is_create_tbl true if requirement is to get the statement + * for table creation + * @param array $indexed_columns the columns for one type of index + * @param string $index_keyword the index keyword to use in the definition + * + * @return array $index_definitions + */ + public static function mergeIndexStatements( + $definitions, $is_create_tbl, $indexed_columns, $index_keyword + ) { + foreach ($indexed_columns as $index) { + $statements = self::buildIndexStatements( + $index, " " . $index_keyword . " ", $is_create_tbl + ); + $definitions = array_merge($definitions, $statements); + } + return $definitions; + } + + /** + * Returns sql statement according to the column and index specifications as + * requested + * + * @param boolean $is_create_tbl true if requirement is to get the statement + * for table creation + * + * @return string sql statement + */ + public static function getColumnCreationStatements($is_create_tbl = true) + { + $sql_statement = ""; + list($field_cnt, $field_primary, $field_index, + $field_unique, $field_fulltext, $field_spatial + ) = self::getIndexedColumns(); + $definitions = self::buildColumnCreationStatement( + $field_cnt, $is_create_tbl + ); + + // Builds the PRIMARY KEY statements + $primary_key_statements = self::buildIndexStatements( + isset($field_primary[0]) ? $field_primary[0] : array(), + " PRIMARY KEY ", + $is_create_tbl + ); + $definitions = array_merge($definitions, $primary_key_statements); + + // Builds the INDEX statements + $definitions = self::mergeIndexStatements( + $definitions, $is_create_tbl, $field_index, "INDEX" + ); + + // Builds the UNIQUE statements + $definitions = self::mergeIndexStatements( + $definitions, $is_create_tbl, $field_unique, "UNIQUE" + ); + + // Builds the FULLTEXT statements + $definitions = self::mergeIndexStatements( + $definitions, $is_create_tbl, $field_fulltext, "FULLTEXT" + ); + + // Builds the SPATIAL statements + $definitions = self::mergeIndexStatements( + $definitions, $is_create_tbl, $field_spatial, "SPATIAL" + ); + + if (count($definitions)) { + $sql_statement = implode(', ', $definitions); + } + $sql_statement = preg_replace('@, $@', '', $sql_statement); + + return $sql_statement; + + } + + /** + * Returns the partitioning clause + * + * @return string partitioning clause + */ + public static function getPartitionsDefinition() + { + $sql_query = ""; + if (! empty($_REQUEST['partition_by']) + && ! empty($_REQUEST['partition_expr']) + && ! empty($_REQUEST['partition_count']) + && $_REQUEST['partition_count'] > 1 + ) { + $sql_query .= " PARTITION BY " . $_REQUEST['partition_by'] + . " (" . $_REQUEST['partition_expr'] . ")" + . " PARTITIONS " . $_REQUEST['partition_count']; + } + + if (! empty($_REQUEST['subpartition_by']) + && ! empty($_REQUEST['subpartition_expr']) + && ! empty($_REQUEST['subpartition_count']) + && $_REQUEST['subpartition_count'] > 1 + ) { + $sql_query .= " SUBPARTITION BY " . $_REQUEST['subpartition_by'] + . " (" . $_REQUEST['subpartition_expr'] . ")" + . " SUBPARTITIONS " . $_REQUEST['subpartition_count']; + } + + if (! empty($_REQUEST['partitions'])) { + $i = 0; + $partitions = array(); + foreach ($_REQUEST['partitions'] as $partition) { + $partitions[] = self::getPartitionDefinition($partition); + $i++; + } + $sql_query .= " (" . implode(", ", $partitions) . ")"; + } + + return $sql_query; + } + + /** + * Returns the definition of a partition/subpartition + * + * @param array $partition array of partition/subpartition detiails + * @param boolean $isSubPartition whether a subpartition + * + * @return string partition/subpartition definition + */ + public static function getPartitionDefinition($partition, $isSubPartition = false) + { + $sql_query = " " . ($isSubPartition ? "SUB" : "") . "PARTITION "; + $sql_query .= $partition['name']; + + if (! empty($partition['value_type'])) { + $sql_query .= " VALUES " . $partition['value_type']; + + if ($partition['value_type'] != 'LESS THAN MAXVALUE') { + $sql_query .= " (" . $partition['value'] . ")"; + } + } + + if (! empty($partition['engine'])) { + $sql_query .= " ENGINE = " . $partition['engine']; + } + if (! empty($partition['comment'])) { + $sql_query .= " COMMENT = '" . $partition['comment'] . "'"; + } + if (! empty($partition['data_directory'])) { + $sql_query .= " DATA DIRECTORY = '" . $partition['data_directory'] . "'"; + } + if (! empty($partition['index_directory'])) { + $sql_query .= " INDEX_DIRECTORY = '" . $partition['index_directory'] . "'"; + } + if (! empty($partition['max_rows'])) { + $sql_query .= " MAX_ROWS = " . $partition['max_rows']; + } + if (! empty($partition['min_rows'])) { + $sql_query .= " MIN_ROWS = " . $partition['min_rows']; + } + if (! empty($partition['tablespace'])) { + $sql_query .= " TABLESPACE = " . $partition['tablespace']; + } + if (! empty($partition['node_group'])) { + $sql_query .= " NODEGROUP = " . $partition['node_group']; + } + + if (! empty($partition['subpartitions'])) { + $j = 0; + $subpartitions = array(); + foreach ($partition['subpartitions'] as $subpartition) { + $subpartitions[] = self::getPartitionDefinition( + $subpartition, + true + ); + $j++; + } + $sql_query .= " (" . implode(", ", $subpartitions) . ")"; + } + + return $sql_query; + } + + /** + * Function to get table creation sql query + * + * @param string $db database name + * @param string $table table name + * + * @return string + */ + public static function getTableCreationQuery($db, $table) + { + // get column addition statements + $sql_statement = self::getColumnCreationStatements(true); + + // Builds the 'create table' statement + $sql_query = 'CREATE TABLE ' . Util::backquote($db) . '.' + . Util::backquote(trim($table)) . ' (' . $sql_statement . ')'; + + // Adds table type, character set, comments and partition definition + if (!empty($_REQUEST['tbl_storage_engine']) + && ($_REQUEST['tbl_storage_engine'] != 'Default') + ) { + $sql_query .= ' ENGINE = ' . $_REQUEST['tbl_storage_engine']; + } + if (!empty($_REQUEST['tbl_collation'])) { + $sql_query .= Util::getCharsetQueryPart($_REQUEST['tbl_collation']); + } + if (! empty($_REQUEST['connection']) + && ! empty($_REQUEST['tbl_storage_engine']) + && $_REQUEST['tbl_storage_engine'] == 'FEDERATED' + ) { + $sql_query .= " CONNECTION = '" + . $GLOBALS['dbi']->escapeString($_REQUEST['connection']) . "'"; + } + if (!empty($_REQUEST['comment'])) { + $sql_query .= ' COMMENT = \'' + . $GLOBALS['dbi']->escapeString($_REQUEST['comment']) . '\''; + } + $sql_query .= self::getPartitionsDefinition(); + $sql_query .= ';'; + + return $sql_query; + } + + /** + * Function to get the number of fields for the table creation form + * + * @return int + */ + public static function getNumberOfFieldsFromRequest() + { + if (isset($_REQUEST['submit_num_fields'])) { // adding new fields + $num_fields = intval($_REQUEST['orig_num_fields']) + intval($_REQUEST['added_fields']); + } elseif (isset($_REQUEST['orig_num_fields'])) { // retaining existing fields + $num_fields = intval($_REQUEST['orig_num_fields']); + } elseif (isset($_REQUEST['num_fields']) + && intval($_REQUEST['num_fields']) > 0 + ) { // new table with specified number of fields + $num_fields = intval($_REQUEST['num_fields']); + } else { // new table with unspecified number of fields + $num_fields = 4; + } + + // Limit to 4096 fields (MySQL maximal value) + return min($num_fields, 4096); + } + + /** + * Function to execute the column creation statement + * + * @param string $db current database + * @param string $table current table + * @param string $err_url error page url + * + * @return array + */ + public static function tryColumnCreationQuery($db, $table, $err_url) + { + // get column addition statements + $sql_statement = self::getColumnCreationStatements(false); + + // To allow replication, we first select the db to use and then run queries + // on this db. + if (!($GLOBALS['dbi']->selectDb($db))) { + Util::mysqlDie( + $GLOBALS['dbi']->getError(), + 'USE ' . Util::backquote($db), false, + $err_url + ); + } + $sql_query = 'ALTER TABLE ' . + Util::backquote($table) . ' ' . $sql_statement . ';'; + // If there is a request for SQL previewing. + if (isset($_REQUEST['preview_sql'])) { + Core::previewSQL($sql_query); + } + return array($GLOBALS['dbi']->tryQuery($sql_query) , $sql_query); + } +} diff --git a/libraries/create_addfield.lib.php b/libraries/create_addfield.lib.php deleted file mode 100644 index 2c43ef3878..0000000000 --- a/libraries/create_addfield.lib.php +++ /dev/null @@ -1,495 +0,0 @@ - $column) { - $index_fields[$key] = PhpMyAdmin\Util::backquote( - $_REQUEST['field_name'][$column['col_index']] - ); - if ($column['size']) { - $index_fields[$key] .= '(' . $column['size'] . ')'; - } - } // end while - - $sql_query .= ' (' . implode(', ', $index_fields) . ')'; - - $keyBlockSizes = $index['Key_block_size']; - if (! empty($keyBlockSizes)) { - $sql_query .= " KEY_BLOCK_SIZE = " - . $GLOBALS['dbi']->escapeString($keyBlockSizes); - } - - // specifying index type is allowed only for primary, unique and index only - $type = $index['Index_type']; - if ($index['Index_choice'] != 'SPATIAL' - && $index['Index_choice'] != 'FULLTEXT' - && in_array($type, PhpMyAdmin\Index::getIndexTypes()) - ) { - $sql_query .= ' USING ' . $type; - } - - $parser = $index['Parser']; - if ($index['Index_choice'] == 'FULLTEXT' && ! empty($parser)) { - $sql_query .= " WITH PARSER " . $GLOBALS['dbi']->escapeString($parser); - } - - $comment = $index['Index_comment']; - if (! empty($comment)) { - $sql_query .= " COMMENT '" . $GLOBALS['dbi']->escapeString($comment) - . "'"; - } - - $statement[] = $sql_query; - - return $statement; -} - -/** - * Statement prefix for the PMA_buildColumnCreationStatement() - * - * @param boolean $is_create_tbl true if requirement is to get the statement - * for table creation - * - * @return string $sql_prefix prefix - */ -function PMA_getStatementPrefix($is_create_tbl = true) -{ - $sql_prefix = " "; - if (! $is_create_tbl) { - $sql_prefix = ' ADD '; - } - return $sql_prefix; -} - -/** - * Merge index definitions for one type of index - * - * @param array $definitions the index definitions to merge to - * @param boolean $is_create_tbl true if requirement is to get the statement - * for table creation - * @param array $indexed_columns the columns for one type of index - * @param string $index_keyword the index keyword to use in the definition - * - * @return array $index_definitions - */ -function PMA_mergeIndexStatements( - $definitions, $is_create_tbl, $indexed_columns, $index_keyword -) { - foreach ($indexed_columns as $index) { - $statements = PMA_buildIndexStatements( - $index, " " . $index_keyword . " ", $is_create_tbl - ); - $definitions = array_merge($definitions, $statements); - } - return $definitions; -} - -/** - * Returns sql statement according to the column and index specifications as - * requested - * - * @param boolean $is_create_tbl true if requirement is to get the statement - * for table creation - * - * @return string sql statement - */ -function PMA_getColumnCreationStatements($is_create_tbl = true) -{ - $sql_statement = ""; - list($field_cnt, $field_primary, $field_index, - $field_unique, $field_fulltext, $field_spatial - ) = PMA_getIndexedColumns(); - $definitions = PMA_buildColumnCreationStatement( - $field_cnt, $is_create_tbl - ); - - // Builds the PRIMARY KEY statements - $primary_key_statements = PMA_buildIndexStatements( - isset($field_primary[0]) ? $field_primary[0] : array(), - " PRIMARY KEY ", - $is_create_tbl - ); - $definitions = array_merge($definitions, $primary_key_statements); - - // Builds the INDEX statements - $definitions = PMA_mergeIndexStatements( - $definitions, $is_create_tbl, $field_index, "INDEX" - ); - - // Builds the UNIQUE statements - $definitions = PMA_mergeIndexStatements( - $definitions, $is_create_tbl, $field_unique, "UNIQUE" - ); - - // Builds the FULLTEXT statements - $definitions = PMA_mergeIndexStatements( - $definitions, $is_create_tbl, $field_fulltext, "FULLTEXT" - ); - - // Builds the SPATIAL statements - $definitions = PMA_mergeIndexStatements( - $definitions, $is_create_tbl, $field_spatial, "SPATIAL" - ); - - if (count($definitions)) { - $sql_statement = implode(', ', $definitions); - } - $sql_statement = preg_replace('@, $@', '', $sql_statement); - - return $sql_statement; - -} - -/** - * Returns the partitioning clause - * - * @return string partitioning clause - */ -function PMA_getPartitionsDefinition() -{ - $sql_query = ""; - if (! empty($_REQUEST['partition_by']) - && ! empty($_REQUEST['partition_expr']) - && ! empty($_REQUEST['partition_count']) - && $_REQUEST['partition_count'] > 1 - ) { - $sql_query .= " PARTITION BY " . $_REQUEST['partition_by'] - . " (" . $_REQUEST['partition_expr'] . ")" - . " PARTITIONS " . $_REQUEST['partition_count']; - } - - if (! empty($_REQUEST['subpartition_by']) - && ! empty($_REQUEST['subpartition_expr']) - && ! empty($_REQUEST['subpartition_count']) - && $_REQUEST['subpartition_count'] > 1 - ) { - $sql_query .= " SUBPARTITION BY " . $_REQUEST['subpartition_by'] - . " (" . $_REQUEST['subpartition_expr'] . ")" - . " SUBPARTITIONS " . $_REQUEST['subpartition_count']; - } - - if (! empty($_REQUEST['partitions'])) { - $i = 0; - $partitions = array(); - foreach ($_REQUEST['partitions'] as $partition) { - $partitions[] = PMA_getPartitionDefinition($partition); - $i++; - } - $sql_query .= " (" . implode(", ", $partitions) . ")"; - } - - return $sql_query; -} - -/** - * Returns the definition of a partition/subpartition - * - * @param array $partition array of partition/subpartition detiails - * @param boolean $isSubPartition whether a subpartition - * - * @return string partition/subpartition definition - */ -function PMA_getPartitionDefinition($partition, $isSubPartition = false) -{ - $sql_query = " " . ($isSubPartition ? "SUB" : "") . "PARTITION "; - $sql_query .= $partition['name']; - - if (! empty($partition['value_type'])) { - $sql_query .= " VALUES " . $partition['value_type']; - - if ($partition['value_type'] != 'LESS THAN MAXVALUE') { - $sql_query .= " (" . $partition['value'] . ")"; - } - } - - if (! empty($partition['engine'])) { - $sql_query .= " ENGINE = " . $partition['engine']; - } - if (! empty($partition['comment'])) { - $sql_query .= " COMMENT = '" . $partition['comment'] . "'"; - } - if (! empty($partition['data_directory'])) { - $sql_query .= " DATA DIRECTORY = '" . $partition['data_directory'] . "'"; - } - if (! empty($partition['index_directory'])) { - $sql_query .= " INDEX_DIRECTORY = '" . $partition['index_directory'] . "'"; - } - if (! empty($partition['max_rows'])) { - $sql_query .= " MAX_ROWS = " . $partition['max_rows']; - } - if (! empty($partition['min_rows'])) { - $sql_query .= " MIN_ROWS = " . $partition['min_rows']; - } - if (! empty($partition['tablespace'])) { - $sql_query .= " TABLESPACE = " . $partition['tablespace']; - } - if (! empty($partition['node_group'])) { - $sql_query .= " NODEGROUP = " . $partition['node_group']; - } - - if (! empty($partition['subpartitions'])) { - $j = 0; - $subpartitions = array(); - foreach ($partition['subpartitions'] as $subpartition) { - $subpartitions[] = PMA_getPartitionDefinition( - $subpartition, - true - ); - $j++; - } - $sql_query .= " (" . implode(", ", $subpartitions) . ")"; - } - - return $sql_query; -} - -/** - * Function to get table creation sql query - * - * @param string $db database name - * @param string $table table name - * - * @return string - */ -function PMA_getTableCreationQuery($db, $table) -{ - // get column addition statements - $sql_statement = PMA_getColumnCreationStatements(true); - - // Builds the 'create table' statement - $sql_query = 'CREATE TABLE ' . PhpMyAdmin\Util::backquote($db) . '.' - . PhpMyAdmin\Util::backquote(trim($table)) . ' (' . $sql_statement . ')'; - - // Adds table type, character set, comments and partition definition - if (!empty($_REQUEST['tbl_storage_engine']) - && ($_REQUEST['tbl_storage_engine'] != 'Default') - ) { - $sql_query .= ' ENGINE = ' . $_REQUEST['tbl_storage_engine']; - } - if (!empty($_REQUEST['tbl_collation'])) { - $sql_query .= Util::getCharsetQueryPart($_REQUEST['tbl_collation']); - } - if (! empty($_REQUEST['connection']) - && ! empty($_REQUEST['tbl_storage_engine']) - && $_REQUEST['tbl_storage_engine'] == 'FEDERATED' - ) { - $sql_query .= " CONNECTION = '" - . $GLOBALS['dbi']->escapeString($_REQUEST['connection']) . "'"; - } - if (!empty($_REQUEST['comment'])) { - $sql_query .= ' COMMENT = \'' - . $GLOBALS['dbi']->escapeString($_REQUEST['comment']) . '\''; - } - $sql_query .= PMA_getPartitionsDefinition(); - $sql_query .= ';'; - - return $sql_query; -} - -/** - * Function to get the number of fields for the table creation form - * - * @return int - */ -function PMA_getNumberOfFieldsFromRequest() -{ - if (isset($_REQUEST['submit_num_fields'])) { // adding new fields - $num_fields = intval($_REQUEST['orig_num_fields']) + intval($_REQUEST['added_fields']); - } elseif (isset($_REQUEST['orig_num_fields'])) { // retaining existing fields - $num_fields = intval($_REQUEST['orig_num_fields']); - } elseif (isset($_REQUEST['num_fields']) - && intval($_REQUEST['num_fields']) > 0 - ) { // new table with specified number of fields - $num_fields = intval($_REQUEST['num_fields']); - } else { // new table with unspecified number of fields - $num_fields = 4; - } - - // Limit to 4096 fields (MySQL maximal value) - return min($num_fields, 4096); -} - -/** - * Function to execute the column creation statement - * - * @param string $db current database - * @param string $table current table - * @param string $err_url error page url - * - * @return array - */ -function PMA_tryColumnCreationQuery($db, $table, $err_url) -{ - // get column addition statements - $sql_statement = PMA_getColumnCreationStatements(false); - - // To allow replication, we first select the db to use and then run queries - // on this db. - if (!($GLOBALS['dbi']->selectDb($db))) { - PhpMyAdmin\Util::mysqlDie( - $GLOBALS['dbi']->getError(), - 'USE ' . PhpMyAdmin\Util::backquote($db), false, - $err_url - ); - } - $sql_query = 'ALTER TABLE ' . - PhpMyAdmin\Util::backquote($table) . ' ' . $sql_statement . ';'; - // If there is a request for SQL previewing. - if (isset($_REQUEST['preview_sql'])) { - Core::previewSQL($sql_query); - } - return array($GLOBALS['dbi']->tryQuery($sql_query) , $sql_query); -} diff --git a/tbl_addfield.php b/tbl_addfield.php index 7827652629..159090a0e9 100644 --- a/tbl_addfield.php +++ b/tbl_addfield.php @@ -6,6 +6,7 @@ * @package PhpMyAdmin */ +use PhpMyAdmin\CreateAddField; use PhpMyAdmin\Response; use PhpMyAdmin\Transformations; use PhpMyAdmin\Url; @@ -62,9 +63,7 @@ if (isset($_REQUEST['do_save_data'])) { //tbl_structure.php below unset($_REQUEST['do_save_data']); - include_once 'libraries/create_addfield.lib.php'; - - list($result, $sql_query) = PMA_tryColumnCreationQuery($db, $table, $err_url); + list($result, $sql_query) = CreateAddField::tryColumnCreationQuery($db, $table, $err_url); if ($result === true) { // Update comment table for mime types [MIME] diff --git a/tbl_create.php b/tbl_create.php index 1992060772..d9160a6d12 100644 --- a/tbl_create.php +++ b/tbl_create.php @@ -7,22 +7,23 @@ */ use PhpMyAdmin\Core; +use PhpMyAdmin\CreateAddField; use PhpMyAdmin\Response; use PhpMyAdmin\Transformations; use PhpMyAdmin\Url; +use PhpMyAdmin\Util; /** * Get some core libraries */ require_once 'libraries/common.inc.php'; -require_once 'libraries/create_addfield.lib.php'; // Check parameters -PhpMyAdmin\Util::checkParameters(array('db')); +Util::checkParameters(array('db')); /* Check if database name is empty */ if (strlen($db) === 0) { - PhpMyAdmin\Util::mysqlDie( + Util::mysqlDie( __('The database name is empty!'), '', false, 'index.php' ); } @@ -31,7 +32,7 @@ if (strlen($db) === 0) { * Selects the database to work with */ if (!$GLOBALS['dbi']->selectDb($db)) { - PhpMyAdmin\Util::mysqlDie( + Util::mysqlDie( sprintf(__('\'%s\' database does not exist.'), htmlspecialchars($db)), '', false, @@ -41,7 +42,7 @@ if (!$GLOBALS['dbi']->selectDb($db)) { if ($GLOBALS['dbi']->getColumns($db, $table)) { // table exists already - PhpMyAdmin\Util::mysqlDie( + Util::mysqlDie( sprintf(__('Table %s already exists!'), htmlspecialchars($table)), '', false, @@ -51,7 +52,7 @@ if ($GLOBALS['dbi']->getColumns($db, $table)) { // for libraries/tbl_columns_definition_form.inc.php // check number of fields to be created -$num_fields = PMA_getNumberOfFieldsFromRequest(); +$num_fields = CreateAddField::getNumberOfFieldsFromRequest(); $action = 'tbl_create.php'; @@ -59,7 +60,7 @@ $action = 'tbl_create.php'; * The form used to define the structure of the table has been submitted */ if (isset($_REQUEST['do_save_data'])) { - $sql_query = PMA_getTableCreationQuery($db, $table); + $sql_query = CreateAddField::getTableCreationQuery($db, $table); // If there is a request for SQL previewing. if (isset($_REQUEST['preview_sql'])) { From e555b779467af8e9113ea60786b75f0351f20233 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Mon, 11 Sep 2017 22:58:58 -0300 Subject: [PATCH 4/4] Refactor file_listing functions to static methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- libraries/classes/Display/Export.php | 1 - libraries/classes/Display/Import.php | 1 - libraries/classes/FileListing.php | 106 +++++++++++++++++++++++++++ libraries/classes/InsertEdit.php | 3 +- libraries/classes/Util.php | 5 +- libraries/file_listing.lib.php | 97 ------------------------ libraries/sql_query_form.lib.php | 5 -- tbl_change.php | 4 - test/classes/InsertEditTest.php | 2 +- 9 files changed, 112 insertions(+), 112 deletions(-) create mode 100644 libraries/classes/FileListing.php delete mode 100644 libraries/file_listing.lib.php diff --git a/libraries/classes/Display/Export.php b/libraries/classes/Display/Export.php index 34f635f57d..48414bb696 100644 --- a/libraries/classes/Display/Export.php +++ b/libraries/classes/Display/Export.php @@ -1002,7 +1002,6 @@ class Export $GLOBALS['single_table'] = $_REQUEST['single_table']; } - include_once './libraries/file_listing.lib.php'; include_once './libraries/plugin_interface.lib.php'; /* Scan for plugins */ diff --git a/libraries/classes/Display/Import.php b/libraries/classes/Display/Import.php index 65a4c8a97a..17afb49e53 100644 --- a/libraries/classes/Display/Import.php +++ b/libraries/classes/Display/Import.php @@ -650,7 +650,6 @@ class Import public static function getImportDisplay($import_type, $db, $table, $max_upload_size) { global $SESSION_KEY; - include_once './libraries/file_listing.lib.php'; include_once './libraries/plugin_interface.lib.php'; list( diff --git a/libraries/classes/FileListing.php b/libraries/classes/FileListing.php new file mode 100644 index 0000000000..5360bd25c4 --- /dev/null +++ b/libraries/classes/FileListing.php @@ -0,0 +1,106 @@ +' . htmlspecialchars($val) . '' . "\n"; + } + return $result; + } + + /** + * Get currently supported decompressions. + * + * @return string separated list of extensions usable in self::getDirContent + */ + public static function supportedDecompressions() + { + global $cfg; + + $compressions = ''; + + if ($cfg['GZipDump'] && @function_exists('gzopen')) { + if (!empty($compressions)) { + $compressions .= '|'; + } + $compressions .= 'gz'; + } + if ($cfg['BZipDump'] && @function_exists('bzopen')) { + if (!empty($compressions)) { + $compressions .= '|'; + } + $compressions .= 'bz2'; + } + if ($cfg['ZipDump'] && @function_exists('gzinflate')) { + if (!empty($compressions)) { + $compressions .= '|'; + } + $compressions .= 'zip'; + } + + return $compressions; + } +} diff --git a/libraries/classes/InsertEdit.php b/libraries/classes/InsertEdit.php index 93bbf7f20f..8b79f806c4 100644 --- a/libraries/classes/InsertEdit.php +++ b/libraries/classes/InsertEdit.php @@ -8,6 +8,7 @@ namespace PhpMyAdmin; use PhpMyAdmin\DatabaseInterface; +use PhpMyAdmin\FileListing; use PhpMyAdmin\Message; use PhpMyAdmin\Plugins\TransformationsPlugin; use PhpMyAdmin\Relation; @@ -1237,7 +1238,7 @@ class InsertEdit */ public static function getSelectOptionForUpload($vkey, $column) { - $files = PMA_getFileSelectOptions( + $files = FileListing::getFileSelectOptions( Util::userDir($GLOBALS['cfg']['UploadDir']) ); diff --git a/libraries/classes/Util.php b/libraries/classes/Util.php index 5770b24ad0..f83d9408e5 100644 --- a/libraries/classes/Util.php +++ b/libraries/classes/Util.php @@ -9,6 +9,7 @@ namespace PhpMyAdmin; use PhpMyAdmin\Core; use PhpMyAdmin\DatabaseInterface; +use PhpMyAdmin\FileListing; use PhpMyAdmin\Message; use PhpMyAdmin\Plugins\ImportPlugin; use PhpMyAdmin\Response; @@ -3385,14 +3386,14 @@ class Util } $matcher = '@\.(' . $extensions . ')(\.(' - . PMA_supportedDecompressions() . '))?$@'; + . FileListing::supportedDecompressions() . '))?$@'; $active = (isset($GLOBALS['timeout_passed']) && $GLOBALS['timeout_passed'] && isset($GLOBALS['local_import_file'])) ? $GLOBALS['local_import_file'] : ''; - $files = PMA_getFileSelectOptions( + $files = FileListing::getFileSelectOptions( self::userDir($uploaddir), $matcher, $active diff --git a/libraries/file_listing.lib.php b/libraries/file_listing.lib.php deleted file mode 100644 index 64b469b014..0000000000 --- a/libraries/file_listing.lib.php +++ /dev/null @@ -1,97 +0,0 @@ -' . htmlspecialchars($val) . '' . "\n"; - } - return $result; -} - -/** - * Get currently supported decompressions. - * - * @return string separated list of extensions usable in PMA_getDirContent - */ -function PMA_supportedDecompressions() -{ - global $cfg; - - $compressions = ''; - - if ($cfg['GZipDump'] && @function_exists('gzopen')) { - if (!empty($compressions)) { - $compressions .= '|'; - } - $compressions .= 'gz'; - } - if ($cfg['BZipDump'] && @function_exists('bzopen')) { - if (!empty($compressions)) { - $compressions .= '|'; - } - $compressions .= 'bz2'; - } - if ($cfg['ZipDump'] && @function_exists('gzinflate')) { - if (!empty($compressions)) { - $compressions .= '|'; - } - $compressions .= 'zip'; - } - - return $compressions; -} diff --git a/libraries/sql_query_form.lib.php b/libraries/sql_query_form.lib.php index 296716823c..3da0bcf5e0 100644 --- a/libraries/sql_query_form.lib.php +++ b/libraries/sql_query_form.lib.php @@ -18,11 +18,6 @@ if (! defined('PHPMYADMIN')) { exit; } -/** - * - */ -require_once './libraries/file_listing.lib.php'; // used for file listing - /** * return HTML for the sql query boxes * diff --git a/tbl_change.php b/tbl_change.php index 5f4cfa7c4c..cc0eb13c50 100644 --- a/tbl_change.php +++ b/tbl_change.php @@ -37,10 +37,6 @@ list( if (!empty($unsaved_values) && count($rows) < count($unsaved_values)) { $rows = array_fill(0, count($unsaved_values), false); } -/** - * file listing -*/ -require_once 'libraries/file_listing.lib.php'; /** * Defines the url to return to in case of error in a sql statement diff --git a/test/classes/InsertEditTest.php b/test/classes/InsertEditTest.php index eb6575c813..89e36ece94 100644 --- a/test/classes/InsertEditTest.php +++ b/test/classes/InsertEditTest.php @@ -1214,7 +1214,7 @@ class InsertEditTest extends \PHPUnit_Framework_TestCase /** * This condition should be tested, however, it gives an undefined function - * PMA_getFileSelectOptions error: + * PhpMyAdmin\FileListing::getFileSelectOptions error: * $GLOBALS['cfg']['UploadDir'] = true; * */