Issues (1880)

src/Bookmarks/BookmarkRepository.php (4 issues)

1
<?php
2
/**
3
 * Handles bookmarking SQL queries
4
 */
5
6
declare(strict_types=1);
7
8
namespace PhpMyAdmin\Bookmarks;
9
10
use PhpMyAdmin\Config;
11
use PhpMyAdmin\ConfigStorage\Features\BookmarkFeature;
12
use PhpMyAdmin\ConfigStorage\Relation;
13
use PhpMyAdmin\DatabaseInterface;
14
use PhpMyAdmin\Dbal\ConnectionType;
15
use PhpMyAdmin\Identifiers\DatabaseName;
0 ignored issues
show
The type PhpMyAdmin\Identifiers\DatabaseName was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use PhpMyAdmin\Util;
17
18
/**
19
 * Handles bookmarking SQL queries
20
 */
21
final class BookmarkRepository
22
{
23
    private BookmarkFeature|null $bookmarkFeature;
24
    private readonly Config $config;
25
26
    public function __construct(private DatabaseInterface $dbi, Relation $relation)
27
    {
28
        $this->bookmarkFeature = $relation->getRelationParameters()->bookmarkFeature;
29
        $this->config = Config::getInstance();
0 ignored issues
show
Deprecated Code introduced by
The function PhpMyAdmin\Config::getInstance() has been deprecated: Use dependency injection instead. ( Ignorable by Annotation )

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

29
        $this->config = /** @scrutinizer ignore-deprecated */ Config::getInstance();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
The property config is declared read-only in PhpMyAdmin\Bookmarks\BookmarkRepository.
Loading history...
30
    }
31
32
    /**
33
     * Creates a Bookmark object from the parameters
34
     *
35
     * @param bool $shared whether to make the bookmark available for all users
36
     */
37
    public function createBookmark(
38
        string $sqlQuery,
39
        string $label,
40
        string $user,
41
        string $database,
42
        bool $shared = false,
43
    ): Bookmark|false {
44
        if ($this->bookmarkFeature === null) {
45
            return false;
46
        }
47
48
        if ($sqlQuery === '' || $label === '') {
49
            return false;
50
        }
51
52
        if (! $this->config->settings['AllowSharedBookmarks']) {
53
            $shared = false;
54
        }
55
56
        if (! $shared && $user === '') {
57
            return false;
58
        }
59
60
        return new Bookmark($this->dbi, $this->bookmarkFeature, $database, $shared ? '' : $user, $label, $sqlQuery);
61
    }
62
63
    /**
64
     * Gets the list of bookmarks defined for the current database
65
     *
66
     * @param string       $user Current user
67
     * @param string|false $db   the current database name or false
68
     *
69
     * @return Bookmark[] the bookmarks list
70
     *
71
     * @infection-ignore-all
72
     */
73
    public function getList(
74
        string $user,
75
        string|false $db = false,
76
    ): array {
77
        if ($this->bookmarkFeature === null) {
78
            return [];
79
        }
80
81
        $exactUserMatch = ! $this->config->settings['AllowSharedBookmarks'];
82
83
        $query = 'SELECT * FROM ' . Util::backquote($this->bookmarkFeature->database)
84
            . '.' . Util::backquote($this->bookmarkFeature->bookmark)
85
            . ' WHERE (`user` = ' . $this->dbi->quoteString($user);
86
        if (! $exactUserMatch) {
87
            $query .= " OR `user` = ''";
88
        }
89
90
        $query .= ')';
91
92
        if ($db !== false) {
93
            $query .= ' AND dbase = ' . $this->dbi->quoteString($db);
94
        }
95
96
        $query .= ' ORDER BY label ASC';
97
98
        $result = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser);
99
100
        $bookmarks = [];
101
        foreach ($result as $row) {
102
            $bookmarks[] = $this->createFromRow($row);
103
        }
104
105
        return $bookmarks;
106
    }
107
108
    /**
109
     * Retrieve a specific bookmark
110
     */
111
    public function get(
112
        string|null $user,
113
        int $id,
114
    ): Bookmark|null {
115
        if ($this->bookmarkFeature === null) {
116
            return null;
117
        }
118
119
        $query = 'SELECT * FROM ' . Util::backquote($this->bookmarkFeature->database)
120
            . '.' . Util::backquote($this->bookmarkFeature->bookmark)
121
            . ' WHERE `id` = ' . $id;
122
123
        if ($user !== null) {
124
            $query .= ' AND (user = ' . $this->dbi->quoteString($user);
125
126
            $exactUserMatch = ! $this->config->settings['AllowSharedBookmarks'];
127
            if (! $exactUserMatch) {
128
                $query .= " OR user = ''";
129
            }
130
131
            $query .= ')';
132
        }
133
134
        $query .= ' LIMIT 1';
135
136
        $result = $this->dbi->fetchSingleRow($query, DatabaseInterface::FETCH_ASSOC, ConnectionType::ControlUser);
137
        if ($result !== null) {
138
            return $this->createFromRow($result);
139
        }
140
141
        return null;
142
    }
143
144
    /**
145
     * Retrieve a specific bookmark by its label
146
     */
147
    public function getByLabel(
148
        string $user,
149
        DatabaseName $db,
150
        string $label,
151
    ): Bookmark|null {
152
        if ($this->bookmarkFeature === null) {
153
            return null;
154
        }
155
156
        $query = 'SELECT * FROM ' . Util::backquote($this->bookmarkFeature->database)
157
            . '.' . Util::backquote($this->bookmarkFeature->bookmark)
158
            . ' WHERE `label`'
159
            . ' = ' . $this->dbi->quoteString($label)
160
            . ' AND dbase = ' . $this->dbi->quoteString($db->getName())
161
            . ' AND user = ' . $this->dbi->quoteString($user)
162
            . ' LIMIT 1';
163
164
        $result = $this->dbi->fetchSingleRow($query, DatabaseInterface::FETCH_ASSOC, ConnectionType::ControlUser);
165
        if ($result !== null) {
166
            return $this->createFromRow($result);
167
        }
168
169
        return null;
170
    }
171
172
    /** @param string[] $row Resource used to build the bookmark */
173
    private function createFromRow(array $row): Bookmark
174
    {
175
        return new Bookmark(
176
            $this->dbi,
177
            $this->bookmarkFeature,
0 ignored issues
show
It seems like $this->bookmarkFeature can also be of type null; however, parameter $bookmarkFeature of PhpMyAdmin\Bookmarks\Bookmark::__construct() does only seem to accept PhpMyAdmin\ConfigStorage\Features\BookmarkFeature, 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

177
            /** @scrutinizer ignore-type */ $this->bookmarkFeature,
Loading history...
178
            $row['dbase'],
179
            $row['user'],
180
            $row['label'],
181
            $row['query'],
182
            (int) $row['id'],
183
        );
184
    }
185
}
186