Minor improvement to prior commit.
[openemr.git] / phpmyadmin / libraries / replication_gui.lib.php
blob1c8bff96b140cf35811a8533cfc95aa3f63064bd
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="#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 .= 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 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 .= 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="#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' . PMA_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 .= PMA_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 .= PMA_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' . PMA_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['pma']->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 .= PMA_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 $odd_row = true;
550 foreach (${"{$type}_variables"} as $variable) {
551 $html .= ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">';
552 $html .= ' <td class="name">';
553 $html .= $variable;
554 $html .= ' </td>';
555 $html .= ' <td class="value">';
557 // TODO change to regexp or something, to allow for negative match
558 if (isset(${"{$type}_variables_alerts"}[$variable])
559 && ${"{$type}_variables_alerts"}[$variable] == ${"server_{$type}_replication"}[0][$variable]
561 $html .= '<span class="attention">';
563 } elseif (isset(${"{$type}_variables_oks"}[$variable])
564 && ${"{$type}_variables_oks"}[$variable] == ${"server_{$type}_replication"}[0][$variable]
566 $html .= '<span class="allfine">';
567 } else {
568 $html .= '<span>';
570 // allow wrapping long table lists into multiple lines
571 static $variables_wrap = array(
572 'Replicate_Do_DB', 'Replicate_Ignore_DB',
573 'Replicate_Do_Table', 'Replicate_Ignore_Table',
574 'Replicate_Wild_Do_Table', 'Replicate_Wild_Ignore_Table');
575 if (in_array($variable, $variables_wrap)) {
576 $html .= str_replace(
577 ',',
578 ', ',
579 ${"server_{$type}_replication"}[0][$variable]
581 } else {
582 $html .= ${"server_{$type}_replication"}[0][$variable];
584 $html .= '</span>';
586 $html .= ' </td>';
587 $html .= ' </tr>';
589 $odd_row = ! $odd_row;
592 $html .= ' </tbody>';
593 $html .= ' </table>';
594 $html .= ' <br />';
595 $html .= '</div>';
597 return $html;
601 * returns html code for table with slave users connected to this master
603 * @param boolean $hidden - if true, then default style is set to hidden,
604 * - default value false
606 * @return string
608 function PMA_getHtmlForReplicationSlavesTable($hidden = false)
610 $html = '';
611 // Fetch data
612 $data = $GLOBALS['dbi']->fetchResult('SHOW SLAVE HOSTS', null, null);
614 $html .= ' <br />';
615 $html .= ' <div id="replication_slaves_section" style="';
616 $html .= ($hidden ? 'display: none;' : '') . '"> ';
617 $html .= ' <table class="data">';
618 $html .= ' <thead>';
619 $html .= ' <tr>';
620 $html .= ' <th>' . __('Server ID') . '</th>';
621 $html .= ' <th>' . __('Host') . '</th>';
622 $html .= ' </tr>';
623 $html .= ' </thead>';
624 $html .= ' <tbody>';
626 $odd_row = true;
627 foreach ($data as $slave) {
628 $html .= ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">';
629 $html .= ' <td class="value">' . $slave['Server_id'] . '</td>';
630 $html .= ' <td class="value">' . $slave['Host'] . '</td>';
631 $html .= ' </tr>';
633 $odd_row = ! $odd_row;
636 $html .= ' </tbody>';
637 $html .= ' </table>';
638 $html .= ' <br />';
639 $html .= PMA_Message::notice(
641 'Only slaves started with the '
642 . '--report-host=host_name option are visible in this list.'
644 )->getDisplay();
645 $html .= ' <br />';
646 $html .= ' </div>';
648 return $html;
652 * get the correct username and hostname lengths for this MySQL server
654 * @return array username length, hostname length
656 function PMA_replicationGetUsernameHostnameLength()
658 $fields_info = $GLOBALS['dbi']->getColumns('mysql', 'user');
659 $username_length = 16;
660 $hostname_length = 41;
661 foreach ($fields_info as $val) {
662 if ($val['Field'] == 'User') {
663 strtok($val['Type'], '()');
664 $v = strtok('()');
665 if (is_int($v)) {
666 $username_length = $v;
668 } elseif ($val['Field'] == 'Host') {
669 strtok($val['Type'], '()');
670 $v = strtok('()');
671 if (is_int($v)) {
672 $hostname_length = $v;
676 return array($username_length, $hostname_length);
680 * returns html code to add a replication slave user to the master
682 * @return String HTML code
684 function PMA_getHtmlForReplicationMasterAddSlaveuser()
686 $html = '';
687 list($username_length, $hostname_length)
688 = PMA_replicationGetUsernameHostnameLength();
690 if (isset($_REQUEST['username'])
691 && /*overload*/mb_strlen($_REQUEST['username']) === 0
693 $GLOBALS['pred_username'] = 'any';
695 $html .= '<div id="master_addslaveuser_gui">';
696 $html .= '<form autocomplete="off" method="post" ';
697 $html .= 'action="server_privileges.php"';
698 $html .= ' onsubmit="return checkAddUser(this);">';
699 $html .= PMA_URL_getHiddenInputs('', '');
700 $html .= '<fieldset id="fieldset_add_user_login">'
701 . '<legend>' . __('Add slave replication user') . '</legend>'
702 . PMA_getHtmlForAddUserLoginForm($username_length)
703 . '<div class="item">'
704 . '<label for="select_pred_hostname">'
705 . ' ' . __('Host:')
706 . '</label>'
707 . '<span class="options">'
708 . ' <select name="pred_hostname" id="select_pred_hostname" title="'
709 . __('Host') . '"';
711 $_current_user = $GLOBALS['dbi']->fetchValue('SELECT USER();');
712 if (! empty($_current_user)) {
713 $thishost = str_replace(
714 "'",
716 /*overload*/mb_substr(
717 $_current_user,
718 (/*overload*/mb_strrpos($_current_user, '@') + 1)
721 if ($thishost == 'localhost' || $thishost == '127.0.0.1') {
722 unset($thishost);
725 $html .= ' onchange="if (this.value == \'any\') { hostname.value = \'%\'; } '
726 . 'else if (this.value == \'localhost\') '
727 . '{ hostname.value = \'localhost\'; } '
728 . (empty($thishost)
729 ? ''
730 : 'else if (this.value == \'thishost\') { hostname.value = \''
731 . addslashes(htmlspecialchars($thishost)) . '\'; } ')
732 . 'else if (this.value == \'hosttable\') { hostname.value = \'\'; } '
733 . 'else if (this.value == \'userdefined\') '
734 . '{ hostname.focus(); hostname.select(); }">'
735 . "\n";
736 unset($_current_user);
738 // when we start editing a user, $GLOBALS['pred_hostname'] is not defined
739 if (! isset($GLOBALS['pred_hostname']) && isset($_REQUEST['hostname'])) {
740 switch (/*overload*/mb_strtolower($_REQUEST['hostname'])) {
741 case 'localhost':
742 case '127.0.0.1':
743 $GLOBALS['pred_hostname'] = 'localhost';
744 break;
745 case '%':
746 $GLOBALS['pred_hostname'] = 'any';
747 break;
748 default:
749 $GLOBALS['pred_hostname'] = 'userdefined';
750 break;
753 $html .= ' <option value="any"'
754 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'any')
755 ? ' selected="selected"' : '') . '>' . __('Any host')
756 . '</option>'
757 . ' <option value="localhost"'
758 . ((isset($GLOBALS['pred_hostname'])
759 && $GLOBALS['pred_hostname'] == 'localhost')
760 ? ' selected="selected"' : '') . '>' . __('Local')
761 . '</option>';
763 if (!empty($thishost)) {
764 $html .= ' <option value="thishost"'
765 . ((isset($GLOBALS['pred_hostname'])
766 && $GLOBALS['pred_hostname'] == 'thishost')
767 ? ' selected="selected"' : '') . '>' . __('This Host')
768 . '</option>';
770 unset($thishost);
772 $html .= PMA_getHtmlForTableInfoForm($hostname_length);
773 $html .= '</form>';
774 $html .= '</div>';
776 return $html;
779 * returns html code to add a replication slave user to the master
781 * @param int $username_length Username length
783 * @return String HTML code
785 function PMA_getHtmlForAddUserLoginForm($username_length)
787 $html = '<input type="hidden" name="grant_count" value="25" />'
788 . '<input type="hidden" name="createdb" id="createdb_0" value="0" />'
789 . '<input id="checkbox_Repl_slave_priv" type="hidden"'
790 . ' title="Needed for the replication slaves." '
791 . 'value="Y" name="Repl_slave_priv"/>'
792 . '<input id="checkbox_Repl_client_priv" type="hidden" '
793 . 'title="Needed for the replication slaves."'
794 . ' value="Y" name="Repl_client_priv"/> '
795 . '<input type="hidden" name="sr_take_action" value="true" />'
796 . '<div class="item">'
797 . '<label for="select_pred_username">'
798 . ' ' . __('User name:')
799 . '</label>'
800 . '<span class="options">'
801 . ' <select name="pred_username" id="select_pred_username" '
802 . 'title="' . __('User name') . '"'
803 . ' onchange="if (this.value == \'any\') { username.value = \'\'; } '
804 . 'else if (this.value == \'userdefined\') { '
805 . ' username.focus(); username.select(); }">'
806 . ' <option value="any"'
807 . ((isset($GLOBALS['pred_username'])
808 && $GLOBALS['pred_username'] == 'any') ? ' selected="selected"' : '')
809 . '>' . __('Any user') . '</option>'
810 . ' <option value="userdefined"'
811 . ((! isset($GLOBALS['pred_username'])
812 || $GLOBALS['pred_username'] == 'userdefined')
813 ? ' selected="selected"' : '')
814 . '>' . __('Use text field:') . '</option>'
815 . ' </select>'
816 . '</span>'
817 . '<input type="text" name="username" maxlength="'
818 . $username_length . '" title="' . __('User name') . '"'
819 . (empty($_REQUEST['username']) ? '' : ' value="'
820 . (isset($GLOBALS['new_username'])
821 ? $GLOBALS['new_username']
822 : $_REQUEST['username']) . '"')
823 . ' onchange="pred_username.value = \'userdefined\';" />'
824 . '</div>';
826 return $html;
830 * returns HTML for TableInfoForm
832 * @param int $hostname_length Selected hostname length
834 * @return String HTML code
836 function PMA_getHtmlForTableInfoForm($hostname_length)
838 $html = ' <option value="hosttable"'
839 . ((isset($GLOBALS['pred_hostname'])
840 && $GLOBALS['pred_hostname'] == 'hosttable')
841 ? ' selected="selected"' : '') . '>' . __('Use Host Table')
842 . '</option>'
843 . ' <option value="userdefined"'
844 . ((isset($GLOBALS['pred_hostname'])
845 && $GLOBALS['pred_hostname'] == 'userdefined')
846 ? ' selected="selected"' : '')
847 . '>' . __('Use text field:') . '</option>'
848 . ' </select>'
849 . '</span>'
850 . '<input type="text" name="hostname" maxlength="'
851 . $hostname_length . '" value="'
852 . (isset($_REQUEST['hostname']) ? $_REQUEST['hostname'] : '')
853 . '" title="' . __('Host')
854 . '" onchange="pred_hostname.value = \'userdefined\';" />'
855 . PMA_Util::showHint(
857 'When Host table is used, this field is ignored '
858 . 'and values stored in Host table are used instead.'
861 . '</div>'
862 . '<div class="item">'
863 . '<label for="select_pred_password">'
864 . ' ' . __('Password:')
865 . '</label>'
866 . '<span class="options">'
867 . ' <select name="pred_password" id="select_pred_password" title="'
868 . __('Password') . '"'
869 . ' onchange="if (this.value == \'none\') '
870 . '{ pma_pw.value = \'\'; pma_pw2.value = \'\'; } '
871 . 'else if (this.value == \'userdefined\') '
872 . '{ pma_pw.focus(); pma_pw.select(); }">'
873 . ' <option value="none"';
874 if (isset($_REQUEST['username'])) {
875 $html .= ' selected="selected"';
877 $html .= '>' . __('No Password') . '</option>'
878 . ' <option value="userdefined"'
879 . (isset($_REQUEST['username']) ? '' : ' selected="selected"')
880 . '>' . __('Use text field:') . '</option>'
881 . ' </select>'
882 . '</span>'
883 . '<input type="password" id="text_pma_pw" name="pma_pw" title="'
884 . __('Password') . '" onchange="pred_password.value = \'userdefined\';" />'
885 . '</div>'
886 . '<div class="item">'
887 . '<label for="text_pma_pw2">'
888 . ' ' . __('Re-type:')
889 . '</label>'
890 . '<span class="options">&nbsp;</span>'
891 . '<input type="password" name="pma_pw2" id="text_pma_pw2" title="'
892 . __('Re-type') . '" onchange="pred_password.value = \'userdefined\';" />'
893 . '</div>'
894 . '<div class="item">'
895 . '<label for="button_generate_password">'
896 . ' ' . __('Generate password:')
897 . '</label>'
898 . '<span class="options">'
899 . ' <input type="button" class="button" '
900 . 'id="button_generate_password" value="' . __('Generate')
901 . '" onclick="suggestPassword(this.form)" />'
902 . '</span>'
903 . '<input type="text" name="generated_pw" id="generated_pw" />'
904 . '</div>'
905 . '</fieldset>';
906 $html .= '<fieldset id="fieldset_user_privtable_footer" class="tblFooters">'
907 . ' <input type="hidden" name="adduser_submit" value="1" />'
908 . ' <input type="submit" id="adduser_submit" value="' . __('Go') . '" />'
909 . '</fieldset>';
910 return $html;
914 * handle control requests
916 * @return NULL
918 function PMA_handleControlRequest()
920 if (isset($_REQUEST['sr_take_action'])) {
921 $refresh = false;
922 $result = null;
923 $messageSuccess = null;
924 $messageError = null;
926 if (isset($_REQUEST['slave_changemaster'])) {
927 $result = PMA_handleRequestForSlaveChangeMaster();
928 } elseif (isset($_REQUEST['sr_slave_server_control'])) {
929 $result = PMA_handleRequestForSlaveServerControl();
930 $refresh = true;
932 switch ($_REQUEST['sr_slave_action']) {
933 case 'start':
934 $messageSuccess = __('Replication started successfully.');
935 $messageError = __('Error starting replication.');
936 break;
937 case 'stop':
938 $messageSuccess = __('Replication stopped successfully.');
939 $messageError = __('Error stopping replication.');
940 break;
941 case 'reset':
942 $messageSuccess = __('Replication resetting successfully.');
943 $messageError = __('Error resetting replication.');
944 break;
945 default:
946 $messageSuccess = __('Success.');
947 $messageError = __('Error.');
948 break;
950 } elseif (isset($_REQUEST['sr_slave_skip_error'])) {
951 $result = PMA_handleRequestForSlaveSkipError();
954 if ($refresh) {
955 $response = PMA_Response::getInstance();
956 if ($response->isAjax()) {
957 $response->isSuccess($result);
958 $response->addJSON(
959 'message',
960 $result
961 ? PMA_Message::success($messageSuccess)
962 : PMA_Message::error($messageError)
964 } else {
965 PMA_sendHeaderLocation(
966 $GLOBALS['cfg']['PmaAbsoluteUri'] . 'server_replication.php'
967 . PMA_URL_getCommon($GLOBALS['url_params'], 'text')
971 unset($refresh);
975 * handle control requests for Slave Change Master
977 * @return boolean
979 function PMA_handleRequestForSlaveChangeMaster()
981 $sr = array();
982 $_SESSION['replication']['m_username'] = $sr['username']
983 = PMA_Util::sqlAddSlashes($_REQUEST['username']);
984 $_SESSION['replication']['m_password'] = $sr['pma_pw']
985 = PMA_Util::sqlAddSlashes($_REQUEST['pma_pw']);
986 $_SESSION['replication']['m_hostname'] = $sr['hostname']
987 = PMA_Util::sqlAddSlashes($_REQUEST['hostname']);
988 $_SESSION['replication']['m_port'] = $sr['port']
989 = PMA_Util::sqlAddSlashes($_REQUEST['text_port']);
990 $_SESSION['replication']['m_correct'] = '';
991 $_SESSION['replication']['sr_action_status'] = 'error';
992 $_SESSION['replication']['sr_action_info'] = __('Unknown error');
994 // Attempt to connect to the new master server
995 $link_to_master = PMA_Replication_connectToMaster(
996 $sr['username'], $sr['pma_pw'], $sr['hostname'], $sr['port']
999 if (! $link_to_master) {
1000 $_SESSION['replication']['sr_action_status'] = 'error';
1001 $_SESSION['replication']['sr_action_info'] = sprintf(
1002 __('Unable to connect to master %s.'),
1003 htmlspecialchars($sr['hostname'])
1005 } else {
1006 // Read the current master position
1007 $position = PMA_Replication_Slave_binLogMaster($link_to_master);
1009 if (empty($position)) {
1010 $_SESSION['replication']['sr_action_status'] = 'error';
1011 $_SESSION['replication']['sr_action_info']
1012 = __(
1013 'Unable to read master log position. '
1014 . 'Possible privilege problem on master.'
1016 } else {
1017 $_SESSION['replication']['m_correct'] = true;
1019 if (! PMA_Replication_Slave_changeMaster(
1020 $sr['username'],
1021 $sr['pma_pw'],
1022 $sr['hostname'],
1023 $sr['port'],
1024 $position,
1025 true,
1026 false
1029 $_SESSION['replication']['sr_action_status'] = 'error';
1030 $_SESSION['replication']['sr_action_info']
1031 = __('Unable to change master!');
1032 } else {
1033 $_SESSION['replication']['sr_action_status'] = 'success';
1034 $_SESSION['replication']['sr_action_info'] = sprintf(
1035 __('Master server changed successfully to %s.'),
1036 htmlspecialchars($sr['hostname'])
1042 return $_SESSION['replication']['sr_action_status'] === 'success';
1046 * handle control requests for Slave Server Control
1048 * @return boolean
1050 function PMA_handleRequestForSlaveServerControl()
1052 if (empty($_REQUEST['sr_slave_control_parm'])) {
1053 $_REQUEST['sr_slave_control_parm'] = null;
1055 if ($_REQUEST['sr_slave_action'] == 'reset') {
1056 $qStop = PMA_Replication_Slave_control("STOP");
1057 $qReset = $GLOBALS['dbi']->tryQuery("RESET SLAVE;");
1058 $qStart = PMA_Replication_Slave_control("START");
1060 $result = ($qStop !== false && $qStop !== -1 &&
1061 $qReset !== false && $qReset !== -1 &&
1062 $qStart !== false && $qStart !== -1);
1063 } else {
1064 $qControl = PMA_Replication_Slave_control(
1065 $_REQUEST['sr_slave_action'],
1066 $_REQUEST['sr_slave_control_parm']
1069 $result = ($qControl !== false && $qControl !== -1);
1072 return $result;
1076 * handle control requests for Slave Skip Error
1078 * @return boolean
1080 function PMA_handleRequestForSlaveSkipError()
1082 $count = 1;
1083 if (isset($_REQUEST['sr_skip_errors_count'])) {
1084 $count = $_REQUEST['sr_skip_errors_count'] * 1;
1087 $qStop = PMA_Replication_Slave_control("STOP");
1088 $qSkip = $GLOBALS['dbi']->tryQuery(
1089 "SET GLOBAL SQL_SLAVE_SKIP_COUNTER = " . $count . ";"
1091 $qStart = PMA_Replication_Slave_control("START");
1093 $result = ($qStop !== false && $qStop !== -1 &&
1094 $qSkip !== false && $qSkip !== -1 &&
1095 $qStart !== false && $qStart !== -1);
1097 return $result;