3 // vim: expandtab sw=4 ts=4 sts=4:
7 * This script imports relation infos from docSQL (www.databay.de)
12 * Get the values of the variables posted or sent to this script and display
15 require('./libraries/read_dump.lib.php3');
16 require('./libraries/grab_globals.lib.php3');
17 require('./header.inc.php3');
19 //require common added for string importing - Robbat2, 15 January 2003 9.34PM
20 //all hardcoded strings converted by Robbat2, 15 January 2003 9.34PM
21 require('./libraries/common.lib.php3');
24 PMA_checkParameters(array('db'));
26 // We do any work, only if docSQL import was enabled in config
27 if (isset($cfg['docSQLDir']) && !empty($cfg['docSQLDir'])) {
29 if (substr($cfg['docSQLDir'], -1) != '/') {
30 $cfg['docSQLDir'] .= '/';
34 * Imports docSQL files
36 * @param string the basepath
37 * @param string the filename
38 * @param string the complete filename
39 * @param string the content of a file
42 * @return boolean always true
44 * @global array GLOBAL variables
46 function docsql_check($docpath = '', $file = '', $filename = '', $content = 'none') {
49 if (eregi('^(.*)_field_comment\.(txt|zip|bz2|bzip).*$', $filename)) {
50 $tab = eregi_replace('^(.*)_field_comment\.(txt|zip|bz2|bzip).*', '\1', $filename);
51 //echo '<h1>Working on Table ' . $_tab . '</h1>';
52 if ($content == 'none') {
54 $fd = fopen($docpath . $file, 'r');
57 $lines[] = fgets($fd, 4096);
61 $content = str_replace("\r\n", "\n", $content);
62 $content = str_replace("\r", "\n", $content);
63 $lines = explode("\n", $content);
66 if (isset($lines) && is_array($lines) && count($lines) > 0) {
68 while(list($lkey, $line) = each($lines)) {
69 //echo '<p>' . $line . '</p>';
70 $inf = explode('|',$line);
71 if (!empty($inf[1]) && strlen(trim($inf[1])) > 0) {
72 $qry = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['column_info'])
73 . ' (db_name, table_name, column_name, ' . PMA_backquote('comment') . ') '
75 . '\'' . PMA_sqlAddslashes($GLOBALS['db']) . '\','
76 . '\'' . PMA_sqlAddslashes(trim($tab)) . '\','
77 . '\'' . PMA_sqlAddslashes(trim($inf[0])) . '\','
78 . '\'' . PMA_sqlAddslashes(trim($inf[1])) . '\')';
79 if (PMA_query_as_cu($qry)) {
80 echo '<p>' . $GLOBALS['strAddedColumnComment'] . ' ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]) . '</p>';
82 echo '<p>' . $GLOBALS['strWritingCommentNotPossible'] . '</p>';
85 } // end inf[1] exists
86 if (!empty($inf[2]) && strlen(trim($inf[2])) > 0) {
87 $for = explode('->', $inf[2]);
88 $qry = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['relation'])
89 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
91 . '\'' . PMA_sqlAddslashes($GLOBALS['db']) . '\', '
92 . '\'' . PMA_sqlAddslashes(trim($tab)) . '\', '
93 . '\'' . PMA_sqlAddslashes(trim($inf[0])) . '\', '
94 . '\'' . PMA_sqlAddslashes($GLOBALS['db']) . '\', '
95 . '\'' . PMA_sqlAddslashes(trim($for[0])) . '\','
96 . '\'' . PMA_sqlAddslashes(trim($for[1])) . '\')';
97 if (PMA_query_as_cu($qry)) {
98 echo '<p>' . $GLOBALS['strAddedColumnRelation'] . ' ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]) . ' to ' . htmlspecialchars($inf[2]) . '</p>';
100 echo '<p>' . $GLOBALS['strWritingRelationNotPossible'] . '</p>';
103 } // end inf[2] exists
105 echo '<p><font color="green">' . $GLOBALS['strImportFinished'] . '</font></p>' . "\n";
107 echo '<p><font color="red">' . $GLOBALS['strFileCouldNotBeRead'] . '</font></p>' . "\n";
112 if ($content != 'none') {
113 echo '<p><font color="orange">' . sprintf($GLOBALS['strIgnoringFile'], ' ' . htmlspecialchars($file)) . '</font></p>' . "\n";
115 // garvin: disabled. Shouldn't impose ANY non-submitted files ever.
116 echo '<p><font color="orange">' . sprintf($GLOBALS['strIgnoringFile'], ' ' . '...') . '</font></p>' . "\n";
119 } // end working on table
123 * Try to get the "$DOCUMENT_ROOT" variable whatever is the register_globals
126 if (empty($DOCUMENT_ROOT)) {
127 if (!empty($_SERVER) && isset($_SERVER['DOCUMENT_ROOT'])) {
128 $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
130 else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['DOCUMENT_ROOT'])) {
131 $DOCUMENT_ROOT = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];
133 else if (!empty($_ENV) && isset($_ENV['DOCUMENT_ROOT'])) {
134 $DOCUMENT_ROOT = $_ENV['DOCUMENT_ROOT'];
136 else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['DOCUMENT_ROOT'])) {
137 $DOCUMENT_ROOT = $HTTP_ENV_VARS['DOCUMENT_ROOT'];
139 else if (@getenv
('DOCUMENT_ROOT')) {
140 $DOCUMENT_ROOT = getenv('DOCUMENT_ROOT');
143 $DOCUMENT_ROOT = '.';
148 * Executes import if required
150 if (isset($do) && $do == 'import') {
151 $orig_docpath = $docpath;
153 if (empty($sql_file)) {
157 // Get relation settings
158 include('./libraries/relation.lib.php3');
159 $cfgRelation = PMA_getRelationsParam();
161 // Gets the query from a file if required
162 if ($sql_file != 'none') {
163 if (file_exists($sql_file)
164 && is_uploaded_file($sql_file)) {
167 if (PMA_PHP_INT_VERSION
>= 40000) {
168 $open_basedir = @ini_get
('open_basedir');
170 if (empty($open_basedir)) {
171 $open_basedir = @get_cfg_var
('open_basedir');
174 // If we are on a server with open_basedir, we must move the file
175 // before opening it. The doc explains how to create the "./tmp"
178 if (!empty($open_basedir)) {
180 $tmp_subdir = (PMA_IS_WINDOWS ?
'.\\tmp\\' : './tmp/');
182 // function is_writeable() is valid on PHP3 and 4
183 if (!is_writeable($tmp_subdir)) {
184 $docsql_text = PMA_readFile($sql_file, $sql_file_compression);
185 if ($docsql_text == FALSE) {
186 echo $strFileCouldNotBeRead;
191 $sql_file_new = $tmp_subdir . basename($sql_file);
192 if (PMA_PHP_INT_VERSION
< 40003) {
193 copy($sql_file, $sql_file_new);
195 move_uploaded_file($sql_file, $sql_file_new);
197 $docsql_text = PMA_readFile($sql_file_new, $sql_file_compression);
198 unlink($sql_file_new);
202 // read from the normal upload dir
203 $docsql_text = PMA_readFile($sql_file, $sql_file_compression);
206 // Convert the file's charset if necessary
207 if ($cfg['AllowAnywhereRecoding'] && $allow_recoding
208 && isset($charset_of_file) && $charset_of_file != $charset) {
209 $docsql_text = PMA_convert_string($charset_of_file, $charset, $docsql_text);
212 if (!isset($docsql_text) ||
$docsql_text == FALSE ||
$docsql_text == '') {
213 echo '<p><font color="red">' . $GLOBALS['strFileCouldNotBeRead'] . '</font></p>' . "\n";
215 docsql_check('', $sql_file_name, $sql_file_name, $docsql_text);
217 } // end uploaded file stuff
220 // echo '<h1>Starting Import</h1>';
221 $docpath = $cfg['docSQLDir'] . eregi_replace('\.\.*', '.', $docpath);
222 if (substr($docpath, -1) != '/') {
228 if (is_dir($docpath)) {
230 $handle = opendir($docpath);
231 while ($file = @readdir
($handle)) {
232 $filename = basename($file);
233 // echo '<p>Working on file ' . $filename . '</p>';
234 $matched_files +
= docsql_check($docpath, $file, $filename);
237 echo '<p><font color="red">' .$docpath . ': ' . $strThisNotDirectory . "</font></p>\n";
248 <form method
="post" action
="db_details_importdocsql.php3" <?php
if ($is_upload) echo ' enctype="multipart/form-data"'; ?
>>
249 <?php
echo PMA_generate_common_hidden_inputs($db); ?
>
250 <input type
="hidden" name
="submit_show" value
="true" />
251 <input type
="hidden" name
="do" value
="import" />
252 <b
><?php
echo $strAbsolutePathToDocSqlDir; ?
>:</b
>
254 <?php
echo $cfg['docSQLDir']; ?
>/<input
class="textfield" type
="text" name
="docpath" size
="15" value
="<?php echo (isset($orig_docpath) ? $orig_docpath : ''); ?>" />
256 // garvin: displays import dump feature only if file upload available
259 echo ' <i>' . $strOr . '</i> ' . $strLocationTextfile . ' :<br />' . "\n";
261 <div style
="margin-bottom: 5px">
262 <input type
="file" name
="sql_file" class="textfield" /><br
/>
264 if ($cfg['AllowAnywhereRecoding'] && $allow_recoding) {
265 $temp_charset = reset($cfg['AvailableCharsets']);
266 echo $strCharsetOfFile . "\n"
267 . ' <select name="charset_of_file" size="1">' . "\n"
268 . ' <option value="' . $temp_charset . '"';
269 if ($temp_charset == $charset) {
270 echo ' selected="selected"';
272 echo '>' . $temp_charset . '</option>' . "\n";
273 while ($temp_charset = next($cfg['AvailableCharsets'])) {
274 echo ' <option value="' . $temp_charset . '"';
275 if ($temp_charset == $charset) {
276 echo ' selected="selected"';
278 echo '>' . $temp_charset . '</option>' . "\n";
280 echo ' </select><br />' . "\n" . ' ';
282 $is_gzip = ($cfg['GZipDump'] && @function_exists
('gzopen'));
283 $is_bzip = ($cfg['BZipDump'] && @function_exists
('bzdecompress'));
284 if ($is_bzip ||
$is_gzip) {
285 echo ' ' . $strCompression . ':' . "\n"
286 . ' <input type="radio" id="radio_sql_file_compression_auto" name="sql_file_compression" value="" checked="checked" />' . "\n"
287 . ' <label for="radio_sql_file_compression_auto">' . $strAutodetect . '</label> ' . "\n"
288 . ' <input type="radio" id="radio_sql_file_compression_plain" name="sql_file_compression" value="text/plain" />' . "\n"
289 . ' <label for="radio_sql_file_compression_plain">' . $strNone . '</label> ' . "\n";
291 echo ' <input type="radio" id="radio_sql_file_compression_gzip" name="sql_file_compression" value="application/x-gzip" />' . "\n"
292 . ' <label for="radio_sql_file_compression_gzip">' . $strGzip . '</label> ' . "\n";
295 echo ' <input type="radio" id="radio_sql_file_compression_bzip" name="sql_file_compression" value="application/x-bzip" />' . "\n"
296 . ' <label for="radio_sql_file_compression_bzip">' . $strBzip . '</label> ' . "\n";
299 echo ' <input type="hidden" name="sql_file_compression" value="text/plain" />' . "\n";
308  
;<input type
="submit" value
="<?php echo $strImportFiles; ?>" />
313 } // End if use docSQL
316 * Displays the footer
319 require('./footer.inc.php3');