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