ReplicationGui.php 41.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
 * Functions for the replication GUI
 *
 * @package PhpMyAdmin
 */
namespace PhpMyAdmin;

use PhpMyAdmin\Core;
use PhpMyAdmin\Message;
use PhpMyAdmin\Replication;
use PhpMyAdmin\Response;
use PhpMyAdmin\Url;
use PhpMyAdmin\Util;

/**
 * PhpMyAdmin\ReplicationGui class
 *
 * @package PhpMyAdmin
 */
class ReplicationGui
{
    /**
     * returns HTML for error message
     *
     * @return String HTML code
     */
    public static function getHtmlForErrorMessage()
    {
        $html = '';
        if (isset($_SESSION['replication']['sr_action_status'])
            && isset($_SESSION['replication']['sr_action_info'])
        ) {
            if ($_SESSION['replication']['sr_action_status'] == 'error') {
                $error_message = $_SESSION['replication']['sr_action_info'];
                $html .= Message::error($error_message)->getDisplay();
                $_SESSION['replication']['sr_action_status'] = 'unknown';
            } elseif ($_SESSION['replication']['sr_action_status'] == 'success') {
                $success_message = $_SESSION['replication']['sr_action_info'];
                $html .= Message::success($success_message)->getDisplay();
                $_SESSION['replication']['sr_action_status'] = 'unknown';
            }
        }
        return $html;
    }

    /**
     * returns HTML for master replication
     *
     * @return String HTML code
     */
    public static function getHtmlForMasterReplication()
    {
        $html = '';
        if (! isset($_POST['repl_clear_scr'])) {
            $html .= '<fieldset>';
            $html .= '<legend>' . __('Master replication') . '</legend>';
            $html .= __('This server is configured as master in a replication process.');
            $html .= '<ul>';
            $html .= '  <li><a href="#master_status_href" id="master_status_href">';
            $html .= __('Show master status') . '</a>';
            $html .= self::getHtmlForReplicationStatusTable('master', true, false);
            $html .= '  </li>';

            $html .= '  <li><a href="#master_slaves_href" id="master_slaves_href">';
            $html .= __('Show connected slaves') . '</a>';
            $html .= self::getHtmlForReplicationSlavesTable(true);
            $html .= '  </li>';

            $_url_params = $GLOBALS['url_params'];
            $_url_params['mr_adduser'] = true;
            $_url_params['repl_clear_scr'] = true;

            $html .= '  <li><a href="server_replication.php" data-post="';
            $html .= Url::getCommon($_url_params, '')
                . '" id="master_addslaveuser_href">';
            $html .= __('Add slave replication user') . '</a></li>';
        }

        // Display 'Add replication slave user' form
        if (isset($_POST['mr_adduser'])) {
            $html .= self::getHtmlForReplicationMasterAddSlaveUser();
        } elseif (! isset($_POST['repl_clear_scr'])) {
            $html .= "</ul>";
            $html .= "</fieldset>";
        }

        return $html;
    }

    /**
     * returns HTML for master replication configuration
     *
     * @return String HTML code
     */
    public static function getHtmlForMasterConfiguration()
    {
        $html  = '<fieldset>';
        $html .= '<legend>' . __('Master configuration') . '</legend>';
        $html .= __(
            'This server is not configured as a master server in a '
            . 'replication process. You can choose from either replicating '
            . 'all databases and ignoring some of them (useful if you want to '
            . 'replicate a majority of the databases) or you can choose to ignore '
            . 'all databases by default and allow only certain databases to be '
            . 'replicated. Please select the mode:'
        ) . '<br /><br />';

        $html .= '<select name="db_type" id="db_type">';
        $html .= '<option value="all">' . __('Replicate all databases; Ignore:');
        $html .= '</option>';
        $html .= '<option value="ign">' . __('Ignore all databases; Replicate:');
        $html .= '</option>';
        $html .= '</select>';
        $html .= '<br /><br />';
        $html .= __('Please select databases:') . '<br />';
        $html .= self::getHtmlForReplicationDbMultibox();
        $html .= '<br /><br />';
        $html .= __(
            'Now, add the following lines at the end of [mysqld] section'
            . ' in your my.cnf and please restart the MySQL server afterwards.'
        ) . '<br />';
        $html .= '<pre id="rep"></pre>';
        $html .= __(
            'Once you restarted MySQL server, please click on Go button. '
            . 'Afterwards, you should see a message informing you, that this server'
            . ' <b>is</b> configured as master.'
        );
        $html .= '</fieldset>';
        $html .= '<fieldset class="tblFooters">';
        $html .= ' <form method="post" action="server_replication.php" >';
        $html .= Url::getHiddenInputs('', '');
        $html .= '  <input type="submit" value="' . __('Go') . '" id="goButton" />';
        $html .= ' </form>';
        $html .= '</fieldset>';

        return $html;
    }

    /**
     * returns HTML for slave replication configuration
     *
     * @param bool  $server_slave_status      Whether it is Master or Slave
     * @param array $server_slave_replication Slave replication
     *
     * @return String HTML code
     */
    public static function getHtmlForSlaveConfiguration(
        $server_slave_status, array $server_slave_replication
    ) {
        $html  = '<fieldset>';
        $html .= '<legend>' . __('Slave replication') . '</legend>';
        /**
         * check for multi-master replication functionality
         */
        $server_slave_multi_replication = $GLOBALS['dbi']->fetchResult(
            'SHOW ALL SLAVES STATUS'
        );
        if ($server_slave_multi_replication) {
            $html .= __('Master connection:');
            $html .= '<form method="get" action="server_replication.php">';
            $html .= Url::getHiddenInputs($GLOBALS['url_params']);
            $html .= ' <select name="master_connection">';
            $html .= '<option value="">' . __('Default') . '</option>';
            foreach ($server_slave_multi_replication as $server) {
                $html .= '<option' . (isset($_POST['master_connection'])
                    && $_POST['master_connection'] == $server['Connection_name'] ?
                        ' selected="selected"' : '') . '>' . $server['Connection_name']
                    . '</option>';
            }
            $html .= '</select>';
            $html .= ' <input type="submit" value="' . __('Go') . '" id="goButton" />';
            $html .= '</form>';
            $html .= '<br /><br />';
        }
        if ($server_slave_status) {
            $html .= '<div id="slave_configuration_gui">';

            $_url_params = $GLOBALS['url_params'];
            $_url_params['sr_take_action'] = true;
            $_url_params['sr_slave_server_control'] = true;

            if ($server_slave_replication[0]['Slave_IO_Running'] == 'No') {
                $_url_params['sr_slave_action'] = 'start';
            } else {
                $_url_params['sr_slave_action'] = 'stop';
            }

            $_url_params['sr_slave_control_parm'] = 'IO_THREAD';
            $slave_control_io_link = Url::getCommon($_url_params, '');

            if ($server_slave_replication[0]['Slave_SQL_Running'] == 'No') {
                $_url_params['sr_slave_action'] = 'start';
            } else {
                $_url_params['sr_slave_action'] = 'stop';
            }

            $_url_params['sr_slave_control_parm'] = 'SQL_THREAD';
            $slave_control_sql_link = Url::getCommon($_url_params, '');

            if ($server_slave_replication[0]['Slave_IO_Running'] == 'No'
                || $server_slave_replication[0]['Slave_SQL_Running'] == 'No'
            ) {
                $_url_params['sr_slave_action'] = 'start';
            } else {
                $_url_params['sr_slave_action'] = 'stop';
            }

            $_url_params['sr_slave_control_parm'] = null;
            $slave_control_full_link = Url::getCommon($_url_params, '');

            $_url_params['sr_slave_action'] = 'reset';
            $slave_control_reset_link = Url::getCommon($_url_params, '');

            $_url_params = $GLOBALS['url_params'];
            $_url_params['sr_take_action'] = true;
            $_url_params['sr_slave_skip_error'] = true;
            $slave_skip_error_link = Url::getCommon($_url_params, '');

            if ($server_slave_replication[0]['Slave_SQL_Running'] == 'No') {
                $html .= Message::error(
                    __('Slave SQL Thread not running!')
                )->getDisplay();
            }
            if ($server_slave_replication[0]['Slave_IO_Running'] == 'No') {
                $html .= Message::error(
                    __('Slave IO Thread not running!')
                )->getDisplay();
            }

            $_url_params = $GLOBALS['url_params'];
            $_url_params['sl_configure'] = true;
            $_url_params['repl_clear_scr'] = true;

            $reconfiguremaster_link =  Url::getCommon($_url_params, '');

            $html .= __(
                'Server is configured as slave in a replication process. Would you ' .
                'like to:'
            );
            $html .= '<br />';
            $html .= '<ul>';
            $html .= ' <li><a href="#slave_status_href" id="slave_status_href">';
            $html .= __('See slave status table') . '</a>';
            $html .= self::getHtmlForReplicationStatusTable('slave', true, false);
            $html .= ' </li>';

            $html .= ' <li><a href="#slave_control_href" id="slave_control_href">';
            $html .= __('Control slave:') . '</a>';
            $html .= ' <div id="slave_control_gui" class="hide">';
            $html .= '  <ul>';
            $html .= '   <li><a href="server_replication.php" data-post="' . $slave_control_full_link . '">';
            $html .= (($server_slave_replication[0]['Slave_IO_Running'] == 'No' ||
                       $server_slave_replication[0]['Slave_SQL_Running'] == 'No')
                     ? __('Full start')
                     : __('Full stop')) . ' </a></li>';
            $html .= '   <li><a class="ajax" id="reset_slave"'
                . ' href="server_replication.php" data-post="' . $slave_control_reset_link . '">';
            $html .= __('Reset slave') . '</a></li>';
            if ($server_slave_replication[0]['Slave_SQL_Running'] == 'No') {
                $html .= '   <li><a href="server_replication.php" data-post="' . $slave_control_sql_link . '">';
                $html .= __('Start SQL Thread only') . '</a></li>';
            } else {
                $html .= '   <li><a href="server_replication.php" data-post="' . $slave_control_sql_link . '">';
                $html .= __('Stop SQL Thread only') . '</a></li>';
            }
            if ($server_slave_replication[0]['Slave_IO_Running'] == 'No') {
                $html .= '   <li><a href="server_replication.php" data-post="' . $slave_control_io_link . '">';
                $html .= __('Start IO Thread only') . '</a></li>';
            } else {
                $html .= '   <li><a href="server_replication.php" data-post="' . $slave_control_io_link . '">';
                $html .= __('Stop IO Thread only') . '</a></li>';
            }
            $html .= '  </ul>';
            $html .= ' </div>';
            $html .= ' </li>';
            $html .= ' <li>';
            $html .= self::getHtmlForSlaveErrorManagement($slave_skip_error_link);
            $html .= ' </li>';
            $html .= ' <li><a href="server_replication.php" data-post="' . $reconfiguremaster_link . '">';
            $html .=  __('Change or reconfigure master server') . '</a></li>';
            $html .= '</ul>';
            $html .= '</div>';

        } elseif (! isset($_POST['sl_configure'])) {
            $_url_params = $GLOBALS['url_params'];
            $_url_params['sl_configure'] = true;
            $_url_params['repl_clear_scr'] = true;

            $html .= sprintf(
                __(
                    'This server is not configured as slave in a replication process. '
                    . 'Would you like to %sconfigure%s it?'
                ),
                '<a href="server_replication.php" data-post="' . Url::getCommon($_url_params, '') . '">',
                '</a>'
            );
        }
        $html .= '</fieldset>';

        return $html;
    }

    /**
     * returns HTML for Slave Error Management
     *
     * @param String $slave_skip_error_link error link
     *
     * @return String HTML code
     */
    public static function getHtmlForSlaveErrorManagement($slave_skip_error_link)
    {
        $html  = '<a href="#slave_errormanagement_href" '
            . 'id="slave_errormanagement_href">';
        $html .= __('Error management:') . '</a>';
        $html .= ' <div id="slave_errormanagement_gui" class="hide">';
        $html .= Message::error(
            __('Skipping errors might lead into unsynchronized master and slave!')
        )->getDisplay();
        $html .= '  <ul>';
        $html .= '   <li><a href="server_replication.php" data-post="' . $slave_skip_error_link . '">';
        $html .= __('Skip current error') . '</a></li>';
        $html .= '   <li>';
        $html .= '    <form method="post" action="server_replication.php">';
        $html .= Url::getHiddenInputs('', '');
        $html .= sprintf(
            __('Skip next %s errors.'),
            '<input type="text" name="sr_skip_errors_count" value="1" '
            . 'class = "repl_gui_skip_err_cnt" />'
        );
        $html .= '              <input type="submit" name="sr_slave_skip_error" ';
        $html .= 'value="' . __('Go') . '" />';
        $html .= '      <input type="hidden" name="sr_take_action" value="1" />';
        $html .= '    </form></li>';
        $html .= '  </ul>';
        $html .= ' </div>';
        return $html;
    }

    /**
     * returns HTML for not configure for a server replication
     *
     * @return String HTML code
     */
    public static function getHtmlForNotServerReplication()
    {
        $_url_params = $GLOBALS['url_params'];
        $_url_params['mr_configure'] = true;

        $html  = '<fieldset>';
        $html .= '<legend>' . __('Master replication') . '</legend>';
        $html .= sprintf(
            __(
                'This server is not configured as master in a replication process. '
                . 'Would you like to %sconfigure%s it?'
            ),
            '<a href="server_replication.php" data-post="' . Url::getCommon($_url_params, '') . '">',
            '</a>'
        );
        $html .= '</fieldset>';
        return $html;
    }

    /**
     * returns HTML code for selecting databases
     *
     * @return String HTML code
     */
    public static function getHtmlForReplicationDbMultibox()
    {
        $multi_values = '';
        $multi_values .= '<select name="db_select[]" '
            . 'size="6" multiple="multiple" id="db_select" class="width96">';

        foreach ($GLOBALS['dblist']->databases as $current_db) {
            if ($GLOBALS['dbi']->isSystemSchema($current_db)) {
                continue;
            }
            $current_db = htmlspecialchars($current_db);
            $multi_values .= '                <option value="' . $current_db . '" ';
            $multi_values .= '>';
            $multi_values .= $current_db . '</option>';
        } // end while

        $multi_values .= '</select><br />';
        $multi_values .= '<a href="#" id="db_select_href">' . __('Select all') . '</a>';
        $multi_values .= '&nbsp;/&nbsp;';
        $multi_values .= '<a href="#" id="db_reset_href">' . __('Unselect all') . '</a>';

        return $multi_values;
    }

    /**
     * returns HTML for changing master
     *
     * @param String $submitname - submit button name
     *
     * @return String HTML code
     */
    public static function getHtmlForReplicationChangeMaster($submitname)
    {
        $html = '';
        list($username_length, $hostname_length)
            = self::getUsernameHostnameLength();

        $html .= '<form method="post" action="server_replication.php">';
        $html .= Url::getHiddenInputs('', '');
        $html .= ' <fieldset id="fieldset_add_user_login">';
        $html .= '  <legend>' . __('Slave configuration');
        $html .= ' - ' . __('Change or reconfigure master server') . '</legend>';
        $html .= __(
            'Make sure you have a unique server-id in your configuration file (my.cnf). '
            . 'If not, please add the following line into [mysqld] section:'
        );
        $html .= '<br />';
        $html .= '<pre>server-id=' . time() . '</pre>';

        $html .= self::getHtmlForAddUserInputDiv(
            array('text'=>__('User name:'), 'for'=>"text_username"),
            array(
                'type'=>'text',
                'name'=>'username',
                'id'=>'text_username',
                'maxlength'=>$username_length,
                'title'=>__('User name'),
                'required'=>'required'
            )
        );

        $html .= self::getHtmlForAddUserInputDiv(
            array('text'=>__('Password:'), 'for'=>"text_pma_pw"),
            array(
                'type'=>'password',
                'name'=>'pma_pw',
                'id'=>'text_pma_pw',
                'title'=>__('Password'),
                'required'=>'required'
            )
        );

        $html .= self::getHtmlForAddUserInputDiv(
            array('text'=>__('Host:'), 'for'=>"text_hostname"),
            array(
                'type'=>'text',
                'name'=>'hostname',
                'id'=>'text_hostname',
                'maxlength'=>$hostname_length,
                'value'=>'',
                'required'=>'required'
            )
        );

        $html .= self::getHtmlForAddUserInputDiv(
            array('text'=>__('Port:'), 'for'=>"text_port"),
            array(
                'type'=>'number',
                'name'=>'text_port',
                'id'=>'text_port',
                'maxlength'=>6,
                'value'=>'3306',
                'required'=>'required'
             )
        );

        $html .= ' </fieldset>';
        $html .= ' <fieldset id="fieldset_user_privtable_footer" class="tblFooters">';
        $html .= '    <input type="hidden" name="sr_take_action" value="true" />';
        $html .= '     <input type="hidden" name="' . $submitname . '" value="1" />';
        $html .= '     <input type="submit" id="confslave_submit" value="';
        $html .= __('Go') . '" />';
        $html .= ' </fieldset>';
        $html .= '</form>';

        return $html;
    }

    /**
     * returns HTML code for Add user input div
     *
     * @param array $label_array label tag elements
     * @param array $input_array input tag elements
     *
     * @return String HTML code
     */
    public static function getHtmlForAddUserInputDiv(array $label_array, array $input_array)
    {
        $html  = '  <div class="item">';
        $html .= '     <label for="' . $label_array['for'] . '">';
        $html .=  $label_array['text'] . '</label>';

        $html .= '    <input ';
        foreach ($input_array as $key=>$value) {
            $html .= ' ' . $key . '="' . $value . '" ';
        }
        $html .= ' />';
        $html .= '  </div>';
        return $html;
    }

    /**
     * This function returns html code for table with replication status.
     *
     * @param string  $type   either master or slave
     * @param boolean $hidden if true, then default style is set to hidden,
     *                        default value false
     * @param boolean $title  if true, then title is displayed, default true
     *
     * @return String HTML code
     */
    public static function getHtmlForReplicationStatusTable($type, $hidden = false, $title = true)
    {
        global ${"{$type}_variables"};
        global ${"{$type}_variables_alerts"};
        global ${"{$type}_variables_oks"};
        global ${"server_{$type}_replication"};
        global ${"strReplicationStatus_{$type}"};

        $html = '';

        // TODO check the Masters server id?
        // seems to default to '1' when queried via SHOW VARIABLES ,
        // but resulted in error on the master when slave connects
        // [ERROR] Error reading packet from server: Misconfigured master
        // - server id was not set ( server_errno=1236)
        // [ERROR] Got fatal error 1236: 'Misconfigured master
        // - server id was not set' from master when reading data from binary log
        //
        //$server_id = $GLOBALS['dbi']->fetchValue(
        //    "SHOW VARIABLES LIKE 'server_id'", 0, 1
        //);

        $html .= '<div id="replication_' . $type . '_section" style="';
        $html .= ($hidden ? 'display: none;' : '') . '"> ';

        if ($title) {
            if ($type == 'master') {
                $html .= '<h4><a name="replication_' . $type . '"></a>';
                $html .= __('Master status') . '</h4>';
            } else {
                $html .= '<h4><a name="replication_' . $type . '"></a>';
                $html .= __('Slave status') . '</h4>';
            }
        } else {
            $html .= '<br />';
        }

        $html .= '   <table id="server' . $type . 'replicationsummary" class="data"> ';
        $html .= '   <thead>';
        $html .= '    <tr>';
        $html .= '     <th>' . __('Variable') . '</th>';
        $html .= '        <th>' . __('Value') . '</th>';
        $html .= '    </tr>';
        $html .= '   </thead>';
        $html .= '   <tbody>';

        foreach (${"{$type}_variables"} as $variable) {
            $html .= '   <tr>';
            $html .= '     <td class="name">';
            $html .= htmlspecialchars($variable);
            $html .= '     </td>';
            $html .= '     <td class="value">';

            // TODO change to regexp or something, to allow for negative match
            if (isset(${"{$type}_variables_alerts"}[$variable])
                && ${"{$type}_variables_alerts"}[$variable] == ${"server_{$type}_replication"}[0][$variable]
            ) {
                $html .= '<span class="attention">';

            } elseif (isset(${"{$type}_variables_oks"}[$variable])
                && ${"{$type}_variables_oks"}[$variable] == ${"server_{$type}_replication"}[0][$variable]
            ) {
                $html .= '<span class="allfine">';
            } else {
                $html .= '<span>';
            }
            // allow wrapping long table lists into multiple lines
            static $variables_wrap = array(
                'Replicate_Do_DB', 'Replicate_Ignore_DB',
                'Replicate_Do_Table', 'Replicate_Ignore_Table',
                'Replicate_Wild_Do_Table', 'Replicate_Wild_Ignore_Table');
            if (in_array($variable, $variables_wrap)) {
                $html .= htmlspecialchars(str_replace(
                    ',',
                    ', ',
                    ${"server_{$type}_replication"}[0][$variable]
                ));
            } else {
                $html .= htmlspecialchars(${"server_{$type}_replication"}[0][$variable]);
            }
            $html .= '</span>';

            $html .= '  </td>';
            $html .= ' </tr>';
        }

        $html .= '   </tbody>';
        $html .= ' </table>';
        $html .= ' <br />';
        $html .= '</div>';

        return $html;
    }

    /**
     * returns html code for table with slave users connected to this master
     *
     * @param boolean $hidden - if true, then default style is set to hidden,
     *                        - default value false
     *
     * @return string
     */
    public static function getHtmlForReplicationSlavesTable($hidden = false)
    {
        $html = '';
        // Fetch data
        $data = $GLOBALS['dbi']->fetchResult('SHOW SLAVE HOSTS', null, null);

        $html .= '  <br />';
        $html .= '  <div id="replication_slaves_section" style="';
        $html .=  ($hidden ? 'display: none;' : '') . '"> ';
        $html .= '    <table class="data">';
        $html .= '    <thead>';
        $html .= '      <tr>';
        $html .= '        <th>' . __('Server ID') . '</th>';
        $html .= '        <th>' . __('Host') . '</th>';
        $html .= '      </tr>';
        $html .= '    </thead>';
        $html .= '    <tbody>';

        foreach ($data as $slave) {
            $html .= '    <tr>';
            $html .= '      <td class="value">' . $slave['Server_id'] . '</td>';
            $html .= '      <td class="value">' . $slave['Host'] . '</td>';
            $html .= '    </tr>';
        }

        $html .= '    </tbody>';
        $html .= '    </table>';
        $html .= '    <br />';
        $html .= Message::notice(
            __(
                'Only slaves started with the '
                . '--report-host=host_name option are visible in this list.'
            )
        )->getDisplay();
        $html .= '    <br />';
        $html .= '  </div>';

        return $html;
    }

    /**
     * get the correct username and hostname lengths for this MySQL server
     *
     * @return array   username length, hostname length
     */
    public static function getUsernameHostnameLength()
    {
        $fields_info = $GLOBALS['dbi']->getColumns('mysql', 'user');
        $username_length = 16;
        $hostname_length = 41;
        foreach ($fields_info as $val) {
            if ($val['Field'] == 'User') {
                strtok($val['Type'], '()');
                $v = strtok('()');
                if (is_int($v)) {
                    $username_length = $v;
                }
            } elseif ($val['Field'] == 'Host') {
                strtok($val['Type'], '()');
                $v = strtok('()');
                if (is_int($v)) {
                    $hostname_length = $v;
                }
            }
        }
        return array($username_length, $hostname_length);
    }

    /**
     * returns html code to add a replication slave user to the master
     *
     * @return String HTML code
     */
    public static function getHtmlForReplicationMasterAddSlaveUser()
    {
        $html = '';
        list($username_length, $hostname_length)
            = self::getUsernameHostnameLength();

        if (isset($_POST['username']) && strlen($_POST['username']) === 0) {
            $GLOBALS['pred_username'] = 'any';
        }
        $html .= '<div id="master_addslaveuser_gui">';
        $html .= '<form autocomplete="off" method="post" ';
        $html .= 'action="server_privileges.php"';
        $html .= ' onsubmit="return checkAddUser(this);">';
        $html .= Url::getHiddenInputs('', '');
        $html .= '<fieldset id="fieldset_add_user_login">'
            . '<legend>' . __('Add slave replication user') . '</legend>'
            . self::getHtmlForAddUserLoginForm($username_length)
            . '<div class="item">'
            . '<label for="select_pred_hostname">'
            . '    ' . __('Host:')
            . '</label>'
            . '<span class="options">'
            . '    <select name="pred_hostname" id="select_pred_hostname" title="'
            . __('Host') . '"';

        $_current_user = $GLOBALS['dbi']->fetchValue('SELECT USER();');
        if (! empty($_current_user)) {
            $thishost = str_replace(
                "'",
                '',
                mb_substr(
                    $_current_user,
                    (mb_strrpos($_current_user, '@') + 1)
                )
            );
            if ($thishost != 'localhost' && $thishost != '127.0.0.1') {
                $html .= ' data-thishost="' . htmlspecialchars($thishost) . '" ';
            } else {
                unset($thishost);
            }
        }
        $html .= '>' . "\n";
        unset($_current_user);

        // when we start editing a user, $GLOBALS['pred_hostname'] is not defined
        if (! isset($GLOBALS['pred_hostname']) && isset($_POST['hostname'])) {
            switch (mb_strtolower($_POST['hostname'])) {
            case 'localhost':
            case '127.0.0.1':
                $GLOBALS['pred_hostname'] = 'localhost';
                break;
            case '%':
                $GLOBALS['pred_hostname'] = 'any';
                break;
            default:
                $GLOBALS['pred_hostname'] = 'userdefined';
                break;
            }
        }
        $html .= '        <option value="any"'
            . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'any')
            ? ' selected="selected"' : '') . '>' . __('Any host')
            . '</option>'
            . '        <option value="localhost"'
            . ((isset($GLOBALS['pred_hostname'])
                && $GLOBALS['pred_hostname'] == 'localhost')
            ? ' selected="selected"' : '') . '>' . __('Local')
            . '</option>';

        if (!empty($thishost)) {
            $html .= '        <option value="thishost"'
                . ((isset($GLOBALS['pred_hostname'])
                    && $GLOBALS['pred_hostname'] == 'thishost')
                ? ' selected="selected"' : '') . '>' . __('This Host')
                . '</option>';
        }
        unset($thishost);

        $html .= self::getHtmlForTableInfoForm($hostname_length);
        $html .= '</form>';
        $html .= '</div>';

        return $html;
    }

    /**
     *  returns html code to add a replication slave user to the master
     *
     * @param int $username_length Username length
     *
     * @return String HTML code
     */
    public static function getHtmlForAddUserLoginForm($username_length)
    {
        $html = '<input type="hidden" name="grant_count" value="25" />'
            . '<input type="hidden" name="createdb" id="createdb_0" value="0" />'
            . '<input id="checkbox_Repl_slave_priv" type="hidden"'
            . ' title="Needed for the replication slaves." '
            . 'value="Y" name="Repl_slave_priv"/>'
            . '<input id="checkbox_Repl_client_priv" type="hidden" '
            . 'title="Needed for the replication slaves."'
            . ' value="Y" name="Repl_client_priv"/> '
            . '<input type="hidden" name="sr_take_action" value="true" />'
            . '<div class="item">'
            . '<label for="select_pred_username">'
            . '    ' . __('User name:')
            . '</label>'
            . '<span class="options">'
            . '    <select name="pred_username" id="select_pred_username" '
            .         'title="' . __('User name') . '">'
            . '        <option value="any"'
            . ((isset($GLOBALS['pred_username'])
                && $GLOBALS['pred_username'] == 'any') ? ' selected="selected"' : '')
            . '>' . __('Any user') . '</option>'
            . '        <option value="userdefined"'
            . ((! isset($GLOBALS['pred_username'])
                || $GLOBALS['pred_username'] == 'userdefined')
                ? ' selected="selected"' : '')
            . '>' . __('Use text field:') . '</option>'
            . '    </select>'
            . '</span>'
            . '<input type="text" name="username" id="pma_username" maxlength="'
            . $username_length . '" title="' . __('User name') . '"'
            . (empty($_POST['username']) ? '' : ' value="'
            . (isset($GLOBALS['new_username'])
                ? $GLOBALS['new_username']
                : htmlspecialchars($_POST['username'])) . '"')
            . ' />'
            . '</div>';

        return $html;
    }

    /**
     * returns HTML for TableInfoForm
     *
     * @param int $hostname_length Selected hostname length
     *
     * @return String HTML code
     */
    public static function getHtmlForTableInfoForm($hostname_length)
    {
        $html = '        <option value="hosttable"'
            . ((isset($GLOBALS['pred_hostname'])
                && $GLOBALS['pred_hostname'] == 'hosttable')
            ? ' selected="selected"' : '') . '>' . __('Use Host Table')
            . '</option>'
            . '        <option value="userdefined"'
            . ((isset($GLOBALS['pred_hostname'])
                && $GLOBALS['pred_hostname'] == 'userdefined')
            ? ' selected="selected"' : '')
            . '>' . __('Use text field:') . '</option>'
            . '    </select>'
            . '</span>'
            . '<input type="text" name="hostname" id="pma_hostname" maxlength="'
            . $hostname_length . '" value="'
            . (isset($_POST['hostname']) ? htmlspecialchars($_POST['hostname']) : '')
            . '" title="' . __('Host')
            . '" />'
            . Util::showHint(
                __(
                    'When Host table is used, this field is ignored '
                    . 'and values stored in Host table are used instead.'
                )
            )
            . '</div>'
            . '<div class="item">'
            . '<label for="select_pred_password">'
            . '    ' . __('Password:')
            . '</label>'
            . '<span class="options">'
            . '    <select name="pred_password" id="select_pred_password" title="'
            . __('Password') . '">'
            . '        <option value="none"';
        if (isset($_POST['username'])) {
            $html .= '  selected="selected"';
        }
        $html .= '>' . __('No Password') . '</option>'
            . '        <option value="userdefined"'
            . (isset($_POST['username']) ? '' : ' selected="selected"')
            . '>' . __('Use text field:') . '</option>'
            . '    </select>'
            . '</span>'
            . '<input type="password" id="text_pma_pw" name="pma_pw" title="'
            . __('Password') . '" />'
            . '</div>'
            . '<div class="item">'
            . '<label for="text_pma_pw2">'
            . '    ' . __('Re-type:')
            . '</label>'
            . '<span class="options">&nbsp;</span>'
            . '<input type="password" name="pma_pw2" id="text_pma_pw2" title="'
            . __('Re-type') . '" />'
            . '</div>'
            . '<div class="item">'
            . '<label for="button_generate_password">'
            . '    ' . __('Generate password:')
            . '</label>'
            . '<span class="options">'
            . '    <input type="button" class="button" '
            . 'id="button_generate_password" value="' . __('Generate')
            . '" onclick="suggestPassword(this.form)" />'
            . '</span>'
            . '<input type="text" name="generated_pw" id="generated_pw" />'
            . '</div>'
            . '</fieldset>';
        $html .= '<fieldset id="fieldset_user_privtable_footer" class="tblFooters">'
            . '    <input type="hidden" name="adduser_submit" value="1" />'
            . '    <input type="submit" id="adduser_submit" value="' . __('Go') . '" />'
            . '</fieldset>';
        return $html;
    }

    /**
     * handle control requests
     *
     * @return NULL
     */
    public static function handleControlRequest()
    {
        if (isset($_POST['sr_take_action'])) {
            $refresh = false;
            $result = false;
            $messageSuccess = null;
            $messageError = null;

            if (isset($_POST['slave_changemaster']) && ! $GLOBALS['cfg']['AllowArbitraryServer']) {
                $_SESSION['replication']['sr_action_status'] = 'error';
                $_SESSION['replication']['sr_action_info'] = __('Connection to server is disabled, please enable $cfg[\'AllowArbitraryServer\'] in phpMyAdmin configuration.');
            } elseif (isset($_POST['slave_changemaster'])) {
                $result = self::handleRequestForSlaveChangeMaster();
            } elseif (isset($_POST['sr_slave_server_control'])) {
                $result = self::handleRequestForSlaveServerControl();
                $refresh = true;

                switch ($_POST['sr_slave_action']) {
                case 'start':
                    $messageSuccess = __('Replication started successfully.');
                    $messageError = __('Error starting replication.');
                    break;
                case 'stop':
                    $messageSuccess = __('Replication stopped successfully.');
                    $messageError = __('Error stopping replication.');
                    break;
                case 'reset':
                    $messageSuccess = __('Replication resetting successfully.');
                    $messageError = __('Error resetting replication.');
                    break;
                default:
                    $messageSuccess = __('Success.');
                    $messageError = __('Error.');
                    break;
                }
            } elseif (isset($_POST['sr_slave_skip_error'])) {
                $result = self::handleRequestForSlaveSkipError();
            }

            if ($refresh) {
                $response = Response::getInstance();
                if ($response->isAjax()) {
                    $response->setRequestStatus($result);
                    $response->addJSON(
                        'message',
                        $result
                        ? Message::success($messageSuccess)
                        : Message::error($messageError)
                    );
                } else {
                    Core::sendHeaderLocation(
                        './server_replication.php'
                        . Url::getCommonRaw($GLOBALS['url_params'])
                    );
                }
            }
            unset($refresh);
        }
    }

    /**
     * handle control requests for Slave Change Master
     *
     * @return boolean
     */
    public static function handleRequestForSlaveChangeMaster()
    {
        $sr = array();
        $_SESSION['replication']['m_username'] = $sr['username']
            = $GLOBALS['dbi']->escapeString($_POST['username']);
        $_SESSION['replication']['m_password'] = $sr['pma_pw']
            = $GLOBALS['dbi']->escapeString($_POST['pma_pw']);
        $_SESSION['replication']['m_hostname'] = $sr['hostname']
            = $GLOBALS['dbi']->escapeString($_POST['hostname']);
        $_SESSION['replication']['m_port']     = $sr['port']
            = $GLOBALS['dbi']->escapeString($_POST['text_port']);
        $_SESSION['replication']['m_correct']  = '';
        $_SESSION['replication']['sr_action_status'] = 'error';
        $_SESSION['replication']['sr_action_info'] = __('Unknown error');

        // Attempt to connect to the new master server
        $link_to_master = Replication::connectToMaster(
            $sr['username'], $sr['pma_pw'], $sr['hostname'], $sr['port']
        );

        if (! $link_to_master) {
            $_SESSION['replication']['sr_action_status'] = 'error';
            $_SESSION['replication']['sr_action_info'] = sprintf(
                __('Unable to connect to master %s.'),
                htmlspecialchars($sr['hostname'])
            );
        } else {
            // Read the current master position
            $position = Replication::slaveBinLogMaster($link_to_master);

            if (empty($position)) {
                $_SESSION['replication']['sr_action_status'] = 'error';
                $_SESSION['replication']['sr_action_info']
                    = __(
                        'Unable to read master log position. '
                        . 'Possible privilege problem on master.'
                    );
            } else {
                $_SESSION['replication']['m_correct']  = true;

                if (! Replication::slaveChangeMaster(
                    $sr['username'],
                    $sr['pma_pw'],
                    $sr['hostname'],
                    $sr['port'],
                    $position,
                    true,
                    false
                )
                ) {
                    $_SESSION['replication']['sr_action_status'] = 'error';
                    $_SESSION['replication']['sr_action_info']
                        = __('Unable to change master!');
                } else {
                    $_SESSION['replication']['sr_action_status'] = 'success';
                    $_SESSION['replication']['sr_action_info'] = sprintf(
                        __('Master server changed successfully to %s.'),
                        htmlspecialchars($sr['hostname'])
                    );
                }
            }
        }

        return $_SESSION['replication']['sr_action_status'] === 'success';
    }

    /**
     * handle control requests for Slave Server Control
     *
     * @return boolean
     */
    public static function handleRequestForSlaveServerControl()
    {
        if (empty($_POST['sr_slave_control_parm'])) {
            $_POST['sr_slave_control_parm'] = null;
        }
        if ($_POST['sr_slave_action'] == 'reset') {
            $qStop = Replication::slaveControl("STOP");
            $qReset = $GLOBALS['dbi']->tryQuery("RESET SLAVE;");
            $qStart = Replication::slaveControl("START");

            $result = ($qStop !== false && $qStop !== -1 &&
                $qReset !== false && $qReset !== -1 &&
                $qStart !== false && $qStart !== -1);
        } else {
            $qControl = Replication::slaveControl(
                $_POST['sr_slave_action'],
                $_POST['sr_slave_control_parm']
            );

            $result = ($qControl !== false && $qControl !== -1);
        }

        return $result;
    }

    /**
     * handle control requests for Slave Skip Error
     *
     * @return boolean
     */
    public static function handleRequestForSlaveSkipError()
    {
        $count = 1;
        if (isset($_POST['sr_skip_errors_count'])) {
            $count = $_POST['sr_skip_errors_count'] * 1;
        }

        $qStop = Replication::slaveControl("STOP");
        $qSkip = $GLOBALS['dbi']->tryQuery(
            "SET GLOBAL SQL_SLAVE_SKIP_COUNTER = " . $count . ";"
        );
        $qStart = Replication::slaveControl("START");

        $result = ($qStop !== false && $qStop !== -1 &&
            $qSkip !== false && $qSkip !== -1 &&
            $qStart !== false && $qStart !== -1);

        return $result;
    }
}