2.5.2 maybe
[phpmyadmin/crack.git] / db_details_importdocsql.php3
blobc34b19239707cfb1f1065527313530a61e790357
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 /**
24 * Imports docSQL files
26 * @param string the basepath
27 * @param string the filename
28 * @param string the complete filename
29 * @param string the content of a file
32 * @return boolean always true
34 * @global array GLOBAL variables
36 function docsql_check($docpath = '', $file = '', $filename = '', $content = 'none') {
37 global $GLOBALS;
39 if (eregi('^(.*)_field_comment\.(txt|zip|bz2|bzip).*$', $filename)) {
40 $tab = eregi_replace('^(.*)_field_comment\.(txt|zip|bz2|bzip).*', '\1', $filename);
41 //echo '<h1>Working on Table ' . $_tab . '</h1>';
42 if ($content == 'none') {
43 $lines = array();
44 $fd = fopen($docpath . $file, 'r');
45 if ($fd) {
46 while (!feof($fd)) {
47 $lines[] = fgets($fd, 4096);
50 } else {
51 $content = str_replace("\r\n", "\n", $content);
52 $content = str_replace("\r", "\n", $content);
53 $lines = explode("\n", $content);
56 if (isset($lines) && is_array($lines) && count($lines) > 0) {
57 @reset($lines);
58 while(list($lkey, $line) = each($lines)) {
59 //echo '<p>' . $line . '</p>';
60 $inf = explode('|',$line);
61 if (!empty($inf[1]) && strlen(trim($inf[1])) > 0) {
62 $qry = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['column_info'])
63 . ' (db_name, table_name, column_name, ' . PMA_backquote('comment') . ') '
64 . ' VALUES('
65 . '\'' . PMA_sqlAddslashes($GLOBALS['db']) . '\','
66 . '\'' . PMA_sqlAddslashes(trim($tab)) . '\','
67 . '\'' . PMA_sqlAddslashes(trim($inf[0])) . '\','
68 . '\'' . PMA_sqlAddslashes(trim($inf[1])) . '\')';
69 if (PMA_query_as_cu($qry)) {
70 echo '<p>' . $GLOBALS['strAddedColumnComment'] . ' ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]) . '</p>';
71 } else {
72 echo '<p>' . $GLOBALS['strWritingCommentNotPossible'] . '</p>';
74 echo "\n";
75 } // end inf[1] exists
76 if (!empty($inf[2]) && strlen(trim($inf[2])) > 0) {
77 $for = explode('->', $inf[2]);
78 $qry = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['relation'])
79 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
80 . ' VALUES('
81 . '\'' . PMA_sqlAddslashes($GLOBALS['db']) . '\', '
82 . '\'' . PMA_sqlAddslashes(trim($tab)) . '\', '
83 . '\'' . PMA_sqlAddslashes(trim($inf[0])) . '\', '
84 . '\'' . PMA_sqlAddslashes($GLOBALS['db']) . '\', '
85 . '\'' . PMA_sqlAddslashes(trim($for[0])) . '\','
86 . '\'' . PMA_sqlAddslashes(trim($for[1])) . '\')';
87 if (PMA_query_as_cu($qry)) {
88 echo '<p>' . $GLOBALS['strAddedColumnRelation'] . ' ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]) . ' to ' . htmlspecialchars($inf[2]) . '</p>';
89 } else {
90 echo '<p>' . $GLOBALS['strWritingRelationNotPossible'] . '</p>';
92 echo "\n";
93 } // end inf[2] exists
95 echo '<p><font color="green">' . $GLOBALS['strImportFinished'] . '</font></p>' . "\n";
96 } else {
97 echo '<p><font color="red">' . $GLOBALS['strFileCouldNotBeRead'] . '</font></p>' . "\n";
100 return 1;
101 } else {
102 if ($content != '') {
103 echo '<p><font color="orange">' . sprintf($GLOBALS['strIgnoringFile'], ' ' . $file) . '</font></p>' . "\n";
104 } else {
105 // garvin: disabled. Shouldn't impose ANY non-submitted files ever.
106 echo '<p><font color="orange">' . sprintf($GLOBALS['strIgnoringFile'], ' ' . '') . '</font></p>' . "\n";
108 return 0;
109 } // end working on table
113 * Try to get the "$DOCUMENT_ROOT" variable whatever is the register_globals
114 * value
116 if (empty($DOCUMENT_ROOT)) {
117 if (!empty($_SERVER) && isset($_SERVER['DOCUMENT_ROOT'])) {
118 $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
120 else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['DOCUMENT_ROOT'])) {
121 $DOCUMENT_ROOT = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];
123 else if (!empty($_ENV) && isset($_ENV['DOCUMENT_ROOT'])) {
124 $DOCUMENT_ROOT = $_ENV['DOCUMENT_ROOT'];
126 else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['DOCUMENT_ROOT'])) {
127 $DOCUMENT_ROOT = $HTTP_ENV_VARS['DOCUMENT_ROOT'];
129 else if (@getenv('DOCUMENT_ROOT')) {
130 $DOCUMENT_ROOT = getenv('DOCUMENT_ROOT');
132 else {
133 $DOCUMENT_ROOT = '.';
135 } // end if
138 * Executes import if required
140 if (isset($do) && $do == 'import') {
141 $orig_docpath = $docpath;
143 if (empty($sql_file)) {
144 $sql_file = 'none';
147 // Get relation settings
148 include('./libraries/relation.lib.php3');
149 $cfgRelation = PMA_getRelationsParam();
151 // Gets the query from a file if required
152 if ($sql_file != 'none') {
153 if (file_exists($sql_file)
154 && is_uploaded_file($sql_file)) {
156 $open_basedir = '';
157 if (PMA_PHP_INT_VERSION >= 40000) {
158 $open_basedir = @ini_get('open_basedir');
160 if (empty($open_basedir)) {
161 $open_basedir = @get_cfg_var('open_basedir');
164 // If we are on a server with open_basedir, we must move the file
165 // before opening it. The doc explains how to create the "./tmp"
166 // directory
168 if (!empty($open_basedir)) {
170 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : './tmp/');
172 // function is_writeable() is valid on PHP3 and 4
173 if (!is_writeable($tmp_subdir)) {
174 // if we cannot move the file, let PHP report the error
175 error_reporting(E_ALL);
176 $docsql_text = PMA_readFile($sql_file, $sql_file_compression);
178 else {
179 $sql_file_new = $tmp_subdir . basename($sql_file);
180 if (PMA_PHP_INT_VERSION < 40003) {
181 copy($sql_file, $sql_file_new);
182 } else {
183 move_uploaded_file($sql_file, $sql_file_new);
185 $docsql_text = PMA_readFile($sql_file_new, $sql_file_compression);
186 unlink($sql_file_new);
189 else {
190 // read from the normal upload dir
191 $docsql_text = PMA_readFile($sql_file, $sql_file_compression);
194 // Convert the file's charset if necessary
195 if ($cfg['AllowAnywhereRecoding'] && $allow_recoding
196 && isset($charset_of_file) && $charset_of_file != $charset) {
197 $docsql_text = PMA_convert_string($charset_of_file, $charset, $docsql_text);
200 if (!isset($docsql_text) || $docsql_text == FALSE || $docsql_text == '') {
201 echo '<p><font color="red">' . $GLOBALS['strFileCouldNotBeRead'] . '</font></p>' . "\n";
202 } else {
203 docsql_check('', $sql_file_name, $sql_file_name, $docsql_text);
205 } // end uploaded file stuff
206 } else {
208 // echo '<h1>Starting Import</h1>';
209 $docpath = $DOCUMENT_ROOT . dirname($PHP_SELF) . '/' . eregi_replace('\.\.*', '.', $docpath);
210 if (substr($docpath, strlen($docpath) - 2, 1) != '/') {
211 $docpath = $docpath . '/';
214 $matched_files = 0;
216 if (is_dir($docpath)) {
217 // Do the work
218 $handle = opendir($docpath);
219 while ($file = @readdir($handle)) {
220 $filename = basename($file);
221 // echo '<p>Working on file ' . $filename . '</p>';
222 $matched_files += docsql_check($docpath, $file, $filename);
223 } // end while
224 } else {
225 echo '<p><font color="red">' .$docpath . ': ' . $strThisNotDirectory . "</font></p>\n";
232 * Displays the form
236 <form method="post" action="db_details_importdocsql.php3" <?php if ($is_upload) echo ' enctype="multipart/form-data"'; ?>>
237 <?php echo PMA_generate_common_hidden_inputs($db); ?>
238 <input type="hidden" name="submit_show" value="true" />
239 <input type="hidden" name="do" value="import" />
240 <b><?php echo $strAbsolutePathToDocSqlDir; ?>:</b>
241 <br /><br />
242 <?php echo dirname($PHP_SELF); ?>/<input class="textfield" type="text" name="docpath" size="15" value="<?php echo (isset($orig_docpath) ? $orig_docpath : 'docSQL/'); ?>" />
243 <?php
244 // garvin: displays import dump feature only if file upload available
245 if ($is_upload) {
246 echo '<br /><br />';
247 echo ' <i>' . $strOr . '</i> ' . $strLocationTextfile . '&nbsp;:<br />' . "\n";
249 <div style="margin-bottom: 5px">
250 <input type="file" name="sql_file" class="textfield" /><br />
251 <?php
252 if ($cfg['AllowAnywhereRecoding'] && $allow_recoding) {
253 $temp_charset = reset($cfg['AvailableCharsets']);
254 echo $strCharsetOfFile . "\n"
255 . ' <select name="charset_of_file" size="1">' . "\n"
256 . ' <option value="' . $temp_charset . '"';
257 if ($temp_charset == $charset) {
258 echo ' selected="selected"';
260 echo '>' . $temp_charset . '</option>' . "\n";
261 while ($temp_charset = next($cfg['AvailableCharsets'])) {
262 echo ' <option value="' . $temp_charset . '"';
263 if ($temp_charset == $charset) {
264 echo ' selected="selected"';
266 echo '>' . $temp_charset . '</option>' . "\n";
267 } // end while
268 echo ' </select><br />' . "\n" . ' ';
269 } // end if
270 $is_gzip = ($cfg['GZipDump'] && @function_exists('gzopen'));
271 $is_bzip = ($cfg['BZipDump'] && @function_exists('bzdecompress'));
272 if ($is_bzip || $is_gzip) {
273 echo ' ' . $strCompression . ':' . "\n"
274 . ' <input type="radio" id="radio_sql_file_compression_auto" name="sql_file_compression" value="" checked="checked" />' . "\n"
275 . ' <label for="radio_sql_file_compression_auto">' . $strAutodetect . '</label>&nbsp;&nbsp;&nbsp;' . "\n"
276 . ' <input type="radio" id="radio_sql_file_compression_plain" name="sql_file_compression" value="text/plain" />' . "\n"
277 . ' <label for="radio_sql_file_compression_plain">' . $strNone . '</label>&nbsp;&nbsp;&nbsp;' . "\n";
278 if ($is_gzip) {
279 echo ' <input type="radio" id="radio_sql_file_compression_gzip" name="sql_file_compression" value="application/x-gzip" />' . "\n"
280 . ' <label for="radio_sql_file_compression_gzip">' . $strGzip . '</label>&nbsp;&nbsp;&nbsp;' . "\n";
282 if ($is_bzip) {
283 echo ' <input type="radio" id="radio_sql_file_compression_bzip" name="sql_file_compression" value="application/x-bzip" />' . "\n"
284 . ' <label for="radio_sql_file_compression_bzip">' . $strBzip . '</label>&nbsp;&nbsp;&nbsp;' . "\n";
286 } else {
287 echo ' <input type="hidden" name="sql_file_compression" value="text/plain" />' . "\n";
290 </div>
291 <?php
292 } // end if
293 echo "\n";
295 <br />
296 &nbsp;<input type="submit" value="<?php echo $strImportFiles; ?>" />
297 </form>
299 <?php
301 * Displays the footer
303 echo "\n";
304 require('./footer.inc.php3');