示例#1
0
 /**
  * Displays a MySQL error message in the right frame.
  *
  * @param   string   the error message
  * @param   string   the sql query that failed
  * @param   boolean  whether to show a "modify" link or not
  * @param   string   the "back" link url (full path is not required)
  * @param   boolean  EXIT the page?
  *
  * @global  array    the configuration array
  *
  * @access  public
  */
 function PMA_mysqlDie($error_message = '', $the_query = '', $is_modify_link = TRUE, $back_url = '', $exit = TRUE)
 {
     global $cfg, $table, $db, $sql_query;
     require_once './header.inc.php';
     if (!$error_message) {
         $error_message = PMA_DBI_getError();
     }
     if (!$the_query && !empty($GLOBALS['sql_query'])) {
         $the_query = $GLOBALS['sql_query'];
     }
     // --- Added to solve bug #641765
     // Robbat2 - 12 January 2003, 9:46PM
     // Revised, Robbat2 - 13 Janurary 2003, 2:59PM
     if (!function_exists('PMA_SQP_isError') || PMA_SQP_isError()) {
         $formatted_sql = htmlspecialchars($the_query);
     } else {
         $formatted_sql = PMA_formatSql(PMA_SQP_parse($the_query), $the_query);
     }
     // ---
     echo "\n" . '<!-- PMA-SQL-ERROR -->' . "\n";
     echo '    <table border="0" cellpadding="2" cellspacing="1">' . '        <tr>' . "\n" . '            <th class="tblHeadError"><div class="errorhead">' . $GLOBALS['strError'] . '</div></th>' . "\n" . '        </tr>' . "\n" . '        <tr>' . "\n" . '            <td>';
     // if the config password is wrong, or the MySQL server does not
     // respond, do not show the query that would reveal the
     // username/password
     if (!empty($the_query) && !strstr($the_query, 'connect')) {
         // --- Added to solve bug #641765
         // Robbat2 - 12 January 2003, 9:46PM
         // Revised, Robbat2 - 13 Janurary 2003, 2:59PM
         if (function_exists('PMA_SQP_isError') && PMA_SQP_isError()) {
             echo PMA_SQP_getErrorString();
         }
         // ---
         // modified to show me the help on sql errors (Michael Keck)
         echo '<div class="tblWarn"><p>' . "\n";
         echo '    <b>' . $GLOBALS['strSQLQuery'] . ':</b>' . "\n";
         if (strstr(strtolower($formatted_sql), 'select')) {
             // please show me help to the error on select
             echo PMA_showMySQLDocu('Reference', 'SELECT');
         }
         if ($is_modify_link && isset($db)) {
             if (isset($table)) {
                 $doedit_goto = '<a href="tbl_properties.php?' . PMA_generate_common_url($db, $table) . '&amp;sql_query=' . urlencode($the_query) . '&amp;show_query=1">';
             } else {
                 $doedit_goto = '<a href="db_details.php?' . PMA_generate_common_url($db) . '&amp;sql_query=' . urlencode($the_query) . '&amp;show_query=1">';
             }
             if ($GLOBALS['cfg']['PropertiesIconic']) {
                 echo $doedit_goto . '<img src=" ' . $GLOBALS['pmaThemeImage'] . 'b_edit.png" width="16" height="16" border="0" hspace="2" align="middle" alt="' . $GLOBALS['strEdit'] . '" />' . '</a>';
             } else {
                 echo '    [' . $doedit_goto . $GLOBALS['strEdit'] . '</a>' . ']' . "\n";
             }
         }
         // end if
         echo '</p>' . "\n" . '<p>' . "\n" . '    ' . $formatted_sql . "\n" . '</p></div>' . "\n";
     }
     // end if
     $tmp_mysql_error = '';
     // for saving the original $error_message
     if (!empty($error_message)) {
         $tmp_mysql_error = strtolower($error_message);
         // save the original $error_message
         $error_message = htmlspecialchars($error_message);
         $error_message = preg_replace("@((\r\n)|(\r)|(\n)){3,}@", "\n\n", $error_message);
     }
     // modified to show me the help on error-returns (Michael Keck)
     echo '<div class="tblWarn"><p>' . "\n" . '    <b>' . $GLOBALS['strMySQLSaid'] . '</b>' . PMA_showMySQLDocu('Error-returns', 'Error-returns') . "\n" . '</p>' . "\n";
     // The error message will be displayed within a CODE segment.
     // To preserve original formatting, but allow wordwrapping, we do a couple of replacements
     // Replace all non-single blanks with their HTML-counterpart
     $error_message = str_replace('  ', '&nbsp;&nbsp;', $error_message);
     // Replace TAB-characters with their HTML-counterpart
     $error_message = str_replace("\t", '&nbsp;&nbsp;&nbsp;&nbsp;', $error_message);
     // Replace linebreaks
     $error_message = nl2br($error_message);
     echo '<code>' . "\n" . $error_message . "\n" . '</code><br />' . "\n";
     // feature request #1036254:
     // Add a link by MySQL-Error #1062 - Duplicate entry
     // 2004-10-20 by mk.keck
     if (substr($error_message, 1, 4) == '1062') {
         // TODO: do not assume that the error message is in English
         // and do not use mysql_result()
         // explode the entry and the column
         $arr_mysql_val_key = explode('entry \'', $tmp_mysql_error);
         $arr_mysql_val_key = explode('\' for key', $arr_mysql_val_key[1]);
         // get the duplicate value
         $string_duplicate_val = trim(strtolower($arr_mysql_val_key[0]));
         // get the field name ...
         $string_duplicate_key = mysql_result(mysql_query("SHOW FIELDS FROM " . $table), $arr_mysql_val_key[1] - 1, 0);
         $duplicate_sql_query = "SELECT * FROM " . $table . " WHERE " . $string_duplicate_key . " LIKE '" . $string_duplicate_val . "'";
         echo '        <form method="post" action="read_dump.php" style="padding: 0px; margin: 0px">' . "\n" . '            <input type="hidden" name="sql_query" value="' . $duplicate_sql_query . '" />' . "\n" . '            ' . PMA_generate_common_hidden_inputs($db, $table) . "\n" . '            <input type="submit" name="submit" value="' . $GLOBALS['strBrowse'] . '" />' . "\n" . '        </form>' . "\n";
     }
     // end of show duplicate entry
     echo '</div>';
     if (!empty($back_url) && $exit) {
         $goto_back_url = '<a href="' . (strstr($back_url, '?') ? $back_url . '&amp;no_history=true' : $back_url . '?no_history=true') . '">&nbsp;';
         echo '            </td> ' . "\n" . '        </tr>' . "\n" . '        <tr><td class="tblHeaders" align="center">';
         echo '[' . $goto_back_url . $GLOBALS['strBack'] . '&nbsp;</a>]';
     }
     echo '            </td>' . "\n" . '        </tr>' . "\n" . '    </table>' . "\n\n";
     if ($exit) {
         require_once './footer.inc.php';
     }
 }
 /**
  * Formats SQL queries to html
  *
  * @param  array   The SQL queries
  * @param  string  mode
  * @param  integer starting token
  * @param  integer number of tokens to format, -1 = all
  *
  * @return string  The formatted SQL queries
  *
  * @access public
  */
 function PMA_SQP_formatHtml($arr, $mode = 'color', $start_token = 0, $number_of_tokens = -1)
 {
     // then check for an array
     if (!is_array($arr)) {
         return htmlspecialchars($arr);
     }
     // first check for the SQL parser having hit an error
     if (PMA_SQP_isError()) {
         return htmlspecialchars($arr['raw']);
     }
     // else do it properly
     switch ($mode) {
         case 'color':
             $str = '<span class="syntax">';
             $html_line_break = '<br />';
             break;
         case 'query_only':
             $str = '';
             $html_line_break = "\n";
             break;
         case 'text':
             $str = '';
             $html_line_break = '<br />';
             break;
     }
     // end switch
     $indent = 0;
     $bracketlevel = 0;
     $functionlevel = 0;
     $infunction = FALSE;
     $space_punct_listsep = ' ';
     $space_punct_listsep_function_name = ' ';
     // $space_alpha_reserved_word = '<br />'."\n";
     $space_alpha_reserved_word = ' ';
     $keywords_with_brackets_1before = array('INDEX', 'KEY', 'ON', 'USING');
     $keywords_with_brackets_1before_cnt = 4;
     $keywords_with_brackets_2before = array('IGNORE', 'INDEX', 'INTO', 'KEY', 'PRIMARY', 'PROCEDURE', 'REFERENCES', 'UNIQUE', 'USE');
     // $keywords_with_brackets_2before_cnt = count($keywords_with_brackets_2before);
     $keywords_with_brackets_2before_cnt = 9;
     // These reserved words do NOT get a newline placed near them.
     $keywords_no_newline = array('AS', 'ASC', 'DESC', 'DISTINCT', 'HOUR', 'INTERVAL', 'IS', 'LIKE', 'NOT', 'NULL', 'ON', 'REGEXP');
     $keywords_no_newline_cnt = 12;
     // These reserved words introduce a privilege list
     $keywords_priv_list = array('GRANT', 'REVOKE');
     $keywords_priv_list_cnt = 2;
     if ($number_of_tokens == -1) {
         $arraysize = $arr['len'];
     } else {
         $arraysize = $number_of_tokens;
     }
     $typearr = array();
     if ($arraysize >= 0) {
         $typearr[0] = '';
         $typearr[1] = '';
         $typearr[2] = '';
         //$typearr[3] = $arr[0]['type'];
         $typearr[3] = $arr[$start_token]['type'];
     }
     $in_priv_list = FALSE;
     for ($i = $start_token; $i < $arraysize; $i++) {
         // DEBUG echo "<b>" . $arr[$i]['data'] . "</b> " . $arr[$i]['type'] . "<br />";
         $before = '';
         $after = '';
         $indent = 0;
         // array_shift($typearr);
         /*
         0 prev2
         1 prev
         2 current
         3 next
         */
         if ($i + 1 < $arraysize) {
             // array_push($typearr, $arr[$i + 1]['type']);
             $typearr[4] = $arr[$i + 1]['type'];
         } else {
             //array_push($typearr, NULL);
             $typearr[4] = '';
         }
         for ($j = 0; $j < 4; $j++) {
             $typearr[$j] = $typearr[$j + 1];
         }
         switch ($typearr[2]) {
             case 'white_newline':
                 $before = '';
                 break;
             case 'punct_bracket_open_round':
                 $bracketlevel++;
                 $infunction = FALSE;
                 // Make sure this array is sorted!
                 if ($typearr[1] == 'alpha_functionName' || $typearr[1] == 'alpha_columnType' || $typearr[1] == 'punct' || $typearr[3] == 'digit_integer' || $typearr[3] == 'digit_hex' || $typearr[3] == 'digit_float' || $typearr[0] == 'alpha_reservedWord' && PMA_STR_binarySearchInArr(strtoupper($arr[$i - 2]['data']), $keywords_with_brackets_2before, $keywords_with_brackets_2before_cnt) || $typearr[1] == 'alpha_reservedWord' && PMA_STR_binarySearchInArr(strtoupper($arr[$i - 1]['data']), $keywords_with_brackets_1before, $keywords_with_brackets_1before_cnt)) {
                     $functionlevel++;
                     $infunction = TRUE;
                     $after .= ' ';
                 } else {
                     $indent++;
                     $after .= $mode != 'query_only' ? '<div class="syntax_indent' . $indent . '">' : ' ';
                 }
                 break;
             case 'alpha_identifier':
                 if ($typearr[1] == 'punct_qualifier' || $typearr[3] == 'punct_qualifier') {
                     $after = '';
                     $before = '';
                 }
                 if ($typearr[3] == 'alpha_columnType' || $typearr[3] == 'alpha_identifier') {
                     $after .= ' ';
                 }
                 break;
             case 'punct_qualifier':
                 $before = '';
                 $after = '';
                 break;
             case 'punct_listsep':
                 if ($infunction == TRUE) {
                     $after .= $space_punct_listsep_function_name;
                 } else {
                     $after .= $space_punct_listsep;
                 }
                 break;
             case 'punct_queryend':
                 if ($typearr[3] != 'comment_mysql' && $typearr[3] != 'comment_ansi' && $typearr[3] != 'comment_c') {
                     $after .= $html_line_break;
                     $after .= $html_line_break;
                 }
                 $space_punct_listsep = ' ';
                 $space_punct_listsep_function_name = ' ';
                 $space_alpha_reserved_word = ' ';
                 $in_priv_list = FALSE;
                 break;
             case 'comment_mysql':
             case 'comment_ansi':
                 $after .= $html_line_break;
                 break;
             case 'punct':
                 $before .= ' ';
                 // workaround for
                 // select * from mytable limit 0,-1
                 // (a side effect of this workaround is that
                 // select 20 - 9
                 // becomes
                 // select 20 -9
                 // )
                 if ($typearr[3] != 'digit_integer') {
                     $after .= ' ';
                 }
                 break;
             case 'punct_bracket_close_round':
                 $bracketlevel--;
                 if ($infunction == TRUE) {
                     $functionlevel--;
                     $after .= ' ';
                     $before .= ' ';
                 } else {
                     $indent--;
                     $before .= $mode != 'query_only' ? '</div>' : ' ';
                 }
                 $infunction = $functionlevel > 0 ? TRUE : FALSE;
                 break;
             case 'alpha_columnType':
                 if ($typearr[3] == 'alpha_columnAttrib') {
                     $after .= ' ';
                 }
                 if ($typearr[1] == 'alpha_columnType') {
                     $before .= ' ';
                 }
                 break;
             case 'alpha_columnAttrib':
                 // ALTER TABLE tbl_name AUTO_INCREMENT = 1
                 // COLLATE LATIN1_GENERAL_CI DEFAULT
                 if ($typearr[1] == 'alpha_identifier' || $typearr[1] == 'alpha_charset') {
                     $before .= ' ';
                 }
                 if ($typearr[3] == 'alpha_columnAttrib' || $typearr[3] == 'quote_single' || $typearr[3] == 'digit_integer') {
                     $after .= ' ';
                 }
                 // workaround for
                 // select * from mysql.user where binary user="******"
                 // binary is marked as alpha_columnAttrib
                 // but should be marked as a reserved word
                 if (strtoupper($arr[$i]['data']) == 'BINARY' && $typearr[3] == 'alpha_identifier') {
                     $after .= ' ';
                 }
                 break;
             case 'alpha_reservedWord':
                 // do not uppercase the reserved word if we are calling
                 // this function in query_only mode, because we need
                 // the original query (otherwise we get problems with
                 // semi-reserved words like "storage" which is legal
                 // as an identifier name)
                 if ($mode != 'query_only') {
                     $arr[$i]['data'] = strtoupper($arr[$i]['data']);
                 }
                 if (($typearr[1] != 'alpha_reservedWord' || $typearr[1] == 'alpha_reservedWord' && PMA_STR_binarySearchInArr(strtoupper($arr[$i - 1]['data']), $keywords_no_newline, $keywords_no_newline_cnt)) && $typearr[1] != 'punct_level_plus' && !PMA_STR_binarySearchInArr($arr[$i]['data'], $keywords_no_newline, $keywords_no_newline_cnt)) {
                     // do not put a space before the first token, because
                     // we use a lot of eregi() checking for the first
                     // reserved word at beginning of query
                     // so do not put a newline before
                     //
                     // also we must not be inside a privilege list
                     if ($i > 0) {
                         // the alpha_identifier exception is there to
                         // catch cases like
                         // GRANT SELECT ON mydb.mytable TO myuser@localhost
                         // (else, we get mydb.mytableTO )
                         //
                         // the quote_single exception is there to
                         // catch cases like
                         // GRANT ... TO 'marc'@'domain.com' IDENTIFIED...
                         //
                         // TODO: fix all cases and find why this happens
                         if (!$in_priv_list || $typearr[1] == 'alpha_identifier' || $typearr[1] == 'quote_single' || $typearr[1] == 'white_newline') {
                             $before .= $space_alpha_reserved_word;
                         }
                     } else {
                         // on first keyword, check if it introduces a
                         // privilege list
                         if (PMA_STR_binarySearchInArr($arr[$i]['data'], $keywords_priv_list, $keywords_priv_list_cnt)) {
                             $in_priv_list = TRUE;
                         }
                     }
                 } else {
                     $before .= ' ';
                 }
                 switch ($arr[$i]['data']) {
                     case 'CREATE':
                         if (!$in_priv_list) {
                             $space_punct_listsep = $html_line_break;
                             $space_alpha_reserved_word = ' ';
                         }
                         break;
                     case 'EXPLAIN':
                     case 'DESCRIBE':
                     case 'SET':
                     case 'ALTER':
                     case 'DELETE':
                     case 'SHOW':
                     case 'DROP':
                     case 'UPDATE':
                     case 'TRUNCATE':
                     case 'ANALYZE':
                     case 'ANALYSE':
                         if (!$in_priv_list) {
                             $space_punct_listsep = $html_line_break;
                             $space_alpha_reserved_word = ' ';
                         }
                         break;
                     case 'INSERT':
                     case 'REPLACE':
                         if (!$in_priv_list) {
                             $space_punct_listsep = $html_line_break;
                             $space_alpha_reserved_word = $html_line_break;
                         }
                         break;
                     case 'VALUES':
                         $space_punct_listsep = ' ';
                         $space_alpha_reserved_word = $html_line_break;
                         break;
                     case 'SELECT':
                         $space_punct_listsep = ' ';
                         $space_alpha_reserved_word = $html_line_break;
                         break;
                     default:
                         break;
                 }
                 // end switch ($arr[$i]['data'])
                 $after .= ' ';
                 break;
             case 'digit_integer':
             case 'digit_float':
             case 'digit_hex':
                 //TODO: could there be other types preceding a digit?
                 if ($typearr[1] == 'alpha_reservedWord') {
                     $after .= ' ';
                 }
                 if ($infunction && $typearr[3] == 'punct_bracket_close_round') {
                     $after .= ' ';
                 }
                 if ($typearr[1] == 'alpha_columnAttrib') {
                     $before .= ' ';
                 }
                 break;
             case 'alpha_variable':
                 // other workaround for a problem similar to the one
                 // explained below for quote_single
                 if (!$in_priv_list) {
                     $after = ' ';
                 }
                 break;
             case 'quote_double':
             case 'quote_single':
                 // workaround: for the query
                 // REVOKE SELECT ON `base2\_db`.* FROM 'user'@'%'
                 // the @ is incorrectly marked as alpha_variable
                 // in the parser, and here, the '%' gets a blank before,
                 // which is a syntax error
                 if ($typearr[1] != 'alpha_variable') {
                     $before .= ' ';
                 }
                 if ($infunction && $typearr[3] == 'punct_bracket_close_round') {
                     $after .= ' ';
                 }
                 break;
             case 'quote_backtick':
                 if ($typearr[3] != 'punct_qualifier') {
                     $after .= ' ';
                 }
                 if ($typearr[1] != 'punct_qualifier') {
                     $before .= ' ';
                 }
                 break;
             default:
                 break;
         }
         // end switch ($typearr[2])
         /*
                     if ($typearr[3] != 'punct_qualifier') {
                         $after             .= ' ';
                     }
                     $after                 .= "\n";
         */
         $str .= $before . ($mode == 'color' ? PMA_SQP_formatHTML_colorize($arr[$i]) : $arr[$i]['data']) . $after;
     }
     // end for
     if ($mode == 'color') {
         $str .= '</span>';
     }
     return $str;
 }
示例#3
0
/**
 * displays the message and the query
 * usually the message is the result of the query executed
 *
 * @param string  $message   the message to display
 * @param string  $sql_query the query to display
 * @param string  $type      the type (level) of the message
 * @param boolean $is_view   is this a message after a VIEW operation?
 *
 * @return  string
 *
 * @access  public
 */
function PMA_showMessage($message, $sql_query = null, $type = 'notice', $is_view = false)
{
    /*
     * PMA_ajaxResponse uses this function to collect the string of HTML generated
     * for showing the message.  Use output buffering to collect it and return it
     * in a string.  In some special cases on sql.php, buffering has to be disabled
     * and hence we check with $GLOBALS['buffer_message']
     */
    if ($GLOBALS['is_ajax_request'] == true && !isset($GLOBALS['buffer_message'])) {
        ob_start();
    }
    global $cfg;
    if (null === $sql_query) {
        if (!empty($GLOBALS['display_query'])) {
            $sql_query = $GLOBALS['display_query'];
        } elseif ($cfg['SQP']['fmtType'] == 'none' && !empty($GLOBALS['unparsed_sql'])) {
            $sql_query = $GLOBALS['unparsed_sql'];
        } elseif (!empty($GLOBALS['sql_query'])) {
            $sql_query = $GLOBALS['sql_query'];
        } else {
            $sql_query = '';
        }
    }
    if (isset($GLOBALS['using_bookmark_message'])) {
        $GLOBALS['using_bookmark_message']->display();
        unset($GLOBALS['using_bookmark_message']);
    }
    // Corrects the tooltip text via JS if required
    // @todo this is REALLY the wrong place to do this - very unexpected here
    if (!$is_view && strlen($GLOBALS['table']) && $cfg['ShowTooltip']) {
        $tooltip = PMA_Table::sGetToolTip($GLOBALS['db'], $GLOBALS['table']);
        $uni_tbl = PMA_jsFormat($GLOBALS['db'] . '.' . $GLOBALS['table'], false);
        echo "\n";
        echo '<script type="text/javascript">' . "\n";
        echo '//<![CDATA[' . "\n";
        echo "if (window.parent.updateTableTitle) window.parent.updateTableTitle('" . $uni_tbl . "', '" . PMA_jsFormat($tooltip, false) . "');" . "\n";
        echo '//]]>' . "\n";
        echo '</script>' . "\n";
    }
    // end if ... elseif
    // Checks if the table needs to be repaired after a TRUNCATE query.
    // @todo what about $GLOBALS['display_query']???
    // @todo this is REALLY the wrong place to do this - very unexpected here
    if (strlen($GLOBALS['table']) && $GLOBALS['sql_query'] == 'TRUNCATE TABLE ' . PMA_backquote($GLOBALS['table'])) {
        if (PMA_Table::sGetStatusInfo($GLOBALS['db'], $GLOBALS['table'], 'Index_length') > 1024 && !PMA_DRIZZLE) {
            PMA_DBI_try_query('REPAIR TABLE ' . PMA_backquote($GLOBALS['table']));
        }
    }
    unset($tbl_status);
    // In an Ajax request, $GLOBALS['cell_align_left'] may not be defined. Hence,
    // check for it's presence before using it
    echo '<div id="result_query" align="' . (isset($GLOBALS['cell_align_left']) ? $GLOBALS['cell_align_left'] : '') . '">' . "\n";
    if ($message instanceof PMA_Message) {
        if (isset($GLOBALS['special_message'])) {
            $message->addMessage($GLOBALS['special_message']);
            unset($GLOBALS['special_message']);
        }
        $message->display();
        $type = $message->getLevel();
    } else {
        echo '<div class="' . $type . '">';
        echo PMA_sanitize($message);
        if (isset($GLOBALS['special_message'])) {
            echo PMA_sanitize($GLOBALS['special_message']);
            unset($GLOBALS['special_message']);
        }
        echo '</div>';
    }
    if ($cfg['ShowSQL'] == true && !empty($sql_query)) {
        // Html format the query to be displayed
        // If we want to show some sql code it is easiest to create it here
        /* SQL-Parser-Analyzer */
        if (!empty($GLOBALS['show_as_php'])) {
            $new_line = '\\n"<br />' . "\n" . '&nbsp;&nbsp;&nbsp;&nbsp;. "';
            $query_base = htmlspecialchars(addslashes($sql_query));
            $query_base = preg_replace('/((\\015\\012)|(\\015)|(\\012))/', $new_line, $query_base);
        } else {
            $query_base = $sql_query;
        }
        $query_too_big = false;
        if (strlen($query_base) > $cfg['MaxCharactersInDisplayedSQL']) {
            // when the query is large (for example an INSERT of binary
            // data), the parser chokes; so avoid parsing the query
            $query_too_big = true;
            $shortened_query_base = nl2br(htmlspecialchars(substr($sql_query, 0, $cfg['MaxCharactersInDisplayedSQL']) . '[...]'));
        } elseif (!empty($GLOBALS['parsed_sql']) && $query_base == $GLOBALS['parsed_sql']['raw']) {
            // (here, use "! empty" because when deleting a bookmark,
            // $GLOBALS['parsed_sql'] is set but empty
            $parsed_sql = $GLOBALS['parsed_sql'];
        } else {
            // Parse SQL if needed
            $parsed_sql = PMA_SQP_parse($query_base);
        }
        // Analyze it
        if (isset($parsed_sql) && !PMA_SQP_isError()) {
            $analyzed_display_query = PMA_SQP_analyze($parsed_sql);
            // Same as below (append LIMIT), append the remembered ORDER BY
            if ($GLOBALS['cfg']['RememberSorting'] && isset($analyzed_display_query[0]['queryflags']['select_from']) && isset($GLOBALS['sql_order_to_append'])) {
                $query_base = $analyzed_display_query[0]['section_before_limit'] . "\n" . $GLOBALS['sql_order_to_append'] . $analyzed_display_query[0]['limit_clause'] . ' ' . $analyzed_display_query[0]['section_after_limit'];
                // Need to reparse query
                $parsed_sql = PMA_SQP_parse($query_base);
                // update the $analyzed_display_query
                $analyzed_display_query[0]['section_before_limit'] .= $GLOBALS['sql_order_to_append'];
                $analyzed_display_query[0]['order_by_clause'] = $GLOBALS['sorted_col'];
            }
            // Here we append the LIMIT added for navigation, to
            // enable its display. Adding it higher in the code
            // to $sql_query would create a problem when
            // using the Refresh or Edit links.
            // Only append it on SELECTs.
            /**
             * @todo what would be the best to do when someone hits Refresh:
             * use the current LIMITs ?
             */
            if (isset($analyzed_display_query[0]['queryflags']['select_from']) && isset($GLOBALS['sql_limit_to_append'])) {
                $query_base = $analyzed_display_query[0]['section_before_limit'] . "\n" . $GLOBALS['sql_limit_to_append'] . $analyzed_display_query[0]['section_after_limit'];
                // Need to reparse query
                $parsed_sql = PMA_SQP_parse($query_base);
            }
        }
        if (!empty($GLOBALS['show_as_php'])) {
            $query_base = '$sql  = "' . $query_base;
        } elseif (!empty($GLOBALS['validatequery'])) {
            try {
                $query_base = PMA_validateSQL($query_base);
            } catch (Exception $e) {
                PMA_Message::error(__('Failed to connect to SQL validator!'))->display();
            }
        } elseif (isset($parsed_sql)) {
            $query_base = PMA_formatSql($parsed_sql, $query_base);
        }
        // Prepares links that may be displayed to edit/explain the query
        // (don't go to default pages, we must go to the page
        // where the query box is available)
        // Basic url query part
        $url_params = array();
        if (!isset($GLOBALS['db'])) {
            $GLOBALS['db'] = '';
        }
        if (strlen($GLOBALS['db'])) {
            $url_params['db'] = $GLOBALS['db'];
            if (strlen($GLOBALS['table'])) {
                $url_params['table'] = $GLOBALS['table'];
                $edit_link = 'tbl_sql.php';
            } else {
                $edit_link = 'db_sql.php';
            }
        } else {
            $edit_link = 'server_sql.php';
        }
        // Want to have the query explained
        // but only explain a SELECT (that has not been explained)
        /* SQL-Parser-Analyzer */
        $explain_link = '';
        $is_select = false;
        if (!empty($cfg['SQLQuery']['Explain']) && !$query_too_big) {
            $explain_params = $url_params;
            // Detect if we are validating as well
            // To preserve the validate uRL data
            if (!empty($GLOBALS['validatequery'])) {
                $explain_params['validatequery'] = 1;
            }
            if (preg_match('@^SELECT[[:space:]]+@i', $sql_query)) {
                $explain_params['sql_query'] = 'EXPLAIN ' . $sql_query;
                $_message = __('Explain SQL');
                $is_select = true;
            } elseif (preg_match('@^EXPLAIN[[:space:]]+SELECT[[:space:]]+@i', $sql_query)) {
                $explain_params['sql_query'] = substr($sql_query, 8);
                $_message = __('Skip Explain SQL');
            }
            if (isset($explain_params['sql_query'])) {
                $explain_link = 'import.php' . PMA_generate_common_url($explain_params);
                $explain_link = ' [' . PMA_linkOrButton($explain_link, $_message) . ']';
            }
        }
        //show explain
        $url_params['sql_query'] = $sql_query;
        $url_params['show_query'] = 1;
        // even if the query is big and was truncated, offer the chance
        // to edit it (unless it's enormous, see PMA_linkOrButton() )
        if (!empty($cfg['SQLQuery']['Edit'])) {
            if ($cfg['EditInWindow'] == true) {
                $onclick = 'window.parent.focus_querywindow(\'' . PMA_jsFormat($sql_query, false) . '\'); return false;';
            } else {
                $onclick = '';
            }
            $edit_link .= PMA_generate_common_url($url_params) . '#querybox';
            $edit_link = ' [' . PMA_linkOrButton($edit_link, __('Edit'), array('onclick' => $onclick)) . ']';
        } else {
            $edit_link = '';
        }
        $url_qpart = PMA_generate_common_url($url_params);
        // Also we would like to get the SQL formed in some nice
        // php-code
        if (!empty($cfg['SQLQuery']['ShowAsPHP']) && !$query_too_big) {
            $php_params = $url_params;
            if (!empty($GLOBALS['show_as_php'])) {
                $_message = __('Without PHP Code');
            } else {
                $php_params['show_as_php'] = 1;
                $_message = __('Create PHP Code');
            }
            $php_link = 'import.php' . PMA_generate_common_url($php_params);
            $php_link = ' [' . PMA_linkOrButton($php_link, $_message) . ']';
            if (isset($GLOBALS['show_as_php'])) {
                $runquery_link = 'import.php' . PMA_generate_common_url($url_params);
                $php_link .= ' [' . PMA_linkOrButton($runquery_link, __('Submit Query')) . ']';
            }
        } else {
            $php_link = '';
        }
        //show as php
        // Refresh query
        if (!empty($cfg['SQLQuery']['Refresh']) && !isset($GLOBALS['show_as_php']) && preg_match('@^(SELECT|SHOW)[[:space:]]+@i', $sql_query)) {
            $refresh_link = 'import.php' . PMA_generate_common_url($url_params);
            $refresh_link = ' [' . PMA_linkOrButton($refresh_link, __('Refresh')) . ']';
        } else {
            $refresh_link = '';
        }
        //refresh
        if (!empty($cfg['SQLValidator']['use']) && !empty($cfg['SQLQuery']['Validate'])) {
            $validate_params = $url_params;
            if (!empty($GLOBALS['validatequery'])) {
                $validate_message = __('Skip Validate SQL');
            } else {
                $validate_params['validatequery'] = 1;
                $validate_message = __('Validate SQL');
            }
            $validate_link = 'import.php' . PMA_generate_common_url($validate_params);
            $validate_link = ' [' . PMA_linkOrButton($validate_link, $validate_message) . ']';
        } else {
            $validate_link = '';
        }
        //validator
        if (!empty($GLOBALS['validatequery'])) {
            echo '<div class="sqlvalidate">';
        } else {
            echo '<code class="sql">';
        }
        if ($query_too_big) {
            echo $shortened_query_base;
        } else {
            echo $query_base;
        }
        //Clean up the end of the PHP
        if (!empty($GLOBALS['show_as_php'])) {
            echo '";';
        }
        if (!empty($GLOBALS['validatequery'])) {
            echo '</div>';
        } else {
            echo '</code>';
        }
        echo '<div class="tools">';
        // avoid displaying a Profiling checkbox that could
        // be checked, which would reexecute an INSERT, for example
        if (!empty($refresh_link)) {
            PMA_profilingCheckbox($sql_query);
        }
        // if needed, generate an invisible form that contains controls for the
        // Inline link; this way, the behavior of the Inline link does not
        // depend on the profiling support or on the refresh link
        if (empty($refresh_link) || !PMA_profilingSupported()) {
            echo '<form action="sql.php" method="post">';
            echo PMA_generate_common_hidden_inputs($GLOBALS['db'], $GLOBALS['table']);
            echo '<input type="hidden" name="sql_query" value="' . htmlspecialchars($sql_query) . '" />';
            echo '</form>';
        }
        // in the tools div, only display the Inline link when not in ajax
        // mode because 1) it currently does not work and 2) we would
        // have two similar mechanisms on the page for the same goal
        if ($is_select || $GLOBALS['is_ajax_request'] === false && !$query_too_big) {
            // see in js/functions.js the jQuery code attached to id inline_edit
            // document.write conflicts with jQuery, hence used $().append()
            echo "<script type=\"text/javascript\">\n" . "//<![CDATA[\n" . "\$('.tools form').last().after('[<a href=\"#\" title=\"" . PMA_escapeJsString(__('Inline edit of this query')) . "\" class=\"inline_edit_sql\">" . PMA_escapeJsString(_pgettext('Inline edit query', 'Inline')) . "</a>]');\n" . "//]]>\n" . "</script>";
        }
        echo $edit_link . $explain_link . $php_link . $refresh_link . $validate_link;
        echo '</div>';
    }
    echo '</div>';
    if ($GLOBALS['is_ajax_request'] === false) {
        echo '<br class="clearfloat" />';
    }
    // If we are in an Ajax request, we have most probably been called in
    // PMA_ajaxResponse().  Hence, collect the buffer contents and return it
    // to PMA_ajaxResponse(), which will encode it for JSON.
    if ($GLOBALS['is_ajax_request'] == true && !isset($GLOBALS['buffer_message'])) {
        $buffer_contents = ob_get_contents();
        ob_end_clean();
        return $buffer_contents;
    }
    return null;
}
示例#4
0
/**
 * Formats SQL queries
 *
 * @param array   $arr              The SQL queries
 * @param string  $mode             formatting mode
 * @param integer $start_token      starting token
 * @param integer $number_of_tokens number of tokens to format, -1 = all
 *
 * @return string  The formatted SQL queries
 *
 * @access public
 */
function PMA_SQP_format($arr, $mode = 'text', $start_token = 0, $number_of_tokens = -1)
{
    //DEBUG echo 'in Format<pre>'; print_r($arr); echo '</pre>';
    // then check for an array
    if (!is_array($arr)) {
        return htmlspecialchars($arr);
    }
    // first check for the SQL parser having hit an error
    if (PMA_SQP_isError()) {
        return htmlspecialchars($arr['raw']);
    }
    // else do it properly
    switch ($mode) {
        case 'query_only':
            $str = '';
            $html_line_break = "\n";
            break;
        case 'text':
            $str = '';
            $html_line_break = '<br />';
            break;
    }
    // end switch
    $indent = 0;
    $bracketlevel = 0;
    $functionlevel = 0;
    $infunction = false;
    $space_punct_listsep = ' ';
    $space_punct_listsep_function_name = ' ';
    // $space_alpha_reserved_word = '<br />'."\n";
    $space_alpha_reserved_word = ' ';
    $keywords_with_brackets_1before = array('INDEX' => 1, 'KEY' => 1, 'ON' => 1, 'USING' => 1);
    $keywords_with_brackets_2before = array('IGNORE' => 1, 'INDEX' => 1, 'INTO' => 1, 'KEY' => 1, 'PRIMARY' => 1, 'PROCEDURE' => 1, 'REFERENCES' => 1, 'UNIQUE' => 1, 'USE' => 1);
    // These reserved words do NOT get a newline placed near them.
    $keywords_no_newline = array('AS' => 1, 'ASC' => 1, 'DESC' => 1, 'DISTINCT' => 1, 'DUPLICATE' => 1, 'HOUR' => 1, 'INTERVAL' => 1, 'IS' => 1, 'LIKE' => 1, 'NOT' => 1, 'NULL' => 1, 'ON' => 1, 'REGEXP' => 1);
    // These reserved words introduce a privilege list
    $keywords_priv_list = array('GRANT' => 1, 'REVOKE' => 1);
    if ($number_of_tokens == -1) {
        $number_of_tokens = $arr['len'];
    }
    $typearr = array();
    if ($number_of_tokens >= 0) {
        $typearr[0] = '';
        $typearr[1] = '';
        $typearr[2] = '';
        $typearr[3] = $arr[$start_token]['type'];
    }
    $in_priv_list = false;
    for ($i = $start_token; $i < $number_of_tokens; $i++) {
        // DEBUG echo "Loop format <strong>" . $arr[$i]['data']
        // . "</strong> " . $arr[$i]['type'] . "<br />";
        $before = '';
        $after = '';
        // array_shift($typearr);
        /*
        0 prev2
        1 prev
        2 current
        3 next
        */
        if ($i + 1 < $number_of_tokens) {
            $typearr[4] = $arr[$i + 1]['type'];
        } else {
            $typearr[4] = '';
        }
        for ($j = 0; $j < 4; $j++) {
            $typearr[$j] = $typearr[$j + 1];
        }
        switch ($typearr[2]) {
            case 'alpha_bitfield_constant_introducer':
                $before = ' ';
                $after = '';
                break;
            case 'white_newline':
                $before = '';
                break;
            case 'punct_bracket_open_round':
                $bracketlevel++;
                $infunction = false;
                $keyword_brackets_2before = isset($keywords_with_brackets_2before[strtoupper($arr[$i - 2]['data'])]);
                $keyword_brackets_1before = isset($keywords_with_brackets_1before[strtoupper($arr[$i - 1]['data'])]);
                // Make sure this array is sorted!
                if ($typearr[1] == 'alpha_functionName' || $typearr[1] == 'alpha_columnType' || $typearr[1] == 'punct' || $typearr[3] == 'digit_integer' || $typearr[3] == 'digit_hex' || $typearr[3] == 'digit_float' || $typearr[0] == 'alpha_reservedWord' && $keyword_brackets_2before || $typearr[1] == 'alpha_reservedWord' && $keyword_brackets_1before) {
                    $functionlevel++;
                    $infunction = true;
                    $after .= ' ';
                } else {
                    $indent++;
                    if ($mode != 'query_only') {
                        $after .= '<div class="syntax_indent' . $indent . '">';
                    } else {
                        $after .= ' ';
                    }
                }
                break;
            case 'alpha_identifier':
                if ($typearr[1] == 'punct_qualifier' || $typearr[3] == 'punct_qualifier') {
                    $after = '';
                    $before = '';
                }
                // for example SELECT 1 somealias
                if ($typearr[1] == 'digit_integer') {
                    $before = ' ';
                }
                if ($typearr[3] == 'alpha_columnType' || $typearr[3] == 'alpha_identifier') {
                    $after .= ' ';
                }
                break;
            case 'punct_user':
            case 'punct_qualifier':
                $before = '';
                $after = '';
                break;
            case 'punct_listsep':
                if ($infunction == true) {
                    $after .= $space_punct_listsep_function_name;
                } else {
                    $after .= $space_punct_listsep;
                }
                break;
            case 'punct_queryend':
                if ($typearr[3] != 'comment_mysql' && $typearr[3] != 'comment_ansi' && $typearr[3] != 'comment_c') {
                    $after .= $html_line_break;
                    $after .= $html_line_break;
                }
                $space_punct_listsep = ' ';
                $space_punct_listsep_function_name = ' ';
                $space_alpha_reserved_word = ' ';
                $in_priv_list = false;
                break;
            case 'comment_mysql':
            case 'comment_ansi':
                $after .= $html_line_break;
                break;
            case 'punct':
                $before .= ' ';
                // workaround for
                // select * from mytable limit 0,-1
                // (a side effect of this workaround is that
                // select 20 - 9
                // becomes
                // select 20 -9
                // )
                if ($typearr[3] != 'digit_integer') {
                    $after .= ' ';
                }
                break;
            case 'punct_bracket_close_round':
                // only close bracket level when it was opened before
                if ($bracketlevel > 0) {
                    $bracketlevel--;
                    if ($infunction == true) {
                        $functionlevel--;
                        $after .= ' ';
                        $before .= ' ';
                    } else {
                        $indent--;
                        $before .= $mode != 'query_only' ? '</div>' : ' ';
                    }
                    $infunction = $functionlevel > 0 ? true : false;
                }
                break;
            case 'alpha_columnType':
                if ($typearr[3] == 'alpha_columnAttrib') {
                    $after .= ' ';
                }
                if ($typearr[1] == 'alpha_columnType') {
                    $before .= ' ';
                }
                break;
            case 'alpha_columnAttrib':
                // ALTER TABLE tbl_name AUTO_INCREMENT = 1
                // COLLATE LATIN1_GENERAL_CI DEFAULT
                if ($typearr[1] == 'alpha_identifier' || $typearr[1] == 'alpha_charset') {
                    $before .= ' ';
                }
                if ($typearr[3] == 'alpha_columnAttrib' || $typearr[3] == 'quote_single' || $typearr[3] == 'digit_integer') {
                    $after .= ' ';
                }
                // workaround for
                // AUTO_INCREMENT = 31DEFAULT_CHARSET = utf-8
                if ($typearr[2] == 'alpha_columnAttrib' && $typearr[3] == 'alpha_reservedWord') {
                    $before .= ' ';
                }
                // workaround for
                // select * from mysql.user where binary user="******"
                // binary is marked as alpha_columnAttrib
                // but should be marked as a reserved word
                if (strtoupper($arr[$i]['data']) == 'BINARY' && $typearr[3] == 'alpha_identifier') {
                    $after .= ' ';
                }
                break;
            case 'alpha_functionName':
                break;
            case 'alpha_reservedWord':
                // do not uppercase the reserved word if we are calling
                // this function in query_only mode, because we need
                // the original query (otherwise we get problems with
                // semi-reserved words like "storage" which is legal
                // as an identifier name)
                if ($mode != 'query_only') {
                    $arr[$i]['data'] = strtoupper($arr[$i]['data']);
                }
                if (($typearr[1] != 'alpha_reservedWord' || $typearr[1] == 'alpha_reservedWord' && isset($keywords_no_newline[strtoupper($arr[$i - 1]['data'])])) && $typearr[1] != 'punct_level_plus' && !isset($keywords_no_newline[$arr[$i]['data']])) {
                    // do not put a space before the first token, because
                    // we use a lot of pattern matching checking for the
                    // first reserved word at beginning of query
                    // so do not put a newline before
                    //
                    // also we must not be inside a privilege list
                    if ($i > 0) {
                        // the alpha_identifier exception is there to
                        // catch cases like
                        // GRANT SELECT ON mydb.mytable TO myuser@localhost
                        // (else, we get mydb.mytableTO)
                        //
                        // the quote_single exception is there to
                        // catch cases like
                        // GRANT ... TO 'marc'@'domain.com' IDENTIFIED...
                        /**
                         * @todo fix all cases and find why this happens
                         */
                        if (!$in_priv_list || $typearr[1] == 'alpha_identifier' || $typearr[1] == 'quote_single' || $typearr[1] == 'white_newline') {
                            $before .= $space_alpha_reserved_word;
                        }
                    } else {
                        // on first keyword, check if it introduces a
                        // privilege list
                        if (isset($keywords_priv_list[$arr[$i]['data']])) {
                            $in_priv_list = true;
                        }
                    }
                } else {
                    $before .= ' ';
                }
                switch ($arr[$i]['data']) {
                    case 'CREATE':
                    case 'ALTER':
                    case 'DROP':
                    case 'RENAME':
                    case 'TRUNCATE':
                    case 'ANALYZE':
                    case 'ANALYSE':
                    case 'OPTIMIZE':
                        if (!$in_priv_list) {
                            $space_punct_listsep = $html_line_break;
                            $space_alpha_reserved_word = ' ';
                        }
                        break;
                    case 'EVENT':
                    case 'TABLESPACE':
                    case 'TABLE':
                    case 'FUNCTION':
                    case 'INDEX':
                    case 'PROCEDURE':
                    case 'SERVER':
                    case 'TRIGGER':
                    case 'DATABASE':
                    case 'VIEW':
                    case 'GROUP':
                        break;
                    case 'SET':
                        if (!$in_priv_list) {
                            $space_punct_listsep = $html_line_break;
                            $space_alpha_reserved_word = ' ';
                        }
                        break;
                    case 'EXPLAIN':
                    case 'DESCRIBE':
                    case 'DELETE':
                    case 'SHOW':
                    case 'UPDATE':
                        if (!$in_priv_list) {
                            $space_punct_listsep = $html_line_break;
                            $space_alpha_reserved_word = ' ';
                        }
                        break;
                    case 'INSERT':
                    case 'REPLACE':
                        if (!$in_priv_list) {
                            $space_punct_listsep = $html_line_break;
                            $space_alpha_reserved_word = $html_line_break;
                        }
                        break;
                    case 'VALUES':
                        $space_punct_listsep = ' ';
                        $space_alpha_reserved_word = $html_line_break;
                        break;
                    case 'SELECT':
                        $space_punct_listsep = ' ';
                        $space_alpha_reserved_word = $html_line_break;
                        break;
                    case 'CALL':
                    case 'DO':
                    case 'HANDLER':
                        break;
                    default:
                        break;
                }
                // end switch ($arr[$i]['data'])
                $after .= ' ';
                break;
            case 'digit_integer':
            case 'digit_float':
            case 'digit_hex':
                /**
                 * @todo could there be other types preceding a digit?
                 */
                if ($typearr[1] == 'alpha_reservedWord') {
                    $after .= ' ';
                }
                if ($infunction && $typearr[3] == 'punct_bracket_close_round') {
                    $after .= ' ';
                }
                if ($typearr[1] == 'alpha_columnAttrib') {
                    $before .= ' ';
                }
                break;
            case 'alpha_variable':
                $after = ' ';
                break;
            case 'quote_double':
            case 'quote_single':
                // workaround: for the query
                // REVOKE SELECT ON `base2\_db`.* FROM 'user'@'%'
                // the @ is incorrectly marked as alpha_variable
                // in the parser, and here, the '%' gets a blank before,
                // which is a syntax error
                if ($typearr[1] != 'punct_user' && $typearr[1] != 'alpha_bitfield_constant_introducer') {
                    $before .= ' ';
                }
                if ($infunction && $typearr[3] == 'punct_bracket_close_round') {
                    $after .= ' ';
                }
                break;
            case 'quote_backtick':
                // here we check for punct_user to handle correctly
                // DEFINER = `username`@`%`
                // where @ is the punct_user and `%` is the quote_backtick
                if ($typearr[3] != 'punct_qualifier' && $typearr[3] != 'alpha_variable' && $typearr[3] != 'punct_user') {
                    $after .= ' ';
                }
                if ($typearr[1] != 'punct_qualifier' && $typearr[1] != 'alpha_variable' && $typearr[1] != 'punct_user') {
                    $before .= ' ';
                }
                break;
            default:
                break;
        }
        // end switch ($typearr[2])
        /*
        if ($typearr[3] != 'punct_qualifier') {
            $after             .= ' ';
        }
        $after                 .= "\n";
        */
        $str .= $before;
        if ($mode == 'text') {
            $str .= htmlspecialchars($arr[$i]['data']);
        } else {
            $str .= $arr[$i]['data'];
        }
        $str .= $after;
    }
    // end for
    // close unclosed indent levels
    while ($indent > 0) {
        $indent--;
        $str .= $mode != 'query_only' ? '</div>' : ' ';
    }
    return $str;
}
示例#5
0
/**
 * Formats SQL queries
 *
 * @param array   $arr              The SQL queries
 * @param string  $mode             formatting mode
 * @param integer $start_token      starting token
 * @param integer $number_of_tokens number of tokens to format, -1 = all
 *
 * @return string  The formatted SQL queries
 *
 * @access public
 */
function PMA_SQP_format($arr, $mode = 'text', $start_token = 0, $number_of_tokens = -1)
{
    //DEBUG echo 'in Format<pre>'; print_r($arr); echo '</pre>';
    // then check for an array
    if (!is_array($arr)) {
        return htmlspecialchars($arr);
    }
    // first check for the SQL parser having hit an error
    if (PMA_SQP_isError()) {
        return htmlspecialchars($arr['raw']);
    }
    // else do it properly
    switch ($mode) {
        case 'query_only':
            $str = '';
            $html_line_break = "\n";
            break;
        case 'text':
            $str = '';
            $html_line_break = '<br />';
            break;
    }
    // end switch
    $indent = 0;
    $bracketlevel = 0;
    $functionlevel = 0;
    $infunction = false;
    $space_punct_listsep = ' ';
    $space_punct_listsep_function_name = ' ';
    // $space_alpha_reserved_word = '<br />'."\n";
    $space_alpha_reserved_word = ' ';
    $keywords_with_brackets_1before = array('INDEX' => 1, 'KEY' => 1, 'ON' => 1, 'USING' => 1);
    $keywords_with_brackets_2before = array('IGNORE' => 1, 'INDEX' => 1, 'INTO' => 1, 'KEY' => 1, 'PRIMARY' => 1, 'PROCEDURE' => 1, 'REFERENCES' => 1, 'UNIQUE' => 1, 'USE' => 1);
    // These reserved words do NOT get a newline placed near them.
    $keywords_no_newline = array('AS' => 1, 'ASC' => 1, 'DESC' => 1, 'DISTINCT' => 1, 'DUPLICATE' => 1, 'HOUR' => 1, 'INTERVAL' => 1, 'IS' => 1, 'LIKE' => 1, 'NOT' => 1, 'NULL' => 1, 'ON' => 1, 'REGEXP' => 1);
    // These reserved words introduce a privilege list
    $keywords_priv_list = array('GRANT' => 1, 'REVOKE' => 1);
    if ($number_of_tokens == -1) {
        $number_of_tokens = $arr['len'];
    }
    $typearr = array();
    if ($number_of_tokens >= 0) {
        $typearr[0] = '';
        $typearr[1] = '';
        $typearr[2] = '';
        $typearr[3] = $arr[$start_token]['type'];
    }
    $in_priv_list = false;
    for ($i = $start_token; $i < $number_of_tokens; $i++) {
        // DEBUG echo "Loop format <strong>" . $arr[$i]['data']
        // . "</strong> " . $arr[$i]['type'] . "<br />";
        $before = '';
        $after = '';
        // array_shift($typearr);
        /*
        0 prev2
        1 prev
        2 current
        3 next
        */
        if ($i + 1 < $number_of_tokens) {
            $typearr[4] = $arr[$i + 1]['type'];
        } else {
            $typearr[4] = '';
        }
        for ($j = 0; $j < 4; $j++) {
            $typearr[$j] = $typearr[$j + 1];
        }
        switch ($typearr[2]) {
            case 'alpha_bitfield_constant_introducer':
                $before = ' ';
                $after = '';
                break;
            case 'white_newline':
                $before = '';
                break;
            case 'punct_bracket_open_round':
                $bracketlevel++;
                $infunction = false;
                $keyword_brackets_2before = isset($keywords_with_brackets_2before[mb_strtoupper($arr[$i - 2]['data'])]);
                $keyword_brackets_1before = isset($keywords_with_brackets_1before[mb_strtoupper($arr[$i - 1]['data'])]);
                // Make sure this array is sorted!
                if ($typearr[1] == 'alpha_functionName' || $typearr[1] == 'alpha_columnType' || $typearr[1] == 'punct' || $typearr[3] == 'digit_integer' || $typearr[3] == 'digit_hex' || $typearr[3] == 'digit_float' || $typearr[0] == 'alpha_reservedWord' && $keyword_brackets_2before || $typearr[1] == 'alpha_reservedWord' && $keyword_brackets_1before) {
                    $functionlevel++;
                    $infunction = true;
                    $after .= ' ';
                } else {
                    $indent++;
                    if ($mode != 'query_only') {
                        $after .= '<div class="syntax_indent' . $indent . '">';
                    } else {
                        $after .= ' ';
                    }
                }
                break;
            case 'alpha_identifier':
                if ($typearr[1] == 'punct_qualifier' || $typearr[3] == 'punct_qualifier') {
                    $after = '';
                    $before = '';
                }
                // for example SELECT 1 somealias
                if ($typearr[1] == 'digit_integer') {
                    $before = ' ';
                }
                if ($typearr[3] == 'alpha_columnType' || $typearr[3] == 'alpha_identifier') {
                    $after .= ' ';
                }
                break;
            case 'punct_user':
            case 'punct_qualifier':
                $before = '';
                $after = '';
                break;
            case 'punct_listsep':
                if ($infunction == true) {
                    $after .= $space_punct_listsep_function_name;
                } else {
                    $after .= $space_punct_listsep;
                }
                break;
            case 'punct_queryend':
                if ($typearr[3] != 'comment_mysql' && $typearr[3] != 'comment_ansi' && $typearr[3] != 'comment_c') {
                    $after .= $html_line_break;
                    $after .= $html_line_break;
                }
                $space_punct_listsep = ' ';
                $space_punct_listsep_function_name = ' ';
                $space_alpha_reserved_word = ' ';
                $in_priv_list = false;
                break;
            case 'comment_mysql':
            case 'comment_ansi':
                $after .= $html_line_break;
                break;
            case 'punct':
                $before .= ' ';
                // workaround for
                // select * from mytable limit 0,-1
                // (a side effect of this workaround is that
                // select 20 - 9
                // becomes
                // select 20 -9
                // )
                if ($typearr[3] != 'digit_integer') {
                    $after .= ' ';
                }
                break;
            case 'punct_bracket_close_round':
                // only close bracket level when it was opened before
                if ($bracketlevel > 0) {
                    $bracketlevel--;
                    if ($infunction == true) {
                        $functionlevel--;
                        $after .= ' ';
                        $before .= ' ';
                    } else {
                        $indent--;
                        $before .= $mode != 'query_only' ? '</div>' : ' ';
                    }
                    $infunction = $functionlevel > 0 ? true : false;
                }
                break;
            case 'alpha_columnType':
                if ($typearr[3] == 'alpha_columnAttrib') {
                    $after .= ' ';
                }
                if ($typearr[1] == 'alpha_columnType') {
                    $before .= ' ';
                }
                break;
            case 'alpha_columnAttrib':
                // ALTER TABLE tbl_name AUTO_INCREMENT = 1
                // COLLATE LATIN1_GENERAL_CI DEFAULT
                if ($typearr[1] == 'alpha_identifier' || $typearr[1] == 'alpha_charset') {
                    $before .= ' ';
                }
                if ($typearr[3] == 'alpha_columnAttrib' || $typearr[3] == 'quote_single' || $typearr[3] == 'digit_integer') {
                    $after .= ' ';
                }
                // workaround for
                // AUTO_INCREMENT = 31DEFAULT_CHARSET = utf-8
                if ($typearr[2] == 'alpha_columnAttrib' && $typearr[3] == 'alpha_reservedWord') {
                    $before .= ' ';
                }
                // workaround for
                // select * from mysql.user where binary user="******"
                // binary is marked as alpha_columnAttrib
                // but should be marked as a reserved word
                if (mb_strtoupper($arr[$i]['data']) == 'BINARY' && $typearr[3] == 'alpha_identifier') {
                    $after .= ' ';
                }
                break;
            case 'alpha_functionName':
                break;
            case 'alpha_reservedWord':
                // do not uppercase the reserved word if we are calling
                // this function in query_only mode, because we need
                // the original query (otherwise we get problems with
                // semi-reserved words like "storage" which is legal
                // as an identifier name)
                if ($mode != 'query_only') {
                    $arr[$i]['data'] = mb_strtoupper($arr[$i]['data']);
                }
                list($before, $in_priv_list) = PMA_SQP_getBeforeAndInPrivList($arr, $typearr, $keywords_no_newline, $i, $in_priv_list, $space_alpha_reserved_word, $before, $keywords_priv_list);
                list($space_punct_listsep, $space_alpha_reserved_word) = PMA_SQP_getListsepAndReservedWord($arr[$i]['data'], $in_priv_list, $html_line_break, $space_punct_listsep, $space_alpha_reserved_word);
                $after .= ' ';
                break;
            case 'digit_integer':
            case 'digit_float':
            case 'digit_hex':
                /**
                 * @todo could there be other types preceding a digit?
                 */
                if ($typearr[1] == 'alpha_reservedWord') {
                    $after .= ' ';
                }
                if ($infunction && $typearr[3] == 'punct_bracket_close_round') {
                    $after .= ' ';
                }
                if ($typearr[1] == 'alpha_columnAttrib') {
                    $before .= ' ';
                }
                break;
            case 'alpha_variable':
                $after = ' ';
                break;
            case 'quote_double':
            case 'quote_single':
                // workaround: for the query
                // REVOKE SELECT ON `base2\_db`.* FROM 'user'@'%'
                // the @ is incorrectly marked as alpha_variable
                // in the parser, and here, the '%' gets a blank before,
                // which is a syntax error
                if ($typearr[1] != 'punct_user' && $typearr[1] != 'alpha_bitfield_constant_introducer') {
                    $before .= ' ';
                }
                if ($infunction && $typearr[3] == 'punct_bracket_close_round') {
                    $after .= ' ';
                }
                break;
            case 'quote_backtick':
                // here we check for punct_user to handle correctly
                // DEFINER = `username`@`%`
                // where @ is the punct_user and `%` is the quote_backtick
                if ($typearr[3] != 'punct_qualifier' && $typearr[3] != 'alpha_variable' && $typearr[3] != 'punct_user') {
                    $after .= ' ';
                }
                if ($typearr[1] != 'punct_qualifier' && $typearr[1] != 'alpha_variable' && $typearr[1] != 'punct_user') {
                    $before .= ' ';
                }
                break;
            default:
                break;
        }
        // end switch ($typearr[2])
        /*
        if ($typearr[3] != 'punct_qualifier') {
            $after             .= ' ';
        }
        $after                 .= "\n";
        */
        $str .= $before;
        if ($mode == 'text') {
            $str .= htmlspecialchars($arr[$i]['data']);
        } else {
            $str .= $arr[$i]['data'];
        }
        $str .= $after;
    }
    // end for
    // close unclosed indent levels
    while ($indent > 0) {
        $indent--;
        $str .= $mode != 'query_only' ? '</div>' : ' ';
    }
    return $str;
}
示例#6
0
/**
 * Displays a MySQL error message in the right frame.
 *
 * @uses    footer.inc.php
 * @uses    header.inc.php
 * @uses    $GLOBALS['sql_query']
 * @uses    $GLOBALS['strError']
 * @uses    $GLOBALS['strSQLQuery']
 * @uses    $GLOBALS['pmaThemeImage']
 * @uses    $GLOBALS['strEdit']
 * @uses    $GLOBALS['strMySQLSaid']
 * @uses    $GLOBALS['cfg']['PropertiesIconic']
 * @uses    $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']
 * @uses    PMA_backquote()
 * @uses    PMA_DBI_getError()
 * @uses    PMA_formatSql()
 * @uses    PMA_generate_common_hidden_inputs()
 * @uses    PMA_generate_common_url()
 * @uses    PMA_showMySQLDocu()
 * @uses    PMA_sqlAddslashes()
 * @uses    PMA_SQP_isError()
 * @uses    PMA_SQP_parse()
 * @uses    PMA_SQP_getErrorString()
 * @uses    strtolower()
 * @uses    urlencode()
 * @uses    str_replace()
 * @uses    nl2br()
 * @uses    substr()
 * @uses    preg_replace()
 * @uses    preg_match()
 * @uses    explode()
 * @uses    implode()
 * @uses    is_array()
 * @uses    function_exists()
 * @uses    htmlspecialchars()
 * @uses    trim()
 * @uses    strstr()
 * @param   string   the error message
 * @param   string   the sql query that failed
 * @param   boolean  whether to show a "modify" link or not
 * @param   string   the "back" link url (full path is not required)
 * @param   boolean  EXIT the page?
 *
 * @global  string    the curent table
 * @global  string    the current db
 *
 * @access  public
 */
function PMA_mysqlDie($error_message = '', $the_query = '', $is_modify_link = true, $back_url = '', $exit = true)
{
    global $table, $db;
    /**
     * start http output, display html headers
     */
    require_once './libraries/header.inc.php';
    $error_msg_output = '';
    if (!$error_message) {
        $error_message = PMA_DBI_getError();
    }
    if (!$the_query && !empty($GLOBALS['sql_query'])) {
        $the_query = $GLOBALS['sql_query'];
    }
    // --- Added to solve bug #641765
    // Robbat2 - 12 January 2003, 9:46PM
    // Revised, Robbat2 - 13 January 2003, 2:59PM
    if (!function_exists('PMA_SQP_isError') || PMA_SQP_isError()) {
        $formatted_sql = htmlspecialchars($the_query);
    } elseif (empty($the_query) || trim($the_query) == '') {
        $formatted_sql = '';
    } else {
        if (strlen($the_query) > $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
            $formatted_sql = substr($the_query, 0, $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) . '[...]';
        } else {
            $formatted_sql = PMA_formatSql(PMA_SQP_parse($the_query), $the_query);
        }
    }
    // ---
    $error_msg_output .= "\n" . '<!-- PMA-SQL-ERROR -->' . "\n";
    $error_msg_output .= '    <div class="error"><h1>' . $GLOBALS['strError'] . '</h1>' . "\n";
    // if the config password is wrong, or the MySQL server does not
    // respond, do not show the query that would reveal the
    // username/password
    if (!empty($the_query) && !strstr($the_query, 'connect')) {
        // --- Added to solve bug #641765
        // Robbat2 - 12 January 2003, 9:46PM
        // Revised, Robbat2 - 13 January 2003, 2:59PM
        if (function_exists('PMA_SQP_isError') && PMA_SQP_isError()) {
            $error_msg_output .= PMA_SQP_getErrorString() . "\n";
            $error_msg_output .= '<br />' . "\n";
        }
        // ---
        // modified to show me the help on sql errors (Michael Keck)
        $error_msg_output .= '    <p><strong>' . $GLOBALS['strSQLQuery'] . ':</strong>' . "\n";
        if (strstr(strtolower($formatted_sql), 'select')) {
            // please show me help to the error on select
            $error_msg_output .= PMA_showMySQLDocu('SQL-Syntax', 'SELECT');
        }
        if ($is_modify_link) {
            $_url_params = array('sql_query' => $the_query, 'show_query' => 1);
            if (strlen($table)) {
                $_url_params['db'] = $db;
                $_url_params['table'] = $table;
                $doedit_goto = '<a href="tbl_sql.php?' . PMA_generate_common_url($_url_params) . '">';
            } elseif (strlen($db)) {
                $_url_params['db'] = $db;
                $doedit_goto = '<a href="db_sql.php?' . PMA_generate_common_url($_url_params) . '">';
            } else {
                $doedit_goto = '<a href="server_sql.php?' . PMA_generate_common_url($_url_params) . '">';
            }
            $error_msg_output .= $doedit_goto . PMA_getIcon('b_edit.png', $GLOBALS['strEdit']) . '</a>';
        }
        // end if
        $error_msg_output .= '    </p>' . "\n" . '    <p>' . "\n" . '        ' . $formatted_sql . "\n" . '    </p>' . "\n";
    }
    // end if
    $tmp_mysql_error = '';
    // for saving the original $error_message
    if (!empty($error_message)) {
        $tmp_mysql_error = strtolower($error_message);
        // save the original $error_message
        $error_message = htmlspecialchars($error_message);
        $error_message = preg_replace("@((\r\n)|(\r)|(\n)){3,}@", "\n\n", $error_message);
    }
    // modified to show me the help on error-returns (Michael Keck)
    // (now error-messages-server)
    $error_msg_output .= '<p>' . "\n" . '    <strong>' . $GLOBALS['strMySQLSaid'] . '</strong>' . PMA_showMySQLDocu('Error-messages-server', 'Error-messages-server') . "\n" . '</p>' . "\n";
    // The error message will be displayed within a CODE segment.
    // To preserve original formatting, but allow wordwrapping, we do a couple of replacements
    // Replace all non-single blanks with their HTML-counterpart
    $error_message = str_replace('  ', '&nbsp;&nbsp;', $error_message);
    // Replace TAB-characters with their HTML-counterpart
    $error_message = str_replace("\t", '&nbsp;&nbsp;&nbsp;&nbsp;', $error_message);
    // Replace linebreaks
    $error_message = nl2br($error_message);
    $error_msg_output .= '<code>' . "\n" . $error_message . "\n" . '</code><br />' . "\n";
    $error_msg_output .= '</div>';
    $_SESSION['Import_message']['message'] = $error_msg_output;
    if ($exit) {
        if (!empty($back_url)) {
            if (strstr($back_url, '?')) {
                $back_url .= '&amp;no_history=true';
            } else {
                $back_url .= '?no_history=true';
            }
            $_SESSION['Import_message']['go_back_url'] = $back_url;
            $error_msg_output .= '<fieldset class="tblFooters">';
            $error_msg_output .= '[ <a href="' . $back_url . '">' . $GLOBALS['strBack'] . '</a> ]';
            $error_msg_output .= '</fieldset>' . "\n\n";
        }
        echo $error_msg_output;
        /**
         * display footer and exit
         */
        require_once './libraries/footer.inc.php';
    } else {
        echo $error_msg_output;
    }
}
示例#7
0
 /**
  * Prepare the message and the query
  * usually the message is the result of the query executed
  *
  * @param string  $message   the message to display
  * @param string  $sql_query the query to display
  * @param string  $type      the type (level) of the message
  * @param boolean $is_view   is this a message after a VIEW operation?
  *
  * @return string
  *
  * @access  public
  */
 public static function getMessage($message, $sql_query = null, $type = 'notice', $is_view = false)
 {
     global $cfg;
     $retval = '';
     if (null === $sql_query) {
         if (!empty($GLOBALS['display_query'])) {
             $sql_query = $GLOBALS['display_query'];
         } elseif (!empty($GLOBALS['unparsed_sql'])) {
             $sql_query = $GLOBALS['unparsed_sql'];
         } elseif (!empty($GLOBALS['sql_query'])) {
             $sql_query = $GLOBALS['sql_query'];
         } else {
             $sql_query = '';
         }
     }
     if (isset($GLOBALS['using_bookmark_message'])) {
         $retval .= $GLOBALS['using_bookmark_message']->getDisplay();
         unset($GLOBALS['using_bookmark_message']);
     }
     // In an Ajax request, $GLOBALS['cell_align_left'] may not be defined. Hence,
     // check for it's presence before using it
     $retval .= '<div id="result_query"' . (isset($GLOBALS['cell_align_left']) ? ' style="text-align: ' . $GLOBALS['cell_align_left'] . '"' : '') . '>' . "\n";
     if ($message instanceof PMA_Message) {
         if (isset($GLOBALS['special_message'])) {
             $message->addMessage($GLOBALS['special_message']);
             unset($GLOBALS['special_message']);
         }
         $retval .= $message->getDisplay();
     } else {
         $retval .= '<div class="' . $type . '">';
         $retval .= PMA_sanitize($message);
         if (isset($GLOBALS['special_message'])) {
             $retval .= PMA_sanitize($GLOBALS['special_message']);
             unset($GLOBALS['special_message']);
         }
         $retval .= '</div>';
     }
     if ($cfg['ShowSQL'] == true && !empty($sql_query)) {
         // Html format the query to be displayed
         // If we want to show some sql code it is easiest to create it here
         /* SQL-Parser-Analyzer */
         if (!empty($GLOBALS['show_as_php'])) {
             $new_line = '\\n"<br />' . "\n" . '&nbsp;&nbsp;&nbsp;&nbsp;. "';
             $query_base = htmlspecialchars(addslashes($sql_query));
             $query_base = preg_replace('/((\\015\\012)|(\\015)|(\\012))/', $new_line, $query_base);
         } else {
             $query_base = $sql_query;
         }
         $query_too_big = false;
         if (strlen($query_base) > $cfg['MaxCharactersInDisplayedSQL']) {
             // when the query is large (for example an INSERT of binary
             // data), the parser chokes; so avoid parsing the query
             $query_too_big = true;
             $shortened_query_base = nl2br(htmlspecialchars(substr($sql_query, 0, $cfg['MaxCharactersInDisplayedSQL']) . '[...]'));
         } elseif (!empty($GLOBALS['parsed_sql']) && $query_base == $GLOBALS['parsed_sql']['raw']) {
             // (here, use "! empty" because when deleting a bookmark,
             // $GLOBALS['parsed_sql'] is set but empty
             $parsed_sql = $GLOBALS['parsed_sql'];
         } else {
             // Parse SQL if needed
             $parsed_sql = PMA_SQP_parse($query_base);
         }
         // Analyze it
         if (isset($parsed_sql) && !PMA_SQP_isError()) {
             $analyzed_display_query = PMA_SQP_analyze($parsed_sql);
             // Same as below (append LIMIT), append the remembered ORDER BY
             if ($GLOBALS['cfg']['RememberSorting'] && isset($analyzed_display_query[0]['queryflags']['select_from']) && isset($GLOBALS['sql_order_to_append'])) {
                 $query_base = $analyzed_display_query[0]['section_before_limit'] . "\n" . $GLOBALS['sql_order_to_append'] . $analyzed_display_query[0]['limit_clause'] . ' ' . $analyzed_display_query[0]['section_after_limit'];
                 // Need to reparse query
                 $parsed_sql = PMA_SQP_parse($query_base);
                 // update the $analyzed_display_query
                 $analyzed_display_query[0]['section_before_limit'] .= $GLOBALS['sql_order_to_append'];
                 $analyzed_display_query[0]['order_by_clause'] = $GLOBALS['sorted_col'];
             }
             // Here we append the LIMIT added for navigation, to
             // enable its display. Adding it higher in the code
             // to $sql_query would create a problem when
             // using the Refresh or Edit links.
             // Only append it on SELECTs.
             /**
              * @todo what would be the best to do when someone hits Refresh:
              * use the current LIMITs ?
              */
             if (isset($analyzed_display_query[0]['queryflags']['select_from']) && !empty($GLOBALS['sql_limit_to_append'])) {
                 $query_base = $analyzed_display_query[0]['section_before_limit'] . "\n" . $GLOBALS['sql_limit_to_append'] . $analyzed_display_query[0]['section_after_limit'];
                 // Need to reparse query
                 $parsed_sql = PMA_SQP_parse($query_base);
             }
         }
         if (!empty($GLOBALS['show_as_php'])) {
             $query_base = '$sql  = "' . $query_base;
         } elseif (isset($query_base)) {
             $query_base = self::formatSql($query_base);
         }
         // Prepares links that may be displayed to edit/explain the query
         // (don't go to default pages, we must go to the page
         // where the query box is available)
         // Basic url query part
         $url_params = array();
         if (!isset($GLOBALS['db'])) {
             $GLOBALS['db'] = '';
         }
         if (strlen($GLOBALS['db'])) {
             $url_params['db'] = $GLOBALS['db'];
             if (strlen($GLOBALS['table'])) {
                 $url_params['table'] = $GLOBALS['table'];
                 $edit_link = 'tbl_sql.php';
             } else {
                 $edit_link = 'db_sql.php';
             }
         } else {
             $edit_link = 'server_sql.php';
         }
         // Want to have the query explained
         // but only explain a SELECT (that has not been explained)
         /* SQL-Parser-Analyzer */
         $explain_link = '';
         $is_select = preg_match('@^SELECT[[:space:]]+@i', $sql_query);
         if (!empty($cfg['SQLQuery']['Explain']) && !$query_too_big) {
             $explain_params = $url_params;
             if ($is_select) {
                 $explain_params['sql_query'] = 'EXPLAIN ' . $sql_query;
                 $_message = __('Explain SQL');
             } elseif (preg_match('@^EXPLAIN[[:space:]]+SELECT[[:space:]]+@i', $sql_query)) {
                 $explain_params['sql_query'] = substr($sql_query, 8);
                 $_message = __('Skip Explain SQL');
             }
             if (isset($explain_params['sql_query'])) {
                 $explain_link = 'import.php' . PMA_URL_getCommon($explain_params);
                 $explain_link = ' [' . self::linkOrButton($explain_link, $_message) . ']';
             }
         }
         //show explain
         $url_params['sql_query'] = $sql_query;
         $url_params['show_query'] = 1;
         // even if the query is big and was truncated, offer the chance
         // to edit it (unless it's enormous, see linkOrButton() )
         if (!empty($cfg['SQLQuery']['Edit'])) {
             if ($cfg['EditInWindow'] == true) {
                 $onclick = 'PMA_querywindow.focus(\'' . PMA_jsFormat($sql_query, false) . '\'); return false;';
             } else {
                 $onclick = '';
             }
             $edit_link .= PMA_URL_getCommon($url_params) . '#querybox';
             $edit_link = ' [' . self::linkOrButton($edit_link, __('Edit'), array('onclick' => $onclick, 'class' => 'disableAjax')) . ']';
         } else {
             $edit_link = '';
         }
         // Also we would like to get the SQL formed in some nice
         // php-code
         if (!empty($cfg['SQLQuery']['ShowAsPHP']) && !$query_too_big) {
             $php_params = $url_params;
             if (!empty($GLOBALS['show_as_php'])) {
                 $_message = __('Without PHP Code');
             } else {
                 $php_params['show_as_php'] = 1;
                 $_message = __('Create PHP Code');
             }
             $php_link = 'import.php' . PMA_URL_getCommon($php_params);
             $php_link = ' [' . self::linkOrButton($php_link, $_message) . ']';
             if (isset($GLOBALS['show_as_php'])) {
                 $runquery_link = 'import.php' . PMA_URL_getCommon($url_params);
                 $php_link .= ' [' . self::linkOrButton($runquery_link, __('Submit Query')) . ']';
             }
         } else {
             $php_link = '';
         }
         //show as php
         // Refresh query
         if (!empty($cfg['SQLQuery']['Refresh']) && !isset($GLOBALS['show_as_php']) && preg_match('@^(SELECT|SHOW)[[:space:]]+@i', $sql_query)) {
             $refresh_link = 'import.php' . PMA_URL_getCommon($url_params);
             $refresh_link = ' [' . self::linkOrButton($refresh_link, __('Refresh')) . ']';
         } else {
             $refresh_link = '';
         }
         //refresh
         $retval .= '<div class="sqlOuter">';
         if ($query_too_big) {
             $retval .= $shortened_query_base;
         } else {
             $retval .= $query_base;
         }
         //Clean up the end of the PHP
         if (!empty($GLOBALS['show_as_php'])) {
             $retval .= '";';
         }
         $retval .= '</div>';
         $retval .= '<div class="tools">';
         $retval .= '<form action="sql.php" method="post">';
         $retval .= PMA_URL_getHiddenInputs($GLOBALS['db'], $GLOBALS['table']);
         $retval .= '<input type="hidden" name="sql_query" value="' . htmlspecialchars($sql_query) . '" />';
         // avoid displaying a Profiling checkbox that could
         // be checked, which would reexecute an INSERT, for example
         if (!empty($refresh_link) && self::profilingSupported()) {
             $retval .= '<input type="hidden" name="profiling_form" value="1" />';
             $retval .= self::getCheckbox('profiling', __('Profiling'), isset($_SESSION['profiling']), true);
         }
         $retval .= '</form>';
         /**
          * TODO: Should we have $cfg['SQLQuery']['InlineEdit']?
          */
         if (!empty($cfg['SQLQuery']['Edit']) && !$query_too_big) {
             $inline_edit_link = ' [' . self::linkOrButton('#', _pgettext('Inline edit query', 'Inline'), array('class' => 'inline_edit_sql')) . ']';
         } else {
             $inline_edit_link = '';
         }
         $retval .= $inline_edit_link . $edit_link . $explain_link . $php_link . $refresh_link;
         $retval .= '</div>';
     }
     $retval .= '</div>';
     if ($GLOBALS['is_ajax_request'] === false) {
         $retval .= '<br class="clearfloat" />';
     }
     return $retval;
 }
示例#8
0
 /**
  * Displays a MySQL error message in the right frame.
  *
  * @param   string   the error message
  * @param   string   the sql query that failed
  * @param   boolean  whether to show a "modify" link or not
  * @param   string   the "back" link url (full path is not required)
  * @param   boolean  EXIT the page?
  *
  * @global  array    the configuration array
  *
  * @access  public
  */
 function PMA_mysqlDie($error_message = '', $the_query = '', $is_modify_link = true, $back_url = '', $exit = true)
 {
     global $cfg, $table, $db, $sql_query;
     require_once './libraries/header.inc.php';
     if (!$error_message) {
         $error_message = PMA_DBI_getError();
     }
     if (!$the_query && !empty($GLOBALS['sql_query'])) {
         $the_query = $GLOBALS['sql_query'];
     }
     // --- Added to solve bug #641765
     // Robbat2 - 12 January 2003, 9:46PM
     // Revised, Robbat2 - 13 January 2003, 2:59PM
     if (!function_exists('PMA_SQP_isError') || PMA_SQP_isError()) {
         $formatted_sql = htmlspecialchars($the_query);
     } elseif (empty($the_query) || trim($the_query) == '') {
         $formatted_sql = '';
     } else {
         $formatted_sql = PMA_formatSql(PMA_SQP_parse($the_query), $the_query);
     }
     // ---
     echo "\n" . '<!-- PMA-SQL-ERROR -->' . "\n";
     echo '    <div class="error"><h1>' . $GLOBALS['strError'] . '</h1>' . "\n";
     // if the config password is wrong, or the MySQL server does not
     // respond, do not show the query that would reveal the
     // username/password
     if (!empty($the_query) && !strstr($the_query, 'connect')) {
         // --- Added to solve bug #641765
         // Robbat2 - 12 January 2003, 9:46PM
         // Revised, Robbat2 - 13 January 2003, 2:59PM
         if (function_exists('PMA_SQP_isError') && PMA_SQP_isError()) {
             echo PMA_SQP_getErrorString() . "\n";
             echo '<br />' . "\n";
         }
         // ---
         // modified to show me the help on sql errors (Michael Keck)
         echo '    <p><strong>' . $GLOBALS['strSQLQuery'] . ':</strong>' . "\n";
         if (strstr(strtolower($formatted_sql), 'select')) {
             // please show me help to the error on select
             echo PMA_showMySQLDocu('SQL-Syntax', 'SELECT');
         }
         if ($is_modify_link && isset($db)) {
             if (isset($table)) {
                 $doedit_goto = '<a href="tbl_properties.php?' . PMA_generate_common_url($db, $table) . '&amp;sql_query=' . urlencode($the_query) . '&amp;show_query=1">';
             } else {
                 $doedit_goto = '<a href="db_details.php?' . PMA_generate_common_url($db) . '&amp;sql_query=' . urlencode($the_query) . '&amp;show_query=1">';
             }
             if ($GLOBALS['cfg']['PropertiesIconic']) {
                 echo $doedit_goto . '<img class="icon" src=" ' . $GLOBALS['pmaThemeImage'] . 'b_edit.png" width="16" height="16" alt="' . $GLOBALS['strEdit'] . '" />' . '</a>';
             } else {
                 echo '    [' . $doedit_goto . $GLOBALS['strEdit'] . '</a>' . ']' . "\n";
             }
         }
         // end if
         echo '    </p>' . "\n" . '    <p>' . "\n" . '        ' . $formatted_sql . "\n" . '    </p>' . "\n";
     }
     // end if
     $tmp_mysql_error = '';
     // for saving the original $error_message
     if (!empty($error_message)) {
         $tmp_mysql_error = strtolower($error_message);
         // save the original $error_message
         $error_message = htmlspecialchars($error_message);
         $error_message = preg_replace("@((\r\n)|(\r)|(\n)){3,}@", "\n\n", $error_message);
     }
     // modified to show me the help on error-returns (Michael Keck)
     // (now error-messages-server)
     echo '<p>' . "\n" . '    <strong>' . $GLOBALS['strMySQLSaid'] . '</strong>' . PMA_showMySQLDocu('Error-messages-server', 'Error-messages-server') . "\n" . '</p>' . "\n";
     // The error message will be displayed within a CODE segment.
     // To preserve original formatting, but allow wordwrapping, we do a couple of replacements
     // Replace all non-single blanks with their HTML-counterpart
     $error_message = str_replace('  ', '&nbsp;&nbsp;', $error_message);
     // Replace TAB-characters with their HTML-counterpart
     $error_message = str_replace("\t", '&nbsp;&nbsp;&nbsp;&nbsp;', $error_message);
     // Replace linebreaks
     $error_message = nl2br($error_message);
     echo '<code>' . "\n" . $error_message . "\n" . '</code><br />' . "\n";
     // feature request #1036254:
     // Add a link by MySQL-Error #1062 - Duplicate entry
     // 2004-10-20 by mkkeck
     // 2005-01-17 modified by mkkeck bugfix
     if (substr($error_message, 1, 4) == '1062') {
         // get the duplicate entry
         // get table name
         // TODO: what would be the best delimiter, while avoiding
         // special characters that can become high-ascii after editing,
         // depending upon which editor is used by the developer?
         $error_table = array();
         if (preg_match('@ALTER\\s*TABLE\\s*\\`([^\\`]+)\\`@iu', $the_query, $error_table)) {
             $error_table = $error_table[1];
         } elseif (preg_match('@INSERT\\s*INTO\\s*\\`([^\\`]+)\\`@iu', $the_query, $error_table)) {
             $error_table = $error_table[1];
         } elseif (preg_match('@UPDATE\\s*\\`([^\\`]+)\\`@iu', $the_query, $error_table)) {
             $error_table = $error_table[1];
         } elseif (preg_match('@INSERT\\s*\\`([^\\`]+)\\`@iu', $the_query, $error_table)) {
             $error_table = $error_table[1];
         }
         // get fields
         $error_fields = array();
         if (preg_match('@\\(([^\\)]+)\\)@i', $the_query, $error_fields)) {
             $error_fields = explode(',', $error_fields[1]);
         } elseif (preg_match('@(`[^`]+`)\\s*=@i', $the_query, $error_fields)) {
             $error_fields = explode(',', $error_fields[1]);
         }
         if (is_array($error_table) || is_array($error_fields)) {
             // duplicate value
             $duplicate_value = array();
             preg_match('@\'([^\']+)\'@i', $tmp_mysql_error, $duplicate_value);
             $duplicate_value = $duplicate_value[1];
             $sql = '
                  SELECT *
                    FROM ' . PMA_backquote($error_table) . '
                   WHERE CONCAT_WS("-", ' . implode(', ', $error_fields) . ')
                         = "' . PMA_sqlAddslashes($duplicate_value) . '"
                ORDER BY ' . implode(', ', $error_fields);
             unset($error_table, $error_fields, $duplicate_value);
             echo '        <form method="post" action="import.php" style="padding: 0; margin: 0">' . "\n" . '            <input type="hidden" name="sql_query" value="' . htmlentities($sql) . '" />' . "\n" . '            ' . PMA_generate_common_hidden_inputs($db, $table) . "\n" . '            <input type="submit" name="submit" value="' . $GLOBALS['strBrowse'] . '" />' . "\n" . '        </form>' . "\n";
             unset($sql);
         }
     }
     // end of show duplicate entry
     echo '</div>';
     echo '<fieldset class="tblFooters">';
     if (!empty($back_url) && $exit) {
         $goto_back_url = '<a href="' . (strstr($back_url, '?') ? $back_url . '&amp;no_history=true' : $back_url . '?no_history=true') . '">';
         echo '[ ' . $goto_back_url . $GLOBALS['strBack'] . '</a> ]';
     }
     echo '    </fieldset>' . "\n\n";
     if ($exit) {
         require_once './libraries/footer.inc.php';
     }
 }
示例#9
0
 /**
  * Displays a MySQL error message in the right frame.
  *
  * @param   string   the error mesage
  * @param   string   the sql query that failed
  * @param   boolean  whether to show a "modify" link or not
  * @param   string   the "back" link url (full path is not required)
  * @param   boolean  EXIT the page?
  *
  * @global  array    the configuration array
  *
  * @access  public
  */
 function PMA_mysqlDie($error_message = '', $the_query = '', $is_modify_link = TRUE, $back_url = '', $exit = TRUE)
 {
     global $cfg, $table, $db, $sql_query;
     require_once './header.inc.php';
     if (!$error_message) {
         $error_message = PMA_mysql_error();
     }
     if (!$the_query && !empty($GLOBALS['sql_query'])) {
         $the_query = $GLOBALS['sql_query'];
     }
     // --- Added to solve bug #641765
     // Robbat2 - 12 January 2003, 9:46PM
     // Revised, Robbat2 - 13 Janurary 2003, 2:59PM
     if (!function_exists('PMA_SQP_isError') || PMA_SQP_isError()) {
         $formatted_sql = htmlspecialchars($the_query);
     } else {
         $formatted_sql = PMA_formatSql(PMA_SQP_parse($the_query), $the_query);
     }
     // ---
     echo '<p><b>' . $GLOBALS['strError'] . '</b></p>' . "\n";
     // if the config password is wrong, or the MySQL server does not
     // respond, do not show the query that would reveal the
     // username/password
     if (!empty($the_query) && !strstr($the_query, 'connect')) {
         // --- Added to solve bug #641765
         // Robbat2 - 12 January 2003, 9:46PM
         // Revised, Robbat2 - 13 Janurary 2003, 2:59PM
         if (function_exists('PMA_SQP_isError') && PMA_SQP_isError()) {
             echo PMA_SQP_getErrorString();
         }
         // ---
         echo '<p>' . "\n";
         echo '    ' . $GLOBALS['strSQLQuery'] . '&nbsp;:&nbsp;' . "\n";
         if ($is_modify_link && isset($db)) {
             echo '    [' . '<a href="db_details.php?' . PMA_generate_common_url($GLOBALS['db']) . '&amp;sql_query=' . urlencode($the_query) . '&amp;show_query=1">' . $GLOBALS['strEdit'] . '</a>' . ']' . "\n";
         }
         // end if
         echo '</p>' . "\n" . '<p>' . "\n" . '    ' . $formatted_sql . "\n" . '</p>' . "\n";
     }
     // end if
     if (!empty($error_message)) {
         $error_message = htmlspecialchars($error_message);
         $error_message = preg_replace("@((\r\n)|(\r)|(\n)){3,}@", "\n\n", $error_message);
     }
     echo '<p>' . "\n" . '    ' . $GLOBALS['strMySQLSaid'] . '<br />' . "\n" . '</p>' . "\n";
     // The error message will be displayed within a CODE segment.
     // To preserve original formatting, but allow wordwrapping, we do a couple of replacements
     // Replace all non-single blanks with their HTML-counterpart
     $error_message = str_replace('  ', '&nbsp;&nbsp;', $error_message);
     // Replace TAB-characters with their HTML-counterpart
     $error_message = str_replace("\t", '&nbsp;&nbsp;&nbsp;&nbsp;', $error_message);
     // Replace linebreaks
     $error_message = nl2br($error_message);
     echo '<code>' . "\n" . $error_message . "\n" . '</code><br /><br />' . "\n";
     echo PMA_showMySQLDocu('Error-returns', 'Error-returns');
     if (!empty($back_url) && $exit) {
         echo '&nbsp;&middot;&nbsp;[<a href="' . (strstr($back_url, '?') ? $back_url . '&amp;no_history=true' : $back_url . '?no_history=true') . '">' . $GLOBALS['strBack'] . '</a>]';
     }
     echo "\n";
     if ($exit) {
         require_once './footer.inc.php';
     }
 }
 /**
  * Displays a MySQL error message in the right frame.
  *
  * @param   string   the error mesage
  * @param   string   the sql query that failed
  * @param   boolean  whether to show a "modify" link or not
  * @param   string   the "back" link url (full path is not required)
  * @param   boolean  EXIT the page?
  *
  * @global  array    the configuration array
  *
  * @access  public
  */
 function PMA_mysqlDie($error_message = '', $the_query = '', $is_modify_link = TRUE, $back_url = '', $exit = TRUE)
 {
     global $cfg, $table, $db, $sql_query;
     require_once './header.inc.php';
     if (!$error_message) {
         $error_message = PMA_DBI_getError();
     }
     if (!$the_query && !empty($GLOBALS['sql_query'])) {
         $the_query = $GLOBALS['sql_query'];
     }
     // --- Added to solve bug #641765
     // Robbat2 - 12 January 2003, 9:46PM
     // Revised, Robbat2 - 13 Janurary 2003, 2:59PM
     if (!function_exists('PMA_SQP_isError') || PMA_SQP_isError()) {
         $formatted_sql = htmlspecialchars($the_query);
     } else {
         $formatted_sql = PMA_formatSql(PMA_SQP_parse($the_query), $the_query);
     }
     // ---
     echo "\n" . '<!-- PMA-SQL-ERROR -->' . "\n";
     echo '    <table border="0" cellpadding="2" cellspacing="1">' . '        <tr>' . "\n" . '            <th class="tblHeadError"><div class="errorhead">' . $GLOBALS['strError'] . '</div></th>' . "\n" . '        </tr>' . "\n" . '        <tr>' . "\n" . '            <td>';
     // if the config password is wrong, or the MySQL server does not
     // respond, do not show the query that would reveal the
     // username/password
     if (!empty($the_query) && !strstr($the_query, 'connect')) {
         // --- Added to solve bug #641765
         // Robbat2 - 12 January 2003, 9:46PM
         // Revised, Robbat2 - 13 Janurary 2003, 2:59PM
         if (function_exists('PMA_SQP_isError') && PMA_SQP_isError()) {
             echo PMA_SQP_getErrorString();
         }
         // ---
         // modified to show me the help on sql errors (Michael Keck)
         echo '<div class="tblWarn"><p>' . "\n";
         echo '    <b>' . $GLOBALS['strSQLQuery'] . ':</b>' . "\n";
         if (strstr(strtolower($formatted_sql), 'select')) {
             // please show me help to the error on select
             echo PMA_showMySQLDocu('Reference', 'SELECT');
         }
         if ($is_modify_link && isset($db)) {
             if (isset($table)) {
                 $doedit_goto = '<a href="tbl_properties.php?' . PMA_generate_common_url($db, $table) . '&amp;sql_query=' . urlencode($the_query) . '&amp;show_query=1">';
             } else {
                 $doedit_goto = '<a href="db_details.php?' . PMA_generate_common_url($db) . '&amp;sql_query=' . urlencode($the_query) . '&amp;show_query=1">';
             }
             if ($GLOBALS['cfg']['PropertiesIconic']) {
                 echo $doedit_goto . '<img src=" ' . $GLOBALS['pmaThemeImage'] . 'b_edit.png" width="16" height="16" border="0" hspace="2" align="absmiddle" alt="' . $GLOBALS['strEdit'] . '" />' . '</a>';
             } else {
                 echo '    [' . $doedit_goto . $GLOBALS['strEdit'] . '</a>' . ']' . "\n";
             }
         }
         // end if
         echo '</p>' . "\n" . '<p>' . "\n" . '    ' . $formatted_sql . "\n" . '</p></div>' . "\n";
     }
     // end if
     if (!empty($error_message)) {
         $error_message = htmlspecialchars($error_message);
         $error_message = preg_replace("@((\r\n)|(\r)|(\n)){3,}@", "\n\n", $error_message);
     }
     // modified to show me the help on error-returns (Michael Keck)
     echo '<div class="tblWarn"><p>' . "\n" . '    <b>' . $GLOBALS['strMySQLSaid'] . '</b>' . PMA_showMySQLDocu('Error-returns', 'Error-returns') . "\n" . '</p>' . "\n";
     // The error message will be displayed within a CODE segment.
     // To preserve original formatting, but allow wordwrapping, we do a couple of replacements
     // Replace all non-single blanks with their HTML-counterpart
     $error_message = str_replace('  ', '&nbsp;&nbsp;', $error_message);
     // Replace TAB-characters with their HTML-counterpart
     $error_message = str_replace("\t", '&nbsp;&nbsp;&nbsp;&nbsp;', $error_message);
     // Replace linebreaks
     $error_message = nl2br($error_message);
     echo '<code>' . "\n" . $error_message . "\n" . '</code><br /><br />' . "\n";
     echo '</div>';
     if (!empty($back_url) && $exit) {
         $goto_back_url = '<a href="' . (strstr($back_url, '?') ? $back_url . '&amp;no_history=true' : $back_url . '?no_history=true') . '">&nbsp;';
         echo '            </td> ' . "\n" . '        </tr>' . "\n" . '        <tr><td class="tblHeaders" align="center">';
         echo '[' . $goto_back_url . $GLOBALS['strBack'] . '&nbsp;</a>]';
     }
     echo '            </td>' . "\n" . '        </tr>' . "\n" . '    </table>' . "\n\n";
     if ($exit) {
         require_once './footer.inc.php';
     }
 }