BinlogController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds the PhpMyAdmin\Controllers\Server\BinlogController
  5. *
  6. * @package PhpMyAdmin\Controllers
  7. */
  8. declare(strict_types=1);
  9. namespace PhpMyAdmin\Controllers\Server;
  10. use PhpMyAdmin\Controllers\AbstractController;
  11. use PhpMyAdmin\DatabaseInterface;
  12. use PhpMyAdmin\Message;
  13. use PhpMyAdmin\Response;
  14. use PhpMyAdmin\Template;
  15. use PhpMyAdmin\Util;
  16. /**
  17. * Handles viewing binary logs
  18. *
  19. * @package PhpMyAdmin\Controllers
  20. */
  21. class BinlogController extends AbstractController
  22. {
  23. /**
  24. * array binary log files
  25. */
  26. protected $binaryLogs;
  27. /**
  28. * Constructs BinlogController
  29. *
  30. * @param Response $response Response object
  31. * @param DatabaseInterface $dbi DatabaseInterface object
  32. * @param Template $template Template object
  33. */
  34. public function __construct($response, $dbi, Template $template)
  35. {
  36. parent::__construct($response, $dbi, $template);
  37. $this->binaryLogs = $this->dbi->fetchResult(
  38. 'SHOW MASTER LOGS',
  39. 'Log_name',
  40. null,
  41. DatabaseInterface::CONNECT_USER,
  42. DatabaseInterface::QUERY_STORE
  43. );
  44. }
  45. /**
  46. * Index action
  47. *
  48. * @param array $params Request params
  49. *
  50. * @return string
  51. */
  52. public function indexAction(array $params): string
  53. {
  54. global $cfg, $pmaThemeImage;
  55. include_once ROOT_PATH . 'libraries/server_common.inc.php';
  56. $position = ! empty($params['pos']) ? (int) $params['pos'] : 0;
  57. $urlParams = [];
  58. if (isset($params['log'])
  59. && array_key_exists($params['log'], $this->binaryLogs)
  60. ) {
  61. $urlParams['log'] = $params['log'];
  62. }
  63. $isFullQuery = false;
  64. if (! empty($params['is_full_query'])) {
  65. $isFullQuery = true;
  66. $urlParams['is_full_query'] = 1;
  67. }
  68. $sqlQuery = $this->getSqlQuery(
  69. $params['log'] ?? '',
  70. $position,
  71. (int) $cfg['MaxRows']
  72. );
  73. $result = $this->dbi->query($sqlQuery);
  74. $numRows = 0;
  75. if (isset($result) && $result) {
  76. $numRows = $this->dbi->numRows($result);
  77. }
  78. $previousParams = $urlParams;
  79. $fullQueriesParams = $urlParams;
  80. $nextParams = $urlParams;
  81. if ($position > 0) {
  82. $fullQueriesParams['pos'] = $position;
  83. if ($position > $cfg['MaxRows']) {
  84. $previousParams['pos'] = $position - $cfg['MaxRows'];
  85. }
  86. }
  87. $fullQueriesParams['is_full_query'] = 1;
  88. if ($isFullQuery) {
  89. unset($fullQueriesParams['is_full_query']);
  90. }
  91. if ($numRows >= $cfg['MaxRows']) {
  92. $nextParams['pos'] = $position + $cfg['MaxRows'];
  93. }
  94. $values = [];
  95. while ($value = $this->dbi->fetchAssoc($result)) {
  96. $values[] = $value;
  97. }
  98. return $this->template->render('server/binlog/index', [
  99. 'url_params' => $urlParams,
  100. 'binary_logs' => $this->binaryLogs,
  101. 'log' => $params['log'],
  102. 'sql_message' => Util::getMessage(Message::success(), $sqlQuery),
  103. 'values' => $values,
  104. 'has_previous' => $position > 0,
  105. 'has_next' => $numRows >= $cfg['MaxRows'],
  106. 'previous_params' => $previousParams,
  107. 'full_queries_params' => $fullQueriesParams,
  108. 'next_params' => $nextParams,
  109. 'has_icons' => Util::showIcons('TableNavigationLinksMode'),
  110. 'is_full_query' => $isFullQuery,
  111. 'image_path' => $pmaThemeImage,
  112. ]);
  113. }
  114. /**
  115. * @param string $log Binary log file name
  116. * @param int $position Position to display
  117. * @param int $maxRows Maximum number of rows
  118. *
  119. * @return string
  120. */
  121. private function getSqlQuery(
  122. string $log,
  123. int $position,
  124. int $maxRows
  125. ): string {
  126. $sqlQuery = 'SHOW BINLOG EVENTS';
  127. if (! empty($log)) {
  128. $sqlQuery .= ' IN \'' . $log . '\'';
  129. }
  130. $sqlQuery .= ' LIMIT ' . $position . ', ' . $maxRows;
  131. return $sqlQuery;
  132. }
  133. }