Finnish update
[phpmyadmin/tyronm.git] / libraries / replication_gui.lib.php
blobd77280068d3708b660dfc7581df34d1ec9cfaa02
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 (! empty($selectall) || (isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $current_db . '|'))) {
76 $is_selected = ' selected="selected"';
77 } else {
78 $is_selected = '';
80 $current_db = htmlspecialchars($current_db);
81 $multi_values .= ' <option value="' . $current_db . '" ' . $is_selected . '>' . $current_db . '</option>';
82 } // end while
84 $multi_values .= '</select>';
85 $multi_values .= '<br /><a href="#" id="db_reset_href">' . $GLOBALS['strUncheckAll'] . '</a>';
87 return $multi_values;
90 /**
91 * prints out code for changing master
93 * @param String $submitname - submit button name
96 function PMA_replication_gui_changemaster($submitname) {
98 list($username_length, $hostname_length) = PMA_replication_get_username_hostname_length();
100 echo '<form method="post" action="server_replication.php">';
101 echo PMA_generate_common_hidden_inputs('', '');
102 echo ' <fieldset id="fieldset_add_user_login">';
103 echo ' <legend>' . $GLOBALS['strReplicationSlaveConfiguration'] . ' - ' . $GLOBALS['strReplicationSlaveChangeMaster'] . '</legend>';
104 echo $GLOBALS['strSlaveConfigure'] . '<br />';
105 echo '<pre>server-id=' . time() . '</pre>';
106 echo ' <div class="item">';
107 echo ' <label for="text_username">' . $GLOBALS['strUserName'] . ':</label>';
108 echo ' <input type="text" name="username" id="text_username" maxlength="'. $username_length . '" title="' . $GLOBALS['strUserName'] . '" />';
109 echo ' </div>';
110 echo ' <div class="item">';
111 echo ' <label for="text_pma_pw">' . $GLOBALS['strPassword'] .' :</label>';
112 echo ' <input type="password" id="text_pma_pw" name="pma_pw" title="' . $GLOBALS['strPassword'] . '" />';
113 echo ' </div>';
114 echo ' <div class="item">';
115 echo ' <label for="text_hostname">' . $GLOBALS['strHost'] . ' :</label>';
116 echo ' <input type="text" id="text_hostname" name="hostname" maxlength="' . $hostname_length . '" value="" />';
117 echo ' </div>';
118 echo ' <div class="item">';
119 echo ' <label for="text_port">' . $GLOBALS['strPort'] . ':</label>';
120 echo ' <input type="text" id="text_port" name="port" maxlength="6" value="3306" />';
121 echo ' </div>';
122 echo ' </fieldset>';
123 echo ' <fieldset id="fieldset_user_privtable_footer" class="tblFooters">';
124 echo ' <input type="hidden" name="sr_take_action" value="true" />';
125 echo ' <input type="submit" name="'.$submitname.'" id="confslave_submit" value="' . $GLOBALS['strGo'] . '" />';
126 echo ' </fieldset>';
127 echo '</form>';
131 * This function prints out table with replication status.
133 * @param String type - either master or slave
134 * @param boolean $hidden - if true, then default style is set to hidden, default value false
135 * @param boolen $title - if true, then title is displayed, default true
137 function PMA_replication_print_status_table($type, $hidden = false, $title = true) {
138 global ${"{$type}_variables"};
139 global ${"{$type}_variables_alerts"};
140 global ${"{$type}_variables_oks"};
141 global ${"server_{$type}_replication"};
142 global ${"strReplicationStatus_{$type}"};
144 // TODO check the Masters server id?
145 // seems to default to '1' when queried via SHOW VARIABLES , but resulted in error on the master when slave connects
146 // [ERROR] Error reading packet from server: Misconfigured master - server id was not set ( server_errno=1236)
147 // [ERROR] Got fatal error 1236: 'Misconfigured master - server id was not set' from master when reading data from binary log
149 //$server_id = PMA_DBI_fetch_value("SHOW VARIABLES LIKE 'server_id'", 0, 1);
151 echo '<div id="replication_' . $type . '_section" style="' . ($hidden ? 'display: none' : '') . '"> ';
153 if ($title) {
154 echo '<h4><a name="replication_' . $type . '"></a>' . ${"strReplicationStatus_{$type}"} . '</h4>';
155 } else {
156 echo '<br />';
159 echo ' <table id="server' . $type . 'replicationsummary" class="data"> ';
160 echo ' <thead>';
161 echo ' <tr>';
162 echo ' <th>' . $GLOBALS['strVar'] . '</th>';
163 echo ' <th>' . $GLOBALS['strValue'] . '</th>';
164 echo ' </tr>';
165 echo ' </thead>';
166 echo ' <tbody>';
168 $odd_row = true;
169 foreach (${"{$type}_variables"} as $variable) {
170 echo ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">';
171 echo ' <td class="name">';
172 echo $variable;
173 echo ' </td>';
174 echo ' <td class="value">';
177 // TODO change to regexp or something, to allow for negative match
178 if (isset(${"{$type}_variables_alerts"}[$variable])
179 && ${"{$type}_variables_alerts"}[$variable] == ${"server_{$type}_replication"}[0][$variable]
181 echo '<span class="attention">';
183 } elseif (isset(${"{$type}_variables_oks"}[$variable])
184 && ${"{$type}_variables_oks"}[$variable] == ${"server_{$type}_replication"}[0][$variable]
186 echo '<span class="allfine">';
187 } else {
188 echo '<span>';
190 echo ${"server_{$type}_replication"}[0][$variable];
191 echo '</span>';
193 echo ' </td>';
194 echo ' </tr>';
196 $odd_row = ! $odd_row;
199 echo ' </tbody>';
200 echo ' </table>';
201 echo ' <br />';
202 echo '</div>';
207 * Prints table with slave users connected to this master
209 * @param boolean $hidden - if true, then default style is set to hidden, default value false
211 function PMA_replication_print_slaves_table($hidden = false) {
213 // Fetch data
214 $data = PMA_DBI_fetch_result('SHOW SLAVE HOSTS', null, null);
216 echo ' <br />';
217 echo ' <div id="replication_slaves_section" style="' . ($hidden ? 'display: none' : '') . '"> ';
218 echo ' <table class="data">';
219 echo ' <thead>';
220 echo ' <tr>';
221 echo ' <th>' . $GLOBALS['strBinLogServerId'] . '</th>';
222 echo ' <th>' . $GLOBALS['strHost'] . '</th>';
223 echo ' </tr>';
224 echo ' </thead>';
225 echo ' <tbody>';
227 $odd_row = true;
228 foreach ($data as $slave) {
229 echo ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">';
230 echo ' <td class="value">' . $slave['Server_id'] . '</td>';
231 echo ' <td class="value">' . $slave['Host'] . '</td>';
232 echo ' </tr>';
234 $odd_row = ! $odd_row;
237 echo ' </tbody>';
238 echo ' </table>';
239 echo ' <br />';
240 PMA_Message::notice('strReplicationShowConnectedSlavesNote')->display();
241 echo ' <br />';
242 echo ' </div>';
246 * get the correct username and hostname lengths for this MySQL server
248 * @uses strtok()
249 * @return array username length, hostname length
252 function PMA_replication_get_username_hostname_length() {
253 $fields_info = PMA_DBI_get_fields('mysql', 'user');
254 $username_length = 16;
255 $hostname_length = 41;
256 foreach ($fields_info as $key => $val) {
257 if ($val['Field'] == 'User') {
258 strtok($val['Type'], '()');
259 $v = strtok('()');
260 if (is_int($v)) {
261 $username_length = $v;
263 } elseif ($val['Field'] == 'Host') {
264 strtok($val['Type'], '()');
265 $v = strtok('()');
266 if (is_int($v)) {
267 $hostname_length = $v;
271 return array($username_length, $hostname_length);
275 * Print code to add a replication slave user to the master
277 function PMA_replication_gui_master_addslaveuser() {
279 list($username_length, $hostname_length) = PMA_replication_get_username_hostname_length();
281 if (isset($GLOBALS['username']) && strlen($GLOBALS['username']) === 0) {
282 $GLOBALS['pred_username'] = 'any';
284 echo '<div id="master_addslaveuser_gui">';
285 echo '<form autocomplete="off" method="post" action="server_privileges.php" onsubmit="return checkAddUser(this);">';
286 echo PMA_generate_common_hidden_inputs('', '');
287 echo '<fieldset id="fieldset_add_user_login">'
288 . '<legend>'.$GLOBALS['strReplicationAddSlaveUser'].'</legend>'
289 . '<input type="hidden" name="grant_count" value="25" />'
290 . '<input type="hidden" name="createdb" id="createdb_0" value="0" />'
291 . '<input id="checkbox_Repl_slave_priv" type="hidden" title="Needed for the replication slaves." value="Y" name="Repl_slave_priv"/>'
292 . '<input id="checkbox_Repl_client_priv" type="hidden" title="Needed for the replication slaves." value="Y" name="Repl_client_priv"/>'
293 . ''
294 . '<input type="hidden" name="sr_take_action" value="true" />'
295 . '<div class="item">'
296 . '<label for="select_pred_username">'
297 . ' ' . $GLOBALS['strUserName'] . ':'
298 . '</label>'
299 . '<span class="options">'
300 . ' <select name="pred_username" id="select_pred_username" title="' . $GLOBALS['strUserName'] . '"'
301 . ' onchange="if (this.value == \'any\') { username.value = \'\'; } else if (this.value == \'userdefined\') { username.focus(); username.select(); }">'
302 . ' <option value="any"' . ((isset($GLOBALS['pred_username']) && $GLOBALS['pred_username'] == 'any') ? ' selected="selected"' : '') . '>' . $GLOBALS['strAnyUser'] . '</option>'
303 . ' <option value="userdefined"' . ((! isset($GLOBALS['pred_username']) || $GLOBALS['pred_username'] == 'userdefined') ? ' selected="selected"' : '') . '>' . $GLOBALS['strUseTextField'] . ':</option>'
304 . ' </select>'
305 . '</span>'
306 . '<input type="text" name="username" maxlength="'
307 . $username_length . '" title="' . $GLOBALS['strUserName'] . '"'
308 . (empty($GLOBALS['username'])
309 ? ''
310 : ' value="' . (isset($GLOBALS['new_username'])
311 ? $GLOBALS['new_username']
312 : $GLOBALS['username']) . '"')
313 . ' onchange="pred_username.value = \'userdefined\';" />'
314 . '</div>'
315 . '<div class="item">'
316 . '<label for="select_pred_hostname">'
317 . ' ' . $GLOBALS['strHost'] . ':'
318 . '</label>'
319 . '<span class="options">'
320 . ' <select name="pred_hostname" id="select_pred_hostname" title="' . $GLOBALS['strHost'] . '"';
321 $_current_user = PMA_DBI_fetch_value('SELECT USER();');
322 if (! empty($_current_user)) {
323 $thishost = str_replace("'", '', substr($_current_user, (strrpos($_current_user, '@') + 1)));
324 if ($thishost == 'localhost' || $thishost == '127.0.0.1') {
325 unset($thishost);
328 echo ' onchange="if (this.value == \'any\') { hostname.value = \'%\'; } else if (this.value == \'localhost\') { hostname.value = \'localhost\'; } '
329 . (empty($thishost) ? '' : 'else if (this.value == \'thishost\') { hostname.value = \'' . addslashes(htmlspecialchars($thishost)) . '\'; } ')
330 . 'else if (this.value == \'hosttable\') { hostname.value = \'\'; } else if (this.value == \'userdefined\') { hostname.focus(); hostname.select(); }">' . "\n";
331 unset($_current_user);
333 // when we start editing a user, $GLOBALS['pred_hostname'] is not defined
334 if (! isset($GLOBALS['pred_hostname']) && isset($GLOBALS['hostname'])) {
335 switch (strtolower($GLOBALS['hostname'])) {
336 case 'localhost':
337 case '127.0.0.1':
338 $GLOBALS['pred_hostname'] = 'localhost';
339 break;
340 case '%':
341 $GLOBALS['pred_hostname'] = 'any';
342 break;
343 default:
344 $GLOBALS['pred_hostname'] = 'userdefined';
345 break;
348 echo ' <option value="any"'
349 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'any')
350 ? ' selected="selected"' : '') . '>' . $GLOBALS['strAnyHost']
351 . '</option>'
352 . ' <option value="localhost"'
353 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'localhost')
354 ? ' selected="selected"' : '') . '>' . $GLOBALS['strLocalhost']
355 . '</option>';
356 if (!empty($thishost)) {
357 echo ' <option value="thishost"'
358 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'thishost')
359 ? ' selected="selected"' : '') . '>' . $GLOBALS['strThisHost']
360 . '</option>';
362 unset($thishost);
363 echo ' <option value="hosttable"'
364 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'hosttable')
365 ? ' selected="selected"' : '') . '>' . $GLOBALS['strUseHostTable']
366 . '</option>'
367 . ' <option value="userdefined"'
368 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'userdefined')
369 ? ' selected="selected"' : '')
370 . '>' . $GLOBALS['strUseTextField'] . ':</option>'
371 . ' </select>'
372 . '</span>'
373 . '<input type="text" name="hostname" maxlength="'
374 . $hostname_length . '" value="'
375 . (isset($GLOBALS['hostname']) ? $GLOBALS['hostname'] : '')
376 . '" title="' . $GLOBALS['strHost']
377 . '" onchange="pred_hostname.value = \'userdefined\';" />'
378 . PMA_showHint($GLOBALS['strHostTableExplanation'])
379 . '</div>'
380 . '<div class="item">'
381 . '<label for="select_pred_password">'
382 . ' ' . $GLOBALS['strPassword'] . ':'
383 . '</label>'
384 . '<span class="options">'
385 . ' <select name="pred_password" id="select_pred_password" title="'
386 . $GLOBALS['strPassword'] . '"'
387 . ' onchange="if (this.value == \'none\') { pma_pw.value = \'\'; pma_pw2.value = \'\'; } else if (this.value == \'userdefined\') { pma_pw.focus(); pma_pw.select(); }">'
388 . ' <option value="none"';
389 if (isset($GLOBALS['username']) && $mode != 'change') {
390 echo ' selected="selected"';
392 echo '>' . $GLOBALS['strNoPassword'] . '</option>'
393 . ' <option value="userdefined"' . (isset($GLOBALS['username']) ? '' : ' selected="selected"') . '>' . $GLOBALS['strUseTextField'] . ':</option>'
394 . ' </select>'
395 . '</span>'
396 . '<input type="password" id="text_pma_pw" name="pma_pw" title="' . $GLOBALS['strPassword'] . '" onchange="pred_password.value = \'userdefined\';" />'
397 . '</div>'
398 . '<div class="item">'
399 . '<label for="text_pma_pw2">'
400 . ' ' . $GLOBALS['strReType'] . ':'
401 . '</label>'
402 . '<span class="options">&nbsp;</span>'
403 . '<input type="password" name="pma_pw2" id="text_pma_pw2" title="' . $GLOBALS['strReType'] . '" onchange="pred_password.value = \'userdefined\';" />'
404 . '</div>'
405 . '<div class="item">'
406 . '<label for="button_generate_password">'
407 . ' ' . $GLOBALS['strGeneratePassword'] . ':'
408 . '</label>'
409 . '<span class="options">'
410 . ' <input type="button" id="button_generate_password" value="' . $GLOBALS['strGenerate'] . '" onclick="suggestPassword(this.form)" />'
411 . '</span>'
412 . '<input type="text" name="generated_pw" id="generated_pw" />'
413 . '</div>'
414 . '</fieldset>';
415 echo '<fieldset id="fieldset_user_privtable_footer" class="tblFooters">'
416 . ' <input type="submit" name="adduser_submit" id="adduser_submit" value="' . $GLOBALS['strGo'] . '" />'
417 . '</fieldset>';
418 echo '</form>';
419 echo '</div>';