Passed
Push — master ( 022b37...3bb133 )
by Maurício
07:36
created

ServerBinlogController::getSqlQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
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
declare(strict_types=1);
9
10
namespace PhpMyAdmin\Controllers\Server;
11
12
use PhpMyAdmin\Controllers\Controller;
13
use PhpMyAdmin\DatabaseInterface;
14
use PhpMyAdmin\Message;
15
use PhpMyAdmin\Util;
16
17
/**
18
 * Handles viewing binary logs
19
 *
20
 * @package PhpMyAdmin\Controllers
21
 */
22
class ServerBinlogController extends Controller
23
{
24
    /**
25
     * array binary log files
26
     */
27
    protected $binaryLogs;
28
29
    /**
30
     * Constructs ServerBinlogController
31
     *
32
     * @param \PhpMyAdmin\Response          $response Response object
33
     * @param \PhpMyAdmin\DatabaseInterface $dbi      DatabaseInterface object
34
     */
35
    public function __construct($response, $dbi)
36
    {
37
        parent::__construct($response, $dbi);
38
        $this->binaryLogs = $this->dbi->fetchResult(
39
            'SHOW MASTER LOGS',
40
            'Log_name',
41
            null,
42
            DatabaseInterface::CONNECT_USER,
43
            DatabaseInterface::QUERY_STORE
44
        );
45
    }
46
47
    /**
48
     * Index action
49
     *
50
     * @param array $params Request params
51
     *
52
     * @return string
53
     */
54
    public function indexAction(array $params): string
55
    {
56
        global $cfg, $pmaThemeImage;
57
58
        include_once ROOT_PATH . 'libraries/server_common.inc.php';
59
60
        $position = ! empty($params['pos']) ? (int) $params['pos'] : 0;
61
62
        $urlParams = [];
63
        if (isset($params['log'])
64
            && array_key_exists($params['log'], $this->binaryLogs)
65
        ) {
66
            $urlParams['log'] = $params['log'];
67
        }
68
69
        $isFullQuery = false;
70
        if (! empty($params['is_full_query'])) {
71
            $isFullQuery = true;
72
            $urlParams['is_full_query'] = 1;
73
        }
74
75
        $sqlQuery = $this->getSqlQuery(
76
            $params['log'] ?? '',
77
            $position,
78
            (int) $cfg['MaxRows']
79
        );
80
        $result = $this->dbi->query($sqlQuery);
81
82
        $numRows = 0;
83
        if (isset($result) && $result) {
84
            $numRows = $this->dbi->numRows($result);
85
        }
86
87
        $previousParams = $urlParams;
88
        $fullQueriesParams = $urlParams;
89
        $nextParams = $urlParams;
90
        if ($position > 0) {
91
            $fullQueriesParams['pos'] = $position;
92
            if ($position > $cfg['MaxRows']) {
93
                $previousParams['pos'] = $position - $cfg['MaxRows'];
94
            }
95
        }
96
        $fullQueriesParams['is_full_query'] = 1;
97
        if ($isFullQuery) {
98
            unset($fullQueriesParams['is_full_query']);
99
        }
100
        if ($numRows >= $cfg['MaxRows']) {
101
            $nextParams['pos'] = $position + $cfg['MaxRows'];
102
        }
103
104
        $values = [];
105
        while ($value = $this->dbi->fetchAssoc($result)) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type false; however, parameter $result of PhpMyAdmin\DatabaseInterface::fetchAssoc() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

105
        while ($value = $this->dbi->fetchAssoc(/** @scrutinizer ignore-type */ $result)) {
Loading history...
106
            $values[] = $value;
107
        }
108
109
        return $this->template->render('server/binlog/index', [
110
            'url_params' => $urlParams,
111
            'binary_logs' => $this->binaryLogs,
112
            'log' => $params['log'],
113
            'sql_message' => Util::getMessage(Message::success(), $sqlQuery),
114
            'values' => $values,
115
            'has_previous' => $position > 0,
116
            'has_next' => $numRows >= $cfg['MaxRows'],
117
            'previous_params' => $previousParams,
118
            'full_queries_params' => $fullQueriesParams,
119
            'next_params' => $nextParams,
120
            'has_icons' => Util::showIcons('TableNavigationLinksMode'),
121
            'is_full_query' => $isFullQuery,
122
            'image_path' => $pmaThemeImage,
123
        ]);
124
    }
125
126
    /**
127
     * @param string $log      Binary log file name
128
     * @param int    $position Position to display
129
     * @param int    $maxRows  Maximum number of rows
130
     *
131
     * @return string
132
     */
133
    private function getSqlQuery(
134
        string $log,
135
        int $position,
136
        int $maxRows
137
    ): string {
138
        $sqlQuery = 'SHOW BINLOG EVENTS';
139
        if (! empty($log)) {
140
            $sqlQuery .= ' IN \'' . $log . '\'';
141
        }
142
        $sqlQuery .= ' LIMIT ' . $position . ', ' . $maxRows;
143
144
        return $sqlQuery;
145
    }
146
}
147