Rev Author Line No. Line
250 kaklik 1 <?php
2 /* $Id: mult_submits.inc.php,v 1.9.2.3 2006/02/09 19:14:37 lem9 Exp $ */
3 // vim: expandtab sw=4 ts=4 sts=4:
4  
5  
6 /**
7 * Prepares the work and runs some other scripts if required
8 */
9 if (!empty($submit_mult)
10 && ($submit_mult != $strWithChecked)
11 && ( ( isset($selected_db) && (is_array($selected_db) || strlen($selected_db)))
12 || ( isset($selected_tbl) && (is_array($selected_tbl) || strlen($selected_tbl)))
13 || ( isset($selected_fld) && (is_array($selected_fld) || strlen($selected_fld)))
14 || !empty($rows_to_delete)
15 )) {
16  
17 if (isset($selected_db) && (is_array($selected_db) || strlen($selected_db))) {
18 $selected = $selected_db;
19 $what = 'drop_db';
20 } elseif (isset($selected_tbl) && (is_array($selected_tbl) || strlen($selected_tbl))) {
21 if ($submit_mult == $strPrintView) {
22 require('./tbl_printview.php');
23 } else {
24 $selected = $selected_tbl;
25 switch ($submit_mult) {
26 case 'drop_db':
27 $what = 'drop_db';
28 break;
29 case $strDrop:
30 $what = 'drop_tbl';
31 break;
32 case $strEmpty:
33 $what = 'empty_tbl';
34 break;
35 case $strCheckTable:
36 unset($submit_mult);
37 $query_type = 'check_tbl';
38 $mult_btn = $strYes;
39 break;
40 case $strOptimizeTable:
41 unset($submit_mult);
42 $query_type = 'optimize_tbl';
43 $mult_btn = $strYes;
44 break;
45 case $strRepairTable:
46 unset($submit_mult);
47 $query_type = 'repair_tbl';
48 $mult_btn = $strYes;
49 break;
50 case $strAnalyzeTable:
51 unset($submit_mult);
52 $query_type = 'analyze_tbl';
53 $mult_btn = $strYes;
54 break;
55 } // end switch
56 }
57 } elseif (isset($selected_fld) && (is_array($selected_fld) || strlen($selected_fld))) {
58 $selected = $selected_fld;
59 switch ($submit_mult) {
60 case $strDrop:
61 $what = 'drop_fld';
62 break;
63 case $strPrimary:
64 // Gets table primary key
65 PMA_DBI_select_db($db);
66 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
67 $primary = '';
68 while ($row = PMA_DBI_fetch_assoc($result)) {
69 // Backups the list of primary keys
70 if ($row['Key_name'] == 'PRIMARY') {
71 $primary .= $row['Column_name'] . ', ';
72 }
73 } // end while
74 PMA_DBI_free_result($result);
75 if (empty($primary)) {
76 // no primary key, so we can safely create new
77 unset($submit_mult);
78 $query_type = 'primary_fld';
79 $mult_btn = $strYes;
80 } else {
81 // primary key exists, so lets as user
82 $what = 'primary_fld';
83 }
84 break;
85 case $strIndex:
86 unset($submit_mult);
87 $query_type = 'index_fld';
88 $mult_btn = $strYes;
89 break;
90 case $strUnique:
91 unset($submit_mult);
92 $query_type = 'unique_fld';
93 $mult_btn = $strYes;
94 break;
95 case $strIdxFulltext:
96 unset($submit_mult);
97 $query_type = 'fulltext_fld';
98 $mult_btn = $strYes;
99 break;
100 case $strChange:
101 require('./tbl_alter.php');
102 break;
103 case $strBrowse:
104 $sql_query = '';
105 foreach ($selected AS $idx => $sval) {
106 if ($sql_query == '') {
107 $sql_query .= 'SELECT ' . PMA_backquote(urldecode($sval));
108 } else {
109 $sql_query .= ', ' . PMA_backquote(urldecode($sval));
110 }
111 }
112 $sql_query .= ' FROM ' . PMA_backquote(htmlspecialchars($table));
113 require('./sql.php');
114 break;
115 }
116 } else {
117 $what = 'row_delete';
118 $selected = $rows_to_delete;
119 }
120 } // end if
121  
122  
123 /**
124 * Displays the confirmation form if required
125 */
126 if ( !empty($submit_mult) && !empty($what)) {
127 $js_to_run = 'functions.js';
128 unset($message);
129 if (isset($table) && strlen($table)) {
130 require('./libraries/tbl_properties_common.php');
131 $url_query .= '&amp;goto=tbl_properties.php&amp;back=tbl_properties.php';
132 require('./libraries/tbl_properties_table_info.inc.php');
133 } elseif (isset($db) && strlen($db)) {
134 require('./libraries/db_details_common.inc.php');
135 require('./libraries/db_details_db_info.inc.php');
136 }
137 // Builds the query
138 $full_query = '';
139 if ($what == 'drop_tbl') {
140 $full_query_views = '';
141 }
142 $selected_cnt = count($selected);
143 $i = 0;
144 foreach ($selected AS $idx => $sval) {
145 switch ($what) {
146 case 'row_delete':
147 $full_query .= htmlspecialchars(urldecode($sval))
148 . ';<br />';
149 break;
150 case 'drop_db':
151 $full_query .= 'DROP DATABASE '
152 . PMA_backquote(htmlspecialchars(urldecode($sval)))
153 . ';<br />';
154 $reload = 1;
155 break;
156  
157 case 'drop_tbl':
158 $current = urldecode($sval);
159 if (!empty($views) && in_array($current, $views)) {
160 $full_query_views .= (empty($full_query_views) ? 'DROP VIEW ' : ', ')
161 . PMA_backquote(htmlspecialchars($current));
162 } else {
163 $full_query .= (empty($full_query) ? 'DROP TABLE ' : ', ')
164 . PMA_backquote(htmlspecialchars($current));
165 }
166 break;
167  
168 case 'empty_tbl':
169 if (PMA_MYSQL_INT_VERSION >= 40000) {
170 $full_query .= 'TRUNCATE ';
171 } else {
172 $full_query .= 'DELETE FROM ';
173 }
174 $full_query .= PMA_backquote(htmlspecialchars(urldecode($sval)))
175 . ';<br />';
176 break;
177  
178 case 'primary_fld':
179 if ($full_query == '') {
180 $full_query .= 'ALTER TABLE '
181 . PMA_backquote(htmlspecialchars($table))
182 . '<br />&nbsp;&nbsp;DROP PRIMARY KEY,'
183 . '<br />&nbsp;&nbsp; ADD PRIMARY KEY('
184 . '<br />&nbsp;&nbsp;&nbsp;&nbsp; '
185 . PMA_backquote(htmlspecialchars(urldecode($sval)))
186 . ',';
187 } else {
188 $full_query .= '<br />&nbsp;&nbsp;&nbsp;&nbsp; '
189 . PMA_backquote(htmlspecialchars(urldecode($sval)))
190 . ',';
191 }
192 if ($i == $selected_cnt-1) {
193 $full_query = preg_replace('@,$@', ');<br />', $full_query);
194 }
195 break;
196  
197 case 'drop_fld':
198 if ($full_query == '') {
199 $full_query .= 'ALTER TABLE '
200 . PMA_backquote(htmlspecialchars($table))
201 . '<br />&nbsp;&nbsp;DROP '
202 . PMA_backquote(htmlspecialchars(urldecode($sval)))
203 . ',';
204 } else {
205 $full_query .= '<br />&nbsp;&nbsp;DROP '
206 . PMA_backquote(htmlspecialchars(urldecode($sval)))
207 . ',';
208 }
209 if ($i == $selected_cnt-1) {
210 $full_query = preg_replace('@,$@', ';<br />', $full_query);
211 }
212 break;
213 } // end switch
214 $i++;
215 }
216 if ($what == 'drop_tbl') {
217 if (!empty($full_query)) {
218 $full_query .= ';<br />' . "\n";
219 }
220 if (!empty($full_query_views)) {
221 $full_query .= $full_query_views . ';<br />' . "\n";
222 }
223 unset($full_query_views);
224 }
225  
226 // Displays the form
227 ?>
228 <!-- Do it really ? -->
229 <form action="<?php echo $action; ?>" method="post">
230 <input type="hidden" name="query_type" value="<?php echo $what; ?>" />
231 <?php
232 if (strpos(' ' . $action, 'db_details') == 1) {
233 echo PMA_generate_common_hidden_inputs($db);
234 } elseif (strpos(' ' . $action, 'tbl_properties') == 1
235 || $what == 'row_delete') {
236 echo PMA_generate_common_hidden_inputs($db, $table);
237 } else {
238 echo PMA_generate_common_hidden_inputs();
239 }
240 ?>
241 <input type="hidden" name="reload" value="<?php echo isset($reload) ? PMA_sanitize($reload) : 0; ?>" />
242 <?php
243 foreach ($selected AS $idx => $sval) {
244 echo '<input type="hidden" name="selected[]" value="' . htmlspecialchars($sval) . '" />' . "\n";
245 }
246 if ($what == 'drop_tbl' && !empty($views)) {
247 foreach ($views as $current) {
248 echo '<input type="hidden" name="views[]" value="' . htmlspecialchars($current) . '" />' . "\n";
249 }
250 }
251 if ($what == 'row_delete') {
252 echo '<input type="hidden" name="original_sql_query" value="' . htmlspecialchars($original_sql_query) . '" />' . "\n";
253 echo '<input type="hidden" name="original_pos" value="' . $original_pos . '" />' . "\n";
254 echo '<input type="hidden" name="original_url_query" value="' . htmlspecialchars($original_url_query) . '" />' . "\n";
255 echo '<input type="hidden" name="disp_direction" value="' . $disp_direction . '" />' . "\n";
256 echo '<input type="hidden" name="repeat_cells" value="' . $repeat_cells . '" />' . "\n";
257 echo '<input type="hidden" name="dontlimitchars" value="' . $dontlimitchars . '" />' . "\n";
258 echo '<input type="hidden" name="pos" value="' . ( isset( $pos ) ? $pos : 0 ) . '" />' . "\n";
259 echo '<input type="hidden" name="session_max_rows" value="' . $session_max_rows . '" />' . "\n";
260 }
261 ?>
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
273  
274  
275 /**
276 * Executes the query
277 */
278 elseif ($mult_btn == $strYes) {
279  
280 if ($query_type == 'drop_db' || $query_type == 'drop_tbl' || $query_type == 'drop_fld') {
281 require_once('./libraries/relation_cleanup.lib.php');
282 }
283  
284 $sql_query = '';
285 if ($query_type == 'drop_tbl') {
286 $sql_query_views = '';
287 }
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)
291  
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'] . ', ';
301 }
302 } // end while
303 PMA_DBI_free_result($result);
304 }
305  
306 for ($i = 0; $i < $selected_cnt; $i++) {
307 switch ($query_type) {
308 case 'row_delete':
309 $a_query = urldecode($selected[$i]);
310 $run_parts = TRUE;
311 break;
312  
313 case 'drop_db':
314 PMA_relationsCleanupDatabase($selected[$i]);
315 $a_query = 'DROP DATABASE '
316 . PMA_backquote(urldecode($selected[$i]));
317 $reload = 1;
318 $run_parts = TRUE;
319 break;
320  
321 case 'drop_tbl':
322 PMA_relationsCleanupTable($db, $selected[$i]);
323 $current = urldecode($selected[$i]);
324 if (!empty($views) && in_array($current, $views)) {
325 $sql_query_views .= (empty($sql_query_views) ? 'DROP VIEW ' : ', ')
326 . PMA_backquote($current);
327 } else {
328 $sql_query .= (empty($sql_query) ? 'DROP TABLE ' : ', ')
329 . PMA_backquote($current);
330 }
331 $reload = 1;
332 break;
333  
334 case 'check_tbl':
335 $sql_query .= (empty($sql_query) ? 'CHECK TABLE ' : ', ')
336 . PMA_backquote(urldecode($selected[$i]));
337 $use_sql = TRUE;
338 break;
339  
340 case 'optimize_tbl':
341 $sql_query .= (empty($sql_query) ? 'OPTIMIZE TABLE ' : ', ')
342 . PMA_backquote(urldecode($selected[$i]));
343 $use_sql = TRUE;
344 break;
345  
346 case 'analyze_tbl':
347 $sql_query .= (empty($sql_query) ? 'ANALYZE TABLE ' : ', ')
348 . PMA_backquote(urldecode($selected[$i]));
349 $use_sql = TRUE;
350 break;
351  
352 case 'repair_tbl':
353 $sql_query .= (empty($sql_query) ? 'REPAIR TABLE ' : ', ')
354 . PMA_backquote(urldecode($selected[$i]));
355 $use_sql = TRUE;
356 break;
357  
358 case 'empty_tbl':
359 if (PMA_MYSQL_INT_VERSION >= 40000) {
360 $a_query = 'TRUNCATE ';
361 } else {
362 $a_query = 'DELETE FROM ';
363 }
364 $a_query .= PMA_backquote(htmlspecialchars(urldecode($selected[$i])));
365 $run_parts = TRUE;
366 break;
367  
368 case 'drop_fld':
369 PMA_relationsCleanupColumn($db, $table, $selected[$i]);
370 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) : ',')
371 . ' DROP ' . PMA_backquote(urldecode($selected[$i]))
372 . (($i == $selected_cnt-1) ? ';' : '');
373 break;
374  
375 case 'primary_fld':
376 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ( empty($primary) ? '' : ' DROP PRIMARY KEY,') . ' ADD PRIMARY KEY( ' : ', ')
377 . PMA_backquote(urldecode($selected[$i]))
378 . (($i == $selected_cnt-1) ? ');' : '');
379 break;
380  
381 case 'index_fld':
382 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX( ' : ', ')
383 . PMA_backquote(urldecode($selected[$i]))
384 . (($i == $selected_cnt-1) ? ');' : '');
385 break;
386  
387 case 'unique_fld':
388 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE( ' : ', ')
389 . PMA_backquote(urldecode($selected[$i]))
390 . (($i == $selected_cnt-1) ? ');' : '');
391 break;
392  
393 case 'fulltext_fld':
394 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT( ' : ', ')
395 . PMA_backquote(urldecode($selected[$i]))
396 . (($i == $selected_cnt-1) ? ');' : '');
397 break;
398 } // end switch
399  
400 // All "DROP TABLE", "DROP FIELD", "OPTIMIZE TABLE" and "REPAIR TABLE"
401 // statements will be run at once below
402 if ($run_parts) {
403 $sql_query .= $a_query . ';' . "\n";
404 if ($query_type != 'drop_db') {
405 PMA_DBI_select_db($db);
406 }
407 $result = @PMA_DBI_query($a_query) or PMA_mysqlDie('', $a_query, FALSE, $err_url);
408 } // end if
409 } // end for
410  
411 if ($query_type == 'drop_tbl') {
412 if (!empty($sql_query)) {
413 $sql_query .= ';';
414 } elseif (!empty($sql_query_views)) {
415 $sql_query = $sql_query_views . ';';
416 unset($sql_query_views);
417 }
418 }
419  
420 if ($use_sql) {
421 require('./sql.php');
422 } elseif (!$run_parts) {
423 PMA_DBI_select_db($db);
424 $result = PMA_DBI_query($sql_query);
425 if (!empty($sql_query_views)) {
426 $sql_query .= ' ' . $sql_query_views . ';';
427 PMA_DBI_query($sql_query_views);
428 unset($sql_query_views);
429 }
430 }
431 }
432 ?>