home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / relation.lib.php < prev    next >
Encoding:
PHP Script  |  2003-11-26  |  25.0 KB  |  649 lines

  1. <?php
  2. /* $Id: relation.lib.php,v 2.2 2003/11/26 22:52:23 rabus Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5. /**
  6.  * Set of functions used with the relation and pdf feature
  7.  */
  8.  
  9.  
  10. /**
  11.  * Executes a query as controluser if possible, otherwise as normal user
  12.  *
  13.  * @param   string   the query to execute
  14.  * @param   boolean  whether to display SQL error messages or not
  15.  *
  16.  * @return  integer  the result id
  17.  *
  18.  * @global  string   the URL of the page to show in case of error
  19.  * @global  string   the name of db to come back to
  20.  * @global  integer  the ressource id of DB connect as controluser
  21.  * @global  array    configuration infos about the relations stuff
  22.  *
  23.  * @access  public
  24.  *
  25.  * @author  Mike Beck <mikebeck@users.sourceforge.net>
  26.  */
  27.  function PMA_query_as_cu($sql, $show_error = TRUE) {
  28.     global $err_url_0, $db, $dbh, $cfgRelation;
  29.  
  30.     if (isset($dbh)) {
  31.         PMA_mysql_select_db($cfgRelation['db'], $dbh);
  32.         $result = @PMA_mysql_query($sql, $dbh);
  33.         if (!$result && $show_error == TRUE) {
  34.             PMA_mysqlDie(mysql_error($dbh), $sql, '', $err_url_0);
  35.         }
  36.         PMA_mysql_select_db($db, $dbh);
  37.     } else {
  38.         PMA_mysql_select_db($cfgRelation['db']);
  39.         $result = @PMA_mysql_query($sql);
  40.         if ($result && $show_error == TRUE) {
  41.             PMA_mysqlDie('', $sql, '', $err_url_0);
  42.         }
  43.         PMA_mysql_select_db($db);
  44.     } // end if... else...
  45.  
  46.     if ($result) {
  47.         return $result;
  48.     } else {
  49.         return FALSE;
  50.     }
  51.  } // end of the "PMA_query_as_cu()" function
  52.  
  53.  
  54. /**
  55.  * Defines the relation parameters for the current user
  56.  * just a copy of the functions used for relations ;-)
  57.  * but added some stuff to check what will work
  58.  *
  59.  * @param   boolean  whether to check validity of settings or not
  60.  *
  61.  * @return  array    the relation parameters for the current user
  62.  *
  63.  * @global  array    the list of settings for servers
  64.  * @global  integer  the id of the current server
  65.  * @global  string   the URL of the page to show in case of error
  66.  * @global  string   the name of the current db
  67.  * @global  string   the name of the current table
  68.  * @global  array    configuration infos about the relations stuff
  69.  *
  70.  * @access  public
  71.  *
  72.  * @author  Mike Beck <mikebeck@users.sourceforge.net>
  73.  */
  74. function PMA_getRelationsParam($verbose = FALSE)
  75. {
  76.     global $cfg, $server, $err_url_0, $db, $table;
  77.     global $cfgRelation;
  78.  
  79.     $cfgRelation                = array();
  80.     $cfgRelation['relwork']     = FALSE;
  81.     $cfgRelation['displaywork'] = FALSE;
  82.     $cfgRelation['bookmarkwork']= FALSE;
  83.     $cfgRelation['pdfwork']     = FALSE;
  84.     $cfgRelation['commwork']    = FALSE;
  85.     $cfgRelation['mimework']    = FALSE;
  86.     $cfgRelation['historywork'] = FALSE;
  87.     $cfgRelation['allworks']    = FALSE;
  88.  
  89.     // No server selected -> no bookmark table
  90.     // we return the array with the FALSEs in it,
  91.     // to avoid some 'Unitialized string offset' errors later
  92.     if ($server == 0
  93.        || empty($cfg['Server'])
  94.        || empty($cfg['Server']['pmadb'])) {
  95.         if ($verbose == TRUE) {
  96.             echo 'PMA Database ... '
  97.                  . '<font color="red"><b>' . $GLOBALS['strNotOK'] . '</b></font>'
  98.                  . '[ <a href="Documentation.html#pmadb">' . $GLOBALS['strDocu'] . '</a> ]<br />' . "\n"
  99.                  . $GLOBALS['strGeneralRelationFeat']
  100.                  . ' <font color="green">' . $GLOBALS['strDisabled'] . '</font>' . "\n";
  101.         }
  102.         return $cfgRelation;
  103.     }
  104.  
  105.     $cfgRelation['user']  = $cfg['Server']['user'];
  106.     $cfgRelation['db']    = $cfg['Server']['pmadb'];
  107.  
  108.     //  Now I just check if all tables that i need are present so I can for
  109.     //  example enable relations but not pdf...
  110.     //  I was thinking of checking if they have all required columns but I
  111.     //  fear it might be too slow
  112.     // PMA_mysql_select_db($cfgRelation['db']);
  113.  
  114.     $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($cfgRelation['db']);
  115.     $tab_rs    = PMA_query_as_cu($tab_query, FALSE);
  116.  
  117.     while ($curr_table = @PMA_mysql_fetch_array($tab_rs)) {
  118.         if ($curr_table[0] == $cfg['Server']['bookmarktable']) {
  119.             $cfgRelation['bookmark']        = $curr_table[0];
  120.         } else if ($curr_table[0] == $cfg['Server']['relation']) {
  121.             $cfgRelation['relation']        = $curr_table[0];
  122.         } else if ($curr_table[0] == $cfg['Server']['table_info']) {
  123.             $cfgRelation['table_info']      = $curr_table[0];
  124.         } else if ($curr_table[0] == $cfg['Server']['table_coords']) {
  125.             $cfgRelation['table_coords']    = $curr_table[0];
  126.         } else if ($curr_table[0] == $cfg['Server']['column_info']) {
  127.             $cfgRelation['column_info'] = $curr_table[0];
  128.         } else if ($curr_table[0] == $cfg['Server']['pdf_pages']) {
  129.             $cfgRelation['pdf_pages']       = $curr_table[0];
  130.         } else if ($curr_table[0] == $cfg['Server']['history']) {
  131.             $cfgRelation['history'] = $curr_table[0];
  132.         }
  133.     } // end while
  134.     if (isset($cfgRelation['relation'])) {
  135.         $cfgRelation['relwork']         = TRUE;
  136.         if (isset($cfgRelation['table_info'])) {
  137.                 $cfgRelation['displaywork'] = TRUE;
  138.         }
  139.     }
  140.     if (isset($cfgRelation['table_coords']) && isset($cfgRelation['pdf_pages'])) {
  141.         $cfgRelation['pdfwork']     = TRUE;
  142.     }
  143.     if (isset($cfgRelation['column_info'])) {
  144.         $cfgRelation['commwork']    = TRUE;
  145.  
  146.         if ($cfg['Server']['verbose_check']) {
  147.             $mime_query  = 'SHOW FIELDS FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']);
  148.             $mime_rs     = PMA_query_as_cu($mime_query, FALSE);
  149.  
  150.             $mime_field_mimetype                = FALSE;
  151.             $mime_field_transformation          = FALSE;
  152.             $mime_field_transformation_options  = FALSE;
  153.             while ($curr_mime_field = @PMA_mysql_fetch_array($mime_rs)) {
  154.                 if ($curr_mime_field[0] == 'mimetype') {
  155.                     $mime_field_mimetype               = TRUE;
  156.                 } else if ($curr_mime_field[0] == 'transformation') {
  157.                     $mime_field_transformation         = TRUE;
  158.                 } else if ($curr_mime_field[0] == 'transformation_options') {
  159.                     $mime_field_transformation_options = TRUE;
  160.                 }
  161.             }
  162.  
  163.             if ($mime_field_mimetype == TRUE
  164.                 && $mime_field_transformation == TRUE
  165.                 && $mime_field_transformation_options == TRUE) {
  166.                 $cfgRelation['mimework'] = TRUE;
  167.             }
  168.         } else {
  169.             $cfgRelation['mimework'] = TRUE;
  170.         }
  171.     }
  172.  
  173.     if (isset($cfgRelation['history'])) {
  174.         $cfgRelation['historywork']     = TRUE;
  175.     }
  176.  
  177.     if (isset($cfgRelation['bookmark'])) {
  178.         $cfgRelation['bookmarkwork']     = TRUE;
  179.     }
  180.  
  181.     if ($cfgRelation['relwork'] == TRUE && $cfgRelation['displaywork'] == TRUE
  182.         && $cfgRelation['pdfwork'] == TRUE && $cfgRelation['commwork'] == TRUE
  183.         && $cfgRelation['mimework'] == TRUE && $cfgRelation['historywork'] == TRUE
  184.         && $cfgRelation['bookmarkwork'] == TRUE) {
  185.         $cfgRelation['allworks'] = TRUE;
  186.     }
  187.     if ($tab_rs) {
  188.         mysql_free_result($tab_rs);
  189.     } else {
  190.         $cfg['Server']['pmadb'] = FALSE;
  191.     }
  192.  
  193.     if ($verbose == TRUE) {
  194.         $shit     = '<font color="red"><b>' . $GLOBALS['strNotOK'] . '</b></font> [ <a href="Documentation.html#%s">' . $GLOBALS['strDocu'] . '</a> ]';
  195.         $hit      = '<font color="green"><b>' . $GLOBALS['strOK'] . '</b></font>';
  196.         $enabled  = '<font color="green">' . $GLOBALS['strEnabled'] . '</font>';
  197.         $disabled = '<font color="red">'   . $GLOBALS['strDisabled'] . '</font>';
  198.  
  199.         echo '<table>' . "\n";
  200.         echo '    <tr><th align="left">$cfg[\'Servers\'][$i][\'pmadb\'] ... </th><td align="right">'
  201.              . (($cfg['Server']['pmadb'] == FALSE) ? sprintf($shit, 'pmadb') : $hit)
  202.              . '</td></tr>' . "\n";
  203.         echo '    <tr><td> </td></tr>' . "\n";
  204.  
  205.         echo '    <tr><th align="left">$cfg[\'Servers\'][$i][\'relation\'] ... </th><td align="right">'
  206.              . ((isset($cfgRelation['relation'])) ? $hit : sprintf($shit, 'relation'))
  207.              . '</td></tr>' . "\n";
  208.         echo '    <tr><td colspan=2 align="center">'. $GLOBALS['strGeneralRelationFeat'] . ': '
  209.              . (($cfgRelation['relwork'] == TRUE) ? $enabled :  $disabled)
  210.              . '</td></tr>' . "\n";
  211.         echo '    <tr><td> </td></tr>' . "\n";
  212.  
  213.         echo '    <tr><th align="left">$cfg[\'Servers\'][$i][\'table_info\']   ... </th><td align="right">'
  214.              . (($cfgRelation['displaywork'] == FALSE) ? sprintf($shit, 'table_info') : $hit)
  215.              . '</td></tr>' . "\n";
  216.         echo '    <tr><td colspan=2 align="center">' . $GLOBALS['strDisplayFeat'] . ': '
  217.              . (($cfgRelation['displaywork'] == TRUE) ? $enabled : $disabled)
  218.              . '</td></tr>' . "\n";
  219.         echo '    <tr><td> </td></tr>' . "\n";
  220.  
  221.         echo '    <tr><th align="left">$cfg[\'Servers\'][$i][\'table_coords\'] ... </th><td align="right">'
  222.              . ((isset($cfgRelation['table_coords'])) ? $hit : sprintf($shit, 'table_coords'))
  223.              . '</td></tr>' . "\n";
  224.         echo '    <tr><th align="left">$cfg[\'Servers\'][$i][\'pdf_pages\'] ... </th><td align="right">'
  225.              . ((isset($cfgRelation['pdf_pages'])) ? $hit : sprintf($shit, 'table_coords'))
  226.              . '</td></tr>' . "\n";
  227.         echo '    <tr><td colspan=2 align="center">' . $GLOBALS['strCreatePdfFeat'] . ': '
  228.              . (($cfgRelation['pdfwork'] == TRUE) ? $enabled : $disabled)
  229.              . '</td></tr>' . "\n";
  230.         echo '    <tr><td> </td></tr>' . "\n";
  231.  
  232.         echo '    <tr><th align="left">$cfg[\'Servers\'][$i][\'column_info\'] ... </th><td align="right">'
  233.              . ((isset($cfgRelation['column_info'])) ? $hit : sprintf($shit, 'col_com'))
  234.              . '</td></tr>' . "\n";
  235.         echo '    <tr><td colspan=2 align="center">' . $GLOBALS['strColComFeat'] . ': '
  236.              . (($cfgRelation['commwork'] == TRUE) ? $enabled : $disabled)
  237.              . '</td></tr>' . "\n";
  238.         echo '    <tr><td colspan=2 align="center">' . $GLOBALS['strBookmarkQuery'] . ': '
  239.              . (($cfgRelation['bookmarkwork'] == TRUE) ? $enabled : $disabled)
  240.              . '</td></tr>' . "\n";
  241.         echo '    <tr><th align="left">MIME ...</th><td align="right">'
  242.              . (($cfgRelation['mimework'] == TRUE) ? $hit : sprintf($shit, 'col_com'))
  243.              . '</td></tr>' . "\n";
  244.  
  245.              if (($cfgRelation['commwork'] == TRUE) && ($cfgRelation['mimework'] != TRUE)) {
  246.                  echo '<tr><td colspan=2 align="left">' . $GLOBALS['strUpdComTab'] . '</td></tr>' . "\n";
  247.              }
  248.  
  249.         echo '    <tr><th align="left">$cfg[\'Servers\'][$i][\'history\'] ... </th><td align="right">'
  250.              . ((isset($cfgRelation['history'])) ? $hit : sprintf($shit, 'history'))
  251.              . '</td></tr>' . "\n";
  252.         echo '    <tr><td colspan=2 align="center">' . $GLOBALS['strQuerySQLHistory'] . ': '
  253.              . (($cfgRelation['historywork'] == TRUE) ? $enabled : $disabled)
  254.              . '</td></tr>' . "\n";
  255.  
  256.         echo '</table>' . "\n";
  257.     } // end if ($verbose == TRUE) {
  258.  
  259.     return $cfgRelation;
  260. } // end of the 'PMA_getRelationsParam()' function
  261.  
  262.  
  263. /**
  264.  * Gets all Relations to foreign tables for a given table or
  265.  * optionally a given column in a table
  266.  *
  267.  * @param   string   the name of the db to check for
  268.  * @param   string   the name of the table to check for
  269.  * @param   string   the name of the column to check for
  270.  * @param   string   the source for foreign key information
  271.  *
  272.  * @return  array    db,table,column
  273.  *
  274.  * @global  array    the list of relations settings
  275.  * @global  string   the URL of the page to show in case of error
  276.  *
  277.  * @access  public
  278.  *
  279.  * @author  Mike Beck <mikebeck@users.sourceforge.net> and Marc Delisle
  280.  */
  281. function PMA_getForeigners($db, $table, $column = '', $source = 'both') {
  282.     global $cfgRelation, $err_url_0;
  283.  
  284.     if ($cfgRelation['relwork'] && ($source == 'both' || $source == 'internal')) {
  285.         $rel_query     = 'SELECT master_field, foreign_db, foreign_table, foreign_field'
  286.                        . ' FROM ' . PMA_backquote($cfgRelation['relation'])
  287.                        . ' WHERE master_db =  \'' . PMA_sqlAddslashes($db) . '\' '
  288.                        . ' AND   master_table = \'' . PMA_sqlAddslashes($table) . '\' ';
  289.         if (!empty($column)) {
  290.             $rel_query .= ' AND master_field = \'' . PMA_sqlAddslashes($column) . '\'';
  291.         }
  292.         $relations     = PMA_query_as_cu($rel_query);
  293.         $i = 0;
  294.         while ($relrow = @PMA_mysql_fetch_array($relations)) {
  295.             $field                            = $relrow['master_field'];
  296.             $foreign[$field]['foreign_db']    = $relrow['foreign_db'];
  297.             $foreign[$field]['foreign_table'] = $relrow['foreign_table'];
  298.             $foreign[$field]['foreign_field'] = $relrow['foreign_field'];
  299.             $i++;
  300.         } // end while
  301.     }
  302.  
  303.     if (($source == 'both' || $source == 'innodb') && !empty($table)) {
  304.         $show_create_table_query = 'SHOW CREATE TABLE '
  305.             . PMA_backquote($db) . '.' . PMA_backquote($table);
  306.         $show_create_table_res = PMA_mysql_query($show_create_table_query);
  307.         list(,$show_create_table) = PMA_mysql_fetch_row($show_create_table_res);
  308.  
  309.         $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
  310.  
  311.         foreach($analyzed_sql[0]['foreign_keys'] AS $one_key) {
  312.  
  313.         // the analyzer may return more than one column name in the
  314.         // index list or the ref_index_list
  315.             foreach($one_key['index_list'] AS $i => $field) {
  316.  
  317.         // If a foreign key is defined in the 'internal' source (pmadb)
  318.         // and in 'innodb', we won't get it twice if $source='both'
  319.         // because we use $field as key
  320.  
  321.                 $foreign[$field]['constraint'] = $one_key['constraint'];
  322.  
  323.                 if (isset($one_key['ref_db_name'])) {
  324.                     $foreign[$field]['foreign_db']    = $one_key['ref_db_name'];
  325.                 } else {
  326.                     $foreign[$field]['foreign_db']    = $db;
  327.                 }
  328.                 $foreign[$field]['foreign_table'] = $one_key['ref_table_name'];
  329.                 $foreign[$field]['foreign_field'] = $one_key['ref_index_list'][$i];
  330.                 if (isset($one_key['on_delete'])) {
  331.                     $foreign[$field]['on_delete'] = $one_key['on_delete'];
  332.                 }
  333.                 if (isset($one_key['on_update'])) {
  334.                     $foreign[$field]['on_update'] = $one_key['on_update'];
  335.                 }
  336.             }
  337.         }
  338.     }
  339.  
  340.     if (isset($foreign) && is_array($foreign)) {
  341.        return $foreign;
  342.     } else {
  343.        return FALSE;
  344.     }
  345. } // end of the 'PMA_getForeigners()' function
  346.  
  347.  
  348. /**
  349.  * Gets the display field of a table
  350.  *
  351.  * @param   string   the name of the db to check for
  352.  * @param   string   the name of the table to check for
  353.  *
  354.  * @return  string   field name
  355.  *
  356.  * @global  array    the list of relations settings
  357.  *
  358.  * @access  public
  359.  *
  360.  * @author  Mike Beck <mikebeck@users.sourceforge.net>
  361.  */
  362. function PMA_getDisplayField($db, $table) {
  363.     global $cfgRelation;
  364.  
  365.     $disp_query = 'SELECT display_field FROM ' . PMA_backquote($cfgRelation['table_info'])
  366.                 . ' WHERE db_name  = \'' . PMA_sqlAddslashes($db) . '\''
  367.                 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
  368.  
  369.     $disp_res   = PMA_query_as_cu($disp_query);
  370.     $row        = ($disp_res ? PMA_mysql_fetch_array($disp_res) : '');
  371.     if (isset($row['display_field'])) {
  372.         return $row['display_field'];
  373.     } else {
  374.         return FALSE;
  375.     }
  376. } // end of the 'PMA_getDisplayField()' function
  377.  
  378.  
  379. /**
  380.  * Gets the comments for all rows of a table
  381.  *
  382.  * @param   string   the name of the db to check for
  383.  * @param   string   the name of the table to check for
  384.  *
  385.  * @return  array    [field_name] = comment
  386.  *
  387.  * @global  array    the list of relations settings
  388.  *
  389.  * @access  public
  390.  *
  391.  * @author  Mike Beck <mikebeck@users.sourceforge.net>
  392.  */
  393. function PMA_getComments($db, $table = '') {
  394.     global $cfgRelation;
  395.  
  396.     if ($table != '') {
  397.         $com_qry  = 'SELECT column_name, ' . PMA_backquote('comment') . ' FROM ' . PMA_backquote($cfgRelation['column_info'])
  398.                   . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
  399.                   . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
  400.         $com_rs   = PMA_query_as_cu($com_qry);
  401.     } else {
  402.         $com_qry  = 'SELECT comment FROM ' . PMA_backquote($cfgRelation['column_info'])
  403.                   . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
  404.                   . ' AND table_name = \'\''
  405.                   . ' AND column_name = \'(db_comment)\'';
  406.         $com_rs   = PMA_query_as_cu($com_qry);
  407.     }
  408.  
  409.     $i = 0;
  410.     while ($row = @PMA_mysql_fetch_array($com_rs)) {
  411.         $i++;
  412.         $col           = ($table != '' ? $row['column_name'] : $i);
  413.  
  414.         if (strlen($row['comment']) > 0) {
  415.             $comment[$col] = $row['comment'];
  416.         }
  417.  
  418.     } // end while
  419.  
  420.     if (isset($comment) && is_array($comment)) {
  421.         return $comment;
  422.      } else {
  423.         return FALSE;
  424.      }
  425.  } // end of the 'PMA_getComments()' function
  426.  
  427. /**
  428. * Adds/removes slashes if required
  429. *
  430. * @param   string  the string to slash
  431. *
  432. * @return  string  the slashed string
  433. *
  434. * @access  public
  435. */
  436. function PMA_handleSlashes($val) {
  437.   return (get_magic_quotes_gpc() ? str_replace('\\"', '"', $val) : PMA_sqlAddslashes($val));
  438. } // end of the "PMA_handleSlashes()" function
  439.  
  440. /**
  441. * Set a single comment to a certain value.
  442. *
  443. * @param   string   the name of the db
  444. * @param   string   the name of the table
  445. * @param   string   the name of the column
  446. * @param   string   the value of the column
  447. * @param   string   (optional) if a column is renamed, this is the name of the former key which will get deleted
  448. *
  449. * @return  boolean  true, if comment-query was made.
  450. *
  451. * @global  array    the list of relations settings
  452. *
  453. * @access  public
  454. */
  455. function PMA_setComment($db, $table, $key, $value, $removekey = '') {
  456.     global $cfgRelation;
  457.  
  458.     if ($removekey != '' AND $removekey != $key) {
  459.         $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['column_info'])
  460.                     . ' WHERE db_name  = \'' . PMA_sqlAddslashes($db) . '\''
  461.                     . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
  462.                     . ' AND column_name = \'' . PMA_sqlAddslashes($removekey) . '\'';
  463.         $rmv_rs    = PMA_query_as_cu($remove_query);
  464.         unset($rmv_query);
  465.     }
  466.  
  467.     $test_qry  = 'SELECT ' . PMA_backquote('comment') . ', mimetype, transformation, transformation_options FROM ' . PMA_backquote($cfgRelation['column_info'])
  468.                 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
  469.                 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
  470.                 . ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\'';
  471.     $test_rs   = PMA_query_as_cu($test_qry);
  472.  
  473.     if ($test_rs && mysql_num_rows($test_rs) > 0) {
  474.         $row = @PMA_mysql_fetch_array($test_rs);
  475.  
  476.         if (strlen($value) > 0 || strlen($row['mimetype']) > 0 || strlen($row['transformation']) > 0 || strlen($row['transformation_options']) > 0) {
  477.             $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['column_info'])
  478.                    . ' SET ' . PMA_backquote('comment') . ' = \'' . PMA_sqlAddslashes($value) . '\''
  479.                    . ' WHERE db_name  = \'' . PMA_sqlAddslashes($db) . '\''
  480.                    . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
  481.                    . ' AND column_name = \'' . PMA_sqlAddSlashes($key) . '\'';
  482.         } else {
  483.             $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['column_info'])
  484.                    . ' WHERE db_name  = \'' . PMA_sqlAddslashes($db) . '\''
  485.                    . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
  486.                    . ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\'';
  487.         }
  488.     } else if (strlen($value) > 0) {
  489.         $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['column_info'])
  490.                    . ' (db_name, table_name, column_name, ' . PMA_backquote('comment') . ') '
  491.                    . ' VALUES('
  492.                    . '\'' . PMA_sqlAddslashes($db) . '\','
  493.                    . '\'' . PMA_sqlAddslashes($table) . '\','
  494.                    . '\'' . PMA_sqlAddslashes($key) . '\','
  495.                    . '\'' . PMA_sqlAddslashes($value) . '\')';
  496.     }
  497.  
  498.     if (isset($upd_query)){
  499.         $upd_rs    = PMA_query_as_cu($upd_query);
  500.         unset($upd_query);
  501.         return true;
  502.     } else {
  503.         return false;
  504.     }
  505. } // end of 'PMA_setComment()' function
  506.  
  507. /**
  508. * Set a SQL history entry
  509. *
  510. * @param   string   the name of the db
  511. * @param   string   the name of the table
  512. * @param   string   the username
  513. * @param   string   the sql query
  514. *
  515. * @return  boolean  true
  516. *
  517. * @access  public
  518. */
  519. function PMA_setHistory($db, $table, $username, $sqlquery) {
  520.     global $cfgRelation;
  521.  
  522.     $hist_rs    = PMA_query_as_cu('INSERT INTO ' . PMA_backquote($cfgRelation['history']) . ' ('
  523.                 . PMA_backquote('username') . ','
  524.                 . PMA_backquote('db') . ','
  525.                 . PMA_backquote('table') . ','
  526.                 . PMA_backquote('timevalue') . ','
  527.                 . PMA_backquote('sqlquery')
  528.                 . ') VALUES ('
  529.                 . '\'' . PMA_sqlAddslashes($username) . '\','
  530.                 . '\'' . PMA_sqlAddslashes($db) . '\','
  531.                 . '\'' . PMA_sqlAddslashes($table) . '\','
  532.                 . 'NOW(),'
  533.                 . '\'' . PMA_sqlAddslashes($sqlquery) . '\')');
  534.     return true;
  535. } // end of 'PMA_setHistory()' function
  536.  
  537. /**
  538. * Gets a SQL history entry
  539. *
  540. * @param   string   the username
  541. *
  542. * @return  array    list of history items
  543. *
  544. * @access  public
  545. */
  546. function PMA_getHistory($username) {
  547.     global $cfgRelation;
  548.  
  549.     $hist_rs    = PMA_query_as_cu('SELECT '
  550.                     . PMA_backquote('db') . ','
  551.                     . PMA_backquote('table') . ','
  552.                     . PMA_backquote('sqlquery')
  553.                     . ' FROM ' . PMA_backquote($cfgRelation['history']) . ' WHERE username = \'' . PMA_sqlAddslashes($username) . '\' ORDER BY id DESC');
  554.  
  555.     $history = array();
  556.  
  557.     while ($row = @PMA_mysql_fetch_array($hist_rs)) {
  558.         $history[] = $row;
  559.     }
  560.  
  561.     return $history;
  562.  
  563. } // end of 'PMA_getHistory()' function
  564.  
  565. /**
  566. * Set a SQL history entry
  567. *
  568. * @param   string   the name of the db
  569. * @param   string   the name of the table
  570. * @param   string   the username
  571. * @param   string   the sql query
  572. *
  573. * @return  boolean  true
  574. *
  575. * @access  public
  576. */
  577. function PMA_purgeHistory($username) {
  578.     global $cfgRelation, $cfg;
  579.  
  580.     $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');
  581.     $i = 0;
  582.     $row = @PMA_mysql_fetch_array($purge_rs);
  583.  
  584.     if (is_array($row) && isset($row[0]) && $row[0] > 0) {
  585.         $maxtime = $row[0];
  586.         // quotes added around $maxtime to prevent a difficult to
  587.         // reproduce problem
  588.         $remove_rs = PMA_query_as_cu('DELETE FROM ' . PMA_backquote($cfgRelation['history']) . ' WHERE timevalue <= "' . $maxtime . '"');
  589.     }
  590.  
  591.     return true;
  592. } // end of 'PMA_purgeHistory()' function
  593.  
  594. /**
  595. * Outputs dropdown with values of foreign fields
  596. *
  597. * @param   string   the query of the foreign keys
  598. * @param   string   the foreign field
  599. * @param   string   the foreign field to display
  600. * @param   string   the current data of the dropdown
  601. *
  602. * @return  string   the <option value=""><option>s
  603. *
  604. * @access  public
  605. */
  606. function PMA_foreignDropdown($disp, $foreign_field, $foreign_display, $data, $max = 100) {
  607.     global $cfg;
  608.  
  609.     $ret = '<option value=""></option>' . "\n";
  610.  
  611.     $reloptions = array('content-id' => array(), 'id-content' => array());
  612.     while ($relrow = @PMA_mysql_fetch_array($disp)) {
  613.         $key   = $relrow[$foreign_field];
  614.         if (strlen($relrow[$foreign_display]) <= $cfg['LimitChars']) {
  615.             $value  = (($foreign_display != FALSE) ? htmlspecialchars($relrow[$foreign_display]) : '');
  616.             $vtitle = '';
  617.         } else {
  618.             $vtitle = htmlspecialchars($relrow[$foreign_display]);
  619.             $value  = (($foreign_display != FALSE) ? htmlspecialchars(substr($vtitle, 0, $cfg['LimitChars']) . '...') : '');
  620.         }
  621.  
  622.         $reloption = '<option value="' . htmlspecialchars($key) . '"';
  623.         if ($vtitle != '') {
  624.             $reloption .= ' title="' . $vtitle . '"';
  625.         }
  626.  
  627.         if ($key == $data) {
  628.            $reloption .= ' selected="selected"';
  629.         } // end if
  630.  
  631.         $reloptions['id-content'][] = $reloption . '>' . $value . ' - ' . htmlspecialchars($key) .  '</option>' . "\n";
  632.         $reloptions['content-id'][] = $reloption . '>' . htmlspecialchars($key) .  ' - ' . $value . '</option>' . "\n";
  633.     } // end while
  634.  
  635.     if ($max == -1 || count($reloptions['content-id']) < $max) {
  636.         $ret .= implode('', $reloptions['content-id']);
  637.         if (count($reloptions['content-id']) > 0) {
  638.             $ret .= '<option value=""></option>' . "\n";
  639.             $ret .= '<option value=""></option>' . "\n";
  640.         }
  641.     }
  642.  
  643.     $ret .= implode('', $reloptions['id-content']);
  644.  
  645.     return $ret;
  646. } // end of 'PMA_foreignDropdown()' function
  647.  
  648. ?>
  649.