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