Select.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Code for displaying server selection
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Server;
  7. use PhpMyAdmin\Url;
  8. use PhpMyAdmin\Util;
  9. use function count;
  10. use function htmlspecialchars;
  11. use function implode;
  12. use function is_array;
  13. use function strpos;
  14. /**
  15. * Displays the MySQL servers choice form
  16. */
  17. class Select
  18. {
  19. /**
  20. * Renders the server selection in list or selectbox form, or option tags only
  21. *
  22. * @param bool $not_only_options whether to include form tags or not
  23. * @param bool $omit_fieldset whether to omit fieldset tag or not
  24. *
  25. * @return string
  26. */
  27. public static function render($not_only_options, $omit_fieldset)
  28. {
  29. $retval = '';
  30. // Show as list?
  31. if ($not_only_options) {
  32. $list = $GLOBALS['cfg']['DisplayServersList'];
  33. $not_only_options = ! $list;
  34. } else {
  35. $list = false;
  36. }
  37. if ($not_only_options) {
  38. $retval .= '<form method="post" action="'
  39. . Util::getScriptNameForOption(
  40. $GLOBALS['cfg']['DefaultTabServer'],
  41. 'server'
  42. )
  43. . '" class="disableAjax">';
  44. if (! $omit_fieldset) {
  45. $retval .= '<fieldset>';
  46. }
  47. $retval .= Url::getHiddenFields([]);
  48. $retval .= '<label for="select_server">'
  49. . __('Current server:') . '</label> ';
  50. $retval .= '<select name="server" id="select_server" class="autosubmit">';
  51. $retval .= '<option value="">(' . __('Servers') . ') ...</option>' . "\n";
  52. } elseif ($list) {
  53. $retval .= __('Current server:') . '<br>';
  54. $retval .= '<ul id="list_server">';
  55. }
  56. foreach ($GLOBALS['cfg']['Servers'] as $key => $server) {
  57. if (empty($server['host'])) {
  58. continue;
  59. }
  60. if (! empty($GLOBALS['server']) && (int) $GLOBALS['server'] === (int) $key) {
  61. $selected = 1;
  62. } else {
  63. $selected = 0;
  64. }
  65. if (! empty($server['verbose'])) {
  66. $label = $server['verbose'];
  67. } else {
  68. $label = $server['host'];
  69. if (! empty($server['port'])) {
  70. $label .= ':' . $server['port'];
  71. }
  72. }
  73. if (! empty($server['only_db'])) {
  74. if (! is_array($server['only_db'])) {
  75. $label .= ' - ' . $server['only_db'];
  76. // try to avoid displaying a too wide selector
  77. } elseif (count($server['only_db']) < 4) {
  78. $label .= ' - ' . implode(', ', $server['only_db']);
  79. }
  80. }
  81. if (! empty($server['user']) && $server['auth_type'] === 'config') {
  82. $label .= ' (' . $server['user'] . ')';
  83. }
  84. if ($list) {
  85. $retval .= '<li>';
  86. if ($selected) {
  87. $retval .= '<strong>' . htmlspecialchars($label) . '</strong>';
  88. } else {
  89. $scriptName = Util::getScriptNameForOption(
  90. $GLOBALS['cfg']['DefaultTabServer'],
  91. 'server'
  92. );
  93. $retval .= '<a class="disableAjax item" href="'
  94. . $scriptName
  95. . Url::getCommon(['server' => $key], strpos($scriptName, '?') === false ? '?' : '&')
  96. . '" >' . htmlspecialchars($label) . '</a>';
  97. }
  98. $retval .= '</li>';
  99. } else {
  100. $retval .= '<option value="' . $key . '" '
  101. . ($selected ? ' selected="selected"' : '') . '>'
  102. . htmlspecialchars($label) . '</option>' . "\n";
  103. }
  104. }
  105. if ($not_only_options) {
  106. $retval .= '</select>';
  107. if (! $omit_fieldset) {
  108. $retval .= '</fieldset>';
  109. }
  110. $retval .= '</form>';
  111. } elseif ($list) {
  112. $retval .= '</ul>';
  113. }
  114. return $retval;
  115. }
  116. }