ReplicationGui.php 41 KB

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