Fix link.
[phpmyadmin/crack.git] / libraries / relation.lib.php
blobd8993e593afddb36fb8e394240f913931adacec0
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used with the relation and pdf feature
6 * @version $Id$
7 * @package phpMyAdmin
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
16 require_once './libraries/Table.class.php';
18 /**
19 * Executes a query as controluser if possible, otherwise as normal user
21 * @param string the query to execute
22 * @param boolean whether to display SQL error messages or not
24 * @return integer the result set, or false if no result set
26 * @access public
28 * @author Mike Beck <mikebeck@users.sourceforge.net>
30 function PMA_query_as_cu($sql, $show_error = true, $options = 0)
32 if ($show_error) {
33 $result = PMA_DBI_query($sql, $GLOBALS['controllink'], $options);
34 } else {
35 $result = @PMA_DBI_try_query($sql, $GLOBALS['controllink'], $options);
36 } // end if... else...
38 if ($result) {
39 return $result;
40 } else {
41 return false;
43 } // end of the "PMA_query_as_cu()" function
45 /**
46 * @uses $_SESSION['relation' . $GLOBALS['server']] for caching
47 * @uses $GLOBALS['cfgRelation'] to set it
48 * @uses $GLOBALS['server'] to ensure we are using server-specific pmadb
49 * @uses PMA__getRelationsParam()
50 * @uses PMA_printRelationsParamDiagnostic()
51 * @param bool $verbose whether to print diagnostic info
52 * @return array $cfgRelation
54 function PMA_getRelationsParam($verbose = false)
56 if (empty($_SESSION['relation' . $GLOBALS['server']])) {
57 $_SESSION['relation' . $GLOBALS['server']] = PMA__getRelationsParam();
60 if ($verbose) {
61 PMA_printRelationsParamDiagnostic($_SESSION['relation' . $GLOBALS['server']]);
64 // just for BC
65 $GLOBALS['cfgRelation'] = $_SESSION['relation' . $GLOBALS['server']];
67 return $_SESSION['relation' . $GLOBALS['server']];
70 /**
71 * prints out diagnostic info for pma relation feature
73 * @uses $GLOBALS['server']
74 * @uses $GLOBALS['controllink']
75 * @uses $GLOBALS['strNotOK']
76 * @uses $GLOBALS['strDocu']
77 * @uses $GLOBALS['strGeneralRelationFeat']
78 * @uses $GLOBALS['strDisabled']
79 * @uses $GLOBALS['strEnabled']
80 * @uses $GLOBALS['strDisplayFeat']
81 * @uses $GLOBALS['strCreatePdfFeat']
82 * @uses $GLOBALS['strColComFeat']
83 * @uses $GLOBALS['strBookmarkQuery']
84 * @uses $GLOBALS['strUpdComTab']
85 * @uses $GLOBALS['strQuerySQLHistory']
86 * @uses $GLOBALS['strDesigner']
87 * @uses $cfg['Server']['pmadb']
88 * @uses sprintf()
89 * @uses PMA_printDiagMessageForFeature()
90 * @uses PMA_printDiagMessageForParameter()
91 * @param array $cfgRelation
93 function PMA_printRelationsParamDiagnostic($cfgRelation)
95 $messages['error'] = '<font color="red"><strong>' . $GLOBALS['strNotOK']
96 . '</strong></font> [ <a href="Documentation.html#%s" target="documentation">'
97 . $GLOBALS['strDocu'] . '</a> ]';
99 $messages['ok'] = '<font color="green"><strong>' . $GLOBALS['strOK'] . '</strong></font>';
100 $messages['enabled'] = '<font color="green">' . $GLOBALS['strEnabled'] . '</font>';
101 $messages['disabled'] = '<font color="red">' . $GLOBALS['strDisabled'] . '</font>';
103 if (false === $GLOBALS['cfg']['Server']['pmadb']) {
104 echo 'PMA Database ... '
105 . sprintf($messages['error'], 'pmadb')
106 . '<br />' . "\n"
107 . $GLOBALS['strGeneralRelationFeat']
108 . ' <font color="green">' . $GLOBALS['strDisabled']
109 . '</font>' . "\n";
110 return;
113 echo '<table>' . "\n";
115 PMA_printDiagMessageForParameter('pmadb', $GLOBALS['cfg']['Server']['pmadb'], $messages, 'pmadb');
117 PMA_printDiagMessageForParameter('relation', isset($cfgRelation['relation']), $messages, 'relation');
119 PMA_printDiagMessageForFeature('strGeneralRelationFeat', 'relwork', $messages);
121 PMA_printDiagMessageForParameter('table_info', isset($cfgRelation['table_info']), $messages, 'table_info');
123 PMA_printDiagMessageForFeature('strDisplayFeat', 'displaywork', $messages);
125 PMA_printDiagMessageForParameter('table_coords', isset($cfgRelation['table_coords']), $messages, 'table_coords');
127 PMA_printDiagMessageForParameter('pdf_pages', isset($cfgRelation['pdf_pages']), $messages, 'table_coords');
129 PMA_printDiagMessageForFeature('strCreatePdfFeat', 'pdfwork', $messages);
131 PMA_printDiagMessageForParameter('column_info', isset($cfgRelation['column_info']), $messages, 'col_com');
133 PMA_printDiagMessageForFeature('strColComFeat', 'commwork', $messages, false);
135 PMA_printDiagMessageForFeature('strBookmarkQuery', 'bookmarkwork', $messages, false);
137 PMA_printDiagMessageForFeature('strMIME_transformation', 'mimework', $messages);
139 if ($cfgRelation['commwork'] && ! $cfgRelation['mimework']) {
140 echo '<tr><td colspan=2 align="left">' . $GLOBALS['strUpdComTab'] . '</td></tr>' . "\n";
143 PMA_printDiagMessageForParameter('history', isset($cfgRelation['history']), $messages, 'history');
145 PMA_printDiagMessageForFeature('strQuerySQLHistory', 'historywork', $messages);
147 PMA_printDiagMessageForParameter('designer_coords', isset($cfgRelation['designer_coords']), $messages, 'designer_coords');
149 PMA_printDiagMessageForFeature('strDesigner', 'designerwork', $messages);
151 echo '</table>' . "\n";
155 * prints out one diagnostic message for a feature
157 * @param string feature name in a message string
158 * @param string the $GLOBALS['cfgRelation'] parameter to check
159 * @param array utility messages
160 * @param boolean whether to skip a line after the message
162 function PMA_printDiagMessageForFeature($feature_name, $relation_parameter, $messages, $skip_line=true)
164 echo ' <tr><td colspan=2 align="right">' . $GLOBALS[$feature_name] . ': '
165 . ($GLOBALS['cfgRelation'][$relation_parameter] ? $messages['enabled'] : $messages['disabled'])
166 . '</td></tr>' . "\n";
167 if ($skip_line) {
168 echo ' <tr><td>&nbsp;</td></tr>' . "\n";
173 * prints out one diagnostic message for a configuration parameter
175 * @param string config parameter name to display
176 * @param boolean whether this parameter is set
177 * @param array utility messages
178 * @param string anchor in Documentation.html
180 function PMA_printDiagMessageForParameter($parameter, $relation_parameter_set, $messages, $doc_anchor)
182 echo ' <tr><th align="left">';
183 echo '$cfg[\'Servers\'][$i][\'' . $parameter . '\'] ... </th><td align="right">';
184 echo ($relation_parameter_set ? $messages['ok'] : sprintf($messages['error'], $doc_anchor)) . '</td></tr>' . "\n";
189 * Defines the relation parameters for the current user
190 * just a copy of the functions used for relations ;-)
191 * but added some stuff to check what will work
193 * @uses $cfg['Server']['user']
194 * @uses $cfg['Server']['pmadb']
195 * @uses $cfg['Server']['verbose_check']
196 * @uses $GLOBALS['server']
197 * @uses $GLOBALS['controllink']
198 * @uses PMA_DBI_QUERY_STORE
199 * @uses PMA_DBI_select_db()
200 * @uses PMA_backquote()
201 * @uses PMA_query_as_cu()
202 * @uses PMA_DBI_fetch_row()
203 * @uses PMA_DBI_free_result()
204 * @access protected
205 * @author Mike Beck <mikebeck@users.sourceforge.net>
206 * @return array the relation parameters for the current user
208 function PMA__getRelationsParam()
210 $cfgRelation = array();
211 $cfgRelation['relwork'] = false;
212 $cfgRelation['displaywork'] = false;
213 $cfgRelation['bookmarkwork']= false;
214 $cfgRelation['pdfwork'] = false;
215 $cfgRelation['commwork'] = false;
216 $cfgRelation['mimework'] = false;
217 $cfgRelation['historywork'] = false;
218 $cfgRelation['designerwork'] = false;
219 $cfgRelation['allworks'] = false;
220 $cfgRelation['user'] = null;
221 $cfgRelation['db'] = null;
223 if ($GLOBALS['server'] == 0 || empty($GLOBALS['cfg']['Server']['pmadb'])
224 || ! PMA_DBI_select_db($GLOBALS['cfg']['Server']['pmadb'], $GLOBALS['controllink'])) {
225 // No server selected -> no bookmark table
226 // we return the array with the falses in it,
227 // to avoid some 'Unitialized string offset' errors later
228 $GLOBALS['cfg']['Server']['pmadb'] = false;
229 return $cfgRelation;
233 $cfgRelation['user'] = $GLOBALS['cfg']['Server']['user'];
234 $cfgRelation['db'] = $GLOBALS['cfg']['Server']['pmadb'];
236 // Now I just check if all tables that i need are present so I can for
237 // example enable relations but not pdf...
238 // I was thinking of checking if they have all required columns but I
239 // fear it might be too slow
241 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($GLOBALS['cfg']['Server']['pmadb']);
242 $tab_rs = PMA_query_as_cu($tab_query, false, PMA_DBI_QUERY_STORE);
244 if (! $tab_rs) {
245 // query failed ... ?
246 //$GLOBALS['cfg']['Server']['pmadb'] = false;
247 return $cfgRelation;
250 while ($curr_table = @PMA_DBI_fetch_row($tab_rs)) {
251 if ($curr_table[0] == $GLOBALS['cfg']['Server']['bookmarktable']) {
252 $cfgRelation['bookmark'] = $curr_table[0];
253 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['relation']) {
254 $cfgRelation['relation'] = $curr_table[0];
255 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['table_info']) {
256 $cfgRelation['table_info'] = $curr_table[0];
257 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['table_coords']) {
258 $cfgRelation['table_coords'] = $curr_table[0];
259 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['designer_coords']) {
260 $cfgRelation['designer_coords'] = $curr_table[0];
261 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['column_info']) {
262 $cfgRelation['column_info'] = $curr_table[0];
263 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['pdf_pages']) {
264 $cfgRelation['pdf_pages'] = $curr_table[0];
265 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['history']) {
266 $cfgRelation['history'] = $curr_table[0];
268 } // end while
269 PMA_DBI_free_result($tab_rs);
271 if (isset($cfgRelation['relation'])) {
272 $cfgRelation['relwork'] = true;
273 if (isset($cfgRelation['table_info'])) {
274 $cfgRelation['displaywork'] = true;
277 if (isset($cfgRelation['table_coords']) && isset($cfgRelation['pdf_pages'])) {
278 $cfgRelation['pdfwork'] = true;
280 if (isset($cfgRelation['column_info'])) {
281 $cfgRelation['commwork'] = true;
283 if ($GLOBALS['cfg']['Server']['verbose_check']) {
284 $mime_query = 'SHOW FIELDS FROM '
285 . PMA_backquote($cfgRelation['db']) . '.'
286 . PMA_backquote($cfgRelation['column_info']);
287 $mime_rs = PMA_query_as_cu($mime_query, false);
289 $mime_field_mimetype = false;
290 $mime_field_transformation = false;
291 $mime_field_transformation_options = false;
292 while ($curr_mime_field = @PMA_DBI_fetch_row($mime_rs)) {
293 if ($curr_mime_field[0] == 'mimetype') {
294 $mime_field_mimetype = true;
295 } elseif ($curr_mime_field[0] == 'transformation') {
296 $mime_field_transformation = true;
297 } elseif ($curr_mime_field[0] == 'transformation_options') {
298 $mime_field_transformation_options = true;
301 PMA_DBI_free_result($mime_rs);
303 if ($mime_field_mimetype
304 && $mime_field_transformation
305 && $mime_field_transformation_options) {
306 $cfgRelation['mimework'] = true;
308 } else {
309 $cfgRelation['mimework'] = true;
313 if (isset($cfgRelation['history'])) {
314 $cfgRelation['historywork'] = true;
317 // we do not absolutely need that the internal relations or the PDF
318 // schema feature be activated
319 if (isset($cfgRelation['designer_coords'])) {
320 $cfgRelation['designerwork'] = true;
323 if (isset($cfgRelation['bookmark'])) {
324 $cfgRelation['bookmarkwork'] = true;
327 if ($cfgRelation['relwork'] && $cfgRelation['displaywork']
328 && $cfgRelation['pdfwork'] && $cfgRelation['commwork']
329 && $cfgRelation['mimework'] && $cfgRelation['historywork']
330 && $cfgRelation['bookmarkwork'] && $cfgRelation['designerwork']) {
331 $cfgRelation['allworks'] = true;
334 return $cfgRelation;
335 } // end of the 'PMA_getRelationsParam()' function
338 * Gets all Relations to foreign tables for a given table or
339 * optionally a given column in a table
341 * @author Mike Beck <mikebeck@users.sourceforge.net>
342 * @author Marc Delisle
343 * @access public
344 * @uses $GLOBALS['controllink']
345 * @uses $GLOBALS['information_schema_relations']
346 * @uses PMA_getRelationsParam()
347 * @uses PMA_backquote()
348 * @uses PMA_sqlAddslashes()
349 * @uses PMA_DBI_fetch_result()
350 * @uses PMA_DBI_fetch_value()
351 * @uses PMA_SQP_analyze()
352 * @uses PMA_SQP_parse()
353 * @uses count()
354 * @uses strlen()
355 * @param string $db the name of the db to check for
356 * @param string $table the name of the table to check for
357 * @param string $column the name of the column to check for
358 * @param string $source the source for foreign key information
359 * @return array db,table,column
361 function PMA_getForeigners($db, $table, $column = '', $source = 'both')
363 $cfgRelation = PMA_getRelationsParam();
364 $foreign = array();
366 if ($cfgRelation['relwork'] && ($source == 'both' || $source == 'internal')) {
367 $rel_query = '
368 SELECT `master_field`,
369 `foreign_db`,
370 `foreign_table`,
371 `foreign_field`
372 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation']) . '
373 WHERE `master_db` = \'' . PMA_sqlAddslashes($db) . '\'
374 AND `master_table` = \'' . PMA_sqlAddslashes($table) . '\' ';
375 if (strlen($column)) {
376 $rel_query .= ' AND `master_field` = \'' . PMA_sqlAddslashes($column) . '\'';
378 $foreign = PMA_DBI_fetch_result($rel_query, 'master_field', null, $GLOBALS['controllink']);
381 if (($source == 'both' || $source == 'foreign') && strlen($table)) {
382 $show_create_table_query = 'SHOW CREATE TABLE '
383 . PMA_backquote($db) . '.' . PMA_backquote($table);
384 $show_create_table = PMA_DBI_fetch_value($show_create_table_query, 0, 1);
385 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
387 foreach ($analyzed_sql[0]['foreign_keys'] as $one_key) {
388 // The analyzer may return more than one column name in the
389 // index list or the ref_index_list; if this happens,
390 // the current logic just discards the whole index; having
391 // more than one index field is currently unsupported (see FAQ 3.6)
392 if (count($one_key['index_list']) == 1) {
393 foreach ($one_key['index_list'] as $i => $field) {
394 // If a foreign key is defined in the 'internal' source (pmadb)
395 // and as a native foreign key, we won't get it twice
396 // if $source='both' because we use $field as key
398 // The parser looks for a CONSTRAINT clause just before
399 // the FOREIGN KEY clause. It finds it (as output from
400 // SHOW CREATE TABLE) in MySQL 4.0.13, but not in older
401 // versions like 3.23.58.
402 // In those cases, the FOREIGN KEY parsing will put numbers
403 // like -1, 0, 1... instead of the constraint number.
405 if (isset($one_key['constraint'])) {
406 $foreign[$field]['constraint'] = $one_key['constraint'];
409 if (isset($one_key['ref_db_name'])) {
410 $foreign[$field]['foreign_db'] = $one_key['ref_db_name'];
411 } else {
412 $foreign[$field]['foreign_db'] = $db;
414 $foreign[$field]['foreign_table'] = $one_key['ref_table_name'];
415 $foreign[$field]['foreign_field'] = $one_key['ref_index_list'][$i];
416 if (isset($one_key['on_delete'])) {
417 $foreign[$field]['on_delete'] = $one_key['on_delete'];
419 if (isset($one_key['on_update'])) {
420 $foreign[$field]['on_update'] = $one_key['on_update'];
428 * Emulating relations for some information_schema tables
430 if ($db == 'information_schema'
431 && ($source == 'internal' || $source == 'both')) {
432 require_once './libraries/information_schema_relations.lib.php';
434 if (isset($GLOBALS['information_schema_relations'][$table])) {
435 foreach ($GLOBALS['information_schema_relations'][$table] as $field => $relations) {
436 if ((! strlen($column) || $column == $field)
437 && (! isset($foreign[$field]) || ! strlen($foreign[$field]))) {
438 $foreign[$field] = $relations;
444 return $foreign;
445 } // end of the 'PMA_getForeigners()' function
448 * Gets the display field of a table
450 * @access public
451 * @author Mike Beck <mikebeck@users.sourceforge.net>
452 * @uses $GLOBALS['controllink']
453 * @uses PMA_getRelationsParam()
454 * @uses PMA_backquote()
455 * @uses PMA_sqlAddslashes()
456 * @uses PMA_DBI_fetch_single_row()
457 * @uses trim()
458 * @param string $db the name of the db to check for
459 * @param string $table the name of the table to check for
460 * @return string field name
462 function PMA_getDisplayField($db, $table)
464 $cfgRelation = PMA_getRelationsParam();
467 * Try to fetch the display field from DB.
469 if ($cfgRelation['displaywork']) {
470 $disp_query = '
471 SELECT `display_field`
472 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['table_info']) . '
473 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
474 AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'';
476 $row = PMA_DBI_fetch_single_row($disp_query, 'ASSOC', $GLOBALS['controllink']);
477 if (isset($row['display_field'])) {
478 return $row['display_field'];
483 * Emulating the display field for some information_schema tables.
485 if ($db == 'information_schema') {
486 switch ($table) {
487 case 'CHARACTER_SETS': return 'DESCRIPTION';
488 case 'TABLES': return 'TABLE_COMMENT';
493 * No Luck...
495 return false;
497 } // end of the 'PMA_getDisplayField()' function
500 * Gets the comments for all rows of a table or the db itself
502 * @author Mike Beck <mikebeck@users.sourceforge.net>
503 * @author lem9
504 * @access public
505 * @uses PMA_DBI_get_fields()
506 * @uses PMA_getDbComment()
507 * @param string the name of the db to check for
508 * @param string the name of the table to check for
509 * @return array [field_name] = comment
511 function PMA_getComments($db, $table = '')
513 $comments = array();
515 if ($table != '') {
516 // MySQL native column comments
517 $fields = PMA_DBI_get_fields($db, $table);
518 if ($fields) {
519 foreach ($fields as $key => $field) {
520 if (! empty($field['Comment'])) {
521 $comments[$field['Field']] = $field['Comment'];
525 } else {
526 $comments[] = PMA_getDbComment($db);
529 return $comments;
530 } // end of the 'PMA_getComments()' function
533 * Gets the comment for a db
535 * @author Mike Beck <mikebeck@users.sourceforge.net>
536 * @author lem9
537 * @access public
538 * @uses PMA_DBI_QUERY_STORE
539 * @uses PMA_DBI_num_rows()
540 * @uses PMA_DBI_fetch_assoc()
541 * @uses PMA_DBI_free_result()
542 * @uses PMA_getRelationsParam()
543 * @uses PMA_backquote()
544 * @uses PMA_sqlAddslashes()
545 * @uses PMA_query_as_cu()
546 * @uses strlen()
547 * @param string the name of the db to check for
548 * @return string comment
550 function PMA_getDbComment($db)
552 $cfgRelation = PMA_getRelationsParam();
553 $comment = '';
555 if ($cfgRelation['commwork']) {
556 // pmadb internal db comment
557 $com_qry = "
558 SELECT `comment`
559 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
560 WHERE db_name = '" . PMA_sqlAddslashes($db) . "'
561 AND table_name = ''
562 AND column_name = '(db_comment)'";
563 $com_rs = PMA_query_as_cu($com_qry, true, PMA_DBI_QUERY_STORE);
565 if ($com_rs && PMA_DBI_num_rows($com_rs) > 0) {
566 $row = PMA_DBI_fetch_assoc($com_rs);
567 $comment = $row['comment'];
569 PMA_DBI_free_result($com_rs);
572 return $comment;
573 } // end of the 'PMA_getDbComment()' function
576 * Gets the comment for a db
578 * @author Mike Beck <mikebeck@users.sourceforge.net>
579 * @author lem9
580 * @access public
581 * @uses PMA_DBI_QUERY_STORE
582 * @uses PMA_DBI_num_rows()
583 * @uses PMA_DBI_fetch_assoc()
584 * @uses PMA_DBI_free_result()
585 * @uses PMA_getRelationsParam()
586 * @uses PMA_backquote()
587 * @uses PMA_sqlAddslashes()
588 * @uses PMA_query_as_cu()
589 * @uses strlen()
590 * @param string the name of the db to check for
591 * @return string comment
593 function PMA_getDbComments()
595 $cfgRelation = PMA_getRelationsParam();
596 $comments = array();
598 if ($cfgRelation['commwork']) {
599 // pmadb internal db comment
600 $com_qry = "
601 SELECT `db_name`, `comment`
602 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
603 WHERE `column_name` = '(db_comment)'";
604 $com_rs = PMA_query_as_cu($com_qry, true, PMA_DBI_QUERY_STORE);
606 if ($com_rs && PMA_DBI_num_rows($com_rs) > 0) {
607 while ($row = PMA_DBI_fetch_assoc($com_rs)) {
608 $comments[$row['db_name']] = $row['comment'];
611 PMA_DBI_free_result($com_rs);
614 return $comments;
615 } // end of the 'PMA_getDbComments()' function
618 * Set a database comment to a certain value.
620 * @uses PMA_getRelationsParam()
621 * @uses PMA_backquote()
622 * @uses PMA_sqlAddslashes()
623 * @uses PMA_query_as_cu()
624 * @uses strlen()
625 * @access public
626 * @param string $db the name of the db
627 * @param string $comment the value of the column
628 * @return boolean true, if comment-query was made.
630 function PMA_setDbComment($db, $comment = '')
632 $cfgRelation = PMA_getRelationsParam();
634 if (! $cfgRelation['commwork']) {
635 return false;
638 if (strlen($comment)) {
639 $upd_query = "
640 INSERT INTO
641 " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
642 (`db_name`, `table_name`, `column_name`, `comment`)
643 VALUES (
644 '" . PMA_sqlAddslashes($db) . "',
646 '(db_comment)',
647 '" . PMA_sqlAddslashes($comment) . "')
648 ON DUPLICATE KEY UPDATE
649 `comment` = '" . PMA_sqlAddslashes($comment) . "'";
650 } else {
651 $upd_query = '
652 DELETE FROM
653 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
654 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
655 AND `table_name` = \'\'
656 AND `column_name` = \'(db_comment)\'';
659 if (isset($upd_query)){
660 return PMA_query_as_cu($upd_query);
663 return false;
664 } // end of 'PMA_setDbComment()' function
667 * Set a SQL history entry
669 * @uses $_SESSION['sql_history']
670 * @uses $cfg['QueryHistoryMax']
671 * @uses PMA_getRelationsParam()
672 * @uses PMA_query_as_cu()
673 * @uses PMA_backquote()
674 * @uses PMA_sqlAddslashes()
675 * @uses count()
676 * @uses md5()
677 * @uses array_shift()
678 * @param string $db the name of the db
679 * @param string $table the name of the table
680 * @param string $username the username
681 * @param string $sqlquery the sql query
682 * @access public
684 function PMA_setHistory($db, $table, $username, $sqlquery)
686 if (strlen($sqlquery) > $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
687 return;
690 $cfgRelation = PMA_getRelationsParam();
692 if (! isset($_SESSION['sql_history'])) {
693 $_SESSION['sql_history'] = array();
696 $key = md5($sqlquery . $db . $table);
698 if (isset($_SESSION['sql_history'][$key])) {
699 unset($_SESSION['sql_history'][$key]);
702 $_SESSION['sql_history'][$key] = array(
703 'db' => $db,
704 'table' => $table,
705 'sqlquery' => $sqlquery,
708 if (count($_SESSION['sql_history']) > $GLOBALS['cfg']['QueryHistoryMax']) {
709 // history should not exceed a maximum count
710 array_shift($_SESSION['sql_history']);
713 if (! $cfgRelation['historywork']) {
714 return;
717 PMA_query_as_cu('
718 INSERT INTO
719 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
720 (`username`,
721 `db`,
722 `table`,
723 `timevalue`,
724 `sqlquery`)
725 VALUES
726 (\'' . PMA_sqlAddslashes($username) . '\',
727 \'' . PMA_sqlAddslashes($db) . '\',
728 \'' . PMA_sqlAddslashes($table) . '\',
729 NOW(),
730 \'' . PMA_sqlAddslashes($sqlquery) . '\')');
731 } // end of 'PMA_setHistory()' function
734 * Gets a SQL history entry
736 * @uses $_SESSION['sql_history']
737 * @uses $GLOBALS['controllink']
738 * @uses PMA_getRelationsParam()
739 * @uses PMA_backquote()
740 * @uses PMA_sqlAddslashes()
741 * @uses PMA_DBI_fetch_result()
742 * @uses array_reverse()
743 * @param string $username the username
744 * @return array list of history items
745 * @access public
747 function PMA_getHistory($username)
749 $cfgRelation = PMA_getRelationsParam();
751 if (isset($_SESSION['sql_history'])) {
752 return array_reverse($_SESSION['sql_history']);
755 if (! $cfgRelation['historywork']) {
756 return false;
759 $hist_query = '
760 SELECT `db`,
761 `table`,
762 `sqlquery`
763 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
764 WHERE `username` = \'' . PMA_sqlAddslashes($username) . '\'
765 ORDER BY `id` DESC';
767 return PMA_DBI_fetch_result($hist_query, null, null, $GLOBALS['controllink']);
768 } // end of 'PMA_getHistory()' function
771 * purges SQL history
773 * deletes entries that exceeds $cfg['QueryHistoryMax'], oldest first, for the
774 * given user
776 * @uses $cfg['QueryHistoryMax']
777 * @uses $cfg['QueryHistoryDB']
778 * @uses $GLOBALS['controllink']
779 * @uses PMA_backquote()
780 * @uses PMA_sqlAddSlashes()
781 * @uses PMA_query_as_cu()
782 * @uses PMA_DBI_fetch_value()
783 * @param string $username the username
784 * @access public
786 function PMA_purgeHistory($username)
788 $cfgRelation = PMA_getRelationsParam();
789 if (! $GLOBALS['cfg']['QueryHistoryDB'] || ! $cfgRelation['historywork']) {
790 return;
793 if (! $cfgRelation['historywork']) {
794 return;
797 $search_query = '
798 SELECT `timevalue`
799 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
800 WHERE `username` = \'' . PMA_sqlAddSlashes($username) . '\'
801 ORDER BY `timevalue` DESC
802 LIMIT ' . $GLOBALS['cfg']['QueryHistoryMax'] . ', 1';
804 if ($max_time = PMA_DBI_fetch_value($search_query, 0, 0, $GLOBALS['controllink'])) {
805 PMA_query_as_cu('
806 DELETE FROM
807 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
808 WHERE `username` = \'' . PMA_sqlAddSlashes($username) . '\'
809 AND `timevalue` <= \'' . $max_time . '\'');
811 } // end of 'PMA_purgeHistory()' function
814 * Prepares the dropdown for one mode
816 * @uses $cfg['LimitChars']
817 * @uses $cfg['NaturalOrder']
818 * @uses PMA_strlen()
819 * @uses htmlspecialchars()
820 * @uses substr()
821 * @uses uksort()
822 * @uses ksort()
823 * @uses natcasesort()
824 * @uses asort()
825 * @param array $foreign the keys and values for foreigns
826 * @param string $data the current data of the dropdown
827 * @param string $mode the needed mode
829 * @return array the <option value=""><option>s
831 * @access protected
833 function PMA__foreignDropdownBuild($foreign, $data, $mode)
835 $reloptions = array();
837 if ($mode == 'id-content') {
838 // sort for id-content
839 if ($GLOBALS['cfg']['NaturalOrder']) {
840 uksort($foreign, 'strnatcasecmp');
841 } else {
842 ksort($foreign);
844 } elseif ($mode == 'content-id') {
845 // sort for content-id
846 if ($GLOBALS['cfg']['NaturalOrder']) {
847 natcasesort($foreign);
848 } else {
849 asort($foreign);
853 foreach ($foreign as $key => $value) {
855 if (PMA_strlen($value) <= $GLOBALS['cfg']['LimitChars']) {
856 $vtitle = '';
857 $value = htmlspecialchars($value);
858 } else {
859 $vtitle = htmlspecialchars($value);
860 $value = htmlspecialchars(substr($value, 0, $GLOBALS['cfg']['LimitChars']) . '...');
863 $reloption = ' <option value="' . htmlspecialchars($key) . '"';
864 if ($vtitle != '') {
865 $reloption .= ' title="' . $vtitle . '"';
868 if ((string) $key == (string) $data) {
869 $reloption .= ' selected="selected"';
872 if ($mode == 'content-id') {
873 $reloptions[] = $reloption . '>' . $value . '&nbsp;-&nbsp;' . htmlspecialchars($key) . '</option>' . "\n";
874 } else {
875 $reloptions[] = $reloption . '>' . htmlspecialchars($key) . '&nbsp;-&nbsp;' . $value . '</option>' . "\n";
877 } // end foreach
879 return $reloptions;
880 } // end of 'PMA__foreignDropdownBuild' function
883 * Outputs dropdown with values of foreign fields
885 * @uses $cfg['ForeignKeyMaxLimit']
886 * @uses $cfg['ForeignKeyDropdownOrder']
887 * @uses PMA__foreignDropdownBuild()
888 * @uses PMA_isValid()
889 * @uses implode()
890 * @param array array of the displayed row
891 * @param string the foreign field
892 * @param string the foreign field to display
893 * @param string the current data of the dropdown (field in row)
894 * @return string the <option value=""><option>s
895 * @access public
897 function PMA_foreignDropdown($disp_row, $foreign_field, $foreign_display, $data,
898 $max = null)
900 if (null === $max) {
901 $max = $GLOBALS['cfg']['ForeignKeyMaxLimit'];
904 $foreign = array();
906 // collect the data
907 foreach ($disp_row as $relrow) {
908 $key = $relrow[$foreign_field];
910 // if the display field has been defined for this foreign table
911 if ($foreign_display) {
912 $value = $relrow[$foreign_display];
913 } else {
914 $value = '';
915 } // end if ($foreign_display)
917 $foreign[$key] = $value;
918 } // end foreach
920 // put the dropdown sections in correct order
921 $top = array();
922 $bot = array();
923 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'], 'array')) {
924 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][0])) {
925 $top = PMA__foreignDropdownBuild($foreign, $data,
926 $GLOBALS['cfg']['ForeignKeyDropdownOrder'][0]);
928 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][1])) {
929 $bot = PMA__foreignDropdownBuild($foreign, $data,
930 $GLOBALS['cfg']['ForeignKeyDropdownOrder'][1]);
932 } else {
933 $top = PMA__foreignDropdownBuild($foreign, $data, 'id-content');
934 $bot = PMA__foreignDropdownBuild($foreign, $data, 'content-id');
937 // beginning of dropdown
938 $ret = '<option value="">&nbsp;</option>' . "\n";
940 $top_count = count($top);
941 if ($max == -1 || $top_count < $max) {
942 $ret .= implode('', $top);
943 if ($top_count > 0) {
944 $ret .= ' <option value="">&nbsp;</option>' . "\n";
945 $ret .= ' <option value="">&nbsp;</option>' . "\n";
948 $ret .= implode('', $bot);
950 return $ret;
951 } // end of 'PMA_foreignDropdown()' function
954 * Gets foreign keys in preparation for a drop-down selector
955 * Thanks to <markus@noga.de>
957 * @uses PMA_Table::countRecords()
958 * @uses PMA_backquote()
959 * @uses PMA_getDisplayField()
960 * @uses PMA_sqlAddslashes()
961 * @uses PMA_DBI_fetch_value()
962 * @uses PMA_DBI_free_result()
963 * @uses PMA_DBI_query()
964 * @uses PMA_DBI_num_rows()
965 * @uses PMA_DBI_fetch_assoc()
966 * @param array array of the foreign keys
967 * @param string the foreign field name
968 * @param bool whether to override the total
969 * @param string a possible filter
970 * @param string a possible LIMIT clause
971 * @return array data about the foreign keys
972 * @access public
975 function PMA_getForeignData($foreigners, $field, $override_total, $foreign_filter, $foreign_limit)
977 // we always show the foreign field in the drop-down; if a display
978 // field is defined, we show it besides the foreign field
979 $foreign_link = false;
980 if ($foreigners && isset($foreigners[$field])) {
981 $foreigner = $foreigners[$field];
982 $foreign_db = $foreigner['foreign_db'];
983 $foreign_table = $foreigner['foreign_table'];
984 $foreign_field = $foreigner['foreign_field'];
986 // Count number of rows in the foreign table. Currently we do
987 // not use a drop-down if more than 200 rows in the foreign table,
988 // for speed reasons and because we need a better interface for this.
990 // We could also do the SELECT anyway, with a LIMIT, and ensure that
991 // the current value of the field is one of the choices.
993 $the_total = PMA_Table::countRecords($foreign_db, $foreign_table, TRUE);
995 if ($override_total == true || $the_total < $GLOBALS['cfg']['ForeignKeyMaxLimit']) {
996 // foreign_display can be FALSE if no display field defined:
997 $foreign_display = PMA_getDisplayField($foreign_db, $foreign_table);
999 $f_query_main = 'SELECT ' . PMA_backquote($foreign_field)
1000 . (($foreign_display == FALSE) ? '' : ', ' . PMA_backquote($foreign_display));
1001 $f_query_from = ' FROM ' . PMA_backquote($foreign_db) . '.' . PMA_backquote($foreign_table);
1002 $f_query_filter = empty($foreign_filter) ? '' : ' WHERE ' . PMA_backquote($foreign_field)
1003 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
1004 . (($foreign_display == FALSE) ? '' : ' OR ' . PMA_backquote($foreign_display)
1005 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
1007 $f_query_order = ($foreign_display == FALSE) ? '' :' ORDER BY ' . PMA_backquote($foreign_table) . '.' . PMA_backquote($foreign_display);
1008 $f_query_limit = isset($foreign_limit) ? $foreign_limit : '';
1010 if (!empty($foreign_filter)) {
1011 $res = PMA_DBI_query('SELECT COUNT(*)' . $f_query_from . $f_query_filter);
1012 if ($res) {
1013 $the_total = PMA_DBI_fetch_value($res);
1014 @PMA_DBI_free_result($res);
1015 } else {
1016 $the_total = 0;
1020 $disp = PMA_DBI_query($f_query_main . $f_query_from . $f_query_filter . $f_query_order . $f_query_limit);
1021 if ($disp && PMA_DBI_num_rows($disp) > 0) {
1022 // If a resultset has been created, pre-cache it in the $disp_row array
1023 // This helps us from not needing to use mysql_data_seek by accessing a pre-cached
1024 // PHP array. Usually those resultsets are not that big, so a performance hit should
1025 // not be expected.
1026 $disp_row = array();
1027 while ($single_disp_row = @PMA_DBI_fetch_assoc($disp)) {
1028 $disp_row[] = $single_disp_row;
1030 @PMA_DBI_free_result($disp);
1032 } else {
1033 $disp_row = null;
1034 $foreign_link = true;
1036 } // end if $foreigners
1038 $foreignData['foreign_link'] = $foreign_link;
1039 $foreignData['the_total'] = isset($the_total) ? $the_total : null;
1040 $foreignData['foreign_display'] = isset($foreign_display) ? $foreign_display : null;
1041 $foreignData['disp_row'] = isset($disp_row) ? $disp_row : null;
1042 $foreignData['foreign_field'] = isset($foreign_field) ? $foreign_field : null;
1043 return $foreignData;
1044 } // end of 'PMA_getForeignData()' function
1047 * Finds all related tables
1049 * @uses $GLOBALS['controllink']
1050 * @uses $GLOBALS['cfgRelation']
1051 * @uses $GLOBALS['db']
1052 * @param string whether to go from master to foreign or vice versa
1053 * @return boolean always TRUE
1054 * @global array $tab_left the list of tables that we still couldn't connect
1055 * @global array $tab_know the list of allready connected tables
1056 * @global string $fromclause
1058 * @access private
1060 function PMA_getRelatives($from)
1062 global $tab_left, $tab_know, $fromclause;
1064 if ($from == 'master') {
1065 $to = 'foreign';
1066 } else {
1067 $to = 'master';
1069 $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
1070 $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
1072 $rel_query = 'SELECT *'
1073 . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db'])
1074 . '.' . PMA_backquote($GLOBALS['cfgRelation']['relation'])
1075 . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\''
1076 . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\''
1077 . ' AND ' . $from . '_table IN ' . $in_know
1078 . ' AND ' . $to . '_table IN ' . $in_left;
1079 $relations = @PMA_DBI_query($rel_query, $GLOBALS['controllink']);
1080 while ($row = PMA_DBI_fetch_assoc($relations)) {
1081 $found_table = $row[$to . '_table'];
1082 if (isset($tab_left[$found_table])) {
1083 $fromclause
1084 .= "\n" . ' LEFT JOIN '
1085 . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($row[$to . '_table']) . ' ON '
1086 . PMA_backquote($row[$from . '_table']) . '.'
1087 . PMA_backquote($row[$from . '_field']) . ' = '
1088 . PMA_backquote($row[$to . '_table']) . '.'
1089 . PMA_backquote($row[$to . '_field']) . ' ';
1090 $tab_know[$found_table] = $found_table;
1091 unset($tab_left[$found_table]);
1093 } // end while
1095 return true;
1096 } // end of the "PMA_getRelatives()" function
1099 * Rename a field in relation tables
1101 * usually called after a field in a table was renamed in tbl_alter.php
1103 * @uses PMA_getRelationsParam()
1104 * @uses PMA_backquote()
1105 * @uses PMA_sqlAddslashes()
1106 * @uses PMA_query_as_cu()
1107 * @param string $db
1108 * @param string $table
1109 * @param string $field
1110 * @param string $new_name
1112 function PMA_REL_renameField($db, $table, $field, $new_name)
1114 $cfgRelation = PMA_getRelationsParam();
1116 if ($cfgRelation['displaywork']) {
1117 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
1118 . ' SET display_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1119 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
1120 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
1121 . ' AND display_field = \'' . PMA_sqlAddslashes($field) . '\'';
1122 PMA_query_as_cu($table_query);
1125 if ($cfgRelation['relwork']) {
1126 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1127 . ' SET master_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1128 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
1129 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
1130 . ' AND master_field = \'' . PMA_sqlAddslashes($field) . '\'';
1131 PMA_query_as_cu($table_query);
1133 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1134 . ' SET foreign_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1135 . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($db) . '\''
1136 . ' AND foreign_table = \'' . PMA_sqlAddslashes($table) . '\''
1137 . ' AND foreign_field = \'' . PMA_sqlAddslashes($field) . '\'';
1138 PMA_query_as_cu($table_query);
1139 } // end if relwork
1143 * Create a PDF page
1145 * @uses $GLOBALS['strNoDescription']
1146 * @uses PMA_backquote()
1147 * @uses $GLOBALS['cfgRelation']['db']
1148 * @uses PMA_sqlAddslashes()
1149 * @uses PMA_query_as_cu()
1150 * @uses PMA_DBI_insert_id()
1151 * @uses $GLOBALS['controllink']
1152 * @param string $newpage
1153 * @param array $cfgRelation
1154 * @param string $db
1155 * @param string $query_default_option
1156 * @return string $pdf_page_number
1158 function PMA_REL_create_page($newpage, $cfgRelation, $db, $query_default_option) {
1159 if (! isset($newpage) || $newpage == '') {
1160 $newpage = $GLOBALS['strNoDescription'];
1162 $ins_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
1163 . ' (db_name, page_descr)'
1164 . ' VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($newpage) . '\')';
1165 PMA_query_as_cu($ins_query, FALSE, $query_default_option);
1166 return PMA_DBI_insert_id(isset($GLOBALS['controllink']) ? $GLOBALS['controllink'] : '');