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($_POST['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 .= '' . __('Replicate all databases; Ignore:');
$html .= ' ';
$html .= '' . __('Ignore all databases; Replicate:');
$html .= ' ';
$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 .= '';
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 .= ' ';
}
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 = Url::getCommon($_url_params, '', false);
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 = Url::getCommon($_url_params, '', false);
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 = Url::getCommon($_url_params, '', false);
$_url_params['sr_slave_action'] = 'reset';
$slave_control_reset_link = Url::getCommon($_url_params, '', false);
$_url_params = $GLOBALS['url_params'];
$_url_params['sr_take_action'] = true;
$_url_params['sr_slave_skip_error'] = true;
$slave_skip_error_link = Url::getCommon($_url_params, '', false);
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 = Url::getCommon($_url_params, '', false);
$html .= __(
'Server is configured as slave in a replication process. Would you ' .
'like to:'
);
$html .= '
';
$html .= '
';
$html .= '
';
} elseif (! isset($_POST['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 %sconfigure%s it?'
),
'',
' '
);
}
$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 %sconfigure%s it?'
),
'',
' '
);
$html .= ' ';
return $html;
}
/**
* returns HTML code for selecting databases
*
* @return String HTML code
*/
public static function getHtmlForReplicationDbMultibox()
{
$multi_values = '';
$multi_values .= '';
foreach ($GLOBALS['dblist']->databases as $current_db) {
if ($GLOBALS['dbi']->isSystemSchema($current_db)) {
continue;
}
$current_db = htmlspecialchars($current_db);
$multi_values .= ' ';
} // end while
$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 .= '';
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 .= $label_array['text'] . ' ';
$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 .= ' ' . __('Variable') . ' ';
$html .= ' ' . __('Value') . ' ';
$html .= ' ';
$html .= ' ';
$html .= ' ';
foreach (${"{$type}_variables"} as $variable) {
$serverReplicationVariable =
is_array(${"server_{$type}_replication"}) && isset(${"server_{$type}_replication"}[0])
? ${"server_{$type}_replication"}[0][$variable] : '';
$html .= ' ';
$html .= ' ';
$html .= htmlspecialchars($variable);
$html .= ' ';
$html .= ' ';
// TODO change to regexp or something, to allow for negative match
if (isset(${"{$type}_variables_alerts"}[$variable])
&& ${"{$type}_variables_alerts"}[$variable] == $serverReplicationVariable
) {
$html .= '';
} elseif (isset(${"{$type}_variables_oks"}[$variable])
&& ${"{$type}_variables_oks"}[$variable] == $serverReplicationVariable
) {
$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(
',',
', ',
$serverReplicationVariable
));
} else {
$html .= htmlspecialchars($serverReplicationVariable);
}
$html .= ' ';
$html .= ' ';
$html .= ' ';
}
$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 .= ' ' . __('Server ID') . ' ';
$html .= ' ' . __('Host') . ' ';
$html .= ' ';
$html .= ' ';
$html .= ' ';
foreach ($data as $slave) {
$html .= ' ';
$html .= ' ' . $slave['Server_id'] . ' ';
$html .= ' ' . $slave['Host'] . ' ';
$html .= ' ';
}
$html .= ' ';
$html .= '
';
$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($_POST['username']) && strlen($_POST['username']) === 0) {
$GLOBALS['pred_username'] = 'any';
}
$html .= ''
. ''
. ''
. ' ' . __('Password:')
. ' '
. ''
. ' '
. ' '
. ' ' . __('Use text field:') . ' '
. ' '
. ' '
. ' '
. '
'
. ''
. ''
. ' ' . __('Re-type:')
. ' '
. ' '
. ' '
. '
'
. ''
. ''
. ' ' . __('Generate password:')
. ' '
. ''
. ' '
. ' '
. ' '
. '
'
. '';
$html .= '';
return $html;
}
/**
* handle control requests
*
* @return NULL
*/
public static function handleControlRequest()
{
if (isset($_POST['sr_take_action'])) {
$refresh = false;
$result = false;
$messageSuccess = null;
$messageError = null;
if (isset($_POST['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($_POST['slave_changemaster'])) {
$result = self::handleRequestForSlaveChangeMaster();
} elseif (isset($_POST['sr_slave_server_control'])) {
$result = self::handleRequestForSlaveServerControl();
$refresh = true;
switch ($_POST['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($_POST['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($_POST['username']);
$_SESSION['replication']['m_password'] = $sr['pma_pw']
= $GLOBALS['dbi']->escapeString($_POST['pma_pw']);
$_SESSION['replication']['m_hostname'] = $sr['hostname']
= $GLOBALS['dbi']->escapeString($_POST['hostname']);
$_SESSION['replication']['m_port'] = $sr['port']
= $GLOBALS['dbi']->escapeString($_POST['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($_POST['sr_slave_control_parm'])) {
$_POST['sr_slave_control_parm'] = null;
}
if ($_POST['sr_slave_action'] == 'reset') {
$qStop = Replication::slaveControl("STOP", null, DatabaseInterface::CONNECT_USER);
$qReset = $GLOBALS['dbi']->tryQuery("RESET SLAVE;");
$qStart = Replication::slaveControl("START", null, DatabaseInterface::CONNECT_USER);
$result = ($qStop !== false && $qStop !== -1 &&
$qReset !== false && $qReset !== -1 &&
$qStart !== false && $qStart !== -1);
} else {
$qControl = Replication::slaveControl(
$_POST['sr_slave_action'],
$_POST['sr_slave_control_parm'],
DatabaseInterface::CONNECT_USER
);
$result = ($qControl !== false && $qControl !== -1);
}
return $result;
}
/**
* handle control requests for Slave Skip Error
*
* @return boolean
*/
public static function handleRequestForSlaveSkipError()
{
$count = 1;
if (isset($_POST['sr_skip_errors_count'])) {
$count = $_POST['sr_skip_errors_count'] * 1;
}
$qStop = Replication::slaveControl("STOP", null, DatabaseInterface::CONNECT_USER);
$qSkip = $GLOBALS['dbi']->tryQuery(
"SET GLOBAL SQL_SLAVE_SKIP_COUNTER = " . $count . ";"
);
$qStart = Replication::slaveControl("START", null, DatabaseInterface::CONNECT_USER);
$result = ($qStop !== false && $qStop !== -1 &&
$qSkip !== false && $qSkip !== -1 &&
$qStart !== false && $qStart !== -1);
return $result;
}
}