forked from phpmyadmin/phpmyadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_details_importdocsql.php
296 lines (262 loc) · 12.5 KB
/
db_details_importdocsql.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/* $Id$ */
// vim: expandtab sw=4 ts=4 sts=4:
require_once('./libraries/common.lib.php');
/**
* This script imports relation infos from docSQL (www.databay.de)
*/
/**
* Get the values of the variables posted or sent to this script and display
* the headers
*/
require_once('./libraries/read_dump.lib.php');
require_once('./libraries/header.inc.php');
// Check parameters
PMA_checkParameters(array('db'));
// We do any work, only if docSQL import was enabled in config
if (isset($cfg['docSQLDir']) && !empty($cfg['docSQLDir'])) {
if (substr($cfg['docSQLDir'], -1) != '/') {
$cfg['docSQLDir'] .= '/';
}
/**
* Imports docSQL files
*
* @param string the basepath
* @param string the filename
* @param string the complete filename
* @param string the content of a file
*
* @return boolean always true
*
* @global array GLOBAL variables
*/
function docsql_check($docpath = '', $file = '', $filename = '', $content = 'none') {
global $GLOBALS;
if (preg_match('@^(.*)_field_comment\.(txt|zip|bz2|bzip).*$@i', $filename)) {
$tab = preg_replace('@^(.*)_field_comment\.(txt|zip|bz2|bzip).*@si', '\1', $filename);
//echo '<h1>Working on Table ' . $_tab . '</h1>';
if ($content == 'none') {
$lines = array();
$fd = fopen($docpath . $file, 'r');
if ($fd) {
while (!feof($fd)) {
$lines[] = fgets($fd, 4096);
}
}
} else {
$content = str_replace("\r\n", "\n", $content);
$content = str_replace("\r", "\n", $content);
$lines = explode("\n", $content);
}
if (isset($lines) && is_array($lines) && count($lines) > 0) {
foreach ($lines AS $lkey => $line) {
//echo '<p>' . $line . '</p>';
$inf = explode('|',$line);
if (!empty($inf[1]) && strlen(trim($inf[1])) > 0) {
$qry = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['column_info'])
. ' (db_name, table_name, column_name, ' . PMA_backquote('comment') . ') '
. ' VALUES('
. '\'' . PMA_sqlAddslashes($GLOBALS['db']) . '\','
. '\'' . PMA_sqlAddslashes(trim($tab)) . '\','
. '\'' . PMA_sqlAddslashes(trim($inf[0])) . '\','
. '\'' . PMA_sqlAddslashes(trim($inf[1])) . '\')';
if (PMA_query_as_cu($qry)) {
echo '<p>' . $GLOBALS['strAddedColumnComment'] . ' ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]) . '</p>';
} else {
echo '<p>' . $GLOBALS['strWritingCommentNotPossible'] . '</p>';
}
echo "\n";
} // end inf[1] exists
if (!empty($inf[2]) && strlen(trim($inf[2])) > 0) {
$for = explode('->', $inf[2]);
$qry = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['relation'])
. '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
. ' VALUES('
. '\'' . PMA_sqlAddslashes($GLOBALS['db']) . '\', '
. '\'' . PMA_sqlAddslashes(trim($tab)) . '\', '
. '\'' . PMA_sqlAddslashes(trim($inf[0])) . '\', '
. '\'' . PMA_sqlAddslashes($GLOBALS['db']) . '\', '
. '\'' . PMA_sqlAddslashes(trim($for[0])) . '\','
. '\'' . PMA_sqlAddslashes(trim($for[1])) . '\')';
if (PMA_query_as_cu($qry)) {
echo '<p>' . $GLOBALS['strAddedColumnRelation'] . ' ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]) . ' to ' . htmlspecialchars($inf[2]) . '</p>';
} else {
echo '<p>' . $GLOBALS['strWritingRelationNotPossible'] . '</p>';
}
echo "\n";
} // end inf[2] exists
}
echo '<p><font color="green">' . $GLOBALS['strImportFinished'] . '</font></p>' . "\n";
} else {
echo '<p><font color="red">' . $GLOBALS['strFileCouldNotBeRead'] . '</font></p>' . "\n";
}
return 1;
} else {
if ($content != 'none') {
echo '<p><font color="orange">' . sprintf($GLOBALS['strIgnoringFile'], ' ' . htmlspecialchars($file)) . '</font></p>' . "\n";
} else {
// garvin: disabled. Shouldn't impose ANY non-submitted files ever.
echo '<p><font color="orange">' . sprintf($GLOBALS['strIgnoringFile'], ' ' . '...') . '</font></p>' . "\n";
}
return 0;
} // end working on table
}
/**
* Try to get the "$DOCUMENT_ROOT" variable whatever is the register_globals
* value
*/
if (empty($DOCUMENT_ROOT)) {
if (!empty($_SERVER) && isset($_SERVER['DOCUMENT_ROOT'])) {
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
}
else if (!empty($_ENV) && isset($_ENV['DOCUMENT_ROOT'])) {
$DOCUMENT_ROOT = $_ENV['DOCUMENT_ROOT'];
}
else if (@getenv('DOCUMENT_ROOT')) {
$DOCUMENT_ROOT = getenv('DOCUMENT_ROOT');
}
else {
$DOCUMENT_ROOT = '.';
}
} // end if
/**
* Executes import if required
*/
if (isset($do) && $do == 'import') {
$orig_docpath = $docpath;
if (empty($sql_file)) {
$sql_file = 'none';
}
// Get relation settings
require_once('./libraries/relation.lib.php');
$cfgRelation = PMA_getRelationsParam();
// Gets the query from a file if required
if ($sql_file != 'none') {
if (file_exists($sql_file)
&& is_uploaded_file($sql_file)) {
$open_basedir = @ini_get('open_basedir');
// If we are on a server with open_basedir, we must move the file
// before opening it. The doc explains how to create the "./tmp"
// directory
if (!empty($open_basedir)) {
$tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : './tmp/');
// function is_writeable() is valid on PHP3 and 4
if (!is_writeable($tmp_subdir)) {
$docsql_text = PMA_readFile($sql_file, $sql_file_compression);
if ($docsql_text == FALSE) {
echo $strFileCouldNotBeRead;
exit();
}
}
else {
$sql_file_new = $tmp_subdir . basename($sql_file);
move_uploaded_file($sql_file, $sql_file_new);
$docsql_text = PMA_readFile($sql_file_new, $sql_file_compression);
unlink($sql_file_new);
}
}
else {
// read from the normal upload dir
$docsql_text = PMA_readFile($sql_file, $sql_file_compression);
}
// Convert the file's charset if necessary
if ($cfg['AllowAnywhereRecoding'] && $allow_recoding
&& isset($charset_of_file) && $charset_of_file != $charset) {
$docsql_text = PMA_convert_string($charset_of_file, $charset, $docsql_text);
}
if (!isset($docsql_text) || $docsql_text == FALSE || $docsql_text == '') {
echo '<p><font color="red">' . $GLOBALS['strFileCouldNotBeRead'] . '</font></p>' . "\n";
} else {
docsql_check('', $sql_file_name, $sql_file_name, $docsql_text);
}
} // end uploaded file stuff
} else {
// echo '<h1>Starting Import</h1>';
$docpath = $cfg['docSQLDir'] . PMA_securePath($docpath);
if (substr($docpath, -1) != '/') {
$docpath .= '/';
}
$matched_files = 0;
if (is_dir($docpath)) {
// Do the work
$handle = opendir($docpath);
while ($file = @readdir($handle)) {
$filename = basename($file);
// echo '<p>Working on file ' . $filename . '</p>';
$matched_files += docsql_check($docpath, $file, $filename);
} // end while
} else {
echo '<p><font color="red">' .$docpath . ': ' . $strThisNotDirectory . "</font></p>\n";
}
}
}
/**
* Displays the form
*/
?>
<form method="post" action="db_details_importdocsql.php" <?php if ($is_upload) echo ' enctype="multipart/form-data"'; ?>>
<?php echo PMA_generate_common_hidden_inputs($db); ?>
<input type="hidden" name="submit_show" value="true" />
<input type="hidden" name="do" value="import" />
<b><?php echo $strAbsolutePathToDocSqlDir; ?>:</b>
<br /><br />
<?php echo $cfg['docSQLDir']; ?>/<input class="textfield" type="text" name="docpath" size="15" value="<?php echo (isset($orig_docpath) ? $orig_docpath : ''); ?>" />
<?php
// garvin: displays import dump feature only if file upload available
if ($is_upload) {
echo '<br /><br />';
echo ' <i>' . $strOr . '</i> ' . $strLocationTextfile . ':<br />' . "\n";
?>
<div style="margin-bottom: 5px">
<input type="file" name="sql_file" class="textfield" /><br />
<?php
if ($cfg['AllowAnywhereRecoding'] && $allow_recoding) {
$temp_charset = reset($cfg['AvailableCharsets']);
echo $strCharsetOfFile . "\n"
. ' <select name="charset_of_file" size="1">' . "\n"
. ' <option value="' . $temp_charset . '"';
if ($temp_charset == $charset) {
echo ' selected="selected"';
}
echo '>' . $temp_charset . '</option>' . "\n";
while ($temp_charset = next($cfg['AvailableCharsets'])) {
echo ' <option value="' . $temp_charset . '"';
if ($temp_charset == $charset) {
echo ' selected="selected"';
}
echo '>' . $temp_charset . '</option>' . "\n";
} // end while
echo ' </select><br />' . "\n" . ' ';
} // end if
$is_gzip = ($cfg['GZipDump'] && @function_exists('gzopen'));
$is_bzip = ($cfg['BZipDump'] && @function_exists('bzdecompress'));
if ($is_bzip || $is_gzip) {
echo ' ' . $strCompression . ':' . "\n"
. ' <input type="radio" id="radio_sql_file_compression_auto" name="sql_file_compression" value="" checked="checked" /><label for="radio_sql_file_compression_auto">' . $strAutodetect . '</label> ' . "\n"
. ' <input type="radio" id="radio_sql_file_compression_plain" name="sql_file_compression" value="text/plain" /><label for="radio_sql_file_compression_plain">' . $strNone . '</label> ' . "\n";
if ($is_gzip) {
echo ' <input type="radio" id="radio_sql_file_compression_gzip" name="sql_file_compression" value="application/x-gzip" /><label for="radio_sql_file_compression_gzip">' . $strGzip . '</label> ' . "\n";
}
if ($is_bzip) {
echo ' <input type="radio" id="radio_sql_file_compression_bzip" name="sql_file_compression" value="application/x-bzip" /><label for="radio_sql_file_compression_bzip">' . $strBzip . '</label> ' . "\n";
}
} else {
echo ' <input type="hidden" name="sql_file_compression" value="text/plain" />' . "\n";
}
?>
</div>
<?php
} // end if
echo "\n";
?>
<br />
<input type="submit" value="<?php echo $strImportFiles; ?>" />
</form>
<?php
} // End if use docSQL
/**
* Displays the footer
*/
echo "\n";
require_once('./libraries/footer.inc.php');
?>