VariablesController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Controllers\Server;
  4. use PhpMyAdmin\Controllers\AbstractController;
  5. use PhpMyAdmin\DatabaseInterface;
  6. use PhpMyAdmin\Html\Generator;
  7. use PhpMyAdmin\Providers\ServerVariables\ServerVariablesProvider;
  8. use PhpMyAdmin\Response;
  9. use PhpMyAdmin\Template;
  10. use PhpMyAdmin\Url;
  11. use PhpMyAdmin\Util;
  12. use function header;
  13. use function htmlspecialchars;
  14. use function implode;
  15. use function in_array;
  16. use function is_numeric;
  17. use function mb_strtolower;
  18. use function pow;
  19. use function preg_match;
  20. use function str_replace;
  21. use function strtolower;
  22. use function trim;
  23. /**
  24. * Handles viewing and editing server variables
  25. */
  26. class VariablesController extends AbstractController
  27. {
  28. /** @var DatabaseInterface */
  29. private $dbi;
  30. /**
  31. * @param Response $response
  32. * @param DatabaseInterface $dbi
  33. */
  34. public function __construct($response, Template $template, $dbi)
  35. {
  36. parent::__construct($response, $template);
  37. $this->dbi = $dbi;
  38. }
  39. public function index(): void
  40. {
  41. global $err_url;
  42. $params = ['filter' => $_GET['filter'] ?? null];
  43. $err_url = Url::getFromRoute('/');
  44. if ($this->dbi->isSuperUser()) {
  45. $this->dbi->selectDb('mysql');
  46. }
  47. $filterValue = ! empty($params['filter']) ? $params['filter'] : '';
  48. $this->addScriptFiles(['server/variables.js']);
  49. $variables = [];
  50. $serverVarsResult = $this->dbi->tryQuery('SHOW SESSION VARIABLES;');
  51. if ($serverVarsResult !== false) {
  52. $serverVarsSession = [];
  53. while ($arr = $this->dbi->fetchRow($serverVarsResult)) {
  54. $serverVarsSession[$arr[0]] = $arr[1];
  55. }
  56. $this->dbi->freeResult($serverVarsResult);
  57. $serverVars = $this->dbi->fetchResult('SHOW GLOBAL VARIABLES;', 0, 1);
  58. // list of static (i.e. non-editable) system variables
  59. $staticVariables = ServerVariablesProvider::getImplementation()->getStaticVariables();
  60. foreach ($serverVars as $name => $value) {
  61. $hasSessionValue = isset($serverVarsSession[$name])
  62. && $serverVarsSession[$name] !== $value;
  63. $docLink = Generator::linkToVarDocumentation(
  64. $name,
  65. $this->dbi->isMariaDB(),
  66. str_replace('_', '&nbsp;', $name)
  67. );
  68. [$formattedValue, $isEscaped] = $this->formatVariable($name, $value);
  69. if ($hasSessionValue) {
  70. [$sessionFormattedValue] = $this->formatVariable(
  71. $name,
  72. $serverVarsSession[$name]
  73. );
  74. }
  75. $variables[] = [
  76. 'name' => $name,
  77. 'is_editable' => ! in_array(strtolower($name), $staticVariables),
  78. 'doc_link' => $docLink,
  79. 'value' => $formattedValue,
  80. 'is_escaped' => $isEscaped,
  81. 'has_session_value' => $hasSessionValue,
  82. 'session_value' => $sessionFormattedValue ?? null,
  83. ];
  84. }
  85. }
  86. $this->render('server/variables/index', [
  87. 'variables' => $variables,
  88. 'filter_value' => $filterValue,
  89. 'is_superuser' => $this->dbi->isSuperUser(),
  90. 'is_mariadb' => $this->dbi->isMariaDB(),
  91. ]);
  92. }
  93. /**
  94. * Handle the AJAX request for a single variable value
  95. *
  96. * @param array $params Request parameters
  97. */
  98. public function getValue(array $params): void
  99. {
  100. if (! $this->response->isAjax()) {
  101. return;
  102. }
  103. // Send with correct charset
  104. header('Content-Type: text/html; charset=UTF-8');
  105. // Do not use double quotes inside the query to avoid a problem
  106. // when server is running in ANSI_QUOTES sql_mode
  107. $varValue = $this->dbi->fetchSingleRow(
  108. 'SHOW GLOBAL VARIABLES WHERE Variable_name=\''
  109. . $this->dbi->escapeString($params['name']) . '\';',
  110. 'NUM'
  111. );
  112. $json = [
  113. 'message' => $varValue[1],
  114. ];
  115. $variableType = ServerVariablesProvider::getImplementation()->getVariableType($params['name']);
  116. if ($variableType === 'byte') {
  117. $json['message'] = implode(
  118. ' ',
  119. Util::formatByteDown($varValue[1], 3, 3)
  120. );
  121. }
  122. $this->response->addJSON($json);
  123. }
  124. /**
  125. * Handle the AJAX request for setting value for a single variable
  126. *
  127. * @param array $vars Request parameters
  128. */
  129. public function setValue(array $vars): void
  130. {
  131. $params = [
  132. 'varName' => $vars['name'],
  133. 'varValue' => $_POST['varValue'] ?? null,
  134. ];
  135. if (! $this->response->isAjax()) {
  136. return;
  137. }
  138. $value = (string) $params['varValue'];
  139. $variableName = (string) $params['varName'];
  140. $matches = [];
  141. $variableType = ServerVariablesProvider::getImplementation()->getVariableType($variableName);
  142. if ($variableType === 'byte' && preg_match(
  143. '/^\s*(\d+(\.\d+)?)\s*(mb|kb|mib|kib|gb|gib)\s*$/i',
  144. $value,
  145. $matches
  146. )) {
  147. $exp = [
  148. 'kb' => 1,
  149. 'kib' => 1,
  150. 'mb' => 2,
  151. 'mib' => 2,
  152. 'gb' => 3,
  153. 'gib' => 3,
  154. ];
  155. $value = (float) $matches[1] * pow(
  156. 1024,
  157. $exp[mb_strtolower($matches[3])]
  158. );
  159. } else {
  160. $value = $this->dbi->escapeString($value);
  161. }
  162. if (! is_numeric($value)) {
  163. $value = "'" . $value . "'";
  164. }
  165. $json = [];
  166. if (! preg_match('/[^a-zA-Z0-9_]+/', $params['varName'])
  167. && $this->dbi->query(
  168. 'SET GLOBAL ' . $params['varName'] . ' = ' . $value
  169. )
  170. ) {
  171. // Some values are rounded down etc.
  172. $varValue = $this->dbi->fetchSingleRow(
  173. 'SHOW GLOBAL VARIABLES WHERE Variable_name="'
  174. . $this->dbi->escapeString($params['varName'])
  175. . '";',
  176. 'NUM'
  177. );
  178. [$formattedValue, $isHtmlFormatted] = $this->formatVariable(
  179. $params['varName'],
  180. $varValue[1]
  181. );
  182. if ($isHtmlFormatted === false) {
  183. $json['variable'] = htmlspecialchars($formattedValue);
  184. } else {
  185. $json['variable'] = $formattedValue;
  186. }
  187. } else {
  188. $this->response->setRequestStatus(false);
  189. $json['error'] = __('Setting variable failed');
  190. }
  191. $this->response->addJSON($json);
  192. }
  193. /**
  194. * Format Variable
  195. *
  196. * @param string $name variable name
  197. * @param int|string $value variable value
  198. *
  199. * @return array formatted string and bool if string is HTML formatted
  200. */
  201. private function formatVariable($name, $value): array
  202. {
  203. $isHtmlFormatted = false;
  204. $formattedValue = $value;
  205. if (is_numeric($value)) {
  206. $variableType = ServerVariablesProvider::getImplementation()->getVariableType($name);
  207. if ($variableType === 'byte') {
  208. $isHtmlFormatted = true;
  209. $formattedValue = trim(
  210. $this->template->render(
  211. 'server/variables/format_variable',
  212. [
  213. 'valueTitle' => Util::formatNumber($value, 0),
  214. 'value' => implode(' ', Util::formatByteDown($value, 3, 3)),
  215. ]
  216. )
  217. );
  218. } else {
  219. $formattedValue = Util::formatNumber($value, 0);
  220. }
  221. }
  222. return [
  223. $formattedValue,
  224. $isHtmlFormatted,
  225. ];
  226. }
  227. }