Translation update done using Pootle.
[phpmyadmin/crack.git] / libraries / relation.lib.php
blobcb8a23e6f994e5de47cdd6228def450d8764e3b4
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 * @package phpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
15 require_once './libraries/Table.class.php';
17 /**
18 * Executes a query as controluser if possible, otherwise as normal user
20 * @param string the query to execute
21 * @param boolean whether to display SQL error messages or not
23 * @return integer the result set, or false if no result set
25 * @access public
28 function PMA_query_as_controluser($sql, $show_error = true, $options = 0)
30 if ($show_error) {
31 $result = PMA_DBI_query($sql, $GLOBALS['controllink'], $options);
32 } else {
33 $result = @PMA_DBI_try_query($sql, $GLOBALS['controllink'], $options);
34 } // end if... else...
36 if ($result) {
37 return $result;
38 } else {
39 return false;
41 } // end of the "PMA_query_as_controluser()" function
43 /**
44 * @uses $_SESSION['relation'][$GLOBALS['server']] for caching
45 * @uses $GLOBALS['cfgRelation'] to set it
46 * @uses $GLOBALS['server'] to ensure we are using server-specific pmadb
47 * @uses PMA__getRelationsParam()
48 * @uses PMA_printRelationsParamDiagnostic()
49 * @param bool $verbose whether to print diagnostic info
50 * @return array $cfgRelation
52 function PMA_getRelationsParam($verbose = false)
54 if (empty($_SESSION['relation'][$GLOBALS['server']])) {
55 $_SESSION['relation'][$GLOBALS['server']] = PMA__getRelationsParam();
58 // just for BC but needs to be before PMA_printRelationsParamDiagnostic()
59 // which uses it
60 $GLOBALS['cfgRelation'] = $_SESSION['relation'][$GLOBALS['server']];
62 if ($verbose) {
63 PMA_printRelationsParamDiagnostic($_SESSION['relation'][$GLOBALS['server']]);
66 return $_SESSION['relation'][$GLOBALS['server']];
69 /**
70 * prints out diagnostic info for pma relation feature
72 * @uses $GLOBALS['server']
73 * @uses $GLOBALS['controllink']
74 * @uses $cfg['Server']['pmadb']
75 * @uses sprintf()
76 * @uses PMA_printDiagMessageForFeature()
77 * @uses PMA_printDiagMessageForParameter()
78 * @param array $cfgRelation
80 function PMA_printRelationsParamDiagnostic($cfgRelation)
82 $messages['error'] = '<font color="red"><strong>' . __('not OK')
83 . '</strong></font> [ <a href="Documentation.html#%s" target="documentation">'
84 . __('Documentation') . '</a> ]';
86 $messages['ok'] = '<font color="green"><strong>' . __('OK') . '</strong></font>';
87 $messages['enabled'] = '<font color="green">' . __('Enabled') . '</font>';
88 $messages['disabled'] = '<font color="red">' . __('Disabled') . '</font>';
90 if (false === $GLOBALS['cfg']['Server']['pmadb']) {
91 echo 'PMA Database ... '
92 . sprintf($messages['error'], 'pmadb')
93 . '<br />' . "\n"
94 . __('General relation features')
95 . ' <font color="green">' . __('Disabled')
96 . '</font>' . "\n";
97 return;
100 echo '<table>' . "\n";
102 PMA_printDiagMessageForParameter('pmadb', $GLOBALS['cfg']['Server']['pmadb'], $messages, 'pmadb');
104 PMA_printDiagMessageForParameter('relation', isset($cfgRelation['relation']), $messages, 'relation');
106 PMA_printDiagMessageForFeature(__('General relation features'), 'relwork', $messages);
108 PMA_printDiagMessageForParameter('table_info', isset($cfgRelation['table_info']), $messages, 'table_info');
110 PMA_printDiagMessageForFeature(__('Display Features'), 'displaywork', $messages);
112 PMA_printDiagMessageForParameter('table_coords', isset($cfgRelation['table_coords']), $messages, 'table_coords');
114 PMA_printDiagMessageForParameter('pdf_pages', isset($cfgRelation['pdf_pages']), $messages, 'table_coords');
116 PMA_printDiagMessageForFeature(__('Creation of PDFs'), 'pdfwork', $messages);
118 PMA_printDiagMessageForParameter('column_info', isset($cfgRelation['column_info']), $messages, 'col_com');
120 PMA_printDiagMessageForFeature(__('Displaying Column Comments'), 'commwork', $messages, false);
122 PMA_printDiagMessageForFeature(__('Browser transformation'), 'mimework', $messages);
124 if ($cfgRelation['commwork'] && ! $cfgRelation['mimework']) {
125 echo '<tr><td colspan=2 align="left">' . __('Please see the documentation on how to update your column_comments table') . '</td></tr>' . "\n";
128 PMA_printDiagMessageForParameter('bookmarktable', isset($cfgRelation['bookmark']), $messages, 'bookmark');
130 PMA_printDiagMessageForFeature(__('Bookmarked SQL query'), 'bookmarkwork', $messages);
132 PMA_printDiagMessageForParameter('history', isset($cfgRelation['history']), $messages, 'history');
134 PMA_printDiagMessageForFeature(__('SQL history'), 'historywork', $messages);
136 PMA_printDiagMessageForParameter('designer_coords', isset($cfgRelation['designer_coords']), $messages, 'designer_coords');
138 PMA_printDiagMessageForFeature(__('Designer'), 'designerwork', $messages);
140 PMA_printDiagMessageForParameter('tracking', isset($cfgRelation['tracking']), $messages, 'tracking');
142 PMA_printDiagMessageForFeature(__('Tracking'), 'trackingwork', $messages);
144 echo '</table>' . "\n";
146 echo '<p>' . __('Quick steps to setup advanced features:') . '</p>';
147 echo '<ul>';
148 echo '<li>' . __('Create the needed tables with the <code>script/create_tables.sql</code>.') . ' ' . PMA_showDocu('linked-tables') . '</li>';
149 echo '<li>' . __('Create a pma user and give access to these tables.') . ' ' . PMA_showDocu('pmausr') . '</li>';
150 echo '<li>' . __('Enable advanced features in configuration file (<code>config.inc.php</code>), for example by starting from <code>config.sample.inc.php</code>.') . ' ' . PMA_showDocu('quick_install') . '</li>';
151 echo '<li>' . __('Re-login to phpMyAdmin to load the updated configuration file.') . '</li>';
152 echo '</ul>';
156 * prints out one diagnostic message for a feature
158 * @param string feature name in a message string
159 * @param string the $GLOBALS['cfgRelation'] parameter to check
160 * @param array utility messages
161 * @param boolean whether to skip a line after the message
163 function PMA_printDiagMessageForFeature($feature_name, $relation_parameter, $messages, $skip_line=true)
165 echo ' <tr><td colspan=2 align="right">' . $feature_name . ': '
166 . ($GLOBALS['cfgRelation'][$relation_parameter] ? $messages['enabled'] : $messages['disabled'])
167 . '</td></tr>' . "\n";
168 if ($skip_line) {
169 echo ' <tr><td>&nbsp;</td></tr>' . "\n";
174 * prints out one diagnostic message for a configuration parameter
176 * @param string config parameter name to display
177 * @param boolean whether this parameter is set
178 * @param array utility messages
179 * @param string anchor in Documentation.html
181 function PMA_printDiagMessageForParameter($parameter, $relation_parameter_set, $messages, $doc_anchor)
183 echo ' <tr><th align="left">';
184 echo '$cfg[\'Servers\'][$i][\'' . $parameter . '\'] ... </th><td align="right">';
185 echo ($relation_parameter_set ? $messages['ok'] : sprintf($messages['error'], $doc_anchor)) . '</td></tr>' . "\n";
190 * Defines the relation parameters for the current user
191 * just a copy of the functions used for relations ;-)
192 * but added some stuff to check what will work
194 * @uses $cfg['Server']['user']
195 * @uses $cfg['Server']['pmadb']
196 * @uses $cfg['Server']['verbose_check']
197 * @uses $GLOBALS['server']
198 * @uses $GLOBALS['controllink']
199 * @uses PMA_DBI_QUERY_STORE
200 * @uses PMA_DBI_select_db()
201 * @uses PMA_backquote()
202 * @uses PMA_query_as_controluser()
203 * @uses PMA_DBI_fetch_row()
204 * @uses PMA_DBI_free_result()
205 * @access protected
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['trackingwork'] = false;
219 $cfgRelation['designerwork'] = false;
220 $cfgRelation['allworks'] = false;
221 $cfgRelation['user'] = null;
222 $cfgRelation['db'] = null;
224 if ($GLOBALS['server'] == 0 || empty($GLOBALS['cfg']['Server']['pmadb'])
225 || ! PMA_DBI_select_db($GLOBALS['cfg']['Server']['pmadb'], $GLOBALS['controllink'])) {
226 // No server selected -> no bookmark table
227 // we return the array with the falses in it,
228 // to avoid some 'Unitialized string offset' errors later
229 $GLOBALS['cfg']['Server']['pmadb'] = false;
230 return $cfgRelation;
234 $cfgRelation['user'] = $GLOBALS['cfg']['Server']['user'];
235 $cfgRelation['db'] = $GLOBALS['cfg']['Server']['pmadb'];
237 // Now I just check if all tables that i need are present so I can for
238 // example enable relations but not pdf...
239 // I was thinking of checking if they have all required columns but I
240 // fear it might be too slow
242 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($GLOBALS['cfg']['Server']['pmadb']);
243 $tab_rs = PMA_query_as_controluser($tab_query, false, PMA_DBI_QUERY_STORE);
245 if (! $tab_rs) {
246 // query failed ... ?
247 //$GLOBALS['cfg']['Server']['pmadb'] = false;
248 return $cfgRelation;
251 while ($curr_table = @PMA_DBI_fetch_row($tab_rs)) {
252 if ($curr_table[0] == $GLOBALS['cfg']['Server']['bookmarktable']) {
253 $cfgRelation['bookmark'] = $curr_table[0];
254 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['relation']) {
255 $cfgRelation['relation'] = $curr_table[0];
256 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['table_info']) {
257 $cfgRelation['table_info'] = $curr_table[0];
258 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['table_coords']) {
259 $cfgRelation['table_coords'] = $curr_table[0];
260 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['designer_coords']) {
261 $cfgRelation['designer_coords'] = $curr_table[0];
262 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['column_info']) {
263 $cfgRelation['column_info'] = $curr_table[0];
264 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['pdf_pages']) {
265 $cfgRelation['pdf_pages'] = $curr_table[0];
266 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['history']) {
267 $cfgRelation['history'] = $curr_table[0];
268 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['tracking']) {
269 $cfgRelation['tracking'] = $curr_table[0];
271 } // end while
272 PMA_DBI_free_result($tab_rs);
274 if (isset($cfgRelation['relation'])) {
275 $cfgRelation['relwork'] = true;
276 if (isset($cfgRelation['table_info'])) {
277 $cfgRelation['displaywork'] = true;
280 if (isset($cfgRelation['table_coords']) && isset($cfgRelation['pdf_pages'])) {
281 $cfgRelation['pdfwork'] = true;
283 if (isset($cfgRelation['column_info'])) {
284 $cfgRelation['commwork'] = true;
286 if ($GLOBALS['cfg']['Server']['verbose_check']) {
287 $mime_query = 'SHOW FIELDS FROM '
288 . PMA_backquote($cfgRelation['db']) . '.'
289 . PMA_backquote($cfgRelation['column_info']);
290 $mime_rs = PMA_query_as_controluser($mime_query, false);
292 $mime_field_mimetype = false;
293 $mime_field_transformation = false;
294 $mime_field_transformation_options = false;
295 while ($curr_mime_field = @PMA_DBI_fetch_row($mime_rs)) {
296 if ($curr_mime_field[0] == 'mimetype') {
297 $mime_field_mimetype = true;
298 } elseif ($curr_mime_field[0] == 'transformation') {
299 $mime_field_transformation = true;
300 } elseif ($curr_mime_field[0] == 'transformation_options') {
301 $mime_field_transformation_options = true;
304 PMA_DBI_free_result($mime_rs);
306 if ($mime_field_mimetype
307 && $mime_field_transformation
308 && $mime_field_transformation_options) {
309 $cfgRelation['mimework'] = true;
311 } else {
312 $cfgRelation['mimework'] = true;
316 if (isset($cfgRelation['history'])) {
317 $cfgRelation['historywork'] = true;
320 if (isset($cfgRelation['tracking'])) {
321 $cfgRelation['trackingwork'] = true;
324 // we do not absolutely need that the internal relations or the PDF
325 // schema feature be activated
326 if (isset($cfgRelation['designer_coords'])) {
327 $cfgRelation['designerwork'] = true;
330 if (isset($cfgRelation['bookmark'])) {
331 $cfgRelation['bookmarkwork'] = true;
334 if ($cfgRelation['relwork'] && $cfgRelation['displaywork']
335 && $cfgRelation['pdfwork'] && $cfgRelation['commwork']
336 && $cfgRelation['mimework'] && $cfgRelation['historywork']
337 && $cfgRelation['trackingwork']
338 && $cfgRelation['bookmarkwork'] && $cfgRelation['designerwork']) {
339 $cfgRelation['allworks'] = true;
342 return $cfgRelation;
343 } // end of the 'PMA_getRelationsParam()' function
346 * Gets all Relations to foreign tables for a given table or
347 * optionally a given column in a table
349 * @access public
350 * @uses $GLOBALS['controllink']
351 * @uses $GLOBALS['information_schema_relations']
352 * @uses PMA_getRelationsParam()
353 * @uses PMA_backquote()
354 * @uses PMA_sqlAddslashes()
355 * @uses PMA_DBI_fetch_result()
356 * @uses PMA_DBI_fetch_value()
357 * @uses PMA_SQP_analyze()
358 * @uses PMA_SQP_parse()
359 * @uses count()
360 * @uses strlen()
361 * @param string $db the name of the db to check for
362 * @param string $table the name of the table to check for
363 * @param string $column the name of the column to check for
364 * @param string $source the source for foreign key information
365 * @return array db,table,column
367 function PMA_getForeigners($db, $table, $column = '', $source = 'both')
369 $cfgRelation = PMA_getRelationsParam();
370 $foreign = array();
372 if ($cfgRelation['relwork'] && ($source == 'both' || $source == 'internal')) {
373 $rel_query = '
374 SELECT `master_field`,
375 `foreign_db`,
376 `foreign_table`,
377 `foreign_field`
378 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation']) . '
379 WHERE `master_db` = \'' . PMA_sqlAddslashes($db) . '\'
380 AND `master_table` = \'' . PMA_sqlAddslashes($table) . '\' ';
381 if (strlen($column)) {
382 $rel_query .= ' AND `master_field` = \'' . PMA_sqlAddslashes($column) . '\'';
384 $foreign = PMA_DBI_fetch_result($rel_query, 'master_field', null, $GLOBALS['controllink']);
387 if (($source == 'both' || $source == 'foreign') && strlen($table)) {
388 $show_create_table_query = 'SHOW CREATE TABLE '
389 . PMA_backquote($db) . '.' . PMA_backquote($table);
390 $show_create_table = PMA_DBI_fetch_value($show_create_table_query, 0, 1);
391 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
393 foreach ($analyzed_sql[0]['foreign_keys'] as $one_key) {
394 // The analyzer may return more than one column name in the
395 // index list or the ref_index_list; if this happens,
396 // the current logic just discards the whole index; having
397 // more than one index field is currently unsupported (see FAQ 3.6)
398 if (count($one_key['index_list']) == 1) {
399 foreach ($one_key['index_list'] as $i => $field) {
400 // If a foreign key is defined in the 'internal' source (pmadb)
401 // and as a native foreign key, we won't get it twice
402 // if $source='both' because we use $field as key
404 // The parser looks for a CONSTRAINT clause just before
405 // the FOREIGN KEY clause. It finds it (as output from
406 // SHOW CREATE TABLE) in MySQL 4.0.13, but not in older
407 // versions like 3.23.58.
408 // In those cases, the FOREIGN KEY parsing will put numbers
409 // like -1, 0, 1... instead of the constraint number.
411 if (isset($one_key['constraint'])) {
412 $foreign[$field]['constraint'] = $one_key['constraint'];
415 if (isset($one_key['ref_db_name'])) {
416 $foreign[$field]['foreign_db'] = $one_key['ref_db_name'];
417 } else {
418 $foreign[$field]['foreign_db'] = $db;
420 $foreign[$field]['foreign_table'] = $one_key['ref_table_name'];
421 $foreign[$field]['foreign_field'] = $one_key['ref_index_list'][$i];
422 if (isset($one_key['on_delete'])) {
423 $foreign[$field]['on_delete'] = $one_key['on_delete'];
425 if (isset($one_key['on_update'])) {
426 $foreign[$field]['on_update'] = $one_key['on_update'];
434 * Emulating relations for some information_schema tables
436 if ($db == 'information_schema'
437 && ($source == 'internal' || $source == 'both')) {
438 require_once './libraries/information_schema_relations.lib.php';
440 if (isset($GLOBALS['information_schema_relations'][$table])) {
441 foreach ($GLOBALS['information_schema_relations'][$table] as $field => $relations) {
442 if ((! strlen($column) || $column == $field)
443 && (! isset($foreign[$field]) || ! strlen($foreign[$field]))) {
444 $foreign[$field] = $relations;
450 return $foreign;
451 } // end of the 'PMA_getForeigners()' function
454 * Gets the display field of a table
456 * @access public
457 * @uses $GLOBALS['controllink']
458 * @uses PMA_getRelationsParam()
459 * @uses PMA_backquote()
460 * @uses PMA_sqlAddslashes()
461 * @uses PMA_DBI_fetch_single_row()
462 * @uses trim()
463 * @param string $db the name of the db to check for
464 * @param string $table the name of the table to check for
465 * @return string field name
467 function PMA_getDisplayField($db, $table)
469 $cfgRelation = PMA_getRelationsParam();
472 * Try to fetch the display field from DB.
474 if ($cfgRelation['displaywork']) {
475 $disp_query = '
476 SELECT `display_field`
477 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['table_info']) . '
478 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
479 AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'';
481 $row = PMA_DBI_fetch_single_row($disp_query, 'ASSOC', $GLOBALS['controllink']);
482 if (isset($row['display_field'])) {
483 return $row['display_field'];
488 * Emulating the display field for some information_schema tables.
490 if ($db == 'information_schema') {
491 switch ($table) {
492 case 'CHARACTER_SETS': return 'DESCRIPTION';
493 case 'TABLES': return 'TABLE_COMMENT';
498 * No Luck...
500 return false;
502 } // end of the 'PMA_getDisplayField()' function
505 * Gets the comments for all rows of a table or the db itself
507 * @access public
508 * @uses PMA_DBI_get_fields()
509 * @uses PMA_getDbComment()
510 * @param string the name of the db to check for
511 * @param string the name of the table to check for
512 * @return array [field_name] = comment
514 function PMA_getComments($db, $table = '')
516 $comments = array();
518 if ($table != '') {
519 // MySQL native column comments
520 $fields = PMA_DBI_get_fields($db, $table);
521 if ($fields) {
522 foreach ($fields as $key => $field) {
523 if (! empty($field['Comment'])) {
524 $comments[$field['Field']] = $field['Comment'];
528 } else {
529 $comments[] = PMA_getDbComment($db);
532 return $comments;
533 } // end of the 'PMA_getComments()' function
536 * Gets the comment for a db
538 * @access public
539 * @uses PMA_DBI_QUERY_STORE
540 * @uses PMA_DBI_num_rows()
541 * @uses PMA_DBI_fetch_assoc()
542 * @uses PMA_DBI_free_result()
543 * @uses PMA_getRelationsParam()
544 * @uses PMA_backquote()
545 * @uses PMA_sqlAddslashes()
546 * @uses PMA_query_as_controluser()
547 * @uses strlen()
548 * @param string the name of the db to check for
549 * @return string comment
551 function PMA_getDbComment($db)
553 $cfgRelation = PMA_getRelationsParam();
554 $comment = '';
556 if ($cfgRelation['commwork']) {
557 // pmadb internal db comment
558 $com_qry = "
559 SELECT `comment`
560 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
561 WHERE db_name = '" . PMA_sqlAddslashes($db) . "'
562 AND table_name = ''
563 AND column_name = '(db_comment)'";
564 $com_rs = PMA_query_as_controluser($com_qry, true, PMA_DBI_QUERY_STORE);
566 if ($com_rs && PMA_DBI_num_rows($com_rs) > 0) {
567 $row = PMA_DBI_fetch_assoc($com_rs);
568 $comment = $row['comment'];
570 PMA_DBI_free_result($com_rs);
573 return $comment;
574 } // end of the 'PMA_getDbComment()' function
577 * Gets the comment for a db
579 * @access public
580 * @uses PMA_DBI_QUERY_STORE
581 * @uses PMA_DBI_num_rows()
582 * @uses PMA_DBI_fetch_assoc()
583 * @uses PMA_DBI_free_result()
584 * @uses PMA_getRelationsParam()
585 * @uses PMA_backquote()
586 * @uses PMA_sqlAddslashes()
587 * @uses PMA_query_as_controluser()
588 * @uses strlen()
589 * @param string the name of the db to check for
590 * @return string comment
592 function PMA_getDbComments()
594 $cfgRelation = PMA_getRelationsParam();
595 $comments = array();
597 if ($cfgRelation['commwork']) {
598 // pmadb internal db comment
599 $com_qry = "
600 SELECT `db_name`, `comment`
601 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
602 WHERE `column_name` = '(db_comment)'";
603 $com_rs = PMA_query_as_controluser($com_qry, true, PMA_DBI_QUERY_STORE);
605 if ($com_rs && PMA_DBI_num_rows($com_rs) > 0) {
606 while ($row = PMA_DBI_fetch_assoc($com_rs)) {
607 $comments[$row['db_name']] = $row['comment'];
610 PMA_DBI_free_result($com_rs);
613 return $comments;
614 } // end of the 'PMA_getDbComments()' function
617 * Set a database comment to a certain value.
619 * @uses PMA_getRelationsParam()
620 * @uses PMA_backquote()
621 * @uses PMA_sqlAddslashes()
622 * @uses PMA_query_as_controluser()
623 * @uses strlen()
624 * @access public
625 * @param string $db the name of the db
626 * @param string $comment the value of the column
627 * @return boolean true, if comment-query was made.
629 function PMA_setDbComment($db, $comment = '')
631 $cfgRelation = PMA_getRelationsParam();
633 if (! $cfgRelation['commwork']) {
634 return false;
637 if (strlen($comment)) {
638 $upd_query = "
639 INSERT INTO
640 " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
641 (`db_name`, `table_name`, `column_name`, `comment`)
642 VALUES (
643 '" . PMA_sqlAddslashes($db) . "',
645 '(db_comment)',
646 '" . PMA_sqlAddslashes($comment) . "')
647 ON DUPLICATE KEY UPDATE
648 `comment` = '" . PMA_sqlAddslashes($comment) . "'";
649 } else {
650 $upd_query = '
651 DELETE FROM
652 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
653 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
654 AND `table_name` = \'\'
655 AND `column_name` = \'(db_comment)\'';
658 if (isset($upd_query)){
659 return PMA_query_as_controluser($upd_query);
662 return false;
663 } // end of 'PMA_setDbComment()' function
666 * Set a SQL history entry
668 * @uses $_SESSION['sql_history']
669 * @uses $cfg['QueryHistoryDB']
670 * @uses $cfg['QueryHistoryMax']
671 * @uses PMA_getRelationsParam()
672 * @uses PMA_query_as_controluser()
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'] || ! $GLOBALS['cfg']['QueryHistoryDB']) {
714 return;
717 PMA_query_as_controluser('
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_controluser()
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_controluser('
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
956 * @uses PMA_Table::countRecords()
957 * @uses PMA_backquote()
958 * @uses PMA_getDisplayField()
959 * @uses PMA_sqlAddslashes()
960 * @uses PMA_DBI_fetch_value()
961 * @uses PMA_DBI_free_result()
962 * @uses PMA_DBI_query()
963 * @uses PMA_DBI_num_rows()
964 * @uses PMA_DBI_fetch_assoc()
965 * @param array array of the foreign keys
966 * @param string the foreign field name
967 * @param bool whether to override the total
968 * @param string a possible filter
969 * @param string a possible LIMIT clause
970 * @return array data about the foreign keys
971 * @access public
974 function PMA_getForeignData($foreigners, $field, $override_total, $foreign_filter, $foreign_limit)
976 // we always show the foreign field in the drop-down; if a display
977 // field is defined, we show it besides the foreign field
978 $foreign_link = false;
979 if ($foreigners && isset($foreigners[$field])) {
980 $foreigner = $foreigners[$field];
981 $foreign_db = $foreigner['foreign_db'];
982 $foreign_table = $foreigner['foreign_table'];
983 $foreign_field = $foreigner['foreign_field'];
985 // Count number of rows in the foreign table. Currently we do
986 // not use a drop-down if more than 200 rows in the foreign table,
987 // for speed reasons and because we need a better interface for this.
989 // We could also do the SELECT anyway, with a LIMIT, and ensure that
990 // the current value of the field is one of the choices.
992 $the_total = PMA_Table::countRecords($foreign_db, $foreign_table);
994 if ($override_total == true || $the_total < $GLOBALS['cfg']['ForeignKeyMaxLimit']) {
995 // foreign_display can be FALSE if no display field defined:
996 $foreign_display = PMA_getDisplayField($foreign_db, $foreign_table);
998 $f_query_main = 'SELECT ' . PMA_backquote($foreign_field)
999 . (($foreign_display == FALSE) ? '' : ', ' . PMA_backquote($foreign_display));
1000 $f_query_from = ' FROM ' . PMA_backquote($foreign_db) . '.' . PMA_backquote($foreign_table);
1001 $f_query_filter = empty($foreign_filter) ? '' : ' WHERE ' . PMA_backquote($foreign_field)
1002 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
1003 . (($foreign_display == FALSE) ? '' : ' OR ' . PMA_backquote($foreign_display)
1004 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
1006 $f_query_order = ($foreign_display == FALSE) ? '' :' ORDER BY ' . PMA_backquote($foreign_table) . '.' . PMA_backquote($foreign_display);
1007 $f_query_limit = isset($foreign_limit) ? $foreign_limit : '';
1009 if (!empty($foreign_filter)) {
1010 $res = PMA_DBI_query('SELECT COUNT(*)' . $f_query_from . $f_query_filter);
1011 if ($res) {
1012 $the_total = PMA_DBI_fetch_value($res);
1013 @PMA_DBI_free_result($res);
1014 } else {
1015 $the_total = 0;
1019 $disp = PMA_DBI_query($f_query_main . $f_query_from . $f_query_filter . $f_query_order . $f_query_limit);
1020 if ($disp && PMA_DBI_num_rows($disp) > 0) {
1021 // If a resultset has been created, pre-cache it in the $disp_row array
1022 // This helps us from not needing to use mysql_data_seek by accessing a pre-cached
1023 // PHP array. Usually those resultsets are not that big, so a performance hit should
1024 // not be expected.
1025 $disp_row = array();
1026 while ($single_disp_row = @PMA_DBI_fetch_assoc($disp)) {
1027 $disp_row[] = $single_disp_row;
1029 @PMA_DBI_free_result($disp);
1031 } else {
1032 $disp_row = null;
1033 $foreign_link = true;
1035 } // end if $foreigners
1037 $foreignData['foreign_link'] = $foreign_link;
1038 $foreignData['the_total'] = isset($the_total) ? $the_total : null;
1039 $foreignData['foreign_display'] = isset($foreign_display) ? $foreign_display : null;
1040 $foreignData['disp_row'] = isset($disp_row) ? $disp_row : null;
1041 $foreignData['foreign_field'] = isset($foreign_field) ? $foreign_field : null;
1042 return $foreignData;
1043 } // end of 'PMA_getForeignData()' function
1046 * Finds all related tables
1048 * @uses $GLOBALS['controllink']
1049 * @uses $GLOBALS['cfgRelation']
1050 * @uses $GLOBALS['db']
1051 * @param string whether to go from master to foreign or vice versa
1052 * @return boolean always TRUE
1053 * @global array $tab_left the list of tables that we still couldn't connect
1054 * @global array $tab_know the list of allready connected tables
1055 * @global string $fromclause
1057 * @access private
1059 function PMA_getRelatives($from)
1061 global $tab_left, $tab_know, $fromclause;
1063 if ($from == 'master') {
1064 $to = 'foreign';
1065 } else {
1066 $to = 'master';
1068 $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
1069 $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
1071 $rel_query = 'SELECT *'
1072 . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db'])
1073 . '.' . PMA_backquote($GLOBALS['cfgRelation']['relation'])
1074 . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\''
1075 . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\''
1076 . ' AND ' . $from . '_table IN ' . $in_know
1077 . ' AND ' . $to . '_table IN ' . $in_left;
1078 $relations = @PMA_DBI_query($rel_query, $GLOBALS['controllink']);
1079 while ($row = PMA_DBI_fetch_assoc($relations)) {
1080 $found_table = $row[$to . '_table'];
1081 if (isset($tab_left[$found_table])) {
1082 $fromclause
1083 .= "\n" . ' LEFT JOIN '
1084 . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($row[$to . '_table']) . ' ON '
1085 . PMA_backquote($row[$from . '_table']) . '.'
1086 . PMA_backquote($row[$from . '_field']) . ' = '
1087 . PMA_backquote($row[$to . '_table']) . '.'
1088 . PMA_backquote($row[$to . '_field']) . ' ';
1089 $tab_know[$found_table] = $found_table;
1090 unset($tab_left[$found_table]);
1092 } // end while
1094 return true;
1095 } // end of the "PMA_getRelatives()" function
1098 * Rename a field in relation tables
1100 * usually called after a field in a table was renamed in tbl_alter.php
1102 * @uses PMA_getRelationsParam()
1103 * @uses PMA_backquote()
1104 * @uses PMA_sqlAddslashes()
1105 * @uses PMA_query_as_controluser()
1106 * @param string $db
1107 * @param string $table
1108 * @param string $field
1109 * @param string $new_name
1111 function PMA_REL_renameField($db, $table, $field, $new_name)
1113 $cfgRelation = PMA_getRelationsParam();
1115 if ($cfgRelation['displaywork']) {
1116 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
1117 . ' SET display_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1118 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
1119 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
1120 . ' AND display_field = \'' . PMA_sqlAddslashes($field) . '\'';
1121 PMA_query_as_controluser($table_query);
1124 if ($cfgRelation['relwork']) {
1125 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1126 . ' SET master_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1127 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
1128 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
1129 . ' AND master_field = \'' . PMA_sqlAddslashes($field) . '\'';
1130 PMA_query_as_controluser($table_query);
1132 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1133 . ' SET foreign_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1134 . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($db) . '\''
1135 . ' AND foreign_table = \'' . PMA_sqlAddslashes($table) . '\''
1136 . ' AND foreign_field = \'' . PMA_sqlAddslashes($field) . '\'';
1137 PMA_query_as_controluser($table_query);
1138 } // end if relwork
1142 * Create a PDF page
1144 * @uses PMA_backquote()
1145 * @uses $GLOBALS['cfgRelation']['db']
1146 * @uses PMA_sqlAddslashes()
1147 * @uses PMA_query_as_controluser()
1148 * @uses PMA_DBI_insert_id()
1149 * @uses $GLOBALS['controllink']
1150 * @param string $newpage
1151 * @param array $cfgRelation
1152 * @param string $db
1153 * @param string $query_default_option
1154 * @return string $pdf_page_number
1156 function PMA_REL_create_page($newpage, $cfgRelation, $db, $query_default_option) {
1157 if (! isset($newpage) || $newpage == '') {
1158 $newpage = __('no description');
1160 $ins_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
1161 . ' (db_name, page_descr)'
1162 . ' VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($newpage) . '\')';
1163 PMA_query_as_controluser($ins_query, FALSE, $query_default_option);
1164 return PMA_DBI_insert_id(isset($GLOBALS['controllink']) ? $GLOBALS['controllink'] : '');