getDisplay(); $_SESSION['replication']['sr_action_status'] = 'unknown'; } elseif ($_SESSION['replication']['sr_action_status'] == 'success') { $success_message = $_SESSION['replication']['sr_action_info']; $html .= Message::success($success_message)->getDisplay(); $_SESSION['replication']['sr_action_status'] = 'unknown'; } } return $html; } /** * returns HTML for master replication * * @return String HTML code */ public static function getHtmlForMasterReplication() { $html = ''; if (! isset($_REQUEST['repl_clear_scr'])) { $html .= '
'; $html .= '' . __('Master replication') . ''; $html .= __('This server is configured as master in a replication process.'); $html .= '"; $html .= "
"; } return $html; } /** * returns HTML for master replication configuration * * @return String HTML code */ public static function getHtmlForMasterConfiguration() { $html = '
'; $html .= '' . __('Master configuration') . ''; $html .= __( 'This server is not configured as a master server in a ' . 'replication process. You can choose from either replicating ' . 'all databases and ignoring some of them (useful if you want to ' . 'replicate a majority of the databases) or you can choose to ignore ' . 'all databases by default and allow only certain databases to be ' . 'replicated. Please select the mode:' ) . '

'; $html .= ''; $html .= '

'; $html .= __('Please select databases:') . '
'; $html .= self::getHtmlForReplicationDbMultibox(); $html .= '

'; $html .= __( 'Now, add the following lines at the end of [mysqld] section' . ' in your my.cnf and please restart the MySQL server afterwards.' ) . '
'; $html .= '
';
        $html .= __(
            'Once you restarted MySQL server, please click on Go button. '
            . 'Afterwards, you should see a message informing you, that this server'
            . ' is configured as master.'
        );
        $html .= '
'; $html .= '
'; $html .= '
'; $html .= Url::getHiddenInputs('', ''); $html .= ' '; $html .= '
'; $html .= '
'; return $html; } /** * returns HTML for slave replication configuration * * @param bool $server_slave_status Whether it is Master or Slave * @param array $server_slave_replication Slave replication * * @return String HTML code */ public static function getHtmlForSlaveConfiguration( $server_slave_status, array $server_slave_replication ) { $html = '
'; $html .= '' . __('Slave replication') . ''; /** * check for multi-master replication functionality */ $server_slave_multi_replication = $GLOBALS['dbi']->fetchResult( 'SHOW ALL SLAVES STATUS' ); if ($server_slave_multi_replication) { $html .= __('Master connection:'); $html .= '
'; $html .= Url::getHiddenInputs($GLOBALS['url_params']); $html .= ' '; $html .= ' '; $html .= '
'; $html .= '

'; } if ($server_slave_status) { $html .= '
'; $_url_params = $GLOBALS['url_params']; $_url_params['sr_take_action'] = true; $_url_params['sr_slave_server_control'] = true; if ($server_slave_replication[0]['Slave_IO_Running'] == 'No') { $_url_params['sr_slave_action'] = 'start'; } else { $_url_params['sr_slave_action'] = 'stop'; } $_url_params['sr_slave_control_parm'] = 'IO_THREAD'; $slave_control_io_link = 'server_replication.php' . Url::getCommon($_url_params); if ($server_slave_replication[0]['Slave_SQL_Running'] == 'No') { $_url_params['sr_slave_action'] = 'start'; } else { $_url_params['sr_slave_action'] = 'stop'; } $_url_params['sr_slave_control_parm'] = 'SQL_THREAD'; $slave_control_sql_link = 'server_replication.php' . Url::getCommon($_url_params); if ($server_slave_replication[0]['Slave_IO_Running'] == 'No' || $server_slave_replication[0]['Slave_SQL_Running'] == 'No' ) { $_url_params['sr_slave_action'] = 'start'; } else { $_url_params['sr_slave_action'] = 'stop'; } $_url_params['sr_slave_control_parm'] = null; $slave_control_full_link = 'server_replication.php' . Url::getCommon($_url_params); $_url_params['sr_slave_action'] = 'reset'; $slave_control_reset_link = 'server_replication.php' . Url::getCommon($_url_params); $_url_params = $GLOBALS['url_params']; $_url_params['sr_take_action'] = true; $_url_params['sr_slave_skip_error'] = true; $slave_skip_error_link = 'server_replication.php' . Url::getCommon($_url_params); if ($server_slave_replication[0]['Slave_SQL_Running'] == 'No') { $html .= Message::error( __('Slave SQL Thread not running!') )->getDisplay(); } if ($server_slave_replication[0]['Slave_IO_Running'] == 'No') { $html .= Message::error( __('Slave IO Thread not running!') )->getDisplay(); } $_url_params = $GLOBALS['url_params']; $_url_params['sl_configure'] = true; $_url_params['repl_clear_scr'] = true; $reconfiguremaster_link = 'server_replication.php' . Url::getCommon($_url_params); $html .= __( 'Server is configured as slave in a replication process. Would you ' . 'like to:' ); $html .= '
'; $html .= ''; $html .= '
'; } elseif (! isset($_REQUEST['sl_configure'])) { $_url_params = $GLOBALS['url_params']; $_url_params['sl_configure'] = true; $_url_params['repl_clear_scr'] = true; $html .= sprintf( __( 'This server is not configured as slave in a replication process. ' . 'Would you like to configure it?' ), 'server_replication.php' . Url::getCommon($_url_params) ); } $html .= '
'; return $html; } /** * returns HTML for Slave Error Management * * @param String $slave_skip_error_link error link * * @return String HTML code */ public static function getHtmlForSlaveErrorManagement($slave_skip_error_link) { $html = ''; $html .= __('Error management:') . ''; $html .= '
'; $html .= Message::error( __('Skipping errors might lead into unsynchronized master and slave!') )->getDisplay(); $html .= ' '; $html .= '
'; return $html; } /** * returns HTML for not configure for a server replication * * @return String HTML code */ public static function getHtmlForNotServerReplication() { $_url_params = $GLOBALS['url_params']; $_url_params['mr_configure'] = true; $html = '
'; $html .= '' . __('Master replication') . ''; $html .= sprintf( __( 'This server is not configured as master in a replication process. ' . 'Would you like to configure it?' ), 'server_replication.php' . Url::getCommon($_url_params) ); $html .= '
'; return $html; } /** * returns HTML code for selecting databases * * @return String HTML code */ public static function getHtmlForReplicationDbMultibox() { $multi_values = ''; $multi_values .= '
'; $multi_values .= '' . __('Select all') . ''; $multi_values .= ' / '; $multi_values .= '' . __('Unselect all') . ''; return $multi_values; } /** * returns HTML for changing master * * @param String $submitname - submit button name * * @return String HTML code */ public static function getHtmlForReplicationChangeMaster($submitname) { $html = ''; list($username_length, $hostname_length) = self::getUsernameHostnameLength(); $html .= '
'; $html .= Url::getHiddenInputs('', ''); $html .= '
'; $html .= ' ' . __('Slave configuration'); $html .= ' - ' . __('Change or reconfigure master server') . ''; $html .= __( 'Make sure you have a unique server-id in your configuration file (my.cnf). ' . 'If not, please add the following line into [mysqld] section:' ); $html .= '
'; $html .= '
server-id=' . time() . '
'; $html .= self::getHtmlForAddUserInputDiv( array('text'=>__('User name:'), 'for'=>"text_username"), array( 'type'=>'text', 'name'=>'username', 'id'=>'text_username', 'maxlength'=>$username_length, 'title'=>__('User name'), 'required'=>'required' ) ); $html .= self::getHtmlForAddUserInputDiv( array('text'=>__('Password:'), 'for'=>"text_pma_pw"), array( 'type'=>'password', 'name'=>'pma_pw', 'id'=>'text_pma_pw', 'title'=>__('Password'), 'required'=>'required' ) ); $html .= self::getHtmlForAddUserInputDiv( array('text'=>__('Host:'), 'for'=>"text_hostname"), array( 'type'=>'text', 'name'=>'hostname', 'id'=>'text_hostname', 'maxlength'=>$hostname_length, 'value'=>'', 'required'=>'required' ) ); $html .= self::getHtmlForAddUserInputDiv( array('text'=>__('Port:'), 'for'=>"text_port"), array( 'type'=>'number', 'name'=>'text_port', 'id'=>'text_port', 'maxlength'=>6, 'value'=>'3306', 'required'=>'required' ) ); $html .= '
'; $html .= ' '; $html .= '
'; return $html; } /** * returns HTML code for Add user input div * * @param array $label_array label tag elements * @param array $input_array input tag elements * * @return String HTML code */ public static function getHtmlForAddUserInputDiv(array $label_array, array $input_array) { $html = '
'; $html .= ' '; $html .= ' $value) { $html .= ' ' . $key . '="' . $value . '" '; } $html .= ' />'; $html .= '
'; return $html; } /** * This function returns html code for table with replication status. * * @param string $type either master or slave * @param boolean $hidden if true, then default style is set to hidden, * default value false * @param boolean $title if true, then title is displayed, default true * * @return String HTML code */ public static function getHtmlForReplicationStatusTable($type, $hidden = false, $title = true) { global ${"{$type}_variables"}; global ${"{$type}_variables_alerts"}; global ${"{$type}_variables_oks"}; global ${"server_{$type}_replication"}; global ${"strReplicationStatus_{$type}"}; $html = ''; // TODO check the Masters server id? // seems to default to '1' when queried via SHOW VARIABLES , // but resulted in error on the master when slave connects // [ERROR] Error reading packet from server: Misconfigured master // - server id was not set ( server_errno=1236) // [ERROR] Got fatal error 1236: 'Misconfigured master // - server id was not set' from master when reading data from binary log // //$server_id = $GLOBALS['dbi']->fetchValue( // "SHOW VARIABLES LIKE 'server_id'", 0, 1 //); $html .= '
'; if ($title) { if ($type == 'master') { $html .= '

'; $html .= __('Master status') . '

'; } else { $html .= '

'; $html .= __('Slave status') . '

'; } } else { $html .= '
'; } $html .= ' '; $html .= ' '; $html .= ' '; $html .= ' '; $html .= ' '; $html .= ' '; $html .= ' '; $html .= ' '; foreach (${"{$type}_variables"} as $variable) { $html .= ' '; $html .= ' '; $html .= ' '; $html .= ' '; } $html .= ' '; $html .= '
' . __('Variable') . '' . __('Value') . '
'; $html .= htmlspecialchars($variable); $html .= ' '; // TODO change to regexp or something, to allow for negative match if (isset(${"{$type}_variables_alerts"}[$variable]) && ${"{$type}_variables_alerts"}[$variable] == ${"server_{$type}_replication"}[0][$variable] ) { $html .= ''; } elseif (isset(${"{$type}_variables_oks"}[$variable]) && ${"{$type}_variables_oks"}[$variable] == ${"server_{$type}_replication"}[0][$variable] ) { $html .= ''; } else { $html .= ''; } // allow wrapping long table lists into multiple lines static $variables_wrap = array( 'Replicate_Do_DB', 'Replicate_Ignore_DB', 'Replicate_Do_Table', 'Replicate_Ignore_Table', 'Replicate_Wild_Do_Table', 'Replicate_Wild_Ignore_Table'); if (in_array($variable, $variables_wrap)) { $html .= htmlspecialchars(str_replace( ',', ', ', ${"server_{$type}_replication"}[0][$variable] )); } else { $html .= htmlspecialchars(${"server_{$type}_replication"}[0][$variable]); } $html .= ''; $html .= '
'; $html .= '
'; $html .= '
'; return $html; } /** * returns html code for table with slave users connected to this master * * @param boolean $hidden - if true, then default style is set to hidden, * - default value false * * @return string */ public static function getHtmlForReplicationSlavesTable($hidden = false) { $html = ''; // Fetch data $data = $GLOBALS['dbi']->fetchResult('SHOW SLAVE HOSTS', null, null); $html .= '
'; $html .= '
'; $html .= ' '; $html .= ' '; $html .= ' '; $html .= ' '; $html .= ' '; $html .= ' '; $html .= ' '; $html .= ' '; foreach ($data as $slave) { $html .= ' '; $html .= ' '; $html .= ' '; $html .= ' '; } $html .= ' '; $html .= '
' . __('Server ID') . '' . __('Host') . '
' . $slave['Server_id'] . '' . $slave['Host'] . '
'; $html .= '
'; $html .= Message::notice( __( 'Only slaves started with the ' . '--report-host=host_name option are visible in this list.' ) )->getDisplay(); $html .= '
'; $html .= '
'; return $html; } /** * get the correct username and hostname lengths for this MySQL server * * @return array username length, hostname length */ public static function getUsernameHostnameLength() { $fields_info = $GLOBALS['dbi']->getColumns('mysql', 'user'); $username_length = 16; $hostname_length = 41; foreach ($fields_info as $val) { if ($val['Field'] == 'User') { strtok($val['Type'], '()'); $v = strtok('()'); if (is_int($v)) { $username_length = $v; } } elseif ($val['Field'] == 'Host') { strtok($val['Type'], '()'); $v = strtok('()'); if (is_int($v)) { $hostname_length = $v; } } } return array($username_length, $hostname_length); } /** * returns html code to add a replication slave user to the master * * @return String HTML code */ public static function getHtmlForReplicationMasterAddSlaveUser() { $html = ''; list($username_length, $hostname_length) = self::getUsernameHostnameLength(); if (isset($_REQUEST['username']) && strlen($_REQUEST['username']) === 0) { $GLOBALS['pred_username'] = 'any'; } $html .= '
'; $html .= '
' . __('Add slave replication user') . '' . self::getHtmlForAddUserLoginForm($username_length) . '
' . '' . '' . ' ' . '' . '' . ' ' . '' . '
' . '' . '' . ' ' . '' . '' . '
'; return $html; } /** * returns HTML for TableInfoForm * * @param int $hostname_length Selected hostname length * * @return String HTML code */ public static function getHtmlForTableInfoForm($hostname_length) { $html = ' ' . ' ' . ' ' . '
' . '' . Util::showHint( __( 'When Host table is used, this field is ignored ' . 'and values stored in Host table are used instead.' ) ) . '
' . '
' . '' . '' . ' ' . '' . '' . '
' . '
' . '' . ' ' . '' . '
' . '
' . '' . '' . ' ' . '' . '' . '
' . ''; $html .= ''; return $html; } /** * handle control requests * * @return NULL */ public static function handleControlRequest() { if (isset($_REQUEST['sr_take_action'])) { $refresh = false; $result = false; $messageSuccess = null; $messageError = null; if (isset($_REQUEST['slave_changemaster']) && ! $GLOBALS['cfg']['AllowArbitraryServer']) { $_SESSION['replication']['sr_action_status'] = 'error'; $_SESSION['replication']['sr_action_info'] = __('Connection to server is disabled, please enable $cfg[\'AllowArbitraryServer\'] in phpMyAdmin configuration.'); } elseif (isset($_REQUEST['slave_changemaster'])) { $result = self::handleRequestForSlaveChangeMaster(); } elseif (isset($_REQUEST['sr_slave_server_control'])) { $result = self::handleRequestForSlaveServerControl(); $refresh = true; switch ($_REQUEST['sr_slave_action']) { case 'start': $messageSuccess = __('Replication started successfully.'); $messageError = __('Error starting replication.'); break; case 'stop': $messageSuccess = __('Replication stopped successfully.'); $messageError = __('Error stopping replication.'); break; case 'reset': $messageSuccess = __('Replication resetting successfully.'); $messageError = __('Error resetting replication.'); break; default: $messageSuccess = __('Success.'); $messageError = __('Error.'); break; } } elseif (isset($_REQUEST['sr_slave_skip_error'])) { $result = self::handleRequestForSlaveSkipError(); } if ($refresh) { $response = Response::getInstance(); if ($response->isAjax()) { $response->setRequestStatus($result); $response->addJSON( 'message', $result ? Message::success($messageSuccess) : Message::error($messageError) ); } else { Core::sendHeaderLocation( './server_replication.php' . Url::getCommonRaw($GLOBALS['url_params']) ); } } unset($refresh); } } /** * handle control requests for Slave Change Master * * @return boolean */ public static function handleRequestForSlaveChangeMaster() { $sr = array(); $_SESSION['replication']['m_username'] = $sr['username'] = $GLOBALS['dbi']->escapeString($_REQUEST['username']); $_SESSION['replication']['m_password'] = $sr['pma_pw'] = $GLOBALS['dbi']->escapeString($_REQUEST['pma_pw']); $_SESSION['replication']['m_hostname'] = $sr['hostname'] = $GLOBALS['dbi']->escapeString($_REQUEST['hostname']); $_SESSION['replication']['m_port'] = $sr['port'] = $GLOBALS['dbi']->escapeString($_REQUEST['text_port']); $_SESSION['replication']['m_correct'] = ''; $_SESSION['replication']['sr_action_status'] = 'error'; $_SESSION['replication']['sr_action_info'] = __('Unknown error'); // Attempt to connect to the new master server $link_to_master = Replication::connectToMaster( $sr['username'], $sr['pma_pw'], $sr['hostname'], $sr['port'] ); if (! $link_to_master) { $_SESSION['replication']['sr_action_status'] = 'error'; $_SESSION['replication']['sr_action_info'] = sprintf( __('Unable to connect to master %s.'), htmlspecialchars($sr['hostname']) ); } else { // Read the current master position $position = Replication::slaveBinLogMaster($link_to_master); if (empty($position)) { $_SESSION['replication']['sr_action_status'] = 'error'; $_SESSION['replication']['sr_action_info'] = __( 'Unable to read master log position. ' . 'Possible privilege problem on master.' ); } else { $_SESSION['replication']['m_correct'] = true; if (! Replication::slaveChangeMaster( $sr['username'], $sr['pma_pw'], $sr['hostname'], $sr['port'], $position, true, false ) ) { $_SESSION['replication']['sr_action_status'] = 'error'; $_SESSION['replication']['sr_action_info'] = __('Unable to change master!'); } else { $_SESSION['replication']['sr_action_status'] = 'success'; $_SESSION['replication']['sr_action_info'] = sprintf( __('Master server changed successfully to %s.'), htmlspecialchars($sr['hostname']) ); } } } return $_SESSION['replication']['sr_action_status'] === 'success'; } /** * handle control requests for Slave Server Control * * @return boolean */ public static function handleRequestForSlaveServerControl() { if (empty($_REQUEST['sr_slave_control_parm'])) { $_REQUEST['sr_slave_control_parm'] = null; } if ($_REQUEST['sr_slave_action'] == 'reset') { $qStop = Replication::slaveControl("STOP"); $qReset = $GLOBALS['dbi']->tryQuery("RESET SLAVE;"); $qStart = Replication::slaveControl("START"); $result = ($qStop !== false && $qStop !== -1 && $qReset !== false && $qReset !== -1 && $qStart !== false && $qStart !== -1); } else { $qControl = Replication::slaveControl( $_REQUEST['sr_slave_action'], $_REQUEST['sr_slave_control_parm'] ); $result = ($qControl !== false && $qControl !== -1); } return $result; } /** * handle control requests for Slave Skip Error * * @return boolean */ public static function handleRequestForSlaveSkipError() { $count = 1; if (isset($_REQUEST['sr_skip_errors_count'])) { $count = $_REQUEST['sr_skip_errors_count'] * 1; } $qStop = Replication::slaveControl("STOP"); $qSkip = $GLOBALS['dbi']->tryQuery( "SET GLOBAL SQL_SLAVE_SKIP_COUNTER = " . $count . ";" ); $qStart = Replication::slaveControl("START"); $result = ($qStop !== false && $qStop !== -1 && $qSkip !== false && $qSkip !== -1 && $qStart !== false && $qStart !== -1); return $result; } }