remove control M
[phpmyadmin/crack.git] / db_details_importdocsql.php3
blob79484d151381f14aee56eff3b53321b1318bbd85
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
6 /**
7 * This script imports relation infos from docSQL (www.databay.de)
8 */
11 /**
12 * Get the values of the variables posted or sent to this script and display
13 * the headers
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');
23 // Check parameters
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'] .= '/';
33 /**
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') {
47 global $GLOBALS;
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') {
53 $lines = array();
54 $fd = fopen($docpath . $file, 'r');
55 if ($fd) {
56 while (!feof($fd)) {
57 $lines[] = fgets($fd, 4096);
60 } else {
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) {
67 @reset($lines);
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') . ') '
74 . ' VALUES('
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>';
81 } else {
82 echo '<p>' . $GLOBALS['strWritingCommentNotPossible'] . '</p>';
84 echo "\n";
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)'
90 . ' VALUES('
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>';
99 } else {
100 echo '<p>' . $GLOBALS['strWritingRelationNotPossible'] . '</p>';
102 echo "\n";
103 } // end inf[2] exists
105 echo '<p><font color="green">' . $GLOBALS['strImportFinished'] . '</font></p>' . "\n";
106 } else {
107 echo '<p><font color="red">' . $GLOBALS['strFileCouldNotBeRead'] . '</font></p>' . "\n";
110 return 1;
111 } else {
112 if ($content != 'none') {
113 echo '<p><font color="orange">' . sprintf($GLOBALS['strIgnoringFile'], ' ' . htmlspecialchars($file)) . '</font></p>' . "\n";
114 } else {
115 // garvin: disabled. Shouldn't impose ANY non-submitted files ever.
116 echo '<p><font color="orange">' . sprintf($GLOBALS['strIgnoringFile'], ' ' . '...') . '</font></p>' . "\n";
118 return 0;
119 } // end working on table
123 * Try to get the "$DOCUMENT_ROOT" variable whatever is the register_globals
124 * value
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');
142 else {
143 $DOCUMENT_ROOT = '.';
145 } // end if
148 * Executes import if required
150 if (isset($do) && $do == 'import') {
151 $orig_docpath = $docpath;
153 if (empty($sql_file)) {
154 $sql_file = 'none';
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)) {
166 $open_basedir = '';
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"
176 // directory
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;
187 exit();
190 else {
191 $sql_file_new = $tmp_subdir . basename($sql_file);
192 if (PMA_PHP_INT_VERSION < 40003) {
193 copy($sql_file, $sql_file_new);
194 } else {
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);
201 else {
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";
214 } else {
215 docsql_check('', $sql_file_name, $sql_file_name, $docsql_text);
217 } // end uploaded file stuff
218 } else {
220 // echo '<h1>Starting Import</h1>';
221 $docpath = $cfg['docSQLDir'] . eregi_replace('\.\.*', '.', $docpath);
222 if (substr($docpath, -1) != '/') {
223 $docpath .= '/';
226 $matched_files = 0;
228 if (is_dir($docpath)) {
229 // Do the work
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);
235 } // end while
236 } else {
237 echo '<p><font color="red">' .$docpath . ': ' . $strThisNotDirectory . "</font></p>\n";
244 * Displays the form
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>
253 <br /><br />
254 <?php echo $cfg['docSQLDir']; ?>/<input class="textfield" type="text" name="docpath" size="15" value="<?php echo (isset($orig_docpath) ? $orig_docpath : ''); ?>" />
255 <?php
256 // garvin: displays import dump feature only if file upload available
257 if ($is_upload) {
258 echo '<br /><br />';
259 echo ' <i>' . $strOr . '</i> ' . $strLocationTextfile . '&nbsp;:<br />' . "\n";
261 <div style="margin-bottom: 5px">
262 <input type="file" name="sql_file" class="textfield" /><br />
263 <?php
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";
279 } // end while
280 echo ' </select><br />' . "\n" . ' ';
281 } // end if
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>&nbsp;&nbsp;&nbsp;' . "\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>&nbsp;&nbsp;&nbsp;' . "\n";
290 if ($is_gzip) {
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>&nbsp;&nbsp;&nbsp;' . "\n";
294 if ($is_bzip) {
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>&nbsp;&nbsp;&nbsp;' . "\n";
298 } else {
299 echo ' <input type="hidden" name="sql_file_compression" value="text/plain" />' . "\n";
302 </div>
303 <?php
304 } // end if
305 echo "\n";
307 <br />
308 &nbsp;<input type="submit" value="<?php echo $strImportFiles; ?>" />
309 </form>
311 <?php
313 } // End if use docSQL
316 * Displays the footer
318 echo "\n";
319 require('./footer.inc.php3');