Translated using Weblate (Armenian)
[phpmyadmin.git] / libraries / replication_gui.lib.php
blob2ffd20a9f25b7c494fda89712f4a1ea8637866b5
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Functions for the replication GUI
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * returns HTML for error message
15 * @return String HTML code
17 function PMA_getHtmlForErrorMessage()
19 $html = '';
20 if (isset($_SESSION['replication']['sr_action_status'])
21 && isset($_SESSION['replication']['sr_action_info'])
22 ) {
23 if ($_SESSION['replication']['sr_action_status'] == 'error') {
24 $error_message = $_SESSION['replication']['sr_action_info'];
25 $html .= PMA_Message::error($error_message)->getDisplay();
26 $_SESSION['replication']['sr_action_status'] = 'unknown';
27 } elseif ($_SESSION['replication']['sr_action_status'] == 'success') {
28 $success_message = $_SESSION['replication']['sr_action_info'];
29 $html .= PMA_Message::success($success_message)->getDisplay();
30 $_SESSION['replication']['sr_action_status'] = 'unknown';
33 return $html;
36 /**
37 * returns HTML for master replication
39 * @return String HTML code
41 function PMA_getHtmlForMasterReplication()
43 $html = '';
44 if (! isset($_REQUEST['repl_clear_scr'])) {
45 $html .= '<fieldset>';
46 $html .= '<legend>' . __('Master replication') . '</legend>';
47 $html .= __('This server is configured as master in a replication process.');
48 $html .= '<ul>';
49 $html .= ' <li><a href="#" id="master_status_href">';
50 $html .= __('Show master status') . '</a>';
51 $html .= PMA_getHtmlForReplicationStatusTable('master', true, false);
52 $html .= ' </li>';
54 $html .= ' <li><a href="#" id="master_slaves_href">';
55 $html .= __('Show connected slaves') . '</a>';
56 $html .= PMA_getHtmlForReplicationSlavesTable(true);
57 $html .= ' </li>';
59 $_url_params = $GLOBALS['url_params'];
60 $_url_params['mr_adduser'] = true;
61 $_url_params['repl_clear_scr'] = true;
63 $html .= ' <li><a href="server_replication.php';
64 $html .= PMA_URL_getCommon($_url_params)
65 . '" id="master_addslaveuser_href">';
66 $html .= __('Add slave replication user') . '</a></li>';
69 // Display 'Add replication slave user' form
70 if (isset($_REQUEST['mr_adduser'])) {
71 $html .= PMA_getHtmlForReplicationMasterAddSlaveuser();
72 } elseif (! isset($_REQUEST['repl_clear_scr'])) {
73 $html .= "</ul>";
74 $html .= "</fieldset>";
77 return $html;
80 /**
81 * returns HTML for master replication configuration
83 * @return String HTML code
85 function PMA_getHtmlForMasterConfiguration()
87 $html = '<fieldset>';
88 $html .= '<legend>' . __('Master configuration') . '</legend>';
89 $html .= __(
90 'This server is not configured as master server in a '
91 . 'replication process. You can choose from either replicating '
92 . 'all databases and ignoring certain (useful if you want to replicate '
93 . 'majority of databases) or you can choose to ignore all databases by '
94 . 'default and allow only certain databases to be replicated. '
95 . 'Please select the mode:'
96 ) . '<br /><br />';
98 $html .= '<select name="db_type" id="db_type">';
99 $html .= '<option value="all">' . __('Replicate all databases; Ignore:');
100 $html .= '</option>';
101 $html .= '<option value="ign">' . __('Ignore all databases; Replicate:');
102 $html .= '</option>';
103 $html .= '</select>';
104 $html .= '<br /><br />';
105 $html .= __('Please select databases:') . '<br />';
106 $html .= PMA_getHtmlForReplicationDbMultibox();
107 $html .= '<br /><br />';
108 $html .= __(
109 'Now, add the following lines at the end of [mysqld] section'
110 . ' in your my.cnf and please restart the MySQL server afterwards.'
111 ) . '<br />';
112 $html .= '<pre id="rep"></pre>';
113 $html .= __(
114 'Once you restarted MySQL server, please click on Go button. '
115 . 'Afterwards, you should see a message informing you, that this server'
116 . ' <b>is</b> configured as master.'
118 $html .= '</fieldset>';
119 $html .= '<fieldset class="tblFooters">';
120 $html .= ' <form method="post" action="server_replication.php" >';
121 $html .= PMA_URL_getHiddenInputs('', '');
122 $html .= ' <input type="submit" value="' . __('Go') . '" id="goButton" />';
123 $html .= ' </form>';
124 $html .= '</fieldset>';
126 return $html;
130 * returns HTML for slave replication configuration
132 * @param bool $server_slave_status Whether it is Master or Slave
133 * @param Array $server_slave_replication Slave replication
135 * @return String HTML code
137 function PMA_getHtmlForSlaveConfiguration(
138 $server_slave_status, $server_slave_replication
140 $html = '<fieldset>';
141 $html .= '<legend>' . __('Slave replication') . '</legend>';
143 * check for multi-master replication functionality
145 $server_slave_multi_replication = $GLOBALS['dbi']->fetchResult(
146 'SHOW ALL SLAVES STATUS'
148 if ($server_slave_multi_replication) {
149 $html .= __('Master connection:');
150 $html .= '<form method="get" action="server_replication.php">';
151 $html .= PMA_URL_getHiddenInputs($GLOBALS['url_params']);
152 $html .= ' <select name="master_connection">';
153 $html .= '<option value="">' . __('Default') . '</option>';
154 foreach ($server_slave_multi_replication as $server) {
155 $html .= '<option' . (isset($_REQUEST['master_connection'])
156 && $_REQUEST['master_connection'] == $server['Connection_name'] ?
157 ' selected="selected"' : '') . '>' . $server['Connection_name']
158 . '</option>';
160 $html .= '</select>';
161 $html .= ' <input type="submit" value="' . __('Go') . '" id="goButton" />';
162 $html .= '</form>';
163 $html .= '<br /><br />';
165 if ($server_slave_status) {
166 $html .= '<div id="slave_configuration_gui">';
168 $_url_params = $GLOBALS['url_params'];
169 $_url_params['sr_take_action'] = true;
170 $_url_params['sr_slave_server_control'] = true;
172 if ($server_slave_replication[0]['Slave_IO_Running'] == 'No') {
173 $_url_params['sr_slave_action'] = 'start';
174 } else {
175 $_url_params['sr_slave_action'] = 'stop';
178 $_url_params['sr_slave_control_parm'] = 'IO_THREAD';
179 $slave_control_io_link = 'server_replication.php'
180 . PMA_URL_getCommon($_url_params);
182 if ($server_slave_replication[0]['Slave_SQL_Running'] == 'No') {
183 $_url_params['sr_slave_action'] = 'start';
184 } else {
185 $_url_params['sr_slave_action'] = 'stop';
188 $_url_params['sr_slave_control_parm'] = 'SQL_THREAD';
189 $slave_control_sql_link = 'server_replication.php'
190 . PMA_URL_getCommon($_url_params);
192 if ($server_slave_replication[0]['Slave_IO_Running'] == 'No'
193 || $server_slave_replication[0]['Slave_SQL_Running'] == 'No'
195 $_url_params['sr_slave_action'] = 'start';
196 } else {
197 $_url_params['sr_slave_action'] = 'stop';
200 $_url_params['sr_slave_control_parm'] = null;
201 $slave_control_full_link = 'server_replication.php'
202 . PMA_URL_getCommon($_url_params);
204 $_url_params['sr_slave_action'] = 'reset';
205 $slave_control_reset_link = 'server_replication.php'
206 . PMA_URL_getCommon($_url_params);
208 $_url_params = $GLOBALS['url_params'];
209 $_url_params['sr_take_action'] = true;
210 $_url_params['sr_slave_skip_error'] = true;
211 $slave_skip_error_link = 'server_replication.php'
212 . PMA_URL_getCommon($_url_params);
214 if ($server_slave_replication[0]['Slave_SQL_Running'] == 'No') {
215 $html .= PMA_Message::error(
216 __('Slave SQL Thread not running!')
217 )->getDisplay();
219 if ($server_slave_replication[0]['Slave_IO_Running'] == 'No') {
220 $html .= PMA_Message::error(
221 __('Slave IO Thread not running!')
222 )->getDisplay();
225 $_url_params = $GLOBALS['url_params'];
226 $_url_params['sl_configure'] = true;
227 $_url_params['repl_clear_scr'] = true;
229 $reconfiguremaster_link = 'server_replication.php'
230 . PMA_URL_getCommon($_url_params);
232 $html .= __(
233 'Server is configured as slave in a replication process. Would you ' .
234 'like to:'
236 $html .= '<br />';
237 $html .= '<ul>';
238 $html .= ' <li><a href="#" id="slave_status_href">';
239 $html .= __('See slave status table') . '</a>';
240 $html .= PMA_getHtmlForReplicationStatusTable('slave', true, false);
241 $html .= ' </li>';
243 $html .= ' <li><a href="#" id="slave_control_href">';
244 $html .= __('Control slave:') . '</a>';
245 $html .= ' <div id="slave_control_gui" style="display: none">';
246 $html .= ' <ul>';
247 $html .= ' <li><a href="' . $slave_control_full_link . '">';
248 $html .= (($server_slave_replication[0]['Slave_IO_Running'] == 'No' ||
249 $server_slave_replication[0]['Slave_SQL_Running'] == 'No')
250 ? __('Full start')
251 : __('Full stop')) . ' </a></li>';
252 $html .= ' <li><a href="' . $slave_control_reset_link . '">';
253 $html .= __('Reset slave') . '</a></li>';
254 if ($server_slave_replication[0]['Slave_SQL_Running'] == 'No') {
255 $html .= ' <li><a href="' . $slave_control_sql_link . '">';
256 $html .= __('Start SQL Thread only') . '</a></li>';
257 } else {
258 $html .= ' <li><a href="' . $slave_control_sql_link . '">';
259 $html .= __('Stop SQL Thread only') . '</a></li>';
261 if ($server_slave_replication[0]['Slave_IO_Running'] == 'No') {
262 $html .= ' <li><a href="' . $slave_control_io_link . '">';
263 $html .= __('Start IO Thread only') . '</a></li>';
264 } else {
265 $html .= ' <li><a href="' . $slave_control_io_link . '">';
266 $html .= __('Stop IO Thread only') . '</a></li>';
268 $html .= ' </ul>';
269 $html .= ' </div>';
270 $html .= ' </li>';
271 $html .= ' <li>';
272 $html .= PMA_getHtmlForSlaveErrorManagement($slave_skip_error_link);
273 $html .= ' </li>';
274 $html .= ' <li><a href="' . $reconfiguremaster_link . '">';
275 $html .= __('Change or reconfigure master server') . '</a></li>';
276 $html .= '</ul>';
277 $html .= '</div>';
279 } elseif (! isset($_REQUEST['sl_configure'])) {
280 $_url_params = $GLOBALS['url_params'];
281 $_url_params['sl_configure'] = true;
282 $_url_params['repl_clear_scr'] = true;
284 $html .= sprintf(
286 'This server is not configured as slave in a replication process. '
287 . 'Would you like to <a href="%s">configure</a> it?'
289 'server_replication.php' . PMA_URL_getCommon($_url_params)
292 $html .= '</fieldset>';
294 return $html;
298 * returns HTML for Slave Error Management
300 * @param String $slave_skip_error_link error link
302 * @return String HTML code
304 function PMA_getHtmlForSlaveErrorManagement($slave_skip_error_link)
306 $html = '<a href="#" id="slave_errormanagement_href">';
307 $html .= __('Error management:') . '</a>';
308 $html .= ' <div id="slave_errormanagement_gui" style="display: none">';
309 $html .= PMA_Message::error(
310 __('Skipping errors might lead into unsynchronized master and slave!')
311 )->getDisplay();
312 $html .= ' <ul>';
313 $html .= ' <li><a href="' . $slave_skip_error_link . '">';
314 $html .= __('Skip current error') . '</a></li>';
315 $html .= ' <li>';
316 $html .= ' <form method="post" action="server_replication.php">';
317 $html .= PMA_URL_getHiddenInputs('', '');
318 $html .= sprintf(
319 __('Skip next %s errors.'),
320 '<input type="text" name="sr_skip_errors_count" value="1" '
321 . 'style="width: 30px" />'
323 $html .= ' <input type="submit" name="sr_slave_skip_error" ';
324 $html .= 'value="' . __('Go') . '" />';
325 $html .= ' <input type="hidden" name="sr_take_action" value="1" />';
326 $html .= ' </form></li>';
327 $html .= ' </ul>';
328 $html .= ' </div>';
329 return $html;
333 * returns HTML for not configure for a server replication
335 * @return String HTML code
337 function PMA_getHtmlForNotServerReplication()
339 $_url_params = $GLOBALS['url_params'];
340 $_url_params['mr_configure'] = true;
342 $html = '<fieldset>';
343 $html .= '<legend>' . __('Master replication') . '</legend>';
344 $html .= sprintf(
346 'This server is not configured as master in a replication process. '
347 . 'Would you like to <a href="%s">configure</a> it?'
349 'server_replication.php' . PMA_URL_getCommon($_url_params)
351 $html .= '</fieldset>';
352 return $html;
356 * returns HTML code for selecting databases
358 * @return String HTML code
360 function PMA_getHtmlForReplicationDbMultibox()
362 $multi_values = '';
363 $multi_values .= '<select name="db_select[]" '
364 . 'size="6" multiple="multiple" id="db_select">';
366 foreach ($GLOBALS['pma']->databases as $current_db) {
367 if ($GLOBALS['dbi']->isSystemSchema($current_db)) {
368 continue;
370 $current_db = htmlspecialchars($current_db);
371 $multi_values .= ' <option value="' . $current_db . '" ';
372 $multi_values .= '>';
373 $multi_values .= $current_db . '</option>';
374 } // end while
376 $multi_values .= '</select><br />';
377 $multi_values .= '<a href="#" id="db_select_href">' . __('Select All') . '</a>';
378 $multi_values .= '&nbsp;/&nbsp;';
379 $multi_values .= '<a href="#" id="db_reset_href">' . __('Unselect All') . '</a>';
381 return $multi_values;
385 * returns HTML for changing master
387 * @param String $submitname - submit button name
389 * @return String HTML code
391 function PMA_getHtmlForReplicationChangeMaster($submitname)
393 $html = '';
394 list($username_length, $hostname_length)
395 = PMA_replicationGetUsernameHostnameLength();
397 $html .= '<form method="post" action="server_replication.php">';
398 $html .= PMA_URL_getHiddenInputs('', '');
399 $html .= ' <fieldset id="fieldset_add_user_login">';
400 $html .= ' <legend>' . __('Slave configuration');
401 $html .= ' - ' . __('Change or reconfigure master server') . '</legend>';
402 $html .= __(
403 'Make sure, you have unique server-id in your configuration file (my.cnf). '
404 . 'If not, please add the following line into [mysqld] section:'
406 $html .= '<br />';
407 $html .= '<pre>server-id=' . time() . '</pre>';
409 $html .= PMA_getHtmlForAddUserInputDiv(
410 array('text'=>__('User name:'), 'for'=>"text_username"),
411 array(
412 'type'=>'text',
413 'name'=>'username',
414 'id'=>'text_username',
415 'maxlength'=>$username_length,
416 'title'=>__('User name'),
417 'required'=>'required'
421 $html .= PMA_getHtmlForAddUserInputDiv(
422 array('text'=>__('Password:'), 'for'=>"text_pma_pw"),
423 array(
424 'type'=>'password',
425 'name'=>'pma_pw',
426 'id'=>'text_pma_pw',
427 'title'=>__('Password'),
428 'required'=>'required'
432 $html .= PMA_getHtmlForAddUserInputDiv(
433 array('text'=>__('Host:'), 'for'=>"text_hostname"),
434 array(
435 'type'=>'text',
436 'name'=>'hostname',
437 'id'=>'text_hostname',
438 'maxlength'=>$hostname_length,
439 'value'=>'',
440 'required'=>'required'
444 $html .= PMA_getHtmlForAddUserInputDiv(
445 array('text'=>__('Port:'), 'for'=>"text_port"),
446 array(
447 'type'=>'number',
448 'name'=>'text_port',
449 'id'=>'text_port',
450 'maxlength'=>6,
451 'value'=>'3306',
452 'required'=>'required'
456 $html .= ' </fieldset>';
457 $html .= ' <fieldset id="fieldset_user_privtable_footer" class="tblFooters">';
458 $html .= ' <input type="hidden" name="sr_take_action" value="true" />';
459 $html .= ' <input type="hidden" name="' . $submitname . '" value="1" />';
460 $html .= ' <input type="submit" id="confslave_submit" value="';
461 $html .= __('Go') . '" />';
462 $html .= ' </fieldset>';
463 $html .= '</form>';
465 return $html;
469 * returns HTML code for Add user input div
471 * @param Array $label_array label tag elements
472 * @param Array $input_array input tag elements
474 * @return String HTML code
476 function PMA_getHtmlForAddUserInputDiv($label_array, $input_array)
478 $html = ' <div class="item">';
479 $html .= ' <label for="' . $label_array['for'] . '">';
480 $html .= $label_array['text'] . '</label>';
482 $html .= ' <input ';
483 foreach ($input_array as $key=>$value) {
484 $html .= ' ' . $key . '="' . $value . '" ';
486 $html .= ' />';
487 $html .= ' </div>';
488 return $html;
492 * This function returns html code for table with replication status.
494 * @param string $type either master or slave
495 * @param boolean $hidden if true, then default style is set to hidden,
496 * default value false
497 * @param boolean $title if true, then title is displayed, default true
499 * @return String HTML code
501 function PMA_getHtmlForReplicationStatusTable($type, $hidden = false, $title = true)
503 global ${"{$type}_variables"};
504 global ${"{$type}_variables_alerts"};
505 global ${"{$type}_variables_oks"};
506 global ${"server_{$type}_replication"};
507 global ${"strReplicationStatus_{$type}"};
509 $html = '';
511 // TODO check the Masters server id?
512 // seems to default to '1' when queried via SHOW VARIABLES ,
513 // but resulted in error on the master when slave connects
514 // [ERROR] Error reading packet from server: Misconfigured master
515 // - server id was not set ( server_errno=1236)
516 // [ERROR] Got fatal error 1236: 'Misconfigured master
517 // - server id was not set' from master when reading data from binary log
519 //$server_id = $GLOBALS['dbi']->fetchValue(
520 // "SHOW VARIABLES LIKE 'server_id'", 0, 1
521 //);
523 $html .= '<div id="replication_' . $type . '_section" style="';
524 $html .= ($hidden ? 'display: none;' : '') . '"> ';
526 if ($title) {
527 if ($type == 'master') {
528 $html .= '<h4><a name="replication_' . $type . '"></a>';
529 $html .= __('Master status') . '</h4>';
530 } else {
531 $html .= '<h4><a name="replication_' . $type . '"></a>';
532 $html .= __('Slave status') . '</h4>';
534 } else {
535 $html .= '<br />';
538 $html .= ' <table id="server' . $type . 'replicationsummary" class="data"> ';
539 $html .= ' <thead>';
540 $html .= ' <tr>';
541 $html .= ' <th>' . __('Variable') . '</th>';
542 $html .= ' <th>' . __('Value') . '</th>';
543 $html .= ' </tr>';
544 $html .= ' </thead>';
545 $html .= ' <tbody>';
547 $odd_row = true;
548 foreach (${"{$type}_variables"} as $variable) {
549 $html .= ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">';
550 $html .= ' <td class="name">';
551 $html .= $variable;
552 $html .= ' </td>';
553 $html .= ' <td class="value">';
555 // TODO change to regexp or something, to allow for negative match
556 if (isset(${"{$type}_variables_alerts"}[$variable])
557 && ${"{$type}_variables_alerts"}[$variable] == ${"server_{$type}_replication"}[0][$variable]
559 $html .= '<span class="attention">';
561 } elseif (isset(${"{$type}_variables_oks"}[$variable])
562 && ${"{$type}_variables_oks"}[$variable] == ${"server_{$type}_replication"}[0][$variable]
564 $html .= '<span class="allfine">';
565 } else {
566 $html .= '<span>';
568 // allow wrapping long table lists into multiple lines
569 static $variables_wrap = array(
570 'Replicate_Do_DB', 'Replicate_Ignore_DB',
571 'Replicate_Do_Table', 'Replicate_Ignore_Table',
572 'Replicate_Wild_Do_Table', 'Replicate_Wild_Ignore_Table');
573 if (in_array($variable, $variables_wrap)) {
574 $html .= str_replace(
575 ',',
576 ', ',
577 ${"server_{$type}_replication"}[0][$variable]
579 } else {
580 $html .= ${"server_{$type}_replication"}[0][$variable];
582 $html .= '</span>';
584 $html .= ' </td>';
585 $html .= ' </tr>';
587 $odd_row = ! $odd_row;
590 $html .= ' </tbody>';
591 $html .= ' </table>';
592 $html .= ' <br />';
593 $html .= '</div>';
595 return $html;
599 * returns html code for table with slave users connected to this master
601 * @param boolean $hidden - if true, then default style is set to hidden,
602 * - default value false
604 * @return string
606 function PMA_getHtmlForReplicationSlavesTable($hidden = false)
608 $html = '';
609 // Fetch data
610 $data = $GLOBALS['dbi']->fetchResult('SHOW SLAVE HOSTS', null, null);
612 $html .= ' <br />';
613 $html .= ' <div id="replication_slaves_section" style="';
614 $html .= ($hidden ? 'display: none;' : '') . '"> ';
615 $html .= ' <table class="data">';
616 $html .= ' <thead>';
617 $html .= ' <tr>';
618 $html .= ' <th>' . __('Server ID') . '</th>';
619 $html .= ' <th>' . __('Host') . '</th>';
620 $html .= ' </tr>';
621 $html .= ' </thead>';
622 $html .= ' <tbody>';
624 $odd_row = true;
625 foreach ($data as $slave) {
626 $html .= ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">';
627 $html .= ' <td class="value">' . $slave['Server_id'] . '</td>';
628 $html .= ' <td class="value">' . $slave['Host'] . '</td>';
629 $html .= ' </tr>';
631 $odd_row = ! $odd_row;
634 $html .= ' </tbody>';
635 $html .= ' </table>';
636 $html .= ' <br />';
637 $html .= PMA_Message::notice(
639 'Only slaves started with the '
640 . '--report-host=host_name option are visible in this list.'
642 )->getDisplay();
643 $html .= ' <br />';
644 $html .= ' </div>';
646 return $html;
650 * get the correct username and hostname lengths for this MySQL server
652 * @return array username length, hostname length
654 function PMA_replicationGetUsernameHostnameLength()
656 $fields_info = $GLOBALS['dbi']->getColumns('mysql', 'user');
657 $username_length = 16;
658 $hostname_length = 41;
659 foreach ($fields_info as $val) {
660 if ($val['Field'] == 'User') {
661 strtok($val['Type'], '()');
662 $v = strtok('()');
663 if (is_int($v)) {
664 $username_length = $v;
666 } elseif ($val['Field'] == 'Host') {
667 strtok($val['Type'], '()');
668 $v = strtok('()');
669 if (is_int($v)) {
670 $hostname_length = $v;
674 return array($username_length, $hostname_length);
678 * returns html code to add a replication slave user to the master
680 * @return String HTML code
682 function PMA_getHtmlForReplicationMasterAddSlaveuser()
684 $html = '';
685 list($username_length, $hostname_length)
686 = PMA_replicationGetUsernameHostnameLength();
688 if (isset($_REQUEST['username'])
689 && /*overload*/mb_strlen($_REQUEST['username']) === 0
691 $GLOBALS['pred_username'] = 'any';
693 $html .= '<div id="master_addslaveuser_gui">';
694 $html .= '<form autocomplete="off" method="post" ';
695 $html .= 'action="server_privileges.php"';
696 $html .= ' onsubmit="return checkAddUser(this);">';
697 $html .= PMA_URL_getHiddenInputs('', '');
698 $html .= '<fieldset id="fieldset_add_user_login">'
699 . '<legend>' . __('Add slave replication user') . '</legend>'
700 . PMA_getHtmlForAddUserLoginForm($username_length)
701 . '<div class="item">'
702 . '<label for="select_pred_hostname">'
703 . ' ' . __('Host:')
704 . '</label>'
705 . '<span class="options">'
706 . ' <select name="pred_hostname" id="select_pred_hostname" title="'
707 . __('Host') . '"';
709 $_current_user = $GLOBALS['dbi']->fetchValue('SELECT USER();');
710 if (! empty($_current_user)) {
711 $thishost = str_replace(
712 "'",
714 /*overload*/mb_substr(
715 $_current_user,
716 (/*overload*/mb_strrpos($_current_user, '@') + 1)
719 if ($thishost == 'localhost' || $thishost == '127.0.0.1') {
720 unset($thishost);
723 $html .= ' onchange="if (this.value == \'any\') { hostname.value = \'%\'; } '
724 . 'else if (this.value == \'localhost\') '
725 . '{ hostname.value = \'localhost\'; } '
726 . (empty($thishost)
727 ? ''
728 : 'else if (this.value == \'thishost\') { hostname.value = \''
729 . addslashes(htmlspecialchars($thishost)) . '\'; } ')
730 . 'else if (this.value == \'hosttable\') { hostname.value = \'\'; } '
731 . 'else if (this.value == \'userdefined\') '
732 . '{ hostname.focus(); hostname.select(); }">'
733 . "\n";
734 unset($_current_user);
736 // when we start editing a user, $GLOBALS['pred_hostname'] is not defined
737 if (! isset($GLOBALS['pred_hostname']) && isset($_REQUEST['hostname'])) {
738 switch (/*overload*/mb_strtolower($_REQUEST['hostname'])) {
739 case 'localhost':
740 case '127.0.0.1':
741 $GLOBALS['pred_hostname'] = 'localhost';
742 break;
743 case '%':
744 $GLOBALS['pred_hostname'] = 'any';
745 break;
746 default:
747 $GLOBALS['pred_hostname'] = 'userdefined';
748 break;
751 $html .= ' <option value="any"'
752 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'any')
753 ? ' selected="selected"' : '') . '>' . __('Any host')
754 . '</option>'
755 . ' <option value="localhost"'
756 . ((isset($GLOBALS['pred_hostname'])
757 && $GLOBALS['pred_hostname'] == 'localhost')
758 ? ' selected="selected"' : '') . '>' . __('Local')
759 . '</option>';
761 if (!empty($thishost)) {
762 $html .= ' <option value="thishost"'
763 . ((isset($GLOBALS['pred_hostname'])
764 && $GLOBALS['pred_hostname'] == 'thishost')
765 ? ' selected="selected"' : '') . '>' . __('This Host')
766 . '</option>';
768 unset($thishost);
770 $html .= PMA_getHtmlForTableInfoForm($hostname_length);
771 $html .= '</form>';
772 $html .= '</div>';
774 return $html;
777 * returns html code to add a replication slave user to the master
779 * @param int $username_length Username length
781 * @return String HTML code
783 function PMA_getHtmlForAddUserLoginForm($username_length)
785 $html = '<input type="hidden" name="grant_count" value="25" />'
786 . '<input type="hidden" name="createdb" id="createdb_0" value="0" />'
787 . '<input id="checkbox_Repl_slave_priv" type="hidden"'
788 . ' title="Needed for the replication slaves." '
789 . 'value="Y" name="Repl_slave_priv"/>'
790 . '<input id="checkbox_Repl_client_priv" type="hidden" '
791 . 'title="Needed for the replication slaves."'
792 . ' value="Y" name="Repl_client_priv"/> '
793 . '<input type="hidden" name="sr_take_action" value="true" />'
794 . '<div class="item">'
795 . '<label for="select_pred_username">'
796 . ' ' . __('User name:')
797 . '</label>'
798 . '<span class="options">'
799 . ' <select name="pred_username" id="select_pred_username" '
800 . 'title="' . __('User name') . '"'
801 . ' onchange="if (this.value == \'any\') { username.value = \'\'; } '
802 . 'else if (this.value == \'userdefined\') { '
803 . ' username.focus(); username.select(); }">'
804 . ' <option value="any"'
805 . ((isset($GLOBALS['pred_username'])
806 && $GLOBALS['pred_username'] == 'any') ? ' selected="selected"' : '')
807 . '>' . __('Any user') . '</option>'
808 . ' <option value="userdefined"'
809 . ((! isset($GLOBALS['pred_username'])
810 || $GLOBALS['pred_username'] == 'userdefined')
811 ? ' selected="selected"' : '')
812 . '>' . __('Use text field:') . '</option>'
813 . ' </select>'
814 . '</span>'
815 . '<input type="text" name="username" maxlength="'
816 . $username_length . '" title="' . __('User name') . '"'
817 . (empty($_REQUEST['username']) ? '' : ' value="'
818 . (isset($GLOBALS['new_username'])
819 ? $GLOBALS['new_username']
820 : $_REQUEST['username']) . '"')
821 . ' onchange="pred_username.value = \'userdefined\';" />'
822 . '</div>';
824 return $html;
828 * returns HTML for TableInfoForm
830 * @param int $hostname_length Selected hostname length
832 * @return String HTML code
834 function PMA_getHtmlForTableInfoForm($hostname_length)
836 $html = ' <option value="hosttable"'
837 . ((isset($GLOBALS['pred_hostname'])
838 && $GLOBALS['pred_hostname'] == 'hosttable')
839 ? ' selected="selected"' : '') . '>' . __('Use Host Table')
840 . '</option>'
841 . ' <option value="userdefined"'
842 . ((isset($GLOBALS['pred_hostname'])
843 && $GLOBALS['pred_hostname'] == 'userdefined')
844 ? ' selected="selected"' : '')
845 . '>' . __('Use text field:') . '</option>'
846 . ' </select>'
847 . '</span>'
848 . '<input type="text" name="hostname" maxlength="'
849 . $hostname_length . '" value="'
850 . (isset($_REQUEST['hostname']) ? $_REQUEST['hostname'] : '')
851 . '" title="' . __('Host')
852 . '" onchange="pred_hostname.value = \'userdefined\';" />'
853 . PMA_Util::showHint(
855 'When Host table is used, this field is ignored '
856 . 'and values stored in Host table are used instead.'
859 . '</div>'
860 . '<div class="item">'
861 . '<label for="select_pred_password">'
862 . ' ' . __('Password:')
863 . '</label>'
864 . '<span class="options">'
865 . ' <select name="pred_password" id="select_pred_password" title="'
866 . __('Password') . '"'
867 . ' onchange="if (this.value == \'none\') '
868 . '{ pma_pw.value = \'\'; pma_pw2.value = \'\'; } '
869 . 'else if (this.value == \'userdefined\') '
870 . '{ pma_pw.focus(); pma_pw.select(); }">'
871 . ' <option value="none"';
872 if (isset($_REQUEST['username'])) {
873 $html .= ' selected="selected"';
875 $html .= '>' . __('No Password') . '</option>'
876 . ' <option value="userdefined"'
877 . (isset($_REQUEST['username']) ? '' : ' selected="selected"')
878 . '>' . __('Use text field:') . '</option>'
879 . ' </select>'
880 . '</span>'
881 . '<input type="password" id="text_pma_pw" name="pma_pw" title="'
882 . __('Password') . '" onchange="pred_password.value = \'userdefined\';" />'
883 . '</div>'
884 . '<div class="item">'
885 . '<label for="text_pma_pw2">'
886 . ' ' . __('Re-type:')
887 . '</label>'
888 . '<span class="options">&nbsp;</span>'
889 . '<input type="password" name="pma_pw2" id="text_pma_pw2" title="'
890 . __('Re-type') . '" onchange="pred_password.value = \'userdefined\';" />'
891 . '</div>'
892 . '<div class="item">'
893 . '<label for="button_generate_password">'
894 . ' ' . __('Generate Password:')
895 . '</label>'
896 . '<span class="options">'
897 . ' <input type="button" class="button" '
898 . 'id="button_generate_password" value="' . __('Generate')
899 . '" onclick="suggestPassword(this.form)" />'
900 . '</span>'
901 . '<input type="text" name="generated_pw" id="generated_pw" />'
902 . '</div>'
903 . '</fieldset>';
904 $html .= '<fieldset id="fieldset_user_privtable_footer" class="tblFooters">'
905 . ' <input type="hidden" name="adduser_submit" value="1" />'
906 . ' <input type="submit" id="adduser_submit" value="' . __('Go') . '" />'
907 . '</fieldset>';
908 return $html;
912 * handle control requests
914 * @return NULL
916 function PMA_handleControlRequest()
918 if (isset($_REQUEST['sr_take_action'])) {
919 $refresh = false;
920 $result = null;
921 $messageSuccess = null;
922 $messageError = null;
924 if (isset($_REQUEST['slave_changemaster'])) {
925 $result = PMA_handleRequestForSlaveChangeMaster();
926 } elseif (isset($_REQUEST['sr_slave_server_control'])) {
927 $result = PMA_handleRequestForSlaveServerControl();
928 $refresh = true;
930 switch ($_REQUEST['sr_slave_action']) {
931 case 'start':
932 $messageSuccess = __('Replication started successfully.');
933 $messageError = __('Error starting replication.');
934 break;
935 case 'stop':
936 $messageSuccess = __('Replication stopped successfully.');
937 $messageError = __('Error stopping replication.');
938 break;
939 case 'reset':
940 $messageSuccess = __('Replication resetting successfully.');
941 $messageError = __('Error resetting replication.');
942 break;
943 default:
944 $messageSuccess = __('Success.');
945 $messageError = __('Error.');
946 break;
948 } elseif (isset($_REQUEST['sr_slave_skip_error'])) {
949 $result = PMA_handleRequestForSlaveSkipError();
952 if ($refresh) {
953 $response = PMA_Response::getInstance();
954 if ($response->isAjax()) {
955 $response->isSuccess($result);
956 $response->addJSON(
957 'message',
958 $result
959 ? PMA_Message::success($messageSuccess)
960 : PMA_Message::error($messageError)
962 } else {
963 PMA_sendHeaderLocation(
964 $GLOBALS['cfg']['PmaAbsoluteUri'] . 'server_replication.php'
965 . PMA_URL_getCommon($GLOBALS['url_params'], 'text')
969 unset($refresh);
973 * handle control requests for Slave Change Master
975 * @return boolean
977 function PMA_handleRequestForSlaveChangeMaster()
979 $sr = array();
980 $_SESSION['replication']['m_username'] = $sr['username']
981 = PMA_Util::sqlAddSlashes($_REQUEST['username']);
982 $_SESSION['replication']['m_password'] = $sr['pma_pw']
983 = PMA_Util::sqlAddSlashes($_REQUEST['pma_pw']);
984 $_SESSION['replication']['m_hostname'] = $sr['hostname']
985 = PMA_Util::sqlAddSlashes($_REQUEST['hostname']);
986 $_SESSION['replication']['m_port'] = $sr['port']
987 = PMA_Util::sqlAddSlashes($_REQUEST['text_port']);
988 $_SESSION['replication']['m_correct'] = '';
989 $_SESSION['replication']['sr_action_status'] = 'error';
990 $_SESSION['replication']['sr_action_info'] = __('Unknown error');
992 // Attempt to connect to the new master server
993 $link_to_master = PMA_Replication_connectToMaster(
994 $sr['username'], $sr['pma_pw'], $sr['hostname'], $sr['port']
997 if (! $link_to_master) {
998 $_SESSION['replication']['sr_action_status'] = 'error';
999 $_SESSION['replication']['sr_action_info'] = sprintf(
1000 __('Unable to connect to master %s.'),
1001 htmlspecialchars($sr['hostname'])
1003 } else {
1004 // Read the current master position
1005 $position = PMA_Replication_Slave_binLogMaster($link_to_master);
1007 if (empty($position)) {
1008 $_SESSION['replication']['sr_action_status'] = 'error';
1009 $_SESSION['replication']['sr_action_info']
1010 = __(
1011 'Unable to read master log position. '
1012 . 'Possible privilege problem on master.'
1014 } else {
1015 $_SESSION['replication']['m_correct'] = true;
1017 if (! PMA_Replication_Slave_changeMaster(
1018 $sr['username'],
1019 $sr['pma_pw'],
1020 $sr['hostname'],
1021 $sr['port'],
1022 $position,
1023 true,
1024 false
1027 $_SESSION['replication']['sr_action_status'] = 'error';
1028 $_SESSION['replication']['sr_action_info']
1029 = __('Unable to change master!');
1030 } else {
1031 $_SESSION['replication']['sr_action_status'] = 'success';
1032 $_SESSION['replication']['sr_action_info'] = sprintf(
1033 __('Master server changed successfully to %s.'),
1034 htmlspecialchars($sr['hostname'])
1040 return $_SESSION['replication']['sr_action_status'] === 'success';
1044 * handle control requests for Slave Server Control
1046 * @return boolean
1048 function PMA_handleRequestForSlaveServerControl()
1050 if (empty($_REQUEST['sr_slave_control_parm'])) {
1051 $_REQUEST['sr_slave_control_parm'] = null;
1053 if ($_REQUEST['sr_slave_action'] == 'reset') {
1054 $qStop = PMA_Replication_Slave_control("STOP");
1055 $qReset = $GLOBALS['dbi']->tryQuery("RESET SLAVE;");
1056 $qStart = PMA_Replication_Slave_control("START");
1058 $result = ($qStop !== false && $qStop !== -1 &&
1059 $qReset !== false && $qReset !== -1 &&
1060 $qStart !== false && $qStart !== -1);
1061 } else {
1062 $qControl = PMA_Replication_Slave_control(
1063 $_REQUEST['sr_slave_action'],
1064 $_REQUEST['sr_slave_control_parm']
1067 $result = ($qControl !== false && $qControl !== -1);
1070 return $result;
1074 * handle control requests for Slave Skip Error
1076 * @return boolean
1078 function PMA_handleRequestForSlaveSkipError()
1080 $count = 1;
1081 if (isset($_REQUEST['sr_skip_errors_count'])) {
1082 $count = $_REQUEST['sr_skip_errors_count'] * 1;
1085 $qStop = PMA_Replication_Slave_control("STOP");
1086 $qSkip = $GLOBALS['dbi']->tryQuery(
1087 "SET GLOBAL SQL_SLAVE_SKIP_COUNTER = " . $count . ";"
1089 $qStart = PMA_Replication_Slave_control("START");
1091 $result = ($qStop !== false && $qStop !== -1 &&
1092 $qSkip !== false && $qSkip !== -1 &&
1093 $qStart !== false && $qStart !== -1);
1095 return $result;