Translated using Weblate (Slovenian)
[phpmyadmin.git] / libraries / replication_gui.lib.php
blob1a7da8988654b2f9083ce5b8a502b8b707d49008
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Functions for the replication GUI
6 * @package PhpMyAdmin
7 */
8 use PMA\libraries\Message;
9 use PMA\libraries\Response;
10 use PMA\libraries\URL;
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 .= 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 .= 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="#master_status_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="#master_slaves_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 .= 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 a master server in a '
91 . 'replication process. You can choose from either replicating '
92 . 'all databases and ignoring some of them (useful if you want to '
93 . 'replicate a majority of the databases) or you can choose to ignore '
94 . 'all databases by default and allow only certain databases to be '
95 . 'replicated. 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 .= 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 .= 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 . 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 . 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 . URL::getCommon($_url_params);
204 $_url_params['sr_slave_action'] = 'reset';
205 $slave_control_reset_link = 'server_replication.php'
206 . 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 . URL::getCommon($_url_params);
214 if ($server_slave_replication[0]['Slave_SQL_Running'] == 'No') {
215 $html .= Message::error(
216 __('Slave SQL Thread not running!')
217 )->getDisplay();
219 if ($server_slave_replication[0]['Slave_IO_Running'] == 'No') {
220 $html .= 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 . 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="#slave_status_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="#slave_control_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 class="ajax" id="reset_slave"'
253 . ' href="' . $slave_control_reset_link . '">';
254 $html .= __('Reset slave') . '</a></li>';
255 if ($server_slave_replication[0]['Slave_SQL_Running'] == 'No') {
256 $html .= ' <li><a href="' . $slave_control_sql_link . '">';
257 $html .= __('Start SQL Thread only') . '</a></li>';
258 } else {
259 $html .= ' <li><a href="' . $slave_control_sql_link . '">';
260 $html .= __('Stop SQL Thread only') . '</a></li>';
262 if ($server_slave_replication[0]['Slave_IO_Running'] == 'No') {
263 $html .= ' <li><a href="' . $slave_control_io_link . '">';
264 $html .= __('Start IO Thread only') . '</a></li>';
265 } else {
266 $html .= ' <li><a href="' . $slave_control_io_link . '">';
267 $html .= __('Stop IO Thread only') . '</a></li>';
269 $html .= ' </ul>';
270 $html .= ' </div>';
271 $html .= ' </li>';
272 $html .= ' <li>';
273 $html .= PMA_getHtmlForSlaveErrorManagement($slave_skip_error_link);
274 $html .= ' </li>';
275 $html .= ' <li><a href="' . $reconfiguremaster_link . '">';
276 $html .= __('Change or reconfigure master server') . '</a></li>';
277 $html .= '</ul>';
278 $html .= '</div>';
280 } elseif (! isset($_REQUEST['sl_configure'])) {
281 $_url_params = $GLOBALS['url_params'];
282 $_url_params['sl_configure'] = true;
283 $_url_params['repl_clear_scr'] = true;
285 $html .= sprintf(
287 'This server is not configured as slave in a replication process. '
288 . 'Would you like to <a href="%s">configure</a> it?'
290 'server_replication.php' . URL::getCommon($_url_params)
293 $html .= '</fieldset>';
295 return $html;
299 * returns HTML for Slave Error Management
301 * @param String $slave_skip_error_link error link
303 * @return String HTML code
305 function PMA_getHtmlForSlaveErrorManagement($slave_skip_error_link)
307 $html = '<a href="#slave_errormanagement_href" '
308 . 'id="slave_errormanagement_href">';
309 $html .= __('Error management:') . '</a>';
310 $html .= ' <div id="slave_errormanagement_gui" style="display: none">';
311 $html .= Message::error(
312 __('Skipping errors might lead into unsynchronized master and slave!')
313 )->getDisplay();
314 $html .= ' <ul>';
315 $html .= ' <li><a href="' . $slave_skip_error_link . '">';
316 $html .= __('Skip current error') . '</a></li>';
317 $html .= ' <li>';
318 $html .= ' <form method="post" action="server_replication.php">';
319 $html .= URL::getHiddenInputs('', '');
320 $html .= sprintf(
321 __('Skip next %s errors.'),
322 '<input type="text" name="sr_skip_errors_count" value="1" '
323 . 'style="width: 30px" />'
325 $html .= ' <input type="submit" name="sr_slave_skip_error" ';
326 $html .= 'value="' . __('Go') . '" />';
327 $html .= ' <input type="hidden" name="sr_take_action" value="1" />';
328 $html .= ' </form></li>';
329 $html .= ' </ul>';
330 $html .= ' </div>';
331 return $html;
335 * returns HTML for not configure for a server replication
337 * @return String HTML code
339 function PMA_getHtmlForNotServerReplication()
341 $_url_params = $GLOBALS['url_params'];
342 $_url_params['mr_configure'] = true;
344 $html = '<fieldset>';
345 $html .= '<legend>' . __('Master replication') . '</legend>';
346 $html .= sprintf(
348 'This server is not configured as master in a replication process. '
349 . 'Would you like to <a href="%s">configure</a> it?'
351 'server_replication.php' . URL::getCommon($_url_params)
353 $html .= '</fieldset>';
354 return $html;
358 * returns HTML code for selecting databases
360 * @return String HTML code
362 function PMA_getHtmlForReplicationDbMultibox()
364 $multi_values = '';
365 $multi_values .= '<select name="db_select[]" '
366 . 'size="6" multiple="multiple" id="db_select">';
368 foreach ($GLOBALS['dblist']->databases as $current_db) {
369 if ($GLOBALS['dbi']->isSystemSchema($current_db)) {
370 continue;
372 $current_db = htmlspecialchars($current_db);
373 $multi_values .= ' <option value="' . $current_db . '" ';
374 $multi_values .= '>';
375 $multi_values .= $current_db . '</option>';
376 } // end while
378 $multi_values .= '</select><br />';
379 $multi_values .= '<a href="#" id="db_select_href">' . __('Select all') . '</a>';
380 $multi_values .= '&nbsp;/&nbsp;';
381 $multi_values .= '<a href="#" id="db_reset_href">' . __('Unselect all') . '</a>';
383 return $multi_values;
387 * returns HTML for changing master
389 * @param String $submitname - submit button name
391 * @return String HTML code
393 function PMA_getHtmlForReplicationChangeMaster($submitname)
395 $html = '';
396 list($username_length, $hostname_length)
397 = PMA_replicationGetUsernameHostnameLength();
399 $html .= '<form method="post" action="server_replication.php">';
400 $html .= URL::getHiddenInputs('', '');
401 $html .= ' <fieldset id="fieldset_add_user_login">';
402 $html .= ' <legend>' . __('Slave configuration');
403 $html .= ' - ' . __('Change or reconfigure master server') . '</legend>';
404 $html .= __(
405 'Make sure, you have unique server-id in your configuration file (my.cnf). '
406 . 'If not, please add the following line into [mysqld] section:'
408 $html .= '<br />';
409 $html .= '<pre>server-id=' . time() . '</pre>';
411 $html .= PMA_getHtmlForAddUserInputDiv(
412 array('text'=>__('User name:'), 'for'=>"text_username"),
413 array(
414 'type'=>'text',
415 'name'=>'username',
416 'id'=>'text_username',
417 'maxlength'=>$username_length,
418 'title'=>__('User name'),
419 'required'=>'required'
423 $html .= PMA_getHtmlForAddUserInputDiv(
424 array('text'=>__('Password:'), 'for'=>"text_pma_pw"),
425 array(
426 'type'=>'password',
427 'name'=>'pma_pw',
428 'id'=>'text_pma_pw',
429 'title'=>__('Password'),
430 'required'=>'required'
434 $html .= PMA_getHtmlForAddUserInputDiv(
435 array('text'=>__('Host:'), 'for'=>"text_hostname"),
436 array(
437 'type'=>'text',
438 'name'=>'hostname',
439 'id'=>'text_hostname',
440 'maxlength'=>$hostname_length,
441 'value'=>'',
442 'required'=>'required'
446 $html .= PMA_getHtmlForAddUserInputDiv(
447 array('text'=>__('Port:'), 'for'=>"text_port"),
448 array(
449 'type'=>'number',
450 'name'=>'text_port',
451 'id'=>'text_port',
452 'maxlength'=>6,
453 'value'=>'3306',
454 'required'=>'required'
458 $html .= ' </fieldset>';
459 $html .= ' <fieldset id="fieldset_user_privtable_footer" class="tblFooters">';
460 $html .= ' <input type="hidden" name="sr_take_action" value="true" />';
461 $html .= ' <input type="hidden" name="' . $submitname . '" value="1" />';
462 $html .= ' <input type="submit" id="confslave_submit" value="';
463 $html .= __('Go') . '" />';
464 $html .= ' </fieldset>';
465 $html .= '</form>';
467 return $html;
471 * returns HTML code for Add user input div
473 * @param array $label_array label tag elements
474 * @param array $input_array input tag elements
476 * @return String HTML code
478 function PMA_getHtmlForAddUserInputDiv($label_array, $input_array)
480 $html = ' <div class="item">';
481 $html .= ' <label for="' . $label_array['for'] . '">';
482 $html .= $label_array['text'] . '</label>';
484 $html .= ' <input ';
485 foreach ($input_array as $key=>$value) {
486 $html .= ' ' . $key . '="' . $value . '" ';
488 $html .= ' />';
489 $html .= ' </div>';
490 return $html;
494 * This function returns html code for table with replication status.
496 * @param string $type either master or slave
497 * @param boolean $hidden if true, then default style is set to hidden,
498 * default value false
499 * @param boolean $title if true, then title is displayed, default true
501 * @return String HTML code
503 function PMA_getHtmlForReplicationStatusTable($type, $hidden = false, $title = true)
505 global ${"{$type}_variables"};
506 global ${"{$type}_variables_alerts"};
507 global ${"{$type}_variables_oks"};
508 global ${"server_{$type}_replication"};
509 global ${"strReplicationStatus_{$type}"};
511 $html = '';
513 // TODO check the Masters server id?
514 // seems to default to '1' when queried via SHOW VARIABLES ,
515 // but resulted in error on the master when slave connects
516 // [ERROR] Error reading packet from server: Misconfigured master
517 // - server id was not set ( server_errno=1236)
518 // [ERROR] Got fatal error 1236: 'Misconfigured master
519 // - server id was not set' from master when reading data from binary log
521 //$server_id = $GLOBALS['dbi']->fetchValue(
522 // "SHOW VARIABLES LIKE 'server_id'", 0, 1
523 //);
525 $html .= '<div id="replication_' . $type . '_section" style="';
526 $html .= ($hidden ? 'display: none;' : '') . '"> ';
528 if ($title) {
529 if ($type == 'master') {
530 $html .= '<h4><a name="replication_' . $type . '"></a>';
531 $html .= __('Master status') . '</h4>';
532 } else {
533 $html .= '<h4><a name="replication_' . $type . '"></a>';
534 $html .= __('Slave status') . '</h4>';
536 } else {
537 $html .= '<br />';
540 $html .= ' <table id="server' . $type . 'replicationsummary" class="data"> ';
541 $html .= ' <thead>';
542 $html .= ' <tr>';
543 $html .= ' <th>' . __('Variable') . '</th>';
544 $html .= ' <th>' . __('Value') . '</th>';
545 $html .= ' </tr>';
546 $html .= ' </thead>';
547 $html .= ' <tbody>';
549 foreach (${"{$type}_variables"} as $variable) {
550 $html .= ' <tr>';
551 $html .= ' <td class="name">';
552 $html .= htmlspecialchars($variable);
553 $html .= ' </td>';
554 $html .= ' <td class="value">';
556 // TODO change to regexp or something, to allow for negative match
557 if (isset(${"{$type}_variables_alerts"}[$variable])
558 && ${"{$type}_variables_alerts"}[$variable] == ${"server_{$type}_replication"}[0][$variable]
560 $html .= '<span class="attention">';
562 } elseif (isset(${"{$type}_variables_oks"}[$variable])
563 && ${"{$type}_variables_oks"}[$variable] == ${"server_{$type}_replication"}[0][$variable]
565 $html .= '<span class="allfine">';
566 } else {
567 $html .= '<span>';
569 // allow wrapping long table lists into multiple lines
570 static $variables_wrap = array(
571 'Replicate_Do_DB', 'Replicate_Ignore_DB',
572 'Replicate_Do_Table', 'Replicate_Ignore_Table',
573 'Replicate_Wild_Do_Table', 'Replicate_Wild_Ignore_Table');
574 if (in_array($variable, $variables_wrap)) {
575 $html .= htmlspecialchars(str_replace(
576 ',',
577 ', ',
578 ${"server_{$type}_replication"}[0][$variable]
580 } else {
581 $html .= htmlspecialchars(${"server_{$type}_replication"}[0][$variable]);
583 $html .= '</span>';
585 $html .= ' </td>';
586 $html .= ' </tr>';
589 $html .= ' </tbody>';
590 $html .= ' </table>';
591 $html .= ' <br />';
592 $html .= '</div>';
594 return $html;
598 * returns html code for table with slave users connected to this master
600 * @param boolean $hidden - if true, then default style is set to hidden,
601 * - default value false
603 * @return string
605 function PMA_getHtmlForReplicationSlavesTable($hidden = false)
607 $html = '';
608 // Fetch data
609 $data = $GLOBALS['dbi']->fetchResult('SHOW SLAVE HOSTS', null, null);
611 $html .= ' <br />';
612 $html .= ' <div id="replication_slaves_section" style="';
613 $html .= ($hidden ? 'display: none;' : '') . '"> ';
614 $html .= ' <table class="data">';
615 $html .= ' <thead>';
616 $html .= ' <tr>';
617 $html .= ' <th>' . __('Server ID') . '</th>';
618 $html .= ' <th>' . __('Host') . '</th>';
619 $html .= ' </tr>';
620 $html .= ' </thead>';
621 $html .= ' <tbody>';
623 foreach ($data as $slave) {
624 $html .= ' <tr>';
625 $html .= ' <td class="value">' . $slave['Server_id'] . '</td>';
626 $html .= ' <td class="value">' . $slave['Host'] . '</td>';
627 $html .= ' </tr>';
630 $html .= ' </tbody>';
631 $html .= ' </table>';
632 $html .= ' <br />';
633 $html .= Message::notice(
635 'Only slaves started with the '
636 . '--report-host=host_name option are visible in this list.'
638 )->getDisplay();
639 $html .= ' <br />';
640 $html .= ' </div>';
642 return $html;
646 * get the correct username and hostname lengths for this MySQL server
648 * @return array username length, hostname length
650 function PMA_replicationGetUsernameHostnameLength()
652 $fields_info = $GLOBALS['dbi']->getColumns('mysql', 'user');
653 $username_length = 16;
654 $hostname_length = 41;
655 foreach ($fields_info as $val) {
656 if ($val['Field'] == 'User') {
657 strtok($val['Type'], '()');
658 $v = strtok('()');
659 if (is_int($v)) {
660 $username_length = $v;
662 } elseif ($val['Field'] == 'Host') {
663 strtok($val['Type'], '()');
664 $v = strtok('()');
665 if (is_int($v)) {
666 $hostname_length = $v;
670 return array($username_length, $hostname_length);
674 * returns html code to add a replication slave user to the master
676 * @return String HTML code
678 function PMA_getHtmlForReplicationMasterAddSlaveuser()
680 $html = '';
681 list($username_length, $hostname_length)
682 = PMA_replicationGetUsernameHostnameLength();
684 if (isset($_REQUEST['username']) && strlen($_REQUEST['username']) === 0) {
685 $GLOBALS['pred_username'] = 'any';
687 $html .= '<div id="master_addslaveuser_gui">';
688 $html .= '<form autocomplete="off" method="post" ';
689 $html .= 'action="server_privileges.php"';
690 $html .= ' onsubmit="return checkAddUser(this);">';
691 $html .= URL::getHiddenInputs('', '');
692 $html .= '<fieldset id="fieldset_add_user_login">'
693 . '<legend>' . __('Add slave replication user') . '</legend>'
694 . PMA_getHtmlForAddUserLoginForm($username_length)
695 . '<div class="item">'
696 . '<label for="select_pred_hostname">'
697 . ' ' . __('Host:')
698 . '</label>'
699 . '<span class="options">'
700 . ' <select name="pred_hostname" id="select_pred_hostname" title="'
701 . __('Host') . '"';
703 $_current_user = $GLOBALS['dbi']->fetchValue('SELECT USER();');
704 if (! empty($_current_user)) {
705 $thishost = str_replace(
706 "'",
708 mb_substr(
709 $_current_user,
710 (mb_strrpos($_current_user, '@') + 1)
713 if ($thishost != 'localhost' && $thishost != '127.0.0.1') {
714 $html .= ' data-thishost="' . htmlspecialchars($thishost) . '" ';
715 } else {
716 unset($thishost);
719 $html .= '>' . "\n";
720 unset($_current_user);
722 // when we start editing a user, $GLOBALS['pred_hostname'] is not defined
723 if (! isset($GLOBALS['pred_hostname']) && isset($_REQUEST['hostname'])) {
724 switch (mb_strtolower($_REQUEST['hostname'])) {
725 case 'localhost':
726 case '127.0.0.1':
727 $GLOBALS['pred_hostname'] = 'localhost';
728 break;
729 case '%':
730 $GLOBALS['pred_hostname'] = 'any';
731 break;
732 default:
733 $GLOBALS['pred_hostname'] = 'userdefined';
734 break;
737 $html .= ' <option value="any"'
738 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'any')
739 ? ' selected="selected"' : '') . '>' . __('Any host')
740 . '</option>'
741 . ' <option value="localhost"'
742 . ((isset($GLOBALS['pred_hostname'])
743 && $GLOBALS['pred_hostname'] == 'localhost')
744 ? ' selected="selected"' : '') . '>' . __('Local')
745 . '</option>';
747 if (!empty($thishost)) {
748 $html .= ' <option value="thishost"'
749 . ((isset($GLOBALS['pred_hostname'])
750 && $GLOBALS['pred_hostname'] == 'thishost')
751 ? ' selected="selected"' : '') . '>' . __('This Host')
752 . '</option>';
754 unset($thishost);
756 $html .= PMA_getHtmlForTableInfoForm($hostname_length);
757 $html .= '</form>';
758 $html .= '</div>';
760 return $html;
763 * returns html code to add a replication slave user to the master
765 * @param int $username_length Username length
767 * @return String HTML code
769 function PMA_getHtmlForAddUserLoginForm($username_length)
771 $html = '<input type="hidden" name="grant_count" value="25" />'
772 . '<input type="hidden" name="createdb" id="createdb_0" value="0" />'
773 . '<input id="checkbox_Repl_slave_priv" type="hidden"'
774 . ' title="Needed for the replication slaves." '
775 . 'value="Y" name="Repl_slave_priv"/>'
776 . '<input id="checkbox_Repl_client_priv" type="hidden" '
777 . 'title="Needed for the replication slaves."'
778 . ' value="Y" name="Repl_client_priv"/> '
779 . '<input type="hidden" name="sr_take_action" value="true" />'
780 . '<div class="item">'
781 . '<label for="select_pred_username">'
782 . ' ' . __('User name:')
783 . '</label>'
784 . '<span class="options">'
785 . ' <select name="pred_username" id="select_pred_username" '
786 . 'title="' . __('User name') . '">'
787 . ' <option value="any"'
788 . ((isset($GLOBALS['pred_username'])
789 && $GLOBALS['pred_username'] == 'any') ? ' selected="selected"' : '')
790 . '>' . __('Any user') . '</option>'
791 . ' <option value="userdefined"'
792 . ((! isset($GLOBALS['pred_username'])
793 || $GLOBALS['pred_username'] == 'userdefined')
794 ? ' selected="selected"' : '')
795 . '>' . __('Use text field:') . '</option>'
796 . ' </select>'
797 . '</span>'
798 . '<input type="text" name="username" id="pma_username" maxlength="'
799 . $username_length . '" title="' . __('User name') . '"'
800 . (empty($_REQUEST['username']) ? '' : ' value="'
801 . (isset($GLOBALS['new_username'])
802 ? $GLOBALS['new_username']
803 : htmlspecialchars($_REQUEST['username'])) . '"')
804 . ' />'
805 . '</div>';
807 return $html;
811 * returns HTML for TableInfoForm
813 * @param int $hostname_length Selected hostname length
815 * @return String HTML code
817 function PMA_getHtmlForTableInfoForm($hostname_length)
819 $html = ' <option value="hosttable"'
820 . ((isset($GLOBALS['pred_hostname'])
821 && $GLOBALS['pred_hostname'] == 'hosttable')
822 ? ' selected="selected"' : '') . '>' . __('Use Host Table')
823 . '</option>'
824 . ' <option value="userdefined"'
825 . ((isset($GLOBALS['pred_hostname'])
826 && $GLOBALS['pred_hostname'] == 'userdefined')
827 ? ' selected="selected"' : '')
828 . '>' . __('Use text field:') . '</option>'
829 . ' </select>'
830 . '</span>'
831 . '<input type="text" name="hostname" id="pma_hostname" maxlength="'
832 . $hostname_length . '" value="'
833 . (isset($_REQUEST['hostname']) ? htmlspecialchars($_REQUEST['hostname']) : '')
834 . '" title="' . __('Host')
835 . '" />'
836 . PMA\libraries\Util::showHint(
838 'When Host table is used, this field is ignored '
839 . 'and values stored in Host table are used instead.'
842 . '</div>'
843 . '<div class="item">'
844 . '<label for="select_pred_password">'
845 . ' ' . __('Password:')
846 . '</label>'
847 . '<span class="options">'
848 . ' <select name="pred_password" id="select_pred_password" title="'
849 . __('Password') . '">'
850 . ' <option value="none"';
851 if (isset($_REQUEST['username'])) {
852 $html .= ' selected="selected"';
854 $html .= '>' . __('No Password') . '</option>'
855 . ' <option value="userdefined"'
856 . (isset($_REQUEST['username']) ? '' : ' selected="selected"')
857 . '>' . __('Use text field:') . '</option>'
858 . ' </select>'
859 . '</span>'
860 . '<input type="password" id="text_pma_pw" name="pma_pw" title="'
861 . __('Password') . '" />'
862 . '</div>'
863 . '<div class="item">'
864 . '<label for="text_pma_pw2">'
865 . ' ' . __('Re-type:')
866 . '</label>'
867 . '<span class="options">&nbsp;</span>'
868 . '<input type="password" name="pma_pw2" id="text_pma_pw2" title="'
869 . __('Re-type') . '" />'
870 . '</div>'
871 . '<div class="item">'
872 . '<label for="button_generate_password">'
873 . ' ' . __('Generate password:')
874 . '</label>'
875 . '<span class="options">'
876 . ' <input type="button" class="button" '
877 . 'id="button_generate_password" value="' . __('Generate')
878 . '" onclick="suggestPassword(this.form)" />'
879 . '</span>'
880 . '<input type="text" name="generated_pw" id="generated_pw" />'
881 . '</div>'
882 . '</fieldset>';
883 $html .= '<fieldset id="fieldset_user_privtable_footer" class="tblFooters">'
884 . ' <input type="hidden" name="adduser_submit" value="1" />'
885 . ' <input type="submit" id="adduser_submit" value="' . __('Go') . '" />'
886 . '</fieldset>';
887 return $html;
891 * handle control requests
893 * @return NULL
895 function PMA_handleControlRequest()
897 if (isset($_REQUEST['sr_take_action'])) {
898 $refresh = false;
899 $result = false;
900 $messageSuccess = null;
901 $messageError = null;
903 if (isset($_REQUEST['slave_changemaster'])) {
904 $result = PMA_handleRequestForSlaveChangeMaster();
905 } elseif (isset($_REQUEST['sr_slave_server_control'])) {
906 $result = PMA_handleRequestForSlaveServerControl();
907 $refresh = true;
909 switch ($_REQUEST['sr_slave_action']) {
910 case 'start':
911 $messageSuccess = __('Replication started successfully.');
912 $messageError = __('Error starting replication.');
913 break;
914 case 'stop':
915 $messageSuccess = __('Replication stopped successfully.');
916 $messageError = __('Error stopping replication.');
917 break;
918 case 'reset':
919 $messageSuccess = __('Replication resetting successfully.');
920 $messageError = __('Error resetting replication.');
921 break;
922 default:
923 $messageSuccess = __('Success.');
924 $messageError = __('Error.');
925 break;
927 } elseif (isset($_REQUEST['sr_slave_skip_error'])) {
928 $result = PMA_handleRequestForSlaveSkipError();
931 if ($refresh) {
932 $response = Response::getInstance();
933 if ($response->isAjax()) {
934 $response->setRequestStatus($result);
935 $response->addJSON(
936 'message',
937 $result
938 ? Message::success($messageSuccess)
939 : Message::error($messageError)
941 } else {
942 PMA_sendHeaderLocation(
943 './server_replication.php'
944 . URL::getCommonRaw($GLOBALS['url_params'])
948 unset($refresh);
952 * handle control requests for Slave Change Master
954 * @return boolean
956 function PMA_handleRequestForSlaveChangeMaster()
958 $sr = array();
959 $_SESSION['replication']['m_username'] = $sr['username']
960 = $GLOBALS['dbi']->escapeString($_REQUEST['username']);
961 $_SESSION['replication']['m_password'] = $sr['pma_pw']
962 = $GLOBALS['dbi']->escapeString($_REQUEST['pma_pw']);
963 $_SESSION['replication']['m_hostname'] = $sr['hostname']
964 = $GLOBALS['dbi']->escapeString($_REQUEST['hostname']);
965 $_SESSION['replication']['m_port'] = $sr['port']
966 = $GLOBALS['dbi']->escapeString($_REQUEST['text_port']);
967 $_SESSION['replication']['m_correct'] = '';
968 $_SESSION['replication']['sr_action_status'] = 'error';
969 $_SESSION['replication']['sr_action_info'] = __('Unknown error');
971 // Attempt to connect to the new master server
972 $link_to_master = PMA_Replication_connectToMaster(
973 $sr['username'], $sr['pma_pw'], $sr['hostname'], $sr['port']
976 if (! $link_to_master) {
977 $_SESSION['replication']['sr_action_status'] = 'error';
978 $_SESSION['replication']['sr_action_info'] = sprintf(
979 __('Unable to connect to master %s.'),
980 htmlspecialchars($sr['hostname'])
982 } else {
983 // Read the current master position
984 $position = PMA_Replication_Slave_binLogMaster($link_to_master);
986 if (empty($position)) {
987 $_SESSION['replication']['sr_action_status'] = 'error';
988 $_SESSION['replication']['sr_action_info']
989 = __(
990 'Unable to read master log position. '
991 . 'Possible privilege problem on master.'
993 } else {
994 $_SESSION['replication']['m_correct'] = true;
996 if (! PMA_Replication_Slave_changeMaster(
997 $sr['username'],
998 $sr['pma_pw'],
999 $sr['hostname'],
1000 $sr['port'],
1001 $position,
1002 true,
1003 false
1006 $_SESSION['replication']['sr_action_status'] = 'error';
1007 $_SESSION['replication']['sr_action_info']
1008 = __('Unable to change master!');
1009 } else {
1010 $_SESSION['replication']['sr_action_status'] = 'success';
1011 $_SESSION['replication']['sr_action_info'] = sprintf(
1012 __('Master server changed successfully to %s.'),
1013 htmlspecialchars($sr['hostname'])
1019 return $_SESSION['replication']['sr_action_status'] === 'success';
1023 * handle control requests for Slave Server Control
1025 * @return boolean
1027 function PMA_handleRequestForSlaveServerControl()
1029 if (empty($_REQUEST['sr_slave_control_parm'])) {
1030 $_REQUEST['sr_slave_control_parm'] = null;
1032 if ($_REQUEST['sr_slave_action'] == 'reset') {
1033 $qStop = PMA_Replication_Slave_control("STOP");
1034 $qReset = $GLOBALS['dbi']->tryQuery("RESET SLAVE;");
1035 $qStart = PMA_Replication_Slave_control("START");
1037 $result = ($qStop !== false && $qStop !== -1 &&
1038 $qReset !== false && $qReset !== -1 &&
1039 $qStart !== false && $qStart !== -1);
1040 } else {
1041 $qControl = PMA_Replication_Slave_control(
1042 $_REQUEST['sr_slave_action'],
1043 $_REQUEST['sr_slave_control_parm']
1046 $result = ($qControl !== false && $qControl !== -1);
1049 return $result;
1053 * handle control requests for Slave Skip Error
1055 * @return boolean
1057 function PMA_handleRequestForSlaveSkipError()
1059 $count = 1;
1060 if (isset($_REQUEST['sr_skip_errors_count'])) {
1061 $count = $_REQUEST['sr_skip_errors_count'] * 1;
1064 $qStop = PMA_Replication_Slave_control("STOP");
1065 $qSkip = $GLOBALS['dbi']->tryQuery(
1066 "SET GLOBAL SQL_SLAVE_SKIP_COUNTER = " . $count . ";"
1068 $qStart = PMA_Replication_Slave_control("START");
1070 $result = ($qStop !== false && $qStop !== -1 &&
1071 $qSkip !== false && $qSkip !== -1 &&
1072 $qStart !== false && $qStart !== -1);
1074 return $result;