PageRenderTime 51ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/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

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

  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 getScriptNameForO

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