PageRenderTime 36ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/libraries/classes/Util.php

http://github.com/phpmyadmin/phpmyadmin
PHP | 2753 lines | 1839 code | 266 blank | 648 comment | 242 complexity | 31c3ddcdbd5993f7de68d7823478336c MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-3.0
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin;
  4. use PhpMyAdmin\Html\Generator;
  5. use PhpMyAdmin\Html\MySQLDocumentation;
  6. use PhpMyAdmin\Query\Utilities;
  7. use PhpMyAdmin\SqlParser\Components\Expression;
  8. use PhpMyAdmin\SqlParser\Context;
  9. use PhpMyAdmin\SqlParser\Token;
  10. use PhpMyAdmin\Utils\SessionCache;
  11. use phpseclib3\Crypt\Random;
  12. use function __;
  13. use function _pgettext;
  14. use function abs;
  15. use function array_key_exists;
  16. use function array_map;
  17. use function array_merge;
  18. use function array_shift;
  19. use function array_unique;
  20. use function basename;
  21. use function bin2hex;
  22. use function chr;
  23. use function class_exists;
  24. use function count;
  25. use function ctype_digit;
  26. use function date;
  27. use function decbin;
  28. use function explode;
  29. use function extension_loaded;
  30. use function fclose;
  31. use function floatval;
  32. use function floor;
  33. use function fread;
  34. use function function_exists;
  35. use function html_entity_decode;
  36. use function htmlentities;
  37. use function htmlspecialchars;
  38. use function htmlspecialchars_decode;
  39. use function implode;
  40. use function in_array;
  41. use function ini_get;
  42. use function is_array;
  43. use function is_callable;
  44. use function is_object;
  45. use function is_scalar;
  46. use function is_string;
  47. use function log10;
  48. use function mb_detect_encoding;
  49. use function mb_strlen;
  50. use function mb_strpos;
  51. use function mb_strrpos;
  52. use function mb_strstr;
  53. use function mb_strtolower;
  54. use function mb_substr;
  55. use function number_format;
  56. use function ord;
  57. use function parse_url;
  58. use function preg_match;
  59. use function preg_quote;
  60. use function preg_replace;
  61. use function range;
  62. use function reset;
  63. use function round;
  64. use function rtrim;
  65. use function set_time_limit;
  66. use function sort;
  67. use function sprintf;
  68. use function str_contains;
  69. use function str_pad;
  70. use function str_replace;
  71. use function strcasecmp;
  72. use function strftime;
  73. use function strlen;
  74. use function strrev;
  75. use function strtolower;
  76. use function strtr;
  77. use function substr;
  78. use function time;
  79. use function trim;
  80. use function uksort;
  81. use const ENT_COMPAT;
  82. use const ENT_QUOTES;
  83. use const PHP_INT_SIZE;
  84. use const PHP_MAJOR_VERSION;
  85. use const STR_PAD_LEFT;
  86. /**
  87. * Misc functions used all over the scripts.
  88. */
  89. class Util
  90. {
  91. /**
  92. * Checks whether configuration value tells to show icons.
  93. *
  94. * @param string $value Configuration option name
  95. */
  96. public static function showIcons($value): bool
  97. {
  98. return in_array($GLOBALS['cfg'][$value], ['icons', 'both']);
  99. }
  100. /**
  101. * Checks whether configuration value tells to show text.
  102. *
  103. * @param string $value Configuration option name
  104. */
  105. public static function showText($value): bool
  106. {
  107. return in_array($GLOBALS['cfg'][$value], ['text', 'both']);
  108. }
  109. /**
  110. * Returns the formatted maximum size for an upload
  111. *
  112. * @param int|float|string $maxUploadSize the size
  113. *
  114. * @return string the message
  115. *
  116. * @access public
  117. */
  118. public static function getFormattedMaximumUploadSize($maxUploadSize): string
  119. {
  120. // I have to reduce the second parameter (sensitiveness) from 6 to 4
  121. // to avoid weird results like 512 kKib
  122. [$maxSize, $maxUnit] = self::formatByteDown($maxUploadSize, 4);
  123. return '(' . sprintf(__('Max: %s%s'), $maxSize, $maxUnit) . ')';
  124. }
  125. /**
  126. * Add slashes before "_" and "%" characters for using them in MySQL
  127. * database, table and field names.
  128. * Note: This function does not escape backslashes!
  129. *
  130. * @param string $name the string to escape
  131. *
  132. * @return string the escaped string
  133. *
  134. * @access public
  135. */
  136. public static function escapeMysqlWildcards($name): string
  137. {
  138. return strtr($name, ['_' => '\\_', '%' => '\\%']);
  139. }
  140. /**
  141. * removes slashes before "_" and "%" characters
  142. * Note: This function does not unescape backslashes!
  143. *
  144. * @param string $name the string to escape
  145. *
  146. * @return string the escaped string
  147. *
  148. * @access public
  149. */
  150. public static function unescapeMysqlWildcards($name): string
  151. {
  152. return strtr($name, ['\\_' => '_', '\\%' => '%']);
  153. }
  154. /**
  155. * removes quotes (',",`) from a quoted string
  156. *
  157. * checks if the string is quoted and removes this quotes
  158. *
  159. * @param string $quotedString string to remove quotes from
  160. * @param string $quote type of quote to remove
  161. *
  162. * @return string unquoted string
  163. */
  164. public static function unQuote(string $quotedString, ?string $quote = null): string
  165. {
  166. $quotes = [];
  167. if ($quote === null) {
  168. $quotes[] = '`';
  169. $quotes[] = '"';
  170. $quotes[] = "'";
  171. } else {
  172. $quotes[] = $quote;
  173. }
  174. foreach ($quotes as $quote) {
  175. if (mb_substr($quotedString, 0, 1) === $quote && mb_substr($quotedString, -1, 1) === $quote) {
  176. $unquotedString = mb_substr($quotedString, 1, -1);
  177. // replace escaped quotes
  178. $unquotedString = str_replace($quote . $quote, $quote, $unquotedString);
  179. return $unquotedString;
  180. }
  181. }
  182. return $quotedString;
  183. }
  184. /**
  185. * Get a URL link to the official MySQL documentation
  186. *
  187. * @param string $link contains name of page/anchor that is being linked
  188. * @param string $anchor anchor to page part
  189. *
  190. * @return string the URL link
  191. *
  192. * @access public
  193. */
  194. public static function getMySQLDocuURL(string $link, string $anchor = ''): string
  195. {
  196. global $dbi;
  197. // Fixup for newly used names:
  198. $link = str_replace('_', '-', mb_strtolower($link));
  199. if (empty($link)) {
  200. $link = 'index';
  201. }
  202. $mysql = '5.5';
  203. $lang = 'en';
  204. if (isset($dbi)) {
  205. $serverVersion = $dbi->getVersion();
  206. if ($serverVersion >= 80000) {
  207. $mysql = '8.0';
  208. } elseif ($serverVersion >= 50700) {
  209. $mysql = '5.7';
  210. } elseif ($serverVersion >= 50600) {
  211. $mysql = '5.6';
  212. } elseif ($serverVersion >= 50500) {
  213. $mysql = '5.5';
  214. }
  215. }
  216. $url = 'https://dev.mysql.com/doc/refman/'
  217. . $mysql . '/' . $lang . '/' . $link . '.html';
  218. if (! empty($anchor)) {
  219. $url .= '#' . $anchor;
  220. }
  221. return Core::linkURL($url);
  222. }
  223. /**
  224. * Get a URL link to the official documentation page of either MySQL
  225. * or MariaDB depending on the database server
  226. * of the user.
  227. *
  228. * @param bool $isMariaDB if the database server is MariaDB
  229. *
  230. * @return string The URL link
  231. */
  232. public static function getDocuURL(bool $isMariaDB = false): string
  233. {
  234. if ($isMariaDB) {
  235. $url = 'https://mariadb.com/kb/en/documentation/';
  236. return Core::linkURL($url);
  237. }
  238. return self::getMySQLDocuURL('');
  239. }
  240. /**
  241. * Check the correct row count
  242. *
  243. * @param string $db the db name
  244. * @param array $table the table infos
  245. *
  246. * @return int the possibly modified row count
  247. */
  248. private static function checkRowCount($db, array $table)
  249. {
  250. global $dbi;
  251. $rowCount = 0;
  252. if ($table['Rows'] === null) {
  253. // Do not check exact row count here,
  254. // if row count is invalid possibly the table is defect
  255. // and this would break the navigation panel;
  256. // but we can check row count if this is a view or the
  257. // information_schema database
  258. // since Table::countRecords() returns a limited row count
  259. // in this case.
  260. // set this because Table::countRecords() can use it
  261. $tableIsView = $table['TABLE_TYPE'] === 'VIEW';
  262. if ($tableIsView || Utilities::isSystemSchema($db)) {
  263. $rowCount = $dbi
  264. ->getTable($db, $table['Name'])
  265. ->countRecords();
  266. }
  267. }
  268. return $rowCount;
  269. }
  270. /**
  271. * returns array with tables of given db with extended information and grouped
  272. *
  273. * @param string $db
  274. *
  275. * @return array (recursive) grouped table list
  276. */
  277. public static function getTableList($db): array
  278. {
  279. global $dbi;
  280. $sep = $GLOBALS['cfg']['NavigationTreeTableSeparator'];
  281. $tables = $dbi->getTablesFull($db);
  282. if ($GLOBALS['cfg']['NaturalOrder']) {
  283. uksort($tables, 'strnatcasecmp');
  284. }
  285. if (count($tables) < 1) {
  286. return $tables;
  287. }
  288. $default = [
  289. 'Name' => '',
  290. 'Rows' => 0,
  291. 'Comment' => '',
  292. 'disp_name' => '',
  293. ];
  294. $tableGroups = [];
  295. foreach ($tables as $tableName => $table) {
  296. $table['Rows'] = self::checkRowCount($db, $table);
  297. // in $group we save the reference to the place in $table_groups
  298. // where to store the table info
  299. if ($GLOBALS['cfg']['NavigationTreeEnableGrouping'] && $sep && mb_strstr($tableName, $sep)) {
  300. $parts = explode($sep, $tableName);
  301. $group =& $tableGroups;
  302. $i = 0;
  303. $groupNameFull = '';
  304. $partsCount = count($parts) - 1;
  305. while (($i < $partsCount) && ($i < $GLOBALS['cfg']['NavigationTreeTableLevel'])) {
  306. $groupName = $parts[$i] . $sep;
  307. $groupNameFull .= $groupName;
  308. if (! isset($group[$groupName])) {
  309. $group[$groupName] = [];
  310. $group[$groupName]['is' . $sep . 'group'] = true;
  311. $group[$groupName]['tab' . $sep . 'count'] = 1;
  312. $group[$groupName]['tab' . $sep . 'group'] = $groupNameFull;
  313. } elseif (! isset($group[$groupName]['is' . $sep . 'group'])) {
  314. $table = $group[$groupName];
  315. $group[$groupName] = [];
  316. $group[$groupName][$groupName] = $table;
  317. $group[$groupName]['is' . $sep . 'group'] = true;
  318. $group[$groupName]['tab' . $sep . 'count'] = 1;
  319. $group[$groupName]['tab' . $sep . 'group'] = $groupNameFull;
  320. } else {
  321. $group[$groupName]['tab' . $sep . 'count']++;
  322. }
  323. $group =& $group[$groupName];
  324. $i++;
  325. }
  326. } else {
  327. if (! isset($tableGroups[$tableName])) {
  328. $tableGroups[$tableName] = [];
  329. }
  330. $group =& $tableGroups;
  331. }
  332. $table['disp_name'] = $table['Name'];
  333. $group[$tableName] = array_merge($default, $table);
  334. }
  335. return $tableGroups;
  336. }
  337. /* ----------------------- Set of misc functions ----------------------- */
  338. /**
  339. * Adds backquotes on both sides of a database, table or field name.
  340. * and escapes backquotes inside the name with another backquote
  341. *
  342. * example:
  343. * <code>
  344. * echo backquote('owner`s db'); // `owner``s db`
  345. *
  346. * </code>
  347. *
  348. * @param array|string $aName the database, table or field name to "backquote" or array of it
  349. *
  350. * @return mixed the "backquoted" database, table or field name
  351. *
  352. * @access public
  353. */
  354. public static function backquote($aName)
  355. {
  356. return static::backquoteCompat($aName, 'NONE', true);
  357. }
  358. /**
  359. * Adds backquotes on both sides of a database, table or field name.
  360. * in compatibility mode
  361. *
  362. * example:
  363. * <code>
  364. * echo backquoteCompat('owner`s db'); // `owner``s db`
  365. *
  366. * </code>
  367. *
  368. * @param array|string $aName the database, table or field name to "backquote" or array of it
  369. * @param string $compatibility string compatibility mode (used by dump functions)
  370. * @param bool $doIt a flag to bypass this function (used by dump functions)
  371. *
  372. * @return mixed the "backquoted" database, table or field name
  373. *
  374. * @access public
  375. */
  376. public static function backquoteCompat(
  377. $aName,
  378. string $compatibility = 'MSSQL',
  379. $doIt = true
  380. ) {
  381. if (is_array($aName)) {
  382. foreach ($aName as &$data) {
  383. $data = self::backquoteCompat($data, $compatibility, $doIt);
  384. }
  385. return $aName;
  386. }
  387. if (! $doIt) {
  388. if (! (Context::isKeyword($aName) & Token::FLAG_KEYWORD_RESERVED)) {
  389. return $aName;
  390. }
  391. }
  392. // @todo add more compatibility cases (ORACLE for example)
  393. switch ($compatibility) {
  394. case 'MSSQL':
  395. $quote = '"';
  396. $escapeChar = '\\';
  397. break;
  398. default:
  399. $quote = '`';
  400. $escapeChar = '`';
  401. break;
  402. }
  403. // '0' is also empty for php :-(
  404. if (strlen((string) $aName) > 0 && $aName !== '*') {
  405. return $quote . str_replace($quote, $escapeChar . $quote, (string) $aName) . $quote;
  406. }
  407. return $aName;
  408. }
  409. /**
  410. * Formats $value to byte view
  411. *
  412. * @param float|int|string|null $value the value to format
  413. * @param int $limes the sensitiveness
  414. * @param int $comma the number of decimals to retain
  415. *
  416. * @return array|null the formatted value and its unit
  417. *
  418. * @access public
  419. */
  420. public static function formatByteDown($value, $limes = 6, $comma = 0): ?array
  421. {
  422. if ($value === null) {
  423. return null;
  424. }
  425. if (is_string($value)) {
  426. $value = (float) $value;
  427. }
  428. $byteUnits = [
  429. /* l10n: shortcuts for Byte */
  430. __('B'),
  431. /* l10n: shortcuts for Kilobyte */
  432. __('KiB'),
  433. /* l10n: shortcuts for Megabyte */
  434. __('MiB'),
  435. /* l10n: shortcuts for Gigabyte */
  436. __('GiB'),
  437. /* l10n: shortcuts for Terabyte */
  438. __('TiB'),
  439. /* l10n: shortcuts for Petabyte */
  440. __('PiB'),
  441. /* l10n: shortcuts for Exabyte */
  442. __('EiB'),
  443. ];
  444. $dh = 10 ** $comma;
  445. $li = 10 ** $limes;
  446. $unit = $byteUnits[0];
  447. for ($d = 6, $ex = 15; $d >= 1; $d--, $ex -= 3) {
  448. $unitSize = $li * 10 ** $ex;
  449. if (isset($byteUnits[$d]) && $value >= $unitSize) {
  450. // use 1024.0 to avoid integer overflow on 64-bit machines
  451. $value = round($value / (1024 ** $d / $dh)) / $dh;
  452. $unit = $byteUnits[$d];
  453. break 1;
  454. }
  455. }
  456. if ($unit != $byteUnits[0]) {
  457. // if the unit is not bytes (as represented in current language)
  458. // reformat with max length of 5
  459. // 4th parameter=true means do not reformat if value < 1
  460. $returnValue = self::formatNumber($value, 5, $comma, true, false);
  461. } else {
  462. // do not reformat, just handle the locale
  463. $returnValue = self::formatNumber($value, 0);
  464. }
  465. return [
  466. trim($returnValue),
  467. $unit,
  468. ];
  469. }
  470. /**
  471. * Formats $value to the given length and appends SI prefixes
  472. * with a $length of 0 no truncation occurs, number is only formatted
  473. * to the current locale
  474. *
  475. * examples:
  476. * <code>
  477. * echo formatNumber(123456789, 6); // 123,457 k
  478. * echo formatNumber(-123456789, 4, 2); // -123.46 M
  479. * echo formatNumber(-0.003, 6); // -3 m
  480. * echo formatNumber(0.003, 3, 3); // 0.003
  481. * echo formatNumber(0.00003, 3, 2); // 0.03 m
  482. * echo formatNumber(0, 6); // 0
  483. * </code>
  484. *
  485. * @param float|int|string $value the value to format
  486. * @param int $digitsLeft number of digits left of the comma
  487. * @param int $digitsRight number of digits right of the comma
  488. * @param bool $onlyDown do not reformat numbers below 1
  489. * @param bool $noTrailingZero removes trailing zeros right of the comma (default: true)
  490. *
  491. * @return string the formatted value and its unit
  492. *
  493. * @access public
  494. */
  495. public static function formatNumber(
  496. $value,
  497. $digitsLeft = 3,
  498. $digitsRight = 0,
  499. $onlyDown = false,
  500. $noTrailingZero = true
  501. ) {
  502. if ($value == 0) {
  503. return '0';
  504. }
  505. if (is_string($value)) {
  506. $value = (float) $value;
  507. }
  508. $originalValue = $value;
  509. //number_format is not multibyte safe, str_replace is safe
  510. if ($digitsLeft === 0) {
  511. $value = number_format(
  512. (float) $value,
  513. $digitsRight,
  514. /* l10n: Decimal separator */
  515. __('.'),
  516. /* l10n: Thousands separator */
  517. __(',')
  518. );
  519. if (($originalValue != 0) && (floatval($value) == 0)) {
  520. $value = ' <' . (1 / 10 ** $digitsRight);
  521. }
  522. return $value;
  523. }
  524. // this units needs no translation, ISO
  525. $units = [
  526. -8 => 'y',
  527. -7 => 'z',
  528. -6 => 'a',
  529. -5 => 'f',
  530. -4 => 'p',
  531. -3 => 'n',
  532. -2 => 'µ',
  533. -1 => 'm',
  534. 0 => ' ',
  535. 1 => 'k',
  536. 2 => 'M',
  537. 3 => 'G',
  538. 4 => 'T',
  539. 5 => 'P',
  540. 6 => 'E',
  541. 7 => 'Z',
  542. 8 => 'Y',
  543. ];
  544. /* l10n: Decimal separator */
  545. $decimalSep = __('.');
  546. /* l10n: Thousands separator */
  547. $thousandsSep = __(',');
  548. // check for negative value to retain sign
  549. if ($value < 0) {
  550. $sign = '-';
  551. $value = abs($value);
  552. } else {
  553. $sign = '';
  554. }
  555. $dh = 10 ** $digitsRight;
  556. /*
  557. * This gives us the right SI prefix already,
  558. * but $digits_left parameter not incorporated
  559. */
  560. $d = floor(log10((float) $value) / 3);
  561. /*
  562. * Lowering the SI prefix by 1 gives us an additional 3 zeros
  563. * So if we have 3,6,9,12.. free digits ($digits_left - $cur_digits)
  564. * to use, then lower the SI prefix
  565. */
  566. $curDigits = floor(log10($value / 1000 ** $d) + 1);
  567. if ($digitsLeft > $curDigits) {
  568. $d -= floor(($digitsLeft - $curDigits) / 3);
  569. }
  570. if ($d < 0 && $onlyDown) {
  571. $d = 0;
  572. }
  573. $value = round($value / (1000 ** $d / $dh)) / $dh;
  574. $unit = $units[$d];
  575. // number_format is not multibyte safe, str_replace is safe
  576. $formattedValue = number_format($value, $digitsRight, $decimalSep, $thousandsSep);
  577. // If we don't want any zeros, remove them now
  578. if ($noTrailingZero && str_contains($formattedValue, $decimalSep)) {
  579. $formattedValue = preg_replace('/' . preg_quote($decimalSep, '/') . '?0+$/', '', $formattedValue);
  580. }
  581. if ($originalValue != 0 && floatval($value) == 0) {
  582. return ' <' . number_format(1 / 10 ** $digitsRight, $digitsRight, $decimalSep, $thousandsSep) . ' ' . $unit;
  583. }
  584. return $sign . $formattedValue . ' ' . $unit;
  585. }
  586. /**
  587. * Returns the number of bytes when a formatted size is given
  588. *
  589. * @param string|int $formattedSize the size expression (for example 8MB)
  590. *
  591. * @return int|float The numerical part of the expression (for example 8)
  592. */
  593. public static function extractValueFromFormattedSize($formattedSize)
  594. {
  595. $returnValue = -1;
  596. $formattedSize = (string) $formattedSize;
  597. if (preg_match('/^[0-9]+GB$/', $formattedSize)) {
  598. $returnValue = (int) mb_substr($formattedSize, 0, -2) * 1024 ** 3;
  599. } elseif (preg_match('/^[0-9]+MB$/', $formattedSize)) {
  600. $returnValue = (int) mb_substr($formattedSize, 0, -2) * 1024 ** 2;
  601. } elseif (preg_match('/^[0-9]+K$/', $formattedSize)) {
  602. $returnValue = (int) mb_substr($formattedSize, 0, -1) * 1024 ** 1;
  603. }
  604. return $returnValue;
  605. }
  606. /**
  607. * Writes localised date
  608. *
  609. * @param int $timestamp the current timestamp
  610. * @param string $format format
  611. *
  612. * @return string the formatted date
  613. *
  614. * @access public
  615. */
  616. public static function localisedDate($timestamp = -1, $format = '')
  617. {
  618. $month = [
  619. /* l10n: Short month name */
  620. __('Jan'),
  621. /* l10n: Short month name */
  622. __('Feb'),
  623. /* l10n: Short month name */
  624. __('Mar'),
  625. /* l10n: Short month name */
  626. __('Apr'),
  627. /* l10n: Short month name */
  628. _pgettext('Short month name', 'May'),
  629. /* l10n: Short month name */
  630. __('Jun'),
  631. /* l10n: Short month name */
  632. __('Jul'),
  633. /* l10n: Short month name */
  634. __('Aug'),
  635. /* l10n: Short month name */
  636. __('Sep'),
  637. /* l10n: Short month name */
  638. __('Oct'),
  639. /* l10n: Short month name */
  640. __('Nov'),
  641. /* l10n: Short month name */
  642. __('Dec'),
  643. ];
  644. $dayOfWeek = [
  645. /* l10n: Short week day name for Sunday */
  646. _pgettext('Short week day name for Sunday', 'Sun'),
  647. /* l10n: Short week day name for Monday */
  648. __('Mon'),
  649. /* l10n: Short week day name for Tuesday */
  650. __('Tue'),
  651. /* l10n: Short week day name for Wednesday */
  652. __('Wed'),
  653. /* l10n: Short week day name for Thursday */
  654. __('Thu'),
  655. /* l10n: Short week day name for Friday */
  656. __('Fri'),
  657. /* l10n: Short week day name for Saturday */
  658. __('Sat'),
  659. ];
  660. if ($format == '') {
  661. /* l10n: See https://www.php.net/manual/en/function.strftime.php */
  662. $format = __('%B %d, %Y at %I:%M %p');
  663. }
  664. if ($timestamp == -1) {
  665. $timestamp = time();
  666. }
  667. $date = (string) preg_replace(
  668. '@%[aA]@',
  669. $dayOfWeek[(int) @strftime('%w', (int) $timestamp)],
  670. $format
  671. );
  672. $date = (string) preg_replace(
  673. '@%[bB]@',
  674. $month[(int) @strftime('%m', (int) $timestamp) - 1],
  675. $date
  676. );
  677. /* Fill in AM/PM */
  678. $hours = (int) date('H', (int) $timestamp);
  679. if ($hours >= 12) {
  680. $amPm = _pgettext('AM/PM indication in time', 'PM');
  681. } else {
  682. $amPm = _pgettext('AM/PM indication in time', 'AM');
  683. }
  684. $date = (string) preg_replace('@%[pP]@', $amPm, $date);
  685. // Can return false on windows for Japanese language
  686. // See https://github.com/phpmyadmin/phpmyadmin/issues/15830
  687. $ret = @strftime($date, (int) $timestamp);
  688. // Some OSes such as Win8.1 Traditional Chinese version did not produce UTF-8
  689. // output here. See https://github.com/phpmyadmin/phpmyadmin/issues/10598
  690. if ($ret === false || mb_detect_encoding($ret, 'UTF-8', true) !== 'UTF-8') {
  691. $ret = date('Y-m-d H:i:s', (int) $timestamp);
  692. }
  693. return $ret;
  694. }
  695. /**
  696. * Splits a URL string by parameter
  697. *
  698. * @param string $url the URL
  699. *
  700. * @return array<int, string> the parameter/value pairs, for example [0] db=sakila
  701. */
  702. public static function splitURLQuery($url): array
  703. {
  704. // decode encoded url separators
  705. $separator = Url::getArgSeparator();
  706. // on most places separator is still hard coded ...
  707. if ($separator !== '&') {
  708. // ... so always replace & with $separator
  709. $url = str_replace([htmlentities('&'), '&'], [$separator, $separator], $url);
  710. }
  711. $url = str_replace(htmlentities($separator), $separator, $url);
  712. // end decode
  713. $urlParts = parse_url($url);
  714. if (is_array($urlParts) && isset($urlParts['query']) && strlen($separator) > 0) {
  715. return explode($separator, $urlParts['query']);
  716. }
  717. return [];
  718. }
  719. /**
  720. * Returns a given timespan value in a readable format.
  721. *
  722. * @param int $seconds the timespan
  723. *
  724. * @return string the formatted value
  725. */
  726. public static function timespanFormat($seconds): string
  727. {
  728. $days = floor($seconds / 86400);
  729. if ($days > 0) {
  730. $seconds -= $days * 86400;
  731. }
  732. $hours = floor($seconds / 3600);
  733. if ($days > 0 || $hours > 0) {
  734. $seconds -= $hours * 3600;
  735. }
  736. $minutes = floor($seconds / 60);
  737. if ($days > 0 || $hours > 0 || $minutes > 0) {
  738. $seconds -= $minutes * 60;
  739. }
  740. return sprintf(
  741. __('%s days, %s hours, %s minutes and %s seconds'),
  742. (string) $days,
  743. (string) $hours,
  744. (string) $minutes,
  745. (string) $seconds
  746. );
  747. }
  748. /**
  749. * Function added to avoid path disclosures.
  750. * Called by each script that needs parameters, it displays
  751. * an error message and, by default, stops the execution.
  752. *
  753. * @param string[] $params The names of the parameters needed by the calling
  754. * script
  755. * @param bool $request Check parameters in request
  756. *
  757. * @access public
  758. */
  759. public static function checkParameters($params, $request = false): void
  760. {
  761. $reportedScriptName = basename($GLOBALS['PMA_PHP_SELF']);
  762. $foundError = false;
  763. $errorMessage = '';
  764. if ($request) {
  765. $array = $_REQUEST;
  766. } else {
  767. $array = $GLOBALS;
  768. }
  769. foreach ($params as $param) {
  770. if (isset($array[$param])) {
  771. continue;
  772. }
  773. $errorMessage .= $reportedScriptName
  774. . ': ' . __('Missing parameter:') . ' '
  775. . $param
  776. . MySQLDocumentation::showDocumentation('faq', 'faqmissingparameters', true)
  777. . '[br]';
  778. $foundError = true;
  779. }
  780. if (! $foundError) {
  781. return;
  782. }
  783. Core::fatalError($errorMessage);
  784. }
  785. /**
  786. * Build a condition and with a value
  787. *
  788. * @param string|int|float|null $row The row value
  789. * @param FieldMetadata $meta The field metadata
  790. * @param int $fieldsCount A number of fields
  791. * @param string $conditionKey A key used for BINARY fields functions
  792. * @param string $condition The condition
  793. *
  794. * @return array<int,string|null>
  795. */
  796. private static function getConditionValue(
  797. $row,
  798. FieldMetadata $meta,
  799. int $fieldsCount,
  800. string $conditionKey,
  801. string $condition
  802. ): array {
  803. global $dbi;
  804. if ($row === null) {
  805. return ['IS NULL', $condition];
  806. }
  807. $conditionValue = '';
  808. $isBinaryString = $meta->isType(FieldMetadata::TYPE_STRING) && $meta->isBinary();
  809. // 63 is the binary charset, see: https://dev.mysql.com/doc/internals/en/charsets.html
  810. $isBlobAndIsBinaryCharset = $meta->isType(FieldMetadata::TYPE_BLOB) && $meta->charsetnr === 63;
  811. // timestamp is numeric on some MySQL 4.1
  812. // for real we use CONCAT above and it should compare to string
  813. // See commit: 049fc7fef7548c2ba603196937c6dcaf9ff9bf00
  814. // See bug: https://sourceforge.net/p/phpmyadmin/bugs/3064/
  815. if ($meta->isNumeric && ! $meta->isMappedTypeTimestamp && $meta->isNotType(FieldMetadata::TYPE_REAL)) {
  816. $conditionValue = '= ' . $row;
  817. } elseif ($isBlobAndIsBinaryCharset || (! empty($row) && $isBinaryString)) {
  818. // hexify only if this is a true not empty BLOB or a BINARY
  819. // do not waste memory building a too big condition
  820. $rowLength = mb_strlen((string) $row);
  821. if ($rowLength > 0 && $rowLength < 1000) {
  822. // use a CAST if possible, to avoid problems
  823. // if the field contains wildcard characters % or _
  824. $conditionValue = '= CAST(0x' . bin2hex((string) $row) . ' AS BINARY)';
  825. } elseif ($fieldsCount === 1) {
  826. // when this blob is the only field present
  827. // try settling with length comparison
  828. $condition = ' CHAR_LENGTH(' . $conditionKey . ') ';
  829. $conditionValue = ' = ' . $rowLength;
  830. } else {
  831. // this blob won't be part of the final condition
  832. $conditionValue = null;
  833. }
  834. } elseif ($meta->isMappedTypeGeometry && ! empty($row)) {
  835. // do not build a too big condition
  836. if (mb_strlen((string) $row) < 5000) {
  837. $condition .= '=0x' . bin2hex((string) $row) . ' AND';
  838. } else {
  839. $condition = '';
  840. }
  841. } elseif ($meta->isMappedTypeBit) {
  842. $conditionValue = "= b'"
  843. . self::printableBitValue((int) $row, (int) $meta->length) . "'";
  844. } else {
  845. $conditionValue = '= \''
  846. . $dbi->escapeString($row) . '\'';
  847. }
  848. return [$conditionValue, $condition];
  849. }
  850. /**
  851. * Function to generate unique condition for specified row.
  852. *
  853. * @param resource|int $handle current query result
  854. * @param int $fieldsCount number of fields
  855. * @param FieldMetadata[] $fieldsMeta meta information about fields
  856. * @param array $row current row
  857. * @param bool $forceUnique generate condition only on pk or unique
  858. * @param string|bool $restrictToTable restrict the unique condition to this table or false if none
  859. * @param Expression[] $expressions An array of Expression instances.
  860. *
  861. * @return array the calculated condition and whether condition is unique
  862. */
  863. public static function getUniqueCondition(
  864. $handle,
  865. $fieldsCount,
  866. array $fieldsMeta,
  867. array $row,
  868. $forceUnique = false,
  869. $restrictToTable = false,
  870. array $expressions = []
  871. ): array {
  872. global $dbi;
  873. $primaryKey = '';
  874. $uniqueKey = '';
  875. $nonPrimaryCondition = '';
  876. $preferredCondition = '';
  877. $primaryKeyArray = [];
  878. $uniqueKeyArray = [];
  879. $nonPrimaryConditionArray = [];
  880. $conditionArray = [];
  881. for ($i = 0; $i < $fieldsCount; ++$i) {
  882. $meta = $fieldsMeta[$i];
  883. // do not use a column alias in a condition
  884. if (! isset($meta->orgname) || strlen($meta->orgname) === 0) {
  885. $meta->orgname = $meta->name;
  886. foreach ($expressions as $expression) {
  887. if (empty($expression->alias) || empty($expression->column)) {
  888. continue;
  889. }
  890. if (strcasecmp($meta->name, $expression->alias) == 0) {
  891. $meta->orgname = $expression->column;
  892. break;
  893. }
  894. }
  895. }
  896. // Do not use a table alias in a condition.
  897. // Test case is:
  898. // select * from galerie x WHERE
  899. //(select count(*) from galerie y where y.datum=x.datum)>1
  900. //
  901. // But orgtable is present only with mysqli extension so the
  902. // fix is only for mysqli.
  903. // Also, do not use the original table name if we are dealing with
  904. // a view because this view might be updatable.
  905. // (The isView() verification should not be costly in most cases
  906. // because there is some caching in the function).
  907. if (
  908. isset($meta->orgtable)
  909. && ($meta->table != $meta->orgtable)
  910. && ! $dbi->getTable($GLOBALS['db'], $meta->table)->isView()
  911. ) {
  912. $meta->table = $meta->orgtable;
  913. }
  914. // If this field is not from the table which the unique clause needs
  915. // to be restricted to.
  916. if ($restrictToTable && $restrictToTable != $meta->table) {
  917. continue;
  918. }
  919. // to fix the bug where float fields (primary or not)
  920. // can't be matched because of the imprecision of
  921. // floating comparison, use CONCAT
  922. // (also, the syntax "CONCAT(field) IS NULL"
  923. // that we need on the next "if" will work)
  924. if ($meta->isType(FieldMetadata::TYPE_REAL)) {
  925. $conKey = 'CONCAT(' . self::backquote($meta->table) . '.'
  926. . self::backquote($meta->orgname) . ')';
  927. } else {
  928. $conKey = self::backquote($meta->table) . '.'
  929. . self::backquote($meta->orgname);
  930. }
  931. $condition = ' ' . $conKey . ' ';
  932. [$conVal, $condition] = self::getConditionValue($row[$i] ?? null, $meta, $fieldsCount, $conKey, $condition);
  933. if ($conVal === null) {
  934. continue;
  935. }
  936. $condition .= $conVal . ' AND';
  937. if ($meta->isPrimaryKey()) {
  938. $primaryKey .= $condition;
  939. $primaryKeyArray[$conKey] = $conVal;
  940. } elseif ($meta->isUniqueKey()) {
  941. $uniqueKey .= $condition;
  942. $uniqueKeyArray[$conKey] = $conVal;
  943. }
  944. $nonPrimaryCondition .= $condition;
  945. $nonPrimaryConditionArray[$conKey] = $conVal;
  946. }
  947. // Correction University of Virginia 19991216:
  948. // prefer primary or unique keys for condition,
  949. // but use conjunction of all values if no primary key
  950. $clauseIsUnique = true;
  951. if ($primaryKey) {
  952. $preferredCondition = $primaryKey;
  953. $conditionArray = $primaryKeyArray;
  954. } elseif ($uniqueKey) {
  955. $preferredCondition = $uniqueKey;
  956. $conditionArray = $uniqueKeyArray;
  957. } elseif (! $forceUnique) {
  958. $preferredCondition = $nonPrimaryCondition;
  959. $conditionArray = $nonPrimaryConditionArray;
  960. $clauseIsUnique = false;
  961. }
  962. $whereClause = trim((string) preg_replace('|\s?AND$|', '', $preferredCondition));
  963. return [
  964. $whereClause,
  965. $clauseIsUnique,
  966. $conditionArray,
  967. ];
  968. }
  969. /**
  970. * Generate the charset query part
  971. *
  972. * @param string $collation Collation
  973. * @param bool $override (optional) force 'CHARACTER SET' keyword
  974. */
  975. public static function getCharsetQueryPart(string $collation, bool $override = false): string
  976. {
  977. [$charset] = explode('_', $collation);
  978. $keyword = ' CHARSET=';
  979. if ($override) {
  980. $keyword = ' CHARACTER SET ';
  981. }
  982. return $keyword . $charset
  983. . ($charset == $collation ? '' : ' COLLATE ' . $collation);
  984. }
  985. /**
  986. * Generate a pagination selector for browsing resultsets
  987. *
  988. * @param string $name The name for the request parameter
  989. * @param int $rows Number of rows in the pagination set
  990. * @param int $pageNow current page number
  991. * @param int $nbTotalPage number of total pages
  992. * @param int $showAll If the number of pages is lower than this
  993. * variable, no pages will be omitted in pagination
  994. * @param int $sliceStart How many rows at the beginning should always
  995. * be shown?
  996. * @param int $sliceEnd How many rows at the end should always be shown?
  997. * @param int $percent Percentage of calculation page offsets to hop to a
  998. * next page
  999. * @param int $range Near the current page, how many pages should
  1000. * be considered "nearby" and displayed as well?
  1001. * @param string $prompt The prompt to display (sometimes empty)
  1002. *
  1003. * @access public
  1004. */
  1005. public static function pageselector(
  1006. $name,
  1007. $rows,
  1008. $pageNow = 1,
  1009. $nbTotalPage = 1,
  1010. $showAll = 200,
  1011. $sliceStart = 5,
  1012. $sliceEnd = 5,
  1013. $percent = 20,
  1014. $range = 10,
  1015. $prompt = ''
  1016. ): string {
  1017. $increment = floor($nbTotalPage / $percent);
  1018. $pageNowMinusRange = $pageNow - $range;
  1019. $pageNowPlusRange = $pageNow + $range;
  1020. $gotoPage = $prompt . ' <select class="pageselector ajax"';
  1021. $gotoPage .= ' name="' . $name . '" >';
  1022. if ($nbTotalPage < $showAll) {
  1023. $pages = range(1, $nbTotalPage);
  1024. } else {
  1025. $pages = [];
  1026. // Always show first X pages
  1027. for ($i = 1; $i <= $sliceStart; $i++) {
  1028. $pages[] = $i;
  1029. }
  1030. // Always show last X pages
  1031. for ($i = $nbTotalPage - $sliceEnd; $i <= $nbTotalPage; $i++) {
  1032. $pages[] = $i;
  1033. }
  1034. // Based on the number of results we add the specified
  1035. // $percent percentage to each page number,
  1036. // so that we have a representing page number every now and then to
  1037. // immediately jump to specific pages.
  1038. // As soon as we get near our currently chosen page ($pageNow -
  1039. // $range), every page number will be shown.
  1040. $i = $sliceStart;
  1041. $x = $nbTotalPage - $sliceEnd;
  1042. $metBoundary = false;
  1043. while ($i <= $x) {
  1044. if ($i >= $pageNowMinusRange && $i <= $pageNowPlusRange) {
  1045. // If our pageselector comes near the current page, we use 1
  1046. // counter increments
  1047. $i++;
  1048. $metBoundary = true;
  1049. } else {
  1050. // We add the percentage increment to our current page to
  1051. // hop to the next one in range
  1052. $i += $increment;
  1053. // Make sure that we do not cross our boundaries.
  1054. if ($i > $pageNowMinusRange && ! $metBoundary) {
  1055. $i = $pageNowMinusRange;
  1056. }
  1057. }
  1058. if ($i <= 0 || $i > $x) {
  1059. continue;
  1060. }
  1061. $pages[] = $i;
  1062. }
  1063. /*
  1064. Add page numbers with "geometrically increasing" distances.
  1065. This helps me a lot when navigating through giant tables.
  1066. Test case: table with 2.28 million sets, 76190 pages. Page of interest
  1067. is between 72376 and 76190.
  1068. Selecting page 72376.
  1069. Now, old version enumerated only +/- 10 pages around 72376 and the
  1070. percentage increment produced steps of about 3000.
  1071. The following code adds page numbers +/- 2,4,8,16,32,64,128,256 etc.
  1072. around the current page.
  1073. */
  1074. $i = $pageNow;
  1075. $dist = 1;
  1076. while ($i < $x) {
  1077. $dist = 2 * $dist;
  1078. $i = $pageNow + $dist;
  1079. if ($i <= 0 || $i > $x) {
  1080. continue;
  1081. }
  1082. $pages[] = $i;
  1083. }
  1084. $i = $pageNow;
  1085. $dist = 1;
  1086. while ($i > 0) {
  1087. $dist = 2 * $dist;
  1088. $i = $pageNow - $dist;
  1089. if ($i <= 0 || $i > $x) {
  1090. continue;
  1091. }
  1092. $pages[] = $i;
  1093. }
  1094. // Since because of ellipsing of the current page some numbers may be
  1095. // double, we unify our array:
  1096. sort($pages);
  1097. $pages = array_unique($pages);
  1098. }
  1099. if ($pageNow > $nbTotalPage) {
  1100. $pages[] = $pageNow;
  1101. }
  1102. foreach ($pages as $i) {
  1103. if ($i == $pageNow) {
  1104. $selected = 'selected="selected" style="font-weight: bold"';
  1105. } else {
  1106. $selected = '';
  1107. }
  1108. $gotoPage .= ' <option ' . $selected
  1109. . ' value="' . (($i - 1) * $rows) . '">' . $i . '</option>' . "\n";
  1110. }
  1111. $gotoPage .= ' </select>';
  1112. return $gotoPage;
  1113. }
  1114. /**
  1115. * Calculate page number through position
  1116. *
  1117. * @param int $pos position of first item
  1118. * @param int $maxCount number of items per page
  1119. *
  1120. * @return int $page_num
  1121. *
  1122. * @access public
  1123. */
  1124. public static function getPageFromPosition($pos, $maxCount)
  1125. {
  1126. return (int) floor($pos / $maxCount) + 1;
  1127. }
  1128. /**
  1129. * replaces %u in given path with current user name
  1130. *
  1131. * example:
  1132. * <code>
  1133. * $user_dir = userDir('/var/pma_tmp/%u/'); // '/var/pma_tmp/root/'
  1134. *
  1135. * </code>
  1136. *
  1137. * @param string $dir with wildcard for user
  1138. *
  1139. * @return string per user directory
  1140. */
  1141. public static function userDir(string $dir): string
  1142. {
  1143. // add trailing slash
  1144. if (mb_substr($dir, -1) !== '/') {
  1145. $dir .= '/';
  1146. }
  1147. return str_replace('%u', Core::securePath($GLOBALS['cfg']['Server']['user']), $dir);
  1148. }
  1149. /**
  1150. * Clears cache content which needs to be refreshed on user change.
  1151. */
  1152. public static function clearUserCache(): void
  1153. {
  1154. SessionCache::remove('is_superuser');
  1155. SessionCache::remove('is_createuser');
  1156. SessionCache::remove('is_grantuser');
  1157. }
  1158. /**
  1159. * Converts a bit value to printable format;
  1160. * in MySQL a BIT field can be from 1 to 64 bits so we need this
  1161. * function because in PHP, decbin() supports only 32 bits
  1162. * on 32-bit servers
  1163. *
  1164. * @param int $value coming from a BIT field
  1165. * @param int $length length
  1166. *
  1167. * @return string the printable value
  1168. */
  1169. public static function printableBitValue(int $value, int $length): string
  1170. {
  1171. // if running on a 64-bit server or the length is safe for decbin()
  1172. if (PHP_INT_SIZE == 8 || $length < 33) {
  1173. $printable = decbin($value);
  1174. } else {
  1175. // FIXME: does not work for the leftmost bit of a 64-bit value
  1176. $i = 0;
  1177. $printable = '';
  1178. while ($value >= 2 ** $i) {
  1179. ++$i;
  1180. }
  1181. if ($i != 0) {
  1182. --$i;
  1183. }
  1184. while ($i >= 0) {
  1185. if ($value - 2 ** $i < 0) {
  1186. $printable = '0' . $printable;
  1187. } else {
  1188. $printable = '1' . $printable;
  1189. $value -= 2 ** $i;
  1190. }
  1191. --$i;
  1192. }
  1193. $printable = strrev($printable);
  1194. }
  1195. $printable = str_pad($printable, $length, '0', STR_PAD_LEFT);
  1196. return $printable;
  1197. }
  1198. /**
  1199. * Converts a BIT type default value
  1200. * for example, b'010' becomes 010
  1201. *
  1202. * @param string|null $bitDefaultValue value
  1203. *
  1204. * @return string the converted value
  1205. */
  1206. public static function convertBitDefaultValue(?string $bitDefaultValue): string
  1207. {
  1208. return (string) preg_replace(
  1209. "/^b'(\d*)'?$/",
  1210. '$1',
  1211. htmlspecialchars_decode((string) $bitDefaultValue, ENT_QUOTES),
  1212. 1
  1213. );
  1214. }
  1215. /**
  1216. * Extracts the various parts from a column spec
  1217. *
  1218. * @param string $columnSpecification Column specification
  1219. *
  1220. * @return array associative array containing type, spec_in_brackets
  1221. * and possibly enum_set_values (another array)
  1222. */
  1223. public static function extractColumnSpec($columnSpecification)
  1224. {
  1225. $firstBracketPos = mb_strpos($columnSpecification, '(');
  1226. if ($firstBracketPos) {
  1227. $specInBrackets = rtrim(
  1228. mb_substr(
  1229. $columnSpecification,
  1230. $firstBracketPos + 1,
  1231. mb_strrpos($columnSpecification, ')') - $firstBracketPos - 1
  1232. )
  1233. );
  1234. // convert to lowercase just to be sure
  1235. $type = mb_strtolower(
  1236. rtrim(mb_substr($columnSpecification, 0, $firstBracketPos))
  1237. );
  1238. } else {
  1239. // Split trailing attributes such as unsigned,
  1240. // binary, zerofill and get data type name
  1241. $typeParts = explode(' ', $columnSpecification);
  1242. $type = mb_strtolower($typeParts[0]);
  1243. $specInBrackets = '';
  1244. }
  1245. if ($type === 'enum' || $type === 'set') {
  1246. // Define our working vars
  1247. $enumSetValues = self::parseEnumSetValues($columnSpecification, false);
  1248. $printType = $type
  1249. . '(' . str_replace("','", "', '", $specInBrackets) . ')';
  1250. $binary = false;
  1251. $unsigned = false;
  1252. $zerofill = false;
  1253. } else {
  1254. $enumSetValues = [];
  1255. /* Create printable type name */
  1256. $printType = mb_strtolower($columnSpecification);
  1257. // Strip the "BINARY" attribute, except if we find "BINARY(" because
  1258. // this would be a BINARY or VARBINARY column type;
  1259. // by the way, a BLOB should not show the BINARY attribute
  1260. // because this is not accepted in MySQL syntax.
  1261. if (str_contains($printType, 'binary') && ! preg_match('@binary[\(]@', $printType)) {
  1262. $printType = str_replace('binary', '', $printType);
  1263. $binary = true;
  1264. } else {
  1265. $binary = false;
  1266. }
  1267. $printType = (string) preg_replace('@zerofill@', '', $printType, -1, $zerofillCount);
  1268. $zerofill = ($zerofillCount > 0);
  1269. $printType = (string) preg_replace('@unsigned@', '', $printType, -1, $unsignedCount);
  1270. $unsigned = ($unsignedCount > 0);
  1271. $printType = trim($printType);
  1272. }
  1273. $attribute = ' ';
  1274. if ($binary) {
  1275. $attribute = 'BINARY';
  1276. }
  1277. if ($unsigned) {
  1278. $attribute = 'UNSIGNED';
  1279. }
  1280. if ($zerofill) {
  1281. $attribute = 'UNSIGNED ZEROFILL';
  1282. }
  1283. $canContainCollation = false;
  1284. if (! $binary && preg_match('@^(char|varchar|text|tinytext|mediumtext|longtext|set|enum)@', $type)) {
  1285. $canContainCollation = true;
  1286. }
  1287. // for the case ENUM('&#8211;','&ldquo;')
  1288. $displayedType = htmlspecialchars($printType, ENT_COMPAT);
  1289. if (mb_strlen($printType) > $GLOBALS['cfg']['LimitChars']) {
  1290. $displayedType = '<abbr title="' . htmlspecialchars($printType) . '">';
  1291. $displayedType .= htmlspecialchars(
  1292. mb_substr(
  1293. $printType,
  1294. 0,
  1295. (int) $GLOBALS['cfg']['LimitChars']
  1296. ) . '...',
  1297. ENT_COMPAT
  1298. );
  1299. $displayedType .= '</abbr>';
  1300. }
  1301. return [
  1302. 'type' => $type,
  1303. 'spec_in_brackets' => $specInBrackets,
  1304. 'enum_set_values' => $enumSetValues,
  1305. 'print_type' => $printType,
  1306. 'binary' => $binary,
  1307. 'unsigned' => $unsigned,
  1308. 'zerofill' => $zerofill,
  1309. 'attribute' => $attribute,
  1310. 'can_contain_collation' => $canContainCollation,
  1311. 'displayed_type' => $displayedType,
  1312. ];
  1313. }
  1314. /**
  1315. * If the string starts with a \r\n pair (0x0d0a) add an extra \n
  1316. *
  1317. * @param string $string string
  1318. *
  1319. * @return string with the chars replaced
  1320. */
  1321. public static function duplicateFirstNewline(string $string): string
  1322. {
  1323. $firstOccurrence = mb_strpos($string, "\r\n");
  1324. if ($firstOccurrence === 0) {
  1325. $string = "\n" . $string;
  1326. }
  1327. return $string;
  1328. }
  1329. /**
  1330. * Get the action word corresponding to a script name
  1331. * in order to display it as a title in navigation panel
  1332. *
  1333. * @param string $target a valid value for $cfg['NavigationTreeDefaultTabTable'],
  1334. * $cfg['NavigationTreeDefaultTabTable2'],
  1335. * $cfg['DefaultTabTable'] or $cfg['DefaultTabDatabase']
  1336. *
  1337. * @return string|bool Title for the $cfg value
  1338. */
  1339. public static function getTitleForTarget($target)
  1340. {
  1341. $mapping = [
  1342. 'structure' => __('Structure'),
  1343. 'sql' => __('SQL'),
  1344. 'search' => __('Search'),
  1345. 'insert' => __('Insert'),
  1346. 'browse' => __('Browse'),
  1347. 'operations' => __('Operations'),
  1348. ];
  1349. return $mapping[$target] ?? false;
  1350. }
  1351. /**
  1352. * Get the script name corresponding to a plain English config word
  1353. * in order to append in links on navigation and main panel
  1354. *
  1355. * @param string $target a valid value for
  1356. * $cfg['NavigationTreeDefaultTabTable'],
  1357. * $cfg['NavigationTreeDefaultTabTable2'],
  1358. * $cfg['DefaultTabTable'], $cfg['DefaultTabDatabase'] or
  1359. * $cfg['DefaultTabServer']
  1360. * @param string $location one out of 'server', 'table', 'database'
  1361. *
  1362. * @return string script name corresponding to the config word
  1363. */
  1364. public static function getScriptNameForOption($target, string $location): string
  1365. {
  1366. return Url::getFromRoute(self::getUrlForOption($target, $location));
  1367. }
  1368. /**
  1369. * Get the URL corresponding to a plain English config word
  1370. * in order to append in links on navigation and main panel
  1371. *
  1372. * @param string $target a valid value for
  1373. * $cfg['NavigationTreeDefaultTabTable'],
  1374. * $cfg['NavigationTreeDefaultTabTable2'],
  1375. * $cfg['DefaultTabTable'], $cfg['DefaultTabDatabase'] or
  1376. * $cfg['DefaultTabServer']
  1377. * @param string $location one out of 'server', 'table', 'database'
  1378. *
  1379. * @return string The URL corresponding to the config word
  1380. */
  1381. public static function getUrlForOption($target, string $location): string
  1382. {
  1383. if ($location === 'server') {
  1384. // Values for $cfg['DefaultTabServer']
  1385. switch ($target) {
  1386. case 'welcome':
  1387. case 'index.php':
  1388. return '/';
  1389. case 'databases':
  1390. case 'server_databases.php':
  1391. return '/server/databases';
  1392. case 'status':
  1393. case 'server_status.php':
  1394. return '/server/status';
  1395. case 'variables':
  1396. case 'server_variables.php':
  1397. return '/server/variables';
  1398. case 'privileges':
  1399. case 'server_privileges.php':
  1400. return '/server/privileges';
  1401. }
  1402. } elseif ($location === 'database') {
  1403. // Values for $cfg['DefaultTabDatabase']
  1404. switch ($target) {
  1405. case 'structure':
  1406. case 'db_structure.php':
  1407. return '/database/structure';
  1408. case 'sql':
  1409. case 'db_sql.php':
  1410. return '/database/sql';
  1411. case 'search':
  1412. case 'db_search.php':
  1413. return '/database/search';
  1414. case 'operations':
  1415. case 'db_operations.php':
  1416. return '/database/operations';
  1417. }
  1418. } elseif ($location === 'table') {
  1419. // Values for $cfg['DefaultTabTable'],
  1420. // $cfg['NavigationTreeDefaultTabTable'] and
  1421. // $cfg['NavigationTreeDefaultTabTable2']
  1422. switch ($target) {
  1423. case 'structure':
  1424. case 'tbl_structure.php':
  1425. return '/table/structure';
  1426. case 'sql':
  1427. case 'tbl_sql.php':
  1428. return '/table/sql';
  1429. case 'search':
  1430. case 'tbl_select.php':
  1431. return '/table/search';
  1432. case 'insert':
  1433. case 'tbl_change.php':
  1434. return '/table/change';
  1435. case 'browse':
  1436. case 'sql.php':
  1437. return '/sql';
  1438. }
  1439. }
  1440. return '/';
  1441. }
  1442. /**
  1443. * Formats user string, expanding @VARIABLES@, accepting strftime format
  1444. * string.
  1445. *
  1446. * @param string $string Text where to do expansion.
  1447. * @param array|string $escape Function to call for escaping variable values.
  1448. * Can also be an array of:
  1449. * - the escape method name
  1450. * - the class that contains the method
  1451. * - location of the class (for inclusion)
  1452. * @param array $updates Array with overrides for default parameters
  1453. * (obtained from GLOBALS).
  1454. *
  1455. * @return string
  1456. */
  1457. public static function expandUserString(
  1458. $string,
  1459. $escape = null,
  1460. array $updates = []
  1461. ) {
  1462. global $dbi;
  1463. /* Content */
  1464. $vars = [];
  1465. $vars['http_host'] = Core::getenv('HTTP_HOST');
  1466. $vars['server_name'] = $GLOBALS['cfg']['Server']['host'];
  1467. $vars['server_verbose'] = $GLOBALS['cfg']['Server']['verbose'];
  1468. if (empty($GLOBALS['cfg']['Server']['verbose'])) {
  1469. $vars['server_verbose_or_name'] = $GLOBALS['cfg']['Server']['host'];
  1470. } else {
  1471. $vars['server_verbose_or_name'] = $GLOBALS['cfg']['Server']['verbose'];
  1472. }
  1473. $vars['database'] = $GLOBALS['db'];
  1474. $vars['table'] = $GLOBALS['table'];
  1475. $vars['phpmyadmin_version'] = 'phpMyAdmin ' . Version::VERSION;
  1476. /* Update forced variables */
  1477. foreach ($updates as $key => $val) {
  1478. $vars[$key] = $val;
  1479. }
  1480. /* Replacement mapping */
  1481. /*
  1482. * The __VAR__ ones are for backward compatibility, because user
  1483. * might still have it in cookies.
  1484. */
  1485. $replace = [
  1486. '@HTTP_HOST@' => $vars['http_host'],
  1487. '@SERVER@' => $vars['server_name'],
  1488. '__SERVER__' => $vars['server_name'],
  1489. '@VERBOSE@' => $vars['server_verbose'],
  1490. '@VSERVER@' => $vars['server_verbose_or_name'],
  1491. '@DATABASE@' => $vars['database'],
  1492. '__DB__' => $vars['database'],
  1493. '@TABLE@' => $vars['table'],
  1494. '__TABLE__' => $vars['table'],
  1495. '@PHPMYADMIN@' => $vars['phpmyadmin_version'],
  1496. ];
  1497. /* Optional escaping */
  1498. if ($escape !== null) {
  1499. if (is_array($escape)) {
  1500. $escapeClass = new $escape[1]();
  1501. $escapeMethod = $escape[0];
  1502. }
  1503. foreach ($replace as $key => $val) {
  1504. if (isset($escapeClass, $escapeMethod)) {
  1505. $replace[$key] = $escapeClass->$escapeMethod($val);
  1506. } elseif ($escape === 'backquote') {
  1507. $replace[$key] = self::backquote($val);
  1508. } elseif (is_callable($escape)) {
  1509. $replace[$key] = $escape($val);
  1510. }
  1511. }
  1512. }
  1513. /* Backward compatibility in 3.5.x */
  1514. if (str_contains($string, '@FIELDS@')) {
  1515. $string = strtr($string, ['@FIELDS@' => '@COLUMNS@']);
  1516. }
  1517. /* Fetch columns list if required */
  1518. if (str_contains($string, '@COLUMNS@')) {
  1519. $columnsList = $dbi->getColumns($GLOBALS['db'], $GLOBALS['table']);
  1520. // sometimes the table no longer exists at this point
  1521. if ($columnsList !== null) {
  1522. $columnNames = [];
  1523. foreach ($columnsList as $column) {
  1524. if ($escape !== null) {
  1525. $columnNames[] = self::$escape($column['Field']);
  1526. } else {
  1527. $columnNames[] = $column['Field'];
  1528. }
  1529. }
  1530. $replace['@COLUMNS@'] = implode(',', $columnNames);
  1531. } else {
  1532. $replace['@COLUMNS@'] = '*';
  1533. }
  1534. }
  1535. /* Do the replacement */
  1536. return strtr((string) @strftime($string), $replace);
  1537. }
  1538. /**
  1539. * This function processes the datatypes supported by the DB,
  1540. * as specified in Types->getColumns() and either returns an array
  1541. * (useful for quickly checking if a datatype is supported)
  1542. * or an HTML snippet that creates a drop-down list.
  1543. *
  1544. * @param bool $html Whether to generate an html snippet or an array
  1545. * @param string $selected The value to mark as selected in HTML mode
  1546. *
  1547. * @return mixed An HTML snippet or an array of datatypes.
  1548. */
  1549. public static function getSupportedDatatypes($html = false, $selected = '')
  1550. {
  1551. global $dbi;
  1552. if ($html) {
  1553. $retval = Generator::getSupportedDatatypes($selected);
  1554. } else {
  1555. $retval = [];
  1556. foreach ($dbi->types->getColumns() as $value) {
  1557. if (is_array($value)) {
  1558. foreach ($value as $subvalue) {
  1559. if ($subvalue === '-') {
  1560. continue;
  1561. }
  1562. $retval[] = $subvalue;
  1563. }
  1564. } else {
  1565. if ($value !== '-') {
  1566. $retval[] = $value;
  1567. }
  1568. }
  1569. }
  1570. }
  1571. return $retval;
  1572. }
  1573. /**
  1574. * Returns a list of datatypes that are not (yet) handled by PMA.
  1575. * Used by: /table/change and libraries/Routines.php
  1576. *
  1577. * @return array list of datatypes
  1578. */
  1579. public static function unsupportedDatatypes(): array
  1580. {
  1581. return [];
  1582. }
  1583. /**
  1584. * Checks if the current user has a specific privilege and returns true if the
  1585. * user indeed has that privilege or false if they don't. This function must
  1586. * only be used for features that are available since MySQL 5, because it
  1587. * relies on the INFORMATION_SCHEMA database to be present.
  1588. *
  1589. * Example: currentUserHasPrivilege('CREATE ROUTINE', 'mydb');
  1590. * // Checks if the currently logged in user has the global
  1591. * // 'CREATE ROUTINE' privilege or, if not, checks if the
  1592. * // user has this privilege on database 'mydb'.
  1593. *
  1594. * @param string $priv The privilege to check
  1595. * @param string|null $db null, to only check global privileges
  1596. * string, db name where to also check
  1597. * for privileges
  1598. * @param string|null $tbl null, to only check global/db privileges
  1599. * string, table name where to also check
  1600. * for privileges
  1601. */
  1602. public static function currentUserHasPrivilege(string $priv, ?string $db = null, ?string $tbl = null): bool
  1603. {
  1604. global $dbi;
  1605. // Get the username for the current user in the format
  1606. // required to use in the information schema database.
  1607. [$user, $host] = $dbi->getCurrentUserAndHost();
  1608. // MySQL is started with --skip-grant-tables
  1609. if ($user === '') {
  1610. return true;
  1611. }
  1612. $username = "''";
  1613. $username .= str_replace("'", "''", $user);
  1614. $username .= "''@''";
  1615. $username .= str_replace("'", "''", $host);
  1616. $username .= "''";
  1617. // Prepare the query
  1618. $query = 'SELECT `PRIVILEGE_TYPE` FROM `INFORMATION_SCHEMA`.`%s` '
  1619. . "WHERE GRANTEE='%s' AND PRIVILEGE_TYPE='%s'";
  1620. // Check global privileges first.
  1621. $userPrivileges = $dbi->fetchValue(
  1622. sprintf(
  1623. $query,
  1624. 'USER_PRIVILEGES',
  1625. $username,
  1626. $priv
  1627. )
  1628. );
  1629. if ($userPrivileges) {
  1630. return true;
  1631. }
  1632. // If a database name was provided and user does not have the
  1633. // required global privilege, try database-wise permissions.
  1634. if ($db === null) {
  1635. // There was no database name provided and the user
  1636. // does not have the correct global privilege.
  1637. return false;
  1638. }
  1639. $query .= " AND '%s' LIKE `TABLE_SCHEMA`";
  1640. $schemaPrivileges = $dbi->fetchValue(
  1641. sprintf(
  1642. $query,
  1643. 'SCHEMA_PRIVILEGES',
  1644. $username,
  1645. $priv,
  1646. $dbi->escapeString($db)
  1647. )
  1648. );
  1649. if ($schemaPrivileges) {
  1650. return true;
  1651. }
  1652. // If a table name was also provided and we still didn't
  1653. // find any valid privileges, try table-wise privileges.
  1654. if ($tbl !== null) {
  1655. $query .= " AND TABLE_NAME='%s'";
  1656. $tablePrivileges = $dbi->fetchValue(
  1657. sprintf(
  1658. $query,
  1659. 'TABLE_PRIVILEGES',
  1660. $username,
  1661. $priv,
  1662. $dbi->escapeString($db),
  1663. $dbi->escapeString($tbl)
  1664. )
  1665. );
  1666. if ($tablePrivileges) {
  1667. return true;
  1668. }
  1669. }
  1670. /**
  1671. * If we reached this point, the user does not
  1672. * have even valid table-wise privileges.
  1673. */
  1674. return false;
  1675. }
  1676. /**
  1677. * Returns server type for current connection
  1678. *
  1679. * Known types are: MariaDB, Percona Server and MySQL (default)
  1680. *
  1681. * @phpstan-return 'MariaDB'|'Percona Server'|'MySQL'
  1682. */
  1683. public static function getServerType(): string
  1684. {
  1685. global $dbi;
  1686. if ($dbi->isMariaDB()) {
  1687. return 'MariaDB';
  1688. }
  1689. if ($dbi->isPercona()) {
  1690. return 'Percona Server';
  1691. }
  1692. return 'MySQL';
  1693. }
  1694. /**
  1695. * Parses ENUM/SET values
  1696. *
  1697. * @param string $definition The definition of the column
  1698. * for which to parse the values
  1699. * @param bool $escapeHtml Whether to escape html entities
  1700. *
  1701. * @return array
  1702. */
  1703. public static function parseEnumSetValues($definition, $escapeHtml = true)
  1704. {
  1705. $valuesString = htmlentities($definition, ENT_COMPAT, 'UTF-8');
  1706. // There is a JS port of the below parser in functions.js
  1707. // If you are fixing something here,
  1708. // you need to also update the JS port.
  1709. $values = [];
  1710. $inString = false;
  1711. $buffer = '';
  1712. for ($i = 0, $length = mb_strlen($valuesString); $i < $length; $i++) {
  1713. $curr = mb_substr($valuesString, $i, 1);
  1714. $next = $i == mb_strlen($valuesString) - 1
  1715. ? ''
  1716. : mb_substr($valuesString, $i + 1, 1);
  1717. if (! $inString && $curr == "'") {
  1718. $inString = true;
  1719. } elseif (($inString && $curr === '\\') && $next === '\\') {
  1720. $buffer .= '&#92;';
  1721. $i++;
  1722. } elseif (($inString && $next == "'") && ($curr == "'" || $curr === '\\')) {
  1723. $buffer .= '&#39;';
  1724. $i++;
  1725. } elseif ($inString && $curr == "'") {
  1726. $inString = false;
  1727. $values[] = $buffer;
  1728. $buffer = '';
  1729. } elseif ($inString) {
  1730. $buffer .= $curr;
  1731. }
  1732. }
  1733. if (strlen($buffer) > 0) {
  1734. // The leftovers in the buffer are the last value (if any)
  1735. $values[] = $buffer;
  1736. }
  1737. if (! $escapeHtml) {
  1738. foreach ($values as $key => $value) {
  1739. $values[$key] = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
  1740. }
  1741. }
  1742. return $values;
  1743. }
  1744. /**
  1745. * Return the list of tabs for the menu with corresponding names
  1746. *
  1747. * @param string $level 'server', 'db' or 'table' level
  1748. *
  1749. * @return array|null list of tabs for the menu
  1750. */
  1751. public static function getMenuTabList($level = null)
  1752. {
  1753. $tabList = [
  1754. 'server' => [
  1755. 'databases' => __('Databases'),
  1756. 'sql' => __('SQL'),
  1757. 'status' => __('Status'),
  1758. 'rights' => __('Users'),
  1759. 'export' => __('Export'),
  1760. 'import' => __('Import'),
  1761. 'settings' => __('Settings'),
  1762. 'binlog' => __('Binary log'),
  1763. 'replication' => __('Replication'),
  1764. 'vars' => __('Variables'),
  1765. 'charset' => __('Charsets'),
  1766. 'plugins' => __('Plugins'),
  1767. 'engine' => __('Engines'),
  1768. ],
  1769. 'db' => [
  1770. 'structure' => __('Structure'),
  1771. 'sql' => __('SQL'),
  1772. 'search' => __('Search'),
  1773. 'query' => __('Query'),
  1774. 'export' => __('Export'),
  1775. 'import' => __('Import'),
  1776. 'operation' => __('Operations'),
  1777. 'privileges' => __('Privileges'),
  1778. 'routines' => __('Routines'),
  1779. 'events' => __('Events'),
  1780. 'triggers' => __('Triggers'),
  1781. 'tracking' => __('Tracking'),
  1782. 'designer' => __('Designer'),
  1783. 'central_columns' => __('Central columns'),
  1784. ],
  1785. 'table' => [
  1786. 'browse' => __('Browse'),
  1787. 'structure' => __('Structure'),
  1788. 'sql' => __('SQL'),
  1789. 'search' => __('Search'),
  1790. 'insert' => __('Insert'),
  1791. 'export' => __('Export'),
  1792. 'import' => __('Import'),
  1793. 'privileges' => __('Privileges'),
  1794. 'operation' => __('Operations'),
  1795. 'tracking' => __('Tracking'),
  1796. 'triggers' => __('Triggers'),
  1797. ],
  1798. ];
  1799. if ($level == null) {
  1800. return $tabList;
  1801. }
  1802. if (array_key_exists($level, $tabList)) {
  1803. return $tabList[$level];
  1804. }
  1805. return null;
  1806. }
  1807. /**
  1808. * Add fractional seconds to time, datetime and timestamp strings.
  1809. * If the string contains fractional seconds,
  1810. * pads it with 0s up to 6 decimal places.
  1811. *
  1812. * @param string $value time, datetime or timestamp strings
  1813. *
  1814. * @return string time, datetime or timestamp strings with fractional seconds
  1815. */
  1816. public static function addMicroseconds($value)
  1817. {
  1818. if (empty($value) || $value === 'CURRENT_TIMESTAMP' || $value === 'current_timestamp()') {
  1819. return $value;
  1820. }
  1821. if (! str_contains($value, '.')) {
  1822. return $value . '.000000';
  1823. }
  1824. $value .= '000000';
  1825. return mb_substr(
  1826. $value,
  1827. 0,
  1828. mb_strpos($value, '.') + 7
  1829. );
  1830. }
  1831. /**
  1832. * Reads the file, detects the compression MIME type, closes the file
  1833. * and returns the MIME type
  1834. *
  1835. * @param resource $file the file handle
  1836. *
  1837. * @return string the MIME type for compression, or 'none'
  1838. */
  1839. public static function getCompressionMimeType($file)
  1840. {
  1841. $test = fread($file, 4);
  1842. if ($test === false) {
  1843. fclose($file);
  1844. return 'none';
  1845. }
  1846. $len = strlen($test);
  1847. fclose($file);
  1848. if ($len >= 2 && $test[0] == chr(31) && $test[1] == chr(139)) {
  1849. return 'application/gzip';
  1850. }
  1851. if ($len >= 3 && substr($test, 0, 3) === 'BZh') {
  1852. return 'application/bzip2';
  1853. }
  1854. if ($len >= 4 && $test == "PK\003\004") {
  1855. return 'application/zip';
  1856. }
  1857. return 'none';
  1858. }
  1859. /**
  1860. * Provide COLLATE clause, if required, to perform case sensitive comparisons
  1861. * for queries on information_schema.
  1862. *
  1863. * @return string COLLATE clause if needed or empty string.
  1864. */
  1865. public static function getCollateForIS()
  1866. {
  1867. global $dbi;
  1868. $names = $dbi->getLowerCaseNames();
  1869. if ($names === '0') {
  1870. return 'COLLATE utf8_bin';
  1871. }
  1872. if ($names === '2') {
  1873. return 'COLLATE utf8_general_ci';
  1874. }
  1875. return '';
  1876. }
  1877. /**
  1878. * Process the index data.
  1879. *
  1880. * @param array $indexes index data
  1881. *
  1882. * @return array processes index data
  1883. */
  1884. public static function processIndexData(array $indexes)
  1885. {
  1886. $lastIndex = '';
  1887. $primary = '';
  1888. $pkArray = []; // will be use to emphasis prim. keys in the table
  1889. $indexesInfo = [];
  1890. $indexesData = [];
  1891. // view
  1892. foreach ($indexes as $row) {
  1893. // Backups the list of primary keys
  1894. if ($row['Key_name'] === 'PRIMARY') {
  1895. $primary .= $row['Column_name'] . ', ';
  1896. $pkArray[$row['Column_name']] = 1;
  1897. }
  1898. // Retains keys informations
  1899. if ($row['Key_name'] != $lastIndex) {
  1900. $indexes[] = $row['Key_name'];
  1901. $lastIndex = $row['Key_name'];
  1902. }
  1903. $indexesInfo[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
  1904. $indexesInfo[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
  1905. if (isset($row['Cardinality'])) {
  1906. $indexesInfo[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
  1907. }
  1908. // I don't know what does following column mean....
  1909. // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
  1910. $indexesInfo[$row['Key_name']]['Comment'] = $row['Comment'];
  1911. $indexesData[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
  1912. if (! isset($row['Sub_part'])) {
  1913. continue;
  1914. }
  1915. $indexesData[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
  1916. }
  1917. return [
  1918. $primary,
  1919. $pkArray,
  1920. $indexesInfo,
  1921. $indexesData,
  1922. ];
  1923. }
  1924. /**
  1925. * Gets the list of tables in the current db and information about these
  1926. * tables if possible
  1927. *
  1928. * @param string $db database name
  1929. * @param string|null $subPart part of script name
  1930. *
  1931. * @return array
  1932. */
  1933. public static function getDbInfo($db, ?string $subPart)
  1934. {
  1935. global $cfg, $dbi;
  1936. /**
  1937. * limits for table list
  1938. */
  1939. if (! isset($_SESSION['tmpval']['table_limit_offset']) || $_SESSION['tmpval']['table_limit_offset_db'] != $db) {
  1940. $_SESSION['tmpval']['table_limit_offset'] = 0;
  1941. $_SESSION['tmpval']['table_limit_offset_db'] = $db;
  1942. }
  1943. if (isset($_REQUEST['pos'])) {
  1944. $_SESSION['tmpval']['table_limit_offset'] = (int) $_REQUEST['pos'];
  1945. }
  1946. $pos = $_SESSION['tmpval']['table_limit_offset'];
  1947. /**
  1948. * whether to display extended stats
  1949. */
  1950. $isShowStats = $cfg['ShowStats'];
  1951. /**
  1952. * whether selected db is information_schema
  1953. */
  1954. $isSystemSchema = false;
  1955. if (Utilities::isSystemSchema($db)) {
  1956. $isShowStats = false;
  1957. $isSystemSchema = true;
  1958. }
  1959. /**
  1960. * information about tables in db
  1961. */
  1962. $tables = [];
  1963. $tooltipTrueName = [];
  1964. $tooltipAliasName = [];
  1965. // Special speedup for newer MySQL Versions (in 4.0 format changed)
  1966. if ($cfg['SkipLockedTables'] === true) {
  1967. $dbInfoResult = $dbi->query(
  1968. 'SHOW OPEN TABLES FROM ' . self::backquote($db) . ' WHERE In_use > 0;'
  1969. );
  1970. // Blending out tables in use
  1971. if ($dbInfoResult && $dbi->numRows($dbInfoResult) > 0) {
  1972. $tables = self::getTablesWhenOpen($db, $dbInfoResult);
  1973. } elseif ($dbInfoResult) {
  1974. $dbi->freeResult($dbInfoResult);
  1975. }
  1976. }
  1977. if (empty($tables)) {
  1978. // Set some sorting defaults
  1979. $sort = 'Name';
  1980. $sortOrder = 'ASC';
  1981. if (isset($_REQUEST['sort'])) {
  1982. $sortableNameMappings = [
  1983. 'table' => 'Name',
  1984. 'records' => 'Rows',
  1985. 'type' => 'Engine',
  1986. 'collation' => 'Collation',
  1987. 'size' => 'Data_length',
  1988. 'overhead' => 'Data_free',
  1989. 'creation' => 'Create_time',
  1990. 'last_update' => 'Update_time',
  1991. 'last_check' => 'Check_time',
  1992. 'comment' => 'Comment',
  1993. ];
  1994. // Make sure the sort type is implemented
  1995. if (isset($sortableNameMappings[$_REQUEST['sort']])) {
  1996. $sort = $sortableNameMappings[$_REQUEST['sort']];
  1997. if ($_REQUEST['sort_order'] === 'DESC') {
  1998. $sortOrder = 'DESC';
  1999. }
  2000. }
  2001. }
  2002. $groupWithSeparator = false;
  2003. $tableType = null;
  2004. $limitOffset = 0;
  2005. $limitCount = false;
  2006. $groupTable = [];
  2007. if (! empty($_REQUEST['tbl_group']) || ! empty($_REQUEST['tbl_type'])) {
  2008. if (! empty($_REQUEST['tbl_type'])) {
  2009. // only tables for selected type
  2010. $tableType = $_REQUEST['tbl_type'];
  2011. }
  2012. if (! empty($_REQUEST['tbl_group'])) {
  2013. // only tables for selected group
  2014. $tableGroup = $_REQUEST['tbl_group'];
  2015. // include the table with the exact name of the group if such
  2016. // exists
  2017. $groupTable = $dbi->getTablesFull(
  2018. $db,
  2019. $tableGroup,
  2020. false,
  2021. $limitOffset,
  2022. $limitCount,
  2023. $sort,
  2024. $sortOrder,
  2025. $tableType
  2026. );
  2027. $groupWithSeparator = $tableGroup
  2028. . $GLOBALS['cfg']['NavigationTreeTableSeparator'];
  2029. }
  2030. } else {
  2031. // all tables in db
  2032. // - get the total number of tables
  2033. // (needed for proper working of the MaxTableList feature)
  2034. $tables = $dbi->getTables($db);
  2035. $totalNumTables = count($tables);
  2036. if (! (isset($subPart) && $subPart === '_export')) {
  2037. // fetch the details for a possible limited subset
  2038. $limitOffset = $pos;
  2039. $limitCount = true;
  2040. }
  2041. }
  2042. $tables = array_merge(
  2043. $groupTable,
  2044. $dbi->getTablesFull(
  2045. $db,
  2046. $groupWithSeparator !== false ? $groupWithSeparator : '',
  2047. $groupWithSeparator !== false,
  2048. $limitOffset,
  2049. $limitCount,
  2050. $sort,
  2051. $sortOrder,
  2052. $tableType
  2053. )
  2054. );
  2055. }
  2056. $numTables = count($tables);
  2057. // (needed for proper working of the MaxTableList feature)
  2058. if (! isset($totalNumTables)) {
  2059. $totalNumTables = $numTables;
  2060. }
  2061. /**
  2062. * If coming from a Show MySQL link on the home page,
  2063. * put something in $sub_part
  2064. */
  2065. if (empty($subPart)) {
  2066. $subPart = '_structure';
  2067. }
  2068. return [
  2069. $tables,
  2070. $numTables,
  2071. $totalNumTables,
  2072. $subPart,
  2073. $isShowStats,
  2074. $isSystemSchema,
  2075. $tooltipTrueName,
  2076. $tooltipAliasName,
  2077. $pos,
  2078. ];
  2079. }
  2080. /**
  2081. * Gets the list of tables in the current db, taking into account
  2082. * that they might be "in use"
  2083. *
  2084. * @param string $db database name
  2085. * @param object $dbInfoResult result set
  2086. *
  2087. * @return array list of tables
  2088. */
  2089. public static function getTablesWhenOpen($db, $dbInfoResult): array
  2090. {
  2091. global $dbi;
  2092. $sotCache = [];
  2093. $tables = [];
  2094. while ($tmp = $dbi->fetchAssoc($dbInfoResult)) {
  2095. $sotCache[$tmp['Table']] = true;
  2096. }
  2097. $dbi->freeResult($dbInfoResult);
  2098. // is there at least one "in use" table?
  2099. if (count($sotCache) > 0) {
  2100. $tblGroupSql = '';
  2101. $whereAdded = false;
  2102. if (
  2103. isset($_REQUEST['tbl_group'])
  2104. && is_scalar($_REQUEST['tbl_group'])
  2105. && strlen((string) $_REQUEST['tbl_group']) > 0
  2106. ) {
  2107. $group = self::escapeMysqlWildcards((string) $_REQUEST['tbl_group']);
  2108. $groupWithSeparator = self::escapeMysqlWildcards(
  2109. $_REQUEST['tbl_group']
  2110. . $GLOBALS['cfg']['NavigationTreeTableSeparator']
  2111. );
  2112. $tblGroupSql .= ' WHERE ('
  2113. . self::backquote('Tables_in_' . $db)
  2114. . " LIKE '" . $groupWithSeparator . "%'"
  2115. . ' OR '
  2116. . self::backquote('Tables_in_' . $db)
  2117. . " LIKE '" . $group . "')";
  2118. $whereAdded = true;
  2119. }
  2120. if (isset($_REQUEST['tbl_type']) && in_array($_REQUEST['tbl_type'], ['table', 'view'])) {
  2121. $tblGroupSql .= $whereAdded ? ' AND' : ' WHERE';
  2122. if ($_REQUEST['tbl_type'] === 'view') {
  2123. $tblGroupSql .= " `Table_type` NOT IN ('BASE TABLE', 'SYSTEM VERSIONED')";
  2124. } else {
  2125. $tblGroupSql .= " `Table_type` IN ('BASE TABLE', 'SYSTEM VERSIONED')";
  2126. }
  2127. }
  2128. $dbInfoResult = $dbi->query(
  2129. 'SHOW FULL TABLES FROM ' . self::backquote($db) . $tblGroupSql,
  2130. DatabaseInterface::CONNECT_USER,
  2131. DatabaseInterface::QUERY_STORE
  2132. );
  2133. unset($tblGroupSql, $whereAdded);
  2134. if ($dbInfoResult && $dbi->numRows($dbInfoResult) > 0) {
  2135. $names = [];
  2136. while ($tmp = $dbi->fetchRow($dbInfoResult)) {
  2137. if (! isset($sotCache[$tmp[0]])) {
  2138. $names[] = $tmp[0];
  2139. } else { // table in use
  2140. $tables[$tmp[0]] = [
  2141. 'TABLE_NAME' => $tmp[0],
  2142. 'ENGINE' => '',
  2143. 'TABLE_TYPE' => '',
  2144. 'TABLE_ROWS' => 0,
  2145. 'TABLE_COMMENT' => '',
  2146. ];
  2147. }
  2148. }
  2149. if (count($names) > 0) {
  2150. $tables = array_merge(
  2151. $tables,
  2152. $dbi->getTablesFull($db, $names)
  2153. );
  2154. }
  2155. if ($GLOBALS['cfg']['NaturalOrder']) {
  2156. uksort($tables, 'strnatcasecmp');
  2157. }
  2158. } elseif ($dbInfoResult) {
  2159. $dbi->freeResult($dbInfoResult);
  2160. }
  2161. unset($sotCache);
  2162. }
  2163. return $tables;
  2164. }
  2165. /**
  2166. * Checks whether database extension is loaded
  2167. *
  2168. * @param string $extension mysql extension to check
  2169. */
  2170. public static function checkDbExtension(string $extension = 'mysqli'): bool
  2171. {
  2172. return function_exists($extension . '_connect');
  2173. }
  2174. /**
  2175. * Returns list of used PHP extensions.
  2176. *
  2177. * @return string[]
  2178. */
  2179. public static function listPHPExtensions(): array
  2180. {
  2181. $result = [];
  2182. if (self::checkDbExtension('mysqli')) {
  2183. $result[] = 'mysqli';
  2184. }
  2185. if (extension_loaded('curl')) {
  2186. $result[] = 'curl';
  2187. }
  2188. if (extension_loaded('mbstring')) {
  2189. $result[] = 'mbstring';
  2190. }
  2191. return $result;
  2192. }
  2193. /**
  2194. * Converts given (request) parameter to string
  2195. *
  2196. * @param mixed $value Value to convert
  2197. */
  2198. public static function requestString($value): string
  2199. {
  2200. while (is_array($value) || is_object($value)) {
  2201. if (is_object($value)) {
  2202. $value = (array) $value;
  2203. }
  2204. $value = reset($value);
  2205. }
  2206. return trim((string) $value);
  2207. }
  2208. /**
  2209. * Generates random string consisting of ASCII chars
  2210. *
  2211. * @param int $length Length of string
  2212. * @param bool $asHex (optional) Send the result as hex
  2213. */
  2214. public static function generateRandom(int $length, bool $asHex = false): string
  2215. {
  2216. $result = '';
  2217. if (class_exists(Random::class)) {
  2218. $randomFunction = [
  2219. Random::class,
  2220. 'string',
  2221. ];
  2222. } else {
  2223. $randomFunction = 'openssl_random_pseudo_bytes';
  2224. }
  2225. while (strlen($result) < $length) {
  2226. // Get random byte and strip highest bit
  2227. // to get ASCII only range
  2228. $byte = ord((string) $randomFunction(1)) & 0x7f;
  2229. // We want only ASCII chars and no DEL character (127)
  2230. if ($byte <= 32 || $byte === 127) {
  2231. continue;
  2232. }
  2233. $result .= chr($byte);
  2234. }
  2235. return $asHex ? bin2hex($result) : $result;
  2236. }
  2237. /**
  2238. * Wrapper around PHP date function
  2239. *
  2240. * @param string $format Date format string
  2241. *
  2242. * @return string
  2243. */
  2244. public static function date($format)
  2245. {
  2246. return date($format);
  2247. }
  2248. /**
  2249. * Wrapper around php's set_time_limit
  2250. */
  2251. public static function setTimeLimit(): void
  2252. {
  2253. // The function can be disabled in php.ini
  2254. if (! function_exists('set_time_limit')) {
  2255. return;
  2256. }
  2257. @set_time_limit((int) $GLOBALS['cfg']['ExecTimeLimit']);
  2258. }
  2259. /**
  2260. * Access to a multidimensional array by dot notation
  2261. *
  2262. * @param array $array List of values
  2263. * @param string|array $path Path to searched value
  2264. * @param mixed $default Default value
  2265. *
  2266. * @return mixed Searched value
  2267. */
  2268. public static function getValueByKey(array $array, $path, $default = null)
  2269. {
  2270. if (is_string($path)) {
  2271. $path = explode('.', $path);
  2272. }
  2273. $p = array_shift($path);
  2274. while (isset($p)) {
  2275. if (! isset($array[$p])) {
  2276. return $default;
  2277. }
  2278. $array = $array[$p];
  2279. $p = array_shift($path);
  2280. }
  2281. return $array;
  2282. }
  2283. /**
  2284. * Creates a clickable column header for table information
  2285. *
  2286. * @param string $title Title to use for the link
  2287. * @param string $sort Corresponds to sortable data name mapped
  2288. * in Util::getDbInfo
  2289. * @param string $initialSortOrder Initial sort order
  2290. *
  2291. * @return string Link to be displayed in the table header
  2292. */
  2293. public static function sortableTableHeader($title, $sort, $initialSortOrder = 'ASC')
  2294. {
  2295. $requestedSort = 'table';
  2296. $requestedSortOrder = $futureSortOrder = $initialSortOrder;
  2297. // If the user requested a sort
  2298. if (isset($_REQUEST['sort'])) {
  2299. $requestedSort = $_REQUEST['sort'];
  2300. if (isset($_REQUEST['sort_order'])) {
  2301. $requestedSortOrder = $_REQUEST['sort_order'];
  2302. }
  2303. }
  2304. $orderImg = '';
  2305. $orderLinkParams = [];
  2306. $orderLinkParams['title'] = __('Sort');
  2307. // If this column was requested to be sorted.
  2308. if ($requestedSort == $sort) {
  2309. if ($requestedSortOrder === 'ASC') {
  2310. $futureSortOrder = 'DESC';
  2311. // current sort order is ASC
  2312. $orderImg = ' ' . Generator::getImage(
  2313. 's_asc',
  2314. __('Ascending'),
  2315. [
  2316. 'class' => 'sort_arrow',
  2317. 'title' => '',
  2318. ]
  2319. );
  2320. $orderImg .= ' ' . Generator::getImage(
  2321. 's_desc',
  2322. __('Descending'),
  2323. [
  2324. 'class' => 'sort_arrow hide',
  2325. 'title' => '',
  2326. ]
  2327. );
  2328. // but on mouse over, show the reverse order (DESC)
  2329. $orderLinkParams['onmouseover'] = "$('.sort_arrow').toggle();";
  2330. // on mouse out, show current sort order (ASC)
  2331. $orderLinkParams['onmouseout'] = "$('.sort_arrow').toggle();";
  2332. } else {
  2333. $futureSortOrder = 'ASC';
  2334. // current sort order is DESC
  2335. $orderImg = ' ' . Generator::getImage(
  2336. 's_asc',
  2337. __('Ascending'),
  2338. [
  2339. 'class' => 'sort_arrow hide',
  2340. 'title' => '',
  2341. ]
  2342. );
  2343. $orderImg .= ' ' . Generator::getImage(
  2344. 's_desc',
  2345. __('Descending'),
  2346. [
  2347. 'class' => 'sort_arrow',
  2348. 'title' => '',
  2349. ]
  2350. );
  2351. // but on mouse over, show the reverse order (ASC)
  2352. $orderLinkParams['onmouseover'] = "$('.sort_arrow').toggle();";
  2353. // on mouse out, show current sort order (DESC)
  2354. $orderLinkParams['onmouseout'] = "$('.sort_arrow').toggle();";
  2355. }
  2356. }
  2357. $urlParams = [
  2358. 'db' => $_REQUEST['db'],
  2359. 'pos' => 0, // We set the position back to 0 every time they sort.
  2360. 'sort' => $sort,
  2361. 'sort_order' => $futureSortOrder,
  2362. ];
  2363. if (isset($_REQUEST['tbl_type']) && in_array($_REQUEST['tbl_type'], ['view', 'table'])) {
  2364. $urlParams['tbl_type'] = $_REQUEST['tbl_type'];
  2365. }
  2366. if (! empty($_REQUEST['tbl_group'])) {
  2367. $urlParams['tbl_group'] = $_REQUEST['tbl_group'];
  2368. }
  2369. $url = Url::getFromRoute('/database/structure', $urlParams);
  2370. return Generator::linkOrButton($url, $title . $orderImg, $orderLinkParams);
  2371. }
  2372. /**
  2373. * Check that input is an int or an int in a string
  2374. *
  2375. * @param mixed $input input to check
  2376. */
  2377. public static function isInteger($input): bool
  2378. {
  2379. return ctype_digit((string) $input);
  2380. }
  2381. /**
  2382. * Get the protocol from the RFC 7239 Forwarded header
  2383. *
  2384. * @param string $headerContents The Forwarded header contents
  2385. *
  2386. * @return string the protocol http/https
  2387. */
  2388. public static function getProtoFromForwardedHeader(string $headerContents): string
  2389. {
  2390. if (str_contains($headerContents, '=')) {// does not contain any equal sign
  2391. $hops = explode(',', $headerContents);
  2392. $parts = explode(';', $hops[0]);
  2393. foreach ($parts as $part) {
  2394. $keyValueArray = explode('=', $part, 2);
  2395. if (count($keyValueArray) !== 2) {
  2396. continue;
  2397. }
  2398. [
  2399. $keyName,
  2400. $value,
  2401. ] = $keyValueArray;
  2402. $value = trim(strtolower($value));
  2403. if (strtolower(trim($keyName)) === 'proto' && in_array($value, ['http', 'https'])) {
  2404. return $value;
  2405. }
  2406. }
  2407. }
  2408. return '';
  2409. }
  2410. /**
  2411. * Check if error reporting is available
  2412. */
  2413. public static function isErrorReportingAvailable(): bool
  2414. {
  2415. // issue #16256 - PHP 7.x does not return false for a core function
  2416. if (PHP_MAJOR_VERSION < 8) {
  2417. $disabled = ini_get('disable_functions');
  2418. if (is_string($disabled)) {
  2419. $disabled = explode(',', $disabled);
  2420. $disabled = array_map(static function (string $part) {
  2421. return trim($part);
  2422. }, $disabled);
  2423. return ! in_array('error_reporting', $disabled);
  2424. }
  2425. }
  2426. return function_exists('error_reporting');
  2427. }
  2428. }