view_create.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. /**
  11. *
  12. */
  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. $view_algorithm_options = array(
  21. 'UNDEFINED',
  22. 'MERGE',
  23. 'TEMPTABLE',
  24. );
  25. $view_with_options = array(
  26. 'CASCADED',
  27. 'LOCAL'
  28. );
  29. $view_security_options = array(
  30. 'DEFINER',
  31. 'INVOKER'
  32. );
  33. if (isset($_REQUEST['createview']) || isset($_REQUEST['alterview'])) {
  34. /**
  35. * Creates the view
  36. */
  37. $sep = "\r\n";
  38. if (isset($_REQUEST['createview'])) {
  39. $sql_query = 'CREATE';
  40. if (isset($_REQUEST['view']['or_replace'])) {
  41. $sql_query .= ' OR REPLACE';
  42. }
  43. } else {
  44. $sql_query = 'ALTER';
  45. }
  46. if (PMA_isValid($_REQUEST['view']['algorithm'], $view_algorithm_options)) {
  47. $sql_query .= $sep . ' ALGORITHM = ' . $_REQUEST['view']['algorithm'];
  48. }
  49. if (! empty($_REQUEST['view']['definer'])) {
  50. $sql_query .= $sep . ' DEFINER = ' . $_REQUEST['view']['definer'];
  51. }
  52. if (isset($_REQUEST['view']['sql_security'])) {
  53. if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
  54. $sql_query .= $sep . ' SQL SECURITY '
  55. . $_REQUEST['view']['sql_security'];
  56. }
  57. }
  58. $sql_query .= $sep . ' VIEW ' . PMA_Util::backquote($_REQUEST['view']['name']);
  59. if (! empty($_REQUEST['view']['column_names'])) {
  60. $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
  61. }
  62. $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
  63. if (isset($_REQUEST['view']['with'])) {
  64. if (in_array($_REQUEST['view']['with'], $view_with_options)) {
  65. $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
  66. . ' CHECK OPTION';
  67. }
  68. }
  69. if ($GLOBALS['dbi']->tryQuery($sql_query)) {
  70. include_once './libraries/tbl_views.lib.php';
  71. // If different column names defined for VIEW
  72. $view_columns = array();
  73. if (isset($_REQUEST['view']['column_names'])) {
  74. $view_columns = explode(',', $_REQUEST['view']['column_names']);
  75. }
  76. $column_map = PMA_getColumnMap($_REQUEST['view']['as'], $view_columns);
  77. $pma_tranformation_data = PMA_getExistingTranformationData($GLOBALS['db']);
  78. if ($pma_tranformation_data !== false) {
  79. // SQL for store new transformation details of VIEW
  80. $new_transformations_sql = PMA_getNewTransformationDataSql(
  81. $pma_tranformation_data, $column_map, $_REQUEST['view']['name'],
  82. $GLOBALS['db']
  83. );
  84. // Store new transformations
  85. if ($new_transformations_sql != '') {
  86. $GLOBALS['dbi']->tryQuery($new_transformations_sql);
  87. }
  88. }
  89. unset($pma_tranformation_data);
  90. if (! isset($_REQUEST['ajax_dialog'])) {
  91. $message = PMA_Message::success();
  92. include 'tbl_structure.php';
  93. } else {
  94. $response = PMA_Response::getInstance();
  95. $response->addJSON(
  96. 'message',
  97. PMA_Util::getMessage(
  98. PMA_Message::success(), $sql_query
  99. )
  100. );
  101. $response->isSuccess(true);
  102. }
  103. exit;
  104. } else {
  105. if (! isset($_REQUEST['ajax_dialog'])) {
  106. $message = PMA_Message::rawError($GLOBALS['dbi']->getError());
  107. } else {
  108. $response = PMA_Response::getInstance();
  109. $response->addJSON(
  110. 'message',
  111. PMA_Message::error(
  112. "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
  113. . $GLOBALS['dbi']->getError()
  114. )
  115. );
  116. $response->isSuccess(false);
  117. exit;
  118. }
  119. }
  120. }
  121. // prefill values if not already filled from former submission
  122. $view = array(
  123. 'operation' => 'create',
  124. 'or_replace' => '',
  125. 'algorithm' => '',
  126. 'definer' => '',
  127. 'sql_security' => '',
  128. 'name' => '',
  129. 'column_names' => '',
  130. 'as' => $sql_query,
  131. 'with' => '',
  132. );
  133. if (PMA_isValid($_REQUEST['view'], 'array')) {
  134. $view = array_merge($view, $_REQUEST['view']);
  135. }
  136. $url_params['db'] = $GLOBALS['db'];
  137. $url_params['reload'] = 1;
  138. /**
  139. * Displays the page
  140. */
  141. $htmlString = '<!-- CREATE VIEW options -->'
  142. . '<div id="div_view_options">'
  143. . '<form method="post" action="view_create.php">'
  144. . PMA_URL_getHiddenInputs($url_params)
  145. . '<fieldset>'
  146. . '<legend>'
  147. . (isset($_REQUEST['ajax_dialog']) ?
  148. __('Details') :
  149. ($view['operation'] == 'create' ? __('Create view') : __('Edit view'))
  150. )
  151. . '</legend>'
  152. . '<table class="rte_table">';
  153. if ($view['operation'] == 'create') {
  154. $htmlString .= '<tr>'
  155. . '<td class="nowrap"><label for="or_replace">OR REPLACE</label></td>'
  156. . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
  157. if ($view['or_replace']) {
  158. $htmlString .= ' checked="checked"';
  159. }
  160. $htmlString .= ' value="1" /></td></tr>';
  161. }
  162. $htmlString .= '<tr>'
  163. . '<td class="nowrap"><label for="algorithm">ALGORITHM</label></td>'
  164. . '<td><select name="view[algorithm]" id="algorithm">';
  165. foreach ($view_algorithm_options as $option) {
  166. $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
  167. if ($view['algorithm'] === $option) {
  168. $htmlString .= ' selected="selected"';
  169. }
  170. $htmlString .= '>' . htmlspecialchars($option) . '</option>';
  171. }
  172. $htmlString .= '</select>'
  173. . '</td></tr>';
  174. $htmlString .= '<tr><td class="nowrap">' . __('Definer') . '</td>'
  175. . '<td><input type="text" maxlength="100" size="50" name="view[definer]"'
  176. . ' value="' . htmlspecialchars($view['definer']) . '" />'
  177. . '</td></tr>';
  178. $htmlString .= '<tr><td class="nowrap">SQL SECURITY</td>'
  179. . '<td><select name="view[sql_security]">'
  180. . '<option value=""></option>';
  181. foreach ($view_security_options as $option) {
  182. $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
  183. if ($option == $view['sql_security']) {
  184. $htmlString .= ' selected="selected"';
  185. }
  186. $htmlString .= '>' . htmlspecialchars($option) . '</option>';
  187. }
  188. $htmlString .= '<select>'
  189. . '</td></tr>';
  190. if ($view['operation'] == 'create') {
  191. $htmlString .= '<tr><td class="nowrap">' . __('VIEW name') . '</td>'
  192. . '<td><input type="text" size="20" name="view[name]"'
  193. . ' onfocus="this.select()"'
  194. . ' value="' . htmlspecialchars($view['name']) . '" />'
  195. . '</td></tr>';
  196. } else {
  197. $htmlString .= '<tr><td><input type="hidden" name="view[name]"'
  198. . ' value="' . htmlspecialchars($view['name']) . '" />'
  199. . '</td></tr>';
  200. }
  201. $htmlString .= '<tr><td class="nowrap">' . __('Column names') . '</td>'
  202. . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
  203. . ' onfocus="this.select()"'
  204. . ' value="' . htmlspecialchars($view['column_names']) . '" />'
  205. . '</td></tr>';
  206. $htmlString .= '<tr><td class="nowrap">AS</td>'
  207. . '<td>'
  208. . '<textarea name="view[as]" rows="' . $cfg['TextareaRows'] . '"'
  209. . ' cols="' . $cfg['TextareaCols'] . '" dir="' . $text_dir . '"';
  210. if ($GLOBALS['cfg']['TextareaAutoSelect'] || true) {
  211. $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
  212. }
  213. $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
  214. . '</td></tr>';
  215. $htmlString .= '<tr><td class="nowrap">WITH CHECK OPTION</td>'
  216. . '<td><select name="view[with]">'
  217. . '<option value=""></option>';
  218. foreach ($view_with_options as $option) {
  219. $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
  220. if ($option == $view['with']) {
  221. $htmlString .= ' selected="selected"';
  222. }
  223. $htmlString .= '>' . htmlspecialchars($option) . '</option>';
  224. }
  225. $htmlString .= '<select>'
  226. . '</td></tr>';
  227. $htmlString .= '</table>'
  228. . '</fieldset>';
  229. if (! isset($_REQUEST['ajax_dialog'])) {
  230. $htmlString .= '<fieldset class="tblFooters">'
  231. . '<input type="hidden" name="'
  232. . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
  233. . '" value="1" />'
  234. . '<input type="submit" name="" value="' . __('Go') . '" />'
  235. . '</fieldset>';
  236. } else {
  237. $htmlString .= '<input type="hidden" name="'
  238. . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
  239. . '" value="1" />'
  240. . '<input type="hidden" name="ajax_dialog" value="1" />'
  241. . '<input type="hidden" name="ajax_request" value="1" />';
  242. }
  243. $htmlString .= '</form>'
  244. . '</div>';
  245. echo $htmlString;