bugfixes: allow to work with multiple servers, fix boolean values in select fields
[phpmyadmin.git] / libraries / replication_gui.lib.php
bloba975bee72a81007653f557ab19ca9458f3727db9
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 /**
13 * returns code for selecting databases
15 * @return String HTML code
17 function PMA_replication_db_multibox()
19 $multi_values = '';
20 $multi_values .= '<select name="db_select[]" size="6" multiple="multiple" id="db_select">';
22 foreach ($GLOBALS['pma']->databases as $current_db) {
23 if ('information_schema' == $current_db) {
24 continue;
26 if (! empty($selectall) || (isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $current_db . '|'))) {
27 $is_selected = ' selected="selected"';
28 } else {
29 $is_selected = '';
31 $current_db = htmlspecialchars($current_db);
32 $multi_values .= ' <option value="' . $current_db . '" ' . $is_selected . '>' . $current_db . '</option>';
33 } // end while
35 $multi_values .= '</select>';
36 $multi_values .= '<br /><a href="#" id="db_reset_href">' . __('Uncheck All') . '</a>';
38 return $multi_values;
41 /**
42 * prints out code for changing master
44 * @param String $submitname - submit button name
47 function PMA_replication_gui_changemaster($submitname) {
49 list($username_length, $hostname_length) = PMA_replication_get_username_hostname_length();
51 echo '<form method="post" action="server_replication.php">';
52 echo PMA_generate_common_hidden_inputs('', '');
53 echo ' <fieldset id="fieldset_add_user_login">';
54 echo ' <legend>' . __('Slave configuration') . ' - ' . __('Change or reconfigure master server') . '</legend>';
55 echo __('Make sure, you have unique server-id in your configuration file (my.cnf). If not, please add the following line into [mysqld] section:') . '<br />';
56 echo '<pre>server-id=' . time() . '</pre>';
57 echo ' <div class="item">';
58 echo ' <label for="text_username">' . __('User name') . ':</label>';
59 echo ' <input type="text" name="username" id="text_username" maxlength="'. $username_length . '" title="' . __('User name') . '" />';
60 echo ' </div>';
61 echo ' <div class="item">';
62 echo ' <label for="text_pma_pw">' . __('Password') .' :</label>';
63 echo ' <input type="password" id="text_pma_pw" name="pma_pw" title="' . __('Password') . '" />';
64 echo ' </div>';
65 echo ' <div class="item">';
66 echo ' <label for="text_hostname">' . __('Host') . ' :</label>';
67 echo ' <input type="text" id="text_hostname" name="hostname" maxlength="' . $hostname_length . '" value="" />';
68 echo ' </div>';
69 echo ' <div class="item">';
70 echo ' <label for="text_port">' . __('Port') . ':</label>';
71 echo ' <input type="text" id="text_port" name="port" maxlength="6" value="3306" />';
72 echo ' </div>';
73 echo ' </fieldset>';
74 echo ' <fieldset id="fieldset_user_privtable_footer" class="tblFooters">';
75 echo ' <input type="hidden" name="sr_take_action" value="true" />';
76 echo ' <input type="submit" name="' . $submitname . '" id="confslave_submit" value="' . __('Go') . '" />';
77 echo ' </fieldset>';
78 echo '</form>';
81 /**
82 * This function prints out table with replication status.
84 * @param String type - either master or slave
85 * @param boolean $hidden - if true, then default style is set to hidden, default value false
86 * @param boolen $title - if true, then title is displayed, default true
88 function PMA_replication_print_status_table($type, $hidden = false, $title = true) {
89 global ${"{$type}_variables"};
90 global ${"{$type}_variables_alerts"};
91 global ${"{$type}_variables_oks"};
92 global ${"server_{$type}_replication"};
93 global ${"strReplicationStatus_{$type}"};
95 // TODO check the Masters server id?
96 // seems to default to '1' when queried via SHOW VARIABLES , but resulted in error on the master when slave connects
97 // [ERROR] Error reading packet from server: Misconfigured master - server id was not set ( server_errno=1236)
98 // [ERROR] Got fatal error 1236: 'Misconfigured master - server id was not set' from master when reading data from binary log
100 //$server_id = PMA_DBI_fetch_value("SHOW VARIABLES LIKE 'server_id'", 0, 1);
102 echo '<div id="replication_' . $type . '_section" style="' . ($hidden ? 'display: none;' : '') . '"> ';
104 if ($title) {
105 if ($type == 'master') {
106 echo '<h4><a name="replication_' . $type . '"></a>' . __('Master status') . '</h4>';
107 } else {
108 echo '<h4><a name="replication_' . $type . '"></a>' . __('Slave status') . '</h4>';
110 } else {
111 echo '<br />';
114 echo ' <table id="server' . $type . 'replicationsummary" class="data"> ';
115 echo ' <thead>';
116 echo ' <tr>';
117 echo ' <th>' . __('Variable') . '</th>';
118 echo ' <th>' . __('Value') . '</th>';
119 echo ' </tr>';
120 echo ' </thead>';
121 echo ' <tbody>';
123 $odd_row = true;
124 foreach (${"{$type}_variables"} as $variable) {
125 echo ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">';
126 echo ' <td class="name">';
127 echo $variable;
128 echo ' </td>';
129 echo ' <td class="value">';
132 // TODO change to regexp or something, to allow for negative match
133 if (isset(${"{$type}_variables_alerts"}[$variable])
134 && ${"{$type}_variables_alerts"}[$variable] == ${"server_{$type}_replication"}[0][$variable]
136 echo '<span class="attention">';
138 } elseif (isset(${"{$type}_variables_oks"}[$variable])
139 && ${"{$type}_variables_oks"}[$variable] == ${"server_{$type}_replication"}[0][$variable]
141 echo '<span class="allfine">';
142 } else {
143 echo '<span>';
145 echo ${"server_{$type}_replication"}[0][$variable];
146 echo '</span>';
148 echo ' </td>';
149 echo ' </tr>';
151 $odd_row = ! $odd_row;
154 echo ' </tbody>';
155 echo ' </table>';
156 echo ' <br />';
157 echo '</div>';
162 * Prints table with slave users connected to this master
164 * @param boolean $hidden - if true, then default style is set to hidden, default value false
166 function PMA_replication_print_slaves_table($hidden = false) {
168 // Fetch data
169 $data = PMA_DBI_fetch_result('SHOW SLAVE HOSTS', null, null);
171 echo ' <br />';
172 echo ' <div id="replication_slaves_section" style="' . ($hidden ? 'display: none;' : '') . '"> ';
173 echo ' <table class="data">';
174 echo ' <thead>';
175 echo ' <tr>';
176 echo ' <th>' . __('Server ID') . '</th>';
177 echo ' <th>' . __('Host') . '</th>';
178 echo ' </tr>';
179 echo ' </thead>';
180 echo ' <tbody>';
182 $odd_row = true;
183 foreach ($data as $slave) {
184 echo ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">';
185 echo ' <td class="value">' . $slave['Server_id'] . '</td>';
186 echo ' <td class="value">' . $slave['Host'] . '</td>';
187 echo ' </tr>';
189 $odd_row = ! $odd_row;
192 echo ' </tbody>';
193 echo ' </table>';
194 echo ' <br />';
195 PMA_Message::notice(__('Only slaves started with the --report-host=host_name option are visible in this list.'))->display();
196 echo ' <br />';
197 echo ' </div>';
201 * get the correct username and hostname lengths for this MySQL server
203 * @uses strtok()
204 * @return array username length, hostname length
207 function PMA_replication_get_username_hostname_length() {
208 $fields_info = PMA_DBI_get_fields('mysql', 'user');
209 $username_length = 16;
210 $hostname_length = 41;
211 foreach ($fields_info as $key => $val) {
212 if ($val['Field'] == 'User') {
213 strtok($val['Type'], '()');
214 $v = strtok('()');
215 if (is_int($v)) {
216 $username_length = $v;
218 } elseif ($val['Field'] == 'Host') {
219 strtok($val['Type'], '()');
220 $v = strtok('()');
221 if (is_int($v)) {
222 $hostname_length = $v;
226 return array($username_length, $hostname_length);
230 * Print code to add a replication slave user to the master
232 function PMA_replication_gui_master_addslaveuser() {
234 list($username_length, $hostname_length) = PMA_replication_get_username_hostname_length();
236 if (isset($GLOBALS['username']) && strlen($GLOBALS['username']) === 0) {
237 $GLOBALS['pred_username'] = 'any';
239 echo '<div id="master_addslaveuser_gui">';
240 echo '<form autocomplete="off" method="post" action="server_privileges.php" onsubmit="return checkAddUser(this);">';
241 echo PMA_generate_common_hidden_inputs('', '');
242 echo '<fieldset id="fieldset_add_user_login">'
243 . '<legend>'.__('Add slave replication user').'</legend>'
244 . '<input type="hidden" name="grant_count" value="25" />'
245 . '<input type="hidden" name="createdb" id="createdb_0" value="0" />'
246 . '<input id="checkbox_Repl_slave_priv" type="hidden" title="Needed for the replication slaves." value="Y" name="Repl_slave_priv"/>'
247 . '<input id="checkbox_Repl_client_priv" type="hidden" title="Needed for the replication slaves." value="Y" name="Repl_client_priv"/>'
248 . ''
249 . '<input type="hidden" name="sr_take_action" value="true" />'
250 . '<div class="item">'
251 . '<label for="select_pred_username">'
252 . ' ' . __('User name') . ':'
253 . '</label>'
254 . '<span class="options">'
255 . ' <select name="pred_username" id="select_pred_username" title="' . __('User name') . '"'
256 . ' onchange="if (this.value == \'any\') { username.value = \'\'; } else if (this.value == \'userdefined\') { username.focus(); username.select(); }">'
257 . ' <option value="any"' . ((isset($GLOBALS['pred_username']) && $GLOBALS['pred_username'] == 'any') ? ' selected="selected"' : '') . '>' . __('Any user') . '</option>'
258 . ' <option value="userdefined"' . ((! isset($GLOBALS['pred_username']) || $GLOBALS['pred_username'] == 'userdefined') ? ' selected="selected"' : '') . '>' . __('Use text field') . ':</option>'
259 . ' </select>'
260 . '</span>'
261 . '<input type="text" name="username" maxlength="'
262 . $username_length . '" title="' . __('User name') . '"'
263 . (empty($GLOBALS['username'])
264 ? ''
265 : ' value="' . (isset($GLOBALS['new_username'])
266 ? $GLOBALS['new_username']
267 : $GLOBALS['username']) . '"')
268 . ' onchange="pred_username.value = \'userdefined\';" />'
269 . '</div>'
270 . '<div class="item">'
271 . '<label for="select_pred_hostname">'
272 . ' ' . __('Host') . ':'
273 . '</label>'
274 . '<span class="options">'
275 . ' <select name="pred_hostname" id="select_pred_hostname" title="' . __('Host') . '"';
276 $_current_user = PMA_DBI_fetch_value('SELECT USER();');
277 if (! empty($_current_user)) {
278 $thishost = str_replace("'", '', substr($_current_user, (strrpos($_current_user, '@') + 1)));
279 if ($thishost == 'localhost' || $thishost == '127.0.0.1') {
280 unset($thishost);
283 echo ' onchange="if (this.value == \'any\') { hostname.value = \'%\'; } else if (this.value == \'localhost\') { hostname.value = \'localhost\'; } '
284 . (empty($thishost) ? '' : 'else if (this.value == \'thishost\') { hostname.value = \'' . addslashes(htmlspecialchars($thishost)) . '\'; } ')
285 . 'else if (this.value == \'hosttable\') { hostname.value = \'\'; } else if (this.value == \'userdefined\') { hostname.focus(); hostname.select(); }">' . "\n";
286 unset($_current_user);
288 // when we start editing a user, $GLOBALS['pred_hostname'] is not defined
289 if (! isset($GLOBALS['pred_hostname']) && isset($GLOBALS['hostname'])) {
290 switch (strtolower($GLOBALS['hostname'])) {
291 case 'localhost':
292 case '127.0.0.1':
293 $GLOBALS['pred_hostname'] = 'localhost';
294 break;
295 case '%':
296 $GLOBALS['pred_hostname'] = 'any';
297 break;
298 default:
299 $GLOBALS['pred_hostname'] = 'userdefined';
300 break;
303 echo ' <option value="any"'
304 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'any')
305 ? ' selected="selected"' : '') . '>' . __('Any host')
306 . '</option>'
307 . ' <option value="localhost"'
308 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'localhost')
309 ? ' selected="selected"' : '') . '>' . __('Local')
310 . '</option>';
312 if (!empty($thishost)) {
313 echo ' <option value="thishost"'
314 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'thishost')
315 ? ' selected="selected"' : '') . '>' . __('This Host')
316 . '</option>';
318 unset($thishost);
319 echo ' <option value="hosttable"'
320 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'hosttable')
321 ? ' selected="selected"' : '') . '>' . __('Use Host Table')
322 . '</option>'
323 . ' <option value="userdefined"'
324 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'userdefined')
325 ? ' selected="selected"' : '')
326 . '>' . __('Use text field') . ':</option>'
327 . ' </select>'
328 . '</span>'
329 . '<input type="text" name="hostname" maxlength="'
330 . $hostname_length . '" value="'
331 . (isset($GLOBALS['hostname']) ? $GLOBALS['hostname'] : '')
332 . '" title="' . __('Host')
333 . '" onchange="pred_hostname.value = \'userdefined\';" />'
334 . PMA_showHint(__('When Host table is used, this field is ignored and values stored in Host table are used instead.'))
335 . '</div>'
336 . '<div class="item">'
337 . '<label for="select_pred_password">'
338 . ' ' . __('Password') . ':'
339 . '</label>'
340 . '<span class="options">'
341 . ' <select name="pred_password" id="select_pred_password" title="'
342 . __('Password') . '"'
343 . ' onchange="if (this.value == \'none\') { pma_pw.value = \'\'; pma_pw2.value = \'\'; } else if (this.value == \'userdefined\') { pma_pw.focus(); pma_pw.select(); }">'
344 . ' <option value="none"';
345 if (isset($GLOBALS['username']) && $mode != 'change') {
346 echo ' selected="selected"';
348 echo '>' . __('No Password') . '</option>'
349 . ' <option value="userdefined"' . (isset($GLOBALS['username']) ? '' : ' selected="selected"') . '>' . __('Use text field') . ':</option>'
350 . ' </select>'
351 . '</span>'
352 . '<input type="password" id="text_pma_pw" name="pma_pw" title="' . __('Password') . '" onchange="pred_password.value = \'userdefined\';" />'
353 . '</div>'
354 . '<div class="item">'
355 . '<label for="text_pma_pw2">'
356 . ' ' . __('Re-type') . ':'
357 . '</label>'
358 . '<span class="options">&nbsp;</span>'
359 . '<input type="password" name="pma_pw2" id="text_pma_pw2" title="' . __('Re-type') . '" onchange="pred_password.value = \'userdefined\';" />'
360 . '</div>'
361 . '<div class="item">'
362 . '<label for="button_generate_password">'
363 . ' ' . __('Generate Password') . ':'
364 . '</label>'
365 . '<span class="options">'
366 . ' <input type="button" id="button_generate_password" value="' . __('Generate') . '" onclick="suggestPassword(this.form)" />'
367 . '</span>'
368 . '<input type="text" name="generated_pw" id="generated_pw" />'
369 . '</div>'
370 . '</fieldset>';
371 echo '<fieldset id="fieldset_user_privtable_footer" class="tblFooters">'
372 . ' <input type="submit" name="adduser_submit" id="adduser_submit" value="' . __('Go') . '" />'
373 . '</fieldset>';
374 echo '</form>';
375 echo '</div>';