|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Db\Facades; |
|
4
|
|
|
|
|
5
|
|
|
use function array_map; |
|
6
|
|
|
use function extension_loaded; |
|
7
|
|
|
use function file_get_contents; |
|
8
|
|
|
use function function_exists; |
|
9
|
|
|
use function iconv; |
|
10
|
|
|
use function implode; |
|
11
|
|
|
use function ini_get; |
|
12
|
|
|
use function preg_match; |
|
13
|
|
|
use function substr; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Facade to import functions |
|
17
|
|
|
*/ |
|
18
|
|
|
class ImportFacade extends CommandFacade |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Get data for import |
|
22
|
|
|
* |
|
23
|
|
|
* @return array |
|
24
|
|
|
*/ |
|
25
|
|
|
public function getImportOptions(): array |
|
26
|
|
|
{ |
|
27
|
|
|
// From sql.inc.php |
|
28
|
|
|
$gz = extension_loaded('zlib') ? '[.gz]' : ''; |
|
29
|
|
|
// ignore post_max_size because it is for all form fields |
|
30
|
|
|
// together and bytes computing would be necessary. |
|
31
|
|
|
$contents = $this->admin->iniBool('file_uploads') ? |
|
32
|
|
|
['upload' => "SQL$gz (< " . ini_get('upload_max_filesize') . 'B)'] : |
|
33
|
|
|
['upload_disabled' => $this->utils->trans->lang('File uploads are disabled.')]; |
|
34
|
|
|
if (($importServerPath = $this->admin->importServerPath())) { |
|
35
|
|
|
$contents['path'] = $this->utils->str->html($importServerPath) . $gz; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return ['contents' => $contents]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* From the get_file() function in functions.inc.php |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $file |
|
45
|
|
|
* @param bool $decompress |
|
46
|
|
|
* |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function readFile(string $file, bool $decompress = false): string |
|
50
|
|
|
{ |
|
51
|
|
|
$compressed = preg_match('~\.gz$~', $file); |
|
52
|
|
|
if (!$decompress || !$compressed) { |
|
53
|
|
|
//! may not be reachable because of open_basedir |
|
54
|
|
|
return file_get_contents($file); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
//! may not be reachable because of open_basedir |
|
58
|
|
|
$content = file_get_contents("compress.zlib://$file"); |
|
59
|
|
|
$start = substr($content, 0, 3); |
|
60
|
|
|
return match(true) { |
|
61
|
|
|
preg_match("~^\xFE\xFF|^\xFF\xFE~", $start, $regs) && |
|
62
|
|
|
function_exists("iconv") => iconv("utf-16", "utf-8", $content), |
|
63
|
|
|
// UTF-8 BOM |
|
64
|
|
|
$start === "\xEF\xBB\xBF" => substr($content, 3), |
|
65
|
|
|
default => $content, |
|
66
|
|
|
}; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Run queries from uploaded files |
|
71
|
|
|
* |
|
72
|
|
|
* @param array $files The uploaded files |
|
73
|
|
|
* @param bool $errorStops Stop executing the requests in case of error |
|
74
|
|
|
* @param bool $onlyErrors Return only errors |
|
75
|
|
|
* |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
|
|
public function executeSqlFiles(array $files, bool $errorStops, bool $onlyErrors): array |
|
79
|
|
|
{ |
|
80
|
|
|
$queries = array_map(fn($file) => $this->readFile($file), $files); |
|
81
|
|
|
$queries = implode("\n\n", $queries); |
|
82
|
|
|
return $this->executeCommands($queries, 0, $errorStops, $onlyErrors); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|