-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Options.php
237 lines (208 loc) · 8.65 KB
/
Options.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Export;
use PhpMyAdmin\Config;
use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\Core;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Encoding;
use PhpMyAdmin\Plugins;
use PhpMyAdmin\Plugins\ExportPlugin;
use PhpMyAdmin\Query\Utilities;
use PhpMyAdmin\Table\Table;
use PhpMyAdmin\Util;
use function explode;
use function function_exists;
use function in_array;
use function is_array;
use function is_string;
use function str_contains;
use function urldecode;
final class Options
{
public function __construct(private Relation $relation, private TemplateModel $templateModel)
{
}
/**
* Outputs appropriate checked statement for checkbox.
*
* @param string $str option name
*/
private function checkboxCheck(string $str): bool
{
$config = Config::getInstance();
return isset($config->settings['Export'][$str])
&& $config->settings['Export'][$str];
}
/**
* Prints Html For Export Selection Options
*
* @param string $tmpSelect Tmp selected method of export
*
* @return array<int, array{name: string, is_selected: bool}>
*/
public function getDatabasesForSelectOptions(string $tmpSelect = ''): array
{
/** @var array|string|null $dbSelect */
$dbSelect = $_POST['db_select'] ?? null;
// Check if the selected databases are defined in $_POST
// (from clicking Back button on /export page)
if (is_string($dbSelect)) {
$dbSelect = urldecode($dbSelect);
$dbSelect = explode(',', $dbSelect);
}
$databases = [];
foreach (DatabaseInterface::getInstance()->getDatabaseList() as $currentDb) {
if (Utilities::isSystemSchema($currentDb, true)) {
continue;
}
$isSelected = false;
if (is_array($dbSelect)) {
if (in_array($currentDb, $dbSelect)) {
$isSelected = true;
}
} elseif ($tmpSelect !== '') {
if (str_contains(' ' . $tmpSelect, '|' . $currentDb . '|')) {
$isSelected = true;
}
} else {
$isSelected = true;
}
$databases[] = ['name' => $currentDb, 'is_selected' => $isSelected];
}
return $databases;
}
/**
* @param string $exportType export type: server|database|table
* @param string $db selected DB
* @param string $table selected table
* @param string $sqlQuery SQL query
* @param int|string $numTables number of tables
* @param int|string $unlimNumRows unlimited number of rows
* @param ExportPlugin[] $exportList
*
* @return array<string, mixed>
*/
public function getOptions(
string $exportType,
string $db,
string $table,
string $sqlQuery,
int|string $numTables,
int|string $unlimNumRows,
array $exportList,
): array {
$exportTemplatesFeature = $this->relation->getRelationParameters()->exportTemplatesFeature;
$templates = [];
$config = Config::getInstance();
if ($exportTemplatesFeature !== null) {
$templates = $this->templateModel->getAll(
$exportTemplatesFeature->database,
$exportTemplatesFeature->exportTemplates,
$config->selectedServer['user'],
$exportType,
);
$templates = is_array($templates) ? $templates : [];
}
$default = isset($_GET['what']) ? (string) $_GET['what'] : Plugins::getDefault('Export', 'format');
$dropdown = Plugins::getChoice($exportList, $default);
$tableObject = new Table($table, $db, DatabaseInterface::getInstance());
$rows = [];
if ($table !== '' && $numTables === 0 && ! $tableObject->isMerge() && $exportType !== 'raw') {
$rows = [
'allrows' => $_POST['allrows'] ?? null,
'limit_to' => $_POST['limit_to'] ?? null,
'limit_from' => $_POST['limit_from'] ?? null,
'unlim_num_rows' => $unlimNumRows,
'number_of_rows' => $tableObject->countRecords(),
];
}
$hasAliases = isset($_SESSION['tmpval']['aliases']) && ! Core::emptyRecursive($_SESSION['tmpval']['aliases']);
$aliases = $_SESSION['tmpval']['aliases'] ?? [];
unset($_SESSION['tmpval']['aliases']);
$filenameTemplate = $this->getFileNameTemplate($exportType, $_POST['filename_template'] ?? null);
$isEncodingSupported = Encoding::isSupported();
$selectedCompression = $_POST['compression'] ?? $config->settings['Export']['compression'] ?? 'none';
if (
isset($config->settings['Export']['as_separate_files']) && $config->settings['Export']['as_separate_files']
) {
$selectedCompression = 'zip';
}
$hiddenInputs = [
'db' => $db,
'table' => $table,
'export_type' => $exportType,
'export_method' => $_POST['export_method'] ?? $config->settings['Export']['method'] ?? 'quick',
'template_id' => $_POST['template_id'] ?? '',
];
if (! empty($GLOBALS['single_table'])) {
$hiddenInputs['single_table'] = true;
}
if ($sqlQuery !== '') {
$hiddenInputs['sql_query'] = $sqlQuery;
}
return [
'export_type' => $exportType,
'db' => $db,
'table' => $table,
'templates' => [
'is_enabled' => $exportTemplatesFeature !== null,
'templates' => $templates,
'selected' => $_POST['template_id'] ?? null,
],
'sql_query' => $sqlQuery,
'hidden_inputs' => $hiddenInputs,
'export_method' => $_POST['quick_or_custom'] ?? $config->settings['Export']['method'] ?? '',
'plugins_choice' => $dropdown,
'options' => Plugins::getOptions('Export', $exportList),
'can_convert_kanji' => Encoding::canConvertKanji(),
'exec_time_limit' => $config->settings['ExecTimeLimit'],
'rows' => $rows,
'has_save_dir' => ! empty($config->settings['SaveDir']),
'save_dir' => Util::userDir($config->settings['SaveDir'] ?? ''),
'export_is_checked' => $this->checkboxCheck('quick_export_onserver'),
'export_overwrite_is_checked' => $this->checkboxCheck('quick_export_onserver_overwrite'),
'has_aliases' => $hasAliases,
'aliases' => $aliases,
'is_checked_lock_tables' => $this->checkboxCheck('lock_tables'),
'is_checked_asfile' => $this->checkboxCheck('asfile'),
'is_checked_as_separate_files' => $this->checkboxCheck('as_separate_files'),
'is_checked_export' => $this->checkboxCheck('onserver'),
'is_checked_export_overwrite' => $this->checkboxCheck('onserver_overwrite'),
'is_checked_remember_file_template' => $this->checkboxCheck('remember_file_template'),
'repopulate' => isset($_POST['repopulate']),
'lock_tables' => isset($_POST['lock_tables']),
'is_encoding_supported' => $isEncodingSupported,
'encodings' => $isEncodingSupported ? Encoding::listEncodings() : [],
'export_charset' => $config->settings['Export']['charset'],
'export_asfile' => $config->settings['Export']['asfile'],
'has_zip' => $config->settings['ZipDump'] && function_exists('gzcompress'),
'has_gzip' => $config->settings['GZipDump'] && function_exists('gzencode'),
'selected_compression' => $selectedCompression,
'filename_template' => $filenameTemplate,
];
}
private function getFileNameTemplate(string $exportType, string|null $filename = null): string
{
if ($filename !== null) {
return $filename;
}
$config = Config::getInstance();
if ($exportType === 'database') {
return (string) $config->getUserValue(
'pma_db_filename_template',
$config->settings['Export']['file_template_database'],
);
}
if ($exportType === 'table') {
return (string) $config->getUserValue(
'pma_table_filename_template',
$config->settings['Export']['file_template_table'],
);
}
return (string) $config->getUserValue(
'pma_server_filename_template',
$config->settings['Export']['file_template_server'],
);
}
}