view_create.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * handles creation of VIEWs
  5. *
  6. * @todo js error when view name is empty (strFormEmpty)
  7. * @todo (also validate if js is disabled, after form submission?)
  8. * @package PhpMyAdmin
  9. */
  10. use PhpMyAdmin\Core;
  11. use PhpMyAdmin\Response;
  12. use PhpMyAdmin\Template;
  13. require_once './libraries/common.inc.php';
  14. /**
  15. * Runs common work
  16. */
  17. require './libraries/db_common.inc.php';
  18. $url_params['goto'] = 'tbl_structure.php';
  19. $url_params['back'] = 'view_create.php';
  20. $response = Response::getInstance();
  21. $view_algorithm_options = array(
  22. 'UNDEFINED',
  23. 'MERGE',
  24. 'TEMPTABLE',
  25. );
  26. $view_with_options = array(
  27. 'CASCADED',
  28. 'LOCAL'
  29. );
  30. $view_security_options = array(
  31. 'DEFINER',
  32. 'INVOKER'
  33. );
  34. // View name is a compulsory field
  35. if (isset($_POST['view']['name'])
  36. && empty($_POST['view']['name'])
  37. ) {
  38. $message = PhpMyAdmin\Message::error(__('View name can not be empty!'));
  39. $response->addJSON(
  40. 'message',
  41. $message
  42. );
  43. $response->setRequestStatus(false);
  44. exit;
  45. }
  46. if (isset($_POST['createview']) || isset($_POST['alterview'])) {
  47. /**
  48. * Creates the view
  49. */
  50. $sep = "\r\n";
  51. if (isset($_POST['createview'])) {
  52. $sql_query = 'CREATE';
  53. if (isset($_POST['view']['or_replace'])) {
  54. $sql_query .= ' OR REPLACE';
  55. }
  56. } else {
  57. $sql_query = 'ALTER';
  58. }
  59. if (Core::isValid($_POST['view']['algorithm'], $view_algorithm_options)) {
  60. $sql_query .= $sep . ' ALGORITHM = ' . $_POST['view']['algorithm'];
  61. }
  62. if (! empty($_POST['view']['definer'])) {
  63. if (strpos($_POST['view']['definer'], '@') === false) {
  64. $sql_query .= $sep . 'DEFINER='
  65. . PhpMyAdmin\Util::backquote($_POST['view']['definer']);
  66. } else {
  67. $arr = explode('@', $_POST['view']['definer']);
  68. $sql_query .= $sep . 'DEFINER=' . PhpMyAdmin\Util::backquote($arr[0]);
  69. $sql_query .= '@' . PhpMyAdmin\Util::backquote($arr[1]) . ' ';
  70. }
  71. }
  72. if (isset($_POST['view']['sql_security'])) {
  73. if (in_array($_POST['view']['sql_security'], $view_security_options)) {
  74. $sql_query .= $sep . ' SQL SECURITY '
  75. . $_POST['view']['sql_security'];
  76. }
  77. }
  78. $sql_query .= $sep . ' VIEW '
  79. . PhpMyAdmin\Util::backquote($_POST['view']['name']);
  80. if (! empty($_POST['view']['column_names'])) {
  81. $sql_query .= $sep . ' (' . $_POST['view']['column_names'] . ')';
  82. }
  83. $sql_query .= $sep . ' AS ' . $_POST['view']['as'];
  84. if (isset($_POST['view']['with'])) {
  85. if (in_array($_POST['view']['with'], $view_with_options)) {
  86. $sql_query .= $sep . ' WITH ' . $_POST['view']['with']
  87. . ' CHECK OPTION';
  88. }
  89. }
  90. if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
  91. if (! isset($_POST['ajax_dialog'])) {
  92. $message = PhpMyAdmin\Message::rawError($GLOBALS['dbi']->getError());
  93. return;
  94. }
  95. $response->addJSON(
  96. 'message',
  97. PhpMyAdmin\Message::error(
  98. "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
  99. . $GLOBALS['dbi']->getError()
  100. )
  101. );
  102. $response->setRequestStatus(false);
  103. exit;
  104. }
  105. // If different column names defined for VIEW
  106. $view_columns = array();
  107. if (isset($_POST['view']['column_names'])) {
  108. $view_columns = explode(',', $_POST['view']['column_names']);
  109. }
  110. $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
  111. $_POST['view']['as'], $view_columns
  112. );
  113. $systemDb = $GLOBALS['dbi']->getSystemDatabase();
  114. $pma_transformation_data = $systemDb->getExistingTransformationData(
  115. $GLOBALS['db']
  116. );
  117. if ($pma_transformation_data !== false) {
  118. // SQL for store new transformation details of VIEW
  119. $new_transformations_sql = $systemDb->getNewTransformationDataSql(
  120. $pma_transformation_data, $column_map,
  121. $_POST['view']['name'], $GLOBALS['db']
  122. );
  123. // Store new transformations
  124. if ($new_transformations_sql != '') {
  125. $GLOBALS['dbi']->tryQuery($new_transformations_sql);
  126. }
  127. }
  128. unset($pma_transformation_data);
  129. if (! isset($_POST['ajax_dialog'])) {
  130. $message = PhpMyAdmin\Message::success();
  131. include 'tbl_structure.php';
  132. } else {
  133. $response->addJSON(
  134. 'message',
  135. PhpMyAdmin\Util::getMessage(
  136. PhpMyAdmin\Message::success(),
  137. $sql_query
  138. )
  139. );
  140. $response->setRequestStatus(true);
  141. }
  142. exit;
  143. }
  144. $sql_query = ! empty($_POST['sql_query']) ? $_POST['sql_query'] : '';
  145. // prefill values if not already filled from former submission
  146. $view = array(
  147. 'operation' => 'create',
  148. 'or_replace' => '',
  149. 'algorithm' => '',
  150. 'definer' => '',
  151. 'sql_security' => '',
  152. 'name' => '',
  153. 'column_names' => '',
  154. 'as' => $sql_query,
  155. 'with' => '',
  156. 'algorithm' => '',
  157. );
  158. // Used to prefill the fields when editing a view
  159. if (isset($_GET['db']) && isset($_GET['table'])) {
  160. $item = $GLOBALS['dbi']->fetchSingleRow(
  161. sprintf(
  162. "SELECT `VIEW_DEFINITION`, `CHECK_OPTION`, `DEFINER`,
  163. `SECURITY_TYPE`
  164. FROM `INFORMATION_SCHEMA`.`VIEWS`
  165. WHERE TABLE_SCHEMA='%s'
  166. AND TABLE_NAME='%s';",
  167. $GLOBALS['dbi']->escapeString($_GET['db']),
  168. $GLOBALS['dbi']->escapeString($_GET['table'])
  169. )
  170. );
  171. $createView = $GLOBALS['dbi']->getTable($_GET['db'], $_GET['table'])
  172. ->showCreate();
  173. // CREATE ALGORITHM=<ALGORITHM> DE...
  174. $parts = explode(" ", substr($createView, 17));
  175. $item['ALGORITHM'] = $parts[0];
  176. $view['operation'] = 'alter';
  177. $view['definer'] = $item['DEFINER'];
  178. $view['sql_security'] = $item['SECURITY_TYPE'];
  179. $view['name'] = $_GET['table'];
  180. $view['as'] = $item['VIEW_DEFINITION'];
  181. $view['with'] = $item['CHECK_OPTION'];
  182. $view['algorithm'] = $item['ALGORITHM'];
  183. }
  184. if (Core::isValid($_POST['view'], 'array')) {
  185. $view = array_merge($view, $_POST['view']);
  186. }
  187. $url_params['db'] = $GLOBALS['db'];
  188. $url_params['reload'] = 1;
  189. echo Template::get('view_create')->render([
  190. 'ajax_dialog' => isset($_POST['ajax_dialog']),
  191. 'text_dir' => $text_dir,
  192. 'url_params' => $url_params,
  193. 'view' => $view,
  194. 'view_algorithm_options' => $view_algorithm_options,
  195. 'view_with_options' => $view_with_options,
  196. 'view_security_options' => $view_security_options,
  197. ]);