bug #2027102 quotes around lang missing
[phpmyadmin/last10db.git] / libraries / mult_submits.inc.php
blobfdd16df945903a255c07201af5915cd1251f6013
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @version $Id$
6 */
7 if (! defined('PHPMYADMIN')) {
8 exit;
11 /**
12 * Prepares the work and runs some other scripts if required
14 if (! empty($submit_mult)
15 && $submit_mult != $strWithChecked
16 && (! empty($selected_db)
17 || ! empty($selected_tbl)
18 || ! empty($selected_fld)
19 || ! empty($rows_to_delete))) {
20 define('PMA_SUBMIT_MULT', 1);
21 if (isset($selected_db) && !empty($selected_db)) {
22 $selected = $selected_db;
23 $what = 'drop_db';
24 } elseif (isset($selected_tbl) && !empty($selected_tbl)) {
25 if ($submit_mult == $strPrintView) {
26 require './tbl_printview.php';
27 } else {
28 $selected = $selected_tbl;
29 switch ($submit_mult) {
30 case 'drop_db':
31 $what = 'drop_db';
32 break;
33 case $strDrop:
34 $what = 'drop_tbl';
35 break;
36 case $strEmpty:
37 $what = 'empty_tbl';
38 break;
39 case $strCheckTable:
40 unset($submit_mult);
41 $query_type = 'check_tbl';
42 $mult_btn = $strYes;
43 break;
44 case $strOptimizeTable:
45 unset($submit_mult);
46 $query_type = 'optimize_tbl';
47 $mult_btn = $strYes;
48 break;
49 case $strRepairTable:
50 unset($submit_mult);
51 $query_type = 'repair_tbl';
52 $mult_btn = $strYes;
53 break;
54 case $strAnalyzeTable:
55 unset($submit_mult);
56 $query_type = 'analyze_tbl';
57 $mult_btn = $strYes;
58 break;
59 } // end switch
61 } elseif (isset($selected_fld) && !empty($selected_fld)) {
62 $selected = $selected_fld;
63 switch ($submit_mult) {
64 case $strDrop:
65 $what = 'drop_fld';
66 break;
67 case $strPrimary:
68 // Gets table primary key
69 PMA_DBI_select_db($db);
70 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
71 $primary = '';
72 while ($row = PMA_DBI_fetch_assoc($result)) {
73 // Backups the list of primary keys
74 if ($row['Key_name'] == 'PRIMARY') {
75 $primary .= $row['Column_name'] . ', ';
77 } // end while
78 PMA_DBI_free_result($result);
79 if (empty($primary)) {
80 // no primary key, so we can safely create new
81 unset($submit_mult);
82 $query_type = 'primary_fld';
83 $mult_btn = $strYes;
84 } else {
85 // primary key exists, so lets as user
86 $what = 'primary_fld';
88 break;
89 case $strIndex:
90 unset($submit_mult);
91 $query_type = 'index_fld';
92 $mult_btn = $strYes;
93 break;
94 case $strUnique:
95 unset($submit_mult);
96 $query_type = 'unique_fld';
97 $mult_btn = $strYes;
98 break;
99 case $strIdxFulltext:
100 unset($submit_mult);
101 $query_type = 'fulltext_fld';
102 $mult_btn = $strYes;
103 break;
104 case $strChange:
105 require './tbl_alter.php';
106 break;
107 case $strBrowse:
108 $sql_query = '';
109 foreach ($selected AS $idx => $sval) {
110 if ($sql_query == '') {
111 $sql_query .= 'SELECT ' . PMA_backquote(urldecode($sval));
112 } else {
113 $sql_query .= ', ' . PMA_backquote(urldecode($sval));
116 $sql_query .= ' FROM ' . PMA_backquote(htmlspecialchars($table));
117 require './sql.php';
118 break;
120 } else {
121 $what = 'row_delete';
122 $selected = $rows_to_delete;
124 } // end if
128 * Displays the confirmation form if required
130 if (!empty($submit_mult) && !empty($what)) {
131 $js_to_run = 'functions.js';
132 unset($message);
133 if (strlen($table)) {
134 require './libraries/tbl_common.php';
135 $url_query .= '&amp;goto=tbl_sql.php&amp;back=tbl_sql.php';
136 require './libraries/tbl_info.inc.php';
137 } elseif (strlen($db)) {
138 require './libraries/db_common.inc.php';
139 require './libraries/db_info.inc.php';
141 // Builds the query
142 $full_query = '';
143 if ($what == 'drop_tbl') {
144 $full_query_views = '';
146 $selected_cnt = count($selected);
147 $i = 0;
148 foreach ($selected AS $idx => $sval) {
149 switch ($what) {
150 case 'row_delete':
151 $full_query .= htmlspecialchars(urldecode($sval))
152 . ';<br />';
153 break;
154 case 'drop_db':
155 $full_query .= 'DROP DATABASE '
156 . PMA_backquote(htmlspecialchars(urldecode($sval)))
157 . ';<br />';
158 $reload = 1;
159 break;
161 case 'drop_tbl':
162 $current = urldecode($sval);
163 // here we must compare with the value before urldecode()
164 // because $views has been treated with htmlspecialchars()
165 if (!empty($views) && in_array($sval, $views)) {
166 $full_query_views .= (empty($full_query_views) ? 'DROP VIEW ' : ', ')
167 . PMA_backquote(htmlspecialchars($current));
168 } else {
169 $full_query .= (empty($full_query) ? 'DROP TABLE ' : ', ')
170 . PMA_backquote(htmlspecialchars($current));
172 break;
174 case 'empty_tbl':
175 if (PMA_MYSQL_INT_VERSION >= 40000) {
176 $full_query .= 'TRUNCATE ';
177 } else {
178 $full_query .= 'DELETE FROM ';
180 $full_query .= PMA_backquote(htmlspecialchars(urldecode($sval)))
181 . ';<br />';
182 break;
184 case 'primary_fld':
185 if ($full_query == '') {
186 $full_query .= 'ALTER TABLE '
187 . PMA_backquote(htmlspecialchars($table))
188 . '<br />&nbsp;&nbsp;DROP PRIMARY KEY,'
189 . '<br />&nbsp;&nbsp; ADD PRIMARY KEY('
190 . '<br />&nbsp;&nbsp;&nbsp;&nbsp; '
191 . PMA_backquote(htmlspecialchars(urldecode($sval)))
192 . ',';
193 } else {
194 $full_query .= '<br />&nbsp;&nbsp;&nbsp;&nbsp; '
195 . PMA_backquote(htmlspecialchars(urldecode($sval)))
196 . ',';
198 if ($i == $selected_cnt-1) {
199 $full_query = preg_replace('@,$@', ');<br />', $full_query);
201 break;
203 case 'drop_fld':
204 if ($full_query == '') {
205 $full_query .= 'ALTER TABLE '
206 . PMA_backquote(htmlspecialchars($table))
207 . '<br />&nbsp;&nbsp;DROP '
208 . PMA_backquote(htmlspecialchars(urldecode($sval)))
209 . ',';
210 } else {
211 $full_query .= '<br />&nbsp;&nbsp;DROP '
212 . PMA_backquote(htmlspecialchars(urldecode($sval)))
213 . ',';
215 if ($i == $selected_cnt-1) {
216 $full_query = preg_replace('@,$@', ';<br />', $full_query);
218 break;
219 } // end switch
220 $i++;
222 if ($what == 'drop_tbl') {
223 if (!empty($full_query)) {
224 $full_query .= ';<br />' . "\n";
226 if (!empty($full_query_views)) {
227 $full_query .= $full_query_views . ';<br />' . "\n";
229 unset($full_query_views);
232 // Displays the form
234 <!-- Do it really ? -->
235 <form action="<?php echo $action; ?>" method="post">
236 <input type="hidden" name="query_type" value="<?php echo $what; ?>" />
237 <?php
238 if (strpos(' ' . $action, 'db_') == 1) {
239 echo PMA_generate_common_hidden_inputs($db);
240 } elseif (strpos(' ' . $action, 'tbl_') == 1
241 || $what == 'row_delete') {
242 echo PMA_generate_common_hidden_inputs($db, $table);
243 } else {
244 echo PMA_generate_common_hidden_inputs();
247 <input type="hidden" name="reload" value="<?php echo isset($reload) ? PMA_sanitize($reload) : 0; ?>" />
248 <?php
249 foreach ($selected as $idx => $sval) {
250 echo '<input type="hidden" name="selected[]" value="' . htmlspecialchars($sval) . '" />' . "\n";
252 if ($what == 'drop_tbl' && !empty($views)) {
253 foreach ($views as $current) {
254 echo '<input type="hidden" name="views[]" value="' . htmlspecialchars($current) . '" />' . "\n";
257 if ($what == 'row_delete') {
258 echo '<input type="hidden" name="original_sql_query" value="' . htmlspecialchars($original_sql_query) . '" />' . "\n";
259 echo '<input type="hidden" name="original_url_query" value="' . htmlspecialchars($original_url_query) . '" />' . "\n";
262 <fieldset class="confirmation">
263 <legend><?php echo ($what == 'drop_db' ? $strDropDatabaseStrongWarning . '&nbsp;' : '') . $strDoYouReally; ?>:</legend>
264 <tt><?php echo $full_query; ?></tt>
265 </fieldset>
266 <fieldset class="tblFooters">
267 <input type="submit" name="mult_btn" value="<?php echo $strYes; ?>" id="buttonYes" />
268 <input type="submit" name="mult_btn" value="<?php echo $strNo; ?>" id="buttonNo" />
269 </fieldset>
270 <?php
271 require_once './libraries/footer.inc.php';
272 } // end if
276 * Executes the query
278 elseif ($mult_btn == $strYes) {
280 if ($query_type == 'drop_db' || $query_type == 'drop_tbl' || $query_type == 'drop_fld') {
281 require_once './libraries/relation_cleanup.lib.php';
284 $sql_query = '';
285 if ($query_type == 'drop_tbl') {
286 $sql_query_views = '';
288 $selected_cnt = count($selected);
289 $run_parts = FALSE; // whether to run query after each pass
290 $use_sql = FALSE; // whether to include sql.php at the end (to display results)
292 if ($query_type == 'primary_fld') {
293 // Gets table primary key
294 PMA_DBI_select_db($db);
295 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
296 $primary = '';
297 while ($row = PMA_DBI_fetch_assoc($result)) {
298 // Backups the list of primary keys
299 if ($row['Key_name'] == 'PRIMARY') {
300 $primary .= $row['Column_name'] . ', ';
302 } // end while
303 PMA_DBI_free_result($result);
306 $rebuild_database_list = false;
308 for ($i = 0; $i < $selected_cnt; $i++) {
309 switch ($query_type) {
310 case 'row_delete':
311 $a_query = urldecode($selected[$i]);
312 $run_parts = TRUE;
313 break;
315 case 'drop_db':
316 PMA_relationsCleanupDatabase($selected[$i]);
317 $a_query = 'DROP DATABASE '
318 . PMA_backquote(urldecode($selected[$i]));
319 $reload = 1;
320 $run_parts = TRUE;
321 $rebuild_database_list = true;
322 break;
324 case 'drop_tbl':
325 PMA_relationsCleanupTable($db, $selected[$i]);
326 $current = urldecode($selected[$i]);
327 // here we must compare with the value before urldecode()
328 // because $views has been treated with htmlspecialchars()
329 if (!empty($views) && in_array($selected[$i], $views)) {
330 $sql_query_views .= (empty($sql_query_views) ? 'DROP VIEW ' : ', ')
331 . PMA_backquote($current);
332 } else {
333 $sql_query .= (empty($sql_query) ? 'DROP TABLE ' : ', ')
334 . PMA_backquote($current);
336 $reload = 1;
337 break;
339 case 'check_tbl':
340 $sql_query .= (empty($sql_query) ? 'CHECK TABLE ' : ', ')
341 . PMA_backquote(urldecode($selected[$i]));
342 $use_sql = TRUE;
343 break;
345 case 'optimize_tbl':
346 $sql_query .= (empty($sql_query) ? 'OPTIMIZE TABLE ' : ', ')
347 . PMA_backquote(urldecode($selected[$i]));
348 $use_sql = TRUE;
349 break;
351 case 'analyze_tbl':
352 $sql_query .= (empty($sql_query) ? 'ANALYZE TABLE ' : ', ')
353 . PMA_backquote(urldecode($selected[$i]));
354 $use_sql = TRUE;
355 break;
357 case 'repair_tbl':
358 $sql_query .= (empty($sql_query) ? 'REPAIR TABLE ' : ', ')
359 . PMA_backquote(urldecode($selected[$i]));
360 $use_sql = TRUE;
361 break;
363 case 'empty_tbl':
364 if (PMA_MYSQL_INT_VERSION >= 40000) {
365 $a_query = 'TRUNCATE ';
366 } else {
367 $a_query = 'DELETE FROM ';
369 $a_query .= PMA_backquote(htmlspecialchars(urldecode($selected[$i])));
370 $run_parts = TRUE;
371 break;
373 case 'drop_fld':
374 PMA_relationsCleanupColumn($db, $table, $selected[$i]);
375 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) : ',')
376 . ' DROP ' . PMA_backquote(urldecode($selected[$i]))
377 . (($i == $selected_cnt-1) ? ';' : '');
378 break;
380 case 'primary_fld':
381 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . (empty($primary) ? '' : ' DROP PRIMARY KEY,') . ' ADD PRIMARY KEY( ' : ', ')
382 . PMA_backquote(urldecode($selected[$i]))
383 . (($i == $selected_cnt-1) ? ');' : '');
384 break;
386 case 'index_fld':
387 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX( ' : ', ')
388 . PMA_backquote(urldecode($selected[$i]))
389 . (($i == $selected_cnt-1) ? ');' : '');
390 break;
392 case 'unique_fld':
393 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE( ' : ', ')
394 . PMA_backquote(urldecode($selected[$i]))
395 . (($i == $selected_cnt-1) ? ');' : '');
396 break;
398 case 'fulltext_fld':
399 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT( ' : ', ')
400 . PMA_backquote(urldecode($selected[$i]))
401 . (($i == $selected_cnt-1) ? ');' : '');
402 break;
403 } // end switch
405 // All "DROP TABLE", "DROP FIELD", "OPTIMIZE TABLE" and "REPAIR TABLE"
406 // statements will be run at once below
407 if ($run_parts) {
408 $sql_query .= $a_query . ';' . "\n";
409 if ($query_type != 'drop_db') {
410 PMA_DBI_select_db($db);
412 $result = @PMA_DBI_query($a_query) or PMA_mysqlDie('', $a_query, FALSE, $err_url);
413 } // end if
414 } // end for
416 if ($query_type == 'drop_tbl') {
417 if (!empty($sql_query)) {
418 $sql_query .= ';';
419 } elseif (!empty($sql_query_views)) {
420 $sql_query = $sql_query_views . ';';
421 unset($sql_query_views);
425 if ($use_sql) {
426 require './sql.php';
427 } elseif (!$run_parts) {
428 PMA_DBI_select_db($db);
429 $result = PMA_DBI_query($sql_query);
430 if (!empty($sql_query_views)) {
431 $sql_query .= ' ' . $sql_query_views . ';';
432 PMA_DBI_query($sql_query_views);
433 unset($sql_query_views);
436 if ($rebuild_database_list) {
437 // avoid a problem with the database list navigator
438 // when dropping a db from server_databases
439 $GLOBALS['PMA_List_Database']->build();