PageRenderTime 61ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/phpMyAdmin/libraries/common.lib.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 3882 lines | 2423 code | 309 blank | 1150 comment | 576 complexity | 9d04b0512a995647389ad91712d76ecf MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Misc functions used all over the scripts.
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. /**
  9. * Detects which function to use for PMA_pow.
  10. *
  11. * @return string Function name.
  12. */
  13. function PMA_detect_pow()
  14. {
  15. if (function_exists('bcpow')) {
  16. // BCMath Arbitrary Precision Mathematics Function
  17. return 'bcpow';
  18. } elseif (function_exists('gmp_pow')) {
  19. // GMP Function
  20. return 'gmp_pow';
  21. } else {
  22. // PHP function
  23. return 'pow';
  24. }
  25. }
  26. /**
  27. * Exponential expression / raise number into power
  28. *
  29. * @param string $base base to raise
  30. * @param string $exp exponent to use
  31. * @param mixed $use_function pow function to use, or false for auto-detect
  32. *
  33. * @return mixed string or float
  34. */
  35. function PMA_pow($base, $exp, $use_function = false)
  36. {
  37. static $pow_function = null;
  38. if (null == $pow_function) {
  39. $pow_function = PMA_detect_pow();
  40. }
  41. if (! $use_function) {
  42. $use_function = $pow_function;
  43. }
  44. if ($exp < 0 && 'pow' != $use_function) {
  45. return false;
  46. }
  47. switch ($use_function) {
  48. case 'bcpow' :
  49. // bcscale() needed for testing PMA_pow() with base values < 1
  50. bcscale(10);
  51. $pow = bcpow($base, $exp);
  52. break;
  53. case 'gmp_pow' :
  54. $pow = gmp_strval(gmp_pow($base, $exp));
  55. break;
  56. case 'pow' :
  57. $base = (float) $base;
  58. $exp = (int) $exp;
  59. $pow = pow($base, $exp);
  60. break;
  61. default:
  62. $pow = $use_function($base, $exp);
  63. }
  64. return $pow;
  65. }
  66. /**
  67. * Returns an HTML IMG tag for a particular icon from a theme,
  68. * which may be an actual file or an icon from a sprite.
  69. * This function takes into account the PropertiesIconic
  70. * configuration setting and wraps the image tag in a span tag.
  71. *
  72. * @param string $icon name of icon file
  73. * @param string $alternate alternate text
  74. * @param boolean $force_text whether to force alternate text to be displayed
  75. *
  76. * @return string an html snippet
  77. */
  78. function PMA_getIcon($icon, $alternate = '', $force_text = false)
  79. {
  80. // $cfg['PropertiesIconic'] is true or both
  81. $include_icon = ($GLOBALS['cfg']['PropertiesIconic'] !== false);
  82. // $cfg['PropertiesIconic'] is false or both
  83. // OR we have no $include_icon
  84. $include_text = ($force_text || true !== $GLOBALS['cfg']['PropertiesIconic']);
  85. // Always use a span (we rely on this in js/sql.js)
  86. $button = '<span class="nowrap">';
  87. if ($include_icon) {
  88. $button .= PMA_getImage($icon, $alternate);
  89. }
  90. if ($include_icon && $include_text) {
  91. $button .= ' ';
  92. }
  93. if ($include_text) {
  94. $button .= $alternate;
  95. }
  96. $button .= '</span>';
  97. return $button;
  98. }
  99. /**
  100. * Returns an HTML IMG tag for a particular image from a theme,
  101. * which may be an actual file or an icon from a sprite
  102. *
  103. * @param string $image The name of the file to get
  104. * @param string $alternate Used to set 'alt' and 'title' attributes of the image
  105. * @param array $attributes An associative array of other attributes
  106. *
  107. * @return string an html IMG tag
  108. */
  109. function PMA_getImage($image, $alternate = '', $attributes = array())
  110. {
  111. static $sprites; // cached list of available sprites (if any)
  112. $url = '';
  113. $is_sprite = false;
  114. $alternate = htmlspecialchars($alternate);
  115. // If it's the first time this function is called
  116. if (! isset($sprites)) {
  117. // Try to load the list of sprites
  118. if (is_readable($_SESSION['PMA_Theme']->getPath() . '/sprites.lib.php')) {
  119. include_once $_SESSION['PMA_Theme']->getPath() . '/sprites.lib.php';
  120. $sprites = PMA_sprites();
  121. } else {
  122. // No sprites are available for this theme
  123. $sprites = array();
  124. }
  125. }
  126. // Check if we have the requested image as a sprite
  127. // and set $url accordingly
  128. $class = str_replace(array('.gif','.png'), '', $image);
  129. if (array_key_exists($class, $sprites)) {
  130. $is_sprite = true;
  131. $url = 'themes/dot.gif';
  132. } else {
  133. $url = $GLOBALS['pmaThemeImage'] . $image;
  134. }
  135. // set class attribute
  136. if ($is_sprite) {
  137. if (isset($attributes['class'])) {
  138. $attributes['class'] = "icon ic_$class " . $attributes['class'];
  139. } else {
  140. $attributes['class'] = "icon ic_$class";
  141. }
  142. }
  143. // set all other attributes
  144. $attr_str = '';
  145. foreach ($attributes as $key => $value) {
  146. if (! in_array($key, array('alt', 'title'))) {
  147. $attr_str .= " $key=\"$value\"";
  148. }
  149. }
  150. // override the alt attribute
  151. if (isset($attributes['alt'])) {
  152. $alt = $attributes['alt'];
  153. } else {
  154. $alt = $alternate;
  155. }
  156. // override the title attribute
  157. if (isset($attributes['title'])) {
  158. $title = $attributes['title'];
  159. } else {
  160. $title = $alternate;
  161. }
  162. // generate the IMG tag
  163. $template = '<img src="%s" title="%s" alt="%s"%s />';
  164. $retval = sprintf($template, $url, $title, $alt, $attr_str);
  165. return $retval;
  166. }
  167. /**
  168. * Displays the maximum size for an upload
  169. *
  170. * @param integer $max_upload_size the size
  171. *
  172. * @return string the message
  173. *
  174. * @access public
  175. */
  176. function PMA_displayMaximumUploadSize($max_upload_size)
  177. {
  178. // I have to reduce the second parameter (sensitiveness) from 6 to 4
  179. // to avoid weird results like 512 kKib
  180. list($max_size, $max_unit) = PMA_formatByteDown($max_upload_size, 4);
  181. return '(' . sprintf(__('Max: %s%s'), $max_size, $max_unit) . ')';
  182. }
  183. /**
  184. * Generates a hidden field which should indicate to the browser
  185. * the maximum size for upload
  186. *
  187. * @param integer $max_size the size
  188. *
  189. * @return string the INPUT field
  190. *
  191. * @access public
  192. */
  193. function PMA_generateHiddenMaxFileSize($max_size)
  194. {
  195. return '<input type="hidden" name="MAX_FILE_SIZE" value="' .$max_size . '" />';
  196. }
  197. /**
  198. * Add slashes before "'" and "\" characters so a value containing them can
  199. * be used in a sql comparison.
  200. *
  201. * @param string $a_string the string to slash
  202. * @param bool $is_like whether the string will be used in a 'LIKE' clause
  203. * (it then requires two more escaped sequences) or not
  204. * @param bool $crlf whether to treat cr/lfs as escape-worthy entities
  205. * (converts \n to \\n, \r to \\r)
  206. * @param bool $php_code whether this function is used as part of the
  207. * "Create PHP code" dialog
  208. *
  209. * @return string the slashed string
  210. *
  211. * @access public
  212. */
  213. function PMA_sqlAddSlashes($a_string = '', $is_like = false, $crlf = false, $php_code = false)
  214. {
  215. if ($is_like) {
  216. $a_string = str_replace('\\', '\\\\\\\\', $a_string);
  217. } else {
  218. $a_string = str_replace('\\', '\\\\', $a_string);
  219. }
  220. if ($crlf) {
  221. $a_string = strtr(
  222. $a_string,
  223. array("\n" => '\n', "\r" => '\r', "\t" => '\t')
  224. );
  225. }
  226. if ($php_code) {
  227. $a_string = str_replace('\'', '\\\'', $a_string);
  228. } else {
  229. $a_string = str_replace('\'', '\'\'', $a_string);
  230. }
  231. return $a_string;
  232. } // end of the 'PMA_sqlAddSlashes()' function
  233. /**
  234. * Add slashes before "_" and "%" characters for using them in MySQL
  235. * database, table and field names.
  236. * Note: This function does not escape backslashes!
  237. *
  238. * @param string $name the string to escape
  239. *
  240. * @return string the escaped string
  241. *
  242. * @access public
  243. */
  244. function PMA_escape_mysql_wildcards($name)
  245. {
  246. return strtr($name, array('_' => '\\_', '%' => '\\%'));
  247. } // end of the 'PMA_escape_mysql_wildcards()' function
  248. /**
  249. * removes slashes before "_" and "%" characters
  250. * Note: This function does not unescape backslashes!
  251. *
  252. * @param string $name the string to escape
  253. *
  254. * @return string the escaped string
  255. *
  256. * @access public
  257. */
  258. function PMA_unescape_mysql_wildcards($name)
  259. {
  260. return strtr($name, array('\\_' => '_', '\\%' => '%'));
  261. } // end of the 'PMA_unescape_mysql_wildcards()' function
  262. /**
  263. * removes quotes (',",`) from a quoted string
  264. *
  265. * checks if the sting is quoted and removes this quotes
  266. *
  267. * @param string $quoted_string string to remove quotes from
  268. * @param string $quote type of quote to remove
  269. *
  270. * @return string unqoted string
  271. */
  272. function PMA_unQuote($quoted_string, $quote = null)
  273. {
  274. $quotes = array();
  275. if (null === $quote) {
  276. $quotes[] = '`';
  277. $quotes[] = '"';
  278. $quotes[] = "'";
  279. } else {
  280. $quotes[] = $quote;
  281. }
  282. foreach ($quotes as $quote) {
  283. if (substr($quoted_string, 0, 1) === $quote
  284. && substr($quoted_string, -1, 1) === $quote
  285. ) {
  286. $unquoted_string = substr($quoted_string, 1, -1);
  287. // replace escaped quotes
  288. $unquoted_string = str_replace(
  289. $quote . $quote,
  290. $quote,
  291. $unquoted_string
  292. );
  293. return $unquoted_string;
  294. }
  295. }
  296. return $quoted_string;
  297. }
  298. /**
  299. * format sql strings
  300. *
  301. * @param mixed $parsed_sql pre-parsed SQL structure
  302. * @param string $unparsed_sql raw SQL string
  303. *
  304. * @return string the formatted sql
  305. *
  306. * @global array the configuration array
  307. * @global boolean whether the current statement is a multiple one or not
  308. *
  309. * @access public
  310. * @todo move into PMA_Sql
  311. */
  312. function PMA_formatSql($parsed_sql, $unparsed_sql = '')
  313. {
  314. global $cfg;
  315. // Check that we actually have a valid set of parsed data
  316. // well, not quite
  317. // first check for the SQL parser having hit an error
  318. if (PMA_SQP_isError()) {
  319. return htmlspecialchars($parsed_sql['raw']);
  320. }
  321. // then check for an array
  322. if (! is_array($parsed_sql)) {
  323. // We don't so just return the input directly
  324. // This is intended to be used for when the SQL Parser is turned off
  325. $formatted_sql = "<pre>\n";
  326. if ($cfg['SQP']['fmtType'] == 'none' && $unparsed_sql != '') {
  327. $formatted_sql .= $unparsed_sql;
  328. } else {
  329. $formatted_sql .= $parsed_sql;
  330. }
  331. $formatted_sql .= "\n</pre>";
  332. return $formatted_sql;
  333. }
  334. $formatted_sql = '';
  335. switch ($cfg['SQP']['fmtType']) {
  336. case 'none':
  337. if ($unparsed_sql != '') {
  338. $formatted_sql = '<span class="inner_sql"><pre>' . "\n"
  339. . PMA_SQP_formatNone(array('raw' => $unparsed_sql)) . "\n"
  340. . '</pre></span>';
  341. } else {
  342. $formatted_sql = PMA_SQP_formatNone($parsed_sql);
  343. }
  344. break;
  345. case 'html':
  346. $formatted_sql = PMA_SQP_formatHtml($parsed_sql, 'color');
  347. break;
  348. case 'text':
  349. $formatted_sql = PMA_SQP_formatHtml($parsed_sql, 'text');
  350. break;
  351. default:
  352. break;
  353. } // end switch
  354. return $formatted_sql;
  355. } // end of the "PMA_formatSql()" function
  356. /**
  357. * Displays a link to the official MySQL documentation
  358. *
  359. * @param string $chapter chapter of "HTML, one page per chapter" documentation
  360. * @param string $link contains name of page/anchor that is being linked
  361. * @param bool $big_icon whether to use big icon (like in left frame)
  362. * @param string $anchor anchor to page part
  363. * @param bool $just_open whether only the opening <a> tag should be returned
  364. *
  365. * @return string the html link
  366. *
  367. * @access public
  368. */
  369. function PMA_showMySQLDocu($chapter, $link, $big_icon = false, $anchor = '', $just_open = false)
  370. {
  371. global $cfg;
  372. if ($cfg['MySQLManualType'] == 'none' || empty($cfg['MySQLManualBase'])) {
  373. return '';
  374. }
  375. // Fixup for newly used names:
  376. $chapter = str_replace('_', '-', strtolower($chapter));
  377. $link = str_replace('_', '-', strtolower($link));
  378. switch ($cfg['MySQLManualType']) {
  379. case 'chapters':
  380. if (empty($chapter)) {
  381. $chapter = 'index';
  382. }
  383. if (empty($anchor)) {
  384. $anchor = $link;
  385. }
  386. $url = $cfg['MySQLManualBase'] . '/' . $chapter . '.html#' . $anchor;
  387. break;
  388. case 'big':
  389. if (empty($anchor)) {
  390. $anchor = $link;
  391. }
  392. $url = $cfg['MySQLManualBase'] . '#' . $anchor;
  393. break;
  394. case 'searchable':
  395. if (empty($link)) {
  396. $link = 'index';
  397. }
  398. $url = $cfg['MySQLManualBase'] . '/' . $link . '.html';
  399. if (!empty($anchor)) {
  400. $url .= '#' . $anchor;
  401. }
  402. break;
  403. case 'viewable':
  404. default:
  405. if (empty($link)) {
  406. $link = 'index';
  407. }
  408. $mysql = '5.5';
  409. $lang = 'en';
  410. if (defined('PMA_MYSQL_INT_VERSION')) {
  411. if (PMA_MYSQL_INT_VERSION >= 50600) {
  412. $mysql = '5.6';
  413. } else if (PMA_MYSQL_INT_VERSION >= 50500) {
  414. $mysql = '5.5';
  415. } else if (PMA_MYSQL_INT_VERSION >= 50100) {
  416. $mysql = '5.1';
  417. } else {
  418. $mysql = '5.0';
  419. }
  420. }
  421. $url = $cfg['MySQLManualBase'] . '/' . $mysql . '/' . $lang . '/' . $link . '.html';
  422. if (!empty($anchor)) {
  423. $url .= '#' . $anchor;
  424. }
  425. break;
  426. }
  427. $open_link = '<a href="' . PMA_linkURL($url) . '" target="mysql_doc">';
  428. if ($just_open) {
  429. return $open_link;
  430. } elseif ($big_icon) {
  431. return $open_link . PMA_getImage('b_sqlhelp.png', __('Documentation')) . '</a>';
  432. } elseif ($GLOBALS['cfg']['ReplaceHelpImg']) {
  433. return $open_link . PMA_getImage('b_help.png', __('Documentation')) . '</a>';
  434. } else {
  435. return '[' . $open_link . __('Documentation') . '</a>]';
  436. }
  437. } // end of the 'PMA_showMySQLDocu()' function
  438. /**
  439. * Displays a link to the phpMyAdmin documentation
  440. *
  441. * @param string $anchor anchor in documentation
  442. *
  443. * @return string the html link
  444. *
  445. * @access public
  446. */
  447. function PMA_showDocu($anchor)
  448. {
  449. if ($GLOBALS['cfg']['ReplaceHelpImg']) {
  450. return '<a href="Documentation.html#' . $anchor . '" target="documentation">'
  451. . PMA_getImage('b_help.png', __('Documentation'))
  452. . '</a>';
  453. } else {
  454. return '[<a href="Documentation.html#' . $anchor . '" target="documentation">'
  455. . __('Documentation') . '</a>]';
  456. }
  457. } // end of the 'PMA_showDocu()' function
  458. /**
  459. * Displays a link to the PHP documentation
  460. *
  461. * @param string $target anchor in documentation
  462. *
  463. * @return string the html link
  464. *
  465. * @access public
  466. */
  467. function PMA_showPHPDocu($target)
  468. {
  469. $url = PMA_getPHPDocLink($target);
  470. if ($GLOBALS['cfg']['ReplaceHelpImg']) {
  471. return '<a href="' . $url . '" target="documentation">'
  472. . PMA_getImage('b_help.png', __('Documentation'))
  473. . '</a>';
  474. } else {
  475. return '[<a href="' . $url . '" target="documentation">' . __('Documentation') . '</a>]';
  476. }
  477. } // end of the 'PMA_showPHPDocu()' function
  478. /**
  479. * returns HTML for a footnote marker and add the messsage to the footnotes
  480. *
  481. * @param string $message the error message
  482. * @param bool $bbcode
  483. * @param string $type message types
  484. *
  485. * @return string html code for a footnote marker
  486. *
  487. * @access public
  488. */
  489. function PMA_showHint($message, $bbcode = false, $type = 'notice')
  490. {
  491. if ($message instanceof PMA_Message) {
  492. $key = $message->getHash();
  493. $type = $message->getLevel();
  494. } else {
  495. $key = md5($message);
  496. }
  497. if (! isset($GLOBALS['footnotes'][$key])) {
  498. if (empty($GLOBALS['footnotes']) || ! is_array($GLOBALS['footnotes'])) {
  499. $GLOBALS['footnotes'] = array();
  500. }
  501. $nr = count($GLOBALS['footnotes']) + 1;
  502. $GLOBALS['footnotes'][$key] = array(
  503. 'note' => $message,
  504. 'type' => $type,
  505. 'nr' => $nr,
  506. );
  507. } else {
  508. $nr = $GLOBALS['footnotes'][$key]['nr'];
  509. }
  510. if ($bbcode) {
  511. return '[sup]' . $nr . '[/sup]';
  512. }
  513. // footnotemarker used in js/tooltip.js
  514. return '<sup class="footnotemarker">' . $nr . '</sup>' .
  515. PMA_getImage('b_help.png', '', array('class' => 'footnotemarker footnote_' . $nr));
  516. }
  517. /**
  518. * Displays a MySQL error message in the right frame.
  519. *
  520. * @param string $error_message the error message
  521. * @param string $the_query the sql query that failed
  522. * @param bool $is_modify_link whether to show a "modify" link or not
  523. * @param string $back_url the "back" link url (full path is not required)
  524. * @param bool $exit EXIT the page?
  525. *
  526. * @global string the curent table
  527. * @global string the current db
  528. *
  529. * @access public
  530. */
  531. function PMA_mysqlDie($error_message = '', $the_query = '',
  532. $is_modify_link = true, $back_url = '', $exit = true)
  533. {
  534. global $table, $db;
  535. /**
  536. * start http output, display html headers
  537. */
  538. include_once './libraries/header.inc.php';
  539. $error_msg_output = '';
  540. if (!$error_message) {
  541. $error_message = PMA_DBI_getError();
  542. }
  543. if (!$the_query && !empty($GLOBALS['sql_query'])) {
  544. $the_query = $GLOBALS['sql_query'];
  545. }
  546. // --- Added to solve bug #641765
  547. if (!function_exists('PMA_SQP_isError') || PMA_SQP_isError()) {
  548. $formatted_sql = htmlspecialchars($the_query);
  549. } elseif (empty($the_query) || trim($the_query) == '') {
  550. $formatted_sql = '';
  551. } else {
  552. if (strlen($the_query) > $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
  553. $formatted_sql = htmlspecialchars(substr($the_query, 0, $GLOBALS['cfg']['MaxCharactersInDisplayedSQL'])) . '[...]';
  554. } else {
  555. $formatted_sql = PMA_formatSql(PMA_SQP_parse($the_query), $the_query);
  556. }
  557. }
  558. // ---
  559. $error_msg_output .= "\n" . '<!-- PMA-SQL-ERROR -->' . "\n";
  560. $error_msg_output .= ' <div class="error"><h1>' . __('Error') . '</h1>' . "\n";
  561. // if the config password is wrong, or the MySQL server does not
  562. // respond, do not show the query that would reveal the
  563. // username/password
  564. if (!empty($the_query) && !strstr($the_query, 'connect')) {
  565. // --- Added to solve bug #641765
  566. if (function_exists('PMA_SQP_isError') && PMA_SQP_isError()) {
  567. $error_msg_output .= PMA_SQP_getErrorString() . "\n";
  568. $error_msg_output .= '<br />' . "\n";
  569. }
  570. // ---
  571. // modified to show the help on sql errors
  572. $error_msg_output .= ' <p><strong>' . __('SQL query') . ':</strong>' . "\n";
  573. if (strstr(strtolower($formatted_sql), 'select')) {
  574. // please show me help to the error on select
  575. $error_msg_output .= PMA_showMySQLDocu('SQL-Syntax', 'SELECT');
  576. }
  577. if ($is_modify_link) {
  578. $_url_params = array(
  579. 'sql_query' => $the_query,
  580. 'show_query' => 1,
  581. );
  582. if (strlen($table)) {
  583. $_url_params['db'] = $db;
  584. $_url_params['table'] = $table;
  585. $doedit_goto = '<a href="tbl_sql.php' . PMA_generate_common_url($_url_params) . '">';
  586. } elseif (strlen($db)) {
  587. $_url_params['db'] = $db;
  588. $doedit_goto = '<a href="db_sql.php' . PMA_generate_common_url($_url_params) . '">';
  589. } else {
  590. $doedit_goto = '<a href="server_sql.php' . PMA_generate_common_url($_url_params) . '">';
  591. }
  592. $error_msg_output .= $doedit_goto
  593. . PMA_getIcon('b_edit.png', __('Edit'))
  594. . '</a>';
  595. } // end if
  596. $error_msg_output .= ' </p>' . "\n"
  597. .' <p>' . "\n"
  598. .' ' . $formatted_sql . "\n"
  599. .' </p>' . "\n";
  600. } // end if
  601. if (! empty($error_message)) {
  602. $error_message = preg_replace(
  603. "@((\015\012)|(\015)|(\012)){3,}@",
  604. "\n\n",
  605. $error_message
  606. );
  607. }
  608. // modified to show the help on error-returns
  609. // (now error-messages-server)
  610. $error_msg_output .= '<p>' . "\n"
  611. . ' <strong>' . __('MySQL said: ') . '</strong>'
  612. . PMA_showMySQLDocu('Error-messages-server', 'Error-messages-server')
  613. . "\n"
  614. . '</p>' . "\n";
  615. // The error message will be displayed within a CODE segment.
  616. // To preserve original formatting, but allow wordwrapping,
  617. // we do a couple of replacements
  618. // Replace all non-single blanks with their HTML-counterpart
  619. $error_message = str_replace(' ', '&nbsp;&nbsp;', $error_message);
  620. // Replace TAB-characters with their HTML-counterpart
  621. $error_message = str_replace("\t", '&nbsp;&nbsp;&nbsp;&nbsp;', $error_message);
  622. // Replace linebreaks
  623. $error_message = nl2br($error_message);
  624. $error_msg_output .= '<code>' . "\n"
  625. . $error_message . "\n"
  626. . '</code><br />' . "\n";
  627. $error_msg_output .= '</div>';
  628. $_SESSION['Import_message']['message'] = $error_msg_output;
  629. if ($exit) {
  630. /**
  631. * If in an Ajax request
  632. * - avoid displaying a Back link
  633. * - use PMA_ajaxResponse() to transmit the message and exit
  634. */
  635. if ($GLOBALS['is_ajax_request'] == true) {
  636. PMA_ajaxResponse($error_msg_output, false);
  637. }
  638. if (! empty($back_url)) {
  639. if (strstr($back_url, '?')) {
  640. $back_url .= '&amp;no_history=true';
  641. } else {
  642. $back_url .= '?no_history=true';
  643. }
  644. $_SESSION['Import_message']['go_back_url'] = $back_url;
  645. $error_msg_output .= '<fieldset class="tblFooters">';
  646. $error_msg_output .= '[ <a href="' . $back_url . '">' . __('Back') . '</a> ]';
  647. $error_msg_output .= '</fieldset>' . "\n\n";
  648. }
  649. echo $error_msg_output;
  650. /**
  651. * display footer and exit
  652. */
  653. include './libraries/footer.inc.php';
  654. } else {
  655. echo $error_msg_output;
  656. }
  657. } // end of the 'PMA_mysqlDie()' function
  658. /**
  659. * returns array with tables of given db with extended information and grouped
  660. *
  661. * @param string $db name of db
  662. * @param string $tables name of tables
  663. * @param integer $limit_offset list offset
  664. * @param int|bool $limit_count max tables to return
  665. *
  666. * @return array (recursive) grouped table list
  667. */
  668. function PMA_getTableList($db, $tables = null, $limit_offset = 0, $limit_count = false)
  669. {
  670. $sep = $GLOBALS['cfg']['LeftFrameTableSeparator'];
  671. if (null === $tables) {
  672. $tables = PMA_DBI_get_tables_full($db, false, false, null, $limit_offset, $limit_count);
  673. if ($GLOBALS['cfg']['NaturalOrder']) {
  674. uksort($tables, 'strnatcasecmp');
  675. }
  676. }
  677. if (count($tables) < 1) {
  678. return $tables;
  679. }
  680. $default = array(
  681. 'Name' => '',
  682. 'Rows' => 0,
  683. 'Comment' => '',
  684. 'disp_name' => '',
  685. );
  686. $table_groups = array();
  687. // for blobstreaming - list of blobstreaming tables
  688. // load PMA configuration
  689. $PMA_Config = $GLOBALS['PMA_Config'];
  690. foreach ($tables as $table_name => $table) {
  691. // if BS tables exist
  692. if (PMA_BS_IsHiddenTable($table_name)) {
  693. continue;
  694. }
  695. // check for correct row count
  696. if (null === $table['Rows']) {
  697. // Do not check exact row count here,
  698. // if row count is invalid possibly the table is defect
  699. // and this would break left frame;
  700. // but we can check row count if this is a view or the
  701. // information_schema database
  702. // since PMA_Table::countRecords() returns a limited row count
  703. // in this case.
  704. // set this because PMA_Table::countRecords() can use it
  705. $tbl_is_view = $table['TABLE_TYPE'] == 'VIEW';
  706. if ($tbl_is_view || PMA_is_system_schema($db)) {
  707. $table['Rows'] = PMA_Table::countRecords($db, $table['Name'], false, true);
  708. }
  709. }
  710. // in $group we save the reference to the place in $table_groups
  711. // where to store the table info
  712. if ($GLOBALS['cfg']['LeftFrameDBTree']
  713. && $sep && strstr($table_name, $sep)
  714. ) {
  715. $parts = explode($sep, $table_name);
  716. $group =& $table_groups;
  717. $i = 0;
  718. $group_name_full = '';
  719. $parts_cnt = count($parts) - 1;
  720. while ($i < $parts_cnt
  721. && $i < $GLOBALS['cfg']['LeftFrameTableLevel']) {
  722. $group_name = $parts[$i] . $sep;
  723. $group_name_full .= $group_name;
  724. if (! isset($group[$group_name])) {
  725. $group[$group_name] = array();
  726. $group[$group_name]['is' . $sep . 'group'] = true;
  727. $group[$group_name]['tab' . $sep . 'count'] = 1;
  728. $group[$group_name]['tab' . $sep . 'group'] = $group_name_full;
  729. } elseif (! isset($group[$group_name]['is' . $sep . 'group'])) {
  730. $table = $group[$group_name];
  731. $group[$group_name] = array();
  732. $group[$group_name][$group_name] = $table;
  733. unset($table);
  734. $group[$group_name]['is' . $sep . 'group'] = true;
  735. $group[$group_name]['tab' . $sep . 'count'] = 1;
  736. $group[$group_name]['tab' . $sep . 'group'] = $group_name_full;
  737. } else {
  738. $group[$group_name]['tab' . $sep . 'count']++;
  739. }
  740. $group =& $group[$group_name];
  741. $i++;
  742. }
  743. } else {
  744. if (! isset($table_groups[$table_name])) {
  745. $table_groups[$table_name] = array();
  746. }
  747. $group =& $table_groups;
  748. }
  749. if ($GLOBALS['cfg']['ShowTooltipAliasTB']
  750. && $GLOBALS['cfg']['ShowTooltipAliasTB'] !== 'nested'
  751. && $table['Comment'] // do not switch if the comment is empty
  752. && $table['Comment'] != 'VIEW' // happens in MySQL 5.1
  753. ) {
  754. // switch tooltip and name
  755. $table['disp_name'] = $table['Comment'];
  756. $table['Comment'] = $table['Name'];
  757. } else {
  758. $table['disp_name'] = $table['Name'];
  759. }
  760. $group[$table_name] = array_merge($default, $table);
  761. }
  762. return $table_groups;
  763. }
  764. /* ----------------------- Set of misc functions ----------------------- */
  765. /**
  766. * Adds backquotes on both sides of a database, table or field name.
  767. * and escapes backquotes inside the name with another backquote
  768. *
  769. * example:
  770. * <code>
  771. * echo PMA_backquote('owner`s db'); // `owner``s db`
  772. *
  773. * </code>
  774. *
  775. * @param mixed $a_name the database, table or field name to "backquote"
  776. * or array of it
  777. * @param boolean $do_it a flag to bypass this function (used by dump
  778. * functions)
  779. *
  780. * @return mixed the "backquoted" database, table or field name
  781. *
  782. * @access public
  783. */
  784. function PMA_backquote($a_name, $do_it = true)
  785. {
  786. if (is_array($a_name)) {
  787. foreach ($a_name as &$data) {
  788. $data = PMA_backquote($data, $do_it);
  789. }
  790. return $a_name;
  791. }
  792. if (! $do_it) {
  793. global $PMA_SQPdata_forbidden_word;
  794. if (! in_array(strtoupper($a_name), $PMA_SQPdata_forbidden_word)) {
  795. return $a_name;
  796. }
  797. }
  798. // '0' is also empty for php :-(
  799. if (strlen($a_name) && $a_name !== '*') {
  800. return '`' . str_replace('`', '``', $a_name) . '`';
  801. } else {
  802. return $a_name;
  803. }
  804. } // end of the 'PMA_backquote()' function
  805. /**
  806. * Defines the <CR><LF> value depending on the user OS.
  807. *
  808. * @return string the <CR><LF> value to use
  809. *
  810. * @access public
  811. */
  812. function PMA_whichCrlf()
  813. {
  814. // The 'PMA_USR_OS' constant is defined in "./libraries/Config.class.php"
  815. // Win case
  816. if (PMA_USR_OS == 'Win') {
  817. $the_crlf = "\r\n";
  818. } else {
  819. // Others
  820. $the_crlf = "\n";
  821. }
  822. return $the_crlf;
  823. } // end of the 'PMA_whichCrlf()' function
  824. /**
  825. * Reloads navigation if needed.
  826. *
  827. * @param bool $jsonly prints out pure JavaScript
  828. *
  829. * @access public
  830. */
  831. function PMA_reloadNavigation($jsonly=false)
  832. {
  833. // Reloads the navigation frame via JavaScript if required
  834. if (isset($GLOBALS['reload']) && $GLOBALS['reload']) {
  835. // one of the reasons for a reload is when a table is dropped
  836. // in this case, get rid of the table limit offset, otherwise
  837. // we have a problem when dropping a table on the last page
  838. // and the offset becomes greater than the total number of tables
  839. unset($_SESSION['tmp_user_values']['table_limit_offset']);
  840. echo "\n";
  841. $reload_url = './navigation.php?' . PMA_generate_common_url($GLOBALS['db'], '', '&');
  842. if (!$jsonly) {
  843. echo '<script type="text/javascript">' . PHP_EOL;
  844. }
  845. ?>
  846. //<![CDATA[
  847. if (typeof(window.parent) != 'undefined'
  848. && typeof(window.parent.frame_navigation) != 'undefined'
  849. && window.parent.goTo) {
  850. window.parent.goTo('<?php echo $reload_url; ?>');
  851. }
  852. //]]>
  853. <?php
  854. if (!$jsonly) {
  855. echo '</script>' . PHP_EOL;
  856. }
  857. unset($GLOBALS['reload']);
  858. }
  859. }
  860. /**
  861. * displays the message and the query
  862. * usually the message is the result of the query executed
  863. *
  864. * @param string $message the message to display
  865. * @param string $sql_query the query to display
  866. * @param string $type the type (level) of the message
  867. * @param boolean $is_view is this a message after a VIEW operation?
  868. *
  869. * @return string
  870. *
  871. * @access public
  872. */
  873. function PMA_showMessage($message, $sql_query = null, $type = 'notice', $is_view = false)
  874. {
  875. /*
  876. * PMA_ajaxResponse uses this function to collect the string of HTML generated
  877. * for showing the message. Use output buffering to collect it and return it
  878. * in a string. In some special cases on sql.php, buffering has to be disabled
  879. * and hence we check with $GLOBALS['buffer_message']
  880. */
  881. if ( $GLOBALS['is_ajax_request'] == true && ! isset($GLOBALS['buffer_message']) ) {
  882. ob_start();
  883. }
  884. global $cfg;
  885. if (null === $sql_query) {
  886. if (! empty($GLOBALS['display_query'])) {
  887. $sql_query = $GLOBALS['display_query'];
  888. } elseif ($cfg['SQP']['fmtType'] == 'none' && ! empty($GLOBALS['unparsed_sql'])) {
  889. $sql_query = $GLOBALS['unparsed_sql'];
  890. } elseif (! empty($GLOBALS['sql_query'])) {
  891. $sql_query = $GLOBALS['sql_query'];
  892. } else {
  893. $sql_query = '';
  894. }
  895. }
  896. if (isset($GLOBALS['using_bookmark_message'])) {
  897. $GLOBALS['using_bookmark_message']->display();
  898. unset($GLOBALS['using_bookmark_message']);
  899. }
  900. // Corrects the tooltip text via JS if required
  901. // @todo this is REALLY the wrong place to do this - very unexpected here
  902. if (! $is_view && strlen($GLOBALS['table']) && $cfg['ShowTooltip']) {
  903. $tooltip = PMA_Table::sGetToolTip($GLOBALS['db'], $GLOBALS['table']);
  904. $uni_tbl = PMA_jsFormat($GLOBALS['db'] . '.' . $GLOBALS['table'], false);
  905. echo "\n";
  906. echo '<script type="text/javascript">' . "\n";
  907. echo '//<![CDATA[' . "\n";
  908. echo "if (window.parent.updateTableTitle) window.parent.updateTableTitle('"
  909. . $uni_tbl . "', '" . PMA_jsFormat($tooltip, false) . "');" . "\n";
  910. echo '//]]>' . "\n";
  911. echo '</script>' . "\n";
  912. } // end if ... elseif
  913. // Checks if the table needs to be repaired after a TRUNCATE query.
  914. // @todo what about $GLOBALS['display_query']???
  915. // @todo this is REALLY the wrong place to do this - very unexpected here
  916. if (strlen($GLOBALS['table'])
  917. && $GLOBALS['sql_query'] == 'TRUNCATE TABLE ' . PMA_backquote($GLOBALS['table'])
  918. ) {
  919. if (PMA_Table::sGetStatusInfo($GLOBALS['db'], $GLOBALS['table'], 'Index_length') > 1024 && !PMA_DRIZZLE) {
  920. PMA_DBI_try_query('REPAIR TABLE ' . PMA_backquote($GLOBALS['table']));
  921. }
  922. }
  923. unset($tbl_status);
  924. // In an Ajax request, $GLOBALS['cell_align_left'] may not be defined. Hence,
  925. // check for it's presence before using it
  926. echo '<div id="result_query" align="'
  927. . ( isset($GLOBALS['cell_align_left']) ? $GLOBALS['cell_align_left'] : '' )
  928. . '">' . "\n";
  929. if ($message instanceof PMA_Message) {
  930. if (isset($GLOBALS['special_message'])) {
  931. $message->addMessage($GLOBALS['special_message']);
  932. unset($GLOBALS['special_message']);
  933. }
  934. $message->display();
  935. $type = $message->getLevel();
  936. } else {
  937. echo '<div class="' . $type . '">';
  938. echo PMA_sanitize($message);
  939. if (isset($GLOBALS['special_message'])) {
  940. echo PMA_sanitize($GLOBALS['special_message']);
  941. unset($GLOBALS['special_message']);
  942. }
  943. echo '</div>';
  944. }
  945. if ($cfg['ShowSQL'] == true && ! empty($sql_query)) {
  946. // Html format the query to be displayed
  947. // If we want to show some sql code it is easiest to create it here
  948. /* SQL-Parser-Analyzer */
  949. if (! empty($GLOBALS['show_as_php'])) {
  950. $new_line = '\\n"<br />' . "\n"
  951. . '&nbsp;&nbsp;&nbsp;&nbsp;. "';
  952. $query_base = htmlspecialchars(addslashes($sql_query));
  953. $query_base = preg_replace('/((\015\012)|(\015)|(\012))/', $new_line, $query_base);
  954. } else {
  955. $query_base = $sql_query;
  956. }
  957. $query_too_big = false;
  958. if (strlen($query_base) > $cfg['MaxCharactersInDisplayedSQL']) {
  959. // when the query is large (for example an INSERT of binary
  960. // data), the parser chokes; so avoid parsing the query
  961. $query_too_big = true;
  962. $shortened_query_base = nl2br(
  963. htmlspecialchars(
  964. substr($sql_query, 0, $cfg['MaxCharactersInDisplayedSQL']) . '[...]'
  965. )
  966. );
  967. } elseif (! empty($GLOBALS['parsed_sql'])
  968. && $query_base == $GLOBALS['parsed_sql']['raw']) {
  969. // (here, use "! empty" because when deleting a bookmark,
  970. // $GLOBALS['parsed_sql'] is set but empty
  971. $parsed_sql = $GLOBALS['parsed_sql'];
  972. } else {
  973. // Parse SQL if needed
  974. $parsed_sql = PMA_SQP_parse($query_base);
  975. }
  976. // Analyze it
  977. if (isset($parsed_sql) && ! PMA_SQP_isError()) {
  978. $analyzed_display_query = PMA_SQP_analyze($parsed_sql);
  979. // Same as below (append LIMIT), append the remembered ORDER BY
  980. if ($GLOBALS['cfg']['RememberSorting']
  981. && isset($analyzed_display_query[0]['queryflags']['select_from'])
  982. && isset($GLOBALS['sql_order_to_append'])
  983. ) {
  984. $query_base = $analyzed_display_query[0]['section_before_limit']
  985. . "\n" . $GLOBALS['sql_order_to_append']
  986. . $analyzed_display_query[0]['limit_clause'] . ' '
  987. . $analyzed_display_query[0]['section_after_limit'];
  988. // Need to reparse query
  989. $parsed_sql = PMA_SQP_parse($query_base);
  990. // update the $analyzed_display_query
  991. $analyzed_display_query[0]['section_before_limit'] .= $GLOBALS['sql_order_to_append'];
  992. $analyzed_display_query[0]['order_by_clause'] = $GLOBALS['sorted_col'];
  993. }
  994. // Here we append the LIMIT added for navigation, to
  995. // enable its display. Adding it higher in the code
  996. // to $sql_query would create a problem when
  997. // using the Refresh or Edit links.
  998. // Only append it on SELECTs.
  999. /**
  1000. * @todo what would be the best to do when someone hits Refresh:
  1001. * use the current LIMITs ?
  1002. */
  1003. if (isset($analyzed_display_query[0]['queryflags']['select_from'])
  1004. && isset($GLOBALS['sql_limit_to_append'])
  1005. ) {
  1006. $query_base = $analyzed_display_query[0]['section_before_limit']
  1007. . "\n" . $GLOBALS['sql_limit_to_append']
  1008. . $analyzed_display_query[0]['section_after_limit'];
  1009. // Need to reparse query
  1010. $parsed_sql = PMA_SQP_parse($query_base);
  1011. }
  1012. }
  1013. if (! empty($GLOBALS['show_as_php'])) {
  1014. $query_base = '$sql = "' . $query_base;
  1015. } elseif (! empty($GLOBALS['validatequery'])) {
  1016. try {
  1017. $query_base = PMA_validateSQL($query_base);
  1018. } catch (Exception $e) {
  1019. PMA_Message::error(__('Failed to connect to SQL validator!'))->display();
  1020. }
  1021. } elseif (isset($parsed_sql)) {
  1022. $query_base = PMA_formatSql($parsed_sql, $query_base);
  1023. }
  1024. // Prepares links that may be displayed to edit/explain the query
  1025. // (don't go to default pages, we must go to the page
  1026. // where the query box is available)
  1027. // Basic url query part
  1028. $url_params = array();
  1029. if (! isset($GLOBALS['db'])) {
  1030. $GLOBALS['db'] = '';
  1031. }
  1032. if (strlen($GLOBALS['db'])) {
  1033. $url_params['db'] = $GLOBALS['db'];
  1034. if (strlen($GLOBALS['table'])) {
  1035. $url_params['table'] = $GLOBALS['table'];
  1036. $edit_link = 'tbl_sql.php';
  1037. } else {
  1038. $edit_link = 'db_sql.php';
  1039. }
  1040. } else {
  1041. $edit_link = 'server_sql.php';
  1042. }
  1043. // Want to have the query explained
  1044. // but only explain a SELECT (that has not been explained)
  1045. /* SQL-Parser-Analyzer */
  1046. $explain_link = '';
  1047. $is_select = false;
  1048. if (! empty($cfg['SQLQuery']['Explain']) && ! $query_too_big) {
  1049. $explain_params = $url_params;
  1050. // Detect if we are validating as well
  1051. // To preserve the validate uRL data
  1052. if (! empty($GLOBALS['validatequery'])) {
  1053. $explain_params['validatequery'] = 1;
  1054. }
  1055. if (preg_match('@^SELECT[[:space:]]+@i', $sql_query)) {
  1056. $explain_params['sql_query'] = 'EXPLAIN ' . $sql_query;
  1057. $_message = __('Explain SQL');
  1058. $is_select = true;
  1059. } elseif (preg_match('@^EXPLAIN[[:space:]]+SELECT[[:space:]]+@i', $sql_query)) {
  1060. $explain_params['sql_query'] = substr($sql_query, 8);
  1061. $_message = __('Skip Explain SQL');
  1062. }
  1063. if (isset($explain_params['sql_query'])) {
  1064. $explain_link = 'import.php' . PMA_generate_common_url($explain_params);
  1065. $explain_link = ' [' . PMA_linkOrButton($explain_link, $_message) . ']';
  1066. }
  1067. } //show explain
  1068. $url_params['sql_query'] = $sql_query;
  1069. $url_params['show_query'] = 1;
  1070. // even if the query is big and was truncated, offer the chance
  1071. // to edit it (unless it's enormous, see PMA_linkOrButton() )
  1072. if (! empty($cfg['SQLQuery']['Edit'])) {
  1073. if ($cfg['EditInWindow'] == true) {
  1074. $onclick = 'window.parent.focus_querywindow(\''
  1075. . PMA_jsFormat($sql_query, false) . '\'); return false;';
  1076. } else {
  1077. $onclick = '';
  1078. }
  1079. $edit_link .= PMA_generate_common_url($url_params) . '#querybox';
  1080. $edit_link = ' [' . PMA_linkOrButton($edit_link, __('Edit'), array('onclick' => $onclick)) . ']';
  1081. } else {
  1082. $edit_link = '';
  1083. }
  1084. $url_qpart = PMA_generate_common_url($url_params);
  1085. // Also we would like to get the SQL formed in some nice
  1086. // php-code
  1087. if (! empty($cfg['SQLQuery']['ShowAsPHP']) && ! $query_too_big) {
  1088. $php_params = $url_params;
  1089. if (! empty($GLOBALS['show_as_php'])) {
  1090. $_message = __('Without PHP Code');
  1091. } else {
  1092. $php_params['show_as_php'] = 1;
  1093. $_message = __('Create PHP Code');
  1094. }
  1095. $php_link = 'import.php' . PMA_generate_common_url($php_params);
  1096. $php_link = ' [' . PMA_linkOrButton($php_link, $_message) . ']';
  1097. if (isset($GLOBALS['show_as_php'])) {
  1098. $runquery_link = 'import.php' . PMA_generate_common_url($url_params);
  1099. $php_link .= ' [' . PMA_linkOrButton($runquery_link, __('Submit Query')) . ']';
  1100. }
  1101. } else {
  1102. $php_link = '';
  1103. } //show as php
  1104. // Refresh query
  1105. if (! empty($cfg['SQLQuery']['Refresh'])
  1106. && ! isset($GLOBALS['show_as_php']) // 'Submit query' does the same
  1107. && preg_match('@^(SELECT|SHOW)[[:space:]]+@i', $sql_query)
  1108. ) {
  1109. $refresh_link = 'import.php' . PMA_generate_common_url($url_params);
  1110. $refresh_link = ' [' . PMA_linkOrButton($refresh_link, __('Refresh')) . ']';
  1111. } else {
  1112. $refresh_link = '';
  1113. } //refresh
  1114. if (! empty($cfg['SQLValidator']['use'])
  1115. && ! empty($cfg['SQLQuery']['Validate'])
  1116. ) {
  1117. $validate_params = $url_params;
  1118. if (!empty($GLOBALS['validatequery'])) {
  1119. $validate_message = __('Skip Validate SQL');
  1120. } else {
  1121. $validate_params['validatequery'] = 1;
  1122. $validate_message = __('Validate SQL');
  1123. }
  1124. $validate_link = 'import.php' . PMA_generate_common_url($validate_params);
  1125. $validate_link = ' [' . PMA_linkOrButton($validate_link, $validate_message) . ']';
  1126. } else {
  1127. $validate_link = '';
  1128. } //validator
  1129. if (!empty($GLOBALS['validatequery'])) {
  1130. echo '<div class="sqlvalidate">';
  1131. } else {
  1132. echo '<code class="sql">';
  1133. }
  1134. if ($query_too_big) {
  1135. echo $shortened_query_base;
  1136. } else {
  1137. echo $query_base;
  1138. }
  1139. //Clean up the end of the PHP
  1140. if (! empty($GLOBALS['show_as_php'])) {
  1141. echo '";';
  1142. }
  1143. if (!empty($GLOBALS['validatequery'])) {
  1144. echo '</div>';
  1145. } else {
  1146. echo '</code>';
  1147. }
  1148. echo '<div class="tools">';
  1149. // avoid displaying a Profiling checkbox that could
  1150. // be checked, which would reexecute an INSERT, for example
  1151. if (! empty($refresh_link)) {
  1152. PMA_profilingCheckbox($sql_query);
  1153. }
  1154. // if needed, generate an invisible form that contains controls for the
  1155. // Inline link; this way, the behavior of the Inline link does not
  1156. // depend on the profiling support or on the refresh link
  1157. if (empty($refresh_link) || ! PMA_profilingSupported()) {
  1158. echo '<form action="sql.php" method="post">';
  1159. echo PMA_generate_common_hidden_inputs($GLOBALS['db'], $GLOBALS['table']);
  1160. echo '<input type="hidden" name="sql_query" value="'
  1161. . htmlspecialchars($sql_query) . '" />';
  1162. echo '</form>';
  1163. }
  1164. // in the tools div, only display the Inline link when not in ajax
  1165. // mode because 1) it currently does not work and 2) we would
  1166. // have two similar mechanisms on the page for the same goal
  1167. if ($is_select
  1168. || $GLOBALS['is_ajax_request'] === false
  1169. && ! $query_too_big
  1170. ) {
  1171. // see in js/functions.js the jQuery code attached to id inline_edit
  1172. // document.write conflicts with jQuery, hence used $().append()
  1173. echo "<script type=\"text/javascript\">\n" .
  1174. "//<![CDATA[\n" .
  1175. "$('.tools form').last().after('[<a href=\"#\" title=\"" .
  1176. PMA_escapeJsString(__('Inline edit of this query')) .
  1177. "\" class=\"inline_edit_sql\">" .
  1178. PMA_escapeJsString(_pgettext('Inline edit query', 'Inline')) .
  1179. "</a>]');\n" .
  1180. "//]]>\n" .
  1181. "</script>";
  1182. }
  1183. echo $edit_link . $explain_link . $php_link . $refresh_link . $validate_link;
  1184. echo '</div>';
  1185. }
  1186. echo '</div>';
  1187. if ($GLOBALS['is_ajax_request'] === false) {
  1188. echo '<br class="clearfloat" />';
  1189. }
  1190. // If we are in an Ajax request, we have most probably been called in
  1191. // PMA_ajaxResponse(). Hence, collect the buffer contents and return it
  1192. // to PMA_ajaxResponse(), which will encode it for JSON.
  1193. if ($GLOBALS['is_ajax_request'] == true
  1194. && ! isset($GLOBALS['buffer_message'])
  1195. ) {
  1196. $buffer_contents = ob_get_contents();
  1197. ob_end_clean();
  1198. return $buffer_contents;
  1199. }
  1200. return null;
  1201. } // end of the 'PMA_showMessage()' function
  1202. /**
  1203. * Verifies if current MySQL server supports profiling
  1204. *
  1205. * @access public
  1206. *
  1207. * @return boolean whether profiling is supported
  1208. */
  1209. function PMA_profilingSupported()
  1210. {
  1211. if (! PMA_cacheExists('profiling_supported', true)) {
  1212. // 5.0.37 has profiling but for example, 5.1.20 does not
  1213. // (avoid a trip to the server for MySQL before 5.0.37)
  1214. // and do not set a constant as we might be switching servers
  1215. if (defined('PMA_MYSQL_INT_VERSION')
  1216. && PMA_MYSQL_INT_VERSION >= 50037
  1217. && PMA_DBI_fetch_value("SHOW VARIABLES LIKE 'profiling'")
  1218. ) {
  1219. PMA_cacheSet('profiling_supported', true, true);
  1220. } else {
  1221. PMA_cacheSet('profiling_supported', false, true);
  1222. }
  1223. }
  1224. return PMA_cacheGet('profiling_supported', true);
  1225. }
  1226. /**
  1227. * Displays a form with the Profiling checkbox
  1228. *
  1229. * @param string $sql_query sql query
  1230. *
  1231. * @access public
  1232. */
  1233. function PMA_profilingCheckbox($sql_query)
  1234. {
  1235. if (PMA_profilingSupported()) {
  1236. echo '<form action="sql.php" method="post">' . "\n";
  1237. echo PMA_generate_common_hidden_inputs($GLOBALS['db'], $GLOBALS['table']);
  1238. echo '<input type="hidden" name="sql_query" value="' . htmlspecialchars($sql_query) . '" />' . "\n";
  1239. echo '<input type="hidden" name="profiling_form" value="1" />' . "\n";
  1240. PMA_display_html_checkbox('profiling', __('Profiling'), isset($_SESSION['profiling']), true);
  1241. echo '<noscript><input type="submit" value="' . __('Go') . '" /></noscript>' . "\n";
  1242. echo '</form>' . "\n";
  1243. }
  1244. }
  1245. /**
  1246. * Formats $value to byte view
  1247. *
  1248. * @param double $value the value to format
  1249. * @param int $limes the sensitiveness
  1250. * @param int $comma the number of decimals to retain
  1251. *
  1252. * @return array the formatted value and its unit
  1253. *
  1254. * @access public
  1255. */
  1256. function PMA_formatByteDown($value, $limes = 6, $comma = 0)
  1257. {
  1258. if ($value === null) {
  1259. return null;
  1260. }
  1261. $byteUnits = array(
  1262. /* l10n: shortcuts for Byte */
  1263. __('B'),
  1264. /* l10n: shortcuts for Kilobyte */
  1265. __('KiB'),
  1266. /* l10n: shortcuts for Megabyte */
  1267. __('MiB'),
  1268. /* l10n: shortcuts for Gigabyte */
  1269. __('GiB'),
  1270. /* l10n: shortcuts for Terabyte */
  1271. __('TiB'),
  1272. /* l10n: shortcuts for Petabyte */
  1273. __('PiB'),
  1274. /* l10n: shortcuts for Exabyte */
  1275. __('EiB')
  1276. );
  1277. $dh = PMA_pow(10, $comma);
  1278. $li = PMA_pow(10, $limes);
  1279. $unit = $byteUnits[0];
  1280. for ($d = 6, $ex = 15; $d >= 1; $d--, $ex-=3) {
  1281. if (isset($byteUnits[$d]) && $value >= $li * PMA_pow(10, $ex)) {
  1282. // use 1024.0 to avoid integer overflow on 64-bit machines
  1283. $value = round($value / (PMA_pow(1024, $d) / $dh)) /$dh;
  1284. $unit = $byteUnits[$d];
  1285. break 1;
  1286. } // end if
  1287. } // end for
  1288. if ($unit != $byteUnits[0]) {
  1289. // if the unit is not bytes (as represented in current language)
  1290. // reformat with max length of 5
  1291. // 4th parameter=true means do not reformat if value < 1
  1292. $return_value = PMA_formatNumber($value, 5, $comma, true);
  1293. } else {
  1294. // do not reformat, just handle the locale
  1295. $return_value = PMA_formatNumber($value, 0);
  1296. }
  1297. return array(trim($return_value), $unit);
  1298. } // end of the 'PMA_formatByteDown' function
  1299. /**
  1300. * Changes thousands and decimal separators to locale specific values.
  1301. *
  1302. * @param string $value the value
  1303. *
  1304. * @return string
  1305. */
  1306. function PMA_localizeNumber($value)
  1307. {
  1308. return str_replace(
  1309. array(',', '.'),
  1310. array(
  1311. /* l10n: Thousands separator */
  1312. __(','),
  1313. /* l10n: Decimal separator */
  1314. __('.'),
  1315. ),
  1316. $value
  1317. );
  1318. }
  1319. /**
  1320. * Formats $value to the given length and appends SI prefixes
  1321. * with a $length of 0 no truncation occurs, number is only formated
  1322. * to the current locale
  1323. *
  1324. * examples:
  1325. * <code>
  1326. * echo PMA_formatNumber(123456789, 6); // 123,457 k
  1327. * echo PMA_formatNumber(-123456789, 4, 2); // -123.46 M
  1328. * echo PMA_formatNumber(-0.003, 6); // -3 m
  1329. * echo PMA_formatNumber(0.003, 3, 3); // 0.003
  1330. * echo PMA_formatNumber(0.00003, 3, 2); // 0.03 m
  1331. * echo PMA_formatNumber(0, 6); // 0
  1332. * </code>
  1333. *
  1334. * @param double $value the value to format
  1335. * @param integer $digits_left number of digits left of the comma
  1336. * @param integer $digits_right number of digits right of the comma
  1337. * @param boolean $only_down do not reformat numbers below 1
  1338. * @param boolean $noTrailingZero removes trailing zeros right of the comma
  1339. * (default: true)
  1340. *
  1341. * @return string the formatted value and its unit
  1342. *
  1343. * @access public
  1344. */
  1345. function PMA_formatNumber($value, $digits_left = 3, $digits_right = 0,
  1346. $only_down = false, $noTrailingZero = true)
  1347. {
  1348. if ($value==0) {
  1349. return '0';
  1350. }
  1351. $originalValue = $value;
  1352. //number_format is not multibyte safe, str_replace is safe
  1353. if ($digits_left === 0) {
  1354. $value = number_format($value, $digits_right);
  1355. if ($originalValue != 0 && floatv

Large files files are truncated, but you can click here to view the full file