ImportFacade::executeSqlFiles()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lagdo\DbAdmin\Db\Facades;
4
5
/**
6
 * Facade to import functions
7
 */
8
class ImportFacade extends CommandFacade
9
{
10
    /**
11
     * Get data for import
12
     *
13
     * @return array
14
     */
15
    public function getImportOptions(): array
16
    {
17
        $contents = [];
18
        // From sql.inc.php
19
        $gz = \extension_loaded('zlib') ? '[.gz]' : '';
20
        // ignore post_max_size because it is for all form fields
21
        // together and bytes computing would be necessary.
22
        if ($this->admin->iniBool('file_uploads')) {
23
            $contents['upload'] = "SQL$gz (&lt; " . \ini_get('upload_max_filesize') . 'B)';
24
        } else {
25
            $contents['upload_disabled'] = $this->utils->trans->lang('File uploads are disabled.');
26
        }
27
28
        $importServerPath = $this->admin->importServerPath();
29
        if (($importServerPath)) {
30
            $contents['path'] = $this->utils->str->html($importServerPath) . $gz;
31
        }
32
33
        $labels = [
34
            'path' => $this->utils->trans->lang('Webserver file %s', ''),
35
            'file_upload' => $this->utils->trans->lang('File upload'),
36
            'from_server' => $this->utils->trans->lang('From server'),
37
            'execute' => $this->utils->trans->lang('Execute'),
38
            'run_file' => $this->utils->trans->lang('Run file'),
39
            'select' => $this->utils->trans->lang('Select'),
40
            'error_stops' => $this->utils->trans->lang('Stop on error'),
41
            'only_errors' => $this->utils->trans->lang('Show only errors'),
42
        ];
43
44
        return \compact('contents', 'labels');
45
    }
46
47
    /**
48
     * Get file contents from $_FILES
49
     * From the getFile() function in functions.inc.php
50
     *
51
     * @param array $files
52
     * @param bool $decompress
53
     *
54
     * @return string
55
     */
56
    protected function readFiles(array $files, bool $decompress = false): string
57
    {
58
        $queries = '';
59
        foreach ($files as $name) {
60
            $compressed = \preg_match('~\.gz$~', $name);
61
            if ($decompress && $compressed) {
62
                //! may not be reachable because of open_basedir
63
                $content = \file_get_contents("compress.zlib://$name");
64
                $start = \substr($content, 0, 3);
65
                if (\function_exists("iconv") && \preg_match("~^\xFE\xFF|^\xFF\xFE~", $start, $regs)) {
66
                    // not ternary operator to save memory
67
                    $content = \iconv("utf-16", "utf-8", $content);
68
                } elseif ($start == "\xEF\xBB\xBF") {
69
                    // UTF-8 BOM
70
                    $content = \substr($content, 3);
71
                }
72
                $queries .= $content . "\n\n";
73
            } else {
74
                //! may not be reachable because of open_basedir
75
                $queries .= \file_get_contents($name) . "\n\n";
76
            }
77
        }
78
        //! Does'nt support SQL files not ending with semicolon
79
        return $queries;
80
    }
81
82
    /**
83
     * Run queries from uploaded files
84
     *
85
     * @param array  $files         The uploaded files
86
     * @param bool   $errorStops    Stop executing the requests in case of error
87
     * @param bool   $onlyErrors    Return only errors
88
     *
89
     * @return array
90
     */
91
    public function executeSqlFiles(array $files, bool $errorStops, bool $onlyErrors): array
92
    {
93
        $queries = $this->readFiles($files);
94
        return $this->executeCommands($queries, 0, $errorStops, $onlyErrors);
95
    }
96
}
97