Revert "Do not assume that DefaultLang is escaped."
[phpmyadmin/madhuracj.git] / libraries / replication_gui.lib.php
blobb5f14ad804b6831448d095d2427895035eeea5a8
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @version $Id$
6 * @package phpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 $jscode['master_replication'] = 'divShowHideFunc(\'master_status_href\', \'replication_master_section\');' . "\n"
13 . 'divShowHideFunc(\'master_slaves_href\', \'replication_slaves_section\');' . "\n";
15 // Add JS events to generate example my.cnf config lines
16 $jscode['configure_master'] =
17 'var c_output = "";' . "\n" .
18 'var c_text = "server-id=' . $serverid . '<br />log-bin=mysql-bin<br />log-error=mysql-bin.err<br />";' . "\n" .
19 'var c_ignore = "binlog_ignore_db=";' . "\n" .
20 'var c_do = "binlog_do_db=";' . "\n" .
22 '$(\'db_reset_href\').addEvent(\'click\', function() {' . "\n" .
23 ' $(\'db_select\').getSelected().each(function(el) {' . "\n" .
24 ' el.selected = false;' . "\n" .
25 ' });' . "\n" .
26 ' $(\'rep\').set(\'html\', c_text);' . "\n" .
27 '});' . "\n" .
28 '$(\'db_type\').addEvent(\'change\',function() {' . "\n" .
29 ' if ($(\'db_type\').getSelected().get(\'value\')=="all")' . "\n" .
30 ' $(\'rep\').set(\'html\', c_text+c_ignore+c_output);' . "\n" .
31 ' else' . "\n" .
32 ' $(\'rep\').set(\'html\', c_text+c_do+c_output);' . "\n" .
33 '});' . "\n" .
35 '$(\'db_select\').addEvent(\'change\',function() {' . "\n" .
36 ' var count = 0;' . "\n" .
38 ' $(\'db_select\').getSelected().each(function(el) { ' . "\n" .
39 ' if (count==0)' . "\n" .
40 ' c_output = el.get(\'value\');' . "\n" .
41 ' else' . "\n" .
42 ' c_output = c_output + \',\' +el.get(\'value\');' . "\n" .
44 ' count=count+1;' . "\n" .
46 ' if ($(\'db_select\').getSelected().length==count) {' . "\n" .
47 ' if ($(\'db_type\').getSelected().get(\'value\')=="all")' . "\n" .
48 ' $(\'rep\').set(\'html\', c_text+c_ignore+c_output);' . "\n" .
49 ' else' . "\n" .
50 ' $(\'rep\').set(\'html\', c_text+c_do+c_output);' . "\n" .
51 ' count = 0;' . "\n" .
52 ' }' . "\n" .
53 ' });' . "\n" .
54 '});' . "\n";
56 $jscode['slave_control'] =
57 'divShowHideFunc(\'slave_status_href\', \'replication_slave_section\');' . "\n" .
58 'divShowHideFunc(\'slave_control_href\', \'slave_control_gui\');' . "\n" .
59 'divShowHideFunc(\'slave_errormanagement_href\',\'slave_errormanagement_gui\'); ' . "\n";
61 $jscode['slave_control_sync'] =
62 'divShowHideFunc(\'slave_synchronization_href\', \'slave_synchronization_gui\');' . "\n";
64 /**
65 * returns code for selecting databases
67 * @return String HTML code
69 function PMA_replication_db_multibox()
71 $multi_values = '';
72 $multi_values .= '<select name="db_select[]" size="6" multiple="multiple" id="db_select">';
74 foreach ($GLOBALS['pma']->databases as $current_db) {
75 if ('information_schema' == $current_db) {
76 continue;
78 if (! empty($selectall) || (isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $current_db . '|'))) {
79 $is_selected = ' selected="selected"';
80 } else {
81 $is_selected = '';
83 $current_db = htmlspecialchars($current_db);
84 $multi_values .= ' <option value="' . $current_db . '" ' . $is_selected . '>' . $current_db . '</option>';
85 } // end while
87 $multi_values .= '</select>';
88 $multi_values .= '<br /><a href="#" id="db_reset_href">' . $GLOBALS['strUncheckAll'] . '</a>';
90 return $multi_values;
93 /**
94 * prints out code for changing master
96 * @param String $submitname - submit button name
99 function PMA_replication_gui_changemaster($submitname) {
101 list($username_length, $hostname_length) = PMA_replication_get_username_hostname_length();
103 echo '<form method="post" action="server_replication.php">';
104 echo PMA_generate_common_hidden_inputs('', '');
105 echo ' <fieldset id="fieldset_add_user_login">';
106 echo ' <legend>' . $GLOBALS['strReplicationSlaveConfiguration'] . ' - ' . $GLOBALS['strReplicationSlaveChangeMaster'] . '</legend>';
107 echo $GLOBALS['strSlaveConfigure'] . '<br />';
108 echo '<pre>server-id=' . time() . '</pre>';
109 echo ' <div class="item">';
110 echo ' <label for="text_username">' . $GLOBALS['strUserName'] . ':</label>';
111 echo ' <input type="text" name="username" id="text_username" maxlength="'. $username_length . '" title="' . $GLOBALS['strUserName'] . '" />';
112 echo ' </div>';
113 echo ' <div class="item">';
114 echo ' <label for="text_pma_pw">' . $GLOBALS['strPassword'] .' :</label>';
115 echo ' <input type="password" id="text_pma_pw" name="pma_pw" title="' . $GLOBALS['strPassword'] . '" />';
116 echo ' </div>';
117 echo ' <div class="item">';
118 echo ' <label for="text_hostname">' . $GLOBALS['strHost'] . ' :</label>';
119 echo ' <input type="text" id="text_hostname" name="hostname" maxlength="' . $hostname_length . '" value="" />';
120 echo ' </div>';
121 echo ' <div class="item">';
122 echo ' <label for="text_port">' . $GLOBALS['strPort'] . ':</label>';
123 echo ' <input type="text" id="text_port" name="port" maxlength="6" value="3306" />';
124 echo ' </div>';
125 echo ' </fieldset>';
126 echo ' <fieldset id="fieldset_user_privtable_footer" class="tblFooters">';
127 echo ' <input type="hidden" name="sr_take_action" value="true" />';
128 echo ' <input type="submit" name="'.$submitname.'" id="confslave_submit" value="' . $GLOBALS['strGo'] . '" />';
129 echo ' </fieldset>';
130 echo '</form>';
134 * This function prints out table with replication status.
136 * @param String type - either master or slave
137 * @param boolean $hidden - if true, then default style is set to hidden, default value false
138 * @param boolen $title - if true, then title is displayed, default true
140 function PMA_replication_print_status_table($type, $hidden = false, $title = true) {
141 global ${"{$type}_variables"};
142 global ${"{$type}_variables_alerts"};
143 global ${"{$type}_variables_oks"};
144 global ${"server_{$type}_replication"};
145 global ${"strReplicationStatus_{$type}"};
147 // TODO check the Masters server id?
148 // seems to default to '1' when queried via SHOW VARIABLES , but resulted in error on the master when slave connects
149 // [ERROR] Error reading packet from server: Misconfigured master - server id was not set ( server_errno=1236)
150 // [ERROR] Got fatal error 1236: 'Misconfigured master - server id was not set' from master when reading data from binary log
152 //$server_id = PMA_DBI_fetch_value("SHOW VARIABLES LIKE 'server_id'", 0, 1);
154 echo '<div id="replication_' . $type . '_section" style="' . ($hidden ? 'display: none' : '') . '"> ';
156 if ($title) {
157 echo '<h4><a name="replication_' . $type . '"></a>' . ${"strReplicationStatus_{$type}"} . '</h4>';
158 } else {
159 echo '<br />';
162 echo ' <table id="server' . $type . 'replicationsummary" class="data"> ';
163 echo ' <thead>';
164 echo ' <tr>';
165 echo ' <th>' . $GLOBALS['strVar'] . '</th>';
166 echo ' <th>' . $GLOBALS['strValue'] . '</th>';
167 echo ' </tr>';
168 echo ' </thead>';
169 echo ' <tbody>';
171 $odd_row = true;
172 foreach (${"{$type}_variables"} as $variable) {
173 echo ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">';
174 echo ' <td class="name">';
175 echo $variable;
176 echo ' </td>';
177 echo ' <td class="value">';
180 // TODO change to regexp or something, to allow for negative match
181 if (isset(${"{$type}_variables_alerts"}[$variable])
182 && ${"{$type}_variables_alerts"}[$variable] == ${"server_{$type}_replication"}[0][$variable]
184 echo '<span class="attention">';
186 } elseif (isset(${"{$type}_variables_oks"}[$variable])
187 && ${"{$type}_variables_oks"}[$variable] == ${"server_{$type}_replication"}[0][$variable]
189 echo '<span class="allfine">';
190 } else {
191 echo '<span>';
193 echo ${"server_{$type}_replication"}[0][$variable];
194 echo '</span>';
196 echo ' </td>';
197 echo ' </tr>';
199 $odd_row = ! $odd_row;
202 echo ' </tbody>';
203 echo ' </table>';
204 echo ' <br />';
205 echo '</div>';
210 * Prints table with slave users connected to this master
212 * @param boolean $hidden - if true, then default style is set to hidden, default value false
214 function PMA_replication_print_slaves_table($hidden = false) {
216 // Fetch data
217 $data = PMA_DBI_fetch_result('SHOW SLAVE HOSTS', null, null);
219 echo ' <br />';
220 echo ' <div id="replication_slaves_section" style="' . ($hidden ? 'display: none' : '') . '"> ';
221 echo ' <table class="data">';
222 echo ' <thead>';
223 echo ' <tr>';
224 echo ' <th>' . $GLOBALS['strBinLogServerId'] . '</th>';
225 echo ' <th>' . $GLOBALS['strHost'] . '</th>';
226 echo ' </tr>';
227 echo ' </thead>';
228 echo ' <tbody>';
230 $odd_row = true;
231 foreach ($data as $slave) {
232 echo ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">';
233 echo ' <td class="value">' . $slave['Server_id'] . '</td>';
234 echo ' <td class="value">' . $slave['Host'] . '</td>';
235 echo ' </tr>';
237 $odd_row = ! $odd_row;
240 echo ' </tbody>';
241 echo ' </table>';
242 echo ' <br />';
243 PMA_Message::notice('strReplicationShowConnectedSlavesNote')->display();
244 echo ' <br />';
245 echo ' </div>';
249 * get the correct username and hostname lengths for this MySQL server
251 * @uses strtok()
252 * @return array username length, hostname length
255 function PMA_replication_get_username_hostname_length() {
256 $fields_info = PMA_DBI_get_fields('mysql', 'user');
257 $username_length = 16;
258 $hostname_length = 41;
259 foreach ($fields_info as $key => $val) {
260 if ($val['Field'] == 'User') {
261 strtok($val['Type'], '()');
262 $v = strtok('()');
263 if (is_int($v)) {
264 $username_length = $v;
266 } elseif ($val['Field'] == 'Host') {
267 strtok($val['Type'], '()');
268 $v = strtok('()');
269 if (is_int($v)) {
270 $hostname_length = $v;
274 return array($username_length, $hostname_length);
278 * Print code to add a replication slave user to the master
280 function PMA_replication_gui_master_addslaveuser() {
282 list($username_length, $hostname_length) = PMA_replication_get_username_hostname_length();
284 if (isset($GLOBALS['username']) && strlen($GLOBALS['username']) === 0) {
285 $GLOBALS['pred_username'] = 'any';
287 echo '<div id="master_addslaveuser_gui">';
288 echo '<form autocomplete="off" method="post" action="server_privileges.php" onsubmit="return checkAddUser(this);">';
289 echo PMA_generate_common_hidden_inputs('', '');
290 echo '<fieldset id="fieldset_add_user_login">'
291 . '<legend>'.$GLOBALS['strReplicationAddSlaveUser'].'</legend>'
292 . '<input type="hidden" name="grant_count" value="25" />'
293 . '<input type="hidden" name="createdb" id="createdb_0" value="0" />'
294 . '<input id="checkbox_Repl_slave_priv" type="hidden" title="Needed for the replication slaves." value="Y" name="Repl_slave_priv"/>'
295 . '<input id="checkbox_Repl_client_priv" type="hidden" title="Needed for the replication slaves." value="Y" name="Repl_client_priv"/>'
296 . ''
297 . '<input type="hidden" name="sr_take_action" value="true" />'
298 . '<div class="item">'
299 . '<label for="select_pred_username">'
300 . ' ' . $GLOBALS['strUserName'] . ':'
301 . '</label>'
302 . '<span class="options">'
303 . ' <select name="pred_username" id="select_pred_username" title="' . $GLOBALS['strUserName'] . '"'
304 . ' onchange="if (this.value == \'any\') { username.value = \'\'; } else if (this.value == \'userdefined\') { username.focus(); username.select(); }">'
305 . ' <option value="any"' . ((isset($GLOBALS['pred_username']) && $GLOBALS['pred_username'] == 'any') ? ' selected="selected"' : '') . '>' . $GLOBALS['strAnyUser'] . '</option>'
306 . ' <option value="userdefined"' . ((! isset($GLOBALS['pred_username']) || $GLOBALS['pred_username'] == 'userdefined') ? ' selected="selected"' : '') . '>' . $GLOBALS['strUseTextField'] . ':</option>'
307 . ' </select>'
308 . '</span>'
309 . '<input type="text" name="username" maxlength="'
310 . $username_length . '" title="' . $GLOBALS['strUserName'] . '"'
311 . (empty($GLOBALS['username'])
312 ? ''
313 : ' value="' . (isset($GLOBALS['new_username'])
314 ? $GLOBALS['new_username']
315 : $GLOBALS['username']) . '"')
316 . ' onchange="pred_username.value = \'userdefined\';" />'
317 . '</div>'
318 . '<div class="item">'
319 . '<label for="select_pred_hostname">'
320 . ' ' . $GLOBALS['strHost'] . ':'
321 . '</label>'
322 . '<span class="options">'
323 . ' <select name="pred_hostname" id="select_pred_hostname" title="' . $GLOBALS['strHost'] . '"';
324 $_current_user = PMA_DBI_fetch_value('SELECT USER();');
325 if (! empty($_current_user)) {
326 $thishost = str_replace("'", '', substr($_current_user, (strrpos($_current_user, '@') + 1)));
327 if ($thishost == 'localhost' || $thishost == '127.0.0.1') {
328 unset($thishost);
331 echo ' onchange="if (this.value == \'any\') { hostname.value = \'%\'; } else if (this.value == \'localhost\') { hostname.value = \'localhost\'; } '
332 . (empty($thishost) ? '' : 'else if (this.value == \'thishost\') { hostname.value = \'' . addslashes(htmlspecialchars($thishost)) . '\'; } ')
333 . 'else if (this.value == \'hosttable\') { hostname.value = \'\'; } else if (this.value == \'userdefined\') { hostname.focus(); hostname.select(); }">' . "\n";
334 unset($_current_user);
336 // when we start editing a user, $GLOBALS['pred_hostname'] is not defined
337 if (! isset($GLOBALS['pred_hostname']) && isset($GLOBALS['hostname'])) {
338 switch (strtolower($GLOBALS['hostname'])) {
339 case 'localhost':
340 case '127.0.0.1':
341 $GLOBALS['pred_hostname'] = 'localhost';
342 break;
343 case '%':
344 $GLOBALS['pred_hostname'] = 'any';
345 break;
346 default:
347 $GLOBALS['pred_hostname'] = 'userdefined';
348 break;
351 echo ' <option value="any"'
352 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'any')
353 ? ' selected="selected"' : '') . '>' . $GLOBALS['strAnyHost']
354 . '</option>'
355 . ' <option value="localhost"'
356 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'localhost')
357 ? ' selected="selected"' : '') . '>' . $GLOBALS['strLocalhost']
358 . '</option>';
359 if (!empty($thishost)) {
360 echo ' <option value="thishost"'
361 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'thishost')
362 ? ' selected="selected"' : '') . '>' . $GLOBALS['strThisHost']
363 . '</option>';
365 unset($thishost);
366 echo ' <option value="hosttable"'
367 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'hosttable')
368 ? ' selected="selected"' : '') . '>' . $GLOBALS['strUseHostTable']
369 . '</option>'
370 . ' <option value="userdefined"'
371 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'userdefined')
372 ? ' selected="selected"' : '')
373 . '>' . $GLOBALS['strUseTextField'] . ':</option>'
374 . ' </select>'
375 . '</span>'
376 . '<input type="text" name="hostname" maxlength="'
377 . $hostname_length . '" value="'
378 . (isset($GLOBALS['hostname']) ? $GLOBALS['hostname'] : '')
379 . '" title="' . $GLOBALS['strHost']
380 . '" onchange="pred_hostname.value = \'userdefined\';" />'
381 . PMA_showHint($GLOBALS['strHostTableExplanation'])
382 . '</div>'
383 . '<div class="item">'
384 . '<label for="select_pred_password">'
385 . ' ' . $GLOBALS['strPassword'] . ':'
386 . '</label>'
387 . '<span class="options">'
388 . ' <select name="pred_password" id="select_pred_password" title="'
389 . $GLOBALS['strPassword'] . '"'
390 . ' onchange="if (this.value == \'none\') { pma_pw.value = \'\'; pma_pw2.value = \'\'; } else if (this.value == \'userdefined\') { pma_pw.focus(); pma_pw.select(); }">'
391 . ' <option value="none"';
392 if (isset($GLOBALS['username']) && $mode != 'change') {
393 echo ' selected="selected"';
395 echo '>' . $GLOBALS['strNoPassword'] . '</option>'
396 . ' <option value="userdefined"' . (isset($GLOBALS['username']) ? '' : ' selected="selected"') . '>' . $GLOBALS['strUseTextField'] . ':</option>'
397 . ' </select>'
398 . '</span>'
399 . '<input type="password" id="text_pma_pw" name="pma_pw" title="' . $GLOBALS['strPassword'] . '" onchange="pred_password.value = \'userdefined\';" />'
400 . '</div>'
401 . '<div class="item">'
402 . '<label for="text_pma_pw2">'
403 . ' ' . $GLOBALS['strReType'] . ':'
404 . '</label>'
405 . '<span class="options">&nbsp;</span>'
406 . '<input type="password" name="pma_pw2" id="text_pma_pw2" title="' . $GLOBALS['strReType'] . '" onchange="pred_password.value = \'userdefined\';" />'
407 . '</div>'
408 . '<div class="item">'
409 . '<label for="button_generate_password">'
410 . ' ' . $GLOBALS['strGeneratePassword'] . ':'
411 . '</label>'
412 . '<span class="options">'
413 . ' <input type="button" id="button_generate_password" value="' . $GLOBALS['strGenerate'] . '" onclick="suggestPassword(this.form)" />'
414 . '</span>'
415 . '<input type="text" name="generated_pw" id="generated_pw" />'
416 . '</div>'
417 . '</fieldset>';
418 echo '<fieldset id="fieldset_user_privtable_footer" class="tblFooters">'
419 . ' <input type="submit" name="adduser_submit" id="adduser_submit" value="' . $GLOBALS['strGo'] . '" />'
420 . '</fieldset>';
421 echo '</form>';
422 echo '</div>';