home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / xampp-win32-1.6.5-installer.exe / phpMyAdmin / libraries / display_tbl.lib.php < prev    next >
Encoding:
PHP Script  |  2007-12-20  |  102.7 KB  |  2,133 lines

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4.  *
  5.  * @version $Id: display_tbl.lib.php 10928 2007-11-15 13:33:35Z lem9 $
  6.  */
  7.  
  8. /**
  9.  *
  10.  */
  11. require_once './libraries/Table.class.php';
  12.  
  13. /**
  14.  * Set of functions used to display the records returned by a SQL query
  15.  */
  16.  
  17. /**
  18.  * Defines the display mode to use for the results of a SQL query
  19.  *
  20.  * It uses a synthetic string that contains all the required informations.
  21.  * In this string:
  22.  *   - the first two characters stand for the action to do while
  23.  *     clicking on the "edit" link (e.g. 'ur' for update a row, 'nn' for no
  24.  *     edit link...);
  25.  *   - the next two characters stand for the action to do while
  26.  *     clicking on the "delete" link (e.g. 'kp' for kill a process, 'nn' for
  27.  *     no delete link...);
  28.  *   - the next characters are boolean values (1/0) and respectively stand
  29.  *     for sorting links, navigation bar, "insert a new row" link, the
  30.  *     bookmark feature, the expand/collapse text/blob fields button and
  31.  *     the "display printable view" option.
  32.  *     Of course '0'/'1' means the feature won't/will be enabled.
  33.  *
  34.  * @param   string   the synthetic value for display_mode (see ∩┐╜1 a few
  35.  *                   lines above for explanations)
  36.  * @param   integer  the total number of rows returned by the SQL query
  37.  *                   without any programmatically appended "LIMIT" clause
  38.  *                   (just a copy of $unlim_num_rows if it exists, else
  39.  *                   computed inside this function)
  40.  *
  41.  * @return  array    an array with explicit indexes for all the display
  42.  *                   elements
  43.  *
  44.  * @global  string   the database name
  45.  * @global  string   the table name
  46.  * @global  integer  the total number of rows returned by the SQL query
  47.  *                   without any programmatically appended "LIMIT" clause
  48.  * @global  array    the properties of the fields returned by the query
  49.  * @global  string   the URL to return to in case of error in a SQL
  50.  *                   statement
  51.  *
  52.  * @access  private
  53.  *
  54.  * @see     PMA_displayTable()
  55.  */
  56. function PMA_setDisplayMode(&$the_disp_mode, &$the_total)
  57. {
  58.     global $db, $table;
  59.     global $unlim_num_rows, $fields_meta;
  60.     global $err_url;
  61.  
  62.     // 1. Initializes the $do_display array
  63.     $do_display              = array();
  64.     $do_display['edit_lnk']  = $the_disp_mode[0] . $the_disp_mode[1];
  65.     $do_display['del_lnk']   = $the_disp_mode[2] . $the_disp_mode[3];
  66.     $do_display['sort_lnk']  = (string) $the_disp_mode[4];
  67.     $do_display['nav_bar']   = (string) $the_disp_mode[5];
  68.     $do_display['ins_row']   = (string) $the_disp_mode[6];
  69.     $do_display['bkm_form']  = (string) $the_disp_mode[7];
  70.     $do_display['text_btn']  = (string) $the_disp_mode[8];
  71.     $do_display['pview_lnk'] = (string) $the_disp_mode[9];
  72.  
  73.     // 2. Display mode is not "false for all elements" -> updates the
  74.     // display mode
  75.     if ($the_disp_mode != 'nnnn000000') {
  76.         // 2.0 Print view -> set all elements to false!
  77.         if (isset($GLOBALS['printview']) && $GLOBALS['printview'] == '1') {
  78.             $do_display['edit_lnk']  = 'nn'; // no edit link
  79.             $do_display['del_lnk']   = 'nn'; // no delete link
  80.             $do_display['sort_lnk']  = (string) '0';
  81.             $do_display['nav_bar']   = (string) '0';
  82.             $do_display['ins_row']   = (string) '0';
  83.             $do_display['bkm_form']  = (string) '0';
  84.             $do_display['text_btn']  = (string) '0';
  85.             $do_display['pview_lnk'] = (string) '0';
  86.         }
  87.         // 2.1 Statement is a "SELECT COUNT", a
  88.         //     "CHECK/ANALYZE/REPAIR/OPTIMIZE", an "EXPLAIN" one or
  89.         //     contains a "PROC ANALYSE" part
  90.         elseif ($GLOBALS['is_count'] || $GLOBALS['is_analyse'] || $GLOBALS['is_maint'] || $GLOBALS['is_explain']) {
  91.             $do_display['edit_lnk']  = 'nn'; // no edit link
  92.             $do_display['del_lnk']   = 'nn'; // no delete link
  93.             $do_display['sort_lnk']  = (string) '0';
  94.             $do_display['nav_bar']   = (string) '0';
  95.             $do_display['ins_row']   = (string) '0';
  96.             $do_display['bkm_form']  = (string) '1';
  97.             if ($GLOBALS['is_maint']) {
  98.                 $do_display['text_btn']  = (string) '1';
  99.             } else {
  100.                 $do_display['text_btn']  = (string) '0';
  101.             }
  102.             $do_display['pview_lnk'] = (string) '1';
  103.         }
  104.         // 2.2 Statement is a "SHOW..."
  105.         elseif ($GLOBALS['is_show']) {
  106.             /**
  107.              * 2.2.1
  108.              * @todo defines edit/delete links depending on show statement
  109.              */
  110.             $tmp = preg_match('@^SHOW[[:space:]]+(VARIABLES|(FULL[[:space:]]+)?PROCESSLIST|STATUS|TABLE|GRANTS|CREATE|LOGS|DATABASES|FIELDS)@i', $GLOBALS['sql_query'], $which);
  111.             if (isset($which[1]) && strpos(' ' . strtoupper($which[1]), 'PROCESSLIST') > 0) {
  112.                 $do_display['edit_lnk'] = 'nn'; // no edit link
  113.                 $do_display['del_lnk']  = 'kp'; // "kill process" type edit link
  114.             } else {
  115.                 // Default case -> no links
  116.                 $do_display['edit_lnk'] = 'nn'; // no edit link
  117.                 $do_display['del_lnk']  = 'nn'; // no delete link
  118.             }
  119.             // 2.2.2 Other settings
  120.             $do_display['sort_lnk']  = (string) '0';
  121.             $do_display['nav_bar']   = (string) '0';
  122.             $do_display['ins_row']   = (string) '0';
  123.             $do_display['bkm_form']  = (string) '1';
  124.             $do_display['text_btn']  = (string) '1';
  125.             $do_display['pview_lnk'] = (string) '1';
  126.         }
  127.         // 2.3 Other statements (ie "SELECT" ones) -> updates
  128.         //     $do_display['edit_lnk'], $do_display['del_lnk'] and
  129.         //     $do_display['text_btn'] (keeps other default values)
  130.         else {
  131.             $prev_table = $fields_meta[0]->table;
  132.             $do_display['text_btn']  = (string) '1';
  133.             for ($i = 0; $i < $GLOBALS['fields_cnt']; $i++) {
  134.                 $is_link = ($do_display['edit_lnk'] != 'nn'
  135.                             || $do_display['del_lnk'] != 'nn'
  136.                             || $do_display['sort_lnk'] != '0'
  137.                             || $do_display['ins_row'] != '0');
  138.                 // 2.3.2 Displays edit/delete/sort/insert links?
  139.                 if ($is_link
  140.                     && ($fields_meta[$i]->table == '' || $fields_meta[$i]->table != $prev_table)) {
  141.                     $do_display['edit_lnk'] = 'nn'; // don't display links
  142.                     $do_display['del_lnk']  = 'nn';
  143.                     /**
  144.                      * @todo May be problematic with same fields names in two joined table.
  145.                      */
  146.                     // $do_display['sort_lnk'] = (string) '0';
  147.                     $do_display['ins_row']  = (string) '0';
  148.                     if ($do_display['text_btn'] == '1') {
  149.                         break;
  150.                     }
  151.                 } // end if (2.3.2)
  152.                 // 2.3.3 Always display print view link
  153.                 $do_display['pview_lnk']    = (string) '1';
  154.                 $prev_table = $fields_meta[$i]->table;
  155.             } // end for
  156.         } // end if..elseif...else (2.1 -> 2.3)
  157.     } // end if (2)
  158.  
  159.     // 3. Gets the total number of rows if it is unknown
  160.     if (isset($unlim_num_rows) && $unlim_num_rows != '') {
  161.         $the_total = $unlim_num_rows;
  162.     } elseif (($do_display['nav_bar'] == '1' || $do_display['sort_lnk'] == '1')
  163.              && (strlen($db) && !empty($table))) {
  164.         $the_total   = PMA_Table::countRecords($db, $table, true);
  165.     }
  166.  
  167.     // 4. If navigation bar or sorting fields names URLs should be
  168.     //    displayed but there is only one row, change these settings to
  169.     //    false
  170.     if ($do_display['nav_bar'] == '1' || $do_display['sort_lnk'] == '1') {
  171.  
  172.         // - Do not display sort links if less than 2 rows.
  173.         // - For a VIEW we (probably) did not count the number of rows
  174.         //   so don't test this number here, it would remove the possibility
  175.         //   of sorting VIEW results.
  176.         if (isset($unlim_num_rows) && $unlim_num_rows < 2 && ! PMA_Table::isView($db, $table)) {
  177.             // garvin: force display of navbar for vertical/horizontal display-choice.
  178.             // $do_display['nav_bar']  = (string) '0';
  179.             $do_display['sort_lnk'] = (string) '0';
  180.         }
  181.     } // end if (3)
  182.  
  183.     // 5. Updates the synthetic var
  184.     $the_disp_mode = join('', $do_display);
  185.  
  186.     return $do_display;
  187. } // end of the 'PMA_setDisplayMode()' function
  188.  
  189.  
  190. /**
  191.  * Displays a navigation bar to browse among the results of a SQL query
  192.  *
  193.  * @uses    $_SESSION['userconf']['disp_direction']
  194.  * @uses    $_SESSION['userconf']['repeat_cells']
  195.  * @uses    $_SESSION['userconf']['max_rows']
  196.  * @uses    $_SESSION['userconf']['pos']
  197.  * @param   integer  the offset for the "next" page
  198.  * @param   integer  the offset for the "previous" page
  199.  * @param   string   the URL-encoded query
  200.  *
  201.  * @global  string   $db             the database name
  202.  * @global  string   $table          the table name
  203.  * @global  string   $goto           the URL to go back in case of errors
  204.  * @global  integer  $num_rows       the total number of rows returned by the
  205.  *                                   SQL query
  206.  * @global  integer  $unlim_num_rows the total number of rows returned by the
  207.  *                                   SQL any programmatically appended "LIMIT" clause
  208.  * @global  boolean  $is_innodb      whether its InnoDB or not
  209.  * @global  array    $showtable      table definitions
  210.  *
  211.  * @access  private
  212.  *
  213.  * @see     PMA_displayTable()
  214.  */
  215. function PMA_displayTableNavigation($pos_next, $pos_prev, $sql_query)
  216. {
  217.     global $db, $table, $goto;
  218.     global $num_rows, $unlim_num_rows;
  219.     global $is_innodb;
  220.     global $showtable;
  221.  
  222.     // here, using htmlentities() would cause problems if the query
  223.     // contains accented characters
  224.     $html_sql_query = htmlspecialchars($sql_query);
  225.  
  226.     /**
  227.      * @todo move this to a central place
  228.      * @todo for other future table types
  229.      */
  230.     $is_innodb = (isset($showtable['Type']) && $showtable['Type'] == 'InnoDB');
  231.  
  232.     ?>
  233.  
  234. <!-- Navigation bar -->
  235. <table border="0" cellpadding="2" cellspacing="0">
  236. <tr>
  237.     <?php
  238.     // Move to the beginning or to the previous page
  239.     if ($_SESSION['userconf']['pos'] && $_SESSION['userconf']['max_rows'] != 'all') {
  240.         // loic1: patch #474210 from Gosha Sakovich - part 1
  241.         if ($GLOBALS['cfg']['NavigationBarIconic']) {
  242.             $caption1 = '<<';
  243.             $caption2 = ' < ';
  244.             $title1   = ' title="' . $GLOBALS['strPos1'] . '"';
  245.             $title2   = ' title="' . $GLOBALS['strPrevious'] . '"';
  246.         } else {
  247.             $caption1 = $GLOBALS['strPos1'] . ' <<';
  248.             $caption2 = $GLOBALS['strPrevious'] . ' <';
  249.             $title1   = '';
  250.             $title2   = '';
  251.         } // end if... else...
  252.         ?>
  253. <td>
  254.     <form action="sql.php" method="post">
  255.         <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
  256.         <input type="hidden" name="sql_query" value="<?php echo $html_sql_query; ?>" />
  257.         <input type="hidden" name="pos" value="0" />
  258.         <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
  259.         <input type="submit" name="navig" value="<?php echo $caption1; ?>"<?php echo $title1; ?> />
  260.     </form>
  261. </td>
  262. <td>
  263.     <form action="sql.php" method="post">
  264.         <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
  265.         <input type="hidden" name="sql_query" value="<?php echo $html_sql_query; ?>" />
  266.         <input type="hidden" name="pos" value="<?php echo $pos_prev; ?>" />
  267.         <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
  268.         <input type="submit" name="navig" value="<?php echo $caption2; ?>"<?php echo $title2; ?> />
  269.     </form>
  270. </td>
  271.         <?php
  272.     } // end move back
  273.     ?>
  274. <td>
  275.        
  276. </td>
  277. <td align="center">
  278. <?php // if displaying a VIEW, $unlim_num_rows could be zero because
  279.       // of $cfg['MaxExactCountViews']; in this case, avoid passing
  280.       // the 5th parameter to checkFormElementInRange() 
  281.       // (this means we can't validate the upper limit ?>
  282.     <form action="sql.php" method="post"
  283. onsubmit="return (checkFormElementInRange(this, 'session_max_rows', '<?php echo str_replace('\'', '\\\'', $GLOBALS['strInvalidRowNumber']); ?>', 1) && checkFormElementInRange(this, 'pos', '<?php echo str_replace('\'', '\\\'', $GLOBALS['strInvalidRowNumber']); ?>', 0<?php echo $unlim_num_rows > 0 ? ',' . $unlim_num_rows - 1 : ''; ?>))">
  284.         <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
  285.         <input type="hidden" name="sql_query" value="<?php echo $html_sql_query; ?>" />
  286.         <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
  287.         <input type="submit" name="navig" value="<?php echo $GLOBALS['strShow']; ?> :" />
  288.         <input type="text" name="session_max_rows" size="3" value="<?php echo (($_SESSION['userconf']['max_rows'] != 'all') ? $_SESSION['userconf']['max_rows'] : $GLOBALS['cfg']['MaxRows']); ?>" class="textfield" onfocus="this.select()" />
  289.         <?php echo $GLOBALS['strRowsFrom'] . "\n"; ?>
  290.         <input type="text" name="pos" size="6" value="<?php echo (($pos_next >= $unlim_num_rows) ? 0 : $pos_next); ?>" class="textfield" onfocus="this.select()" />
  291.         <br />
  292.     <?php
  293.     // Display mode (horizontal/vertical and repeat headers)
  294.     $param1 = '            <select name="disp_direction">' . "\n"
  295.             . '                <option value="horizontal"' . (($_SESSION['userconf']['disp_direction'] == 'horizontal') ? ' selected="selected"': '') . '>' . $GLOBALS['strRowsModeHorizontal'] . '</option>' . "\n"
  296.             . '                <option value="horizontalflipped"' . (($_SESSION['userconf']['disp_direction'] == 'horizontalflipped') ? ' selected="selected"': '') . '>' . $GLOBALS['strRowsModeFlippedHorizontal'] . '</option>' . "\n"
  297.             . '                <option value="vertical"' . (($_SESSION['userconf']['disp_direction'] == 'vertical') ? ' selected="selected"': '') . '>' . $GLOBALS['strRowsModeVertical'] . '</option>' . "\n"
  298.             . '            </select>' . "\n"
  299.             . '           ';
  300.     $param2 = '            <input type="text" size="3" name="repeat_cells" value="' . $_SESSION['userconf']['repeat_cells'] . '" class="textfield" />' . "\n"
  301.             . '           ';
  302.     echo '    ' . sprintf($GLOBALS['strRowsModeOptions'], "\n" . $param1, "\n" . $param2) . "\n";
  303.     ?>
  304.     </form>
  305. </td>
  306. <td>
  307.        
  308. </td>
  309.     <?php
  310.     // Move to the next page or to the last one
  311.     if (($_SESSION['userconf']['pos'] + $_SESSION['userconf']['max_rows'] < $unlim_num_rows) && $num_rows >= $_SESSION['userconf']['max_rows']
  312.         && $_SESSION['userconf']['max_rows'] != 'all') {
  313.         // loic1: patch #474210 from Gosha Sakovich - part 2
  314.         if ($GLOBALS['cfg']['NavigationBarIconic']) {
  315.             $caption3 = ' > ';
  316.             $caption4 = '>>';
  317.             $title3   = ' title="' . $GLOBALS['strNext'] . '"';
  318.             $title4   = ' title="' . $GLOBALS['strEnd'] . '"';
  319.         } else {
  320.             $caption3 = '> ' . $GLOBALS['strNext'];
  321.             $caption4 = '>> ' . $GLOBALS['strEnd'];
  322.             $title3   = '';
  323.             $title4   = '';
  324.         } // end if... else...
  325.         echo "\n";
  326.         ?>
  327. <td>
  328.     <form action="sql.php" method="post">
  329.         <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
  330.         <input type="hidden" name="sql_query" value="<?php echo $html_sql_query; ?>" />
  331.         <input type="hidden" name="pos" value="<?php echo $pos_next; ?>" />
  332.         <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
  333.         <input type="submit" name="navig" value="<?php echo $caption3; ?>"<?php echo $title3; ?> />
  334.     </form>
  335. </td>
  336. <td>
  337.     <form action="sql.php" method="post"
  338.         onsubmit="return <?php echo (($_SESSION['userconf']['pos'] + $_SESSION['userconf']['max_rows'] < $unlim_num_rows && $num_rows >= $_SESSION['userconf']['max_rows']) ? 'true' : 'false'); ?>">
  339.         <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
  340.         <input type="hidden" name="sql_query" value="<?php echo $html_sql_query; ?>" />
  341.         <input type="hidden" name="pos" value="<?php echo @((ceil($unlim_num_rows / $_SESSION['userconf']['max_rows'])- 1) * $_SESSION['userconf']['max_rows']); ?>" />
  342.         <?php
  343.         if ($is_innodb && $unlim_num_rows > $GLOBALS['cfg']['MaxExactCount']) {
  344.             echo '<input type="hidden" name="find_real_end" value="1" />' . "\n";
  345.             // no backquote around this message
  346.             $onclick = ' onclick="return confirmAction(\'' . PMA_jsFormat($GLOBALS['strLongOperation'], false) . '\')"';
  347.         }
  348.         ?>
  349.         <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
  350.         <input type="submit" name="navig" value="<?php echo $caption4; ?>"<?php echo $title4; ?> <?php echo (empty($onclick) ? '' : $onclick); ?>/>
  351.     </form>
  352. </td>
  353.         <?php
  354.     } // end move toward
  355.  
  356.  
  357.     //page redirection
  358.     // (unless we are showing all records)
  359.     if ('all' != $_SESSION['userconf']['max_rows']) { //if1
  360.         $pageNow = @floor($_SESSION['userconf']['pos'] / $_SESSION['userconf']['max_rows']) + 1;
  361.         $nbTotalPage = @ceil($unlim_num_rows / $_SESSION['userconf']['max_rows']);
  362.  
  363.         if ($nbTotalPage > 1){ //if2
  364.        ?>
  365.    <td>
  366.           
  367.    </td>
  368.    <td>
  369.         <?php //<form> for keep the form alignment of button < and << ?>
  370.         <form action="none">
  371.         <?php echo PMA_pageselector(
  372.                      'sql.php?sql_query='   . urlencode($sql_query) .
  373.                         '&goto='        . $goto .
  374.                         '&'             . PMA_generate_common_url($db, $table) .
  375.                         '&',
  376.                      $_SESSION['userconf']['max_rows'],
  377.                      $pageNow,
  378.                      $nbTotalPage,
  379.                      200,
  380.                      5,
  381.                      5,
  382.                      20,
  383.                      10,
  384.                      $GLOBALS['strPageNumber']
  385.               );
  386.         ?>
  387.         </form>
  388.     </td>
  389.         <?php
  390.         } //_if2
  391.     } //_if1
  392.  
  393.  
  394.     // Display the "Show all" button if allowed
  395.     if ($GLOBALS['cfg']['ShowAll'] && ($num_rows < $unlim_num_rows)) {
  396.         echo "\n";
  397.         ?>
  398. <td>
  399.        
  400. </td>
  401. <td>
  402.     <form action="sql.php" method="post">
  403.         <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
  404.         <input type="hidden" name="sql_query" value="<?php echo $html_sql_query; ?>" />
  405.         <input type="hidden" name="pos" value="0" />
  406.         <input type="hidden" name="session_max_rows" value="all" />
  407.         <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
  408.         <input type="submit" name="navig" value="<?php echo $GLOBALS['strShowAll']; ?>" />
  409.     </form>
  410. </td>
  411.         <?php
  412.     } // end show all
  413.     echo "\n";
  414.     ?>
  415. </tr>
  416. </table>
  417.  
  418.     <?php
  419. } // end of the 'PMA_displayTableNavigation()' function
  420.  
  421.  
  422. /**
  423.  * Displays the headers of the results table
  424.  *
  425.  * @uses    $_SESSION['userconf']['disp_direction']
  426.  * @uses    $_SESSION['userconf']['repeat_cells']
  427.  * @uses    $_SESSION['userconf']['max_rows']
  428.  * @uses    $_SESSION['userconf']['dontlimitchars']
  429.  * @param   array    which elements to display
  430.  * @param   array    the list of fields properties
  431.  * @param   integer  the total number of fields returned by the SQL query
  432.  * @param   array    the analyzed query
  433.  *
  434.  * @return  boolean  always true
  435.  *
  436.  * @global  string   $db               the database name
  437.  * @global  string   $table            the table name
  438.  * @global  string   $goto             the URL to go back in case of errors
  439.  * @global  string   $sql_query        the SQL query
  440.  * @global  integer  $num_rows         the total number of rows returned by the
  441.  *                                     SQL query
  442.  * @global  array    $vertical_display informations used with vertical display
  443.  *                                     mode
  444.  *
  445.  * @access  private
  446.  *
  447.  * @see     PMA_displayTable()
  448.  */
  449. function PMA_displayTableHeaders(&$is_display, &$fields_meta, $fields_cnt = 0, $analyzed_sql = '')
  450. {
  451.     global $db, $table, $goto;
  452.     global $sql_query, $num_rows;
  453.     global $vertical_display, $highlight_columns;
  454.  
  455.     if ($analyzed_sql == '') {
  456.         $analyzed_sql = array();
  457.     }
  458.  
  459.     // can the result be sorted?
  460.     if ($is_display['sort_lnk'] == '1') {
  461.  
  462.         // Just as fallback
  463.         $unsorted_sql_query     = $sql_query;
  464.         if (isset($analyzed_sql[0]['unsorted_query'])) {
  465.             $unsorted_sql_query = $analyzed_sql[0]['unsorted_query'];
  466.         }
  467.  
  468.         // we need $sort_expression and $sort_expression_nodir
  469.         // even if there are many table references
  470.  
  471.         $sort_expression = trim(str_replace('  ', ' ', $analyzed_sql[0]['order_by_clause']));
  472.  
  473.         /**
  474.          * Get rid of ASC|DESC
  475.          * @todo analyzer
  476.          */
  477.         preg_match('@(.*)([[:space:]]*(ASC|DESC))@si', $sort_expression, $matches);
  478.         $sort_expression_nodir = isset($matches[1]) ? trim($matches[1]) : $sort_expression;
  479.  
  480.         // sorting by indexes, only if it makes sense (only one table ref)
  481.         if (isset($analyzed_sql) && isset($analyzed_sql[0]) &&
  482.             isset($analyzed_sql[0]['querytype']) && $analyzed_sql[0]['querytype'] == 'SELECT' &&
  483.             isset($analyzed_sql[0]['table_ref']) && count($analyzed_sql[0]['table_ref']) == 1) {
  484.  
  485.             // grab indexes data:
  486.             PMA_DBI_select_db($db);
  487.             if (!defined('PMA_IDX_INCLUDED')) {
  488.                 $ret_keys = PMA_get_indexes($table);
  489.             }
  490.  
  491.             $prev_index = '';
  492.             foreach ($ret_keys as $row) {
  493.  
  494.                 if ($row['Key_name'] != $prev_index){
  495.                     $indexes[]  = $row['Key_name'];
  496.                     $prev_index = $row['Key_name'];
  497.                 }
  498.                 $indexes_info[$row['Key_name']]['Sequences'][]     = $row['Seq_in_index'];
  499.                 $indexes_info[$row['Key_name']]['Non_unique']      = $row['Non_unique'];
  500.                 if (isset($row['Cardinality'])) {
  501.                     $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
  502.                 }
  503.             //    I don't know what does the following column mean....
  504.             //    $indexes_info[$row['Key_name']]['Packed']          = $row['Packed'];
  505.                 $indexes_info[$row['Key_name']]['Comment']         = (isset($row['Comment']))
  506.                                                                    ? $row['Comment']
  507.                                                                    : '';
  508.                 $indexes_info[$row['Key_name']]['Index_type']      = (isset($row['Index_type']))
  509.                                                                    ? $row['Index_type']
  510.                                                                    : '';
  511.  
  512.                 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name']  = $row['Column_name'];
  513.                 if (isset($row['Sub_part'])) {
  514.                     $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
  515.                 }
  516.             } // end while
  517.  
  518.             // do we have any index?
  519.             if (isset($indexes_data)) {
  520.  
  521.                 if ($_SESSION['userconf']['disp_direction'] == 'horizontal'
  522.                  || $_SESSION['userconf']['disp_direction'] == 'horizontalflipped') {
  523.                     $span = $fields_cnt;
  524.                     if ($is_display['edit_lnk'] != 'nn') {
  525.                         $span++;
  526.                     }
  527.                     if ($is_display['del_lnk'] != 'nn') {
  528.                         $span++;
  529.                     }
  530.                     if ($is_display['del_lnk'] != 'kp' && $is_display['del_lnk'] != 'nn') {
  531.                         $span++;
  532.                     }
  533.                 } else {
  534.                     $span = $num_rows + floor($num_rows/$_SESSION['userconf']['repeat_cells']) + 1;
  535.                 }
  536.  
  537.                 echo '<form action="sql.php" method="post">' . "\n";
  538.                 echo PMA_generate_common_hidden_inputs($db, $table, 5);
  539.                 echo $GLOBALS['strSortByKey'] . ': <select name="sql_query" onchange="this.form.submit();">' . "\n";
  540.                 $used_index = false;
  541.                 $local_order = (isset($sort_expression) ? $sort_expression : '');
  542.                 foreach ($indexes_data AS $key => $val) {
  543.                     $asc_sort = '';
  544.                     $desc_sort = '';
  545.                     foreach ($val AS $key2 => $val2) {
  546.                         $asc_sort .= PMA_backquote($val2['Column_name']) . ' ASC , ';
  547.                         $desc_sort .= PMA_backquote($val2['Column_name']) . ' DESC , ';
  548.                     }
  549.                     $asc_sort = substr($asc_sort, 0, -3);
  550.                     $desc_sort = substr($desc_sort, 0, -3);
  551.                     $used_index = $used_index || $local_order == $asc_sort || $local_order == $desc_sort;
  552.                     echo '<option value="' . htmlspecialchars($unsorted_sql_query . ' ORDER BY ' . $asc_sort) . '"' . ($local_order == $asc_sort ? ' selected="selected"' : '') . '>' . htmlspecialchars($key) . ' (' . $GLOBALS['strAscending'] . ')</option>';
  553.                     echo "\n";
  554.                     echo '<option value="' . htmlspecialchars($unsorted_sql_query . ' ORDER BY ' . $desc_sort) . '"' . ($local_order == $desc_sort ? ' selected="selected"' : '') . '>' . htmlspecialchars($key) . ' (' . $GLOBALS['strDescending'] . ')</option>';
  555.                     echo "\n";
  556.                 }
  557.                 echo '<option value="' . htmlspecialchars($unsorted_sql_query) . '"' . ($used_index ? '' : ' selected="selected"') . '>' . $GLOBALS['strNone'] . '</option>';
  558.                 echo "\n";
  559.                 echo '</select>' . "\n";
  560.                 echo '<noscript><input type="submit" value="' . $GLOBALS['strGo'] . '" /></noscript>';
  561.                 echo "\n";
  562.                 echo '</form>' . "\n";
  563.             }
  564.         }
  565.     }
  566.  
  567.  
  568.     $vertical_display['emptypre']   = 0;
  569.     $vertical_display['emptyafter'] = 0;
  570.     $vertical_display['textbtn']    = '';
  571.  
  572.  
  573.     // Start of form for multi-rows delete
  574.  
  575.     if ($is_display['del_lnk'] == 'dr' || $is_display['del_lnk'] == 'kp') {
  576.         echo '<form method="post" action="tbl_row_action.php" name="rowsDeleteForm" id="rowsDeleteForm">' . "\n";
  577.         echo PMA_generate_common_hidden_inputs($db, $table, 1);
  578.         echo '<input type="hidden" name="goto"             value="sql.php" />' . "\n";
  579.     }
  580.  
  581.     echo '<table id="table_results" class="data">' . "\n";
  582.     if ($_SESSION['userconf']['disp_direction'] == 'horizontal'
  583.      || $_SESSION['userconf']['disp_direction'] == 'horizontalflipped') {
  584.         echo '<thead><tr>' . "\n";
  585.     }
  586.  
  587.     // 1. Displays the full/partial text button (part 1)...
  588.     if ($_SESSION['userconf']['disp_direction'] == 'horizontal'
  589.      || $_SESSION['userconf']['disp_direction'] == 'horizontalflipped') {
  590.         $colspan  = ($is_display['edit_lnk'] != 'nn' && $is_display['del_lnk'] != 'nn')
  591.                   ? ' colspan="3"'
  592.                   : '';
  593.     } else {
  594.         $rowspan  = ($is_display['edit_lnk'] != 'nn' && $is_display['del_lnk'] != 'nn')
  595.                   ? ' rowspan="3"'
  596.                   : '';
  597.     }
  598.     $url_params = array(
  599.         'db' => $db,
  600.         'table' => $table,
  601.         'sql_query' => $sql_query,
  602.         'goto' => $goto,
  603.     );
  604.     $text_message = '<img class="fulltext" width="50" height="20"';
  605.     if ($_SESSION['userconf']['dontlimitchars']) {
  606.         $url_params['dontlimitchars'] = '0';
  607.         $text_message .= ''
  608.             . ' src="' . $GLOBALS['pmaThemeImage'] . 's_partialtext.png"'
  609.             . ' alt="' . $GLOBALS['strPartialText'] . '"'
  610.             . ' title="' . $GLOBALS['strPartialText'] . '"';
  611.     } else {
  612.         $url_params['dontlimitchars'] = '1';
  613.         $text_message .= ''
  614.             . ' src="' . $GLOBALS['pmaThemeImage'] . 's_fulltext.png"'
  615.             . ' alt="' . $GLOBALS['strFullText'] . '"'
  616.             . ' title="' . $GLOBALS['strFullText'] . '"';
  617.     }
  618.     $text_message .= ' />';
  619.     $text_url = 'sql.php' . PMA_generate_common_url($url_params);
  620.     $text_link = PMA_linkOrButton($text_url, $text_message, array(), false);
  621.  
  622.     //     ... before the result table
  623.     if (($is_display['edit_lnk'] == 'nn' && $is_display['del_lnk'] == 'nn')
  624.         && $is_display['text_btn'] == '1') {
  625.         $vertical_display['emptypre'] = ($is_display['edit_lnk'] != 'nn' && $is_display['del_lnk'] != 'nn') ? 3 : 0;
  626.         if ($_SESSION['userconf']['disp_direction'] == 'horizontal'
  627.          || $_SESSION['userconf']['disp_direction'] == 'horizontalflipped') {
  628.             ?>
  629.     <th colspan="<?php echo $fields_cnt; ?>"><?php echo $text_link; ?></th>
  630. </tr>
  631. <tr>
  632.             <?php
  633.         } // end horizontal/horizontalflipped mode
  634.         else {
  635.             ?>
  636. <tr>
  637.     <th colspan="<?php echo $num_rows + floor($num_rows/$_SESSION['userconf']['repeat_cells']) + 1; ?>">
  638.         <?php echo $text_link; ?></th>
  639. </tr>
  640.             <?php
  641.         } // end vertical mode
  642.     }
  643.  
  644.     //     ... at the left column of the result table header if possible
  645.     //     and required
  646.     elseif ($GLOBALS['cfg']['ModifyDeleteAtLeft'] && $is_display['text_btn'] == '1') {
  647.         $vertical_display['emptypre'] = ($is_display['edit_lnk'] != 'nn' && $is_display['del_lnk'] != 'nn') ? 3 : 0;
  648.         if ($_SESSION['userconf']['disp_direction'] == 'horizontal'
  649.          || $_SESSION['userconf']['disp_direction'] == 'horizontalflipped') {
  650.             ?>
  651.     <th <?php echo $colspan; ?>><?php echo $text_link; ?></th>
  652.             <?php
  653.         } // end horizontal/horizontalflipped mode
  654.         else {
  655.             $vertical_display['textbtn'] = '    <th ' . $rowspan . ' valign="middle">' . "\n"
  656.                                          . '        ' . $text_link . "\n"
  657.                                          . '    </th>' . "\n";
  658.         } // end vertical mode
  659.     }
  660.  
  661.     //     ... elseif no button, displays empty(ies) col(s) if required
  662.     elseif ($GLOBALS['cfg']['ModifyDeleteAtLeft']
  663.              && ($is_display['edit_lnk'] != 'nn' || $is_display['del_lnk'] != 'nn')) {
  664.         $vertical_display['emptypre'] = ($is_display['edit_lnk'] != 'nn' && $is_display['del_lnk'] != 'nn') ? 3 : 0;
  665.         if ($_SESSION['userconf']['disp_direction'] == 'horizontal'
  666.          || $_SESSION['userconf']['disp_direction'] == 'horizontalflipped') {
  667.             ?>
  668.     <td<?php echo $colspan; ?>></td>
  669.             <?php
  670.         } // end horizontal/horizontalfipped mode
  671.         else {
  672.             $vertical_display['textbtn'] = '    <td' . $rowspan . '></td>' . "\n";
  673.         } // end vertical mode
  674.     }
  675.  
  676.     // 2. Displays the fields' name
  677.     // 2.0 If sorting links should be used, checks if the query is a "JOIN"
  678.     //     statement (see 2.1.3)
  679.  
  680.     // 2.0.1 Prepare Display column comments if enabled ($GLOBALS['cfg']['ShowBrowseComments']).
  681.     //       Do not show comments, if using horizontalflipped mode, because of space usage
  682.     if ($GLOBALS['cfg']['ShowBrowseComments']
  683.      && ($GLOBALS['cfgRelation']['commwork']
  684.       || PMA_MYSQL_INT_VERSION >= 40100)
  685.      && $_SESSION['userconf']['disp_direction'] != 'horizontalflipped') {
  686.         $comments_map = array();
  687.         if (isset($analyzed_sql[0]) && is_array($analyzed_sql[0])) {
  688.             foreach ($analyzed_sql[0]['table_ref'] as $tbl) {
  689.                 $tb = $tbl['table_true_name'];
  690.                 $comments_map[$tb] = PMA_getComments($db, $tb);
  691.                 unset($tb);
  692.             }
  693.         }
  694.     }
  695.  
  696.     if ($GLOBALS['cfgRelation']['commwork'] && $GLOBALS['cfgRelation']['mimework'] && $GLOBALS['cfg']['BrowseMIME']) {
  697.         require_once './libraries/transformations.lib.php';
  698.         $GLOBALS['mime_map'] = PMA_getMIME($db, $table);
  699.     }
  700.  
  701.     if ($is_display['sort_lnk'] == '1') {
  702.         //$is_join = preg_match('@(.*)[[:space:]]+FROM[[:space:]]+.*[[:space:]]+JOIN@im', $sql_query, $select_stt);
  703.         $is_join = (isset($analyzed_sql[0]['queryflags']['join']) ? true : false);
  704.         $select_expr = $analyzed_sql[0]['select_expr_clause'];
  705.     } else {
  706.         $is_join = false;
  707.     }
  708.  
  709.     // garvin: See if we have to highlight any header fields of a WHERE query.
  710.     //  Uses SQL-Parser results.
  711.     $highlight_columns = array();
  712.     if (isset($analyzed_sql) && isset($analyzed_sql[0]) &&
  713.         isset($analyzed_sql[0]['where_clause_identifiers'])) {
  714.  
  715.         $wi = 0;
  716.         if (isset($analyzed_sql[0]['where_clause_identifiers']) && is_array($analyzed_sql[0]['where_clause_identifiers'])) {
  717.             foreach ($analyzed_sql[0]['where_clause_identifiers'] AS $wci_nr => $wci) {
  718.                 $highlight_columns[$wci] = 'true';
  719.             }
  720.         }
  721.     }
  722.  
  723.     for ($i = 0; $i < $fields_cnt; $i++) {
  724.         // garvin: See if this column should get highlight because it's used in the
  725.         //  where-query.
  726.         if (isset($highlight_columns[$fields_meta[$i]->name]) || isset($highlight_columns[PMA_backquote($fields_meta[$i]->name)])) {
  727.             $condition_field = true;
  728.         } else {
  729.             $condition_field = false;
  730.         }
  731.  
  732.         // 2.0 Prepare comment-HTML-wrappers for each row, if defined/enabled.
  733.         if (isset($comments_map) &&
  734.                 isset($comments_map[$fields_meta[$i]->table]) &&
  735.                 isset($comments_map[$fields_meta[$i]->table][$fields_meta[$i]->name])) {
  736.             $comments = '<span class="tblcomment">' . htmlspecialchars($comments_map[$fields_meta[$i]->table][$fields_meta[$i]->name]) . '</span>';
  737.         } else {
  738.             $comments = '';
  739.         }
  740.  
  741.         // 2.1 Results can be sorted
  742.         if ($is_display['sort_lnk'] == '1') {
  743.  
  744.             // 2.1.1 Checks if the table name is required; it's the case
  745.             //       for a query with a "JOIN" statement and if the column
  746.             //       isn't aliased, or in queries like
  747.             //       SELECT `1`.`master_field` , `2`.`master_field`
  748.             //       FROM `PMA_relation` AS `1` , `PMA_relation` AS `2`
  749.             /**
  750.              * we prefer always using table if existing
  751.              * and second this code does not correctly check $fields_meta[$i]->table
  752.             if (($is_join
  753.                 && !preg_match('~([^[:space:],]|`[^`]`)[[:space:]]+(as[[:space:]]+)?' . strtr($fields_meta[$i]->name, array('[' => '\\[', '~' => '\\~', '\\' => '\\\\')) . '~i', $select_expr, $parts))
  754.                || (isset($analyzed_sql[0]['select_expr'][$i]['expr'])
  755.                    && isset($analyzed_sql[0]['select_expr'][$i]['column'])
  756.                    && $analyzed_sql[0]['select_expr'][$i]['expr'] !=
  757.                    $analyzed_sql[0]['select_expr'][$i]['column']
  758.                   && isset($fields_meta[$i]->table) && strlen($fields_meta[$i]->table))) {
  759.             */
  760.             if (isset($fields_meta[$i]->table) && strlen($fields_meta[$i]->table)) {
  761.                 $sort_tbl = PMA_backquote($fields_meta[$i]->table) . '.';
  762.             } else {
  763.                 $sort_tbl = '';
  764.             }
  765.  
  766.             // 2.1.2 Checks if the current column is used to sort the
  767.             //       results
  768.             if (empty($sort_expression)) {
  769.                 $is_in_sort = false;
  770.             } else {
  771.                 // field name may be preceded by a space, or any number
  772.                 // of characters followed by a dot (tablename.fieldname)
  773.                 // so do a direct comparison
  774.                 // for the sort expression (avoids problems with queries
  775.                 // like "SELECT id, count(id)..." and clicking to sort
  776.                 // on id or on count(id))
  777.                 $is_in_sort = ($sort_tbl . PMA_backquote($fields_meta[$i]->name) == $sort_expression_nodir ? true : false);
  778.             }
  779.             // 2.1.3 Check the field name for backquotes.
  780.             //       If it contains some, it's probably a function column
  781.             //       like 'COUNT(`field`)'
  782.             if (strpos($fields_meta[$i]->name, '`') !== false) {
  783.                 $sort_order = ' ORDER BY ' . PMA_backquote($fields_meta[$i]->name) . ' ';
  784.             } else {
  785.                 $sort_order = ' ORDER BY ' . $sort_tbl . PMA_backquote($fields_meta[$i]->name) . ' ';
  786.             }
  787.  
  788.             // 2.1.4 Do define the sorting URL
  789.             if (! $is_in_sort) {
  790.                 // loic1: patch #455484 ("Smart" order)
  791.                 $GLOBALS['cfg']['Order'] = strtoupper($GLOBALS['cfg']['Order']);
  792.                 if ($GLOBALS['cfg']['Order'] === 'SMART') {
  793.                     $sort_order .= (preg_match('@time|date@i', $fields_meta[$i]->type)) ? 'DESC' : 'ASC';
  794.                 } else {
  795.                     $sort_order .= $GLOBALS['cfg']['Order'];
  796.                 }
  797.                 $order_img   = '';
  798.             } elseif (preg_match('@[[:space:]]DESC$@i', $sort_expression)) {
  799.                 $sort_order .= ' ASC';
  800.                 $order_img   = ' <img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 's_desc.png" width="11" height="9" alt="'. $GLOBALS['strDescending'] . '" title="'. $GLOBALS['strDescending'] . '" id="soimg' . $i . '" />';
  801.             } else {
  802.                 $sort_order .= ' DESC';
  803.                 $order_img   = ' <img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 's_asc.png" width="11" height="9" alt="'. $GLOBALS['strAscending'] . '" title="'. $GLOBALS['strAscending'] . '" id="soimg' . $i . '" />';
  804.             }
  805.  
  806.             if (preg_match('@(.*)([[:space:]](LIMIT (.*)|PROCEDURE (.*)|FOR UPDATE|LOCK IN SHARE MODE))@i', $unsorted_sql_query, $regs3)) {
  807.                 $sorted_sql_query = $regs3[1] . $sort_order . $regs3[2];
  808.             } else {
  809.                 $sorted_sql_query = $unsorted_sql_query . $sort_order;
  810.             }
  811.             $url_query = PMA_generate_common_url($db, $table)
  812.                        . '&sql_query=' . urlencode($sorted_sql_query);
  813.             $order_url  = 'sql.php?' . $url_query;
  814.  
  815.             // 2.1.5 Displays the sorting URL
  816.             // added 20004-06-09: Michael Keck <mail@michaelkeck.de>
  817.             //                    enable sort order swapping for image
  818.             $order_link_params = array();
  819.             if (isset($order_img) && $order_img!='') {
  820.                 if (strstr($order_img, 'asc')) {
  821.                     $order_link_params['onmouseover'] = 'if(document.getElementById(\'soimg' . $i . '\')){ document.getElementById(\'soimg' . $i . '\').src=\'' . $GLOBALS['pmaThemeImage'] . 's_desc.png\'; }';
  822.                     $order_link_params['onmouseout']  = 'if(document.getElementById(\'soimg' . $i . '\')){ document.getElementById(\'soimg' . $i . '\').src=\'' . $GLOBALS['pmaThemeImage'] . 's_asc.png\'; }';
  823.                 } elseif (strstr($order_img, 'desc')) {
  824.                     $order_link_params['onmouseover'] = 'if(document.getElementById(\'soimg' . $i . '\')){ document.getElementById(\'soimg' . $i . '\').src=\'' . $GLOBALS['pmaThemeImage'] . 's_asc.png\'; }';
  825.                     $order_link_params['onmouseout']  = 'if(document.getElementById(\'soimg' . $i . '\')){ document.getElementById(\'soimg' . $i . '\').src=\'' . $GLOBALS['pmaThemeImage'] . 's_desc.png\'; }';
  826.                 }
  827.             }
  828.             if ($_SESSION['userconf']['disp_direction'] == 'horizontalflipped'
  829.              && $GLOBALS['cfg']['HeaderFlipType'] == 'css') {
  830.                 $order_link_params['style'] = 'direction: ltr; writing-mode: tb-rl;';
  831.             }
  832.             $order_link_params['title'] = $GLOBALS['strSort'];
  833.             $order_link_content = ($_SESSION['userconf']['disp_direction'] == 'horizontalflipped' && $GLOBALS['cfg']['HeaderFlipType'] == 'fake' ? PMA_flipstring(htmlspecialchars($fields_meta[$i]->name), "<br />\n") : htmlspecialchars($fields_meta[$i]->name));
  834.             $order_link = PMA_linkOrButton($order_url, $order_link_content . $order_img, $order_link_params, false, true);
  835.  
  836.             if ($_SESSION['userconf']['disp_direction'] == 'horizontal'
  837.              || $_SESSION['userconf']['disp_direction'] == 'horizontalflipped') {
  838.                 echo '<th';
  839.                 if ($condition_field) {
  840.                     echo ' class="condition"';
  841.                 }
  842.                 if ($_SESSION['userconf']['disp_direction'] == 'horizontalflipped') {
  843.                     echo ' valign="bottom"';
  844.                 }
  845.                 echo '>' . $order_link . $comments . '</th>';
  846.             }
  847.             $vertical_display['desc'][] = '    <th '
  848.                 . ($condition_field ? ' class="condition"' : '') . '>' . "\n"
  849.                 . $order_link . $comments . '    </th>' . "\n";
  850.         } // end if (2.1)
  851.  
  852.         // 2.2 Results can't be sorted
  853.         else {
  854.             if ($_SESSION['userconf']['disp_direction'] == 'horizontal'
  855.              || $_SESSION['userconf']['disp_direction'] == 'horizontalflipped') {
  856.                 echo '<th';
  857.                 if ($condition_field) {
  858.                     echo ' class="condition"';
  859.                 }
  860.                 if ($_SESSION['userconf']['disp_direction'] == 'horizontalflipped') {
  861.                     echo ' valign="bottom"';
  862.                 }
  863.                 if ($_SESSION['userconf']['disp_direction'] == 'horizontalflipped'
  864.                  && $GLOBALS['cfg']['HeaderFlipType'] == 'css') {
  865.                     echo ' style="direction: ltr; writing-mode: tb-rl;"';
  866.                 }
  867.                 echo '>';
  868.                 if ($_SESSION['userconf']['disp_direction'] == 'horizontalflipped'
  869.                  && $GLOBALS['cfg']['HeaderFlipType'] == 'fake') {
  870.                     echo PMA_flipstring(htmlspecialchars($fields_meta[$i]->name), '<br />');
  871.                 } else {
  872.                     echo htmlspecialchars($fields_meta[$i]->name);
  873.                 }
  874.                 echo "\n" . $comments . '</th>';
  875.             }
  876.             $vertical_display['desc'][] = '    <th '
  877.                 . ($condition_field ? ' class="condition"' : '') . '>' . "\n"
  878.                 . '        ' . htmlspecialchars($fields_meta[$i]->name) . "\n"
  879.                 . $comments . '    </th>';
  880.         } // end else (2.2)
  881.     } // end for
  882.  
  883.     // 3. Displays the full/partial text button (part 2) at the right
  884.     //    column of the result table header if possible and required...
  885.     if ($GLOBALS['cfg']['ModifyDeleteAtRight']
  886.         && ($is_display['edit_lnk'] != 'nn' || $is_display['del_lnk'] != 'nn')
  887.         && $is_display['text_btn'] == '1') {
  888.         $vertical_display['emptyafter'] = ($is_display['edit_lnk'] != 'nn' && $is_display['del_lnk'] != 'nn') ? 3 : 1;
  889.         if ($_SESSION['userconf']['disp_direction'] == 'horizontal'
  890.          || $_SESSION['userconf']['disp_direction'] == 'horizontalflipped') {
  891.             echo "\n";
  892.             ?>
  893. <th <?php echo $colspan; ?>>
  894.     <?php echo $text_link; ?>
  895. </th>
  896.             <?php
  897.         } // end horizontal/horizontalflipped mode
  898.         else {
  899.             $vertical_display['textbtn'] = '    <th ' . $rowspan . ' valign="middle">' . "\n"
  900.                                          . '        ' . $text_link . "\n"
  901.                                          . '    </th>' . "\n";
  902.         } // end vertical mode
  903.     }
  904.  
  905.     //     ... elseif no button, displays empty columns if required
  906.     // (unless coming from Browse mode print view)
  907.     elseif ($GLOBALS['cfg']['ModifyDeleteAtRight']
  908.              && ($is_display['edit_lnk'] == 'nn' && $is_display['del_lnk'] == 'nn')
  909.              && (!$GLOBALS['is_header_sent'])) {
  910.         $vertical_display['emptyafter'] = ($is_display['edit_lnk'] != 'nn' && $is_display['del_lnk'] != 'nn') ? 3 : 1;
  911.         if ($_SESSION['userconf']['disp_direction'] == 'horizontal'
  912.          || $_SESSION['userconf']['disp_direction'] == 'horizontalflipped') {
  913.             echo "\n";
  914.             ?>
  915. <td<?php echo $colspan; ?>></td>
  916.             <?php
  917.         } // end horizontal/horizontalflipped mode
  918.         else {
  919.             $vertical_display['textbtn'] = '    <td' . $rowspan . '></td>' . "\n";
  920.         } // end vertical mode
  921.     }
  922.  
  923.     if ($_SESSION['userconf']['disp_direction'] == 'horizontal'
  924.      || $_SESSION['userconf']['disp_direction'] == 'horizontalflipped') {
  925.         ?>
  926. </tr>
  927. </thead>
  928.         <?php
  929.     }
  930.  
  931.     return true;
  932. } // end of the 'PMA_displayTableHeaders()' function
  933.  
  934.  
  935.  
  936. /**
  937.  * Displays the body of the results table
  938.  *
  939.  * @uses    $_SESSION['userconf']['disp_direction']
  940.  * @uses    $_SESSION['userconf']['repeat_cells']
  941.  * @uses    $_SESSION['userconf']['max_rows']
  942.  * @uses    $_SESSION['userconf']['dontlimitchars']
  943.  * @param   integer  the link id associated to the query which results have
  944.  *                   to be displayed
  945.  * @param   array    which elements to display
  946.  * @param   array    the list of relations
  947.  * @param   array    the analyzed query
  948.  *
  949.  * @return  boolean  always true
  950.  *
  951.  * @global  string   $db                the database name
  952.  * @global  string   $table             the table name
  953.  * @global  string   $goto              the URL to go back in case of errors
  954.  * @global  string   $sql_query         the SQL query
  955.  * @global  array    $fields_meta       the list of fields properties
  956.  * @global  integer  $fields_cnt        the total number of fields returned by
  957.  *                                      the SQL query
  958.  * @global  array    $vertical_display  informations used with vertical display
  959.  *                                      mode
  960.  * @global  array    $highlight_columns column names to highlight
  961.  * @global  array    $row               current row data
  962.  *
  963.  * @access  private
  964.  *
  965.  * @see     PMA_displayTable()
  966.  */
  967. function PMA_displayTableBody(&$dt_result, &$is_display, $map, $analyzed_sql) {
  968.     global $db, $table, $goto;
  969.     global $sql_query, $fields_meta, $fields_cnt;
  970.     global $vertical_display, $highlight_columns;
  971.     global $row; // mostly because of browser transformations, to make the row-data accessible in a plugin
  972.  
  973.     $url_sql_query          = $sql_query;
  974.  
  975.     // query without conditions to shorten URLs when needed, 200 is just
  976.     // guess, it should depend on remaining URL length
  977.  
  978.     if (isset($analyzed_sql) && isset($analyzed_sql[0]) &&
  979.         isset($analyzed_sql[0]['querytype']) && $analyzed_sql[0]['querytype'] == 'SELECT' &&
  980.         strlen($sql_query) > 200) {
  981.  
  982.         $url_sql_query = 'SELECT ';
  983.         if (isset($analyzed_sql[0]['queryflags']['distinct'])) {
  984.             $url_sql_query .= ' DISTINCT ';
  985.         }
  986.         $url_sql_query .= $analyzed_sql[0]['select_expr_clause'];
  987.         if (!empty($analyzed_sql[0]['from_clause'])) {
  988.             $url_sql_query .= ' FROM ' . $analyzed_sql[0]['from_clause'];
  989.         }
  990.     }
  991.  
  992.     if (!is_array($map)) {
  993.         $map = array();
  994.     }
  995.     $row_no                         = 0;
  996.     $vertical_display['edit']       = array();
  997.     $vertical_display['delete']     = array();
  998.     $vertical_display['data']       = array();
  999.     $vertical_display['row_delete'] = array();
  1000.  
  1001.     // Correction University of Virginia 19991216 in the while below
  1002.     // Previous code assumed that all tables have keys, specifically that
  1003.     // the phpMyAdmin GUI should support row delete/edit only for such
  1004.     // tables.
  1005.     // Although always using keys is arguably the prescribed way of
  1006.     // defining a relational table, it is not required. This will in
  1007.     // particular be violated by the novice.
  1008.     // We want to encourage phpMyAdmin usage by such novices. So the code
  1009.     // below has been changed to conditionally work as before when the
  1010.     // table being displayed has one or more keys; but to display
  1011.     // delete/edit options correctly for tables without keys.
  1012.  
  1013.     // loic1: use 'PMA_mysql_fetch_array' rather than 'PMA_mysql_fetch_row'
  1014.     //        to get the NULL values
  1015.  
  1016.     // rabus: This function needs a little rework.
  1017.     //        Using MYSQL_BOTH just pollutes the memory!
  1018.  
  1019.     // ne0x:  Use function PMA_DBI_fetch_array() due to mysqli
  1020.     //        compatibility. Now this function is wrapped.
  1021.  
  1022.     $odd_row = true;
  1023.     while ($row = PMA_DBI_fetch_row($dt_result)) {
  1024.         // lem9: "vertical display" mode stuff
  1025.         if ($row_no != 0 && $_SESSION['userconf']['repeat_cells'] != 0 && !($row_no % $_SESSION['userconf']['repeat_cells'])
  1026.           && ($_SESSION['userconf']['disp_direction'] == 'horizontal'
  1027.            || $_SESSION['userconf']['disp_direction'] == 'horizontalflipped'))
  1028.         {
  1029.             echo '<tr>' . "\n";
  1030.             if ($vertical_display['emptypre'] > 0) {
  1031.                 echo '    <th colspan="' . $vertical_display['emptypre'] . '">' . "\n"
  1032.                     .'         </th>' . "\n";
  1033.             }
  1034.  
  1035.             foreach ($vertical_display['desc'] as $val) {
  1036.                 echo $val;
  1037.             }
  1038.  
  1039.             if ($vertical_display['emptyafter'] > 0) {
  1040.                 echo '    <th colspan="' . $vertical_display['emptyafter'] . '">' . "\n"
  1041.                     .'         </th>' . "\n";
  1042.             }
  1043.             echo '</tr>' . "\n";
  1044.         } // end if
  1045.  
  1046.         $class = $odd_row ? 'odd' : 'even';
  1047.         $odd_row = ! $odd_row;
  1048.         if ($_SESSION['userconf']['disp_direction'] == 'horizontal'
  1049.          || $_SESSION['userconf']['disp_direction'] == 'horizontalflipped') {
  1050.             // loic1: pointer code part
  1051.             echo '    <tr class="' . $class . '">' . "\n";
  1052.             $class = '';
  1053.         }
  1054.  
  1055.  
  1056.         // 1. Prepares the row (gets primary keys to use)
  1057.         // 1.1 Results from a "SELECT" statement -> builds the
  1058.         //     "primary" key to use in links
  1059.         /**
  1060.          * @todo $unique_condition could be empty, for example a table
  1061.          *       with only one field and it's a BLOB; in this case,
  1062.          *       avoid to display the delete and edit links
  1063.          */
  1064.         $unique_condition     = urlencode(PMA_getUniqueCondition($dt_result, $fields_cnt, $fields_meta, $row));
  1065.  
  1066.         // 1.2 Defines the URLs for the modify/delete link(s)
  1067.         $url_query  = PMA_generate_common_url($db, $table);
  1068.  
  1069.         if ($is_display['edit_lnk'] != 'nn' || $is_display['del_lnk'] != 'nn') {
  1070.             // We need to copy the value or else the == 'both' check will always return true
  1071.  
  1072.             if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
  1073.                 $iconic_spacer = '<div class="nowrap">';
  1074.             } else {
  1075.                 $iconic_spacer = '';
  1076.             }
  1077.  
  1078.             // 1.2.1 Modify link(s)
  1079.             if ($is_display['edit_lnk'] == 'ur') { // update row case
  1080.                 $lnk_goto = 'sql.php';
  1081.  
  1082.                 $edit_url = 'tbl_change.php'
  1083.                           . '?' . $url_query
  1084.                           . '&primary_key=' . $unique_condition
  1085.                           . '&sql_query=' . urlencode($url_sql_query)
  1086.                           . '&goto=' . urlencode($lnk_goto);
  1087.                 if ($GLOBALS['cfg']['PropertiesIconic'] === false) {
  1088.                     $edit_str = $GLOBALS['strEdit'];
  1089.                 } else {
  1090.                     $edit_str = $iconic_spacer . '<img class="icon" width="16" height="16" src="' . $GLOBALS['pmaThemeImage'] . 'b_edit.png" alt="' . $GLOBALS['strEdit'] . '" title="' . $GLOBALS['strEdit'] . '" />';
  1091.                     if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
  1092.                         $edit_str .= ' ' . $GLOBALS['strEdit'] . '</div>';
  1093.                     }
  1094.                 }
  1095.             } // end if (1.2.1)
  1096.  
  1097.             if (isset($GLOBALS['cfg']['Bookmark']['table']) && isset($GLOBALS['cfg']['Bookmark']['db']) && $table == $GLOBALS['cfg']['Bookmark']['table'] && $db == $GLOBALS['cfg']['Bookmark']['db'] && isset($row[1]) && isset($row[0])) {
  1098.                 $bookmark_go = '<a href="import.php?'
  1099.                                 . PMA_generate_common_url($row[1], '')
  1100.                                 . '&id_bookmark=' . $row[0]
  1101.                                 . '&action_bookmark=0'
  1102.                                 . '&action_bookmark_all=1'
  1103.                                 . '&SQL=' . $GLOBALS['strExecuteBookmarked']
  1104.                                 .' " title="' . $GLOBALS['strExecuteBookmarked'] . '">';
  1105.  
  1106.                 if ($GLOBALS['cfg']['PropertiesIconic'] === false) {
  1107.                     $bookmark_go .= $GLOBALS['strExecuteBookmarked'];
  1108.                 } else {
  1109.                     $bookmark_go .= $iconic_spacer . '<img class="icon" width="16" height="16" src="' . $GLOBALS['pmaThemeImage'] . 'b_bookmark.png" alt="' . $GLOBALS['strExecuteBookmarked'] . '" title="' . $GLOBALS['strExecuteBookmarked'] . '" />';
  1110.                     if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
  1111.                         $bookmark_go .= ' ' . $GLOBALS['strExecuteBookmarked'] . '</div>';
  1112.                     }
  1113.                 }
  1114.  
  1115.                 $bookmark_go .= '</a>';
  1116.             } else {
  1117.                 $bookmark_go = '';
  1118.             }
  1119.  
  1120.             // 1.2.2 Delete/Kill link(s)
  1121.             if ($is_display['del_lnk'] == 'dr') { // delete row case
  1122.                 $lnk_goto = 'sql.php'
  1123.                           . '?' . str_replace('&', '&', $url_query)
  1124.                           . '&sql_query=' . urlencode($url_sql_query)
  1125.                           . '&zero_rows=' . urlencode(htmlspecialchars($GLOBALS['strDeleted']))
  1126.                           . '&goto=' . (empty($goto) ? 'tbl_sql.php' : $goto);
  1127.                 $del_query = urlencode('DELETE FROM ' . PMA_backquote($table) . ' WHERE') . $unique_condition . '+LIMIT+1';
  1128.                 $del_url  = 'sql.php'
  1129.                           . '?' . $url_query
  1130.                           . '&sql_query=' . $del_query
  1131.                           . '&zero_rows=' . urlencode(htmlspecialchars($GLOBALS['strDeleted']))
  1132.                           . '&goto=' . urlencode($lnk_goto);
  1133.                 $js_conf  = 'DELETE FROM ' . PMA_jsFormat($table)
  1134.                           . ' WHERE ' . trim(PMA_jsFormat(urldecode($unique_condition), false))
  1135.                           . ' LIMIT 1';
  1136.                 if ($GLOBALS['cfg']['PropertiesIconic'] === false) {
  1137.                     $del_str = $GLOBALS['strDelete'];
  1138.                 } else {
  1139.                     $del_str = $iconic_spacer . '<img class="icon" width="16" height="16" src="' . $GLOBALS['pmaThemeImage'] . 'b_drop.png" alt="' . $GLOBALS['strDelete'] . '" title="' . $GLOBALS['strDelete'] . '" />';
  1140.                     if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
  1141.                         $del_str .= ' ' . $GLOBALS['strDelete'] . '</div>';
  1142.                     }
  1143.                 }
  1144.             } elseif ($is_display['del_lnk'] == 'kp') { // kill process case
  1145.                 $lnk_goto = 'sql.php'
  1146.                           . '?' . str_replace('&', '&', $url_query)
  1147.                           . '&sql_query=' . urlencode($url_sql_query)
  1148.                           . '&goto=main.php';
  1149.                 $del_url  = 'sql.php?'
  1150.                           . PMA_generate_common_url('mysql')
  1151.                           . '&sql_query=' . urlencode('KILL ' . $row[0])
  1152.                           . '&goto=' . urlencode($lnk_goto);
  1153.                 $del_query = urlencode('KILL ' . $row[0]);
  1154.                 $js_conf  = 'KILL ' . $row[0];
  1155.                 if ($GLOBALS['cfg']['PropertiesIconic'] === false) {
  1156.                     $del_str = $GLOBALS['strKill'];
  1157.                 } else {
  1158.                     $del_str = $iconic_spacer . '<img class="icon" width="16" height="16" src="' . $GLOBALS['pmaThemeImage'] . 'b_drop.png" alt="' . $GLOBALS['strKill'] . '" title="' . $GLOBALS['strKill'] . '" />';
  1159.                     if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
  1160.                         $del_str .= ' ' . $GLOBALS['strKill'] . '</div>';
  1161.                     }
  1162.                 }
  1163.             } // end if (1.2.2)
  1164.  
  1165.             // 1.3 Displays the links at left if required
  1166.             if ($GLOBALS['cfg']['ModifyDeleteAtLeft']
  1167.              && ($_SESSION['userconf']['disp_direction'] == 'horizontal'
  1168.               || $_SESSION['userconf']['disp_direction'] == 'horizontalflipped')) {
  1169.                 $doWriteModifyAt = 'left';
  1170.                 require './libraries/display_tbl_links.lib.php';
  1171.             } // end if (1.3)
  1172.         } // end if (1)
  1173.  
  1174.         // 2. Displays the rows' values
  1175.         for ($i = 0; $i < $fields_cnt; ++$i) {
  1176.             $meta    = $fields_meta[$i];
  1177.             // loic1: To fix bug #474943 under php4, the row pointer will
  1178.             //        depend on whether the "is_null" php4 function is
  1179.             //        available or not
  1180.             $pointer = (function_exists('is_null') ? $i : $meta->name);
  1181.             // garvin: See if this column should get highlight because it's used in the
  1182.             //  where-query.
  1183.             if (isset($highlight_columns) && (isset($highlight_columns[$meta->name]) || isset($highlight_columns[PMA_backquote($meta->name)]))) {
  1184.                 $condition_field = true;
  1185.             } else {
  1186.                 $condition_field = false;
  1187.             }
  1188.  
  1189.             $mouse_events = '';
  1190.             if ($_SESSION['userconf']['disp_direction'] == 'vertical' && (!isset($GLOBALS['printview']) || ($GLOBALS['printview'] != '1'))) {
  1191.                 if ($GLOBALS['cfg']['BrowsePointerEnable'] == true) {
  1192.                     $mouse_events .= ' onmouseover="setVerticalPointer(this, ' . $row_no . ', \'over\', \'odd\', \'even\', \'hover\', \'marked\');"'
  1193.                               . ' onmouseout="setVerticalPointer(this, ' . $row_no . ', \'out\', \'odd\', \'even\', \'hover\', \'marked\');" ';
  1194.                 }
  1195.                 if ($GLOBALS['cfg']['BrowseMarkerEnable'] == true) {
  1196.                     $mouse_events .= ' onmousedown="setVerticalPointer(this, ' . $row_no . ', \'click\', \'odd\', \'even\', \'hover\', \'marked\'); setCheckboxColumn(\'id_rows_to_delete' . $row_no . '\');" ';
  1197.                 } else {
  1198.                     $mouse_events .= ' onmousedown="setCheckboxColumn(\'id_rows_to_delete' . $row_no . '\');" ';
  1199.                 }
  1200.             }// end if
  1201.  
  1202.             // garvin: Wrap MIME-transformations. [MIME]
  1203.             $default_function = 'default_function'; // default_function
  1204.             $transform_function = $default_function;
  1205.             $transform_options = array();
  1206.  
  1207.             if ($GLOBALS['cfgRelation']['mimework'] && $GLOBALS['cfg']['BrowseMIME']) {
  1208.  
  1209.                 if (isset($GLOBALS['mime_map'][$meta->name]['mimetype']) && isset($GLOBALS['mime_map'][$meta->name]['transformation']) && !empty($GLOBALS['mime_map'][$meta->name]['transformation'])) {
  1210.                     $include_file = PMA_sanitizeTransformationFile($GLOBALS['mime_map'][$meta->name]['transformation']);
  1211.  
  1212.                     if (file_exists('./libraries/transformations/' . $include_file)) {
  1213.                         $transformfunction_name = preg_replace('@(\.inc\.php3?)$@i', '', $GLOBALS['mime_map'][$meta->name]['transformation']);
  1214.  
  1215.                         require_once './libraries/transformations/' . $include_file;
  1216.  
  1217.                         if (function_exists('PMA_transformation_' . $transformfunction_name)) {
  1218.                             $transform_function = 'PMA_transformation_' . $transformfunction_name;
  1219.                             $transform_options  = PMA_transformation_getOptions((isset($GLOBALS['mime_map'][$meta->name]['transformation_options']) ? $GLOBALS['mime_map'][$meta->name]['transformation_options'] : ''));
  1220.                             $meta->mimetype     = str_replace('_', '/', $GLOBALS['mime_map'][$meta->name]['mimetype']);
  1221.                         }
  1222.                     } // end if file_exists
  1223.                 } // end if transformation is set
  1224.             } // end if mime/transformation works.
  1225.  
  1226.             $transform_options['wrapper_link'] = '?'
  1227.                                                 . (isset($url_query) ? $url_query : '')
  1228.                                                 . '&primary_key=' . (isset($unique_condition) ? $unique_condition : '')
  1229.                                                 . '&sql_query=' . (empty($sql_query) ? '' : urlencode($url_sql_query))
  1230.                                                 . '&goto=' . (isset($sql_goto) ? urlencode($lnk_goto) : '')
  1231.                                                 . '&transform_key=' . urlencode($meta->name);
  1232.  
  1233.  
  1234.             // n u m e r i c
  1235.             if ($meta->numeric == 1) {
  1236.  
  1237.  
  1238.             // lem9: if two fields have the same name (this is possible
  1239.             //       with self-join queries, for example), using $meta->name
  1240.             //       will show both fields NULL even if only one is NULL,
  1241.             //       so use the $pointer
  1242.             //      (works only if function_exists('is_null')
  1243.             // PS:   why not always work with the number ($i), since
  1244.             //       the default second parameter of
  1245.             //       mysql_fetch_array() is MYSQL_BOTH, so we always get
  1246.             //       associative and numeric indices?
  1247.  
  1248.                 //if (!isset($row[$meta->name])
  1249.                 if (!isset($row[$i]) || is_null($row[$i])) {
  1250.                     $vertical_display['data'][$row_no][$i]     = '    <td align="right"' . $mouse_events . ' class="' . $class . ($condition_field ? ' condition' : '') . '"><i>NULL</i></td>' . "\n";
  1251.                 } elseif ($row[$i] != '') {
  1252.                     $vertical_display['data'][$row_no][$i]     = '    <td align="right"' . $mouse_events . ' class="' . $class . ($condition_field ? ' condition' : '') . ' nowrap">';
  1253.  
  1254.                     if (isset($analyzed_sql[0]['select_expr']) && is_array($analyzed_sql[0]['select_expr'])) {
  1255.                         foreach ($analyzed_sql[0]['select_expr'] AS $select_expr_position => $select_expr) {
  1256.                             $alias = $analyzed_sql[0]['select_expr'][$select_expr_position]['alias'];
  1257.                             if (isset($alias) && strlen($alias)) {
  1258.                                 $true_column = $analyzed_sql[0]['select_expr'][$select_expr_position]['column'];
  1259.                                 if ($alias == $meta->name) {
  1260.                                     $meta->name = $true_column;
  1261.                                 } // end if
  1262.                             } // end if
  1263.                         } // end while
  1264.                     }
  1265.  
  1266.                     if (isset($map[$meta->name])) {
  1267.                         // Field to display from the foreign table?
  1268.                         if (isset($map[$meta->name][2]) && strlen($map[$meta->name][2])) {
  1269.                             $dispsql     = 'SELECT ' . PMA_backquote($map[$meta->name][2])
  1270.                                          . ' FROM ' . PMA_backquote($map[$meta->name][3]) . '.' . PMA_backquote($map[$meta->name][0])
  1271.                                          . ' WHERE ' . PMA_backquote($map[$meta->name][1])
  1272.                                          . ' = ' . $row[$i];
  1273.                             $dispresult  = PMA_DBI_try_query($dispsql, null, PMA_DBI_QUERY_STORE);
  1274.                             if ($dispresult && PMA_DBI_num_rows($dispresult) > 0) {
  1275.                                 list($dispval) = PMA_DBI_fetch_row($dispresult, 0);
  1276.                             } else {
  1277.                                 $dispval = $GLOBALS['strLinkNotFound'];
  1278.                             }
  1279.                             @PMA_DBI_free_result($dispresult);
  1280.                         } else {
  1281.                             $dispval     = '';
  1282.                         } // end if... else...
  1283.  
  1284.                         if (isset($GLOBALS['printview']) && $GLOBALS['printview'] == '1') {
  1285.                             $vertical_display['data'][$row_no][$i] .= ($transform_function != $default_function ? $transform_function($row[$i], $transform_options, $meta) : $transform_function($row[$i], array(), $meta)) . ' <code>[->' . $dispval . ']</code>';
  1286.                         } else {
  1287.                             $title = (!empty($dispval))? ' title="' . htmlspecialchars($dispval) . '"' : '';
  1288.  
  1289.                             $vertical_display['data'][$row_no][$i] .= '<a href="sql.php?'
  1290.                                                                    .  PMA_generate_common_url($map[$meta->name][3], $map[$meta->name][0])
  1291.                                                                    .  '&pos=0'
  1292.                                                                    .  '&sql_query=' . urlencode('SELECT * FROM ' . PMA_backquote($map[$meta->name][0]) . ' WHERE ' . PMA_backquote($map[$meta->name][1]) . ' = ' . $row[$i]) . '"' . $title . '>'
  1293.                                                                    .  ($transform_function != $default_function ? $transform_function($row[$i], $transform_options, $meta) : $transform_function($row[$i], array(), $meta)) . '</a>';
  1294.                         }
  1295.                     } else {
  1296.                         $vertical_display['data'][$row_no][$i] .= ($transform_function != $default_function ? $transform_function($row[$i], $transform_options, $meta) : $transform_function($row[$i], array(), $meta));
  1297.                     }
  1298.                     $vertical_display['data'][$row_no][$i]     .= '</td>' . "\n";
  1299.                 } else {
  1300.                     $vertical_display['data'][$row_no][$i]     = '    <td align="right"' . $mouse_events . ' class="' . $class . ' nowrap' . ($condition_field ? ' condition' : '') . '"> </td>' . "\n";
  1301.                 }
  1302.  
  1303.             //  b l o b
  1304.  
  1305.             } elseif ($GLOBALS['cfg']['ShowBlob'] == false && stristr($meta->type, 'BLOB')) {
  1306.                 // loic1 : PMA_mysql_fetch_fields returns BLOB in place of
  1307.                 // TEXT fields type, however TEXT fields must be displayed
  1308.                 // even if $GLOBALS['cfg']['ShowBlob'] is false -> get the true type
  1309.                 // of the fields.
  1310.                 $field_flags = PMA_DBI_field_flags($dt_result, $i);
  1311.                 if (stristr($field_flags, 'BINARY')) {
  1312.                     $blobtext = '[BLOB';
  1313.                     if (!isset($row[$i]) || is_null($row[$i])) {
  1314.                         $blobtext .= ' - NULL';
  1315.                         $blob_size = 0;
  1316.                     } elseif (isset($row[$i])) {
  1317.                         $blob_size = strlen($row[$i]);
  1318.                         $display_blob_size = PMA_formatByteDown($blob_size, 3, 1);
  1319.                         $blobtext .= ' - '. $display_blob_size[0] . ' ' . $display_blob_size[1];
  1320.                         unset($display_blob_size);
  1321.                     }
  1322.  
  1323.                     $blobtext .= ']';
  1324.                     if (strpos($transform_function, 'octetstream')) {
  1325.                         $blobtext = $row[$i];
  1326.                     }
  1327.                     if ($blob_size > 0) {
  1328.                         $blobtext = ($default_function != $transform_function ? $transform_function($blobtext, $transform_options, $meta) : $default_function($blobtext, array(), $meta));
  1329.                     }
  1330.                     unset($blob_size);
  1331.  
  1332.                     $vertical_display['data'][$row_no][$i]      = '    <td align="left"' . $mouse_events . ' class="' . $class . ($condition_field ? ' condition' : '') . '">' . $blobtext . '</td>';
  1333.                 } else {
  1334.                     if (!isset($row[$i]) || is_null($row[$i])) {
  1335.                         $vertical_display['data'][$row_no][$i] = '    <td' . $mouse_events . ' class="' . $class . ($condition_field ? ' condition' : '') . '"><i>NULL</i></td>' . "\n";
  1336.                     } elseif ($row[$i] != '') {
  1337.                         // garvin: if a transform function for blob is set, none of these replacements will be made
  1338.                         if (PMA_strlen($row[$i]) > $GLOBALS['cfg']['LimitChars'] && ! $_SESSION['userconf']['dontlimitchars']) {
  1339.                             $row[$i] = PMA_substr($row[$i], 0, $GLOBALS['cfg']['LimitChars']) . '...';
  1340.                         }
  1341.                         // loic1: displays all space characters, 4 space
  1342.                         // characters for tabulations and <cr>/<lf>
  1343.                         $row[$i]     = ($default_function != $transform_function ? $transform_function($row[$i], $transform_options, $meta) : $default_function($row[$i], array(), $meta));
  1344.  
  1345.                         $vertical_display['data'][$row_no][$i] = '    <td' . $mouse_events . ' class="' . $class . ($condition_field ? ' condition' : '') . '">' . $row[$i] . '</td>' . "\n";
  1346.                     } else {
  1347.                         $vertical_display['data'][$row_no][$i] = '    <td' . $mouse_events . ' class="' . $class . ($condition_field ? ' condition' : '') . '"> </td>' . "\n";
  1348.                     }
  1349.                 }
  1350.             } else {
  1351.                 if (!isset($row[$i]) || is_null($row[$i])) {
  1352.                     $vertical_display['data'][$row_no][$i]     = '    <td' . $mouse_events . ' class="' . $class . ($condition_field ? ' condition' : '') . '"><i>NULL</i></td>' . "\n";
  1353.                 } elseif ($row[$i] != '') {
  1354.                     // loic1: support blanks in the key
  1355.                     $relation_id = $row[$i];
  1356.  
  1357.                     // nijel: Cut all fields to $GLOBALS['cfg']['LimitChars']
  1358.                     // lem9: (unless it's a link-type transformation)
  1359.                     if (PMA_strlen($row[$i]) > $GLOBALS['cfg']['LimitChars'] && ! $_SESSION['userconf']['dontlimitchars'] && !strpos($transform_function, 'link') === true) {
  1360.                         $row[$i] = PMA_substr($row[$i], 0, $GLOBALS['cfg']['LimitChars']) . '...';
  1361.                     }
  1362.  
  1363.                     // loic1: displays special characters from binaries
  1364.                     $field_flags = PMA_DBI_field_flags($dt_result, $i);
  1365.                     if (isset($meta->_type) && $meta->_type === MYSQLI_TYPE_BIT) {
  1366.                         $db_value = $row[$i];
  1367.                         $row[$i]  = '';
  1368.                         for ($j = 0; $j < ceil($meta->length / 8); $j++) {
  1369.                             $row[$i] .= sprintf('%08d', decbin(ord(substr($db_value, $j, 1))));
  1370.                         }
  1371.                         $row[$i]     = substr($row[$i], -$meta->length);
  1372.                     } elseif (stristr($field_flags, 'BINARY')) {
  1373.                         $row[$i]     = str_replace("\x00", '\0', $row[$i]);
  1374.                         $row[$i]     = str_replace("\x08", '\b', $row[$i]);
  1375.                         $row[$i]     = str_replace("\x0a", '\n', $row[$i]);
  1376.                         $row[$i]     = str_replace("\x0d", '\r', $row[$i]);
  1377.                         $row[$i]     = str_replace("\x1a", '\Z', $row[$i]);
  1378.                         $row[$i]     = ($default_function != $transform_function ? $transform_function($row[$i], $transform_options, $meta) : $default_function($row[$i], array(), $meta));
  1379.                     }
  1380.                     // loic1: displays all space characters, 4 space
  1381.                     // characters for tabulations and <cr>/<lf>
  1382.                     else {
  1383.                         $row[$i]     = ($default_function != $transform_function ? $transform_function($row[$i], $transform_options, $meta) : $default_function($row[$i], array(), $meta));
  1384.                     }
  1385.  
  1386.                     // garvin: transform functions may enable no-wrapping:
  1387.                     $function_nowrap = $transform_function . '_nowrap';
  1388.                     $bool_nowrap = (($default_function != $transform_function && function_exists($function_nowrap)) ? $function_nowrap($transform_options) : false);
  1389.  
  1390.                     // loic1: do not wrap if date field type
  1391.                     $nowrap = ((preg_match('@DATE|TIME@i', $meta->type) || $bool_nowrap) ? ' nowrap' : '');
  1392.                     $vertical_display['data'][$row_no][$i]     = '    <td' . $mouse_events . ' class="' . $class . $nowrap . ($condition_field ? ' condition' : '') . '">';
  1393.  
  1394.                     if (isset($analyzed_sql[0]['select_expr']) && is_array($analyzed_sql[0]['select_expr'])) {
  1395.                         foreach ($analyzed_sql[0]['select_expr'] AS $select_expr_position => $select_expr) {
  1396.                             $alias = $analyzed_sql[0]['select_expr'][$select_expr_position]['alias'];
  1397.                             if (isset($alias) && strlen($alias)) {
  1398.                                 $true_column = $analyzed_sql[0]['select_expr'][$select_expr_position]['column'];
  1399.                                 if ($alias == $meta->name) {
  1400.                                     $meta->name = $true_column;
  1401.                                 } // end if
  1402.                             } // end if
  1403.                         } // end while
  1404.                     }
  1405.  
  1406.                     if (isset($map[$meta->name])) {
  1407.                         // Field to display from the foreign table?
  1408.                         if (isset($map[$meta->name][2]) && strlen($map[$meta->name][2])) {
  1409.                             $dispsql     = 'SELECT ' . PMA_backquote($map[$meta->name][2])
  1410.                                          . ' FROM ' . PMA_backquote($map[$meta->name][3]) . '.' . PMA_backquote($map[$meta->name][0])
  1411.                                          . ' WHERE ' . PMA_backquote($map[$meta->name][1])
  1412.                                          . ' = \'' . PMA_sqlAddslashes($row[$i]) . '\'';
  1413.                             $dispresult  = PMA_DBI_try_query($dispsql, null, PMA_DBI_QUERY_STORE);
  1414.                             if ($dispresult && PMA_DBI_num_rows($dispresult) > 0) {
  1415.                                 list($dispval) = PMA_DBI_fetch_row($dispresult);
  1416.                                 @PMA_DBI_free_result($dispresult);
  1417.                             } else {
  1418.                                 $dispval = $GLOBALS['strLinkNotFound'];
  1419.                             }
  1420.                         } else {
  1421.                             $dispval = '';
  1422.                         }
  1423.                         $title = (!empty($dispval))? ' title="' . htmlspecialchars($dispval) . '"' : '';
  1424.  
  1425.                         $vertical_display['data'][$row_no][$i] .= '<a href="sql.php?'
  1426.                                                                .  PMA_generate_common_url($map[$meta->name][3], $map[$meta->name][0])
  1427.                                                                .  '&pos=0'
  1428.                                                                .  '&sql_query=' . urlencode('SELECT * FROM ' . PMA_backquote($map[$meta->name][0]) . ' WHERE ' . PMA_backquote($map[$meta->name][1]) . ' = \'' . PMA_sqlAddslashes($relation_id) . '\'') . '"' . $title . '>'
  1429.                                                                .  $row[$i] . '</a>';
  1430.                     } else {
  1431.                         $vertical_display['data'][$row_no][$i] .= $row[$i];
  1432.                     }
  1433.                     $vertical_display['data'][$row_no][$i]     .= '</td>' . "\n";
  1434.                 } else {
  1435.                     $vertical_display['data'][$row_no][$i]     = '    <td' . $mouse_events . ' class="' . $class . ($condition_field ? ' condition' : '') . '"> </td>' . "\n";
  1436.                 }
  1437.             }
  1438.  
  1439.             // lem9: output stored cell
  1440.             if ($_SESSION['userconf']['disp_direction'] == 'horizontal'
  1441.              || $_SESSION['userconf']['disp_direction'] == 'horizontalflipped') {
  1442.                 echo $vertical_display['data'][$row_no][$i];
  1443.             }
  1444.  
  1445.             if (isset($vertical_display['rowdata'][$i][$row_no])) {
  1446.                 $vertical_display['rowdata'][$i][$row_no] .= $vertical_display['data'][$row_no][$i];
  1447.             } else {
  1448.                 $vertical_display['rowdata'][$i][$row_no] = $vertical_display['data'][$row_no][$i];
  1449.             }
  1450.         } // end for (2)
  1451.  
  1452.         // 3. Displays the modify/delete links on the right if required
  1453.         if ($GLOBALS['cfg']['ModifyDeleteAtRight']
  1454.          && ($_SESSION['userconf']['disp_direction'] == 'horizontal'
  1455.           || $_SESSION['userconf']['disp_direction'] == 'horizontalflipped')) {
  1456.                 $doWriteModifyAt = 'right';
  1457.                 require './libraries/display_tbl_links.lib.php';
  1458.         } // end if (3)
  1459.  
  1460.         if ($_SESSION['userconf']['disp_direction'] == 'horizontal'
  1461.          || $_SESSION['userconf']['disp_direction'] == 'horizontalflipped') {
  1462.             ?>
  1463. </tr>
  1464.             <?php
  1465.         } // end if
  1466.  
  1467.         // 4. Gather links of del_urls and edit_urls in an array for later
  1468.         //    output
  1469.         if (!isset($vertical_display['edit'][$row_no])) {
  1470.             $vertical_display['edit'][$row_no]       = '';
  1471.             $vertical_display['delete'][$row_no]     = '';
  1472.             $vertical_display['row_delete'][$row_no] = '';
  1473.         }
  1474.  
  1475.         $column_style_vertical = '';
  1476.         if ($GLOBALS['cfg']['BrowsePointerEnable'] == true) {
  1477.             $column_style_vertical .= ' onmouseover="setVerticalPointer(this, ' . $row_no . ', \'over\', \'odd\', \'even\', \'hover\', \'marked\');"'
  1478.                          . ' onmouseout="setVerticalPointer(this, ' . $row_no . ', \'out\', \'odd\', \'even\', \'hover\', \'marked\');"';
  1479.         }
  1480.         $column_marker_vertical = '';
  1481.         if ($GLOBALS['cfg']['BrowseMarkerEnable'] == true) {
  1482.             $column_marker_vertical .= 'setVerticalPointer(this, ' . $row_no . ', \'click\', \'odd\', \'even\', \'hover\', \'marked\');';
  1483.         }
  1484.  
  1485.         if (!empty($del_url) && $is_display['del_lnk'] != 'kp') {
  1486.             $vertical_display['row_delete'][$row_no] .= '    <td align="center" class="' . $class . '" ' . $column_style_vertical . '>' . "\n"
  1487.                                                      .  '        <input type="checkbox" id="id_rows_to_delete' . $row_no . '[%_PMA_CHECKBOX_DIR_%]" name="rows_to_delete[' . $unique_condition . ']"'
  1488.                                                      .  ' onclick="' . $column_marker_vertical . 'copyCheckboxesRange(\'rowsDeleteForm\', \'id_rows_to_delete' . $row_no . '\',\'[%_PMA_CHECKBOX_DIR_%]\');"'
  1489.                                                      .  ' value="' . $del_query . '" ' . (isset($GLOBALS['checkall']) ? 'checked="checked"' : '') . ' />' . "\n"
  1490.                                                      .  '    </td>' . "\n";
  1491.         } else {
  1492.             unset($vertical_display['row_delete'][$row_no]);
  1493.         }
  1494.  
  1495.         if (isset($edit_url)) {
  1496.             $vertical_display['edit'][$row_no]   .= '    <td align="center" class="' . $class . '" ' . $column_style_vertical . '>' . "\n"
  1497.                                                  . PMA_linkOrButton($edit_url, $edit_str, array(), false)
  1498.                                                  . $bookmark_go
  1499.                                                  .  '    </td>' . "\n";
  1500.         } else {
  1501.             unset($vertical_display['edit'][$row_no]);
  1502.         }
  1503.  
  1504.         if (isset($del_url)) {
  1505.             $vertical_display['delete'][$row_no] .= '    <td align="center" class="' . $class . '" ' . $column_style_vertical . '>' . "\n"
  1506.                                                  . PMA_linkOrButton($del_url, $del_str, (isset($js_conf) ? $js_conf : ''), false)
  1507.                                                  .  '    </td>' . "\n";
  1508.         } else {
  1509.             unset($vertical_display['delete'][$row_no]);
  1510.         }
  1511.  
  1512.         echo (($_SESSION['userconf']['disp_direction'] == 'horizontal' || $_SESSION['userconf']['disp_direction'] == 'horizontalflipped') ? "\n" : '');
  1513.         $row_no++;
  1514.     } // end while
  1515.  
  1516.     if (isset($url_query)) {
  1517.         $GLOBALS['url_query'] = $url_query;
  1518.     }
  1519.  
  1520.     return true;
  1521. } // end of the 'PMA_displayTableBody()' function
  1522.  
  1523.  
  1524. /**
  1525.  * Do display the result table with the vertical direction mode.
  1526.  * Credits for this feature goes to Garvin Hicking <hicking@faktor-e.de>.
  1527.  *
  1528.  * @return  boolean  always true
  1529.  *
  1530.  * @uses    $_SESSION['userconf']['repeat_cells']
  1531.  * @global  array    $vertical_display the information to display
  1532.  *
  1533.  * @access  private
  1534.  *
  1535.  * @see     PMA_displayTable()
  1536.  */
  1537. function PMA_displayVerticalTable()
  1538. {
  1539.     global $vertical_display;
  1540.  
  1541.     // Displays "multi row delete" link at top if required
  1542.     if ($GLOBALS['cfg']['ModifyDeleteAtLeft'] && is_array($vertical_display['row_delete']) && (count($vertical_display['row_delete']) > 0 || !empty($vertical_display['textbtn']))) {
  1543.         echo '<tr>' . "\n";
  1544.         echo $vertical_display['textbtn'];
  1545.         $foo_counter = 0;
  1546.         foreach ($vertical_display['row_delete'] as $val) {
  1547.             if (($foo_counter != 0) && ($_SESSION['userconf']['repeat_cells'] != 0) && !($foo_counter % $_SESSION['userconf']['repeat_cells'])) {
  1548.                 echo '<th> </th>' . "\n";
  1549.             }
  1550.  
  1551.             echo str_replace('[%_PMA_CHECKBOX_DIR_%]', '', $val);
  1552.             $foo_counter++;
  1553.         } // end while
  1554.         echo '</tr>' . "\n";
  1555.     } // end if
  1556.  
  1557.     // Displays "edit" link at top if required
  1558.     if ($GLOBALS['cfg']['ModifyDeleteAtLeft'] && is_array($vertical_display['edit']) && (count($vertical_display['edit']) > 0 || !empty($vertical_display['textbtn']))) {
  1559.         echo '<tr>' . "\n";
  1560.         if (!is_array($vertical_display['row_delete'])) {
  1561.             echo $vertical_display['textbtn'];
  1562.         }
  1563.         $foo_counter = 0;
  1564.         foreach ($vertical_display['edit'] as $val) {
  1565.             if (($foo_counter != 0) && ($_SESSION['userconf']['repeat_cells'] != 0) && !($foo_counter % $_SESSION['userconf']['repeat_cells'])) {
  1566.                 echo '    <th> </th>' . "\n";
  1567.             }
  1568.  
  1569.             echo $val;
  1570.             $foo_counter++;
  1571.         } // end while
  1572.         echo '</tr>' . "\n";
  1573.     } // end if
  1574.  
  1575.     // Displays "delete" link at top if required
  1576.     if ($GLOBALS['cfg']['ModifyDeleteAtLeft'] && is_array($vertical_display['delete']) && (count($vertical_display['delete']) > 0 || !empty($vertical_display['textbtn']))) {
  1577.         echo '<tr>' . "\n";
  1578.         if (!is_array($vertical_display['edit']) && !is_array($vertical_display['row_delete'])) {
  1579.             echo $vertical_display['textbtn'];
  1580.         }
  1581.         $foo_counter = 0;
  1582.         foreach ($vertical_display['delete'] as $val) {
  1583.             if (($foo_counter != 0) && ($_SESSION['userconf']['repeat_cells'] != 0) && !($foo_counter % $_SESSION['userconf']['repeat_cells'])) {
  1584.                 echo '<th> </th>' . "\n";
  1585.             }
  1586.  
  1587.             echo $val;
  1588.             $foo_counter++;
  1589.         } // end while
  1590.         echo '</tr>' . "\n";
  1591.     } // end if
  1592.  
  1593.     // Displays data
  1594.     foreach ($vertical_display['desc'] AS $key => $val) {
  1595.  
  1596.         echo '<tr>' . "\n";
  1597.         echo $val;
  1598.  
  1599.         $foo_counter = 0;
  1600.         foreach ($vertical_display['rowdata'][$key] as $subval) {
  1601.             if (($foo_counter != 0) && ($_SESSION['userconf']['repeat_cells'] != 0) and !($foo_counter % $_SESSION['userconf']['repeat_cells'])) {
  1602.                 echo $val;
  1603.             }
  1604.  
  1605.             echo $subval;
  1606.             $foo_counter++;
  1607.         } // end while
  1608.  
  1609.         echo '</tr>' . "\n";
  1610.     } // end while
  1611.  
  1612.     // Displays "multi row delete" link at bottom if required
  1613.     if ($GLOBALS['cfg']['ModifyDeleteAtRight'] && is_array($vertical_display['row_delete']) && (count($vertical_display['row_delete']) > 0 || !empty($vertical_display['textbtn']))) {
  1614.         echo '<tr>' . "\n";
  1615.         echo $vertical_display['textbtn'];
  1616.         $foo_counter = 0;
  1617.         foreach ($vertical_display['row_delete'] as $val) {
  1618.             if (($foo_counter != 0) && ($_SESSION['userconf']['repeat_cells'] != 0) && !($foo_counter % $_SESSION['userconf']['repeat_cells'])) {
  1619.                 echo '<th> </th>' . "\n";
  1620.             }
  1621.  
  1622.             echo str_replace('[%_PMA_CHECKBOX_DIR_%]', 'r', $val);
  1623.             $foo_counter++;
  1624.         } // end while
  1625.         echo '</tr>' . "\n";
  1626.     } // end if
  1627.  
  1628.     // Displays "edit" link at bottom if required
  1629.     if ($GLOBALS['cfg']['ModifyDeleteAtRight'] && is_array($vertical_display['edit']) && (count($vertical_display['edit']) > 0 || !empty($vertical_display['textbtn']))) {
  1630.         echo '<tr>' . "\n";
  1631.         if (!is_array($vertical_display['row_delete'])) {
  1632.             echo $vertical_display['textbtn'];
  1633.         }
  1634.         $foo_counter = 0;
  1635.         foreach ($vertical_display['edit'] as $val) {
  1636.             if (($foo_counter != 0) && ($_SESSION['userconf']['repeat_cells'] != 0) && !($foo_counter % $_SESSION['userconf']['repeat_cells'])) {
  1637.                 echo '<th> </th>' . "\n";
  1638.             }
  1639.  
  1640.             echo $val;
  1641.             $foo_counter++;
  1642.         } // end while
  1643.         echo '</tr>' . "\n";
  1644.     } // end if
  1645.  
  1646.     // Displays "delete" link at bottom if required
  1647.     if ($GLOBALS['cfg']['ModifyDeleteAtRight'] && is_array($vertical_display['delete']) && (count($vertical_display['delete']) > 0 || !empty($vertical_display['textbtn']))) {
  1648.         echo '<tr>' . "\n";
  1649.         if (!is_array($vertical_display['edit']) && !is_array($vertical_display['row_delete'])) {
  1650.             echo $vertical_display['textbtn'];
  1651.         }
  1652.         $foo_counter = 0;
  1653.         foreach ($vertical_display['delete'] as $val) {
  1654.             if (($foo_counter != 0) && ($_SESSION['userconf']['repeat_cells'] != 0) && !($foo_counter % $_SESSION['userconf']['repeat_cells'])) {
  1655.                 echo '<th> </th>' . "\n";
  1656.             }
  1657.  
  1658.             echo $val;
  1659.             $foo_counter++;
  1660.         } // end while
  1661.         echo '</tr>' . "\n";
  1662.     }
  1663.  
  1664.     return true;
  1665. } // end of the 'PMA_displayVerticalTable' function
  1666.  
  1667. /**
  1668.  *
  1669.  * @uses    $_SESSION['userconf']['disp_direction']
  1670.  * @uses    $_REQUEST['disp_direction']
  1671.  * @uses    $GLOBALS['cfg']['DefaultDisplay']
  1672.  * @uses    $_SESSION['userconf']['repeat_cells']
  1673.  * @uses    $_REQUEST['repeat_cells']
  1674.  * @uses    $GLOBALS['cfg']['RepeatCells']
  1675.  * @uses    $_SESSION['userconf']['max_rows']
  1676.  * @uses    $_REQUEST['session_max_rows']
  1677.  * @uses    $GLOBALS['cfg']['MaxRows']
  1678.  * @uses    $_SESSION['userconf']['pos']
  1679.  * @uses    $_REQUEST['pos']
  1680.  * @uses    $_SESSION['userconf']['dontlimitchars']
  1681.  * @uses    $_REQUEST['dontlimitchars']
  1682.  * @uses    PMA_isValid()
  1683.  * @uses    $GLOBALS['sql_query']
  1684.  * @todo    make maximum remembered queries configurable
  1685.  * @todo    move/split into SQL class!?
  1686.  * @todo    currently this is called twice unnecessary
  1687.  * @todo    ignore LIMIT and ORDER in query!?
  1688.  */
  1689. function PMA_displayTable_checkConfigParams()
  1690. {
  1691.     $sql_key = md5($GLOBALS['sql_query']);
  1692.  
  1693.     $_SESSION['userconf']['query'][$sql_key]['sql'] = $GLOBALS['sql_query'];
  1694.  
  1695.     if (PMA_isValid($_REQUEST['disp_direction'], array('horizontal', 'vertical', 'horizontalflipped'))) {
  1696.         $_SESSION['userconf']['query'][$sql_key]['disp_direction'] = $_REQUEST['disp_direction'];
  1697.         unset($_REQUEST['disp_direction']);
  1698.     } elseif (empty($_SESSION['userconf']['query'][$sql_key]['disp_direction'])) {
  1699.         $_SESSION['userconf']['query'][$sql_key]['disp_direction'] = $GLOBALS['cfg']['DefaultDisplay'];
  1700.     }
  1701.  
  1702.     if (PMA_isValid($_REQUEST['repeat_cells'], 'numeric')) {
  1703.         $_SESSION['userconf']['query'][$sql_key]['repeat_cells'] = $_REQUEST['repeat_cells'];
  1704.         unset($_REQUEST['repeat_cells']);
  1705.     } elseif (empty($_SESSION['userconf']['query'][$sql_key]['repeat_cells'])) {
  1706.         $_SESSION['userconf']['query'][$sql_key]['repeat_cells'] = $GLOBALS['cfg']['RepeatCells'];
  1707.     }
  1708.  
  1709.     if (PMA_isValid($_REQUEST['session_max_rows'], 'numeric') || $_REQUEST['session_max_rows'] == 'all') {
  1710.         $_SESSION['userconf']['query'][$sql_key]['max_rows'] = $_REQUEST['session_max_rows'];
  1711.         unset($_REQUEST['session_max_rows']);
  1712.     } elseif (empty($_SESSION['userconf']['query'][$sql_key]['max_rows'])) {
  1713.         $_SESSION['userconf']['query'][$sql_key]['max_rows'] = $GLOBALS['cfg']['MaxRows'];
  1714.     }
  1715.  
  1716.     if (PMA_isValid($_REQUEST['pos'], 'numeric')) {
  1717.         $_SESSION['userconf']['query'][$sql_key]['pos'] = $_REQUEST['pos'];
  1718.         unset($_REQUEST['pos']);
  1719.     } elseif (empty($_SESSION['userconf']['query'][$sql_key]['pos'])) {
  1720.         $_SESSION['userconf']['query'][$sql_key]['pos'] = 0;
  1721.     }
  1722.  
  1723.     if (PMA_isValid($_REQUEST['dontlimitchars'], array('0', '1'))) {
  1724.         $_SESSION['userconf']['query'][$sql_key]['dontlimitchars'] = (int) $_REQUEST['dontlimitchars'];
  1725.         unset($_REQUEST['dontlimitchars']);
  1726.     } elseif (empty($_SESSION['userconf']['query'][$sql_key]['dontlimitchars'])) {
  1727.         $_SESSION['userconf']['query'][$sql_key]['dontlimitchars'] = 0;
  1728.     }
  1729.  
  1730.     // move current query to the last position, to be removed last
  1731.     // so only least executed query will be removed if maximum remembered queries
  1732.     // limit is reached
  1733.     $tmp = $_SESSION['userconf']['query'][$sql_key];
  1734.     unset($_SESSION['userconf']['query'][$sql_key]);
  1735.     $_SESSION['userconf']['query'][$sql_key] = $tmp;
  1736.  
  1737.     // do not exceed a maximum number of queries to remember
  1738.     if (count($_SESSION['userconf']['query']) > 10) {
  1739.         array_shift($_SESSION['userconf']['query']);
  1740.         //echo 'deleting one element ...';
  1741.     }
  1742.  
  1743.     // populate query configuration
  1744.     $_SESSION['userconf']['dontlimitchars'] = $_SESSION['userconf']['query'][$sql_key]['dontlimitchars'];
  1745.     $_SESSION['userconf']['pos'] = $_SESSION['userconf']['query'][$sql_key]['pos'];
  1746.     $_SESSION['userconf']['max_rows'] = $_SESSION['userconf']['query'][$sql_key]['max_rows'];
  1747.     $_SESSION['userconf']['repeat_cells'] = $_SESSION['userconf']['query'][$sql_key]['repeat_cells'];
  1748.     $_SESSION['userconf']['disp_direction'] = $_SESSION['userconf']['query'][$sql_key]['disp_direction'];
  1749.  
  1750.     /*
  1751.      * debugging
  1752.     echo '<pre>';
  1753.     var_dump($_SESSION['userconf']);
  1754.     echo '</pre>';
  1755.      */
  1756. }
  1757.  
  1758. /**
  1759.  * Displays a table of results returned by a SQL query.
  1760.  * This function is called by the "sql.php" script.
  1761.  *
  1762.  * @param   integer the link id associated to the query which results have
  1763.  *                  to be displayed
  1764.  * @param   array   the display mode
  1765.  * @param   array   the analyzed query
  1766.  *
  1767.  * @uses    $_SESSION['userconf']['pos']
  1768.  * @global  string   $db                the database name
  1769.  * @global  string   $table             the table name
  1770.  * @global  string   $goto              the URL to go back in case of errors
  1771.  * @global  string   $sql_query         the current SQL query
  1772.  * @global  integer  $num_rows          the total number of rows returned by the
  1773.  *                                      SQL query
  1774.  * @global  integer  $unlim_num_rows    the total number of rows returned by the
  1775.  *                                      SQL query without any programmatically
  1776.  *                                      appended "LIMIT" clause
  1777.  * @global  array    $fields_meta       the list of fields properties
  1778.  * @global  integer  $fields_cnt        the total number of fields returned by
  1779.  *                                      the SQL query
  1780.  * @global  array    $vertical_display  informations used with vertical display
  1781.  *                                      mode
  1782.  * @global  array    $highlight_columns column names to highlight
  1783.  * @global  array    $cfgRelation       the relation settings
  1784.  *
  1785.  * @access  private
  1786.  *
  1787.  * @see     PMA_showMessage(), PMA_setDisplayMode(),
  1788.  *          PMA_displayTableNavigation(), PMA_displayTableHeaders(),
  1789.  *          PMA_displayTableBody(), PMA_displayResultsOperations()
  1790.  */
  1791. function PMA_displayTable(&$dt_result, &$the_disp_mode, $analyzed_sql)
  1792. {
  1793.     global $db, $table, $goto;
  1794.     global $sql_query, $num_rows, $unlim_num_rows, $fields_meta, $fields_cnt;
  1795.     global $vertical_display, $highlight_columns;
  1796.     global $cfgRelation;
  1797.     global $showtable;
  1798.  
  1799.     PMA_displayTable_checkConfigParams();
  1800.  
  1801.     /**
  1802.      * @todo move this to a central place
  1803.      * @todo for other future table types
  1804.      */
  1805.     $is_innodb = (isset($showtable['Type']) && $showtable['Type'] == 'InnoDB');
  1806.  
  1807.     if ($is_innodb
  1808.      && ! isset($analyzed_sql[0]['queryflags']['union'])
  1809.      && ! isset($analyzed_sql[0]['table_ref'][1]['table_name'])
  1810.      && (empty($analyzed_sql[0]['where_clause'])
  1811.       || $analyzed_sql[0]['where_clause'] == '1 ')) {
  1812.         // "j u s t   b r o w s i n g"
  1813.         $pre_count = '~';
  1814.         $after_count = '[sup]1[/sup]';
  1815.     } else {
  1816.         $pre_count = '';
  1817.         $after_count = '';
  1818.     }
  1819.  
  1820.     // 1. ----- Prepares the work -----
  1821.  
  1822.     // 1.1 Gets the informations about which functionalities should be
  1823.     //     displayed
  1824.     $total      = '';
  1825.     $is_display = PMA_setDisplayMode($the_disp_mode, $total);
  1826.  
  1827.     // 1.2 Defines offsets for the next and previous pages
  1828.     if ($is_display['nav_bar'] == '1') {
  1829.         if ($_SESSION['userconf']['max_rows'] == 'all') {
  1830.             $pos_next     = 0;
  1831.             $pos_prev     = 0;
  1832.         } else {
  1833.             $pos_next     = $_SESSION['userconf']['pos'] + $_SESSION['userconf']['max_rows'];
  1834.             $pos_prev     = $_SESSION['userconf']['pos'] - $_SESSION['userconf']['max_rows'];
  1835.             if ($pos_prev < 0) {
  1836.                 $pos_prev = 0;
  1837.             }
  1838.         }
  1839.     } // end if
  1840.  
  1841.     // 1.3 URL-encodes the query to use in input form fields
  1842.     $encoded_sql_query = urlencode($sql_query);
  1843.  
  1844.     // 2. ----- Displays the top of the page -----
  1845.  
  1846.     // 2.1 Displays a messages with position informations
  1847.     if ($is_display['nav_bar'] == '1' && isset($pos_next)) {
  1848.         if (isset($unlim_num_rows) && $unlim_num_rows != $total) {
  1849.             $selectstring = ', ' . $unlim_num_rows . ' ' . $GLOBALS['strSelectNumRows'];
  1850.         } else {
  1851.             $selectstring = '';
  1852.         }
  1853.         $last_shown_rec = ($_SESSION['userconf']['max_rows'] == 'all' || $pos_next > $total)
  1854.                         ? $total - 1
  1855.                         : $pos_next - 1;
  1856.         PMA_showMessage($GLOBALS['strShowingRecords'] . ' ' . $_SESSION['userconf']['pos'] . ' - ' . $last_shown_rec . ' (' . $pre_count . PMA_formatNumber($total, 0) . $after_count . ' ' . $GLOBALS['strTotal'] . $selectstring . ', ' . sprintf($GLOBALS['strQueryTime'], $GLOBALS['querytime']) . ')');
  1857.  
  1858.         if (PMA_Table::isView($db, $table) && $total ==  $GLOBALS['cfg']['MaxExactCount']) {
  1859.             echo '<div class="notice">' . "\n";
  1860.             echo PMA_sanitize(sprintf($GLOBALS['strViewMaxExactCount'], PMA_formatNumber($GLOBALS['cfg']['MaxExactCount'], 0), '[a@./Documentation.html#cfg_MaxExactCount@_blank]', '[/a]')) . "\n";
  1861.             echo '</div>' . "\n";
  1862.         }
  1863.         if ($pre_count) {
  1864.             echo '<div class="notice">' . "\n";
  1865.             echo '<sup>1</sup>' . PMA_sanitize($GLOBALS['strApproximateCount']) . "\n";
  1866.             echo '</div>' . "\n";
  1867.         }
  1868.  
  1869.     } elseif (!isset($GLOBALS['printview']) || $GLOBALS['printview'] != '1') {
  1870.         PMA_showMessage($GLOBALS['strSQLQuery']);
  1871.     }
  1872.  
  1873.     // 2.3 Displays the navigation bars
  1874.     if (! strlen($table)) {
  1875.         if (isset($analyzed_sql[0]['query_type'])
  1876.            && $analyzed_sql[0]['query_type'] == 'SELECT') {
  1877.             // table does not always contain a real table name,
  1878.             // for example in MySQL 5.0.x, the query SHOW STATUS
  1879.             // returns STATUS as a table name
  1880.             $table = $fields_meta[0]->table;
  1881.         } else {
  1882.             $table = '';
  1883.         }
  1884.     }
  1885.  
  1886.     if ($is_display['nav_bar'] == '1') {
  1887.         PMA_displayTableNavigation($pos_next, $pos_prev, $sql_query);
  1888.         echo "\n";
  1889.     } elseif (!isset($GLOBALS['printview']) || $GLOBALS['printview'] != '1') {
  1890.         echo "\n" . '<br /><br />' . "\n";
  1891.     }
  1892.  
  1893.     // 2b ----- Get field references from Database -----
  1894.     // (see the 'relation' configuration variable)
  1895.     // loic1, 2002-03-02: extended to php3
  1896.  
  1897.     // initialize map
  1898.     $map = array();
  1899.  
  1900.     // find tables
  1901.     $target=array();
  1902.     if (isset($analyzed_sql[0]['table_ref']) && is_array($analyzed_sql[0]['table_ref'])) {
  1903.         foreach ($analyzed_sql[0]['table_ref'] AS $table_ref_position => $table_ref) {
  1904.            $target[] = $analyzed_sql[0]['table_ref'][$table_ref_position]['table_true_name'];
  1905.         }
  1906.     }
  1907.     $tabs    = '(\'' . join('\',\'', $target) . '\')';
  1908.  
  1909.     if ($cfgRelation['displaywork']) {
  1910.         if (! strlen($table)) {
  1911.             $exist_rel = false;
  1912.         } else {
  1913.             $exist_rel = PMA_getForeigners($db, $table, '', 'both');
  1914.             if ($exist_rel) {
  1915.                 foreach ($exist_rel AS $master_field => $rel) {
  1916.                     $display_field = PMA_getDisplayField($rel['foreign_db'], $rel['foreign_table']);
  1917.                     $map[$master_field] = array($rel['foreign_table'],
  1918.                                           $rel['foreign_field'],
  1919.                                           $display_field,
  1920.                                           $rel['foreign_db']);
  1921.                 } // end while
  1922.             } // end if
  1923.         } // end if
  1924.     } // end if
  1925.     // end 2b
  1926.  
  1927.     // 3. ----- Displays the results table -----
  1928.     PMA_displayTableHeaders($is_display, $fields_meta, $fields_cnt, $analyzed_sql);
  1929.     $url_query='';
  1930.     echo '<tbody>' . "\n";
  1931.     PMA_displayTableBody($dt_result, $is_display, $map, $analyzed_sql);
  1932.     // vertical output case
  1933.     if ($_SESSION['userconf']['disp_direction'] == 'vertical') {
  1934.         PMA_displayVerticalTable();
  1935.     } // end if
  1936.     unset($vertical_display);
  1937.     echo '</tbody>' . "\n";
  1938.     ?>
  1939. </table>
  1940.  
  1941.     <?php
  1942.     // 4. ----- Displays the link for multi-fields delete
  1943.  
  1944.     if ($is_display['del_lnk'] == 'dr' && $is_display['del_lnk'] != 'kp') {
  1945.  
  1946.         $delete_text = $is_display['del_lnk'] == 'dr' ? $GLOBALS['strDelete'] : $GLOBALS['strKill'];
  1947.  
  1948.         $uncheckall_url = 'sql.php?'
  1949.                   . PMA_generate_common_url($db, $table)
  1950.                   . '&sql_query=' . urlencode($sql_query)
  1951.                   . '&goto=' . $goto;
  1952.         $checkall_url = $uncheckall_url . '&checkall=1';
  1953.  
  1954.         if ($_SESSION['userconf']['disp_direction'] == 'vertical') {
  1955.             $checkall_params['onclick'] = 'if (setCheckboxes(\'rowsDeleteForm\', true)) return false;';
  1956.             $uncheckall_params['onclick'] = 'if (setCheckboxes(\'rowsDeleteForm\', false)) return false;';
  1957.         } else {
  1958.             $checkall_params['onclick'] = 'if (markAllRows(\'rowsDeleteForm\')) return false;';
  1959.             $uncheckall_params['onclick'] = 'if (unMarkAllRows(\'rowsDeleteForm\')) return false;';
  1960.         }
  1961.         $checkall_link = PMA_linkOrButton($checkall_url, $GLOBALS['strCheckAll'], $checkall_params, false);
  1962.         $uncheckall_link = PMA_linkOrButton($uncheckall_url, $GLOBALS['strUncheckAll'], $uncheckall_params, false);
  1963.         if ($_SESSION['userconf']['disp_direction'] != 'vertical') {
  1964.             echo '<img class="selectallarrow" width="38" height="22"'
  1965.                 .' src="' . $GLOBALS['pmaThemeImage'] . 'arrow_' . $GLOBALS['text_dir'] . '.png' . '"'
  1966.                 .' alt="' . $GLOBALS['strWithChecked'] . '" />';
  1967.         }
  1968.         echo $checkall_link . "\n"
  1969.             .' / ' . "\n"
  1970.             .$uncheckall_link . "\n"
  1971.             .'<i>' . $GLOBALS['strWithChecked'] . '</i>' . "\n";
  1972.  
  1973.         if ($GLOBALS['cfg']['PropertiesIconic']) {
  1974.             PMA_buttonOrImage('submit_mult', 'mult_submit',
  1975.                 'submit_mult_change', $GLOBALS['strChange'], 'b_edit.png');
  1976.             PMA_buttonOrImage('submit_mult', 'mult_submit',
  1977.                 'submit_mult_delete', $delete_text, 'b_drop.png');
  1978.             if ($analyzed_sql[0]['querytype'] == 'SELECT') {
  1979.                 PMA_buttonOrImage('submit_mult', 'mult_submit',
  1980.                     'submit_mult_export', $GLOBALS['strExport'],
  1981.                     'b_tblexport.png');
  1982.             }
  1983.             echo "\n";
  1984.         } else {
  1985.             echo ' <input type="submit" name="submit_mult"'
  1986.                 .' value="' . htmlspecialchars($GLOBALS['strEdit']) . '"'
  1987.                 .' title="' . $GLOBALS['strEdit'] . '" />' . "\n";
  1988.             echo ' <input type="submit" name="submit_mult"'
  1989.                 .' value="' . htmlspecialchars($delete_text) . '"'
  1990.                 .' title="' . $delete_text . '" />' . "\n";
  1991.             if ($analyzed_sql[0]['querytype'] == 'SELECT') {
  1992.                 echo ' <input type="submit" name="submit_mult"'
  1993.                     .' value="' . htmlspecialchars($GLOBALS['strExport']) . '"'
  1994.                     .' title="' . $GLOBALS['strExport'] . '" />' . "\n";
  1995.             }
  1996.         }
  1997.         echo '<input type="hidden" name="sql_query"'
  1998.             .' value="' . htmlspecialchars($sql_query) . '" />' . "\n";
  1999.         echo '<input type="hidden" name="url_query"'
  2000.             .' value="' . $GLOBALS['url_query'] . '" />' . "\n";
  2001.         echo '</form>' . "\n";
  2002.     }
  2003.  
  2004.     // 5. ----- Displays the navigation bar at the bottom if required -----
  2005.  
  2006.     if ($is_display['nav_bar'] == '1') {
  2007.         echo '<br />' . "\n";
  2008.         PMA_displayTableNavigation($pos_next, $pos_prev, $sql_query);
  2009.     } elseif (!isset($GLOBALS['printview']) || $GLOBALS['printview'] != '1') {
  2010.         echo "\n" . '<br /><br />' . "\n";
  2011.     }
  2012.     
  2013.     // 6. ----- Displays "Query results operations"
  2014.     if (!isset($GLOBALS['printview']) || $GLOBALS['printview'] != '1') {
  2015.         PMA_displayResultsOperations($the_disp_mode, $analyzed_sql);
  2016.     }
  2017. } // end of the 'PMA_displayTable()' function
  2018.  
  2019. function default_function($buffer) {
  2020.     $buffer = htmlspecialchars($buffer);
  2021.     $buffer = str_replace("\011", '    ',
  2022.         str_replace('  ', '  ', $buffer));
  2023.     $buffer = preg_replace("@((\015\012)|(\015)|(\012))@", '<br />', $buffer);
  2024.  
  2025.     return $buffer;
  2026. }
  2027.  
  2028. /**
  2029.  * Displays operations that are available on results.
  2030.  *
  2031.  * @param   array   the display mode
  2032.  * @param   array   the analyzed query
  2033.  *
  2034.  * @uses    $_SESSION['userconf']['pos']
  2035.  * @uses    $_SESSION['userconf']['dontlimitchars']
  2036.  * @global  string   $db                the database name
  2037.  * @global  string   $table             the table name
  2038.  * @global  string   $sql_query         the current SQL query
  2039.  * @global  integer  $unlim_num_rows    the total number of rows returned by the
  2040.  *                                      SQL query without any programmatically
  2041.  *                                      appended "LIMIT" clause
  2042.  *
  2043.  * @access  private
  2044.  *
  2045.  * @see     PMA_showMessage(), PMA_setDisplayMode(),
  2046.  *          PMA_displayTableNavigation(), PMA_displayTableHeaders(),
  2047.  *          PMA_displayTableBody(), PMA_displayResultsOperations()
  2048.  */
  2049. function PMA_displayResultsOperations($the_disp_mode, $analyzed_sql) {
  2050.     global $db, $table, $sql_query, $unlim_num_rows;
  2051.  
  2052.     $header_shown = FALSE;
  2053.     $header = '<fieldset><legend>' . $GLOBALS['strQueryResultsOperations'] . '</legend>';
  2054.  
  2055.     if ($the_disp_mode[6] == '1' || $the_disp_mode[9] == '1') {
  2056.         // Displays "printable view" link if required
  2057.         if ($the_disp_mode[9] == '1') {
  2058.  
  2059.             if (!$header_shown) {
  2060.                 echo $header;
  2061.                 $header_shown = TRUE;
  2062.             }
  2063.  
  2064.             $url_query = '?'
  2065.                        . PMA_generate_common_url($db, $table)
  2066.                        . '&printview=1'
  2067.                        . '&sql_query=' . urlencode($sql_query);
  2068.             echo '    <!-- Print view -->' . "\n";
  2069.             echo PMA_linkOrButton(
  2070.                 'sql.php' . $url_query,
  2071.                 ($GLOBALS['cfg']['PropertiesIconic'] ? '<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_print.png" height="16" width="16" alt="' . $GLOBALS['strPrintView'] . '"/>' : '') . $GLOBALS['strPrintView'],
  2072.                 '', true, true, 'print_view') . "\n";
  2073.  
  2074.             if (! $_SESSION['userconf']['dontlimitchars']) {
  2075.                 echo   '      ' . "\n";
  2076.                 echo PMA_linkOrButton(
  2077.                     'sql.php' . $url_query . '&dontlimitchars=1',
  2078.                     ($GLOBALS['cfg']['PropertiesIconic'] ? '<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_print.png" height="16" width="16" alt="' . $GLOBALS['strPrintViewFull'] . '"/>' : '') . $GLOBALS['strPrintViewFull'],
  2079.                     '', true, true, 'print_view') . "\n";
  2080.             }
  2081.         } // end displays "printable view"
  2082.  
  2083.         echo "\n";
  2084.     }
  2085.  
  2086.     // Export link
  2087.     // (the url_query has extra parameters that won't be used to export)
  2088.     // (the single_table parameter is used in display_export.lib.php
  2089.     //  to hide the SQL and the structure export dialogs)
  2090.     if (isset($analyzed_sql[0]) && $analyzed_sql[0]['querytype'] == 'SELECT' && !isset($printview)) {
  2091.         if (isset($analyzed_sql[0]['table_ref'][0]['table_true_name']) && !isset($analyzed_sql[0]['table_ref'][1]['table_true_name'])) {
  2092.             $single_table   = '&single_table=true';
  2093.         } else {
  2094.             $single_table   = '';
  2095.         }
  2096.         if (!$header_shown) {
  2097.             echo $header;
  2098.             $header_shown = TRUE;
  2099.         }
  2100.         echo '    <!-- Export -->' . "\n";
  2101.         echo   '      ' . "\n";
  2102.         echo PMA_linkOrButton(
  2103.             'tbl_export.php' . $url_query . '&unlim_num_rows=' . $unlim_num_rows . $single_table,
  2104.             ($GLOBALS['cfg']['PropertiesIconic'] ? '<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_tblexport.png" height="16" width="16" alt="' . $GLOBALS['strExport'] . '" />' : '') . $GLOBALS['strExport'],
  2105.             '', true, true, '') . "\n";
  2106.     }
  2107.  
  2108.     // CREATE VIEW
  2109.     /**
  2110.      *
  2111.      * @todo detect privileges to create a view
  2112.      *       (but see 2006-01-19 note in display_create_table.lib.php,
  2113.      *        I think we cannot detect db-specific privileges reliably)
  2114.      */
  2115.     if (PMA_MYSQL_INT_VERSION >= 50000) {
  2116.         if (!$header_shown) {
  2117.             echo $header;
  2118.             $header_shown = TRUE;
  2119.         }
  2120.         echo '    <!-- Create View -->' . "\n";
  2121.         echo '      ' . "\n";
  2122.         echo PMA_linkOrButton(
  2123.             'view_create.php' . $url_query,
  2124.             ($GLOBALS['cfg']['PropertiesIconic'] ? '<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_views.png" height="16" width="16" alt="CREATE VIEW" />' : '') . 'CREATE VIEW',
  2125.             '', true, true, '') . "\n";
  2126.     }
  2127.  
  2128.     if ($header_shown) {
  2129.         echo '</fieldset><br />';
  2130.     }
  2131. }
  2132. ?>
  2133.