isAjax() && isset($_GET['type']) && $_GET['type'] === 'getval' ) { $this->getValueAction(); return; } if ($response->isAjax() && isset($_POST['type']) && $_POST['type'] === 'setval' ) { $this->setValueAction(); return; } include ROOT_PATH . 'libraries/server_common.inc.php'; $header = $this->response->getHeader(); $scripts = $header->getScripts(); $scripts->addFile('server_variables.js'); /** * Displays the sub-page heading */ $this->response->addHTML( $this->template->render('server/sub_page_header', [ 'type' => 'variables', 'link' => 'server_system_variables', ]) ); /** * Sends the queries and buffers the results */ $serverVarsResult = $this->dbi->tryQuery('SHOW SESSION VARIABLES;'); if ($serverVarsResult !== false) { $serverVarsSession = []; while ($arr = $this->dbi->fetchRow($serverVarsResult)) { $serverVarsSession[$arr[0]] = $arr[1]; } $this->dbi->freeResult($serverVarsResult); $serverVars = $this->dbi->fetchResult('SHOW GLOBAL VARIABLES;', 0, 1); /** * Link templates */ $this->response->addHtml($this->_getHtmlForLinkTemplates()); /** * Displays the page */ $this->response->addHtml( $this->_getHtmlForServerVariables($serverVars, $serverVarsSession) ); } else { /** * Display the error message */ $this->response->addHTML( Message::error( sprintf( __( 'Not enough privilege to view server variables and ' . 'settings. %s' ), Util::linkToVarDocumentation( 'show_compatibility_56', $this->dbi->isMariaDB() ) ) )->getDisplay() ); } } /** * Handle the AJAX request for a single variable value * * @return void */ public function getValueAction() { // Send with correct charset header('Content-Type: text/html; charset=UTF-8'); // Do not use double quotes inside the query to avoid a problem // when server is running in ANSI_QUOTES sql_mode $varValue = $this->dbi->fetchSingleRow( 'SHOW GLOBAL VARIABLES WHERE Variable_name=\'' . $this->dbi->escapeString($_GET['varName']) . '\';', 'NUM' ); try { $type = KBSearch::getVariableType($_GET['varName']); if ($type === 'byte') { $this->response->addJSON( 'message', implode( ' ', Util::formatByteDown($varValue[1], 3, 3) ) ); } else { throw new KBException("Not a type=byte"); } } catch (KBException $e) { $this->response->addJSON( 'message', $varValue[1] ); } } /** * Handle the AJAX request for setting value for a single variable * * @return void */ public function setValueAction() { $value = $_POST['varValue']; $matches = []; try { $type = KBSearch::getVariableType($_POST['varName']); if ($type === 'byte' && preg_match( '/^\s*(\d+(\.\d+)?)\s*(mb|kb|mib|kib|gb|gib)\s*$/i', $value, $matches )) { $exp = [ 'kb' => 1, 'kib' => 1, 'mb' => 2, 'mib' => 2, 'gb' => 3, 'gib' => 3, ]; $value = floatval($matches[1]) * pow( 1024, $exp[mb_strtolower($matches[3])] ); } else { throw new KBException("Not a type=byte or regex not matching"); } } catch (KBException $e) { $value = $this->dbi->escapeString($value); } if (! is_numeric($value)) { $value = "'" . $value . "'"; } if (! preg_match("/[^a-zA-Z0-9_]+/", $_POST['varName']) && $this->dbi->query( 'SET GLOBAL ' . $_POST['varName'] . ' = ' . $value ) ) { // Some values are rounded down etc. $varValue = $this->dbi->fetchSingleRow( 'SHOW GLOBAL VARIABLES WHERE Variable_name="' . $this->dbi->escapeString($_POST['varName']) . '";', 'NUM' ); list($formattedValue, $isHtmlFormatted) = $this->_formatVariable( $_POST['varName'], $varValue[1] ); if ($isHtmlFormatted == false) { $this->response->addJSON( 'variable', htmlspecialchars( $formattedValue ) ); } else { $this->response->addJSON( 'variable', $formattedValue ); } } else { $this->response->setRequestStatus(false); $this->response->addJSON( 'error', __('Setting variable failed') ); } } /** * Format Variable * * @param string $name variable name * @param integer $value variable value * * @return array formatted string and bool if string is HTML formatted */ private function _formatVariable($name, $value) { $isHtmlFormatted = false; $formattedValue = $value; if (is_numeric($value)) { try { $type = KBSearch::getVariableType($name); if ($type === 'byte') { $isHtmlFormatted = true; $formattedValue = '' . htmlspecialchars( implode(' ', Util::formatByteDown($value, 3, 3)) ) . ''; } else { throw new KBException("Not a type=byte or regex not matching"); } } catch (KBException $e) { $formattedValue = Util::formatNumber($value, 0); } } return [ $formattedValue, $isHtmlFormatted, ]; } /** * Prints link templates * * @return string */ private function _getHtmlForLinkTemplates() { $url = 'server_variables.php' . Url::getCommon(); return $this->template->render('server/variables/link_template', ['url' => $url]); } /** * Prints Html for Server Variables * * @param array $serverVars global variables * @param array $serverVarsSession session variables * * @return string */ private function _getHtmlForServerVariables(array $serverVars, array $serverVarsSession) { // filter $filterValue = ! empty($_REQUEST['filter']) ? $_REQUEST['filter'] : ''; $output = $this->template->render('filter', ['filter_value' => $filterValue]); $output .= '