Passed
Push — master ( 442876...4ec1bc )
by Felipe
15:55 queued 10:33
created

src/controllers/RulesController.php (5 issues)

1
<?php
2
0 ignored issues
show
You must use "/**" style comments for a file comment
Loading history...
3
/*
4
 * PHPPgAdmin v6.0.0-beta.30
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
    public $controller_name = 'RulesController';
17
18
    /**
19
     * Default method to render the controller according to the action parameter.
20
     */
21
    public function render()
22
    {
23
        $lang = $this->lang;
24
25
        $action = $this->action;
26
        if ('tree' == $action) {
27
            return $this->doTree();
28
        }
29
30
        // Different header if we're view rules or table rules
31
        $this->printHeader($_REQUEST[$_REQUEST['subject']] . ' - ' . $lang['strrules']);
32
        $this->printBody();
33
34
        switch ($action) {
35
            case 'create_rule':
36
                $this->createRule(true);
37
38
                break;
39
            case 'save_create_rule':
40
                if (isset($_POST['cancel'])) {
41
                    $this->doDefault();
42
                } else {
43
                    $this->createRule(false);
44
                }
45
46
                break;
47
            case 'drop':
48
                if (isset($_POST['yes'])) {
49
                    $this->doDrop(false);
50
                } else {
51
                    $this->doDefault();
52
                }
53
54
                break;
55
            case 'confirm_drop':
56
                $this->doDrop(true);
57
58
                break;
59
            default:
60
                $this->doDefault();
61
62
                break;
63
        }
64
65
        return $this->printFooter();
66
    }
67
68
    /**
69
     * List all the rules on the table.
70
     *
71
     * @param mixed $msg
1 ignored issue
show
Missing parameter comment
Loading history...
72
     */
73
    public function doDefault($msg = '')
74
    {
75
        $lang = $this->lang;
76
        $data = $this->misc->getDatabaseAccessor();
77
78
        $this->printTrail($_REQUEST['subject']);
79
        $this->printTabs($_REQUEST['subject'], 'rules');
80
        $this->printMsg($msg);
81
82
        $rules = $data->getRules($_REQUEST[$_REQUEST['subject']]);
83
84
        $columns = [
85
            'rule'       => [
86
                'title' => $lang['strname'],
87
                'field' => Decorator::field('rulename'),
88
            ],
89
            'definition' => [
90
                'title' => $lang['strdefinition'],
91
                'field' => Decorator::field('definition'),
92
            ],
93
            'actions'    => [
94
                'title' => $lang['stractions'],
95
            ],
96
        ];
97
98
        $subject = urlencode($_REQUEST['subject']);
99
        $object  = urlencode($_REQUEST[$_REQUEST['subject']]);
100
101
        $actions = [
102
            'drop' => [
103
                'content' => $lang['strdrop'],
104
                'attr'    => [
105
                    'href' => [
106
                        'url'     => 'rules.php',
107
                        'urlvars' => [
108
                            'action'  => 'confirm_drop',
109
                            'reltype' => $subject,
110
                            $subject  => $object,
111
                            'subject' => 'rule',
112
                            'rule'    => Decorator::field('rulename'),
113
                        ],
114
                    ],
115
                ],
116
            ],
117
        ];
118
119
        echo $this->printTable($rules, $columns, $actions, 'rules-rules', $lang['strnorules']);
120
121
        $this->printNavLinks(['create' => [
122
            'attr'    => [
123
                'href' => [
124
                    'url'     => 'rules.php',
125
                    'urlvars' => [
126
                        'action'   => 'create_rule',
127
                        'server'   => $_REQUEST['server'],
128
                        'database' => $_REQUEST['database'],
129
                        'schema'   => $_REQUEST['schema'],
130
                        $subject   => $object,
131
                        'subject'  => $subject,
132
                    ],
133
                ],
134
            ],
135
            'content' => $lang['strcreaterule'],
136
        ]], 'rules-rules', get_defined_vars());
137
    }
138
139
    public function doTree()
140
    {
141
        $lang = $this->lang;
142
        $data = $this->misc->getDatabaseAccessor();
143
144
        $rules = $data->getRules($_REQUEST[$_REQUEST['subject']]);
145
146
        $reqvars = $this->misc->getRequestVars($_REQUEST['subject']);
147
148
        $attrs = [
149
            'text' => Decorator::field('rulename'),
150
            'icon' => 'Rule',
151
        ];
152
153
        return $this->printTree($rules, $attrs, 'rules');
154
    }
155
156
    /**
157
     * Confirm and then actually create a rule.
158
     *
159
     * @param mixed $confirm
1 ignored issue
show
Missing parameter comment
Loading history...
160
     * @param mixed $msg
1 ignored issue
show
Missing parameter comment
Loading history...
161
     */
162
    public function createRule($confirm, $msg = '')
163
    {
164
        $lang = $this->lang;
165
        $data = $this->misc->getDatabaseAccessor();
166
167
        if (!isset($_POST['name'])) {
168
            $_POST['name'] = '';
169
        }
170
171
        if (!isset($_POST['event'])) {
172
            $_POST['event'] = '';
173
        }
174
175
        if (!isset($_POST['where'])) {
176
            $_POST['where'] = '';
177
        }
178
179
        if (!isset($_POST['type'])) {
180
            $_POST['type'] = 'SOMETHING';
181
        }
182
183
        if (!isset($_POST['raction'])) {
184
            $_POST['raction'] = '';
185
        }
186
187
        if ($confirm) {
188
            $this->printTrail($_REQUEST['subject']);
189
            $this->printTitle($lang['strcreaterule'], 'pg.rule.create');
190
            $this->printMsg($msg);
191
192
            echo '<form action="' . \SUBFOLDER . "/src/views/rules.php\" method=\"post\">\n";
193
            echo "<table>\n";
194
            echo "<tr><th class=\"data left required\">{$lang['strname']}</th>\n";
195
            echo "<td class=\"data1\"><input name=\"name\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"",
196
            htmlspecialchars($_POST['name']), "\" /></td></tr>\n";
197
            echo "<tr><th class=\"data left required\">{$lang['strevent']}</th>\n";
198
            echo "<td class=\"data1\"><select name=\"event\">\n";
199
            foreach ($data->rule_events as $v) {
200
                echo "<option value=\"{$v}\"", ($v == $_POST['event']) ? ' selected="selected"' : '',
201
                    ">{$v}</option>\n";
202
            }
203
            echo "</select></td></tr>\n";
204
            echo "<tr><th class=\"data left\">{$lang['strwhere']}</th>\n";
205
            echo '<td class="data1"><input name="where" size="32" value="',
206
            htmlspecialchars($_POST['where']), "\" /></td></tr>\n";
207
            echo "<tr><th class=\"data left\"><label for=\"instead\">{$lang['strinstead']}</label></th>\n";
208
            echo '<td class="data1">';
209
            echo '<input type="checkbox" id="instead" name="instead" ', (isset($_POST['instead'])) ? ' checked="checked"' : '', " />\n";
210
            echo "</td></tr>\n";
211
            echo "<tr><th class=\"data left required\">{$lang['straction']}</th>\n";
212
            echo '<td class="data1">';
213
            echo '<input type="radio" id="type1" name="type" value="NOTHING"', ('NOTHING' == $_POST['type']) ? ' checked="checked"' : '', " /> <label for=\"type1\">NOTHING</label><br />\n";
214
            echo '<input type="radio" name="type" value="SOMETHING"', ('SOMETHING' == $_POST['type']) ? ' checked="checked"' : '', " />\n";
215
            echo '(<input name="raction" size="32" value="',
216
            htmlspecialchars($_POST['raction']), "\" />)</td></tr>\n";
217
            echo "</table>\n";
218
219
            echo "<input type=\"hidden\" name=\"action\" value=\"save_create_rule\" />\n";
220
            echo '<input type="hidden" name="subject" value="', htmlspecialchars($_REQUEST['subject']), "\" />\n";
221
            echo '<input type="hidden" name="', htmlspecialchars($_REQUEST['subject']),
222
            '" value="', htmlspecialchars($_REQUEST[$_REQUEST['subject']]), "\" />\n";
223
            echo $this->misc->form;
224
            echo "<p><input type=\"submit\" name=\"ok\" value=\"{$lang['strcreate']}\" />\n";
225
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
226
            echo "</form>\n";
227
        } else {
228
            if ('' == trim($_POST['name'])) {
229
                $this->createRule(true, $lang['strruleneedsname']);
230
            } else {
231
                $status = $data->createRule(
232
                    $_POST['name'],
233
                    $_POST['event'],
234
                    $_POST[$_POST['subject']],
235
                    $_POST['where'],
236
                    isset($_POST['instead']),
237
                    $_POST['type'],
238
                    $_POST['raction']
239
                );
240
                if (0 == $status) {
241
                    $this->doDefault($lang['strrulecreated']);
242
                } else {
243
                    $this->createRule(true, $lang['strrulecreatedbad']);
244
                }
245
            }
246
        }
247
    }
248
249
    /**
250
     * Show confirmation of drop and perform actual drop.
251
     *
252
     * @param mixed $confirm
1 ignored issue
show
Missing parameter comment
Loading history...
253
     */
254
    public function doDrop($confirm)
255
    {
256
        $lang = $this->lang;
257
        $data = $this->misc->getDatabaseAccessor();
258
259
        if ($confirm) {
260
            $this->printTrail($_REQUEST['subject']);
261
            $this->printTitle($lang['strdrop'], 'pg.rule.drop');
262
263
            echo '<p>', sprintf(
264
                $lang['strconfdroprule'],
265
                $this->misc->printVal($_REQUEST['rule']),
266
                $this->misc->printVal($_REQUEST[$_REQUEST['reltype']])
267
            ), "</p>\n";
268
269
            echo '<form action="' . \SUBFOLDER . "/src/views/rules.php\" method=\"post\">\n";
270
            echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
271
            echo '<input type="hidden" name="subject" value="', htmlspecialchars($_REQUEST['reltype']), "\" />\n";
272
            echo '<input type="hidden" name="', htmlspecialchars($_REQUEST['reltype']),
273
            '" value="', htmlspecialchars($_REQUEST[$_REQUEST['reltype']]), "\" />\n";
274
            echo '<input type="hidden" name="rule" value="', htmlspecialchars($_REQUEST['rule']), "\" />\n";
275
            echo $this->misc->form;
276
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$lang['strcascade']}</label></p>\n";
277
            echo "<input type=\"submit\" name=\"yes\" value=\"{$lang['stryes']}\" />\n";
278
            echo "<input type=\"submit\" name=\"no\" value=\"{$lang['strno']}\" />\n";
279
            echo "</form>\n";
280
        } else {
281
            $status = $data->dropRule($_POST['rule'], $_POST[$_POST['subject']], isset($_POST['cascade']));
282
            if (0 == $status) {
283
                $this->doDefault($lang['strruledropped']);
284
            } else {
285
                $this->doDefault($lang['strruledroppedbad']);
286
            }
287
        }
288
    }
289
}
290