2018-04-14 09:18:00 +00:00
|
|
|
<?php
|
|
|
|
/* vim: set expandtab sw=4 ts=4 sts=4: */
|
|
|
|
/**
|
|
|
|
* handles creation of VIEWs
|
|
|
|
*
|
|
|
|
* @todo js error when view name is empty (strFormEmpty)
|
|
|
|
* @todo (also validate if js is disabled, after form submission?)
|
|
|
|
* @package PhpMyAdmin
|
|
|
|
*/
|
|
|
|
|
|
|
|
use PhpMyAdmin\Core;
|
|
|
|
use PhpMyAdmin\Response;
|
|
|
|
use PhpMyAdmin\Template;
|
|
|
|
|
|
|
|
require_once './libraries/common.inc.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs common work
|
|
|
|
*/
|
|
|
|
require './libraries/db_common.inc.php';
|
|
|
|
$url_params['goto'] = 'tbl_structure.php';
|
|
|
|
$url_params['back'] = 'view_create.php';
|
|
|
|
|
|
|
|
$response = Response::getInstance();
|
|
|
|
|
|
|
|
$view_algorithm_options = array(
|
|
|
|
'UNDEFINED',
|
|
|
|
'MERGE',
|
|
|
|
'TEMPTABLE',
|
|
|
|
);
|
|
|
|
|
|
|
|
$view_with_options = array(
|
|
|
|
'CASCADED',
|
|
|
|
'LOCAL'
|
|
|
|
);
|
|
|
|
|
|
|
|
$view_security_options = array(
|
|
|
|
'DEFINER',
|
|
|
|
'INVOKER'
|
|
|
|
);
|
|
|
|
|
|
|
|
// View name is a compulsory field
|
2018-12-16 14:31:03 +00:00
|
|
|
if (isset($_POST['view']['name'])
|
|
|
|
&& empty($_POST['view']['name'])
|
2018-04-14 09:18:00 +00:00
|
|
|
) {
|
|
|
|
$message = PhpMyAdmin\Message::error(__('View name can not be empty!'));
|
|
|
|
$response->addJSON(
|
|
|
|
'message',
|
|
|
|
$message
|
|
|
|
);
|
|
|
|
$response->setRequestStatus(false);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2018-12-16 14:31:03 +00:00
|
|
|
if (isset($_POST['createview']) || isset($_POST['alterview'])) {
|
2018-04-14 09:18:00 +00:00
|
|
|
/**
|
|
|
|
* Creates the view
|
|
|
|
*/
|
|
|
|
$sep = "\r\n";
|
|
|
|
|
2018-12-16 14:31:03 +00:00
|
|
|
if (isset($_POST['createview'])) {
|
2018-04-14 09:18:00 +00:00
|
|
|
$sql_query = 'CREATE';
|
2018-12-16 14:31:03 +00:00
|
|
|
if (isset($_POST['view']['or_replace'])) {
|
2018-04-14 09:18:00 +00:00
|
|
|
$sql_query .= ' OR REPLACE';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$sql_query = 'ALTER';
|
|
|
|
}
|
|
|
|
|
2018-12-16 14:31:03 +00:00
|
|
|
if (Core::isValid($_POST['view']['algorithm'], $view_algorithm_options)) {
|
|
|
|
$sql_query .= $sep . ' ALGORITHM = ' . $_POST['view']['algorithm'];
|
2018-04-14 09:18:00 +00:00
|
|
|
}
|
|
|
|
|
2018-12-16 14:31:03 +00:00
|
|
|
if (! empty($_POST['view']['definer'])) {
|
|
|
|
if (strpos($_POST['view']['definer'], '@') === false) {
|
2018-04-14 09:18:00 +00:00
|
|
|
$sql_query .= $sep . 'DEFINER='
|
2018-12-16 14:31:03 +00:00
|
|
|
. PhpMyAdmin\Util::backquote($_POST['view']['definer']);
|
2018-04-14 09:18:00 +00:00
|
|
|
} else {
|
2018-12-16 14:31:03 +00:00
|
|
|
$arr = explode('@', $_POST['view']['definer']);
|
2018-04-14 09:18:00 +00:00
|
|
|
$sql_query .= $sep . 'DEFINER=' . PhpMyAdmin\Util::backquote($arr[0]);
|
|
|
|
$sql_query .= '@' . PhpMyAdmin\Util::backquote($arr[1]) . ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-16 14:31:03 +00:00
|
|
|
if (isset($_POST['view']['sql_security'])) {
|
|
|
|
if (in_array($_POST['view']['sql_security'], $view_security_options)) {
|
2018-04-14 09:18:00 +00:00
|
|
|
$sql_query .= $sep . ' SQL SECURITY '
|
2018-12-16 14:31:03 +00:00
|
|
|
. $_POST['view']['sql_security'];
|
2018-04-14 09:18:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql_query .= $sep . ' VIEW '
|
2018-12-16 14:31:03 +00:00
|
|
|
. PhpMyAdmin\Util::backquote($_POST['view']['name']);
|
2018-04-14 09:18:00 +00:00
|
|
|
|
2018-12-16 14:31:03 +00:00
|
|
|
if (! empty($_POST['view']['column_names'])) {
|
|
|
|
$sql_query .= $sep . ' (' . $_POST['view']['column_names'] . ')';
|
2018-04-14 09:18:00 +00:00
|
|
|
}
|
|
|
|
|
2018-12-16 14:31:03 +00:00
|
|
|
$sql_query .= $sep . ' AS ' . $_POST['view']['as'];
|
2018-04-14 09:18:00 +00:00
|
|
|
|
2018-12-16 14:31:03 +00:00
|
|
|
if (isset($_POST['view']['with'])) {
|
|
|
|
if (in_array($_POST['view']['with'], $view_with_options)) {
|
|
|
|
$sql_query .= $sep . ' WITH ' . $_POST['view']['with']
|
2018-04-14 09:18:00 +00:00
|
|
|
. ' CHECK OPTION';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
|
2018-12-16 14:31:03 +00:00
|
|
|
if (! isset($_POST['ajax_dialog'])) {
|
2018-04-14 09:18:00 +00:00
|
|
|
$message = PhpMyAdmin\Message::rawError($GLOBALS['dbi']->getError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->addJSON(
|
|
|
|
'message',
|
|
|
|
PhpMyAdmin\Message::error(
|
|
|
|
"<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
|
|
|
|
. $GLOBALS['dbi']->getError()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response->setRequestStatus(false);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If different column names defined for VIEW
|
|
|
|
$view_columns = array();
|
2018-12-16 14:31:03 +00:00
|
|
|
if (isset($_POST['view']['column_names'])) {
|
|
|
|
$view_columns = explode(',', $_POST['view']['column_names']);
|
2018-04-14 09:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$column_map = $GLOBALS['dbi']->getColumnMapFromSql(
|
2018-12-16 14:31:03 +00:00
|
|
|
$_POST['view']['as'], $view_columns
|
2018-04-14 09:18:00 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$systemDb = $GLOBALS['dbi']->getSystemDatabase();
|
|
|
|
$pma_transformation_data = $systemDb->getExistingTransformationData(
|
|
|
|
$GLOBALS['db']
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($pma_transformation_data !== false) {
|
|
|
|
|
|
|
|
// SQL for store new transformation details of VIEW
|
|
|
|
$new_transformations_sql = $systemDb->getNewTransformationDataSql(
|
|
|
|
$pma_transformation_data, $column_map,
|
2018-12-16 14:31:03 +00:00
|
|
|
$_POST['view']['name'], $GLOBALS['db']
|
2018-04-14 09:18:00 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Store new transformations
|
|
|
|
if ($new_transformations_sql != '') {
|
|
|
|
$GLOBALS['dbi']->tryQuery($new_transformations_sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
unset($pma_transformation_data);
|
|
|
|
|
2018-12-16 14:31:03 +00:00
|
|
|
if (! isset($_POST['ajax_dialog'])) {
|
2018-04-14 09:18:00 +00:00
|
|
|
$message = PhpMyAdmin\Message::success();
|
|
|
|
include 'tbl_structure.php';
|
|
|
|
} else {
|
|
|
|
$response->addJSON(
|
|
|
|
'message',
|
|
|
|
PhpMyAdmin\Util::getMessage(
|
|
|
|
PhpMyAdmin\Message::success(),
|
|
|
|
$sql_query
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response->setRequestStatus(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2019-12-08 07:14:01 +00:00
|
|
|
$sql_query = ! empty($_POST['sql_query']) ? $_POST['sql_query'] : '';
|
2018-12-16 14:31:03 +00:00
|
|
|
|
2018-04-14 09:18:00 +00:00
|
|
|
// prefill values if not already filled from former submission
|
|
|
|
$view = array(
|
|
|
|
'operation' => 'create',
|
|
|
|
'or_replace' => '',
|
|
|
|
'algorithm' => '',
|
|
|
|
'definer' => '',
|
|
|
|
'sql_security' => '',
|
|
|
|
'name' => '',
|
|
|
|
'column_names' => '',
|
|
|
|
'as' => $sql_query,
|
|
|
|
'with' => '',
|
2019-12-08 07:14:01 +00:00
|
|
|
'algorithm' => '',
|
2018-04-14 09:18:00 +00:00
|
|
|
);
|
|
|
|
|
2019-12-08 07:14:01 +00:00
|
|
|
// Used to prefill the fields when editing a view
|
|
|
|
if (isset($_GET['db']) && isset($_GET['table'])) {
|
|
|
|
$item = $GLOBALS['dbi']->fetchSingleRow(
|
|
|
|
sprintf(
|
|
|
|
"SELECT `VIEW_DEFINITION`, `CHECK_OPTION`, `DEFINER`,
|
|
|
|
`SECURITY_TYPE`
|
|
|
|
FROM `INFORMATION_SCHEMA`.`VIEWS`
|
|
|
|
WHERE TABLE_SCHEMA='%s'
|
|
|
|
AND TABLE_NAME='%s';",
|
|
|
|
$GLOBALS['dbi']->escapeString($_GET['db']),
|
|
|
|
$GLOBALS['dbi']->escapeString($_GET['table'])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$createView = $GLOBALS['dbi']->getTable($_GET['db'], $_GET['table'])
|
|
|
|
->showCreate();
|
|
|
|
|
|
|
|
// CREATE ALGORITHM=<ALGORITHM> DE...
|
|
|
|
$parts = explode(" ", substr($createView, 17));
|
|
|
|
$item['ALGORITHM'] = $parts[0];
|
|
|
|
|
|
|
|
$view['operation'] = 'alter';
|
|
|
|
$view['definer'] = $item['DEFINER'];
|
|
|
|
$view['sql_security'] = $item['SECURITY_TYPE'];
|
|
|
|
$view['name'] = $_GET['table'];
|
|
|
|
$view['as'] = $item['VIEW_DEFINITION'];
|
|
|
|
$view['with'] = $item['CHECK_OPTION'];
|
|
|
|
$view['algorithm'] = $item['ALGORITHM'];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-16 14:31:03 +00:00
|
|
|
if (Core::isValid($_POST['view'], 'array')) {
|
|
|
|
$view = array_merge($view, $_POST['view']);
|
2018-04-14 09:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$url_params['db'] = $GLOBALS['db'];
|
|
|
|
$url_params['reload'] = 1;
|
|
|
|
|
|
|
|
echo Template::get('view_create')->render([
|
2018-12-16 14:31:03 +00:00
|
|
|
'ajax_dialog' => isset($_POST['ajax_dialog']),
|
|
|
|
'text_dir' => $text_dir,
|
|
|
|
'url_params' => $url_params,
|
|
|
|
'view' => $view,
|
|
|
|
'view_algorithm_options' => $view_algorithm_options,
|
|
|
|
'view_with_options' => $view_with_options,
|
|
|
|
'view_security_options' => $view_security_options,
|
2018-04-14 09:18:00 +00:00
|
|
|
]);
|