PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/phpmyadmin/libraries/replication_gui.lib.php

https://bitbucket.org/adarshj/convenient_website
PHP | 380 lines | 305 code | 32 blank | 43 comment | 44 complexity | 5d210dbc3f563ff21066abcd5edbafc9 MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-2-Clause, GPL-2.0, LGPL-3.0
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. *
  5. * @package PhpMyAdmin
  6. */
  7. if (! defined('PHPMYADMIN')) {
  8. exit;
  9. }
  10. /**
  11. * returns code for selecting databases
  12. *
  13. * @return String HTML code
  14. */
  15. function PMA_replication_db_multibox()
  16. {
  17. $multi_values = '';
  18. $multi_values .= '<select name="db_select[]" size="6" multiple="multiple" id="db_select">';
  19. foreach ($GLOBALS['pma']->databases as $current_db) {
  20. if (PMA_is_system_schema($current_db)) {
  21. continue;
  22. }
  23. if (! empty($selectall) || (isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $current_db . '|'))) {
  24. $is_selected = ' selected="selected"';
  25. } else {
  26. $is_selected = '';
  27. }
  28. $current_db = htmlspecialchars($current_db);
  29. $multi_values .= ' <option value="' . $current_db . '" ' . $is_selected . '>' . $current_db . '</option>';
  30. } // end while
  31. $multi_values .= '</select>';
  32. $multi_values .= '<br /><a href="#" id="db_reset_href">' . __('Uncheck All') . '</a>';
  33. return $multi_values;
  34. }
  35. /**
  36. * prints out code for changing master
  37. *
  38. * @param String $submitname - submit button name
  39. */
  40. function PMA_replication_gui_changemaster($submitname)
  41. {
  42. list($username_length, $hostname_length) = PMA_replication_get_username_hostname_length();
  43. echo '<form method="post" action="server_replication.php">';
  44. echo PMA_generate_common_hidden_inputs('', '');
  45. echo ' <fieldset id="fieldset_add_user_login">';
  46. echo ' <legend>' . __('Slave configuration') . ' - ' . __('Change or reconfigure master server') . '</legend>';
  47. 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 />';
  48. echo '<pre>server-id=' . time() . '</pre>';
  49. echo ' <div class="item">';
  50. echo ' <label for="text_username">' . __('User name') . ':</label>';
  51. echo ' <input type="text" name="username" id="text_username" maxlength="'. $username_length . '" title="' . __('User name') . '" />';
  52. echo ' </div>';
  53. echo ' <div class="item">';
  54. echo ' <label for="text_pma_pw">' . __('Password') .' :</label>';
  55. echo ' <input type="password" id="text_pma_pw" name="pma_pw" title="' . __('Password') . '" />';
  56. echo ' </div>';
  57. echo ' <div class="item">';
  58. echo ' <label for="text_hostname">' . __('Host') . ' :</label>';
  59. echo ' <input type="text" id="text_hostname" name="hostname" maxlength="' . $hostname_length . '" value="" />';
  60. echo ' </div>';
  61. echo ' <div class="item">';
  62. echo ' <label for="text_port">' . __('Port') . ':</label>';
  63. echo ' <input type="text" id="text_port" name="port" maxlength="6" value="3306" />';
  64. echo ' </div>';
  65. echo ' </fieldset>';
  66. echo ' <fieldset id="fieldset_user_privtable_footer" class="tblFooters">';
  67. echo ' <input type="hidden" name="sr_take_action" value="true" />';
  68. echo ' <input type="submit" name="' . $submitname . '" id="confslave_submit" value="' . __('Go') . '" />';
  69. echo ' </fieldset>';
  70. echo '</form>';
  71. }
  72. /**
  73. * This function prints out table with replication status.
  74. *
  75. * @param string $type either master or slave
  76. * @param boolean $hidden if true, then default style is set to hidden, default value false
  77. * @param boolen $title if true, then title is displayed, default true
  78. */
  79. function PMA_replication_print_status_table($type, $hidden = false, $title = true)
  80. {
  81. global ${"{$type}_variables"};
  82. global ${"{$type}_variables_alerts"};
  83. global ${"{$type}_variables_oks"};
  84. global ${"server_{$type}_replication"};
  85. global ${"strReplicationStatus_{$type}"};
  86. // TODO check the Masters server id?
  87. // seems to default to '1' when queried via SHOW VARIABLES , but resulted in error on the master when slave connects
  88. // [ERROR] Error reading packet from server: Misconfigured master - server id was not set ( server_errno=1236)
  89. // [ERROR] Got fatal error 1236: 'Misconfigured master - server id was not set' from master when reading data from binary log
  90. //
  91. //$server_id = PMA_DBI_fetch_value("SHOW VARIABLES LIKE 'server_id'", 0, 1);
  92. echo '<div id="replication_' . $type . '_section" style="' . ($hidden ? 'display: none;' : '') . '"> ';
  93. if ($title) {
  94. if ($type == 'master') {
  95. echo '<h4><a name="replication_' . $type . '"></a>' . __('Master status') . '</h4>';
  96. } else {
  97. echo '<h4><a name="replication_' . $type . '"></a>' . __('Slave status') . '</h4>';
  98. }
  99. } else {
  100. echo '<br />';
  101. }
  102. echo ' <table id="server' . $type . 'replicationsummary" class="data"> ';
  103. echo ' <thead>';
  104. echo ' <tr>';
  105. echo ' <th>' . __('Variable') . '</th>';
  106. echo ' <th>' . __('Value') . '</th>';
  107. echo ' </tr>';
  108. echo ' </thead>';
  109. echo ' <tbody>';
  110. $odd_row = true;
  111. foreach (${"{$type}_variables"} as $variable) {
  112. echo ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">';
  113. echo ' <td class="name">';
  114. echo $variable;
  115. echo ' </td>';
  116. echo ' <td class="value">';
  117. // TODO change to regexp or something, to allow for negative match
  118. if (isset(${"{$type}_variables_alerts"}[$variable])
  119. && ${"{$type}_variables_alerts"}[$variable] == ${"server_{$type}_replication"}[0][$variable]
  120. ) {
  121. echo '<span class="attention">';
  122. } elseif (isset(${"{$type}_variables_oks"}[$variable])
  123. && ${"{$type}_variables_oks"}[$variable] == ${"server_{$type}_replication"}[0][$variable]
  124. ) {
  125. echo '<span class="allfine">';
  126. } else {
  127. echo '<span>';
  128. }
  129. echo ${"server_{$type}_replication"}[0][$variable];
  130. echo '</span>';
  131. echo ' </td>';
  132. echo ' </tr>';
  133. $odd_row = ! $odd_row;
  134. }
  135. echo ' </tbody>';
  136. echo ' </table>';
  137. echo ' <br />';
  138. echo '</div>';
  139. }
  140. /**
  141. * Prints table with slave users connected to this master
  142. *
  143. * @param boolean $hidden - if true, then default style is set to hidden, default value false
  144. */
  145. function PMA_replication_print_slaves_table($hidden = false)
  146. {
  147. // Fetch data
  148. $data = PMA_DBI_fetch_result('SHOW SLAVE HOSTS', null, null);
  149. echo ' <br />';
  150. echo ' <div id="replication_slaves_section" style="' . ($hidden ? 'display: none;' : '') . '"> ';
  151. echo ' <table class="data">';
  152. echo ' <thead>';
  153. echo ' <tr>';
  154. echo ' <th>' . __('Server ID') . '</th>';
  155. echo ' <th>' . __('Host') . '</th>';
  156. echo ' </tr>';
  157. echo ' </thead>';
  158. echo ' <tbody>';
  159. $odd_row = true;
  160. foreach ($data as $slave) {
  161. echo ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">';
  162. echo ' <td class="value">' . $slave['Server_id'] . '</td>';
  163. echo ' <td class="value">' . $slave['Host'] . '</td>';
  164. echo ' </tr>';
  165. $odd_row = ! $odd_row;
  166. }
  167. echo ' </tbody>';
  168. echo ' </table>';
  169. echo ' <br />';
  170. PMA_Message::notice(__('Only slaves started with the --report-host=host_name option are visible in this list.'))->display();
  171. echo ' <br />';
  172. echo ' </div>';
  173. }
  174. /**
  175. * get the correct username and hostname lengths for this MySQL server
  176. *
  177. * @return array username length, hostname length
  178. */
  179. function PMA_replication_get_username_hostname_length()
  180. {
  181. $fields_info = PMA_DBI_get_columns('mysql', 'user');
  182. $username_length = 16;
  183. $hostname_length = 41;
  184. foreach ($fields_info as $val) {
  185. if ($val['Field'] == 'User') {
  186. strtok($val['Type'], '()');
  187. $v = strtok('()');
  188. if (is_int($v)) {
  189. $username_length = $v;
  190. }
  191. } elseif ($val['Field'] == 'Host') {
  192. strtok($val['Type'], '()');
  193. $v = strtok('()');
  194. if (is_int($v)) {
  195. $hostname_length = $v;
  196. }
  197. }
  198. }
  199. return array($username_length, $hostname_length);
  200. }
  201. /**
  202. * Print code to add a replication slave user to the master
  203. */
  204. function PMA_replication_gui_master_addslaveuser()
  205. {
  206. list($username_length, $hostname_length) = PMA_replication_get_username_hostname_length();
  207. if (isset($GLOBALS['username']) && strlen($GLOBALS['username']) === 0) {
  208. $GLOBALS['pred_username'] = 'any';
  209. }
  210. echo '<div id="master_addslaveuser_gui">';
  211. echo '<form autocomplete="off" method="post" action="server_privileges.php" onsubmit="return checkAddUser(this);">';
  212. echo PMA_generate_common_hidden_inputs('', '');
  213. echo '<fieldset id="fieldset_add_user_login">'
  214. . '<legend>'.__('Add slave replication user').'</legend>'
  215. . '<input type="hidden" name="grant_count" value="25" />'
  216. . '<input type="hidden" name="createdb" id="createdb_0" value="0" />'
  217. . '<input id="checkbox_Repl_slave_priv" type="hidden" title="Needed for the replication slaves." value="Y" name="Repl_slave_priv"/>'
  218. . '<input id="checkbox_Repl_client_priv" type="hidden" title="Needed for the replication slaves." value="Y" name="Repl_client_priv"/>'
  219. . ''
  220. . '<input type="hidden" name="sr_take_action" value="true" />'
  221. . '<div class="item">'
  222. . '<label for="select_pred_username">'
  223. . ' ' . __('User name') . ':'
  224. . '</label>'
  225. . '<span class="options">'
  226. . ' <select name="pred_username" id="select_pred_username" title="' . __('User name') . '"'
  227. . ' onchange="if (this.value == \'any\') { username.value = \'\'; } else if (this.value == \'userdefined\') { username.focus(); username.select(); }">'
  228. . ' <option value="any"' . ((isset($GLOBALS['pred_username']) && $GLOBALS['pred_username'] == 'any') ? ' selected="selected"' : '') . '>' . __('Any user') . '</option>'
  229. . ' <option value="userdefined"' . ((! isset($GLOBALS['pred_username']) || $GLOBALS['pred_username'] == 'userdefined') ? ' selected="selected"' : '') . '>' . __('Use text field') . ':</option>'
  230. . ' </select>'
  231. . '</span>'
  232. . '<input type="text" name="username" maxlength="'
  233. . $username_length . '" title="' . __('User name') . '"'
  234. . (empty($GLOBALS['username'])
  235. ? ''
  236. : ' value="' . (isset($GLOBALS['new_username'])
  237. ? $GLOBALS['new_username']
  238. : $GLOBALS['username']) . '"')
  239. . ' onchange="pred_username.value = \'userdefined\';" />'
  240. . '</div>'
  241. . '<div class="item">'
  242. . '<label for="select_pred_hostname">'
  243. . ' ' . __('Host') . ':'
  244. . '</label>'
  245. . '<span class="options">'
  246. . ' <select name="pred_hostname" id="select_pred_hostname" title="' . __('Host') . '"';
  247. $_current_user = PMA_DBI_fetch_value('SELECT USER();');
  248. if (! empty($_current_user)) {
  249. $thishost = str_replace("'", '', substr($_current_user, (strrpos($_current_user, '@') + 1)));
  250. if ($thishost == 'localhost' || $thishost == '127.0.0.1') {
  251. unset($thishost);
  252. }
  253. }
  254. echo ' onchange="if (this.value == \'any\') { hostname.value = \'%\'; } else if (this.value == \'localhost\') { hostname.value = \'localhost\'; } '
  255. . (empty($thishost) ? '' : 'else if (this.value == \'thishost\') { hostname.value = \'' . addslashes(htmlspecialchars($thishost)) . '\'; } ')
  256. . 'else if (this.value == \'hosttable\') { hostname.value = \'\'; } else if (this.value == \'userdefined\') { hostname.focus(); hostname.select(); }">' . "\n";
  257. unset($_current_user);
  258. // when we start editing a user, $GLOBALS['pred_hostname'] is not defined
  259. if (! isset($GLOBALS['pred_hostname']) && isset($GLOBALS['hostname'])) {
  260. switch (strtolower($GLOBALS['hostname'])) {
  261. case 'localhost':
  262. case '127.0.0.1':
  263. $GLOBALS['pred_hostname'] = 'localhost';
  264. break;
  265. case '%':
  266. $GLOBALS['pred_hostname'] = 'any';
  267. break;
  268. default:
  269. $GLOBALS['pred_hostname'] = 'userdefined';
  270. break;
  271. }
  272. }
  273. echo ' <option value="any"'
  274. . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'any')
  275. ? ' selected="selected"' : '') . '>' . __('Any host')
  276. . '</option>'
  277. . ' <option value="localhost"'
  278. . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'localhost')
  279. ? ' selected="selected"' : '') . '>' . __('Local')
  280. . '</option>';
  281. if (!empty($thishost)) {
  282. echo ' <option value="thishost"'
  283. . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'thishost')
  284. ? ' selected="selected"' : '') . '>' . __('This Host')
  285. . '</option>';
  286. }
  287. unset($thishost);
  288. echo ' <option value="hosttable"'
  289. . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'hosttable')
  290. ? ' selected="selected"' : '') . '>' . __('Use Host Table')
  291. . '</option>'
  292. . ' <option value="userdefined"'
  293. . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'userdefined')
  294. ? ' selected="selected"' : '')
  295. . '>' . __('Use text field') . ':</option>'
  296. . ' </select>'
  297. . '</span>'
  298. . '<input type="text" name="hostname" maxlength="'
  299. . $hostname_length . '" value="'
  300. . (isset($GLOBALS['hostname']) ? $GLOBALS['hostname'] : '')
  301. . '" title="' . __('Host')
  302. . '" onchange="pred_hostname.value = \'userdefined\';" />'
  303. . PMA_showHint(__('When Host table is used, this field is ignored and values stored in Host table are used instead.'))
  304. . '</div>'
  305. . '<div class="item">'
  306. . '<label for="select_pred_password">'
  307. . ' ' . __('Password') . ':'
  308. . '</label>'
  309. . '<span class="options">'
  310. . ' <select name="pred_password" id="select_pred_password" title="'
  311. . __('Password') . '"'
  312. . ' onchange="if (this.value == \'none\') { pma_pw.value = \'\'; pma_pw2.value = \'\'; } else if (this.value == \'userdefined\') { pma_pw.focus(); pma_pw.select(); }">'
  313. . ' <option value="none"';
  314. if (isset($GLOBALS['username']) && $mode != 'change') {
  315. echo ' selected="selected"';
  316. }
  317. echo '>' . __('No Password') . '</option>'
  318. . ' <option value="userdefined"' . (isset($GLOBALS['username']) ? '' : ' selected="selected"') . '>' . __('Use text field') . ':</option>'
  319. . ' </select>'
  320. . '</span>'
  321. . '<input type="password" id="text_pma_pw" name="pma_pw" title="' . __('Password') . '" onchange="pred_password.value = \'userdefined\';" />'
  322. . '</div>'
  323. . '<div class="item">'
  324. . '<label for="text_pma_pw2">'
  325. . ' ' . __('Re-type') . ':'
  326. . '</label>'
  327. . '<span class="options">&nbsp;</span>'
  328. . '<input type="password" name="pma_pw2" id="text_pma_pw2" title="' . __('Re-type') . '" onchange="pred_password.value = \'userdefined\';" />'
  329. . '</div>'
  330. . '<div class="item">'
  331. . '<label for="button_generate_password">'
  332. . ' ' . __('Generate Password') . ':'
  333. . '</label>'
  334. . '<span class="options">'
  335. . ' <input type="button" id="button_generate_password" value="' . __('Generate') . '" onclick="suggestPassword(this.form)" />'
  336. . '</span>'
  337. . '<input type="text" name="generated_pw" id="generated_pw" />'
  338. . '</div>'
  339. . '</fieldset>';
  340. echo '<fieldset id="fieldset_user_privtable_footer" class="tblFooters">'
  341. . ' <input type="submit" name="adduser_submit" id="adduser_submit" value="' . __('Go') . '" />'
  342. . '</fieldset>';
  343. echo '</form>';
  344. echo '</div>';
  345. }
  346. ?>