Issues (217)

src/controllers/RulesController.php (3 issues)

1
<?php
2
3
/**
4
 * PHPPgAdmin 6.1.3
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 */
14
class RulesController extends BaseController
15
{
16
    /**
17
     * Default method to render the controller according to the action parameter.
18
     */
19
    public function render()
20
    {
21
        if ('tree' === $this->action) {
22
            return $this->doTree();
23
        }
24
25
        // Different header if we're view rules or table rules
26
        $this->printHeader($_REQUEST[$_REQUEST['subject']] . ' - ' . $this->lang['strrules']);
27
        $this->printBody();
28
29
        switch ($this->action) {
30
            case 'create_rule':
31
                $this->createRule(true);
32
33
                break;
34
            case 'save_create_rule':
35
                if (null !== $this->getPostParam('cancel')) {
36
                    $this->doDefault();
37
                } else {
38
                    $this->createRule(false);
39
                }
40
41
                break;
42
            case 'drop':
43
                if (isset($_POST['yes'])) {
44
                    $this->doDrop(false);
45
                } else {
46
                    $this->doDefault();
47
                }
48
49
                break;
50
            case 'confirm_drop':
51
                $this->doDrop(true);
52
53
                break;
54
55
            default:
56
                $this->doDefault();
57
58
                break;
59
        }
60
61
        return $this->printFooter();
62
    }
63
64
    /**
65
     * List all the rules on the table.
66
     *
67
     * @param mixed $msg
68
     */
69
    public function doDefault($msg = ''): void
70
    {
71
        $data = $this->misc->getDatabaseAccessor();
72
73
        $this->printTrail($_REQUEST['subject']);
74
        $this->printTabs($_REQUEST['subject'], 'rules');
75
        $this->printMsg($msg);
76
77
        $rules = $data->getRules($_REQUEST[$_REQUEST['subject']]);
78
79
        $columns = [
80
            'rule' => [
81
                'title' => $this->lang['strname'],
82
                'field' => Decorator::field('rulename'),
83
            ],
84
            'definition' => [
85
                'title' => $this->lang['strdefinition'],
86
                'field' => Decorator::field('definition'),
87
            ],
88
            'actions' => [
89
                'title' => $this->lang['stractions'],
90
            ],
91
        ];
92
93
        $subject = \urlencode($_REQUEST['subject']);
94
        $object = \urlencode($_REQUEST[$_REQUEST['subject']]);
95
96
        $actions = [
97
            'drop' => [
98
                'content' => $this->lang['strdrop'],
99
                'attr' => [
100
                    'href' => [
101
                        'url' => 'rules',
102
                        'urlvars' => [
103
                            'action' => 'confirm_drop',
104
                            'reltype' => $subject,
105
                            $subject => $object,
106
                            'subject' => 'rule',
107
                            'rule' => Decorator::field('rulename'),
108
                        ],
109
                    ],
110
                ],
111
            ],
112
        ];
113
114
        echo $this->printTable($rules, $columns, $actions, 'rules-rules', $this->lang['strnorules']);
0 ignored issues
show
It seems like $rules can also be of type integer; however, parameter $tabledata of PHPPgAdmin\Controller\BaseController::printTable() does only seem to accept ADORecordSet|PHPPgAdmin\ArrayRecordSet, 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

114
        echo $this->printTable(/** @scrutinizer ignore-type */ $rules, $columns, $actions, 'rules-rules', $this->lang['strnorules']);
Loading history...
115
116
        $this->printNavLinks(['create' => [
117
            'attr' => [
118
                'href' => [
119
                    'url' => 'rules',
120
                    'urlvars' => [
121
                        'action' => 'create_rule',
122
                        'server' => $_REQUEST['server'],
123
                        'database' => $_REQUEST['database'],
124
                        'schema' => $_REQUEST['schema'],
125
                        $subject => $object,
126
                        'subject' => $subject,
127
                    ],
128
                ],
129
            ],
130
            'content' => $this->lang['strcreaterule'],
131
        ]], 'rules-rules', \get_defined_vars());
132
    }
133
134
    public function doTree()
135
    {
136
        $data = $this->misc->getDatabaseAccessor();
137
138
        $rules = $data->getRules($_REQUEST[$_REQUEST['subject']]);
139
140
        $attrs = [
141
            'text' => Decorator::field('rulename'),
142
            'icon' => 'Rule',
143
        ];
144
145
        return $this->printTree($rules, $attrs, 'rules');
0 ignored issues
show
It seems like $rules can also be of type integer; however, parameter $_treedata of PHPPgAdmin\Controller\BaseController::printTree() does only seem to accept PHPPgAdmin\ADORecordSet|PHPPgAdmin\ArrayRecordSet, 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

145
        return $this->printTree(/** @scrutinizer ignore-type */ $rules, $attrs, 'rules');
Loading history...
146
    }
147
148
    /**
149
     * Confirm and then actually create a rule.
150
     *
151
     * @param mixed $confirm
152
     * @param mixed $msg
153
     */
154
    public function createRule($confirm, $msg = ''): void
155
    {
156
        $data = $this->misc->getDatabaseAccessor();
157
158
        $this->coalesceArr($_POST, 'name', '');
159
160
        $this->coalesceArr($_POST, 'event', '');
161
162
        $this->coalesceArr($_POST, 'where', '');
163
164
        $this->coalesceArr($_POST, 'type', 'SOMETHING');
165
166
        $this->coalesceArr($_POST, 'raction', '');
167
168
        if ($confirm) {
169
            $this->printTrail($_REQUEST['subject']);
170
            $this->printTitle($this->lang['strcreaterule'], 'pg.rule.create');
171
            $this->printMsg($msg);
172
173
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/rules" method="post">' . \PHP_EOL;
174
            echo '<table>' . \PHP_EOL;
175
            echo "<tr><th class=\"data left required\">{$this->lang['strname']}</th>" . \PHP_EOL;
176
            echo "<td class=\"data1\"><input name=\"name\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"",
177
            \htmlspecialchars($_POST['name']), '" /></td></tr>' . \PHP_EOL;
178
            echo "<tr><th class=\"data left required\">{$this->lang['strevent']}</th>" . \PHP_EOL;
179
            echo '<td class="data1"><select name="event">' . \PHP_EOL;
180
181
            foreach ($data->rule_events as $v) {
182
                echo "<option value=\"{$v}\"", ($v === $_POST['event']) ? ' selected="selected"' : '',
183
                ">{$v}</option>" . \PHP_EOL;
184
            }
185
            echo '</select></td></tr>' . \PHP_EOL;
186
            echo "<tr><th class=\"data left\">{$this->lang['strwhere']}</th>" . \PHP_EOL;
187
            echo '<td class="data1"><input name="where" size="32" value="',
188
            \htmlspecialchars($_POST['where']), '" /></td></tr>' . \PHP_EOL;
189
            echo "<tr><th class=\"data left\"><label for=\"instead\">{$this->lang['strinstead']}</label></th>" . \PHP_EOL;
190
            echo '<td class="data1">';
191
            echo '<input type="checkbox" id="instead" name="instead" ', (isset($_POST['instead'])) ? ' checked="checked"' : '', ' />' . \PHP_EOL;
192
            echo '</td></tr>' . \PHP_EOL;
193
            echo "<tr><th class=\"data left required\">{$this->lang['straction']}</th>" . \PHP_EOL;
194
            echo '<td class="data1">';
195
            echo '<input type="radio" id="type1" name="type" value="NOTHING"', ('NOTHING' === $_POST['type']) ? ' checked="checked"' : '', ' /> <label for="type1">NOTHING</label><br />' . \PHP_EOL;
0 ignored issues
show
This line exceeds maximum limit of 190 characters; contains 197 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
196
            echo '<input type="radio" name="type" value="SOMETHING"', ('SOMETHING' === $_POST['type']) ? ' checked="checked"' : '', ' />' . \PHP_EOL;
197
            echo '(<input name="raction" size="32" value="',
198
            \htmlspecialchars($_POST['raction']), '" />)</td></tr>' . \PHP_EOL;
199
            echo '</table>' . \PHP_EOL;
200
201
            echo '<input type="hidden" name="action" value="save_create_rule" />' . \PHP_EOL;
202
            echo '<input type="hidden" name="subject" value="', \htmlspecialchars($_REQUEST['subject']), '" />' . \PHP_EOL;
203
            echo '<input type="hidden" name="', \htmlspecialchars($_REQUEST['subject']),
204
            '" value="', \htmlspecialchars($_REQUEST[$_REQUEST['subject']]), '" />' . \PHP_EOL;
205
            echo $this->view->form;
206
            echo "<p><input type=\"submit\" name=\"ok\" value=\"{$this->lang['strcreate']}\" />" . \PHP_EOL;
207
            echo \sprintf('<input type="submit" name="cancel" value="%s"  /></p>%s', $this->lang['strcancel'], \PHP_EOL);
208
            echo '</form>' . \PHP_EOL;
209
        } else {
210
            if ('' === \trim($_POST['name'])) {
211
                $this->createRule(true, $this->lang['strruleneedsname']);
212
            } else {
213
                $status = $data->createRule(
214
                    $_POST['name'],
215
                    $_POST['event'],
216
                    $_POST[$_POST['subject']],
217
                    $_POST['where'],
218
                    isset($_POST['instead']),
219
                    $_POST['type'],
220
                    $_POST['raction']
221
                );
222
223
                if (0 === $status) {
224
                    $this->doDefault($this->lang['strrulecreated']);
225
                } else {
226
                    $this->createRule(true, $this->lang['strrulecreatedbad']);
227
                }
228
            }
229
        }
230
    }
231
232
    /**
233
     * Show confirmation of drop and perform actual drop.
234
     *
235
     * @param mixed $confirm
236
     */
237
    public function doDrop($confirm): void
238
    {
239
        $data = $this->misc->getDatabaseAccessor();
240
241
        if ($confirm) {
242
            $this->printTrail($_REQUEST['subject']);
243
            $this->printTitle($this->lang['strdrop'], 'pg.rule.drop');
244
245
            echo '<p>', \sprintf(
246
                $this->lang['strconfdroprule'],
247
                $this->misc->printVal($_REQUEST['rule']),
248
                $this->misc->printVal($_REQUEST[$_REQUEST['reltype']])
249
            ), '</p>' . \PHP_EOL;
250
251
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/rules" method="post">' . \PHP_EOL;
252
            echo '<input type="hidden" name="action" value="drop" />' . \PHP_EOL;
253
            echo '<input type="hidden" name="subject" value="', \htmlspecialchars($_REQUEST['reltype']), '" />' . \PHP_EOL;
254
            echo '<input type="hidden" name="', \htmlspecialchars($_REQUEST['reltype']),
255
            '" value="', \htmlspecialchars($_REQUEST[$_REQUEST['reltype']]), '" />' . \PHP_EOL;
256
            echo '<input type="hidden" name="rule" value="', \htmlspecialchars($_REQUEST['rule']), '" />' . \PHP_EOL;
257
            echo $this->view->form;
258
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$this->lang['strcascade']}</label></p>" . \PHP_EOL;
259
            echo "<input type=\"submit\" name=\"yes\" value=\"{$this->lang['stryes']}\" />" . \PHP_EOL;
260
            echo "<input type=\"submit\" name=\"no\" value=\"{$this->lang['strno']}\" />" . \PHP_EOL;
261
            echo '</form>' . \PHP_EOL;
262
        } else {
263
            $status = $data->dropRule($_POST['rule'], $_POST[$_POST['subject']], isset($_POST['cascade']));
264
265
            if (0 === $status) {
266
                $this->doDefault($this->lang['strruledropped']);
267
            } else {
268
                $this->doDefault($this->lang['strruledroppedbad']);
269
            }
270
        }
271
    }
272
}
273