Passed
Pull Request — develop (#92)
by Felipe
04:47
created

DbexportController   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Importance

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

1 Method

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