Documentation TYPO3 par Ameos

db_details_importdocsql.php

00001 <?php
00002 /* $Id: db_details_importdocsql.php,v 2.6 2004/10/08 11:14:06 garvinhicking Exp $ */
00003 // vim: expandtab sw=4 ts=4 sts=4:
00004 
00005 
00015 require_once('./libraries/read_dump.lib.php');
00016 require_once('./libraries/grab_globals.lib.php');
00017 require_once('./header.inc.php');
00018 
00019 //require common added for string importing - Robbat2, 15 January 2003 9.34PM
00020 //all hardcoded strings converted by Robbat2, 15 January 2003 9.34PM
00021 require_once('./libraries/common.lib.php');
00022 
00023 // Check parameters
00024 PMA_checkParameters(array('db'));
00025 
00026 // We do any work, only if docSQL import was enabled in config
00027 if (isset($cfg['docSQLDir']) && !empty($cfg['docSQLDir'])) {
00028 
00029     if (substr($cfg['docSQLDir'], -1) != '/') {
00030         $cfg['docSQLDir'] .= '/';
00031     }
00032 
00046     function docsql_check($docpath = '', $file = '', $filename = '', $content = 'none') {
00047     global $GLOBALS;
00048 
00049         if (preg_match('@^(.*)_field_comment\.(txt|zip|bz2|bzip).*$@i', $filename)) {
00050             $tab = preg_replace('@^(.*)_field_comment\.(txt|zip|bz2|bzip).*@si', '\1', $filename);
00051             //echo '<h1>Working on Table ' . $_tab . '</h1>';
00052             if ($content == 'none') {
00053                 $lines = array();
00054                 $fd  = fopen($docpath . $file, 'r');
00055                 if ($fd) {
00056                     while (!feof($fd)) {
00057                         $lines[]    = fgets($fd, 4096);
00058                     }
00059                 }
00060             } else {
00061                 $content = str_replace("\r\n", "\n", $content);
00062                 $content = str_replace("\r", "\n", $content);
00063                 $lines = explode("\n", $content);
00064             }
00065 
00066             if (isset($lines) && is_array($lines) && count($lines) > 0) {
00067                 foreach ($lines AS $lkey => $line) {
00068                     //echo '<p>' . $line . '</p>';
00069                     $inf     = explode('|',$line);
00070                     if (!empty($inf[1]) && strlen(trim($inf[1])) > 0) {
00071                         $qry = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['column_info'])
00072                                 . ' (db_name, table_name, column_name, ' . PMA_backquote('comment') . ') '
00073                                 . ' VALUES('
00074                                 . '\'' . PMA_sqlAddslashes($GLOBALS['db']) . '\','
00075                                 . '\'' . PMA_sqlAddslashes(trim($tab)) . '\','
00076                                 . '\'' . PMA_sqlAddslashes(trim($inf[0])) . '\','
00077                                 . '\'' . PMA_sqlAddslashes(trim($inf[1])) . '\')';
00078                         if (PMA_query_as_cu($qry)) {
00079                             echo '<p>' . $GLOBALS['strAddedColumnComment'] . ' ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]) . '</p>';
00080                         } else {
00081                             echo '<p>' . $GLOBALS['strWritingCommentNotPossible'] . '</p>';
00082                         }
00083                         echo "\n";
00084                     } // end inf[1] exists
00085                     if (!empty($inf[2]) && strlen(trim($inf[2])) > 0) {
00086                         $for = explode('->', $inf[2]);
00087                         $qry = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['relation'])
00088                                 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
00089                                 . ' VALUES('
00090                                 . '\'' . PMA_sqlAddslashes($GLOBALS['db']) . '\', '
00091                                 . '\'' . PMA_sqlAddslashes(trim($tab)) . '\', '
00092                                 . '\'' . PMA_sqlAddslashes(trim($inf[0])) . '\', '
00093                                 . '\'' . PMA_sqlAddslashes($GLOBALS['db']) . '\', '
00094                                 . '\'' . PMA_sqlAddslashes(trim($for[0])) . '\','
00095                                 . '\'' . PMA_sqlAddslashes(trim($for[1])) . '\')';
00096                         if (PMA_query_as_cu($qry)) {
00097                             echo '<p>' . $GLOBALS['strAddedColumnRelation'] . ' ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]) . ' to ' . htmlspecialchars($inf[2]) . '</p>';
00098                         } else {
00099                             echo '<p>' . $GLOBALS['strWritingRelationNotPossible'] . '</p>';
00100                         }
00101                         echo "\n";
00102                     } // end inf[2] exists
00103                 }
00104                 echo '<p><font color="green">' . $GLOBALS['strImportFinished'] . '</font></p>' . "\n";
00105             } else {
00106                 echo '<p><font color="red">' . $GLOBALS['strFileCouldNotBeRead'] . '</font></p>' . "\n";
00107             }
00108 
00109             return 1;
00110         } else {
00111             if ($content != 'none') {
00112                 echo '<p><font color="orange">' . sprintf($GLOBALS['strIgnoringFile'], ' ' . htmlspecialchars($file)) . '</font></p>' . "\n";
00113             } else {
00114                 // garvin: disabled. Shouldn't impose ANY non-submitted files ever.
00115                 echo '<p><font color="orange">' . sprintf($GLOBALS['strIgnoringFile'], ' ' . '...') . '</font></p>' . "\n";
00116             }
00117             return 0;
00118         } // end working on table
00119     }
00120 
00125     if (empty($DOCUMENT_ROOT)) {
00126         if (!empty($_SERVER) && isset($_SERVER['DOCUMENT_ROOT'])) {
00127             $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
00128         }
00129         else if (!empty($_ENV) && isset($_ENV['DOCUMENT_ROOT'])) {
00130             $DOCUMENT_ROOT = $_ENV['DOCUMENT_ROOT'];
00131         }
00132         else if (@getenv('DOCUMENT_ROOT')) {
00133             $DOCUMENT_ROOT = getenv('DOCUMENT_ROOT');
00134         }
00135         else {
00136             $DOCUMENT_ROOT = '.';
00137         }
00138     } // end if
00139 
00143     if (isset($do) && $do == 'import') {
00144         $orig_docpath = $docpath;
00145 
00146         if (empty($sql_file)) {
00147             $sql_file  = 'none';
00148         }
00149 
00150         // Get relation settings
00151         require_once('./libraries/relation.lib.php');
00152         $cfgRelation = PMA_getRelationsParam();
00153 
00154         // Gets the query from a file if required
00155         if ($sql_file != 'none') {
00156             if (file_exists($sql_file)
00157                 && is_uploaded_file($sql_file)) {
00158 
00159                 $open_basedir = @ini_get('open_basedir');
00160 
00161                 // If we are on a server with open_basedir, we must move the file
00162                 // before opening it. The doc explains how to create the "./tmp"
00163                 // directory
00164 
00165                 if (!empty($open_basedir)) {
00166 
00167                     $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : './tmp/');
00168 
00169                     // function is_writeable() is valid on PHP3 and 4
00170                     if (!is_writeable($tmp_subdir)) {
00171                         $docsql_text = PMA_readFile($sql_file, $sql_file_compression);
00172                         if ($docsql_text == FALSE) {
00173                             echo $strFileCouldNotBeRead;
00174                             exit();
00175                         }
00176                     }
00177                     else {
00178                         $sql_file_new = $tmp_subdir . basename($sql_file);
00179                         move_uploaded_file($sql_file, $sql_file_new);
00180                         $docsql_text = PMA_readFile($sql_file_new, $sql_file_compression);
00181                         unlink($sql_file_new);
00182                     }
00183                 }
00184                 else {
00185                     // read from the normal upload dir
00186                     $docsql_text = PMA_readFile($sql_file, $sql_file_compression);
00187                 }
00188 
00189                 // Convert the file's charset if necessary
00190                 if ($cfg['AllowAnywhereRecoding'] && $allow_recoding
00191                     && isset($charset_of_file) && $charset_of_file != $charset) {
00192                     $docsql_text = PMA_convert_string($charset_of_file, $charset, $docsql_text);
00193                 }
00194 
00195                 if (!isset($docsql_text) || $docsql_text == FALSE || $docsql_text == '') {
00196                     echo '<p><font color="red">' . $GLOBALS['strFileCouldNotBeRead'] . '</font></p>' . "\n";
00197                 } else {
00198                     docsql_check('', $sql_file_name, $sql_file_name, $docsql_text);
00199                 }
00200             } // end uploaded file stuff
00201         } else {
00202 
00203             // echo '<h1>Starting Import</h1>';
00204             $docpath = $cfg['docSQLDir'] . PMA_securePath($docpath);
00205             if (substr($docpath, -1) != '/') {
00206                 $docpath .= '/';
00207             }
00208 
00209             $matched_files = 0;
00210 
00211             if (is_dir($docpath)) {
00212                 // Do the work
00213                 $handle = opendir($docpath);
00214                 while ($file = @readdir($handle)) {
00215                     $filename = basename($file);
00216                     // echo '<p>Working on file ' . $filename . '</p>';
00217                     $matched_files += docsql_check($docpath, $file, $filename);
00218                 } // end while
00219             } else {
00220                 echo '<p><font color="red">' .$docpath . ': ' . $strThisNotDirectory . "</font></p>\n";
00221             }
00222         }
00223     }
00224 
00225 
00229     ?>
00230 
00231     <form method="post" action="db_details_importdocsql.php" <?php if ($is_upload) echo ' enctype="multipart/form-data"'; ?>>
00232         <?php echo PMA_generate_common_hidden_inputs($db); ?>
00233         <input type="hidden" name="submit_show" value="true" />
00234         <input type="hidden" name="do" value="import" />
00235         <b><?php echo $strAbsolutePathToDocSqlDir; ?>:</b>
00236         <br /><br />
00237         <?php echo $cfg['docSQLDir']; ?>/<input class="textfield" type="text" name="docpath" size="15" value="<?php echo (isset($orig_docpath) ? $orig_docpath : ''); ?>" />
00238     <?php
00239     // garvin: displays import dump feature only if file upload available
00240     if ($is_upload) {
00241         echo '<br /><br />';
00242         echo '            <i>' . $strOr . '</i> ' . $strLocationTextfile . ':<br />' . "\n";
00243         ?>
00244                 <div style="margin-bottom: 5px">
00245                 <input type="file" name="sql_file" class="textfield" /><br />
00246         <?php
00247         if ($cfg['AllowAnywhereRecoding'] && $allow_recoding) {
00248             $temp_charset = reset($cfg['AvailableCharsets']);
00249             echo $strCharsetOfFile . "\n"
00250                  . '        <select name="charset_of_file" size="1">' . "\n"
00251                  . '                <option value="' . $temp_charset . '"';
00252             if ($temp_charset == $charset) {
00253                 echo ' selected="selected"';
00254             }
00255             echo '>' . $temp_charset . '</option>' . "\n";
00256             while ($temp_charset = next($cfg['AvailableCharsets'])) {
00257                 echo '                <option value="' . $temp_charset . '"';
00258                 if ($temp_charset == $charset) {
00259                     echo ' selected="selected"';
00260                 }
00261                 echo '>' . $temp_charset . '</option>' . "\n";
00262             } // end while
00263             echo '            </select><br />' . "\n" . '    ';
00264         } // end if
00265         $is_gzip = ($cfg['GZipDump'] && @function_exists('gzopen'));
00266         $is_bzip = ($cfg['BZipDump'] && @function_exists('bzdecompress'));
00267         if ($is_bzip || $is_gzip) {
00268             echo '        ' . $strCompression . ':' . "\n"
00269                . '            <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"
00270                . '            <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";
00271             if ($is_gzip) {
00272                 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";
00273             }
00274             if ($is_bzip) {
00275                 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";
00276             }
00277         } else {
00278             echo '        <input type="hidden" name="sql_file_compression" value="text/plain" />' . "\n";
00279         }
00280         ?>
00281                 </div>
00282         <?php
00283     } // end if
00284     echo "\n";
00285     ?>
00286         <br />
00287         &nbsp;<input type="submit" value="<?php echo $strImportFiles; ?>" />
00288     </form>
00289 
00290 <?php
00291 
00292 } // End if use docSQL
00293 
00297 echo "\n";
00298 require_once('./footer.inc.php');
00299 
00300 ?>


Généré par TYPO3 Ameos avec  doxygen 1.4.6