Skip to content

Commit

Permalink
Merge pull request #15758 from mauriciofauth/common-includes
Browse files Browse the repository at this point in the history
Remove server, db and tbl common include files
  • Loading branch information
MauricioFauth committed Jan 8, 2020
2 parents 59ce2db + d0f28ce commit 5b7e1c0
Show file tree
Hide file tree
Showing 50 changed files with 353 additions and 324 deletions.
239 changes: 239 additions & 0 deletions libraries/classes/Common.php
@@ -0,0 +1,239 @@
<?php
/**
* @package PhpMyAdmin
*/
declare(strict_types=1);

namespace PhpMyAdmin;

/**
* Shared code for server, database and table level pages.
*
* @package PhpMyAdmin
*/
final class Common
{
/**
* @return void
*/
public static function server(): void
{
global $db, $table, $url_query, $viewing_mode, $err_url, $is_grantuser, $is_createuser, $dbi;

/**
* Handles some variables that may have been sent by the calling script
* Note: this can be called also from the db panel to get the privileges of
* a db, in which case we want to keep displaying the tabs of
* the Database panel
*/
if (empty($viewing_mode)) {
$db = '';
$table = '';
}

/**
* Set parameters for links
*/
$url_query = Url::getCommon();

/**
* Defines the urls to return to in case of error in a sql statement
*/
$err_url = Url::getFromRoute('/');

$is_grantuser = $dbi->isUserType('grant');
$is_createuser = $dbi->isUserType('create');

// now, select the mysql db
if ($dbi->isSuperuser()) {
$dbi->selectDb('mysql');
}
}

/**
* @return void
*/
public static function database(): void
{
global $cfg, $db, $is_show_stats, $db_is_system_schema, $err_url;
global $message, $dbi, $url_query, $errno, $is_db, $err_url_0;

Util::checkParameters(['db']);

$response = Response::getInstance();
$is_show_stats = $cfg['ShowStats'];

$db_is_system_schema = $dbi->isSystemSchema($db);
if ($db_is_system_schema) {
$is_show_stats = false;
}

$relation = new Relation($dbi);
$operations = new Operations($dbi, $relation);

/**
* Defines the urls to return to in case of error in a sql statement
*/
$err_url_0 = Url::getFromRoute('/');

$err_url = Util::getScriptNameForOption(
$cfg['DefaultTabDatabase'],
'database'
);
$err_url .= Url::getCommon(['db' => $db], strpos($err_url, '?') === false ? '?' : '&');

/**
* Ensures the database exists (else move to the "parent" script) and displays
* headers
*/
if (! isset($is_db) || ! $is_db) {
if (strlen($db) > 0) {
$is_db = $dbi->selectDb($db);
// This "Command out of sync" 2014 error may happen, for example
// after calling a MySQL procedure; at this point we can't select
// the db but it's not necessarily wrong
if ($dbi->getError() && $errno == 2014) {
$is_db = true;
unset($errno);
}
} else {
$is_db = false;
}
// Not a valid db name -> back to the welcome page
$params = ['reload' => '1'];
if (isset($message)) {
$params['message'] = $message;
}
$uri = './index.php?route=/' . Url::getCommonRaw($params, '&');
if (strlen($db) === 0 || ! $is_db) {
if ($response->isAjax()) {
$response->setRequestStatus(false);
$response->addJSON(
'message',
Message::error(__('No databases selected.'))
);
} else {
Core::sendHeaderLocation($uri);
}
exit;
}
} // end if (ensures db exists)

/**
* Changes database charset if requested by the user
*/
if (isset($_POST['submitcollation'], $_POST['db_collation']) && ! empty($_POST['db_collation'])) {
[$db_charset] = explode('_', $_POST['db_collation']);
$sql_query = 'ALTER DATABASE ' . Util::backquote($db)
. ' DEFAULT' . Util::getCharsetQueryPart($_POST['db_collation']);
$result = $dbi->query($sql_query);
$message = Message::success();

/**
* Changes tables charset if requested by the user
*/
if (isset($_POST['change_all_tables_collations']) &&
$_POST['change_all_tables_collations'] === 'on'
) {
list($tables, , , , , , , ,) = Util::getDbInfo($db, null);
foreach ($tables as $tableName => $data) {
if ($dbi->getTable($db, $tableName)->isView()) {
// Skip views, we can not change the collation of a view.
// issue #15283
continue;
}
$sql_query = 'ALTER TABLE '
. Util::backquote($db)
. '.'
. Util::backquote($tableName)
. ' DEFAULT '
. Util::getCharsetQueryPart($_POST['db_collation']);
$dbi->query($sql_query);

/**
* Changes columns charset if requested by the user
*/
if (isset($_POST['change_all_tables_columns_collations']) &&
$_POST['change_all_tables_columns_collations'] === 'on'
) {
$operations->changeAllColumnsCollation($db, $tableName, $_POST['db_collation']);
}
}
}
unset($db_charset);

/**
* If we are in an Ajax request, let us stop the execution here. Necessary for
* db charset change action on /database/operations. If this causes a bug on
* other pages, we might have to move this to a different location.
*/
if ($response->isAjax()) {
$response->setRequestStatus($message->isSuccess());
$response->addJSON('message', $message);
exit;
}
} elseif (isset($_POST['submitcollation'], $_POST['db_collation']) && empty($_POST['db_collation'])) {
if ($response->isAjax()) {
$response->setRequestStatus(false);
$response->addJSON(
'message',
Message::error(__('No collation provided.'))
);
}
}

/**
* Set parameters for links
*/
$url_query = Url::getCommon(['db' => $db]);
}

/**
* @return void
*/
public static function table(): void
{
global $db, $table, $db_is_system_schema, $url_query, $url_params, $cfg, $dbi, $err_url, $err_url_0;

Util::checkParameters(['db', 'table']);

$db_is_system_schema = $dbi->isSystemSchema($db);

/**
* Set parameters for links
*
* @deprecated
*/
$url_query = Url::getCommon(['db' => $db, 'table' => $table]);

/**
* Set parameters for links
*/
$url_params = [];
$url_params['db'] = $db;
$url_params['table'] = $table;

/**
* Defines the urls to return to in case of error in a sql statement
*/
$err_url_0 = Util::getScriptNameForOption(
$cfg['DefaultTabDatabase'],
'database'
);
$err_url_0 .= Url::getCommon(['db' => $db], strpos($err_url_0, '?') === false ? '?' : '&');

$err_url = Util::getScriptNameForOption(
$cfg['DefaultTabTable'],
'table'
);
$err_url .= Url::getCommon($url_params, strpos($err_url, '?') === false ? '?' : '&');

/**
* Ensures the database and the table exist (else move to the "parent" script)
* Skip test if we are exporting as we can't tell whether a table name is an alias (which would fail the test).
*/
if (($_GET['route'] ?? $_POST['route'] ?? '') === '/table/export') {
require_once ROOT_PATH . 'libraries/db_table_exists.inc.php';
}
}
}
11 changes: 6 additions & 5 deletions libraries/classes/Controllers/Database/DesignerController.php
Expand Up @@ -6,8 +6,9 @@

namespace PhpMyAdmin\Controllers\Database;

use PhpMyAdmin\Common;
use PhpMyAdmin\Database\Designer;
use PhpMyAdmin\Database\Designer\Common;
use PhpMyAdmin\Database\Designer\Common as DesignerCommon;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Response;
use PhpMyAdmin\Template;
Expand All @@ -21,7 +22,7 @@ class DesignerController extends AbstractController
/** @var Designer */
private $databaseDesigner;

/** @var Common */
/** @var DesignerCommon */
private $designerCommon;

/**
Expand All @@ -30,15 +31,15 @@ class DesignerController extends AbstractController
* @param Template $template Template object
* @param string $db Database name
* @param Designer $databaseDesigner Designer object
* @param Common $designerCommon Designer\Common object
* @param DesignerCommon $designerCommon Designer\Common object
*/
public function __construct(
$response,
$dbi,
Template $template,
$db,
Designer $databaseDesigner,
Common $designerCommon
DesignerCommon $designerCommon
) {
parent::__construct($response, $dbi, $template, $db);
$this->databaseDesigner = $databaseDesigner;
Expand Down Expand Up @@ -146,7 +147,7 @@ public function index(): void
return;
}

require ROOT_PATH . 'libraries/db_common.inc.php';
Common::database();

$script_display_field = $this->designerCommon->getTablesInfo();

Expand Down
5 changes: 3 additions & 2 deletions libraries/classes/Controllers/Database/EventsController.php
Expand Up @@ -8,6 +8,7 @@

namespace PhpMyAdmin\Controllers\Database;

use PhpMyAdmin\Common;
use PhpMyAdmin\Rte\Events;
use PhpMyAdmin\Url;
use PhpMyAdmin\Util;
Expand All @@ -34,10 +35,10 @@ public function index(): void
* Displays the header and tabs
*/
if (! empty($table) && in_array($table, $this->dbi->getTables($db))) {
include_once ROOT_PATH . 'libraries/tbl_common.inc.php';
Common::table();
} else {
$table = '';
include_once ROOT_PATH . 'libraries/db_common.inc.php';
Common::database();

list(
$tables,
Expand Down
3 changes: 2 additions & 1 deletion libraries/classes/Controllers/Database/ExportController.php
Expand Up @@ -6,6 +6,7 @@

namespace PhpMyAdmin\Controllers\Database;

use PhpMyAdmin\Common;
use PhpMyAdmin\Config\PageSettings;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Display\Export as DisplayExport;
Expand Down Expand Up @@ -56,7 +57,7 @@ public function index(): void
// /database/export, in which case we don't obey $cfg['MaxTableList']
$sub_part = '_export';

require_once ROOT_PATH . 'libraries/db_common.inc.php';
Common::database();

$url_params['goto'] = Url::getFromRoute('/database/export');
$url_query .= Url::getCommon($url_params, '&');
Expand Down
3 changes: 2 additions & 1 deletion libraries/classes/Controllers/Database/ImportController.php
Expand Up @@ -6,6 +6,7 @@

namespace PhpMyAdmin\Controllers\Database;

use PhpMyAdmin\Common;
use PhpMyAdmin\Config\PageSettings;
use PhpMyAdmin\Display\Import;
use PhpMyAdmin\Util;
Expand All @@ -32,7 +33,7 @@ public function index(): void
/**
* Gets tables information and displays top links
*/
require ROOT_PATH . 'libraries/db_common.inc.php';
Common::database();

list(
$tables,
Expand Down
Expand Up @@ -7,6 +7,7 @@
namespace PhpMyAdmin\Controllers\Database;

use PhpMyAdmin\CheckUserPrivileges;
use PhpMyAdmin\Common;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Display\CreateTable;
use PhpMyAdmin\Html\Generator;
Expand Down Expand Up @@ -267,7 +268,7 @@ public function index(): void
$this->relation->setDbComment($db, $_POST['comment']);
}

require ROOT_PATH . 'libraries/db_common.inc.php';
Common::database();

$url_params['goto'] = Url::getFromRoute('/database/operations');
$url_query .= Url::getCommon($url_params, '&');
Expand Down
Expand Up @@ -6,6 +6,7 @@

namespace PhpMyAdmin\Controllers\Database;

use PhpMyAdmin\Common;
use PhpMyAdmin\Database\Qbe;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Message;
Expand Down Expand Up @@ -138,7 +139,7 @@ public function index(): void
}

$sub_part = '_qbe';
require ROOT_PATH . 'libraries/db_common.inc.php';
Common::database();

$url_params['goto'] = Url::getFromRoute('/database/qbe');
$url_query .= Url::getCommon($url_params, '&');
Expand Down
5 changes: 3 additions & 2 deletions libraries/classes/Controllers/Database/RoutinesController.php
Expand Up @@ -9,6 +9,7 @@
namespace PhpMyAdmin\Controllers\Database;

use PhpMyAdmin\CheckUserPrivileges;
use PhpMyAdmin\Common;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Response;
use PhpMyAdmin\Rte\Routines;
Expand Down Expand Up @@ -59,10 +60,10 @@ public function index(array $params): void
* Displays the header and tabs
*/
if (! empty($table) && in_array($table, $this->dbi->getTables($db))) {
include_once ROOT_PATH . 'libraries/tbl_common.inc.php';
Common::table();
} else {
$table = '';
include_once ROOT_PATH . 'libraries/db_common.inc.php';
Common::database();

list(
$tables,
Expand Down

0 comments on commit 5b7e1c0

Please sign in to comment.