aboutsummaryrefslogtreecommitdiff
path: root/srcs/phpmyadmin/libraries/classes/Util.php
diff options
context:
space:
mode:
Diffstat (limited to 'srcs/phpmyadmin/libraries/classes/Util.php')
-rw-r--r--srcs/phpmyadmin/libraries/classes/Util.php4975
1 files changed, 0 insertions, 4975 deletions
diff --git a/srcs/phpmyadmin/libraries/classes/Util.php b/srcs/phpmyadmin/libraries/classes/Util.php
deleted file mode 100644
index ad7bcdf..0000000
--- a/srcs/phpmyadmin/libraries/classes/Util.php
+++ /dev/null
@@ -1,4975 +0,0 @@
-<?php
-/* vim: set expandtab sw=4 ts=4 sts=4: */
-/**
- * Hold the PhpMyAdmin\Util class
- *
- * @package PhpMyAdmin
- */
-declare(strict_types=1);
-
-namespace PhpMyAdmin;
-
-use Closure;
-use PhpMyAdmin\Core;
-use PhpMyAdmin\DatabaseInterface;
-use PhpMyAdmin\FileListing;
-use PhpMyAdmin\Message;
-use PhpMyAdmin\Plugins\ImportPlugin;
-use PhpMyAdmin\Response;
-use PhpMyAdmin\Sanitize;
-use PhpMyAdmin\SqlParser\Context;
-use PhpMyAdmin\SqlParser\Lexer;
-use PhpMyAdmin\SqlParser\Parser;
-use PhpMyAdmin\SqlParser\Token;
-use PhpMyAdmin\SqlParser\Utils\Error as ParserError;
-use PhpMyAdmin\Template;
-use PhpMyAdmin\Url;
-use phpseclib\Crypt\Random;
-use stdClass;
-use Williamdes\MariaDBMySQLKBS\KBException;
-use Williamdes\MariaDBMySQLKBS\Search as KBSearch;
-
-/**
- * Misc functions used all over the scripts.
- *
- * @package PhpMyAdmin
- */
-class Util
-{
- /**
- * Checks whether configuration value tells to show icons.
- *
- * @param string $value Configuration option name
- *
- * @return boolean Whether to show icons.
- */
- public static function showIcons($value)
- {
- return in_array($GLOBALS['cfg'][$value], ['icons', 'both']);
- }
-
- /**
- * Checks whether configuration value tells to show text.
- *
- * @param string $value Configuration option name
- *
- * @return boolean Whether to show text.
- */
- public static function showText($value)
- {
- return in_array($GLOBALS['cfg'][$value], ['text', 'both']);
- }
-
- /**
- * Returns an HTML IMG tag for a particular icon from a theme,
- * which may be an actual file or an icon from a sprite.
- * This function takes into account the ActionLinksMode
- * configuration setting and wraps the image tag in a span tag.
- *
- * @param string $icon name of icon file
- * @param string $alternate alternate text
- * @param boolean $force_text whether to force alternate text to be displayed
- * @param boolean $menu_icon whether this icon is for the menu bar or not
- * @param string $control_param which directive controls the display
- *
- * @return string an html snippet
- */
- public static function getIcon(
- $icon,
- $alternate = '',
- $force_text = false,
- $menu_icon = false,
- $control_param = 'ActionLinksMode'
- ) {
- $include_icon = $include_text = false;
- if (self::showIcons($control_param)) {
- $include_icon = true;
- }
- if ($force_text
- || self::showText($control_param)
- ) {
- $include_text = true;
- }
- // Sometimes use a span (we rely on this in js/sql.js). But for menu bar
- // we don't need a span
- $button = $menu_icon ? '' : '<span class="nowrap">';
- if ($include_icon) {
- $button .= self::getImage($icon, $alternate);
- }
- if ($include_icon && $include_text) {
- $button .= '&nbsp;';
- }
- if ($include_text) {
- $button .= $alternate;
- }
- $button .= $menu_icon ? '' : '</span>';
-
- return $button;
- }
-
- /**
- * Returns an HTML IMG tag for a particular image from a theme
- *
- * The image name should match CSS class defined in icons.css.php
- *
- * @param string $image The name of the file to get
- * @param string $alternate Used to set 'alt' and 'title' attributes
- * of the image
- * @param array $attributes An associative array of other attributes
- *
- * @return string an html IMG tag
- */
- public static function getImage($image, $alternate = '', array $attributes = [])
- {
- $alternate = htmlspecialchars($alternate);
-
- if (isset($attributes['class'])) {
- $attributes['class'] = "icon ic_$image " . $attributes['class'];
- } else {
- $attributes['class'] = "icon ic_$image";
- }
-
- // set all other attributes
- $attr_str = '';
- foreach ($attributes as $key => $value) {
- if (! in_array($key, ['alt', 'title'])) {
- $attr_str .= " $key=\"$value\"";
- }
- }
-
- // override the alt attribute
- if (isset($attributes['alt'])) {
- $alt = $attributes['alt'];
- } else {
- $alt = $alternate;
- }
-
- // override the title attribute
- if (isset($attributes['title'])) {
- $title = $attributes['title'];
- } else {
- $title = $alternate;
- }
-
- // generate the IMG tag
- $template = '<img src="themes/dot.gif" title="%s" alt="%s"%s>';
- return sprintf($template, $title, $alt, $attr_str);
- }
-
- /**
- * Returns the formatted maximum size for an upload
- *
- * @param integer $max_upload_size the size
- *
- * @return string the message
- *
- * @access public
- */
- public static function getFormattedMaximumUploadSize($max_upload_size)
- {
- // I have to reduce the second parameter (sensitiveness) from 6 to 4
- // to avoid weird results like 512 kKib
- list($max_size, $max_unit) = self::formatByteDown($max_upload_size, 4);
- return '(' . sprintf(__('Max: %s%s'), $max_size, $max_unit) . ')';
- }
-
- /**
- * Generates a hidden field which should indicate to the browser
- * the maximum size for upload
- *
- * @param integer $max_size the size
- *
- * @return string the INPUT field
- *
- * @access public
- */
- public static function generateHiddenMaxFileSize($max_size)
- {
- return '<input type="hidden" name="MAX_FILE_SIZE" value="'
- . $max_size . '">';
- }
-
- /**
- * Add slashes before "_" and "%" characters for using them in MySQL
- * database, table and field names.
- * Note: This function does not escape backslashes!
- *
- * @param string $name the string to escape
- *
- * @return string the escaped string
- *
- * @access public
- */
- public static function escapeMysqlWildcards($name)
- {
- return strtr($name, ['_' => '\\_', '%' => '\\%']);
- } // end of the 'escapeMysqlWildcards()' function
-
- /**
- * removes slashes before "_" and "%" characters
- * Note: This function does not unescape backslashes!
- *
- * @param string $name the string to escape
- *
- * @return string the escaped string
- *
- * @access public
- */
- public static function unescapeMysqlWildcards($name)
- {
- return strtr($name, ['\\_' => '_', '\\%' => '%']);
- } // end of the 'unescapeMysqlWildcards()' function
-
- /**
- * removes quotes (',",`) from a quoted string
- *
- * checks if the string is quoted and removes this quotes
- *
- * @param string $quoted_string string to remove quotes from
- * @param string $quote type of quote to remove
- *
- * @return string unqoted string
- */
- public static function unQuote($quoted_string, $quote = null)
- {
- $quotes = [];
-
- if ($quote === null) {
- $quotes[] = '`';
- $quotes[] = '"';
- $quotes[] = "'";
- } else {
- $quotes[] = $quote;
- }
-
- foreach ($quotes as $quote) {
- if (mb_substr($quoted_string, 0, 1) === $quote
- && mb_substr($quoted_string, -1, 1) === $quote
- ) {
- $unquoted_string = mb_substr($quoted_string, 1, -1);
- // replace escaped quotes
- $unquoted_string = str_replace(
- $quote . $quote,
- $quote,
- $unquoted_string
- );
- return $unquoted_string;
- }
- }
-
- return $quoted_string;
- }
-
- /**
- * format sql strings
- *
- * @param string $sqlQuery raw SQL string
- * @param boolean $truncate truncate the query if it is too long
- *
- * @return string the formatted sql
- *
- * @global array $cfg the configuration array
- *
- * @access public
- * @todo move into PMA_Sql
- */
- public static function formatSql($sqlQuery, $truncate = false)
- {
- global $cfg;
-
- if ($truncate
- && mb_strlen($sqlQuery) > $cfg['MaxCharactersInDisplayedSQL']
- ) {
- $sqlQuery = mb_substr(
- $sqlQuery,
- 0,
- $cfg['MaxCharactersInDisplayedSQL']
- ) . '[...]';
- }
- return '<code class="sql"><pre>' . "\n"
- . htmlspecialchars($sqlQuery) . "\n"
- . '</pre></code>';
- } // end of the "formatSql()" function
-
- /**
- * Displays a button to copy content to clipboard
- *
- * @param string $text Text to copy to clipboard
- *
- * @return string the html link
- *
- * @access public
- */
- public static function showCopyToClipboard($text)
- {
- $open_link = ' <a href="#" class="copyQueryBtn" data-text="'
- . htmlspecialchars($text) . '">' . __('Copy') . '</a>';
- return $open_link;
- } // end of the 'showCopyToClipboard()' function
-
- /**
- * Displays a link to the documentation as an icon
- *
- * @param string $link documentation link
- * @param string $target optional link target
- * @param boolean $bbcode optional flag indicating whether to output bbcode
- *
- * @return string the html link
- *
- * @access public
- */
- public static function showDocLink($link, $target = 'documentation', $bbcode = false)
- {
- if ($bbcode) {
- return "[a@$link@$target][dochelpicon][/a]";
- }
-
- return '<a href="' . $link . '" target="' . $target . '">'
- . self::getImage('b_help', __('Documentation'))
- . '</a>';
- } // end of the 'showDocLink()' function
-
- /**
- * Get a URL link to the official MySQL documentation
- *
- * @param string $link contains name of page/anchor that is being linked
- * @param string $anchor anchor to page part
- *
- * @return string the URL link
- *
- * @access public
- */
- public static function getMySQLDocuURL($link, $anchor = '')
- {
- // Fixup for newly used names:
- $link = str_replace('_', '-', mb_strtolower($link));
-
- if (empty($link)) {
- $link = 'index';
- }
- $mysql = '5.5';
- $lang = 'en';
- if (isset($GLOBALS['dbi'])) {
- $serverVersion = $GLOBALS['dbi']->getVersion();
- if ($serverVersion >= 50700) {
- $mysql = '5.7';
- } elseif ($serverVersion >= 50600) {
- $mysql = '5.6';
- } elseif ($serverVersion >= 50500) {
- $mysql = '5.5';
- }
- }
- $url = 'https://dev.mysql.com/doc/refman/'
- . $mysql . '/' . $lang . '/' . $link . '.html';
- if (! empty($anchor)) {
- $url .= '#' . $anchor;
- }
-
- return Core::linkURL($url);
- }
-
- /**
- * Get a link to variable documentation
- *
- * @param string $name The variable name
- * @param boolean $useMariaDB Use only MariaDB documentation
- * @param string $text (optional) The text for the link
- * @return string link or empty string
- */
- public static function linkToVarDocumentation(
- string $name,
- bool $useMariaDB = false,
- string $text = null
- ): string {
- $html = '';
- try {
- $type = KBSearch::MYSQL;
- if ($useMariaDB) {
- $type = KBSearch::MARIADB;
- }
- $docLink = KBSearch::getByName($name, $type);
- $html = Util::showMySQLDocu(
- $name,
- false,
- $docLink,
- $text
- );
- } catch (KBException $e) {
- unset($e);// phpstan workaround
- }
- return $html;
- }
-
- /**
- * Displays a link to the official MySQL documentation
- *
- * @param string $link contains name of page/anchor that is being linked
- * @param bool $bigIcon whether to use big icon (like in left frame)
- * @param string|null $url href attribute
- * @param string|null $text text of link
- * @param string $anchor anchor to page part
- *
- * @return string the html link
- *
- * @access public
- */
- public static function showMySQLDocu(
- $link,
- bool $bigIcon = false,
- $url = null,
- $text = null,
- $anchor = ''
- ): string {
- if ($url === null) {
- $url = self::getMySQLDocuURL($link, $anchor);
- }
- $openLink = '<a href="' . htmlspecialchars($url) . '" target="mysql_doc">';
- $closeLink = '</a>';
- $html = '';
-
- if ($bigIcon) {
- $html = $openLink .
- self::getImage('b_sqlhelp', __('Documentation'))
- . $closeLink;
- } elseif ($text !== null) {
- $html = $openLink . $text . $closeLink;
- } else {
- $html = self::showDocLink($url, 'mysql_doc');
- }
-
- return $html;
- } // end of the 'showMySQLDocu()' function
-
- /**
- * Returns link to documentation.
- *
- * @param string $page Page in documentation
- * @param string $anchor Optional anchor in page
- *
- * @return string URL
- */
- public static function getDocuLink($page, $anchor = '')
- {
- /* Construct base URL */
- $url = $page . '.html';
- if (! empty($anchor)) {
- $url .= '#' . $anchor;
- }
-
- /* Check if we have built local documentation, however
- * provide consistent URL for testsuite
- */
- if (! defined('TESTSUITE') && @file_exists(ROOT_PATH . 'doc/html/index.html')) {
- return 'doc/html/' . $url;
- }
-
- return Core::linkURL('https://docs.phpmyadmin.net/en/latest/' . $url);
- }
-
- /**
- * Displays a link to the phpMyAdmin documentation
- *
- * @param string $page Page in documentation
- * @param string $anchor Optional anchor in page
- * @param boolean $bbcode Optional flag indicating whether to output bbcode
- *
- * @return string the html link
- *
- * @access public
- */
- public static function showDocu($page, $anchor = '', $bbcode = false)
- {
- return self::showDocLink(self::getDocuLink($page, $anchor), 'documentation', $bbcode);
- } // end of the 'showDocu()' function
-
- /**
- * Displays a link to the PHP documentation
- *
- * @param string $target anchor in documentation
- *
- * @return string the html link
- *
- * @access public
- */
- public static function showPHPDocu($target)
- {
- $url = Core::getPHPDocLink($target);
-
- return self::showDocLink($url);
- } // end of the 'showPHPDocu()' function
-
- /**
- * Returns HTML code for a tooltip
- *
- * @param string $message the message for the tooltip
- *
- * @return string
- *
- * @access public
- */
- public static function showHint($message)
- {
- if ($GLOBALS['cfg']['ShowHint']) {
- $classClause = ' class="pma_hint"';
- } else {
- $classClause = '';
- }
- return '<span' . $classClause . '>'
- . self::getImage('b_help')
- . '<span class="hide">' . $message . '</span>'
- . '</span>';
- }
-
- /**
- * Displays a MySQL error message in the main panel when $exit is true.
- * Returns the error message otherwise.
- *
- * @param string|bool $server_msg Server's error message.
- * @param string $sql_query The SQL query that failed.
- * @param bool $is_modify_link Whether to show a "modify" link or not.
- * @param string $back_url URL for the "back" link (full path is
- * not required).
- * @param bool $exit Whether execution should be stopped or
- * the error message should be returned.
- *
- * @return string
- *
- * @global string $table The current table.
- * @global string $db The current database.
- *
- * @access public
- */
- public static function mysqlDie(
- $server_msg = '',
- $sql_query = '',
- $is_modify_link = true,
- $back_url = '',
- $exit = true
- ) {
- global $table, $db;
-
- /**
- * Error message to be built.
- * @var string $error_msg
- */
- $error_msg = '';
-
- // Checking for any server errors.
- if (empty($server_msg)) {
- $server_msg = $GLOBALS['dbi']->getError();
- }
-
- // Finding the query that failed, if not specified.
- if (empty($sql_query) && ! empty($GLOBALS['sql_query'])) {
- $sql_query = $GLOBALS['sql_query'];
- }
- $sql_query = trim($sql_query);
-
- /**
- * The lexer used for analysis.
- * @var Lexer $lexer
- */
- $lexer = new Lexer($sql_query);
-
- /**
- * The parser used for analysis.
- * @var Parser $parser
- */
- $parser = new Parser($lexer->list);
-
- /**
- * The errors found by the lexer and the parser.
- * @var array $errors
- */
- $errors = ParserError::get([$lexer, $parser]);
-
- if (empty($sql_query)) {
- $formatted_sql = '';
- } elseif (count($errors)) {
- $formatted_sql = htmlspecialchars($sql_query);
- } else {
- $formatted_sql = self::formatSql($sql_query, true);
- }
-
- $error_msg .= '<div class="error"><h1>' . __('Error') . '</h1>';
-
- // For security reasons, if the MySQL refuses the connection, the query
- // is hidden so no details are revealed.
- if (! empty($sql_query) && ! mb_strstr($sql_query, 'connect')) {
- // Static analysis errors.
- if (! empty($errors)) {
- $error_msg .= '<p><strong>' . __('Static analysis:')
- . '</strong></p>';
- $error_msg .= '<p>' . sprintf(
- __('%d errors were found during analysis.'),
- count($errors)
- ) . '</p>';
- $error_msg .= '<p><ol>';
- $error_msg .= implode(
- ParserError::format(
- $errors,
- '<li>%2$s (near "%4$s" at position %5$d)</li>'
- )
- );
- $error_msg .= '</ol></p>';
- }
-
- // Display the SQL query and link to MySQL documentation.
- $error_msg .= '<p><strong>' . __('SQL query:') . '</strong>' . self::showCopyToClipboard($sql_query) . "\n";
- $formattedSqlToLower = mb_strtolower($formatted_sql);
-
- // TODO: Show documentation for all statement types.
- if (mb_strstr($formattedSqlToLower, 'select')) {
- // please show me help to the error on select
- $error_msg .= self::showMySQLDocu('SELECT');
- }
-
- if ($is_modify_link) {
- $_url_params = [
- 'sql_query' => $sql_query,
- 'show_query' => 1,
- ];
- if (strlen($table) > 0) {
- $_url_params['db'] = $db;
- $_url_params['table'] = $table;
- $doedit_goto = '<a href="tbl_sql.php'
- . Url::getCommon($_url_params) . '">';
- } elseif (strlen($db) > 0) {
- $_url_params['db'] = $db;
- $doedit_goto = '<a href="db_sql.php'
- . Url::getCommon($_url_params) . '">';
- } else {
- $doedit_goto = '<a href="server_sql.php'
- . Url::getCommon($_url_params) . '">';
- }
-
- $error_msg .= $doedit_goto
- . self::getIcon('b_edit', __('Edit'))
- . '</a>';
- }
-
- $error_msg .= ' </p>' . "\n"
- . '<p>' . "\n"
- . $formatted_sql . "\n"
- . '</p>' . "\n";
- }
-
- // Display server's error.
- if (! empty($server_msg)) {
- $server_msg = preg_replace(
- "@((\015\012)|(\015)|(\012)){3,}@",
- "\n\n",
- $server_msg
- );
-
- // Adds a link to MySQL documentation.
- $error_msg .= '<p>' . "\n"
- . ' <strong>' . __('MySQL said: ') . '</strong>'
- . self::showMySQLDocu('Error-messages-server')
- . "\n"
- . '</p>' . "\n";
-
- // The error message will be displayed within a CODE segment.
- // To preserve original formatting, but allow word-wrapping,
- // a couple of replacements are done.
- // All non-single blanks and TAB-characters are replaced with their
- // HTML-counterpart
- $server_msg = str_replace(
- [
- ' ',
- "\t",
- ],
- [
- '&nbsp;&nbsp;',
- '&nbsp;&nbsp;&nbsp;&nbsp;',
- ],
- $server_msg
- );
-
- // Replace line breaks
- $server_msg = nl2br($server_msg);
-
- $error_msg .= '<code>' . $server_msg . '</code><br>';
- }
-
- $error_msg .= '</div>';
- $_SESSION['Import_message']['message'] = $error_msg;
-
- if (! $exit) {
- return $error_msg;
- }
-
- /**
- * If this is an AJAX request, there is no "Back" link and
- * `Response()` is used to send the response.
- */
- $response = Response::getInstance();
- if ($response->isAjax()) {
- $response->setRequestStatus(false);
- $response->addJSON('message', $error_msg);
- exit;
- }
-
- if (! empty($back_url)) {
- if (mb_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 .= '<fieldset class="tblFooters">'
- . '[ <a href="' . $back_url . '">' . __('Back') . '</a> ]'
- . '</fieldset>' . "\n\n";
- }
-
- exit($error_msg);
- }
-
- /**
- * Check the correct row count
- *
- * @param string $db the db name
- * @param array $table the table infos
- *
- * @return int the possibly modified row count
- *
- */
- private static function _checkRowCount($db, array $table)
- {
- $rowCount = 0;
-
- if ($table['Rows'] === null) {
- // Do not check exact row count here,
- // if row count is invalid possibly the table is defect
- // and this would break the navigation panel;
- // but we can check row count if this is a view or the
- // information_schema database
- // since Table::countRecords() returns a limited row count
- // in this case.
-
- // set this because Table::countRecords() can use it
- $tbl_is_view = $table['TABLE_TYPE'] == 'VIEW';
-
- if ($tbl_is_view || $GLOBALS['dbi']->isSystemSchema($db)) {
- $rowCount = $GLOBALS['dbi']
- ->getTable($db, $table['Name'])
- ->countRecords();
- }
- }
- return $rowCount;
- }
-
- /**
- * returns array with tables of given db with extended information and grouped
- *
- * @param string $db name of db
- * @param string $tables name of tables
- * @param integer $limit_offset list offset
- * @param int|bool $limit_count max tables to return
- *
- * @return array (recursive) grouped table list
- */
- public static function getTableList(
- $db,
- $tables = null,
- $limit_offset = 0,
- $limit_count = false
- ) {
- $sep = $GLOBALS['cfg']['NavigationTreeTableSeparator'];
-
- if ($tables === null) {
- $tables = $GLOBALS['dbi']->getTablesFull(
- $db,
- '',
- false,
- $limit_offset,
- $limit_count
- );
- if ($GLOBALS['cfg']['NaturalOrder']) {
- uksort($tables, 'strnatcasecmp');
- }
- }
-
- if (count($tables) < 1) {
- return $tables;
- }
-
- $default = [
- 'Name' => '',
- 'Rows' => 0,
- 'Comment' => '',
- 'disp_name' => '',
- ];
-
- $table_groups = [];
-
- foreach ($tables as $table_name => $table) {
- $table['Rows'] = self::_checkRowCount($db, $table);
-
- // in $group we save the reference to the place in $table_groups
- // where to store the table info
- if ($GLOBALS['cfg']['NavigationTreeEnableGrouping']
- && $sep && mb_strstr($table_name, $sep)
- ) {
- $parts = explode($sep, $table_name);
-
- $group =& $table_groups;
- $i = 0;
- $group_name_full = '';
- $parts_cnt = count($parts) - 1;
-
- while (($i < $parts_cnt)
- && ($i < $GLOBALS['cfg']['NavigationTreeTableLevel'])
- ) {
- $group_name = $parts[$i] . $sep;
- $group_name_full .= $group_name;
-
- if (! isset($group[$group_name])) {
- $group[$group_name] = [];
- $group[$group_name]['is' . $sep . 'group'] = true;
- $group[$group_name]['tab' . $sep . 'count'] = 1;
- $group[$group_name]['tab' . $sep . 'group']
- = $group_name_full;
- } elseif (! isset($group[$group_name]['is' . $sep . 'group'])) {
- $table = $group[$group_name];
- $group[$group_name] = [];
- $group[$group_name][$group_name] = $table;
- $group[$group_name]['is' . $sep . 'group'] = true;
- $group[$group_name]['tab' . $sep . 'count'] = 1;
- $group[$group_name]['tab' . $sep . 'group']
- = $group_name_full;
- } else {
- $group[$group_name]['tab' . $sep . 'count']++;
- }
-
- $group =& $group[$group_name];
- $i++;
- }
- } else {
- if (! isset($table_groups[$table_name])) {
- $table_groups[$table_name] = [];
- }
- $group =& $table_groups;
- }
-
- $table['disp_name'] = $table['Name'];
- $group[$table_name] = array_merge($default, $table);
- }
-
- return $table_groups;
- }
-
- /* ----------------------- Set of misc functions ----------------------- */
-
- /**
- * Adds backquotes on both sides of a database, table or field name.
- * and escapes backquotes inside the name with another backquote
- *
- * example:
- * <code>
- * echo backquote('owner`s db'); // `owner``s db`
- *
- * </code>
- *
- * @param mixed $a_name the database, table or field name to "backquote"
- * or array of it
- * @param boolean $do_it a flag to bypass this function (used by dump
- * functions)
- *
- * @return mixed the "backquoted" database, table or field name
- *
- * @access public
- */
- public static function backquote($a_name, $do_it = true)
- {
- if (is_array($a_name)) {
- foreach ($a_name as &$data) {
- $data = self::backquote($data, $do_it);
- }
- return $a_name;
- }
-
- if (! $do_it) {
- if (! (Context::isKeyword($a_name) & Token::FLAG_KEYWORD_RESERVED)
- ) {
- return $a_name;
- }
- }
-
- // '0' is also empty for php :-(
- if (strlen((string) $a_name) > 0 && $a_name !== '*') {
- return '`' . str_replace('`', '``', (string) $a_name) . '`';
- }
-
- return $a_name;
- } // end of the 'backquote()' function
-
- /**
- * Adds backquotes on both sides of a database, table or field name.
- * in compatibility mode
- *
- * example:
- * <code>
- * echo backquoteCompat('owner`s db'); // `owner``s db`
- *
- * </code>
- *
- * @param mixed $a_name the database, table or field name to
- * "backquote" or array of it
- * @param string $compatibility string compatibility mode (used by dump
- * functions)
- * @param boolean $do_it a flag to bypass this function (used by dump
- * functions)
- *
- * @return mixed the "backquoted" database, table or field name
- *
- * @access public
- */
- public static function backquoteCompat(
- $a_name,
- $compatibility = 'MSSQL',
- $do_it = true
- ) {
- if (is_array($a_name)) {
- foreach ($a_name as &$data) {
- $data = self::backquoteCompat($data, $compatibility, $do_it);
- }
- return $a_name;
- }
-
- if (! $do_it) {
- if (! Context::isKeyword($a_name)) {
- return $a_name;
- }
- }
-
- // @todo add more compatibility cases (ORACLE for example)
- switch ($compatibility) {
- case 'MSSQL':
- $quote = '"';
- break;
- default:
- $quote = "`";
- break;
- }
-
- // '0' is also empty for php :-(
- if (strlen((string) $a_name) > 0 && $a_name !== '*') {
- return $quote . $a_name . $quote;
- }
-
- return $a_name;
- } // end of the 'backquoteCompat()' function
-
- /**
- * Prepare the message and the query
- * usually the message is the result of the query executed
- *
- * @param Message|string $message the message to display
- * @param string $sql_query the query to display
- * @param string $type the type (level) of the message
- *
- * @return string
- *
- * @access public
- */
- public static function getMessage(
- $message,
- $sql_query = null,
- $type = 'notice'
- ) {
- global $cfg;
- $template = new Template();
- $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 = '';
- }
- }
-
- $render_sql = $cfg['ShowSQL'] == true && ! empty($sql_query) && $sql_query !== ';';
-
- if (isset($GLOBALS['using_bookmark_message'])) {
- $retval .= $GLOBALS['using_bookmark_message']->getDisplay();
- unset($GLOBALS['using_bookmark_message']);
- }
-
- if ($render_sql) {
- $retval .= '<div class="result_query">' . "\n";
- }
-
- if ($message instanceof Message) {
- if (isset($GLOBALS['special_message'])) {
- $message->addText($GLOBALS['special_message']);
- unset($GLOBALS['special_message']);
- }
- $retval .= $message->getDisplay();
- } else {
- $retval .= '<div class="' . $type . '">';
- $retval .= Sanitize::sanitizeMessage($message);
- if (isset($GLOBALS['special_message'])) {
- $retval .= Sanitize::sanitizeMessage($GLOBALS['special_message']);
- unset($GLOBALS['special_message']);
- }
- $retval .= '</div>';
- }
-
- if ($render_sql) {
- $query_too_big = false;
-
- $queryLength = mb_strlen($sql_query);
- if ($queryLength > $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;
- $query_base = mb_substr(
- $sql_query,
- 0,
- $cfg['MaxCharactersInDisplayedSQL']
- ) . '[...]';
- } else {
- $query_base = $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($query_base));
- $query_base = preg_replace(
- '/((\015\012)|(\015)|(\012))/',
- $new_line,
- $query_base
- );
- $query_base = '<code class="php"><pre>' . "\n"
- . '$sql = "' . $query_base . '";' . "\n"
- . '</pre></code>';
- } elseif ($query_too_big) {
- $query_base = '<code class="sql"><pre>' . "\n" .
- htmlspecialchars($query_base) .
- '</pre></code>';
- } else {
- $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 = [];
- if (! isset($GLOBALS['db'])) {
- $GLOBALS['db'] = '';
- }
- if (strlen($GLOBALS['db']) > 0) {
- $url_params['db'] = $GLOBALS['db'];
- if (strlen($GLOBALS['table']) > 0) {
- $url_params['table'] = $GLOBALS['table'];
- $edit_link = 'tbl_sql.php';
- } else {
- $edit_link = 'db_sql.php';
- }
- } else {
- $edit_link = 'server_sql.php';
- }
-
-