3 // vim: expandtab sw=4 ts=4 sts=4:
5 require_once('./libraries/common.lib.php');
8 * This script imports relation infos from docSQL (www.databay.de)
13 * Get the values of the variables posted or sent to this script and display
16 require_once('./libraries/read_dump.lib.php');
17 require_once('./libraries/header.inc.php');
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'] .= '/';
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') {
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') {
50 $fd = fopen($docpath . $file, 'r');
53 $lines[] = fgets($fd, 4096);
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) {
69 ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['column_info']) . '
70 ( db_name, table_name, column_name, ' . PMA_backquote('comment') . ' )
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>';
79 echo '<p>' . $GLOBALS['strWritingCommentNotPossible'] . '</p>';
82 } // end inf[1] exists
83 if (!empty($inf[2]) && strlen(trim($inf[2])) > 0) {
84 $for = explode('->', $inf[2]);
87 ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['relation']) . '
88 ( master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)
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>';
99 echo '<p>' . $GLOBALS['strWritingRelationNotPossible'] . '</p>';
102 } // end inf[2] exists
104 echo '<p><font color="green">' . sprintf($GLOBALS['strImportSuccessfullyFinished'], count($lines)) . '</font></p>' . "\n";
106 echo '<p><font color="red">' . $GLOBALS['strFileCouldNotBeRead'] . '</font></p>' . "\n";
111 if ($content != 'none') {
112 echo '<p><font color="orange">' . sprintf($GLOBALS['strIgnoringFile'], ' ' . htmlspecialchars($file)) . '</font></p>' . "\n";
114 // garvin: disabled. Shouldn't impose ANY non-submitted files ever.
115 echo '<p><font color="orange">' . sprintf($GLOBALS['strIgnoringFile'], ' ' . '...') . '</font></p>' . "\n";
118 } // end working on table
122 * Try to get the "$DOCUMENT_ROOT" variable whatever is the register_globals
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');
133 $DOCUMENT_ROOT = '.';
138 * Executes import if required
140 if (isset($do) && $do == 'import') {
141 $orig_docpath = $docpath;
143 if (empty($sql_file)) {
147 // Get relation settings
148 require_once('./libraries/relation.lib.php');
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 = @ini_get
('open_basedir');
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"
162 if (!empty($open_basedir)) {
164 $tmp_subdir = (PMA_IS_WINDOWS ?
'.\\tmp\\' : './tmp/');
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;
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 // read from the normal upload dir
181 $docsql_text = PMA_readFile($sql_file, $sql_file_compression);
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);
190 if (!isset($docsql_text) ||
$docsql_text == FALSE ||
$docsql_text == '') {
191 echo '<p><font color="red">' . $GLOBALS['strFileCouldNotBeRead'] . '</font></p>' . "\n";
193 docsql_check('', $sql_file_name, $sql_file_name, $docsql_text);
195 } // end uploaded file stuff
198 // echo '<h1>Starting Import</h1>';
199 $docpath = $cfg['docSQLDir'] . PMA_securePath($docpath);
200 if (substr($docpath, -1) != '/') {
206 if (is_dir($docpath)) {
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);
215 echo '<p><font color="red">' .$docpath . ': ' . $strThisNotDirectory . "</font></p>\n";
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
>
232 <?php
echo $cfg['docSQLDir']; ?
>/<input
class="textfield" type
="text" name
="docpath" size
="15" value
="<?php echo (isset($orig_docpath) ? $orig_docpath : ''); ?>" />
234 // garvin: displays import dump feature only if file upload available
237 echo ' <i>' . $strOr . '</i> ' . $strLocationTextfile . ':<br />' . "\n";
239 <div style
="margin-bottom: 5px">
240 <input type
="file" name
="sql_file" class="textfield" /><br
/>
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"';
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"';
256 echo '>' . $temp_charset . '</option>' . "\n";
258 echo ' </select><br />' . "\n" . ' ';
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> ' . "\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> ' . "\n";
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> ' . "\n";
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> ' . "\n";
273 echo ' <input type="hidden" name="sql_file_compression" value="text/plain" />' . "\n";
282  
;<input type
="submit" value
="<?php echo $strImportFiles; ?>" />
287 } // End if use docSQL
290 * Displays the footer
293 require_once('./libraries/footer.inc.php');