Rev Author Line No. Line
250 kaklik 1 <?php
2 /* $Id: relation.lib.php,v 2.52.2.1 2006/02/24 20:28:13 lem9 Exp $ */
3 // vim: expandtab sw=4 ts=4 sts=4:
4  
5 /**
6 * Set of functions used with the relation and pdf feature
7 */
8  
9 /**
10 * Executes a query as controluser if possible, otherwise as normal user
11 *
12 * @param string the query to execute
13 * @param boolean whether to display SQL error messages or not
14 *
15 * @return integer the result id
16 *
17 * @global string the URL of the page to show in case of error
18 * @global string the name of db to come back to
19 * @global resource the resource id of DB connect as controluser
20 * @global array configuration infos about the relations stuff
21 *
22 * @access public
23 *
24 * @author Mike Beck <mikebeck@users.sourceforge.net>
25 */
26 function PMA_query_as_cu($sql, $show_error = TRUE, $options = 0) {
27 global $err_url_0, $db, $controllink, $cfgRelation;
28  
29 // Comparing resource ids works on PHP 5 because, when no controluser
30 // is defined, connecting with the same user for controllink does
31 // not create a new connection. However a new connection is created
32 // on PHP 4, so we cannot directly compare resource ids.
33  
34 if ($controllink == $GLOBALS['userlink'] || PMA_MYSQL_INT_VERSION < 50000) {
35 PMA_DBI_select_db($cfgRelation['db'], $controllink);
36 }
37 if ($show_error) {
38 $result = PMA_DBI_query($sql, $controllink, $options);
39 } else {
40 $result = @PMA_DBI_try_query($sql, $controllink, $options);
41 } // end if... else...
42 // It makes no sense to restore database on control user
43 if ($controllink == $GLOBALS['userlink'] || PMA_MYSQL_INT_VERSION < 50000) {
44 PMA_DBI_select_db($db, $controllink);
45 }
46  
47 if ($result) {
48 return $result;
49 } else {
50 return FALSE;
51 }
52 } // end of the "PMA_query_as_cu()" function
53  
54  
55 /**
56 * Defines the relation parameters for the current user
57 * just a copy of the functions used for relations ;-)
58 * but added some stuff to check what will work
59 *
60 * @param boolean whether to check validity of settings or not
61 *
62 * @return array the relation parameters for the current user
63 *
64 * @global array the list of settings for servers
65 * @global integer the id of the current server
66 * @global string the URL of the page to show in case of error
67 * @global string the name of the current db
68 * @global string the name of the current table
69 * @global array configuration infos about the relations stuff
70 *
71 * @access public
72 *
73 * @author Mike Beck <mikebeck@users.sourceforge.net>
74 */
75 function PMA_getRelationsParam($verbose = FALSE)
76 {
77 global $cfg, $server, $err_url_0, $db, $table, $controllink;
78 global $cfgRelation;
79  
80 $cfgRelation = array();
81 $cfgRelation['relwork'] = FALSE;
82 $cfgRelation['displaywork'] = FALSE;
83 $cfgRelation['bookmarkwork']= FALSE;
84 $cfgRelation['pdfwork'] = FALSE;
85 $cfgRelation['commwork'] = FALSE;
86 $cfgRelation['mimework'] = FALSE;
87 $cfgRelation['historywork'] = FALSE;
88 $cfgRelation['allworks'] = FALSE;
89  
90 // No server selected -> no bookmark table
91 // we return the array with the FALSEs in it,
92 // to avoid some 'Unitialized string offset' errors later
93 if ($server == 0
94 || empty($cfg['Server'])
95 || empty($cfg['Server']['pmadb'])) {
96 if ($verbose == TRUE) {
97 echo 'PMA Database ... '
98 . '<font color="red"><b>' . $GLOBALS['strNotOK'] . '</b></font>'
99 . '[ <a href="Documentation.html#pmadb">' . $GLOBALS['strDocu'] . '</a> ]<br />' . "\n"
100 . $GLOBALS['strGeneralRelationFeat']
101 . ' <font color="green">' . $GLOBALS['strDisabled'] . '</font>' . "\n";
102 }
103 return $cfgRelation;
104 }
105  
106 $cfgRelation['user'] = $cfg['Server']['user'];
107 $cfgRelation['db'] = $cfg['Server']['pmadb'];
108  
109 // Now I just check if all tables that i need are present so I can for
110 // example enable relations but not pdf...
111 // I was thinking of checking if they have all required columns but I
112 // fear it might be too slow
113  
114 PMA_DBI_select_db($cfgRelation['db'], $controllink);
115 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($cfgRelation['db']);
116 $tab_rs = PMA_query_as_cu($tab_query, FALSE, PMA_DBI_QUERY_STORE);
117  
118 if ($tab_rs) {
119 while ($curr_table = @PMA_DBI_fetch_row($tab_rs)) {
120 if ($curr_table[0] == $cfg['Server']['bookmarktable']) {
121 $cfgRelation['bookmark'] = $curr_table[0];
122 } elseif ($curr_table[0] == $cfg['Server']['relation']) {
123 $cfgRelation['relation'] = $curr_table[0];
124 } elseif ($curr_table[0] == $cfg['Server']['table_info']) {
125 $cfgRelation['table_info'] = $curr_table[0];
126 } elseif ($curr_table[0] == $cfg['Server']['table_coords']) {
127 $cfgRelation['table_coords'] = $curr_table[0];
128 } elseif ($curr_table[0] == $cfg['Server']['column_info']) {
129 $cfgRelation['column_info'] = $curr_table[0];
130 } elseif ($curr_table[0] == $cfg['Server']['pdf_pages']) {
131 $cfgRelation['pdf_pages'] = $curr_table[0];
132 } elseif ($curr_table[0] == $cfg['Server']['history']) {
133 $cfgRelation['history'] = $curr_table[0];
134 }
135 } // end while
136 }
137  
138 if (isset($cfgRelation['relation'])) {
139 $cfgRelation['relwork'] = TRUE;
140 if (isset($cfgRelation['table_info'])) {
141 $cfgRelation['displaywork'] = TRUE;
142 }
143 }
144 if (isset($cfgRelation['table_coords']) && isset($cfgRelation['pdf_pages'])) {
145 $cfgRelation['pdfwork'] = TRUE;
146 }
147 if (isset($cfgRelation['column_info'])) {
148 $cfgRelation['commwork'] = TRUE;
149  
150 if ($cfg['Server']['verbose_check']) {
151 $mime_query = 'SHOW FIELDS FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']);
152 $mime_rs = PMA_query_as_cu($mime_query, FALSE);
153  
154 $mime_field_mimetype = FALSE;
155 $mime_field_transformation = FALSE;
156 $mime_field_transformation_options = FALSE;
157 while ($curr_mime_field = @PMA_DBI_fetch_row($mime_rs)) {
158 if ($curr_mime_field[0] == 'mimetype') {
159 $mime_field_mimetype = TRUE;
160 } elseif ($curr_mime_field[0] == 'transformation') {
161 $mime_field_transformation = TRUE;
162 } elseif ($curr_mime_field[0] == 'transformation_options') {
163 $mime_field_transformation_options = TRUE;
164 }
165 }
166 PMA_DBI_free_result($mime_rs);
167  
168 if ($mime_field_mimetype == TRUE
169 && $mime_field_transformation == TRUE
170 && $mime_field_transformation_options == TRUE) {
171 $cfgRelation['mimework'] = TRUE;
172 }
173 } else {
174 $cfgRelation['mimework'] = TRUE;
175 }
176 }
177  
178 if (isset($cfgRelation['history'])) {
179 $cfgRelation['historywork'] = TRUE;
180 }
181  
182 if (isset($cfgRelation['bookmark'])) {
183 $cfgRelation['bookmarkwork'] = TRUE;
184 }
185  
186 if ($cfgRelation['relwork'] == TRUE && $cfgRelation['displaywork'] == TRUE
187 && $cfgRelation['pdfwork'] == TRUE && $cfgRelation['commwork'] == TRUE
188 && $cfgRelation['mimework'] == TRUE && $cfgRelation['historywork'] == TRUE
189 && $cfgRelation['bookmarkwork'] == TRUE) {
190 $cfgRelation['allworks'] = TRUE;
191 }
192 if ($tab_rs) {
193 PMA_DBI_free_result($tab_rs);
194 } else {
195 $cfg['Server']['pmadb'] = FALSE;
196 }
197  
198 if ($verbose == TRUE) {
199 $shit = '<font color="red"><b>' . $GLOBALS['strNotOK'] . '</b></font> [ <a href="Documentation.html#%s">' . $GLOBALS['strDocu'] . '</a> ]';
200 $hit = '<font color="green"><b>' . $GLOBALS['strOK'] . '</b></font>';
201 $enabled = '<font color="green">' . $GLOBALS['strEnabled'] . '</font>';
202 $disabled = '<font color="red">' . $GLOBALS['strDisabled'] . '</font>';
203  
204 echo '<table>' . "\n";
205 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'pmadb\'] ... </th><td align="right">'
206 . (($cfg['Server']['pmadb'] == FALSE) ? sprintf($shit, 'pmadb') : $hit)
207 . '</td></tr>' . "\n";
208 echo ' <tr><td>&nbsp;</td></tr>' . "\n";
209  
210 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'relation\'] ... </th><td align="right">'
211 . ((isset($cfgRelation['relation'])) ? $hit : sprintf($shit, 'relation'))
212 . '</td></tr>' . "\n";
213 echo ' <tr><td colspan=2 align="center">'. $GLOBALS['strGeneralRelationFeat'] . ': '
214 . (($cfgRelation['relwork'] == TRUE) ? $enabled : $disabled)
215 . '</td></tr>' . "\n";
216 echo ' <tr><td>&nbsp;</td></tr>' . "\n";
217  
218 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'table_info\'] ... </th><td align="right">'
219 . (($cfgRelation['displaywork'] == FALSE) ? sprintf($shit, 'table_info') : $hit)
220 . '</td></tr>' . "\n";
221 echo ' <tr><td colspan=2 align="center">' . $GLOBALS['strDisplayFeat'] . ': '
222 . (($cfgRelation['displaywork'] == TRUE) ? $enabled : $disabled)
223 . '</td></tr>' . "\n";
224 echo ' <tr><td>&nbsp;</td></tr>' . "\n";
225  
226 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'table_coords\'] ... </th><td align="right">'
227 . ((isset($cfgRelation['table_coords'])) ? $hit : sprintf($shit, 'table_coords'))
228 . '</td></tr>' . "\n";
229 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'pdf_pages\'] ... </th><td align="right">'
230 . ((isset($cfgRelation['pdf_pages'])) ? $hit : sprintf($shit, 'table_coords'))
231 . '</td></tr>' . "\n";
232 echo ' <tr><td colspan=2 align="center">' . $GLOBALS['strCreatePdfFeat'] . ': '
233 . (($cfgRelation['pdfwork'] == TRUE) ? $enabled : $disabled)
234 . '</td></tr>' . "\n";
235 echo ' <tr><td>&nbsp;</td></tr>' . "\n";
236  
237 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'column_info\'] ... </th><td align="right">'
238 . ((isset($cfgRelation['column_info'])) ? $hit : sprintf($shit, 'col_com'))
239 . '</td></tr>' . "\n";
240 echo ' <tr><td colspan=2 align="center">' . $GLOBALS['strColComFeat'] . ': '
241 . (($cfgRelation['commwork'] == TRUE) ? $enabled : $disabled)
242 . '</td></tr>' . "\n";
243 echo ' <tr><td colspan=2 align="center">' . $GLOBALS['strBookmarkQuery'] . ': '
244 . (($cfgRelation['bookmarkwork'] == TRUE) ? $enabled : $disabled)
245 . '</td></tr>' . "\n";
246 echo ' <tr><th align="left">MIME ...</th><td align="right">'
247 . (($cfgRelation['mimework'] == TRUE) ? $hit : sprintf($shit, 'col_com'))
248 . '</td></tr>' . "\n";
249  
250 if (($cfgRelation['commwork'] == TRUE) && ($cfgRelation['mimework'] != TRUE)) {
251 echo '<tr><td colspan=2 align="left">' . $GLOBALS['strUpdComTab'] . '</td></tr>' . "\n";
252 }
253  
254 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'history\'] ... </th><td align="right">'
255 . ((isset($cfgRelation['history'])) ? $hit : sprintf($shit, 'history'))
256 . '</td></tr>' . "\n";
257 echo ' <tr><td colspan=2 align="center">' . $GLOBALS['strQuerySQLHistory'] . ': '
258 . (($cfgRelation['historywork'] == TRUE) ? $enabled : $disabled)
259 . '</td></tr>' . "\n";
260  
261 echo '</table>' . "\n";
262 } // end if ($verbose == TRUE) {
263  
264 return $cfgRelation;
265 } // end of the 'PMA_getRelationsParam()' function
266  
267  
268 /**
269 * Gets all Relations to foreign tables for a given table or
270 * optionally a given column in a table
271 *
272 * @param string the name of the db to check for
273 * @param string the name of the table to check for
274 * @param string the name of the column to check for
275 * @param string the source for foreign key information
276 *
277 * @return array db,table,column
278 *
279 * @global array the list of relations settings
280 * @global string the URL of the page to show in case of error
281 *
282 * @access public
283 *
284 * @author Mike Beck <mikebeck@users.sourceforge.net> and Marc Delisle
285 */
286 function PMA_getForeigners($db, $table, $column = '', $source = 'both') {
287 global $cfgRelation, $err_url_0;
288  
289 if ($cfgRelation['relwork'] && ($source == 'both' || $source == 'internal')) {
290 $rel_query = '
291 SELECT master_field,
292 foreign_db,
293 foreign_table,
294 foreign_field
295 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation']) . '
296 WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\'
297 AND master_table = \'' . PMA_sqlAddslashes($table) . '\' ';
298 if (isset($column) && strlen($column)) {
299 $rel_query .= ' AND master_field = \'' . PMA_sqlAddslashes($column) . '\'';
300 }
301 $relations = PMA_query_as_cu($rel_query);
302 $i = 0;
303 while ($relrow = PMA_DBI_fetch_assoc($relations)) {
304 $field = $relrow['master_field'];
305 $foreign[$field]['foreign_db'] = $relrow['foreign_db'];
306 $foreign[$field]['foreign_table'] = $relrow['foreign_table'];
307 $foreign[$field]['foreign_field'] = $relrow['foreign_field'];
308 $i++;
309 } // end while
310 PMA_DBI_free_result($relations);
311 unset($relations);
312 }
313  
314 if (($source == 'both' || $source == 'innodb') && isset($table) && strlen($table)) {
315 $show_create_table_query = 'SHOW CREATE TABLE '
316 . PMA_backquote($db) . '.' . PMA_backquote($table);
317 $show_create_table_res = PMA_DBI_query($show_create_table_query);
318 list(, $show_create_table) = PMA_DBI_fetch_row($show_create_table_res);
319 PMA_DBI_free_result($show_create_table_res);
320 unset($show_create_table_res, $show_create_table_query);
321 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
322  
323 foreach ($analyzed_sql[0]['foreign_keys'] AS $one_key) {
324  
325 // the analyzer may return more than one column name in the
326 // index list or the ref_index_list
327 foreach ($one_key['index_list'] AS $i => $field) {
328  
329 // If a foreign key is defined in the 'internal' source (pmadb)
330 // and in 'innodb', we won't get it twice if $source='both'
331 // because we use $field as key
332  
333 // The parser looks for a CONSTRAINT clause just before
334 // the FOREIGN KEY clause. It finds it (as output from
335 // SHOW CREATE TABLE) in MySQL 4.0.13, but not in older
336 // versions like 3.23.58.
337 // In those cases, the FOREIGN KEY parsing will put numbers
338 // like -1, 0, 1... instead of the constraint number.
339  
340 if (isset($one_key['constraint'])) {
341 $foreign[$field]['constraint'] = $one_key['constraint'];
342 }
343  
344 if (isset($one_key['ref_db_name'])) {
345 $foreign[$field]['foreign_db'] = $one_key['ref_db_name'];
346 } else {
347 $foreign[$field]['foreign_db'] = $db;
348 }
349 $foreign[$field]['foreign_table'] = $one_key['ref_table_name'];
350 $foreign[$field]['foreign_field'] = $one_key['ref_index_list'][$i];
351 if (isset($one_key['on_delete'])) {
352 $foreign[$field]['on_delete'] = $one_key['on_delete'];
353 }
354 if (isset($one_key['on_update'])) {
355 $foreign[$field]['on_update'] = $one_key['on_update'];
356 }
357 }
358 }
359 }
360  
361 /**
362 * Emulating relations for some information_schema tables
363 */
364 if (PMA_MYSQL_INT_VERSION >= 50002 && $db == 'information_schema'
365 && ($source == 'internal' || $source == 'both')) {
366  
367 require_once('./libraries/information_schema_relations.lib.php');
368  
369 if (!isset($foreign)) {
370 $foreign = array();
371 }
372  
373 if (isset($GLOBALS['information_schema_relations'][$table])) {
374 foreach ($GLOBALS['information_schema_relations'][$table] as $field => $relations) {
375 if ( ( ! isset($column) || ! strlen($column) || $column == $field )
376 && ( ! isset($foreign[$field]) || ! strlen($foreign[$field]) ) ) {
377 $foreign[$field] = $relations;
378 }
379 }
380 }
381 }
382  
383 if (!empty($foreign) && is_array($foreign)) {
384 return $foreign;
385 } else {
386 return FALSE;
387 }
388  
389 } // end of the 'PMA_getForeigners()' function
390  
391  
392 /**
393 * Gets the display field of a table
394 *
395 * @param string the name of the db to check for
396 * @param string the name of the table to check for
397 *
398 * @return string field name
399 *
400 * @global array the list of relations settings
401 *
402 * @access public
403 *
404 * @author Mike Beck <mikebeck@users.sourceforge.net>
405 */
406 function PMA_getDisplayField($db, $table) {
407 global $cfgRelation;
408  
409 /**
410 * Try to fetch the display field from DB.
411 */
412 if (trim(@$cfgRelation['table_info']) != '') {
413  
414 $disp_query = '
415 SELECT display_field
416 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['table_info']) . '
417 WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'
418 AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
419  
420 $disp_res = PMA_query_as_cu($disp_query);
421 $row = ($disp_res ? PMA_DBI_fetch_assoc($disp_res) : '');
422 PMA_DBI_free_result($disp_res);
423 if (isset($row['display_field'])) {
424 return $row['display_field'];
425 }
426  
427 }
428  
429 /**
430 * Emulating the display field for some information_schema tables.
431 */
432 if (PMA_MYSQL_INT_VERSION >= 50002 && $db == 'information_schema') {
433 switch ($table) {
434 case 'CHARACTER_SETS': return 'DESCRIPTION';
435 case 'TABLES': return 'TABLE_COMMENT';
436 }
437 }
438  
439 /**
440 * No Luck...
441 */
442 return FALSE;
443  
444 } // end of the 'PMA_getDisplayField()' function
445  
446  
447 /**
448 * Gets the comments for all rows of a table
449 *
450 * @param string the name of the db to check for
451 * @param string the name of the table to check for
452 *
453 * @return array [field_name] = comment
454 *
455 * @global array the list of relations settings
456 *
457 * @access public
458 *
459 * @authors Mike Beck <mikebeck@users.sourceforge.net>
460 * and lem9
461 */
462 function PMA_getComments($db, $table = '') {
463 global $cfgRelation;
464  
465 if ($table != '') {
466  
467 // MySQL 4.1.x native column comments
468 if (PMA_MYSQL_INT_VERSION >= 40100) {
469 $fields = PMA_DBI_get_fields($db, $table);
470 if ($fields) {
471 foreach ($fields as $key=>$field) {
472 $tmp_col = $field['Field'];
473 if (!empty($field['Comment'])) {
474 $native_comment[$tmp_col] = $field['Comment'];
475 }
476 }
477 if (isset($native_comment)) {
478 $comment = $native_comment;
479 }
480 }
481 }
482  
483 // pmadb internal column comments
484 // (this function can be called even if $cfgRelation['commwork'] is
485 // FALSE, to get native column comments, so recheck here)
486 if ($cfgRelation['commwork']) {
487 $com_qry = '
488 SELECT column_name,
489 comment
490 FROM ' . PMA_backquote($cfgRelation['db']) . '.' .PMA_backquote($cfgRelation['column_info']) . '
491 WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'
492 AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
493 $com_rs = PMA_query_as_cu($com_qry, TRUE, PMA_DBI_QUERY_STORE);
494 }
495 } else {
496 // pmadb internal db comments
497 $com_qry = '
498 SELECT ' . PMA_backquote('comment') . '
499 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
500 WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'
501 AND table_name = \'\'
502 AND column_name = \'(db_comment)\'';
503 $com_rs = PMA_query_as_cu($com_qry, TRUE, PMA_DBI_QUERY_STORE);
504 }
505  
506  
507 if (isset($com_rs) && PMA_DBI_num_rows($com_rs) > 0) {
508 $i = 0;
509 while ($row = PMA_DBI_fetch_assoc($com_rs)) {
510 $i++;
511 $col = ($table != '' ? $row['column_name'] : $i);
512  
513 if (strlen($row['comment']) > 0) {
514 $comment[$col] = $row['comment'];
515 // if this version supports native comments and this function
516 // was called with a table parameter
517 if (PMA_MYSQL_INT_VERSION >= 40100 && isset($table) && strlen($table)) {
518 // if native comment found, use it instead of pmadb
519 if (!empty($native_comment[$col])) {
520 $comment[$col] = $native_comment[$col];
521 } else {
522 // no native comment, so migrate pmadb-style to native
523 PMA_setComment($db, $table, $col, $comment[$col], '', 'native');
524 // and erase the pmadb-style comment
525 PMA_setComment($db, $table, $col, '', '', 'pmadb');
526 }
527 }
528 }
529 } // end while
530  
531 PMA_DBI_free_result($com_rs);
532 unset($com_rs);
533 }
534  
535 if (isset($comment) && is_array($comment)) {
536 return $comment;
537 } else {
538 return FALSE;
539 }
540 } // end of the 'PMA_getComments()' function
541  
542 /**
543 * Adds/removes slashes if required
544 *
545 * @param string the string to slash
546 *
547 * @return string the slashed string
548 *
549 * @access public
550 */
551 function PMA_handleSlashes($val) {
552 return PMA_sqlAddslashes($val);
553 } // end of the "PMA_handleSlashes()" function
554  
555 /**
556 * Set a single comment to a certain value.
557 *
558 * @param string the name of the db
559 * @param string the name of the table (may be empty in case of a db comment)
560 * @param string the name of the column
561 * @param string the value of the column
562 * @param string (optional) if a column is renamed, this is the name of the former key which will get deleted
563 * @param string whether we set pmadb comments, native comments or both
564 *
565 * @return boolean true, if comment-query was made.
566 *
567 * @global array the list of relations settings
568 *
569 * @access public
570 */
571 function PMA_setComment($db, $table, $col, $comment, $removekey = '', $mode='auto') {
572 global $cfgRelation;
573  
574 if ($mode=='auto') {
575 if (PMA_MYSQL_INT_VERSION >= 40100) {
576 $mode='native';
577 } else {
578 $mode='pmadb';
579 }
580 }
581  
582 // native mode is only for column comments so we need a table name
583 if ($mode == 'native' && isset($table) && strlen($table)) {
584 $query = 'ALTER TABLE ' . PMA_backquote($table) . ' CHANGE '
585 . PMA_generateAlterTable($col, $col, '', '', '', '', FALSE, '', FALSE, '', $comment, '', '');
586 PMA_DBI_try_query($query, null, PMA_DBI_QUERY_STORE);
587 return TRUE;
588 }
589  
590 // $mode == 'pmadb' section:
591  
592 $cols = array(
593 'db_name' => 'db_name ',
594 'table_name' => 'table_name ',
595 'column_name' => 'column_name'
596 );
597  
598 if ($removekey != '' AND $removekey != $col) {
599 $remove_query = '
600 DELETE FROM
601 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
602 WHERE ' . $cols['db_name'] . ' = \'' . PMA_sqlAddslashes($db) . '\'
603 AND ' . $cols['table_name'] . ' = \'' . PMA_sqlAddslashes($table) . '\'
604 AND ' . $cols['column_name'] . ' = \'' . PMA_sqlAddslashes($removekey) . '\'';
605 PMA_query_as_cu($remove_query);
606 unset($remove_query);
607 }
608  
609 $test_qry = '
610 SELECT ' . PMA_backquote('comment') . ',
611 mimetype,
612 transformation,
613 transformation_options
614 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
615 WHERE ' . $cols['db_name'] . ' = \'' . PMA_sqlAddslashes($db) . '\'
616 AND ' . $cols['table_name'] . ' = \'' . PMA_sqlAddslashes($table) . '\'
617 AND ' . $cols['column_name'] . ' = \'' . PMA_sqlAddslashes($col) . '\'';
618 $test_rs = PMA_query_as_cu($test_qry, TRUE, PMA_DBI_QUERY_STORE);
619  
620 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
621 $row = PMA_DBI_fetch_assoc($test_rs);
622 PMA_DBI_free_result($test_rs);
623  
624 if (strlen($comment) > 0 || strlen($row['mimetype']) > 0 || strlen($row['transformation']) > 0 || strlen($row['transformation_options']) > 0) {
625 $upd_query = '
626 UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
627 SET ' . PMA_backquote('comment') . ' = \'' . PMA_sqlAddslashes($comment) . '\'
628 WHERE ' . $cols['db_name'] . ' = \'' . PMA_sqlAddslashes($db) . '\'
629 AND ' . $cols['table_name'] . ' = \'' . PMA_sqlAddslashes($table) . '\'
630 AND ' . $cols['column_name'] . ' = \'' . PMA_sqlAddSlashes($col) . '\'';
631 } else {
632 $upd_query = '
633 DELETE FROM
634 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
635 WHERE ' . $cols['db_name'] . ' = \'' . PMA_sqlAddslashes($db) . '\'
636 AND ' . $cols['table_name'] . ' = \'' . PMA_sqlAddslashes($table) . '\'
637 AND ' . $cols['column_name'] . ' = \'' . PMA_sqlAddslashes($col) . '\'';
638 }
639 } elseif (strlen($comment) > 0) {
640 $upd_query = '
641 INSERT INTO
642 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
643 (db_name, table_name, column_name, ' . PMA_backquote('comment') . ')
644 VALUES (
645 \'' . PMA_sqlAddslashes($db) . '\',
646 \'' . PMA_sqlAddslashes($table) . '\',
647 \'' . PMA_sqlAddslashes($col) . '\',
648 \'' . PMA_sqlAddslashes($comment) . '\')';
649 }
650  
651 if (isset($upd_query)){
652 $upd_rs = PMA_query_as_cu($upd_query);
653 unset($upd_query);
654 return true;
655 } else {
656 return false;
657 }
658 } // end of 'PMA_setComment()' function
659  
660 /**
661 * Set a SQL history entry
662 *
663 * @param string the name of the db
664 * @param string the name of the table
665 * @param string the username
666 * @param string the sql query
667 *
668 * @global array the list of relations settings
669 *
670 * @return boolean true
671 *
672 * @access public
673 */
674 function PMA_setHistory($db, $table, $username, $sqlquery) {
675 global $cfgRelation;
676  
677 $hist_rs = PMA_query_as_cu('
678 INSERT INTO
679 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
680 ( ' . PMA_backquote('username') . ',
681 ' . PMA_backquote('db') . ',
682 ' . PMA_backquote('table') . ',
683 ' . PMA_backquote('timevalue') . ',
684 ' . PMA_backquote('sqlquery') . ' )
685 VALUES
686 ( \'' . PMA_sqlAddslashes($username) . '\',
687 \'' . PMA_sqlAddslashes($db) . '\',
688 \'' . PMA_sqlAddslashes($table) . '\',
689 NOW(),
690 \'' . PMA_sqlAddslashes($sqlquery) . '\' )');
691 return true;
692 } // end of 'PMA_setHistory()' function
693  
694 /**
695 * Gets a SQL history entry
696 *
697 * @param string the username
698 *
699 * @global array the list of relations settings
700 *
701 * @return array list of history items
702 *
703 * @access public
704 */
705 function PMA_getHistory($username) {
706 global $cfgRelation;
707  
708 $hist_query = '
709 SELECT ' . PMA_backquote('db') . ',
710 ' . PMA_backquote('table') . ',
711 ' . PMA_backquote('sqlquery') . '
712 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
713 WHERE username = \'' . PMA_sqlAddslashes($username) . '\'
714 ORDER BY id DESC';
715  
716 $hist_rs = PMA_query_as_cu($hist_query);
717 unset($hist_query);
718  
719 $history = array();
720  
721 while ($row = PMA_DBI_fetch_assoc($hist_rs)) {
722 $history[] = $row;
723 }
724 PMA_DBI_free_result($hist_rs);
725  
726 return $history;
727  
728 } // end of 'PMA_getHistory()' function
729  
730 /**
731 * Set a SQL history entry
732 *
733 * @param string the name of the db
734 * @param string the name of the table
735 * @param string the username
736 * @param string the sql query
737 *
738 * @global array the list of relations settings
739 * @global array global phpMyAdmin configuration
740 *
741 * @return boolean true
742 *
743 * @access public
744 */
745 function PMA_purgeHistory($username) {
746 global $cfgRelation, $cfg;
747  
748 $purge_query = '
749 SELECT timevalue
750 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
751 WHERE username = \'' . PMA_sqlAddSlashes($username) . '\'
752 ORDER BY timevalue DESC LIMIT ' . $cfg['QueryHistoryMax'] . ', 1';
753 $purge_rs = PMA_query_as_cu($purge_query);
754 $i = 0;
755 $row = PMA_DBI_fetch_row($purge_rs);
756 PMA_DBI_free_result($purge_rs);
757  
758 if (is_array($row) && isset($row[0]) && $row[0] > 0) {
759 $maxtime = $row[0];
760 // quotes added around $maxtime to prevent a difficult to
761 // reproduce problem
762 $remove_rs = PMA_query_as_cu('
763 DELETE FROM
764 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
765 WHERE timevalue <= \'' . $maxtime . '\'');
766 }
767  
768 return true;
769 } // end of 'PMA_purgeHistory()' function
770  
771  
772 /**
773 * Prepares the dropdown for one mode
774 *
775 * @param array the keys and values for foreigns
776 * @param string the current data of the dropdown
777 * @param string the needed mode
778 *
779 * @global array global phpMyAdmin configuration
780 *
781 * @return array the <option value=""><option>s
782 *
783 * @access private
784 */
785 function PMA_foreignDropdownBuild($foreign, $data, $mode) {
786 global $cfg;
787  
788 $reloptions = array();
789  
790 foreach ($foreign as $key => $value) {
791  
792 if (PMA_strlen($value) <= $cfg['LimitChars']) {
793 $vtitle = '';
794 $value = htmlspecialchars($value);
795 } else {
796 $vtitle = htmlspecialchars($value);
797 $value = htmlspecialchars(substr($value, 0, $cfg['LimitChars']) . '...');
798 }
799  
800 $reloption = ' <option value="' . htmlspecialchars($key) . '"';
801 if ($vtitle != '') {
802 $reloption .= ' title="' . $vtitle . '"';
803 }
804  
805 if ((string) $key == (string) $data) {
806 $reloption .= ' selected="selected"';
807 }
808  
809 if ($mode == 'content-id') {
810 $reloptions[] = $reloption . '>' . $value . '&nbsp;-&nbsp;' . htmlspecialchars($key) . '</option>' . "\n";
811 } else {
812 $reloptions[] = $reloption . '>' . htmlspecialchars($key) . '&nbsp;-&nbsp;' . $value . '</option>' . "\n";
813 }
814 } // end foreach
815  
816 return $reloptions;
817 } // end of 'PMA_foreignDropdownBuild' function
818  
819 /**
820 * Outputs dropdown with values of foreign fields
821 *
822 * @param string the query of the foreign keys
823 * @param string the foreign field
824 * @param string the foreign field to display
825 * @param string the current data of the dropdown
826 *
827 * @global array global phpMyAdmin configuration
828 *
829 * @return string the <option value=""><option>s
830 *
831 * @access public
832 */
833 function PMA_foreignDropdown($disp, $foreign_field, $foreign_display, $data, $max) {
834 global $cfg;
835  
836 $foreign = array();
837  
838 // collect the data
839 foreach ($disp as $relrow) {
840 $key = $relrow[$foreign_field];
841  
842 // if the display field has been defined for this foreign table
843 if ($foreign_display) {
844 $value = $relrow[$foreign_display];
845 } else {
846 $value = '';
847 } // end if ($foreign_display)
848  
849 $foreign[$key] = $value;
850 } // end foreach
851  
852 // beginning of dropdown
853 $ret = '<option value=""></option>' . "\n";
854  
855 // master array for dropdowns
856 $reloptions = array('content-id' => array(), 'id-content' => array());
857  
858 // sort for id-content
859 if ($cfg['NaturalOrder']) {
860 uksort($foreign, 'strnatcasecmp');
861 } else {
862 ksort($foreign);
863 }
864  
865 // build id-content dropdown
866 $reloptions['id-content'] = PMA_foreignDropdownBuild($foreign, $data, 'id-content');
867  
868 // sort for content-id
869 if ($cfg['NaturalOrder']) {
870 natcasesort($foreign);
871 } else {
872 asort($foreign);
873 }
874  
875 // build content-id dropdown
876 $reloptions['content-id'] = PMA_foreignDropdownBuild($foreign, $data, 'content-id');
877  
878  
879 // put the dropdown sections in correct order
880  
881 $c = count($cfg['ForeignKeyDropdownOrder']);
882 if ($c == 2) {
883 $top = $reloptions[$cfg['ForeignKeyDropdownOrder'][0]];
884 $bot = $reloptions[$cfg['ForeignKeyDropdownOrder'][1]];
885 } elseif ($c == 1) {
886 $bot = $reloptions[$cfg['ForeignKeyDropdownOrder'][0]];
887 $top = null;
888 } else {
889 $top = $reloptions['id-content'];
890 $bot = $reloptions['content-id'];
891 }
892 $str_bot = implode('', $bot);
893 if ($top !== null) {
894 $str_top = implode('', $top);
895 $top_count = count($top);
896 if ($max == -1 || $top_count < $max) {
897 $ret .= $str_top;
898 if ($top_count > 0) {
899 $ret .= ' <option value=""></option>' . "\n";
900 $ret .= ' <option value=""></option>' . "\n";
901 }
902 }
903 }
904 $ret .= $str_bot;
905  
906 return $ret;
907 } // end of 'PMA_foreignDropdown()' function
908  
909 ?>