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

DbexportController   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 177
rs 9.6
c 0
b 0
f 0
wmc 32

1 Method

Rating   Name   Duplication   Size   Complexity  
F render() 0 170 32
1
<?php
2
0 ignored issues
show
Coding Style introduced by
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
/**
10
 * Base controller class.
11
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
12
class DbexportController extends BaseController
13
{
14
    public $controller_name = 'DbexportController';
15
16
    /**
17
     * Default method to render the controller according to the action parameter.
18
     */
19
    public function render()
20
    {
21
        $lang   = $this->lang;
22
        $data   = $this->misc->getDatabaseAccessor();
23
        $action = $this->action;
0 ignored issues
show
Unused Code introduced by
The assignment to $action is dead and can be removed.
Loading history...
24
25
        // Prevent timeouts on large exports
26
        set_time_limit(0);
27
28
        // Include application functions
29
        $f_schema = $f_object = '';
1 ignored issue
show
Unused Code introduced by
The assignment to $f_object is dead and can be removed.
Loading history...
30
        $this->setNoOutput(true);
31
32
        ini_set('memory_limit', '768M');
33
34
        // Are we doing a cluster-wide dump or just a per-database dump
35
        $dumpall = ('server' == $_REQUEST['subject']);
36
        $this->prtrace('REQUEST[subject]', $_REQUEST['subject']);
37
38
        // Check that database dumps are enabled.
39
        if ($this->misc->isDumpEnabled($dumpall)) {
40
            $server_info = $this->misc->getServerInfo();
41
42
            // Get the path of the pg_dump/pg_dumpall executable
43
            $exe = $this->misc->escapeShellCmd($server_info[$dumpall ? 'pg_dumpall_path' : 'pg_dump_path']);
44
45
            // Obtain the pg_dump version number and check if the path is good
46
            $version = [];
47
            preg_match('/(\\d+(?:\\.\\d+)?)(?:\\.\\d+)?.*$/', exec($exe . ' --version'), $version);
48
49
            $this->prtrace('$exe', $exe, 'version', $version[1]);
50
51
            if (empty($version)) {
52
                if ($dumpall) {
53
                    printf($lang['strbadpgdumpallpath'], $server_info['pg_dumpall_path']);
54
                } else {
55
                    printf($lang['strbadpgdumppath'], $server_info['pg_dump_path']);
56
                }
57
58
                return;
59
            }
60
61
            $this->prtrace('REQUEST[output]', $_REQUEST['output']);
62
            // Make it do a download, if necessary
63
            switch ($_REQUEST['output']) {
64
                case 'show':
65
                    header('Content-Type: text/plain');
66
67
                    break;
68
                case 'download':
69
                    // Set headers.  MSIE is totally broken for SSL downloading, so
70
                    // we need to have it download in-place as plain text
71
                    if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') && isset($_SERVER['HTTPS'])) {
72
                        header('Content-Type: text/plain');
73
                    } else {
74
                        header('Content-Type: application/download');
75
                        header('Content-Disposition: attachment; filename=dump.sql');
76
                    }
77
78
                    break;
79
                case 'gzipped':
80
                    // MSIE in SSL mode cannot do this - it should never get to this point
81
                    header('Content-Type: application/download');
82
                    header('Content-Disposition: attachment; filename=dump.sql.gz');
83
84
                    break;
85
            }
86
87
            // Set environmental variables that pg_dump uses
88
            putenv('PGPASSWORD=' . $server_info['password']);
89
            putenv('PGUSER=' . $server_info['username']);
90
            $hostname = $server_info['host'];
91
            if (null !== $hostname && '' != $hostname) {
92
                putenv('PGHOST=' . $hostname);
93
            }
94
            $port = $server_info['port'];
95
            if (null !== $port && '' != $port) {
96
                putenv('PGPORT=' . $port);
97
            }
98
99
            // Build command for executing pg_dump.  '-i' means ignore version differences.
100
            if (((float) $version[1]) < 9.5) {
101
                $cmd = $exe . ' -i';
102
            } else {
103
                $cmd = $exe;
104
            }
105
106
            // we are PG 7.4+, so we always have a schema
107
            if (isset($_REQUEST['schema'])) {
108
                $f_schema = $_REQUEST['schema'];
109
                $data->fieldClean($f_schema);
110
            }
111
112
            // Check for a specified table/view
113
            switch ($_REQUEST['subject']) {
114
                case 'schema':
115
                    // This currently works for 8.2+ (due to the orthoganl -t -n issue introduced then)
116
                    $cmd .= ' -n ' . $this->misc->escapeShellArg("\"{$f_schema}\"");
117
118
                    break;
119
                case 'table':
120
                case 'view':
121
                    $f_object = $_REQUEST[$_REQUEST['subject']];
122
                    $this->prtrace('f_object', $f_object);
123
                    $data->fieldClean($f_object);
124
125
                    // Starting in 8.2, -n and -t are orthagonal, so we now schema qualify
126
                    // the table name in the -t argument and quote both identifiers
127
                    if (((float) $version[1]) >= 8.2) {
128
                        $cmd .= ' -t ' . $this->misc->escapeShellArg("\"{$f_schema}\".\"{$f_object}\"");
129
                    } else {
130
                        // If we are 7.4 or higher, assume they are using 7.4 pg_dump and
131
                        // set dump schema as well.  Also, mixed case dumping has been fixed
132
                        // then..
133
                        $cmd .= ' -t ' . $this->misc->escapeShellArg($f_object)
134
                        . ' -n ' . $this->misc->escapeShellArg($f_schema);
135
                    }
136
            }
137
138
            // Check for GZIP compression specified
139
            if ('gzipped' == $_REQUEST['output'] && !$dumpall) {
140
                $cmd .= ' -Z 9';
141
            }
142
143
            switch ($_REQUEST['what']) {
144
                case 'dataonly':
145
                    $cmd .= ' -a';
146
                    if ('sql' == $_REQUEST['d_format']) {
147
                        $cmd .= ' --inserts';
148
                    } elseif (isset($_REQUEST['d_oids'])) {
149
                        $cmd .= ' -o';
150
                    }
151
152
                    break;
153
                case 'structureonly':
154
                    $cmd .= ' -s';
155
                    if (isset($_REQUEST['s_clean'])) {
156
                        $cmd .= ' -c';
157
                    }
158
159
                    break;
160
                case 'structureanddata':
161
                    if ('sql' == $_REQUEST['sd_format']) {
162
                        $cmd .= ' --inserts';
163
                    } elseif (isset($_REQUEST['sd_oids'])) {
164
                        $cmd .= ' -o';
165
                    }
166
167
                    if (isset($_REQUEST['sd_clean'])) {
168
                        $cmd .= ' -c';
169
                    }
170
171
                    break;
172
            }
173
174
            if (!$dumpall) {
175
                putenv('PGDATABASE=' . $_REQUEST['database']);
176
            }
177
178
            $this->prtrace('ENV VARS', [
1 ignored issue
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
179
                'PGUSER'     => getenv('PGUSER'),
180
                'PGPASSWORD' => getenv('PGPASSWORD'),
181
                'PGHOST'     => getenv('PGHOST'),
182
                'PGPORT'     => getenv('PGPORT'),
183
                'PGDATABASE' => getenv('PGDATABASE'),
184
            ]);
1 ignored issue
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
185
            $this->prtrace('cmd', $cmd);
186
187
            // Execute command and return the output to the screen
188
            passthru($cmd);
189
        }
190
    }
191
}
192