ReplicationGui.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Functions for the replication GUI
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin;
  9. use PhpMyAdmin\Core;
  10. use PhpMyAdmin\Message;
  11. use PhpMyAdmin\Replication;
  12. use PhpMyAdmin\Response;
  13. use PhpMyAdmin\Url;
  14. use PhpMyAdmin\Util;
  15. /**
  16. * PhpMyAdmin\ReplicationGui class
  17. *
  18. * @package PhpMyAdmin
  19. */
  20. class ReplicationGui
  21. {
  22. /**
  23. * returns HTML for error message
  24. *
  25. * @return String HTML code
  26. */
  27. public static function getHtmlForErrorMessage()
  28. {
  29. $html = '';
  30. if (isset($_SESSION['replication']['sr_action_status'])
  31. && isset($_SESSION['replication']['sr_action_info'])
  32. ) {
  33. if ($_SESSION['replication']['sr_action_status'] == 'error') {
  34. $error_message = $_SESSION['replication']['sr_action_info'];
  35. $html .= Message::error($error_message)->getDisplay();
  36. $_SESSION['replication']['sr_action_status'] = 'unknown';
  37. } elseif ($_SESSION['replication']['sr_action_status'] == 'success') {
  38. $success_message = $_SESSION['replication']['sr_action_info'];
  39. $html .= Message::success($success_message)->getDisplay();
  40. $_SESSION['replication']['sr_action_status'] = 'unknown';
  41. }
  42. }
  43. return $html;
  44. }
  45. /**
  46. * returns HTML for master replication
  47. *
  48. * @return String HTML code
  49. */
  50. public static function getHtmlForMasterReplication()
  51. {
  52. $html = '';
  53. if (! isset($_POST['repl_clear_scr'])) {
  54. $html .= '<fieldset>';
  55. $html .= '<legend>' . __('Master replication') . '</legend>';
  56. $html .= __('This server is configured as master in a replication process.');
  57. $html .= '<ul>';
  58. $html .= ' <li><a href="#master_status_href" id="master_status_href">';
  59. $html .= __('Show master status') . '</a>';
  60. $html .= self::getHtmlForReplicationStatusTable('master', true, false);
  61. $html .= ' </li>';
  62. $html .= ' <li><a href="#master_slaves_href" id="master_slaves_href">';
  63. $html .= __('Show connected slaves') . '</a>';
  64. $html .= self::getHtmlForReplicationSlavesTable(true);
  65. $html .= ' </li>';
  66. $_url_params = $GLOBALS['url_params'];
  67. $_url_params['mr_adduser'] = true;
  68. $_url_params['repl_clear_scr'] = true;
  69. $html .= ' <li><a href="server_replication.php" data-post="';
  70. $html .= Url::getCommon($_url_params, '', false)
  71. . '" id="master_addslaveuser_href">';
  72. $html .= __('Add slave replication user') . '</a></li>';
  73. }
  74. // Display 'Add replication slave user' form
  75. if (isset($_POST['mr_adduser'])) {
  76. $html .= self::getHtmlForReplicationMasterAddSlaveUser();
  77. } elseif (! isset($_POST['repl_clear_scr'])) {
  78. $html .= "</ul>";
  79. $html .= "</fieldset>";
  80. }
  81. return $html;
  82. }
  83. /**
  84. * returns HTML for master replication configuration
  85. *
  86. * @return String HTML code
  87. */
  88. public static function getHtmlForMasterConfiguration()
  89. {
  90. $html = '<fieldset>';
  91. $html .= '<legend>' . __('Master configuration') . '</legend>';
  92. $html .= __(
  93. 'This server is not configured as a master server in a '
  94. . 'replication process. You can choose from either replicating '
  95. . 'all databases and ignoring some of them (useful if you want to '
  96. . 'replicate a majority of the databases) or you can choose to ignore '
  97. . 'all databases by default and allow only certain databases to be '
  98. . 'replicated. Please select the mode:'
  99. ) . '<br /><br />';
  100. $html .= '<select name="db_type" id="db_type">';
  101. $html .= '<option value="all">' . __('Replicate all databases; Ignore:');
  102. $html .= '</option>';
  103. $html .= '<option value="ign">' . __('Ignore all databases; Replicate:');
  104. $html .= '</option>';
  105. $html .= '</select>';
  106. $html .= '<br /><br />';
  107. $html .= __('Please select databases:') . '<br />';
  108. $html .= self::getHtmlForReplicationDbMultibox();
  109. $html .= '<br /><br />';
  110. $html .= __(
  111. 'Now, add the following lines at the end of [mysqld] section'
  112. . ' in your my.cnf and please restart the MySQL server afterwards.'
  113. ) . '<br />';
  114. $html .= '<pre id="rep"></pre>';
  115. $html .= __(
  116. 'Once you restarted MySQL server, please click on Go button. '
  117. . 'Afterwards, you should see a message informing you, that this server'
  118. . ' <b>is</b> configured as master.'
  119. );
  120. $html .= '</fieldset>';
  121. $html .= '<fieldset class="tblFooters">';
  122. $html .= ' <form method="post" action="server_replication.php" >';
  123. $html .= Url::getHiddenInputs('', '');
  124. $html .= ' <input type="submit" value="' . __('Go') . '" id="goButton" />';
  125. $html .= ' </form>';
  126. $html .= '</fieldset>';
  127. return $html;
  128. }
  129. /**
  130. * returns HTML for slave replication configuration
  131. *
  132. * @param bool $server_slave_status Whether it is Master or Slave
  133. * @param array $server_slave_replication Slave replication
  134. *
  135. * @return String HTML code
  136. */
  137. public static function getHtmlForSlaveConfiguration(
  138. $server_slave_status, array $server_slave_replication
  139. ) {
  140. $html = '<fieldset>';
  141. $html .= '<legend>' . __('Slave replication') . '</legend>';
  142. /**
  143. * check for multi-master replication functionality
  144. */
  145. $server_slave_multi_replication = $GLOBALS['dbi']->fetchResult(
  146. 'SHOW ALL SLAVES STATUS'
  147. );
  148. if ($server_slave_multi_replication) {
  149. $html .= __('Master connection:');
  150. $html .= '<form method="get" action="server_replication.php">';
  151. $html .= Url::getHiddenInputs($GLOBALS['url_params']);
  152. $html .= ' <select name="master_connection">';
  153. $html .= '<option value="">' . __('Default') . '</option>';
  154. foreach ($server_slave_multi_replication as $server) {
  155. $html .= '<option' . (isset($_POST['master_connection'])
  156. && $_POST['master_connection'] == $server['Connection_name'] ?
  157. ' selected="selected"' : '') . '>' . $server['Connection_name']
  158. . '</option>';
  159. }
  160. $html .= '</select>';
  161. $html .= ' <input type="submit" value="' . __('Go') . '" id="goButton" />';
  162. $html .= '</form>';
  163. $html .= '<br /><br />';
  164. }
  165. if ($server_slave_status) {
  166. $html .= '<div id="slave_configuration_gui">';
  167. $_url_params = $GLOBALS['url_params'];
  168. $_url_params['sr_take_action'] = true;
  169. $_url_params['sr_slave_server_control'] = true;
  170. if ($server_slave_replication[0]['Slave_IO_Running'] == 'No') {
  171. $_url_params['sr_slave_action'] = 'start';
  172. } else {
  173. $_url_params['sr_slave_action'] = 'stop';
  174. }
  175. $_url_params['sr_slave_control_parm'] = 'IO_THREAD';
  176. $slave_control_io_link = Url::getCommon($_url_params, '', false);
  177. if ($server_slave_replication[0]['Slave_SQL_Running'] == 'No') {
  178. $_url_params['sr_slave_action'] = 'start';
  179. } else {
  180. $_url_params['sr_slave_action'] = 'stop';
  181. }
  182. $_url_params['sr_slave_control_parm'] = 'SQL_THREAD';
  183. $slave_control_sql_link = Url::getCommon($_url_params, '', false);
  184. if ($server_slave_replication[0]['Slave_IO_Running'] == 'No'
  185. || $server_slave_replication[0]['Slave_SQL_Running'] == 'No'
  186. ) {
  187. $_url_params['sr_slave_action'] = 'start';
  188. } else {
  189. $_url_params['sr_slave_action'] = 'stop';
  190. }
  191. $_url_params['sr_slave_control_parm'] = null;
  192. $slave_control_full_link = Url::getCommon($_url_params, '', false);
  193. $_url_params['sr_slave_action'] = 'reset';
  194. $slave_control_reset_link = Url::getCommon($_url_params, '', false);
  195. $_url_params = $GLOBALS['url_params'];
  196. $_url_params['sr_take_action'] = true;
  197. $_url_params['sr_slave_skip_error'] = true;
  198. $slave_skip_error_link = Url::getCommon($_url_params, '', false);
  199. if ($server_slave_replication[0]['Slave_SQL_Running'] == 'No') {
  200. $html .= Message::error(
  201. __('Slave SQL Thread not running!')
  202. )->getDisplay();
  203. }
  204. if ($server_slave_replication[0]['Slave_IO_Running'] == 'No') {
  205. $html .= Message::error(
  206. __('Slave IO Thread not running!')
  207. )->getDisplay();
  208. }
  209. $_url_params = $GLOBALS['url_params'];
  210. $_url_params['sl_configure'] = true;
  211. $_url_params['repl_clear_scr'] = true;
  212. $reconfiguremaster_link = Url::getCommon($_url_params, '', false);
  213. $html .= __(
  214. 'Server is configured as slave in a replication process. Would you ' .
  215. 'like to:'
  216. );
  217. $html .= '<br />';
  218. $html .= '<ul>';
  219. $html .= ' <li><a href="#slave_status_href" id="slave_status_href">';
  220. $html .= __('See slave status table') . '</a>';
  221. $html .= self::getHtmlForReplicationStatusTable('slave', true, false);
  222. $html .= ' </li>';
  223. $html .= ' <li><a href="#slave_control_href" id="slave_control_href">';
  224. $html .= __('Control slave:') . '</a>';
  225. $html .= ' <div id="slave_control_gui" class="hide">';
  226. $html .= ' <ul>';
  227. $html .= ' <li><a href="server_replication.php" data-post="' . $slave_control_full_link . '">';
  228. $html .= (($server_slave_replication[0]['Slave_IO_Running'] == 'No' ||
  229. $server_slave_replication[0]['Slave_SQL_Running'] == 'No')
  230. ? __('Full start')
  231. : __('Full stop')) . ' </a></li>';
  232. $html .= ' <li><a class="ajax" id="reset_slave"'
  233. . ' href="server_replication.php" data-post="' . $slave_control_reset_link . '">';
  234. $html .= __('Reset slave') . '</a></li>';
  235. if ($server_slave_replication[0]['Slave_SQL_Running'] == 'No') {
  236. $html .= ' <li><a href="server_replication.php" data-post="' . $slave_control_sql_link . '">';
  237. $html .= __('Start SQL Thread only') . '</a></li>';
  238. } else {
  239. $html .= ' <li><a href="server_replication.php" data-post="' . $slave_control_sql_link . '">';
  240. $html .= __('Stop SQL Thread only') . '</a></li>';
  241. }
  242. if ($server_slave_replication[0]['Slave_IO_Running'] == 'No') {
  243. $html .= ' <li><a href="server_replication.php" data-post="' . $slave_control_io_link . '">';
  244. $html .= __('Start IO Thread only') . '</a></li>';
  245. } else {
  246. $html .= ' <li><a href="server_replication.php" data-post="' . $slave_control_io_link . '">';
  247. $html .= __('Stop IO Thread only') . '</a></li>';
  248. }
  249. $html .= ' </ul>';
  250. $html .= ' </div>';
  251. $html .= ' </li>';
  252. $html .= ' <li>';
  253. $html .= self::getHtmlForSlaveErrorManagement($slave_skip_error_link);
  254. $html .= ' </li>';
  255. $html .= ' <li><a href="server_replication.php" data-post="' . $reconfiguremaster_link . '">';
  256. $html .= __('Change or reconfigure master server') . '</a></li>';
  257. $html .= '</ul>';
  258. $html .= '</div>';
  259. } elseif (! isset($_POST['sl_configure'])) {
  260. $_url_params = $GLOBALS['url_params'];
  261. $_url_params['sl_configure'] = true;
  262. $_url_params['repl_clear_scr'] = true;
  263. $html .= sprintf(
  264. __(
  265. 'This server is not configured as slave in a replication process. '
  266. . 'Would you like to %sconfigure%s it?'
  267. ),
  268. '<a href="server_replication.php" data-post="' . Url::getCommon($_url_params, '', false) . '">',
  269. '</a>'
  270. );
  271. }
  272. $html .= '</fieldset>';
  273. return $html;
  274. }
  275. /**
  276. * returns HTML for Slave Error Management
  277. *
  278. * @param String $slave_skip_error_link error link
  279. *
  280. * @return String HTML code
  281. */
  282. public static function getHtmlForSlaveErrorManagement($slave_skip_error_link)
  283. {
  284. $html = '<a href="#slave_errormanagement_href" '
  285. . 'id="slave_errormanagement_href">';
  286. $html .= __('Error management:') . '</a>';
  287. $html .= ' <div id="slave_errormanagement_gui" class="hide">';
  288. $html .= Message::error(
  289. __('Skipping errors might lead into unsynchronized master and slave!')
  290. )->getDisplay();
  291. $html .= ' <ul>';
  292. $html .= ' <li><a href="server_replication.php" data-post="' . $slave_skip_error_link . '">';
  293. $html .= __('Skip current error') . '</a></li>';
  294. $html .= ' <li>';
  295. $html .= ' <form method="post" action="server_replication.php">';
  296. $html .= Url::getHiddenInputs('', '');
  297. $html .= sprintf(
  298. __('Skip next %s errors.'),
  299. '<input type="text" name="sr_skip_errors_count" value="1" '
  300. . 'class = "repl_gui_skip_err_cnt" />'
  301. );
  302. $html .= ' <input type="submit" name="sr_slave_skip_error" ';
  303. $html .= 'value="' . __('Go') . '" />';
  304. $html .= ' <input type="hidden" name="sr_take_action" value="1" />';
  305. $html .= ' </form></li>';
  306. $html .= ' </ul>';
  307. $html .= ' </div>';
  308. return $html;
  309. }
  310. /**
  311. * returns HTML for not configure for a server replication
  312. *
  313. * @return String HTML code
  314. */
  315. public static function getHtmlForNotServerReplication()
  316. {
  317. $_url_params = $GLOBALS['url_params'];
  318. $_url_params['mr_configure'] = true;
  319. $html = '<fieldset>';
  320. $html .= '<legend>' . __('Master replication') . '</legend>';
  321. $html .= sprintf(
  322. __(
  323. 'This server is not configured as master in a replication process. '
  324. . 'Would you like to %sconfigure%s it?'
  325. ),
  326. '<a href="server_replication.php" data-post="' . Url::getCommon($_url_params, '', false) . '">',
  327. '</a>'
  328. );
  329. $html .= '</fieldset>';
  330. return $html;
  331. }
  332. /**
  333. * returns HTML code for selecting databases
  334. *
  335. * @return String HTML code
  336. */
  337. public static function getHtmlForReplicationDbMultibox()
  338. {
  339. $multi_values = '';
  340. $multi_values .= '<select name="db_select[]" '
  341. . 'size="6" multiple="multiple" id="db_select" class="width96">';
  342. foreach ($GLOBALS['dblist']->databases as $current_db) {
  343. if ($GLOBALS['dbi']->isSystemSchema($current_db)) {
  344. continue;
  345. }
  346. $current_db = htmlspecialchars($current_db);
  347. $multi_values .= ' <option value="' . $current_db . '" ';
  348. $multi_values .= '>';
  349. $multi_values .= $current_db . '</option>';
  350. } // end while
  351. $multi_values .= '</select><br />';
  352. $multi_values .= '<a href="#" id="db_select_href">' . __('Select all') . '</a>';
  353. $multi_values .= '&nbsp;/&nbsp;';
  354. $multi_values .= '<a href="#" id="db_reset_href">' . __('Unselect all') . '</a>';
  355. return $multi_values;
  356. }
  357. /**
  358. * returns HTML for changing master
  359. *
  360. * @param String $submitname - submit button name
  361. *
  362. * @return String HTML code
  363. */
  364. public static function getHtmlForReplicationChangeMaster($submitname)
  365. {
  366. $html = '';
  367. list($username_length, $hostname_length)
  368. = self::getUsernameHostnameLength();
  369. $html .= '<form method="post" action="server_replication.php">';
  370. $html .= Url::getHiddenInputs('', '');
  371. $html .= ' <fieldset id="fieldset_add_user_login">';
  372. $html .= ' <legend>' . __('Slave configuration');
  373. $html .= ' - ' . __('Change or reconfigure master server') . '</legend>';
  374. $html .= __(
  375. 'Make sure you have a unique server-id in your configuration file (my.cnf). '
  376. . 'If not, please add the following line into [mysqld] section:'
  377. );
  378. $html .= '<br />';
  379. $html .= '<pre>server-id=' . time() . '</pre>';
  380. $html .= self::getHtmlForAddUserInputDiv(
  381. array('text'=>__('User name:'), 'for'=>"text_username"),
  382. array(
  383. 'type'=>'text',
  384. 'name'=>'username',
  385. 'id'=>'text_username',
  386. 'maxlength'=>$username_length,
  387. 'title'=>__('User name'),
  388. 'required'=>'required'
  389. )
  390. );
  391. $html .= self::getHtmlForAddUserInputDiv(
  392. array('text'=>__('Password:'), 'for'=>"text_pma_pw"),
  393. array(
  394. 'type'=>'password',
  395. 'name'=>'pma_pw',
  396. 'id'=>'text_pma_pw',
  397. 'title'=>__('Password'),
  398. 'required'=>'required'
  399. )
  400. );
  401. $html .= self::getHtmlForAddUserInputDiv(
  402. array('text'=>__('Host:'), 'for'=>"text_hostname"),
  403. array(
  404. 'type'=>'text',
  405. 'name'=>'hostname',
  406. 'id'=>'text_hostname',
  407. 'maxlength'=>$hostname_length,
  408. 'value'=>'',
  409. 'required'=>'required'
  410. )
  411. );
  412. $html .= self::getHtmlForAddUserInputDiv(
  413. array('text'=>__('Port:'), 'for'=>"text_port"),
  414. array(
  415. 'type'=>'number',
  416. 'name'=>'text_port',
  417. 'id'=>'text_port',
  418. 'maxlength'=>6,
  419. 'value'=>'3306',
  420. 'required'=>'required'
  421. )
  422. );
  423. $html .= ' </fieldset>';
  424. $html .= ' <fieldset id="fieldset_user_privtable_footer" class="tblFooters">';
  425. $html .= ' <input type="hidden" name="sr_take_action" value="true" />';
  426. $html .= ' <input type="hidden" name="' . $submitname . '" value="1" />';
  427. $html .= ' <input type="submit" id="confslave_submit" value="';
  428. $html .= __('Go') . '" />';
  429. $html .= ' </fieldset>';
  430. $html .= '</form>';
  431. return $html;
  432. }
  433. /**
  434. * returns HTML code for Add user input div
  435. *
  436. * @param array $label_array label tag elements
  437. * @param array $input_array input tag elements
  438. *
  439. * @return String HTML code
  440. */
  441. public static function getHtmlForAddUserInputDiv(array $label_array, array $input_array)
  442. {
  443. $html = ' <div class="item">';
  444. $html .= ' <label for="' . $label_array['for'] . '">';
  445. $html .= $label_array['text'] . '</label>';
  446. $html .= ' <input ';
  447. foreach ($input_array as $key=>$value) {
  448. $html .= ' ' . $key . '="' . $value . '" ';
  449. }
  450. $html .= ' />';
  451. $html .= ' </div>';
  452. return $html;
  453. }
  454. /**
  455. * This function returns html code for table with replication status.
  456. *
  457. * @param string $type either master or slave
  458. * @param boolean $hidden if true, then default style is set to hidden,
  459. * default value false
  460. * @param boolean $title if true, then title is displayed, default true
  461. *
  462. * @return String HTML code
  463. */
  464. public static function getHtmlForReplicationStatusTable($type, $hidden = false, $title = true)
  465. {
  466. global ${"{$type}_variables"};
  467. global ${"{$type}_variables_alerts"};
  468. global ${"{$type}_variables_oks"};
  469. global ${"server_{$type}_replication"};
  470. global ${"strReplicationStatus_{$type}"};
  471. $html = '';
  472. // TODO check the Masters server id?
  473. // seems to default to '1' when queried via SHOW VARIABLES ,
  474. // but resulted in error on the master when slave connects
  475. // [ERROR] Error reading packet from server: Misconfigured master
  476. // - server id was not set ( server_errno=1236)
  477. // [ERROR] Got fatal error 1236: 'Misconfigured master
  478. // - server id was not set' from master when reading data from binary log
  479. //
  480. //$server_id = $GLOBALS['dbi']->fetchValue(
  481. // "SHOW VARIABLES LIKE 'server_id'", 0, 1
  482. //);
  483. $html .= '<div id="replication_' . $type . '_section" style="';
  484. $html .= ($hidden ? 'display: none;' : '') . '"> ';
  485. if ($title) {
  486. if ($type == 'master') {
  487. $html .= '<h4><a name="replication_' . $type . '"></a>';
  488. $html .= __('Master status') . '</h4>';
  489. } else {
  490. $html .= '<h4><a name="replication_' . $type . '"></a>';
  491. $html .= __('Slave status') . '</h4>';
  492. }
  493. } else {
  494. $html .= '<br />';
  495. }
  496. $html .= ' <table id="server' . $type . 'replicationsummary" class="data"> ';
  497. $html .= ' <thead>';
  498. $html .= ' <tr>';
  499. $html .= ' <th>' . __('Variable') . '</th>';
  500. $html .= ' <th>' . __('Value') . '</th>';
  501. $html .= ' </tr>';
  502. $html .= ' </thead>';
  503. $html .= ' <tbody>';
  504. foreach (${"{$type}_variables"} as $variable) {
  505. $serverReplicationVariable =
  506. is_array(${"server_{$type}_replication"}) && isset(${"server_{$type}_replication"}[0])
  507. ? ${"server_{$type}_replication"}[0][$variable] : '';
  508. $html .= ' <tr>';
  509. $html .= ' <td class="name">';
  510. $html .= htmlspecialchars($variable);
  511. $html .= ' </td>';
  512. $html .= ' <td class="value">';
  513. // TODO change to regexp or something, to allow for negative match
  514. if (isset(${"{$type}_variables_alerts"}[$variable])
  515. && ${"{$type}_variables_alerts"}[$variable] == $serverReplicationVariable
  516. ) {
  517. $html .= '<span class="attention">';
  518. } elseif (isset(${"{$type}_variables_oks"}[$variable])
  519. && ${"{$type}_variables_oks"}[$variable] == $serverReplicationVariable
  520. ) {
  521. $html .= '<span class="allfine">';
  522. } else {
  523. $html .= '<span>';
  524. }
  525. // allow wrapping long table lists into multiple lines
  526. static $variables_wrap = array(
  527. 'Replicate_Do_DB', 'Replicate_Ignore_DB',
  528. 'Replicate_Do_Table', 'Replicate_Ignore_Table',
  529. 'Replicate_Wild_Do_Table', 'Replicate_Wild_Ignore_Table');
  530. if (in_array($variable, $variables_wrap)) {
  531. $html .= htmlspecialchars(str_replace(
  532. ',',
  533. ', ',
  534. $serverReplicationVariable
  535. ));
  536. } else {
  537. $html .= htmlspecialchars($serverReplicationVariable);
  538. }
  539. $html .= '</span>';
  540. $html .= ' </td>';
  541. $html .= ' </tr>';
  542. }
  543. $html .= ' </tbody>';
  544. $html .= ' </table>';
  545. $html .= ' <br />';
  546. $html .= '</div>';
  547. return $html;
  548. }
  549. /**
  550. * returns html code for table with slave users connected to this master
  551. *
  552. * @param boolean $hidden - if true, then default style is set to hidden,
  553. * - default value false
  554. *
  555. * @return string
  556. */
  557. public static function getHtmlForReplicationSlavesTable($hidden = false)
  558. {
  559. $html = '';
  560. // Fetch data
  561. $data = $GLOBALS['dbi']->fetchResult('SHOW SLAVE HOSTS', null, null);
  562. $html .= ' <br />';
  563. $html .= ' <div id="replication_slaves_section" style="';
  564. $html .= ($hidden ? 'display: none;' : '') . '"> ';
  565. $html .= ' <table class="data">';
  566. $html .= ' <thead>';
  567. $html .= ' <tr>';
  568. $html .= ' <th>' . __('Server ID') . '</th>';
  569. $html .= ' <th>' . __('Host') . '</th>';
  570. $html .= ' </tr>';
  571. $html .= ' </thead>';
  572. $html .= ' <tbody>';
  573. foreach ($data as $slave) {
  574. $html .= ' <tr>';
  575. $html .= ' <td class="value">' . $slave['Server_id'] . '</td>';
  576. $html .= ' <td class="value">' . $slave['Host'] . '</td>';
  577. $html .= ' </tr>';
  578. }
  579. $html .= ' </tbody>';
  580. $html .= ' </table>';
  581. $html .= ' <br />';
  582. $html .= Message::notice(
  583. __(
  584. 'Only slaves started with the '
  585. . '--report-host=host_name option are visible in this list.'
  586. )
  587. )->getDisplay();
  588. $html .= ' <br />';
  589. $html .= ' </div>';
  590. return $html;
  591. }
  592. /**
  593. * get the correct username and hostname lengths for this MySQL server
  594. *
  595. * @return array username length, hostname length
  596. */
  597. public static function getUsernameHostnameLength()
  598. {
  599. $fields_info = $GLOBALS['dbi']->getColumns('mysql', 'user');
  600. $username_length = 16;
  601. $hostname_length = 41;
  602. foreach ($fields_info as $val) {
  603. if ($val['Field'] == 'User') {
  604. strtok($val['Type'], '()');
  605. $v = strtok('()');
  606. if (is_int($v)) {
  607. $username_length = $v;
  608. }
  609. } elseif ($val['Field'] == 'Host') {
  610. strtok($val['Type'], '()');
  611. $v = strtok('()');
  612. if (is_int($v)) {
  613. $hostname_length = $v;
  614. }
  615. }
  616. }
  617. return array($username_length, $hostname_length);
  618. }
  619. /**
  620. * returns html code to add a replication slave user to the master
  621. *
  622. * @return String HTML code
  623. */
  624. public static function getHtmlForReplicationMasterAddSlaveUser()
  625. {
  626. $html = '';
  627. list($username_length, $hostname_length)
  628. = self::getUsernameHostnameLength();
  629. if (isset($_POST['username']) && strlen($_POST['username']) === 0) {
  630. $GLOBALS['pred_username'] = 'any';
  631. }
  632. $html .= '<div id="master_addslaveuser_gui">';
  633. $html .= '<form autocomplete="off" method="post" ';
  634. $html .= 'action="server_privileges.php"';
  635. $html .= ' onsubmit="return checkAddUser(this);">';
  636. $html .= Url::getHiddenInputs('', '');
  637. $html .= '<fieldset id="fieldset_add_user_login">'
  638. . '<legend>' . __('Add slave replication user') . '</legend>'
  639. . self::getHtmlForAddUserLoginForm($username_length)
  640. . '<div class="item">'
  641. . '<label for="select_pred_hostname">'
  642. . ' ' . __('Host:')
  643. . '</label>'
  644. . '<span class="options">'
  645. . ' <select name="pred_hostname" id="select_pred_hostname" title="'
  646. . __('Host') . '"';
  647. $_current_user = $GLOBALS['dbi']->fetchValue('SELECT USER();');
  648. if (! empty($_current_user)) {
  649. $thishost = str_replace(
  650. "'",
  651. '',
  652. mb_substr(
  653. $_current_user,
  654. (mb_strrpos($_current_user, '@') + 1)
  655. )
  656. );
  657. if ($thishost != 'localhost' && $thishost != '127.0.0.1') {
  658. $html .= ' data-thishost="' . htmlspecialchars($thishost) . '" ';
  659. } else {
  660. unset($thishost);
  661. }
  662. }
  663. $html .= '>' . "\n";
  664. unset($_current_user);
  665. // when we start editing a user, $GLOBALS['pred_hostname'] is not defined
  666. if (! isset($GLOBALS['pred_hostname']) && isset($_POST['hostname'])) {
  667. switch (mb_strtolower($_POST['hostname'])) {
  668. case 'localhost':
  669. case '127.0.0.1':
  670. $GLOBALS['pred_hostname'] = 'localhost';
  671. break;
  672. case '%':
  673. $GLOBALS['pred_hostname'] = 'any';
  674. break;
  675. default:
  676. $GLOBALS['pred_hostname'] = 'userdefined';
  677. break;
  678. }
  679. }
  680. $html .= ' <option value="any"'
  681. . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'any')
  682. ? ' selected="selected"' : '') . '>' . __('Any host')
  683. . '</option>'
  684. . ' <option value="localhost"'
  685. . ((isset($GLOBALS['pred_hostname'])
  686. && $GLOBALS['pred_hostname'] == 'localhost')
  687. ? ' selected="selected"' : '') . '>' . __('Local')
  688. . '</option>';
  689. if (!empty($thishost)) {
  690. $html .= ' <option value="thishost"'
  691. . ((isset($GLOBALS['pred_hostname'])
  692. && $GLOBALS['pred_hostname'] == 'thishost')
  693. ? ' selected="selected"' : '') . '>' . __('This Host')
  694. . '</option>';
  695. }
  696. unset($thishost);
  697. $html .= self::getHtmlForTableInfoForm($hostname_length);
  698. $html .= '</form>';
  699. $html .= '</div>';
  700. return $html;
  701. }
  702. /**
  703. * returns html code to add a replication slave user to the master
  704. *
  705. * @param int $username_length Username length
  706. *
  707. * @return String HTML code
  708. */
  709. public static function getHtmlForAddUserLoginForm($username_length)
  710. {
  711. $html = '<input type="hidden" name="grant_count" value="25" />'
  712. . '<input type="hidden" name="createdb" id="createdb_0" value="0" />'
  713. . '<input id="checkbox_Repl_slave_priv" type="hidden"'
  714. . ' title="Needed for the replication slaves." '
  715. . 'value="Y" name="Repl_slave_priv"/>'
  716. . '<input id="checkbox_Repl_client_priv" type="hidden" '
  717. . 'title="Needed for the replication slaves."'
  718. . ' value="Y" name="Repl_client_priv"/> '
  719. . '<input type="hidden" name="sr_take_action" value="true" />'
  720. . '<div class="item">'
  721. . '<label for="select_pred_username">'
  722. . ' ' . __('User name:')
  723. . '</label>'
  724. . '<span class="options">'
  725. . ' <select name="pred_username" id="select_pred_username" '
  726. . 'title="' . __('User name') . '">'
  727. . ' <option value="any"'
  728. . ((isset($GLOBALS['pred_username'])
  729. && $GLOBALS['pred_username'] == 'any') ? ' selected="selected"' : '')
  730. . '>' . __('Any user') . '</option>'
  731. . ' <option value="userdefined"'
  732. . ((! isset($GLOBALS['pred_username'])
  733. || $GLOBALS['pred_username'] == 'userdefined')
  734. ? ' selected="selected"' : '')
  735. . '>' . __('Use text field:') . '</option>'
  736. . ' </select>'
  737. . '</span>'
  738. . '<input type="text" name="username" id="pma_username" maxlength="'
  739. . $username_length . '" title="' . __('User name') . '"'
  740. . (empty($_POST['username']) ? '' : ' value="'
  741. . (isset($GLOBALS['new_username'])
  742. ? $GLOBALS['new_username']
  743. : htmlspecialchars($_POST['username'])) . '"')
  744. . ' />'
  745. . '</div>';
  746. return $html;
  747. }
  748. /**
  749. * returns HTML for TableInfoForm
  750. *
  751. * @param int $hostname_length Selected hostname length
  752. *
  753. * @return String HTML code
  754. */
  755. public static function getHtmlForTableInfoForm($hostname_length)
  756. {
  757. $html = ' <option value="hosttable"'
  758. . ((isset($GLOBALS['pred_hostname'])
  759. && $GLOBALS['pred_hostname'] == 'hosttable')
  760. ? ' selected="selected"' : '') . '>' . __('Use Host Table')
  761. . '</option>'
  762. . ' <option value="userdefined"'
  763. . ((isset($GLOBALS['pred_hostname'])
  764. && $GLOBALS['pred_hostname'] == 'userdefined')
  765. ? ' selected="selected"' : '')
  766. . '>' . __('Use text field:') . '</option>'
  767. . ' </select>'
  768. . '</span>'
  769. . '<input type="text" name="hostname" id="pma_hostname" maxlength="'
  770. . $hostname_length . '" value="'
  771. . (isset($_POST['hostname']) ? htmlspecialchars($_POST['hostname']) : '')
  772. . '" title="' . __('Host')
  773. . '" />'
  774. . Util::showHint(
  775. __(
  776. 'When Host table is used, this field is ignored '
  777. . 'and values stored in Host table are used instead.'
  778. )
  779. )
  780. . '</div>'
  781. . '<div class="item">'
  782. . '<label for="select_pred_password">'
  783. . ' ' . __('Password:')
  784. . '</label>'
  785. . '<span class="options">'
  786. . ' <select name="pred_password" id="select_pred_password" title="'
  787. . __('Password') . '">'
  788. . ' <option value="none"';
  789. if (isset($_POST['username'])) {
  790. $html .= ' selected="selected"';
  791. }
  792. $html .= '>' . __('No Password') . '</option>'
  793. . ' <option value="userdefined"'
  794. . (isset($_POST['username']) ? '' : ' selected="selected"')
  795. . '>' . __('Use text field:') . '</option>'
  796. . ' </select>'
  797. . '</span>'
  798. . '<input type="password" id="text_pma_pw" name="pma_pw" title="'
  799. . __('Password') . '" />'
  800. . '</div>'
  801. . '<div class="item">'
  802. . '<label for="text_pma_pw2">'
  803. . ' ' . __('Re-type:')
  804. . '</label>'
  805. . '<span class="options">&nbsp;</span>'
  806. . '<input type="password" name="pma_pw2" id="text_pma_pw2" title="'
  807. . __('Re-type') . '" />'
  808. . '</div>'
  809. . '<div class="item">'
  810. . '<label for="button_generate_password">'
  811. . ' ' . __('Generate password:')
  812. . '</label>'
  813. . '<span class="options">'
  814. . ' <input type="button" class="button" '
  815. . 'id="button_generate_password" value="' . __('Generate')
  816. . '" onclick="suggestPassword(this.form)" />'
  817. . '</span>'
  818. . '<input type="text" name="generated_pw" id="generated_pw" />'
  819. . '</div>'
  820. . '</fieldset>';
  821. $html .= '<fieldset id="fieldset_user_privtable_footer" class="tblFooters">'
  822. . ' <input type="hidden" name="adduser_submit" value="1" />'
  823. . ' <input type="submit" id="adduser_submit" value="' . __('Go') . '" />'
  824. . '</fieldset>';
  825. return $html;
  826. }
  827. /**
  828. * handle control requests
  829. *
  830. * @return NULL
  831. */
  832. public static function handleControlRequest()
  833. {
  834. if (isset($_POST['sr_take_action'])) {
  835. $refresh = false;
  836. $result = false;
  837. $messageSuccess = null;
  838. $messageError = null;
  839. if (isset($_POST['slave_changemaster']) && ! $GLOBALS['cfg']['AllowArbitraryServer']) {
  840. $_SESSION['replication']['sr_action_status'] = 'error';
  841. $_SESSION['replication']['sr_action_info'] = __('Connection to server is disabled, please enable $cfg[\'AllowArbitraryServer\'] in phpMyAdmin configuration.');
  842. } elseif (isset($_POST['slave_changemaster'])) {
  843. $result = self::handleRequestForSlaveChangeMaster();
  844. } elseif (isset($_POST['sr_slave_server_control'])) {
  845. $result = self::handleRequestForSlaveServerControl();
  846. $refresh = true;
  847. switch ($_POST['sr_slave_action']) {
  848. case 'start':
  849. $messageSuccess = __('Replication started successfully.');
  850. $messageError = __('Error starting replication.');
  851. break;
  852. case 'stop':
  853. $messageSuccess = __('Replication stopped successfully.');
  854. $messageError = __('Error stopping replication.');
  855. break;
  856. case 'reset':
  857. $messageSuccess = __('Replication resetting successfully.');
  858. $messageError = __('Error resetting replication.');
  859. break;
  860. default:
  861. $messageSuccess = __('Success.');
  862. $messageError = __('Error.');
  863. break;
  864. }
  865. } elseif (isset($_POST['sr_slave_skip_error'])) {
  866. $result = self::handleRequestForSlaveSkipError();
  867. }
  868. if ($refresh) {
  869. $response = Response::getInstance();
  870. if ($response->isAjax()) {
  871. $response->setRequestStatus($result);
  872. $response->addJSON(
  873. 'message',
  874. $result
  875. ? Message::success($messageSuccess)
  876. : Message::error($messageError)
  877. );
  878. } else {
  879. Core::sendHeaderLocation(
  880. './server_replication.php'
  881. . Url::getCommonRaw($GLOBALS['url_params'])
  882. );
  883. }
  884. }
  885. unset($refresh);
  886. }
  887. }
  888. /**
  889. * handle control requests for Slave Change Master
  890. *
  891. * @return boolean
  892. */
  893. public static function handleRequestForSlaveChangeMaster()
  894. {
  895. $sr = array();
  896. $_SESSION['replication']['m_username'] = $sr['username']
  897. = $GLOBALS['dbi']->escapeString($_POST['username']);
  898. $_SESSION['replication']['m_password'] = $sr['pma_pw']
  899. = $GLOBALS['dbi']->escapeString($_POST['pma_pw']);
  900. $_SESSION['replication']['m_hostname'] = $sr['hostname']
  901. = $GLOBALS['dbi']->escapeString($_POST['hostname']);
  902. $_SESSION['replication']['m_port'] = $sr['port']
  903. = $GLOBALS['dbi']->escapeString($_POST['text_port']);
  904. $_SESSION['replication']['m_correct'] = '';
  905. $_SESSION['replication']['sr_action_status'] = 'error';
  906. $_SESSION['replication']['sr_action_info'] = __('Unknown error');
  907. // Attempt to connect to the new master server
  908. $link_to_master = Replication::connectToMaster(
  909. $sr['username'], $sr['pma_pw'], $sr['hostname'], $sr['port']
  910. );
  911. if (! $link_to_master) {
  912. $_SESSION['replication']['sr_action_status'] = 'error';
  913. $_SESSION['replication']['sr_action_info'] = sprintf(
  914. __('Unable to connect to master %s.'),
  915. htmlspecialchars($sr['hostname'])
  916. );
  917. } else {
  918. // Read the current master position
  919. $position = Replication::slaveBinLogMaster($link_to_master);
  920. if (empty($position)) {
  921. $_SESSION['replication']['sr_action_status'] = 'error';
  922. $_SESSION['replication']['sr_action_info']
  923. = __(
  924. 'Unable to read master log position. '
  925. . 'Possible privilege problem on master.'
  926. );
  927. } else {
  928. $_SESSION['replication']['m_correct'] = true;
  929. if (! Replication::slaveChangeMaster(
  930. $sr['username'],
  931. $sr['pma_pw'],
  932. $sr['hostname'],
  933. $sr['port'],
  934. $position,
  935. true,
  936. false
  937. )
  938. ) {
  939. $_SESSION['replication']['sr_action_status'] = 'error';
  940. $_SESSION['replication']['sr_action_info']
  941. = __('Unable to change master!');
  942. } else {
  943. $_SESSION['replication']['sr_action_status'] = 'success';
  944. $_SESSION['replication']['sr_action_info'] = sprintf(
  945. __('Master server changed successfully to %s.'),
  946. htmlspecialchars($sr['hostname'])
  947. );
  948. }
  949. }
  950. }
  951. return $_SESSION['replication']['sr_action_status'] === 'success';
  952. }
  953. /**
  954. * handle control requests for Slave Server Control
  955. *
  956. * @return boolean
  957. */
  958. public static function handleRequestForSlaveServerControl()
  959. {
  960. if (empty($_POST['sr_slave_control_parm'])) {
  961. $_POST['sr_slave_control_parm'] = null;
  962. }
  963. if ($_POST['sr_slave_action'] == 'reset') {
  964. $qStop = Replication::slaveControl("STOP", null, DatabaseInterface::CONNECT_USER);
  965. $qReset = $GLOBALS['dbi']->tryQuery("RESET SLAVE;");
  966. $qStart = Replication::slaveControl("START", null, DatabaseInterface::CONNECT_USER);
  967. $result = ($qStop !== false && $qStop !== -1 &&
  968. $qReset !== false && $qReset !== -1 &&
  969. $qStart !== false && $qStart !== -1);
  970. } else {
  971. $qControl = Replication::slaveControl(
  972. $_POST['sr_slave_action'],
  973. $_POST['sr_slave_control_parm'],
  974. DatabaseInterface::CONNECT_USER
  975. );
  976. $result = ($qControl !== false && $qControl !== -1);
  977. }
  978. return $result;
  979. }
  980. /**
  981. * handle control requests for Slave Skip Error
  982. *
  983. * @return boolean
  984. */
  985. public static function handleRequestForSlaveSkipError()
  986. {
  987. $count = 1;
  988. if (isset($_POST['sr_skip_errors_count'])) {
  989. $count = $_POST['sr_skip_errors_count'] * 1;
  990. }
  991. $qStop = Replication::slaveControl("STOP", null, DatabaseInterface::CONNECT_USER);
  992. $qSkip = $GLOBALS['dbi']->tryQuery(
  993. "SET GLOBAL SQL_SLAVE_SKIP_COUNTER = " . $count . ";"
  994. );
  995. $qStart = Replication::slaveControl("START", null, DatabaseInterface::CONNECT_USER);
  996. $result = ($qStop !== false && $qStop !== -1 &&
  997. $qSkip !== false && $qSkip !== -1 &&
  998. $qStart !== false && $qStart !== -1);
  999. return $result;
  1000. }
  1001. }