Let's again obey TextareaRows (bug #1465906).
[phpmyadmin/last10db.git] / db_details_importdocsql.php
blob8701b976dfdb61401bb27903419c4f585d67b725
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 require_once('./libraries/common.lib.php');
7 /**
8 * This script imports relation infos from docSQL (www.databay.de)
9 */
12 /**
13 * Get the values of the variables posted or sent to this script and display
14 * the headers
16 require_once('./libraries/read_dump.lib.php');
17 require_once('./libraries/header.inc.php');
19 // Check parameters
20 PMA_checkParameters(array('db'));
22 // We do any work, only if docSQL import was enabled in config
23 if (isset($cfg['docSQLDir']) && !empty($cfg['docSQLDir'])) {
25 if (substr($cfg['docSQLDir'], -1) != '/') {
26 $cfg['docSQLDir'] .= '/';
29 /**
30 * Imports docSQL files
32 * @param string the basepath
33 * @param string the filename
34 * @param string the complete filename
35 * @param string the content of a file
38 * @return boolean always true
40 * @global array GLOBAL variables
42 function docsql_check($docpath = '', $file = '', $filename = '', $content = 'none') {
43 global $GLOBALS;
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);
56 } else {
57 $content = str_replace("\r\n", "\n", $content);
58 $content = str_replace("\r", "\n", $content);
59 $lines = explode("\n", $content);
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>';
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>';
101 echo "\n";
102 } // end inf[2] exists
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";
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";
117 return 0;
118 } // end working on table
122 * Executes import if required
124 if (isset($do) && $do == 'import') {
125 $orig_docpath = $docpath;
127 if (empty($sql_file)) {
128 $sql_file = 'none';
131 // Get relation settings
132 require_once('./libraries/relation.lib.php');
133 $cfgRelation = PMA_getRelationsParam();
135 // Gets the query from a file if required
136 if ($sql_file != 'none') {
137 if (file_exists($sql_file)
138 && is_uploaded_file($sql_file)) {
140 $open_basedir = @ini_get('open_basedir');
142 // If we are on a server with open_basedir, we must move the file
143 // before opening it. The doc explains how to create the "./tmp"
144 // directory
146 if (!empty($open_basedir)) {
148 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : './tmp/');
150 // function is_writeable() is valid on PHP3 and 4
151 if (!is_writeable($tmp_subdir)) {
152 $docsql_text = PMA_readFile($sql_file, $sql_file_compression);
153 if ($docsql_text == FALSE) {
154 echo $strFileCouldNotBeRead;
155 exit();
157 } else {
158 $sql_file_new = $tmp_subdir . basename($sql_file);
159 move_uploaded_file($sql_file, $sql_file_new);
160 $docsql_text = PMA_readFile($sql_file_new, $sql_file_compression);
161 unlink($sql_file_new);
163 } else {
164 // read from the normal upload dir
165 $docsql_text = PMA_readFile($sql_file, $sql_file_compression);
168 // Convert the file's charset if necessary
169 if ($cfg['AllowAnywhereRecoding'] && $allow_recoding
170 && isset($charset_of_file) && $charset_of_file != $charset) {
171 $docsql_text = PMA_convert_string($charset_of_file, $charset, $docsql_text);
174 if (!isset($docsql_text) || $docsql_text == FALSE || $docsql_text == '') {
175 echo '<p><font color="red">' . $GLOBALS['strFileCouldNotBeRead'] . '</font></p>' . "\n";
176 } else {
177 docsql_check('', $sql_file_name, $sql_file_name, $docsql_text);
179 } // end uploaded file stuff
180 } else {
182 // echo '<h1>Starting Import</h1>';
183 $docpath = $cfg['docSQLDir'] . PMA_securePath($docpath);
184 if (substr($docpath, -1) != '/') {
185 $docpath .= '/';
188 $matched_files = 0;
190 if (is_dir($docpath)) {
191 // Do the work
192 $handle = opendir($docpath);
193 while ($file = @readdir($handle)) {
194 $filename = basename($file);
195 // echo '<p>Working on file ' . $filename . '</p>';
196 $matched_files += docsql_check($docpath, $file, $filename);
197 } // end while
198 } else {
199 echo '<p><font color="red">' .$docpath . ': ' . $strThisNotDirectory . "</font></p>\n";
206 * Displays the form
210 <form method="post" action="db_details_importdocsql.php" <?php if ($is_upload) { echo ' enctype="multipart/form-data"'; } ?>>
211 <?php echo PMA_generate_common_hidden_inputs($db); ?>
212 <input type="hidden" name="submit_show" value="true" />
213 <input type="hidden" name="do" value="import" />
214 <b><?php echo $strAbsolutePathToDocSqlDir; ?>:</b>
215 <br /><br />
216 <?php echo $cfg['docSQLDir']; ?>/<input class="textfield" type="text" name="docpath" size="15" value="<?php echo (isset($orig_docpath) ? $orig_docpath : ''); ?>" />
217 <?php
218 // garvin: displays import dump feature only if file upload available
219 if ($is_upload) {
220 echo '<br /><br />';
221 echo ' <i>' . $strOr . '</i> ' . $strLocationTextfile . ':<br />' . "\n";
223 <div style="margin-bottom: 5px">
224 <input type="file" name="sql_file" class="textfield" /><br />
225 <?php
226 if ($cfg['AllowAnywhereRecoding'] && $allow_recoding) {
227 $temp_charset = reset($cfg['AvailableCharsets']);
228 echo $strCharsetOfFile . "\n"
229 . ' <select name="charset_of_file" size="1">' . "\n"
230 . ' <option value="' . $temp_charset . '"';
231 if ($temp_charset == $charset) {
232 echo ' selected="selected"';
234 echo '>' . $temp_charset . '</option>' . "\n";
235 while ($temp_charset = next($cfg['AvailableCharsets'])) {
236 echo ' <option value="' . $temp_charset . '"';
237 if ($temp_charset == $charset) {
238 echo ' selected="selected"';
240 echo '>' . $temp_charset . '</option>' . "\n";
241 } // end while
242 echo ' </select><br />' . "\n" . ' ';
243 } // end if
244 $is_gzip = ($cfg['GZipDump'] && @function_exists('gzopen'));
245 $is_bzip = ($cfg['BZipDump'] && @function_exists('bzdecompress'));
246 if ($is_bzip || $is_gzip) {
247 echo ' ' . $strCompression . ':' . "\n"
248 . ' <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"
249 . ' <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";
250 if ($is_gzip) {
251 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";
253 if ($is_bzip) {
254 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";
256 } else {
257 echo ' <input type="hidden" name="sql_file_compression" value="text/plain" />' . "\n";
260 </div>
261 <?php
262 } // end if
263 echo "\n";
265 <br />
266 &nbsp;<input type="submit" value="<?php echo $strImportFiles; ?>" />
267 </form>
269 <?php
271 } // End if use docSQL
274 * Displays the footer
276 echo "\n";
277 require_once('./libraries/footer.inc.php');