BinlogController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\ResponseRenderer;
  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. public function __construct(ResponseRenderer $response, Template $template, DatabaseInterface $dbi)
  27. {
  28. parent::__construct($response, $template);
  29. $this->dbi = $dbi;
  30. $this->binaryLogs = $this->dbi->fetchResult('SHOW BINARY LOGS', 'Log_name');
  31. }
  32. public function __invoke(): void
  33. {
  34. global $cfg, $errorUrl;
  35. $params = [
  36. 'log' => $_POST['log'] ?? null,
  37. 'pos' => $_POST['pos'] ?? null,
  38. 'is_full_query' => $_POST['is_full_query'] ?? null,
  39. ];
  40. $errorUrl = Url::getFromRoute('/');
  41. if ($this->dbi->isSuperUser()) {
  42. $this->dbi->selectDb('mysql');
  43. }
  44. $position = ! empty($params['pos']) ? (int) $params['pos'] : 0;
  45. $urlParams = [];
  46. if (isset($params['log']) && array_key_exists($params['log'], $this->binaryLogs)) {
  47. $urlParams['log'] = $params['log'];
  48. }
  49. $isFullQuery = false;
  50. if (! empty($params['is_full_query'])) {
  51. $isFullQuery = true;
  52. $urlParams['is_full_query'] = 1;
  53. }
  54. $sqlQuery = $this->getSqlQuery($params['log'] ?? '', $position, (int) $cfg['MaxRows']);
  55. $result = $this->dbi->query($sqlQuery);
  56. $numRows = $result->numRows();
  57. $previousParams = $urlParams;
  58. $fullQueriesParams = $urlParams;
  59. $nextParams = $urlParams;
  60. if ($position > 0) {
  61. $fullQueriesParams['pos'] = $position;
  62. if ($position > $cfg['MaxRows']) {
  63. $previousParams['pos'] = $position - $cfg['MaxRows'];
  64. }
  65. }
  66. $fullQueriesParams['is_full_query'] = 1;
  67. if ($isFullQuery) {
  68. unset($fullQueriesParams['is_full_query']);
  69. }
  70. if ($numRows >= $cfg['MaxRows']) {
  71. $nextParams['pos'] = $position + $cfg['MaxRows'];
  72. }
  73. $values = $result->fetchAllAssoc();
  74. $this->render('server/binlog/index', [
  75. 'url_params' => $urlParams,
  76. 'binary_logs' => $this->binaryLogs,
  77. 'log' => $params['log'],
  78. 'sql_message' => Generator::getMessage(Message::success(), $sqlQuery),
  79. 'values' => $values,
  80. 'has_previous' => $position > 0,
  81. 'has_next' => $numRows >= $cfg['MaxRows'],
  82. 'previous_params' => $previousParams,
  83. 'full_queries_params' => $fullQueriesParams,
  84. 'next_params' => $nextParams,
  85. 'has_icons' => Util::showIcons('TableNavigationLinksMode'),
  86. 'is_full_query' => $isFullQuery,
  87. ]);
  88. }
  89. /**
  90. * @param string $log Binary log file name
  91. * @param int $position Position to display
  92. * @param int $maxRows Maximum number of rows
  93. */
  94. private function getSqlQuery(
  95. string $log,
  96. int $position,
  97. int $maxRows
  98. ): string {
  99. $sqlQuery = 'SHOW BINLOG EVENTS';
  100. if (! empty($log)) {
  101. $sqlQuery .= ' IN \'' . $log . '\'';
  102. }
  103. $sqlQuery .= ' LIMIT ' . $position . ', ' . $maxRows;
  104. return $sqlQuery;
  105. }
  106. }