ServerBinlogController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds the PhpMyAdmin\Controllers\Server\ServerBinlogController
  5. *
  6. * @package PhpMyAdmin\Controllers
  7. */
  8. namespace PhpMyAdmin\Controllers\Server;
  9. use PhpMyAdmin\Controllers\Controller;
  10. use PhpMyAdmin\DatabaseInterface;
  11. use PhpMyAdmin\Message;
  12. use PhpMyAdmin\Server\Common;
  13. use PhpMyAdmin\Template;
  14. use PhpMyAdmin\Url;
  15. use PhpMyAdmin\Util;
  16. /**
  17. * Handles viewing binary logs
  18. *
  19. * @package PhpMyAdmin\Controllers
  20. */
  21. class ServerBinlogController extends Controller
  22. {
  23. /**
  24. * array binary log files
  25. */
  26. protected $binary_logs;
  27. /**
  28. * Constructs ServerBinlogController
  29. */
  30. public function __construct($response, $dbi)
  31. {
  32. parent::__construct($response, $dbi);
  33. $this->binary_logs = $this->dbi->fetchResult(
  34. 'SHOW MASTER LOGS',
  35. 'Log_name',
  36. null,
  37. DatabaseInterface::CONNECT_USER,
  38. DatabaseInterface::QUERY_STORE
  39. );
  40. }
  41. /**
  42. * Index action
  43. *
  44. * @return void
  45. */
  46. public function indexAction()
  47. {
  48. /**
  49. * Does the common work
  50. */
  51. include_once 'libraries/server_common.inc.php';
  52. $url_params = array();
  53. if (! isset($_POST['log'])
  54. || ! array_key_exists($_POST['log'], $this->binary_logs)
  55. ) {
  56. $_POST['log'] = '';
  57. } else {
  58. $url_params['log'] = $_POST['log'];
  59. }
  60. if (!empty($_POST['dontlimitchars'])) {
  61. $url_params['dontlimitchars'] = 1;
  62. }
  63. $this->response->addHTML(
  64. Template::get('server/sub_page_header')->render([
  65. 'type' => 'binlog',
  66. ])
  67. );
  68. $this->response->addHTML($this->_getLogSelector($url_params));
  69. $this->response->addHTML($this->_getLogInfo($url_params));
  70. }
  71. /**
  72. * Returns the html for log selector.
  73. *
  74. * @param array $url_params links parameters
  75. *
  76. * @return string
  77. */
  78. private function _getLogSelector(array $url_params)
  79. {
  80. return Template::get('server/binlog/log_selector')->render(
  81. array(
  82. 'url_params' => $url_params,
  83. 'binary_logs' => $this->binary_logs,
  84. 'log' => $_POST['log'],
  85. )
  86. );
  87. }
  88. /**
  89. * Returns the html for binary log information.
  90. *
  91. * @param array $url_params links parameters
  92. *
  93. * @return string
  94. */
  95. private function _getLogInfo(array $url_params)
  96. {
  97. /**
  98. * Need to find the real end of rows?
  99. */
  100. if (! isset($_POST['pos'])) {
  101. $pos = 0;
  102. } else {
  103. /* We need this to be a integer */
  104. $pos = (int) $_POST['pos'];
  105. }
  106. $sql_query = 'SHOW BINLOG EVENTS';
  107. if (! empty($_POST['log'])) {
  108. $sql_query .= ' IN \'' . $_POST['log'] . '\'';
  109. }
  110. $sql_query .= ' LIMIT ' . $pos . ', ' . intval($GLOBALS['cfg']['MaxRows']);
  111. /**
  112. * Sends the query
  113. */
  114. $result = $this->dbi->query($sql_query);
  115. /**
  116. * prepare some vars for displaying the result table
  117. */
  118. // Gets the list of fields properties
  119. if (isset($result) && $result) {
  120. $num_rows = $this->dbi->numRows($result);
  121. } else {
  122. $num_rows = 0;
  123. }
  124. if (empty($_POST['dontlimitchars'])) {
  125. $dontlimitchars = false;
  126. } else {
  127. $dontlimitchars = true;
  128. $url_params['dontlimitchars'] = 1;
  129. }
  130. //html output
  131. $html = Util::getMessage(Message::success(), $sql_query);
  132. $html .= '<table id="binlogTable">'
  133. . '<thead>'
  134. . '<tr>'
  135. . '<td colspan="6" class="center">';
  136. $html .= $this->_getNavigationRow($url_params, $pos, $num_rows, $dontlimitchars);
  137. $html .= '</td>'
  138. . '</tr>'
  139. . '<tr>'
  140. . '<th>' . __('Log name') . '</th>'
  141. . '<th>' . __('Position') . '</th>'
  142. . '<th>' . __('Event type') . '</th>'
  143. . '<th>' . __('Server ID') . '</th>'
  144. . '<th>' . __('Original position') . '</th>'
  145. . '<th>' . __('Information') . '</th>'
  146. . '</tr>'
  147. . '</thead>'
  148. . '<tbody>';
  149. $html .= $this->_getAllLogItemInfo($result, $dontlimitchars);
  150. $html .= '</tbody>'
  151. . '</table>';
  152. return $html;
  153. }
  154. /**
  155. * Returns the html for Navigation Row.
  156. *
  157. * @param array $url_params Links parameters
  158. * @param int $pos Position to display
  159. * @param int $num_rows Number of results row
  160. * @param bool $dontlimitchars Whether limit chars
  161. *
  162. * @return string
  163. */
  164. private function _getNavigationRow(array $url_params, $pos, $num_rows, $dontlimitchars)
  165. {
  166. $html = "";
  167. // we do not know how much rows are in the binlog
  168. // so we can just force 'NEXT' button
  169. if ($pos > 0) {
  170. $this_url_params = $url_params;
  171. if ($pos > $GLOBALS['cfg']['MaxRows']) {
  172. $this_url_params['pos'] = $pos - $GLOBALS['cfg']['MaxRows'];
  173. }
  174. $html .= '<a href="server_binlog.php" data-post="'
  175. . Url::getCommon($this_url_params, '', false) . '"';
  176. if (Util::showIcons('TableNavigationLinksMode')) {
  177. $html .= ' title="' . _pgettext('Previous page', 'Previous') . '">';
  178. } else {
  179. $html .= '>' . _pgettext('Previous page', 'Previous');
  180. } // end if... else...
  181. $html .= ' &lt; </a> - ';
  182. }
  183. $this_url_params = $url_params;
  184. if ($pos > 0) {
  185. $this_url_params['pos'] = $pos;
  186. }
  187. if ($dontlimitchars) {
  188. unset($this_url_params['dontlimitchars']);
  189. $tempTitle = __('Truncate Shown Queries');
  190. $tempImgMode = 'partial';
  191. } else {
  192. $this_url_params['dontlimitchars'] = 1;
  193. $tempTitle = __('Show Full Queries');
  194. $tempImgMode = 'full';
  195. }
  196. $html .= '<a href="server_binlog.php" data-post="' . Url::getCommon($this_url_params, '', false)
  197. . '" title="' . $tempTitle . '">'
  198. . '<img src="' . $GLOBALS['pmaThemeImage'] . 's_' . $tempImgMode
  199. . 'text.png" alt="' . $tempTitle . '" /></a>';
  200. // we do not now how much rows are in the binlog
  201. // so we can just force 'NEXT' button
  202. if ($num_rows >= $GLOBALS['cfg']['MaxRows']) {
  203. $this_url_params = $url_params;
  204. $this_url_params['pos'] = $pos + $GLOBALS['cfg']['MaxRows'];
  205. $html .= ' - <a href="server_binlog.php" data-post="'
  206. . Url::getCommon($this_url_params, '', false)
  207. . '"';
  208. if (Util::showIcons('TableNavigationLinksMode')) {
  209. $html .= ' title="' . _pgettext('Next page', 'Next') . '">';
  210. } else {
  211. $html .= '>' . _pgettext('Next page', 'Next');
  212. } // end if... else...
  213. $html .= ' &gt; </a>';
  214. }
  215. return $html;
  216. }
  217. /**
  218. * Returns the html for all binary log items.
  219. *
  220. * @param resource $result MySQL Query result
  221. * @param bool $dontlimitchars Whether limit chars
  222. *
  223. * @return string
  224. */
  225. private function _getAllLogItemInfo($result, $dontlimitchars)
  226. {
  227. $html = "";
  228. while ($value = $this->dbi->fetchAssoc($result)) {
  229. $html .= Template::get('server/binlog/log_row')->render(
  230. array(
  231. 'value' => $value,
  232. 'dontlimitchars' => $dontlimitchars,
  233. )
  234. );
  235. }
  236. return $html;
  237. }
  238. }