sorry, wrong version checked in
[phpmyadmin/arisferyanto.git] / db_details_importdocsql.php
blob2bfe545c2163944138a023d1ceb542609c9975f1
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('./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 = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['column_info'])
68 . ' (db_name, table_name, column_name, ' . PMA_backquote('comment') . ') '
69 . ' VALUES('
70 . '\'' . PMA_sqlAddslashes($GLOBALS['db']) . '\','
71 . '\'' . PMA_sqlAddslashes(trim($tab)) . '\','
72 . '\'' . PMA_sqlAddslashes(trim($inf[0])) . '\','
73 . '\'' . PMA_sqlAddslashes(trim($inf[1])) . '\')';
74 if (PMA_query_as_cu($qry)) {
75 echo '<p>' . $GLOBALS['strAddedColumnComment'] . ' ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]) . '</p>';
76 } else {
77 echo '<p>' . $GLOBALS['strWritingCommentNotPossible'] . '</p>';
79 echo "\n";
80 } // end inf[1] exists
81 if (!empty($inf[2]) && strlen(trim($inf[2])) > 0) {
82 $for = explode('->', $inf[2]);
83 $qry = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['relation'])
84 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
85 . ' VALUES('
86 . '\'' . PMA_sqlAddslashes($GLOBALS['db']) . '\', '
87 . '\'' . PMA_sqlAddslashes(trim($tab)) . '\', '
88 . '\'' . PMA_sqlAddslashes(trim($inf[0])) . '\', '
89 . '\'' . PMA_sqlAddslashes($GLOBALS['db']) . '\', '
90 . '\'' . PMA_sqlAddslashes(trim($for[0])) . '\','
91 . '\'' . PMA_sqlAddslashes(trim($for[1])) . '\')';
92 if (PMA_query_as_cu($qry)) {
93 echo '<p>' . $GLOBALS['strAddedColumnRelation'] . ' ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]) . ' to ' . htmlspecialchars($inf[2]) . '</p>';
94 } else {
95 echo '<p>' . $GLOBALS['strWritingRelationNotPossible'] . '</p>';
97 echo "\n";
98 } // end inf[2] exists
100 echo '<p><font color="green">' . $GLOBALS['strImportFinished'] . '</font></p>' . "\n";
101 } else {
102 echo '<p><font color="red">' . $GLOBALS['strFileCouldNotBeRead'] . '</font></p>' . "\n";
105 return 1;
106 } else {
107 if ($content != 'none') {
108 echo '<p><font color="orange">' . sprintf($GLOBALS['strIgnoringFile'], ' ' . htmlspecialchars($file)) . '</font></p>' . "\n";
109 } else {
110 // garvin: disabled. Shouldn't impose ANY non-submitted files ever.
111 echo '<p><font color="orange">' . sprintf($GLOBALS['strIgnoringFile'], ' ' . '...') . '</font></p>' . "\n";
113 return 0;
114 } // end working on table
118 * Try to get the "$DOCUMENT_ROOT" variable whatever is the register_globals
119 * value
121 if (empty($DOCUMENT_ROOT)) {
122 if (!empty($_SERVER) && isset($_SERVER['DOCUMENT_ROOT'])) {
123 $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
125 else if (!empty($_ENV) && isset($_ENV['DOCUMENT_ROOT'])) {
126 $DOCUMENT_ROOT = $_ENV['DOCUMENT_ROOT'];
128 else if (@getenv('DOCUMENT_ROOT')) {
129 $DOCUMENT_ROOT = getenv('DOCUMENT_ROOT');
131 else {
132 $DOCUMENT_ROOT = '.';
134 } // end if
137 * Executes import if required
139 if (isset($do) && $do == 'import') {
140 $orig_docpath = $docpath;
142 if (empty($sql_file)) {
143 $sql_file = 'none';
146 // Get relation settings
147 require_once('./libraries/relation.lib.php');
148 $cfgRelation = PMA_getRelationsParam();
150 // Gets the query from a file if required
151 if ($sql_file != 'none') {
152 if (file_exists($sql_file)
153 && is_uploaded_file($sql_file)) {
155 $open_basedir = @ini_get('open_basedir');
157 // If we are on a server with open_basedir, we must move the file
158 // before opening it. The doc explains how to create the "./tmp"
159 // directory
161 if (!empty($open_basedir)) {
163 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : './tmp/');
165 // function is_writeable() is valid on PHP3 and 4
166 if (!is_writeable($tmp_subdir)) {
167 $docsql_text = PMA_readFile($sql_file, $sql_file_compression);
168 if ($docsql_text == FALSE) {
169 echo $strFileCouldNotBeRead;
170 exit();
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);
180 else {
181 // read from the normal upload dir
182 $docsql_text = PMA_readFile($sql_file, $sql_file_compression);
185 // Convert the file's charset if necessary
186 if ($cfg['AllowAnywhereRecoding'] && $allow_recoding
187 && isset($charset_of_file) && $charset_of_file != $charset) {
188 $docsql_text = PMA_convert_string($charset_of_file, $charset, $docsql_text);
191 if (!isset($docsql_text) || $docsql_text == FALSE || $docsql_text == '') {
192 echo '<p><font color="red">' . $GLOBALS['strFileCouldNotBeRead'] . '</font></p>' . "\n";
193 } else {
194 docsql_check('', $sql_file_name, $sql_file_name, $docsql_text);
196 } // end uploaded file stuff
197 } else {
199 // echo '<h1>Starting Import</h1>';
200 $docpath = $cfg['docSQLDir'] . PMA_securePath($docpath);
201 if (substr($docpath, -1) != '/') {
202 $docpath .= '/';
205 $matched_files = 0;
207 if (is_dir($docpath)) {
208 // Do the work
209 $handle = opendir($docpath);
210 while ($file = @readdir($handle)) {
211 $filename = basename($file);
212 // echo '<p>Working on file ' . $filename . '</p>';
213 $matched_files += docsql_check($docpath, $file, $filename);
214 } // end while
215 } else {
216 echo '<p><font color="red">' .$docpath . ': ' . $strThisNotDirectory . "</font></p>\n";
223 * Displays the form
227 <form method="post" action="db_details_importdocsql.php" <?php if ($is_upload) echo ' enctype="multipart/form-data"'; ?>>
228 <?php echo PMA_generate_common_hidden_inputs($db); ?>
229 <input type="hidden" name="submit_show" value="true" />
230 <input type="hidden" name="do" value="import" />
231 <b><?php echo $strAbsolutePathToDocSqlDir; ?>:</b>
232 <br /><br />
233 <?php echo $cfg['docSQLDir']; ?>/<input class="textfield" type="text" name="docpath" size="15" value="<?php echo (isset($orig_docpath) ? $orig_docpath : ''); ?>" />
234 <?php
235 // garvin: displays import dump feature only if file upload available
236 if ($is_upload) {
237 echo '<br /><br />';
238 echo ' <i>' . $strOr . '</i> ' . $strLocationTextfile . ':<br />' . "\n";
240 <div style="margin-bottom: 5px">
241 <input type="file" name="sql_file" class="textfield" /><br />
242 <?php
243 if ($cfg['AllowAnywhereRecoding'] && $allow_recoding) {
244 $temp_charset = reset($cfg['AvailableCharsets']);
245 echo $strCharsetOfFile . "\n"
246 . ' <select name="charset_of_file" size="1">' . "\n"
247 . ' <option value="' . $temp_charset . '"';
248 if ($temp_charset == $charset) {
249 echo ' selected="selected"';
251 echo '>' . $temp_charset . '</option>' . "\n";
252 while ($temp_charset = next($cfg['AvailableCharsets'])) {
253 echo ' <option value="' . $temp_charset . '"';
254 if ($temp_charset == $charset) {
255 echo ' selected="selected"';
257 echo '>' . $temp_charset . '</option>' . "\n";
258 } // end while
259 echo ' </select><br />' . "\n" . ' ';
260 } // end if
261 $is_gzip = ($cfg['GZipDump'] && @function_exists('gzopen'));
262 $is_bzip = ($cfg['BZipDump'] && @function_exists('bzdecompress'));
263 if ($is_bzip || $is_gzip) {
264 echo ' ' . $strCompression . ':' . "\n"
265 . ' <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"
266 . ' <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";
267 if ($is_gzip) {
268 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";
270 if ($is_bzip) {
271 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";
273 } else {
274 echo ' <input type="hidden" name="sql_file_compression" value="text/plain" />' . "\n";
277 </div>
278 <?php
279 } // end if
280 echo "\n";
282 <br />
283 &nbsp;<input type="submit" value="<?php echo $strImportFiles; ?>" />
284 </form>
286 <?php
288 } // End if use docSQL
291 * Displays the footer
293 echo "\n";
294 require_once('./footer.inc.php');