Test Failed
Branch develop (db5506)
by Felipe
03:46
created

AlldbController::doSaveCreate()   C

Complexity

Conditions 7
Paths 48

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 22
nc 48
nop 0
dl 0
loc 38
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace PHPPgAdmin\Controller;
4
5
use \PHPPgAdmin\Decorators\Decorator;
6
7
/**
8
 * Base controller class
9
 */
10
class AlldbController extends BaseController
11
{
12
    public $_name       = 'AlldbController';
13
    public $table_place = 'alldb-databases';
14
15
    public function render()
0 ignored issues
show
Coding Style introduced by
render uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
render uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
16
    {
17
        $conf   = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
18
        $misc   = $this->misc;
0 ignored issues
show
Unused Code introduced by
The assignment to $misc is dead and can be removed.
Loading history...
19
        $lang   = $this->lang;
20
        $action = $this->action;
21
22
        if ($action == 'tree') {
23
            return $this->doTree();
24
        }
25
26
        $this->printHeader($lang['strdatabases']);
27
        $this->printBody();
28
29
        switch ($action) {
30
            case 'export':
31
                $this->doExport();
32
                break;
33
            case 'save_create':
34
                if (isset($_POST['cancel'])) {
35
                    $this->doDefault();
36
                } else {
37
                    $this->doSaveCreate();
38
                }
39
40
                break;
41
            case 'create':
42
                $this->doCreate();
43
                break;
44
            case 'drop':
45
                if (isset($_REQUEST['drop'])) {
46
                    $this->doDrop(false);
47
                } else {
48
                    $this->doDefault();
49
                }
50
51
                break;
52
            case 'confirm_drop':
53
                doDrop(true);
0 ignored issues
show
Bug introduced by
The function doDrop was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

53
                /** @scrutinizer ignore-call */ doDrop(true);
Loading history...
54
                break;
55
            case 'alter':
56
                if (isset($_POST['oldname']) && isset($_POST['newname']) && !isset($_POST['cancel'])) {
57
                    $this->doAlter(false);
58
                } else {
59
                    $this->doDefault();
60
                }
61
62
                break;
63
            case 'confirm_alter':
64
                $this->doAlter(true);
65
                break;
66
            default:
67
                $this->doDefault();
68
69
                break;
70
        }
71
72
        return $this->printFooter();
73
    }
74
75
    /**
76
     * Show default list of databases in the server
77
     */
78
    public function doDefault($msg = '')
0 ignored issues
show
Coding Style introduced by
doDefault uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
79
    {
80
        $conf = $this->conf;
81
        $misc = $this->misc;
82
        $lang = $this->lang;
83
84
        $this->printTrail('server');
85
        $this->printTabs('server', 'databases');
86
        $this->printMsg($msg);
87
        $data      = $misc->getDatabaseAccessor();
88
        $databases = $data->getDatabases();
89
90
        $columns = [
91
            'database'   => [
92
                'title' => $lang['strdatabase'],
93
                'field' => Decorator::field('datname'),
94
                'url'   => SUBFOLDER . "/redirect/database?{$misc->href}&amp;",
95
                'vars'  => ['database' => 'datname'],
96
            ],
97
            'owner'      => [
98
                'title' => $lang['strowner'],
99
                'field' => Decorator::field('datowner'),
100
            ],
101
            'encoding'   => [
102
                'title' => $lang['strencoding'],
103
                'field' => Decorator::field('datencoding'),
104
            ],
105
            'lc_collate' => [
106
                'title' => $lang['strcollation'],
107
                'field' => Decorator::field('datcollate'),
108
            ],
109
            'lc_ctype'   => [
110
                'title' => $lang['strctype'],
111
                'field' => Decorator::field('datctype'),
112
            ],
113
            'tablespace' => [
114
                'title' => $lang['strtablespace'],
115
                'field' => Decorator::field('tablespace'),
116
            ],
117
            'dbsize'     => [
118
                'title' => $lang['strsize'],
119
                'field' => Decorator::field('dbsize'),
120
                'type'  => 'prettysize',
121
            ],
122
            'actions'    => [
123
                'title' => $lang['stractions'],
124
            ],
125
            'comment'    => [
126
                'title' => $lang['strcomment'],
127
                'field' => Decorator::field('datcomment'),
128
            ],
129
        ];
130
131
        $actions = [
132
            'multiactions' => [
133
                'keycols' => ['database' => 'datname'],
134
                'url'     => 'alldb.php',
135
                'default' => null,
136
            ],
137
            'drop'         => [
138
                'content'     => $lang['strdrop'],
139
                'attr'        => [
140
                    'href' => [
141
                        'url'     => 'alldb.php',
142
                        'urlvars' => [
143
                            'subject'      => 'database',
144
                            'action'       => 'confirm_drop',
145
                            'dropdatabase' => Decorator::field('datname'),
146
                        ],
147
                    ],
148
                ],
149
                'multiaction' => 'confirm_drop',
150
            ],
151
            'privileges'   => [
152
                'content' => $lang['strprivileges'],
153
                'attr'    => [
154
                    'href' => [
155
                        'url'     => 'privileges.php',
156
                        'urlvars' => [
157
                            'subject'  => 'database',
158
                            'database' => Decorator::field('datname'),
159
                        ],
160
                    ],
161
                ],
162
            ],
163
        ];
164
        if ($data->hasAlterDatabase()) {
165
            $actions['alter'] = [
166
                'content' => $lang['stralter'],
167
                'attr'    => [
168
                    'href' => [
169
                        'url'     => 'alldb.php',
170
                        'urlvars' => [
171
                            'subject'       => 'database',
172
                            'action'        => 'confirm_alter',
173
                            'alterdatabase' => Decorator::field('datname'),
174
                        ],
175
                    ],
176
                ],
177
            ];
178
        }
179
180
        if (!$data->hasTablespaces()) {
181
            unset($columns['tablespace']);
182
        }
183
184
        if (!$data->hasServerAdminFuncs()) {
185
            unset($columns['dbsize']);
186
        }
187
188
        if (!$data->hasDatabaseCollation()) {
189
            unset($columns['lc_collate'], $columns['lc_ctype']);
190
        }
191
192
        if (!isset($data->privlist['database'])) {
193
            unset($actions['privileges']);
194
        }
195
196
        echo $this->printTable($databases, $columns, $actions, $this->table_place, $lang['strnodatabases']);
197
198
        $navlinks = [
199
            'create' => [
200
                'attr'    => [
201
                    'href' => [
202
                        'url'     => 'alldb.php',
203
                        'urlvars' => [
204
                            'action' => 'create',
205
                            'server' => $_REQUEST['server'],
206
                        ],
207
                    ],
208
                ],
209
                'content' => $lang['strcreatedatabase'],
210
            ],
211
        ];
212
        $this->printNavLinks($navlinks, $this->table_place, get_defined_vars());
213
    }
214
215 View Code Duplication
    public function doTree()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
216
    {
217
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
218
        $misc = $this->misc;
219
        $lang = $this->lang;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is dead and can be removed.
Loading history...
220
        $data = $misc->getDatabaseAccessor();
221
222
        $databases = $data->getDatabases();
223
224
        $reqvars = $misc->getRequestVars('database');
225
226
        //$this->prtrace($reqvars);
227
228
        $attrs = [
229
            'text'    => Decorator::field('datname'),
230
            'icon'    => 'Database',
231
            'toolTip' => Decorator::field('datcomment'),
232
            'action'  => Decorator::redirecturl('redirect.php', $reqvars, ['database' => Decorator::field('datname')]),
233
            'branch'  => Decorator::url('/src/views/database', $reqvars, ['action' => 'tree', 'database' => Decorator::field('datname')]),
234
235
        ];
236
237
        return $this->printTree($databases, $attrs, 'databases');
238
    }
239
240
    /**
241
     * Display a form for alter and perform actual alter
242
     */
243
    public function doAlter($confirm)
0 ignored issues
show
Coding Style introduced by
doAlter uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
doAlter uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
244
    {
245
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
246
        $misc = $this->misc;
247
        $lang = $this->lang;
248
        $data = $misc->getDatabaseAccessor();
249
250
        if ($confirm) {
251
            $this->printTrail('database');
252
            $this->printTitle($lang['stralter'], 'pg.database.alter');
253
254
            echo '<form action="' . SUBFOLDER . "/src/views/alldb.php\" method=\"post\">\n";
255
            echo "<table>\n";
256
            echo "<tr><th class=\"data left required\">{$lang['strname']}</th>\n";
257
            echo '<td class="data1">';
258
            echo "<input name=\"newname\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
259
            htmlspecialchars($_REQUEST['alterdatabase']), "\" /></td></tr>\n";
260
261
            if ($data->hasAlterDatabaseOwner() && $data->isSuperUser()) {
262
                // Fetch all users
263
264
                $rs    = $data->getDatabaseOwner($_REQUEST['alterdatabase']);
265
                $owner = isset($rs->fields['usename']) ? $rs->fields['usename'] : '';
266
                $users = $data->getUsers();
267
268
                echo "<tr><th class=\"data left required\">{$lang['strowner']}</th>\n";
269
                echo '<td class="data1"><select name="owner">';
270
                while (!$users->EOF) {
271
                    $uname = $users->fields['usename'];
272
                    echo '<option value="', htmlspecialchars($uname), '"',
273
                    ($uname == $owner) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), "</option>\n";
274
                    $users->moveNext();
275
                }
276
                echo "</select></td></tr>\n";
277
            }
278
            if ($data->hasSharedComments()) {
279
                $rs      = $data->getDatabaseComment($_REQUEST['alterdatabase']);
280
                $comment = isset($rs->fields['description']) ? $rs->fields['description'] : '';
281
                echo "<tr><th class=\"data left\">{$lang['strcomment']}</th>\n";
282
                echo '<td class="data1">';
283
                echo '<textarea rows="3" cols="32" name="dbcomment">',
284
                htmlspecialchars($comment), "</textarea></td></tr>\n";
285
            }
286
            echo "</table>\n";
287
            echo "<input type=\"hidden\" name=\"action\" value=\"alter\" />\n";
288
            echo $misc->form;
289
            echo '<input type="hidden" name="oldname" value="',
290
            htmlspecialchars($_REQUEST['alterdatabase']), "\" />\n";
291
            echo "<input type=\"submit\" name=\"alter\" value=\"{$lang['stralter']}\" />\n";
292
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
293
            echo "</form>\n";
294
        } else {
295
            if (!isset($_POST['owner'])) {
296
                $_POST['owner'] = '';
297
            }
298
299
            if (!isset($_POST['dbcomment'])) {
300
                $_POST['dbcomment'] = '';
301
            }
302
303
            if ($data->alterDatabase($_POST['oldname'], $_POST['newname'], $_POST['owner'], $_POST['dbcomment']) == 0) {
304
                $this->misc->setReloadBrowser(true);
305
                $this->doDefault($lang['strdatabasealtered']);
306
            } else {
307
                $this->doDefault($lang['strdatabasealteredbad']);
308
            }
309
        }
310
    }
311
312
    /**
313
     * Show confirmation of drop and perform actual drop
314
     */
315
    public function doDrop($confirm)
0 ignored issues
show
Coding Style introduced by
doDrop uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
doDrop uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
316
    {
317
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
318
        $misc = $this->misc;
319
        $lang = $this->lang;
320
        $data = $misc->getDatabaseAccessor();
321
322
        if (empty($_REQUEST['dropdatabase']) && empty($_REQUEST['ma'])) {
323
            $this->doDefault($lang['strspecifydatabasetodrop']);
324
            exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method doDrop() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
325
        }
326
327
        if ($confirm) {
328
            $this->printTrail('database');
329
            $this->printTitle($lang['strdrop'], 'pg.database.drop');
330
331
            echo '<form action="' . SUBFOLDER . "/src/views/alldb.php\" method=\"post\">\n";
332
            //If multi drop
333
            if (isset($_REQUEST['ma'])) {
334
                foreach ($_REQUEST['ma'] as $v) {
335
                    $a = unserialize(htmlspecialchars_decode($v, ENT_QUOTES));
0 ignored issues
show
Security introduced by
htmlspecialchars_decode(...\Controller\ENT_QUOTES) can contain request data and is used in unserialized context(s) leading to a potential security vulnerability.

1 path for user data to reach this point

  1. Read from $_REQUEST, and $_REQUEST['ma'] is assigned to $v
    in src/controllers/AlldbController.php on line 334
  2. Data is passed through htmlspecialchars_decode()
    in src/controllers/AlldbController.php on line 335

Preventing Object Injection Attacks

If you pass raw user-data to unserialize() for example, this can be used to create an object of any class that is available in your local filesystem. For an attacker, classes that have magic methods like __destruct or __wakeup are particularly interesting in such a case, as they can be exploited very easily.

We recommend to not pass user data to such a function. In case of unserialize, better use JSON to transfer data.

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
336
                    echo '<p>', sprintf($lang['strconfdropdatabase'], $misc->printVal($a['database'])), "</p>\n";
337
                    printf('<input type="hidden" name="dropdatabase[]" value="%s" />', htmlspecialchars($a['database']));
338
                }
339
            } else {
340
                echo '<p>', sprintf($lang['strconfdropdatabase'], $misc->printVal($_REQUEST['dropdatabase'])), "</p>\n";
341
                echo '<input type="hidden" name="dropdatabase" value="', htmlspecialchars($_REQUEST['dropdatabase']), "\" />\n";
342
            } // END if multi drop
343
344
            echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
345
            echo $misc->form;
346
            echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
347
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
348
            echo "</form>\n";
349
        } // END confirm
350
        else {
351
            //If multi drop
352
            if (is_array($_REQUEST['dropdatabase'])) {
353
                $msg = '';
354
                foreach ($_REQUEST['dropdatabase'] as $d) {
355
                    $status = $data->dropDatabase($d);
356
                    if ($status == 0) {
357
                        $msg .= sprintf('%s: %s<br />', htmlentities($d, ENT_QUOTES, 'UTF-8'), $lang['strdatabasedropped']);
358
                    } else {
359
                        $this->doDefault(sprintf('%s%s: %s<br />', $msg, htmlentities($d, ENT_QUOTES, 'UTF-8'), $lang['strdatabasedroppedbad']));
360
                        return;
361
                    }
362
                } // Everything went fine, back to Default page...
363
                $this->setReloadDropDatabase(true);
364
                $this->doDefault($msg);
365
            } else {
366
                $status = $data->dropDatabase($_POST['dropdatabase']);
367
                if ($status == 0) {
368
                    $this->setReloadDropDatabase(true);
369
                    $this->doDefault($lang['strdatabasedropped']);
370
                } else {
371
                    $this->doDefault($lang['strdatabasedroppedbad']);
372
                }
373
            }
374
        } //END DROP
375
    }
376
377
    // END FUNCTION
378
379
    /**
380
     * Displays a screen where they can enter a new database
381
     */
382
    public function doCreate($msg = '')
0 ignored issues
show
Coding Style introduced by
doCreate uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
383
    {
384
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
385
        $misc = $this->misc;
386
        $lang = $this->lang;
387
        $data = $misc->getDatabaseAccessor();
388
389
        $this->printTrail('server');
390
        $this->printTitle($lang['strcreatedatabase'], 'pg.database.create');
391
        $this->printMsg($msg);
392
393
        if (!isset($_POST['formName'])) {
394
            $_POST['formName'] = '';
395
        }
396
397
        // Default encoding is that in language file
398
        if (!isset($_POST['formEncoding'])) {
399
            $_POST['formEncoding'] = '';
400
        }
401
        if (!isset($_POST['formTemplate'])) {
402
            $_POST['formTemplate'] = 'template1';
403
        }
404
405
        if (!isset($_POST['formSpc'])) {
406
            $_POST['formSpc'] = '';
407
        }
408
409
        if (!isset($_POST['formComment'])) {
410
            $_POST['formComment'] = '';
411
        }
412
413
        // Fetch a list of databases in the cluster
414
        $templatedbs = $data->getDatabases(false);
415
416
        // Fetch all tablespaces from the database
417
        if ($data->hasTablespaces()) {
418
            $tablespaces = $data->getTablespaces();
419
        }
420
421
        echo '<form action="' . SUBFOLDER . "/src/views/alldb.php\" method=\"post\">\n";
422
        echo "<table>\n";
423
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n";
424
        echo "\t\t<td class=\"data1\"><input name=\"formName\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
425
        htmlspecialchars($_POST['formName']), "\" /></td>\n\t</tr>\n";
426
427
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strtemplatedb']}</th>\n";
428
        echo "\t\t<td class=\"data1\">\n";
429
        echo "\t\t\t<select name=\"formTemplate\">\n";
430
        // Always offer template0 and template1
431
        echo "\t\t\t\t<option value=\"template0\"",
432
        ($_POST['formTemplate'] == 'template0') ? ' selected="selected"' : '', ">template0</option>\n";
433
        echo "\t\t\t\t<option value=\"template1\"",
434
        ($_POST['formTemplate'] == 'template1') ? ' selected="selected"' : '', ">template1</option>\n";
435
        while (!$templatedbs->EOF) {
436
            $dbname = htmlspecialchars($templatedbs->fields['datname']);
437
            if ($dbname != 'template1') {
438
                // filter out for $conf[show_system] users so we dont get duplicates
439
                echo "\t\t\t\t<option value=\"{$dbname}\"",
440
                ($dbname == $_POST['formTemplate']) ? ' selected="selected"' : '', ">{$dbname}</option>\n";
441
            }
442
            $templatedbs->moveNext();
443
        }
444
        echo "\t\t\t</select>\n";
445
        echo "\t\t</td>\n\t</tr>\n";
446
447
        // ENCODING
448
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strencoding']}</th>\n";
449
        echo "\t\t<td class=\"data1\">\n";
450
        echo "\t\t\t<select name=\"formEncoding\">\n";
451
        echo "\t\t\t\t<option value=\"\"></option>\n";
452
        while (list($key) = each($data->codemap)) {
453
            echo "\t\t\t\t<option value=\"", htmlspecialchars($key), '"',
454
            ($key == $_POST['formEncoding']) ? ' selected="selected"' : '', '>',
455
            $misc->printVal($key), "</option>\n";
456
        }
457
        echo "\t\t\t</select>\n";
458
        echo "\t\t</td>\n\t</tr>\n";
459
460
        if ($data->hasDatabaseCollation()) {
461
            if (!isset($_POST['formCollate'])) {
462
                $_POST['formCollate'] = '';
463
            }
464
465
            if (!isset($_POST['formCType'])) {
466
                $_POST['formCType'] = '';
467
            }
468
469
            // LC_COLLATE
470
            echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcollation']}</th>\n";
471
            echo "\t\t<td class=\"data1\">\n";
472
            echo "\t\t\t<input name=\"formCollate\" value=\"", htmlspecialchars($_POST['formCollate']), "\" />\n";
473
            echo "\t\t</td>\n\t</tr>\n";
474
475
            // LC_CTYPE
476
            echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strctype']}</th>\n";
477
            echo "\t\t<td class=\"data1\">\n";
478
            echo "\t\t\t<input name=\"formCType\" value=\"", htmlspecialchars($_POST['formCType']), "\" />\n";
479
            echo "\t\t</td>\n\t</tr>\n";
480
        }
481
482
        // Tablespace (if there are any)
483 View Code Duplication
        if ($data->hasTablespaces() && $tablespaces->recordCount() > 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $tablespaces does not seem to be defined for all execution paths leading up to this point.
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
484
            echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strtablespace']}</th>\n";
485
            echo "\t\t<td class=\"data1\">\n\t\t\t<select name=\"formSpc\">\n";
486
            // Always offer the default (empty) option
487
            echo "\t\t\t\t<option value=\"\"",
488
            ($_POST['formSpc'] == '') ? ' selected="selected"' : '', "></option>\n";
489
            // Display all other tablespaces
490
            while (!$tablespaces->EOF) {
491
                $spcname = htmlspecialchars($tablespaces->fields['spcname']);
492
                echo "\t\t\t\t<option value=\"{$spcname}\"",
493
                ($spcname == $_POST['formSpc']) ? ' selected="selected"' : '', ">{$spcname}</option>\n";
494
                $tablespaces->moveNext();
495
            }
496
            echo "\t\t\t</select>\n\t\t</td>\n\t</tr>\n";
497
        }
498
499
        // Comments (if available)
500 View Code Duplication
        if ($data->hasSharedComments()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
501
            echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n";
502
            echo "\t\t<td><textarea name=\"formComment\" rows=\"3\" cols=\"32\">",
503
            htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>\n";
504
        }
505
506
        echo "</table>\n";
507
        echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
508
        echo $misc->form;
509
        echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
510
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
511
        echo "</form>\n";
512
    }
513
514
    /**
515
     * Actually creates the new view in the database
516
     */
517
    public function doSaveCreate()
0 ignored issues
show
Coding Style introduced by
doSaveCreate uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
518
    {
519
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
520
        $misc = $this->misc;
521
        $lang = $this->lang;
522
        $data = $misc->getDatabaseAccessor();
523
524
        // Default tablespace to null if it isn't set
525
        if (!isset($_POST['formSpc'])) {
526
            $_POST['formSpc'] = null;
527
        }
528
529
        // Default comment to blank if it isn't set
530
        if (!isset($_POST['formComment'])) {
531
            $_POST['formComment'] = null;
532
        }
533
534
        // Default collate to blank if it isn't set
535
        if (!isset($_POST['formCollate'])) {
536
            $_POST['formCollate'] = null;
537
        }
538
539
        // Default ctype to blank if it isn't set
540
        if (!isset($_POST['formCType'])) {
541
            $_POST['formCType'] = null;
542
        }
543
544
        // Check that they've given a name and a definition
545
        if ($_POST['formName'] == '') {
546
            $this->doCreate($lang['strdatabaseneedsname']);
547
        } else {
548
            $status = $data->createDatabase($_POST['formName'], $_POST['formEncoding'], $_POST['formSpc'],
549
                $_POST['formComment'], $_POST['formTemplate'], $_POST['formCollate'], $_POST['formCType']);
550
            if ($status == 0) {
551
                $this->misc->setReloadBrowser(true);
552
                $this->doDefault($lang['strdatabasecreated']);
553
            } else {
554
                $this->doCreate($lang['strdatabasecreatedbad']);
555
            }
556
        }
557
    }
558
559
    /**
560
     * Displays options for cluster download
561
     */
562
    public function doExport($msg = '')
563
    {
564
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
565
        $misc = $this->misc;
566
        $lang = $this->lang;
567
        $data = $misc->getDatabaseAccessor();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
568
569
        $this->printTrail('server');
570
        $this->printTabs('server', 'export');
571
        $this->printMsg($msg);
572
573
        echo '<form action="' . SUBFOLDER . "/src/views/dbexport.php\" method=\"post\">\n";
574
        echo "<table>\n";
575
        echo "<tr><th class=\"data\">{$lang['strformat']}</th><th class=\"data\">{$lang['stroptions']}</th></tr>\n";
576
        // Data only
577
        echo '<tr><th class="data left" rowspan="2">';
578
        echo "<input type=\"radio\" id=\"what1\" name=\"what\" value=\"dataonly\" checked=\"checked\" /><label for=\"what1\">{$lang['strdataonly']}</label></th>\n";
579
        echo "<td>{$lang['strformat']}\n";
580
        echo "<select name=\"d_format\">\n";
581
        echo "<option value=\"copy\">COPY</option>\n";
582
        echo "<option value=\"sql\">SQL</option>\n";
583
        echo "</select>\n</td>\n</tr>\n";
584
        echo "<tr><td><input type=\"checkbox\" id=\"d_oids\" name=\"d_oids\" /><label for=\"d_oids\">{$lang['stroids']}</label></td>\n</tr>\n";
585
        // Structure only
586
        echo "<tr><th class=\"data left\"><input type=\"radio\" id=\"what2\" name=\"what\" value=\"structureonly\" /><label for=\"what2\">{$lang['strstructureonly']}</label></th>\n";
587
        echo "<td><input type=\"checkbox\" id=\"s_clean\" name=\"s_clean\" /><label for=\"s_clean\">{$lang['strdrop']}</label></td>\n</tr>\n";
588
        // Structure and data
589
        echo '<tr><th class="data left" rowspan="3">';
590
        echo "<input type=\"radio\" id=\"what3\" name=\"what\" value=\"structureanddata\" /><label for=\"what3\">{$lang['strstructureanddata']}</label></th>\n";
591
        echo "<td>{$lang['strformat']}\n";
592
        echo "<select name=\"sd_format\">\n";
593
        echo "<option value=\"copy\">COPY</option>\n";
594
        echo "<option value=\"sql\">SQL</option>\n";
595
        echo "</select>\n</td>\n</tr>\n";
596
        echo "<tr><td><input type=\"checkbox\" id=\"sd_clean\" name=\"sd_clean\" /><label for=\"sd_clean\">{$lang['strdrop']}</label></td>\n</tr>\n";
597
        echo "<tr><td><input type=\"checkbox\" id=\"sd_oids\" name=\"sd_oids\" /><label for=\"sd_oids\">{$lang['stroids']}</label></td>\n</tr>\n";
598
        echo "</table>\n";
599
600
        echo "<h3>{$lang['stroptions']}</h3>\n";
601
        echo "<p><input type=\"radio\" id=\"output1\" name=\"output\" value=\"show\" checked=\"checked\" /><label for=\"output1\">{$lang['strshow']}</label>\n";
602
        echo "<br/><input type=\"radio\" id=\"output2\" name=\"output\" value=\"download\" /><label for=\"output2\">{$lang['strdownload']}</label></p>\n";
603
604
        echo "<p><input type=\"hidden\" name=\"action\" value=\"export\" />\n";
605
        echo "<input type=\"hidden\" name=\"subject\" value=\"server\" />\n";
606
        echo $misc->form;
607
        echo "<input type=\"submit\" value=\"{$lang['strexport']}\" /></p>\n";
608
        echo "</form>\n";
609
    }
610
}
611