BinlogController.php 4.1 KB

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