Rev Author Line No. Line
250 kaklik 1 <?php
2 /* $Id: db_details_importdocsql.php,v 2.11 2006/01/17 17:02:28 cybot_tm Exp $ */
3 // vim: expandtab sw=4 ts=4 sts=4:
4  
5 require_once('./libraries/common.lib.php');
6  
7 /**
8 * This script imports relation infos from docSQL (www.databay.de)
9 */
10  
11  
12 /**
13 * Get the values of the variables posted or sent to this script and display
14 * the headers
15 */
16 require_once('./libraries/read_dump.lib.php');
17 require_once('./libraries/header.inc.php');
18  
19 // Check parameters
20 PMA_checkParameters(array('db'));
21  
22 // We do any work, only if docSQL import was enabled in config
23 if (isset($cfg['docSQLDir']) && !empty($cfg['docSQLDir'])) {
24  
25 if (substr($cfg['docSQLDir'], -1) != '/') {
26 $cfg['docSQLDir'] .= '/';
27 }
28  
29 /**
30 * Imports docSQL files
31 *
32 * @param string the basepath
33 * @param string the filename
34 * @param string the complete filename
35 * @param string the content of a file
36  
37 *
38 * @return boolean always true
39 *
40 * @global array GLOBAL variables
41 */
42 function docsql_check($docpath = '', $file = '', $filename = '', $content = 'none') {
43 global $GLOBALS;
44  
45 if (preg_match('@^(.*)_field_comment\.(txt|zip|bz2|bzip).*$@i', $filename)) {
46 $tab = preg_replace('@^(.*)_field_comment\.(txt|zip|bz2|bzip).*@si', '\1', $filename);
47 //echo '<h1>Working on Table ' . $_tab . '</h1>';
48 if ($content == 'none') {
49 $lines = array();
50 $fd = fopen($docpath . $file, 'r');
51 if ($fd) {
52 while (!feof($fd)) {
53 $lines[] = fgets($fd, 4096);
54 }
55 }
56 } else {
57 $content = str_replace("\r\n", "\n", $content);
58 $content = str_replace("\r", "\n", $content);
59 $lines = explode("\n", $content);
60 }
61  
62 if (isset($lines) && is_array($lines) && count($lines) > 0) {
63 foreach ($lines AS $lkey => $line) {
64 //echo '<p>' . $line . '</p>';
65 $inf = explode('|', $line);
66 if (!empty($inf[1]) && strlen(trim($inf[1])) > 0) {
67 $qry = '
68 INSERT INTO
69 ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['column_info']) . '
70 ( db_name, table_name, column_name, ' . PMA_backquote('comment') . ' )
71 VALUES (
72 \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\',
73 \'' . PMA_sqlAddslashes(trim($tab)) . '\',
74 \'' . PMA_sqlAddslashes(trim($inf[0])) . '\',
75 \'' . PMA_sqlAddslashes(trim($inf[1])) . '\')';
76 if (PMA_query_as_cu($qry)) {
77 echo '<p>' . $GLOBALS['strAddedColumnComment'] . ' ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]) . '</p>';
78 } else {
79 echo '<p>' . $GLOBALS['strWritingCommentNotPossible'] . '</p>';
80 }
81 echo "\n";
82 } // end inf[1] exists
83 if (!empty($inf[2]) && strlen(trim($inf[2])) > 0) {
84 $for = explode('->', $inf[2]);
85 $qry = '
86 INSERT INTO
87 ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['relation']) . '
88 ( master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)
89 VALUES (
90 \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\',
91 \'' . PMA_sqlAddslashes(trim($tab)) . '\',
92 \'' . PMA_sqlAddslashes(trim($inf[0])) . '\',
93 \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\',
94 \'' . PMA_sqlAddslashes(trim($for[0])) . '\',
95 \'' . PMA_sqlAddslashes(trim($for[1])) . '\')';
96 if (PMA_query_as_cu($qry)) {
97 echo '<p>' . $GLOBALS['strAddedColumnRelation'] . ' ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]) . ' to ' . htmlspecialchars($inf[2]) . '</p>';
98 } else {
99 echo '<p>' . $GLOBALS['strWritingRelationNotPossible'] . '</p>';
100 }
101 echo "\n";
102 } // end inf[2] exists
103 }
104 echo '<p><font color="green">' . sprintf($GLOBALS['strImportSuccessfullyFinished'], count($lines)) . '</font></p>' . "\n";
105 } else {
106 echo '<p><font color="red">' . $GLOBALS['strFileCouldNotBeRead'] . '</font></p>' . "\n";
107 }
108  
109 return 1;
110 } else {
111 if ($content != 'none') {
112 echo '<p><font color="orange">' . sprintf($GLOBALS['strIgnoringFile'], ' ' . htmlspecialchars($file)) . '</font></p>' . "\n";
113 } else {
114 // garvin: disabled. Shouldn't impose ANY non-submitted files ever.
115 echo '<p><font color="orange">' . sprintf($GLOBALS['strIgnoringFile'], ' ' . '...') . '</font></p>' . "\n";
116 }
117 return 0;
118 } // end working on table
119 }
120  
121 /**
122 * Try to get the "$DOCUMENT_ROOT" variable whatever is the register_globals
123 * value
124 */
125 if (empty($DOCUMENT_ROOT)) {
126 if (!empty($_SERVER) && isset($_SERVER['DOCUMENT_ROOT'])) {
127 $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
128 } elseif (!empty($_ENV) && isset($_ENV['DOCUMENT_ROOT'])) {
129 $DOCUMENT_ROOT = $_ENV['DOCUMENT_ROOT'];
130 } elseif (@getenv('DOCUMENT_ROOT')) {
131 $DOCUMENT_ROOT = getenv('DOCUMENT_ROOT');
132 } else {
133 $DOCUMENT_ROOT = '.';
134 }
135 } // end if
136  
137 /**
138 * Executes import if required
139 */
140 if (isset($do) && $do == 'import') {
141 $orig_docpath = $docpath;
142  
143 if (empty($sql_file)) {
144 $sql_file = 'none';
145 }
146  
147 // Get relation settings
148 require_once('./libraries/relation.lib.php');
149 $cfgRelation = PMA_getRelationsParam();
150  
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)) {
155  
156 $open_basedir = @ini_get('open_basedir');
157  
158 // If we are on a server with open_basedir, we must move the file
159 // before opening it. The doc explains how to create the "./tmp"
160 // directory
161  
162 if (!empty($open_basedir)) {
163  
164 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : './tmp/');
165  
166 // function is_writeable() is valid on PHP3 and 4
167 if (!is_writeable($tmp_subdir)) {
168 $docsql_text = PMA_readFile($sql_file, $sql_file_compression);
169 if ($docsql_text == FALSE) {
170 echo $strFileCouldNotBeRead;
171 exit();
172 }
173 } else {
174 $sql_file_new = $tmp_subdir . basename($sql_file);
175 move_uploaded_file($sql_file, $sql_file_new);
176 $docsql_text = PMA_readFile($sql_file_new, $sql_file_compression);
177 unlink($sql_file_new);
178 }
179 } else {
180 // read from the normal upload dir
181 $docsql_text = PMA_readFile($sql_file, $sql_file_compression);
182 }
183  
184 // Convert the file's charset if necessary
185 if ($cfg['AllowAnywhereRecoding'] && $allow_recoding
186 && isset($charset_of_file) && $charset_of_file != $charset) {
187 $docsql_text = PMA_convert_string($charset_of_file, $charset, $docsql_text);
188 }
189  
190 if (!isset($docsql_text) || $docsql_text == FALSE || $docsql_text == '') {
191 echo '<p><font color="red">' . $GLOBALS['strFileCouldNotBeRead'] . '</font></p>' . "\n";
192 } else {
193 docsql_check('', $sql_file_name, $sql_file_name, $docsql_text);
194 }
195 } // end uploaded file stuff
196 } else {
197  
198 // echo '<h1>Starting Import</h1>';
199 $docpath = $cfg['docSQLDir'] . PMA_securePath($docpath);
200 if (substr($docpath, -1) != '/') {
201 $docpath .= '/';
202 }
203  
204 $matched_files = 0;
205  
206 if (is_dir($docpath)) {
207 // Do the work
208 $handle = opendir($docpath);
209 while ($file = @readdir($handle)) {
210 $filename = basename($file);
211 // echo '<p>Working on file ' . $filename . '</p>';
212 $matched_files += docsql_check($docpath, $file, $filename);
213 } // end while
214 } else {
215 echo '<p><font color="red">' .$docpath . ': ' . $strThisNotDirectory . "</font></p>\n";
216 }
217 }
218 }
219  
220  
221 /**
222 * Displays the form
223 */
224 ?>
225  
226 <form method="post" action="db_details_importdocsql.php" <?php if ($is_upload) { echo ' enctype="multipart/form-data"'; } ?>>
227 <?php echo PMA_generate_common_hidden_inputs($db); ?>
228 <input type="hidden" name="submit_show" value="true" />
229 <input type="hidden" name="do" value="import" />
230 <b><?php echo $strAbsolutePathToDocSqlDir; ?>:</b>
231 <br /><br />
232 <?php echo $cfg['docSQLDir']; ?>/<input class="textfield" type="text" name="docpath" size="15" value="<?php echo (isset($orig_docpath) ? $orig_docpath : ''); ?>" />
233 <?php
234 // garvin: displays import dump feature only if file upload available
235 if ($is_upload) {
236 echo '<br /><br />';
237 echo ' <i>' . $strOr . '</i> ' . $strLocationTextfile . ':<br />' . "\n";
238 ?>
239 <div style="margin-bottom: 5px">
240 <input type="file" name="sql_file" class="textfield" /><br />
241 <?php
242 if ($cfg['AllowAnywhereRecoding'] && $allow_recoding) {
243 $temp_charset = reset($cfg['AvailableCharsets']);
244 echo $strCharsetOfFile . "\n"
245 . ' <select name="charset_of_file" size="1">' . "\n"
246 . ' <option value="' . $temp_charset . '"';
247 if ($temp_charset == $charset) {
248 echo ' selected="selected"';
249 }
250 echo '>' . $temp_charset . '</option>' . "\n";
251 while ($temp_charset = next($cfg['AvailableCharsets'])) {
252 echo ' <option value="' . $temp_charset . '"';
253 if ($temp_charset == $charset) {
254 echo ' selected="selected"';
255 }
256 echo '>' . $temp_charset . '</option>' . "\n";
257 } // end while
258 echo ' </select><br />' . "\n" . ' ';
259 } // end if
260 $is_gzip = ($cfg['GZipDump'] && @function_exists('gzopen'));
261 $is_bzip = ($cfg['BZipDump'] && @function_exists('bzdecompress'));
262 if ($is_bzip || $is_gzip) {
263 echo ' ' . $strCompression . ':' . "\n"
264 . ' <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>&nbsp;&nbsp;&nbsp;' . "\n"
265 . ' <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>&nbsp;&nbsp;&nbsp;' . "\n";
266 if ($is_gzip) {
267 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>&nbsp;&nbsp;&nbsp;' . "\n";
268 }
269 if ($is_bzip) {
270 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>&nbsp;&nbsp;&nbsp;' . "\n";
271 }
272 } else {
273 echo ' <input type="hidden" name="sql_file_compression" value="text/plain" />' . "\n";
274 }
275 ?>
276 </div>
277 <?php
278 } // end if
279 echo "\n";
280 ?>
281 <br />
282 &nbsp;<input type="submit" value="<?php echo $strImportFiles; ?>" />
283 </form>
284  
285 <?php
286  
287 } // End if use docSQL
288  
289 /**
290 * Displays the footer
291 */
292 echo "\n";
293 require_once('./libraries/footer.inc.php');
294  
295 ?>