update
[phpmyadmin/crack.git] / libraries / relation.lib.php3
blob92c2524db557fcd5b515f6c414d8c52c9604d9c6
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Set of functions used with the relation and pdf feature
7 */
10 if (!defined('PMA_RELATION_LIB_INCLUDED')){
11 define('PMA_RELATION_LIB_INCLUDED', 1);
13 /**
14 * Executes a query as controluser if possible, otherwise as normal user
16 * @param string the query to execute
17 * @param boolean whether to display SQL error messages or not
19 * @return integer the result id
21 * @global string the URL of the page to show in case of error
22 * @global string the name of db to come back to
23 * @global integer the ressource id of DB connect as controluser
24 * @global array configuration infos about the relations stuff
26 * @access public
28 * @author Mike Beck <mikebeck@users.sourceforge.net>
30 function PMA_query_as_cu($sql, $show_error = TRUE) {
31 global $err_url_0, $db, $dbh, $cfgRelation;
33 if (isset($dbh)) {
34 PMA_mysql_select_db($cfgRelation['db'], $dbh);
35 $result = @PMA_mysql_query($sql, $dbh);
36 if (!$result && $show_error == TRUE) {
37 PMA_mysqlDie(mysql_error($dbh), $sql, '', $err_url_0);
39 PMA_mysql_select_db($db, $dbh);
40 } else {
41 PMA_mysql_select_db($cfgRelation['db']);
42 $result = @PMA_mysql_query($sql);
43 if ($result && $show_error == TRUE) {
44 PMA_mysqlDie('', $sql, '', $err_url_0);
46 PMA_mysql_select_db($db);
47 } // end if... else...
49 if ($result) {
50 return $result;
51 } else {
52 return FALSE;
54 } // end of the "PMA_query_as_cu()" function
57 /**
58 * Defines the relation parameters for the current user
59 * just a copy of the functions used for relations ;-)
60 * but added some stuff to check what will work
62 * @param boolean whether to check validity of settings or not
64 * @return array the relation parameters for the current user
66 * @global array the list of settings for servers
67 * @global integer the id of the current server
68 * @global string the URL of the page to show in case of error
69 * @global string the name of the current db
70 * @global string the name of the current table
71 * @global array configuration infos about the relations stuff
73 * @access public
75 * @author Mike Beck <mikebeck@users.sourceforge.net>
77 function PMA_getRelationsParam($verbose = FALSE)
79 global $cfg, $server, $err_url_0, $db, $table;
80 global $cfgRelation;
82 $cfgRelation = array();
83 $cfgRelation['relwork'] = FALSE;
84 $cfgRelation['displaywork'] = FALSE;
85 $cfgRelation['bookmarkwork']= FALSE;
86 $cfgRelation['pdfwork'] = FALSE;
87 $cfgRelation['commwork'] = FALSE;
88 $cfgRelation['mimework'] = FALSE;
89 $cfgRelation['historywork'] = FALSE;
90 $cfgRelation['allworks'] = FALSE;
92 // No server selected -> no bookmark table
93 // we return the array with the FALSEs in it,
94 // to avoid some 'Unitialized string offset' errors later
95 if ($server == 0
96 || empty($cfg['Server'])
97 || empty($cfg['Server']['pmadb'])) {
98 if ($verbose == TRUE) {
99 echo 'PMA Database ... '
100 . '<font color="red"><b>' . $GLOBALS['strNotOK'] . '</b></font>'
101 . '[ <a href="Documentation.html#pmadb">' . $GLOBALS['strDocu'] . '</a> ]<br />' . "\n"
102 . $GLOBALS['strGeneralRelationFeat']
103 . ' <font color="green">' . $GLOBALS['strDisabled'] . '</font>' . "\n";
105 return $cfgRelation;
108 $cfgRelation['user'] = $cfg['Server']['user'];
109 $cfgRelation['db'] = $cfg['Server']['pmadb'];
111 // Now I just check if all tables that i need are present so I can for
112 // example enable relations but not pdf...
113 // I was thinking of checking if they have all required columns but I
114 // fear it might be too slow
115 // PMA_mysql_select_db($cfgRelation['db']);
117 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($cfgRelation['db']);
118 $tab_rs = PMA_query_as_cu($tab_query, FALSE);
120 while ($curr_table = @PMA_mysql_fetch_array($tab_rs)) {
121 if ($curr_table[0] == $cfg['Server']['bookmarktable']) {
122 $cfgRelation['bookmark'] = $curr_table[0];
123 } else if ($curr_table[0] == $cfg['Server']['relation']) {
124 $cfgRelation['relation'] = $curr_table[0];
125 } else if ($curr_table[0] == $cfg['Server']['table_info']) {
126 $cfgRelation['table_info'] = $curr_table[0];
127 } else if ($curr_table[0] == $cfg['Server']['table_coords']) {
128 $cfgRelation['table_coords'] = $curr_table[0];
129 } else if ($curr_table[0] == $cfg['Server']['column_info']) {
130 $cfgRelation['column_info'] = $curr_table[0];
131 } else if ($curr_table[0] == $cfg['Server']['pdf_pages']) {
132 $cfgRelation['pdf_pages'] = $curr_table[0];
133 } else if ($curr_table[0] == $cfg['Server']['history']) {
134 $cfgRelation['history'] = $curr_table[0];
136 } // end while
137 if (isset($cfgRelation['relation'])) {
138 $cfgRelation['relwork'] = TRUE;
139 if (isset($cfgRelation['table_info'])) {
140 $cfgRelation['displaywork'] = TRUE;
143 if (isset($cfgRelation['table_coords']) && isset($cfgRelation['pdf_pages'])) {
144 $cfgRelation['pdfwork'] = TRUE;
146 if (isset($cfgRelation['column_info'])) {
147 $cfgRelation['commwork'] = TRUE;
149 if ($cfg['Server']['verbose_check']) {
150 $mime_query = 'SHOW FIELDS FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']);
151 $mime_rs = PMA_query_as_cu($mime_query, FALSE);
153 $mime_field_mimetype = FALSE;
154 $mime_field_transformation = FALSE;
155 $mime_field_transformation_options = FALSE;
156 while ($curr_mime_field = @PMA_mysql_fetch_array($mime_rs)) {
157 if ($curr_mime_field[0] == 'mimetype') {
158 $mime_field_mimetype = TRUE;
159 } else if ($curr_mime_field[0] == 'transformation') {
160 $mime_field_transformation = TRUE;
161 } else if ($curr_mime_field[0] == 'transformation_options') {
162 $mime_field_transformation_options = TRUE;
166 if ($mime_field_mimetype == TRUE
167 && $mime_field_transformation == TRUE
168 && $mime_field_transformation_options == TRUE) {
169 $cfgRelation['mimework'] = TRUE;
171 } else {
172 $cfgRelation['mimework'] = TRUE;
176 if (isset($cfgRelation['history'])) {
177 $cfgRelation['historywork'] = TRUE;
180 if (isset($cfgRelation['bookmark'])) {
181 $cfgRelation['bookmarkwork'] = TRUE;
184 if ($cfgRelation['relwork'] == TRUE && $cfgRelation['displaywork'] == TRUE
185 && $cfgRelation['pdfwork'] == TRUE && $cfgRelation['commwork'] == TRUE
186 && $cfgRelation['mimework'] == TRUE && $cfgRelation['historywork'] == TRUE
187 && $cfgRelation['bookmarkwork'] == TRUE) {
188 $cfgRelation['allworks'] = TRUE;
190 if ($tab_rs) {
191 mysql_free_result($tab_rs);
192 } else {
193 $cfg['Server']['pmadb'] = FALSE;
196 if ($verbose == TRUE) {
197 $shit = '<font color="red"><b>' . $GLOBALS['strNotOK'] . '</b></font> [ <a href="Documentation.html#%s">' . $GLOBALS['strDocu'] . '</a> ]';
198 $hit = '<font color="green"><b>' . $GLOBALS['strOK'] . '</b></font>';
199 $enabled = '<font color="green">' . $GLOBALS['strEnabled'] . '</font>';
200 $disabled = '<font color="red">' . $GLOBALS['strDisabled'] . '</font>';
202 echo '<table>' . "\n";
203 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'pmadb\'] ... </th><td align="right">'
204 . (($cfg['Server']['pmadb'] == FALSE) ? sprintf($shit, 'pmadb') : $hit)
205 . '</td></tr>' . "\n";
206 echo ' <tr><td>&nbsp;</td></tr>' . "\n";
208 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'relation\'] ... </th><td align="right">'
209 . ((isset($cfgRelation['relation'])) ? $hit : sprintf($shit, 'relation'))
210 . '</td></tr>' . "\n";
211 echo ' <tr><td colspan=2 align="center">'. $GLOBALS['strGeneralRelationFeat'] . ': '
212 . (($cfgRelation['relwork'] == TRUE) ? $enabled : $disabled)
213 . '</td></tr>' . "\n";
214 echo ' <tr><td>&nbsp;</td></tr>' . "\n";
216 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'table_info\'] ... </th><td align="right">'
217 . (($cfgRelation['displaywork'] == FALSE) ? sprintf($shit, 'table_info') : $hit)
218 . '</td></tr>' . "\n";
219 echo ' <tr><td colspan=2 align="center">' . $GLOBALS['strDisplayFeat'] . ': '
220 . (($cfgRelation['displaywork'] == TRUE) ? $enabled : $disabled)
221 . '</td></tr>' . "\n";
222 echo ' <tr><td>&nbsp;</td></tr>' . "\n";
224 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'table_coords\'] ... </th><td align="right">'
225 . ((isset($cfgRelation['table_coords'])) ? $hit : sprintf($shit, 'table_coords'))
226 . '</td></tr>' . "\n";
227 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'pdf_pages\'] ... </th><td align="right">'
228 . ((isset($cfgRelation['pdf_pages'])) ? $hit : sprintf($shit, 'table_coords'))
229 . '</td></tr>' . "\n";
230 echo ' <tr><td colspan=2 align="center">' . $GLOBALS['strCreatePdfFeat'] . ': '
231 . (($cfgRelation['pdfwork'] == TRUE) ? $enabled : $disabled)
232 . '</td></tr>' . "\n";
233 echo ' <tr><td>&nbsp;</td></tr>' . "\n";
235 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'column_info\'] ... </th><td align="right">'
236 . ((isset($cfgRelation['column_info'])) ? $hit : sprintf($shit, 'col_com'))
237 . '</td></tr>' . "\n";
238 echo ' <tr><td colspan=2 align="center">' . $GLOBALS['strColComFeat'] . ': '
239 . (($cfgRelation['commwork'] == TRUE) ? $enabled : $disabled)
240 . '</td></tr>' . "\n";
241 echo ' <tr><td colspan=2 align="center">' . $GLOBALS['strBookmarkQuery'] . ': '
242 . (($cfgRelation['bookmarkwork'] == TRUE) ? $enabled : $disabled)
243 . '</td></tr>' . "\n";
244 echo ' <tr><th align="left">MIME ...</th><td align="right">'
245 . (($cfgRelation['mimework'] == TRUE) ? $hit : sprintf($shit, 'col_com'))
246 . '</td></tr>' . "\n";
248 if (($cfgRelation['commwork'] == TRUE) && ($cfgRelation['mimework'] != TRUE)) {
249 echo '<tr><td colspan=2 align="left">' . $GLOBALS['strUpdComTab'] . '</td></tr>' . "\n";
252 // . '<br />(MIME: ' . (($cfgRelation['mimework'] == TRUE) ? $enabled : $disabled) . ')'
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";
261 echo '</table>' . "\n";
262 } // end if ($verbose == TRUE) {
264 return $cfgRelation;
265 } // end of the 'PMA_getRelationsParam()' function
269 * Gets all Relations to foreign tables for a given table or
270 * optionally a given column in a table
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
277 * @return array db,table,column
279 * @global array the list of relations settings
280 * @global string the URL of the page to show in case of error
282 * @access public
284 * @author Mike Beck <mikebeck@users.sourceforge.net> and Marc Delisle
286 function PMA_getForeigners($db, $table, $column = '', $source = 'both') {
287 global $cfgRelation, $err_url_0;
289 if ($cfgRelation['relwork'] && ($source == 'both' || $source == 'internal')) {
290 $rel_query = 'SELECT master_field, foreign_db, foreign_table, foreign_field'
291 . ' FROM ' . PMA_backquote($cfgRelation['relation'])
292 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\' '
293 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\' ';
294 if (!empty($column)) {
295 $rel_query .= ' AND master_field = \'' . PMA_sqlAddslashes($column) . '\'';
297 $relations = PMA_query_as_cu($rel_query);
298 $i = 0;
299 while ($relrow = @PMA_mysql_fetch_array($relations)) {
300 $field = $relrow['master_field'];
301 $foreign[$field]['foreign_db'] = $relrow['foreign_db'];
302 $foreign[$field]['foreign_table'] = $relrow['foreign_table'];
303 $foreign[$field]['foreign_field'] = $relrow['foreign_field'];
304 $i++;
305 } // end while
308 if (($source == 'both' || $source == 'innodb') && !empty($table)) {
309 $show_create_table_query = 'SHOW CREATE TABLE '
310 . PMA_backquote($db) . '.' . PMA_backquote($table);
311 $show_create_table_res = PMA_mysql_query($show_create_table_query);
312 list(,$show_create_table) = PMA_mysql_fetch_row($show_create_table_res);
313 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
315 while (list(,$one_key) = each ($analyzed_sql[0]['foreign_keys'])) {
317 // TODO: the analyzer may return more than one column name in the
318 // index list or the ref_index_list but for now we take the first
319 $field = $one_key['index_list'][0];
321 // TODO: SHOW CREATE TABLE does not return the db name in
322 // the REFERENCES, so we assume the same db as master
324 // If a foreign key is defined in the 'internal' source (pmadb)
325 // and in 'innodb', we won't get it twice if $source='both'
326 // because we use $field as key
328 $foreign[$field]['foreign_db'] = $db;
329 $foreign[$field]['foreign_table'] = $one_key['ref_table_name'];
330 $foreign[$field]['foreign_field'] = $one_key['ref_index_list'][0];
334 if (isset($foreign) && is_array($foreign)) {
335 return $foreign;
336 } else {
337 return FALSE;
339 } // end of the 'PMA_getForeigners()' function
343 * Gets the display field of a table
345 * @param string the name of the db to check for
346 * @param string the name of the table to check for
348 * @return string field name
350 * @global array the list of relations settings
352 * @access public
354 * @author Mike Beck <mikebeck@users.sourceforge.net>
356 function PMA_getDisplayField($db, $table) {
357 global $cfgRelation;
359 $disp_query = 'SELECT display_field FROM ' . PMA_backquote($cfgRelation['table_info'])
360 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
361 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
363 $disp_res = PMA_query_as_cu($disp_query);
364 $row = ($disp_res ? PMA_mysql_fetch_array($disp_res) : '');
365 if (isset($row['display_field'])) {
366 return $row['display_field'];
367 } else {
368 return FALSE;
370 } // end of the 'PMA_getDisplayField()' function
374 * Gets the comments for all rows of a table
376 * @param string the name of the db to check for
377 * @param string the name of the table to check for
379 * @return array [field_name] = comment
381 * @global array the list of relations settings
383 * @access public
385 * @author Mike Beck <mikebeck@users.sourceforge.net>
387 function PMA_getComments($db, $table = '') {
388 global $cfgRelation;
390 if ($table != '') {
391 $com_qry = 'SELECT column_name, ' . PMA_backquote('comment') . ' FROM ' . PMA_backquote($cfgRelation['column_info'])
392 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
393 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
394 $com_rs = PMA_query_as_cu($com_qry);
395 } else {
396 $com_qry = 'SELECT comment FROM ' . PMA_backquote($cfgRelation['column_info'])
397 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
398 . ' AND table_name = \'\''
399 . ' AND column_name = \'(db_comment)\'';
400 $com_rs = PMA_query_as_cu($com_qry);
403 $i = 0;
404 while ($row = @PMA_mysql_fetch_array($com_rs)) {
405 $i++;
406 $col = ($table != '' ? $row['column_name'] : $i);
408 if (strlen($row['comment']) > 0) {
409 $comment[$col] = $row['comment'];
412 } // end while
414 if (isset($comment) && is_array($comment)) {
415 return $comment;
416 } else {
417 return FALSE;
419 } // end of the 'PMA_getComments()' function
422 * Adds/removes slashes if required
424 * @param string the string to slash
426 * @return string the slashed string
428 * @access public
430 function PMA_handleSlashes($val) {
431 return (get_magic_quotes_gpc() ? str_replace('\\"', '"', $val) : PMA_sqlAddslashes($val));
432 } // end of the "PMA_handleSlashes()" function
435 * Set a single comment to a certain value.
437 * @param string the name of the db
438 * @param string the name of the table
439 * @param string the name of the column
440 * @param string the value of the column
441 * @param string (optional) if a column is renamed, this is the name of the former key which will get deleted
443 * @return boolean true, if comment-query was made.
445 * @global array the list of relations settings
447 * @access public
449 function PMA_setComment($db, $table, $key, $value, $removekey = '') {
450 global $cfgRelation;
452 if ($removekey != '' AND $removekey != $key) {
453 $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['column_info'])
454 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
455 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
456 . ' AND column_name = \'' . PMA_sqlAddslashes($removekey) . '\'';
457 $rmv_rs = PMA_query_as_cu($remove_query);
458 unset($rmv_query);
461 $test_qry = 'SELECT ' . PMA_backquote('comment') . ', mimetype, transformation, transformation_options FROM ' . PMA_backquote($cfgRelation['column_info'])
462 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
463 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
464 . ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\'';
465 $test_rs = PMA_query_as_cu($test_qry);
467 if ($test_rs && mysql_num_rows($test_rs) > 0) {
468 $row = @PMA_mysql_fetch_array($test_rs);
470 if (strlen($value) > 0 || strlen($row['mimetype']) > 0 || strlen($row['transformation']) > 0 || strlen($row['transformation_options']) > 0) {
471 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['column_info'])
472 . ' SET ' . PMA_backquote('comment') . ' = \'' . PMA_sqlAddslashes($value) . '\''
473 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
474 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
475 . ' AND column_name = \'' . PMA_sqlAddSlashes($key) . '\'';
476 } else {
477 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['column_info'])
478 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
479 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
480 . ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\'';
482 } else if (strlen($value) > 0) {
483 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['column_info'])
484 . ' (db_name, table_name, column_name, ' . PMA_backquote('comment') . ') '
485 . ' VALUES('
486 . '\'' . PMA_sqlAddslashes($db) . '\','
487 . '\'' . PMA_sqlAddslashes($table) . '\','
488 . '\'' . PMA_sqlAddslashes($key) . '\','
489 . '\'' . PMA_sqlAddslashes($value) . '\')';
492 if (isset($upd_query)){
493 $upd_rs = PMA_query_as_cu($upd_query);
494 unset($upd_query);
495 return true;
496 } else {
497 return false;
499 } // end of 'PMA_setComment()' function
502 * Set a SQL history entry
504 * @param string the name of the db
505 * @param string the name of the table
506 * @param string the username
507 * @param string the sql query
509 * @return boolean true
511 * @access public
513 function PMA_setHistory($db, $table, $username, $sqlquery) {
514 global $cfgRelation;
516 $hist_rs = PMA_query_as_cu('INSERT INTO ' . PMA_backquote($cfgRelation['history']) . ' ('
517 . PMA_backquote('username') . ','
518 . PMA_backquote('db') . ','
519 . PMA_backquote('table') . ','
520 . PMA_backquote('timevalue') . ','
521 . PMA_backquote('sqlquery')
522 . ') VALUES ('
523 . '\'' . PMA_sqlAddslashes($username) . '\','
524 . '\'' . PMA_sqlAddslashes($db) . '\','
525 . '\'' . PMA_sqlAddslashes($table) . '\','
526 . 'NOW(),'
527 . '\'' . PMA_sqlAddslashes($sqlquery) . '\')');
528 return true;
529 } // end of 'PMA_setHistory()' function
532 * Gets a SQL history entry
534 * @param string the username
536 * @return array list of history items
538 * @access public
540 function PMA_getHistory($username) {
541 global $cfgRelation;
543 $hist_rs = PMA_query_as_cu('SELECT '
544 . PMA_backquote('db') . ','
545 . PMA_backquote('table') . ','
546 . PMA_backquote('sqlquery')
547 . ' FROM ' . PMA_backquote($cfgRelation['history']) . ' WHERE username = \'' . PMA_sqlAddslashes($username) . '\' ORDER BY id DESC');
549 $history = array();
551 while ($row = @PMA_mysql_fetch_array($hist_rs)) {
552 $history[] = $row;
555 return $history;
557 } // end of 'PMA_getHistory()' function
560 * Set a SQL history entry
562 * @param string the name of the db
563 * @param string the name of the table
564 * @param string the username
565 * @param string the sql query
567 * @return boolean true
569 * @access public
571 function PMA_purgeHistory($username) {
572 global $cfgRelation, $cfg;
574 $purge_rs = PMA_query_as_cu('SELECT timevalue FROM ' . PMA_backquote($cfgRelation['history']) . ' WHERE username = \'' . PMA_sqlAddSlashes($username) . '\' ORDER BY timevalue DESC LIMIT ' . $cfg['QueryHistoryMax'] . ', 1');
575 $i = 0;
576 $row = @PMA_mysql_fetch_array($purge_rs);
578 if (is_array($row) && isset($row[0]) && $row[0] > 0) {
579 $maxtime = $row[0];
580 $remove_rs = PMA_query_as_cu('DELETE FROM ' . PMA_backquote($cfgRelation['history']) . ' WHERE timevalue <= ' . $maxtime);
583 return true;
584 } // end of 'PMA_purgeHistory()' function
585 } // $__PMA_RELATION_LIB__