Updated documentation.
[openemr.git] / interface / main / myadmin / db_details_importdocsql.php
blob9eb97da9968c493ecb11b860c926b123235ac2cd
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
6 /**
7 * This script imports relation infos from docSQL (www.databay.de)
8 */
11 /**
12 * Get the values of the variables posted or sent to this script and display
13 * the headers
15 require_once('./libraries/read_dump.lib.php');
16 require_once('./libraries/grab_globals.lib.php');
17 require_once('./header.inc.php');
19 //require common added for string importing - Robbat2, 15 January 2003 9.34PM
20 //all hardcoded strings converted by Robbat2, 15 January 2003 9.34PM
21 require_once('./libraries/common.lib.php');
23 // Check parameters
24 PMA_checkParameters(array('db'));
26 // We do any work, only if docSQL import was enabled in config
27 if (isset($cfg['docSQLDir']) && !empty($cfg['docSQLDir'])) {
29 if (substr($cfg['docSQLDir'], -1) != '/') {
30 $cfg['docSQLDir'] .= '/';
33 /**
34 * Imports docSQL files
36 * @param string the basepath
37 * @param string the filename
38 * @param string the complete filename
39 * @param string the content of a file
42 * @return boolean always true
44 * @global array GLOBAL variables
46 function docsql_check($docpath = '', $file = '', $filename = '', $content = 'none') {
47 global $GLOBALS;
49 if (preg_match('@^(.*)_field_comment\.(txt|zip|bz2|bzip).*$@i', $filename)) {
50 $tab = preg_replace('@^(.*)_field_comment\.(txt|zip|bz2|bzip).*@si', '\1', $filename);
51 //echo '<h1>Working on Table ' . $_tab . '</h1>';
52 if ($content == 'none') {
53 $lines = array();
54 $fd = fopen($docpath . $file, 'r');
55 if ($fd) {
56 while (!feof($fd)) {
57 $lines[] = fgets($fd, 4096);
60 } else {
61 $content = str_replace("\r\n", "\n", $content);
62 $content = str_replace("\r", "\n", $content);
63 $lines = explode("\n", $content);
66 if (isset($lines) && is_array($lines) && count($lines) > 0) {
67 foreach($lines AS $lkey => $line) {
68 //echo '<p>' . $line . '</p>';
69 $inf = explode('|',$line);
70 if (!empty($inf[1]) && strlen(trim($inf[1])) > 0) {
71 $qry = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['column_info'])
72 . ' (db_name, table_name, column_name, ' . PMA_backquote('comment') . ') '
73 . ' VALUES('
74 . '\'' . PMA_sqlAddslashes($GLOBALS['db']) . '\','
75 . '\'' . PMA_sqlAddslashes(trim($tab)) . '\','
76 . '\'' . PMA_sqlAddslashes(trim($inf[0])) . '\','
77 . '\'' . PMA_sqlAddslashes(trim($inf[1])) . '\')';
78 if (PMA_query_as_cu($qry)) {
79 echo '<p>' . $GLOBALS['strAddedColumnComment'] . ' ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]) . '</p>';
80 } else {
81 echo '<p>' . $GLOBALS['strWritingCommentNotPossible'] . '</p>';
83 echo "\n";
84 } // end inf[1] exists
85 if (!empty($inf[2]) && strlen(trim($inf[2])) > 0) {
86 $for = explode('->', $inf[2]);
87 $qry = 'INSERT INTO ' . 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">' . $GLOBALS['strImportFinished'] . '</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 * Try to get the "$DOCUMENT_ROOT" variable whatever is the register_globals
123 * value
125 if (empty($DOCUMENT_ROOT)) {
126 if (!empty($_SERVER) && isset($_SERVER['DOCUMENT_ROOT'])) {
127 $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
129 else if (!empty($_ENV) && isset($_ENV['DOCUMENT_ROOT'])) {
130 $DOCUMENT_ROOT = $_ENV['DOCUMENT_ROOT'];
132 else if (@getenv('DOCUMENT_ROOT')) {
133 $DOCUMENT_ROOT = getenv('DOCUMENT_ROOT');
135 else {
136 $DOCUMENT_ROOT = '.';
138 } // end if
141 * Executes import if required
143 if (isset($do) && $do == 'import') {
144 $orig_docpath = $docpath;
146 if (empty($sql_file)) {
147 $sql_file = 'none';
150 // Get relation settings
151 require_once('./libraries/relation.lib.php');
152 $cfgRelation = PMA_getRelationsParam();
154 // Gets the query from a file if required
155 if ($sql_file != 'none') {
156 if (file_exists($sql_file)
157 && is_uploaded_file($sql_file)) {
159 $open_basedir = @ini_get('open_basedir');
161 // If we are on a server with open_basedir, we must move the file
162 // before opening it. The doc explains how to create the "./tmp"
163 // directory
165 if (!empty($open_basedir)) {
167 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : './tmp/');
169 // function is_writeable() is valid on PHP3 and 4
170 if (!is_writeable($tmp_subdir)) {
171 $docsql_text = PMA_readFile($sql_file, $sql_file_compression);
172 if ($docsql_text == FALSE) {
173 echo $strFileCouldNotBeRead;
174 exit();
177 else {
178 $sql_file_new = $tmp_subdir . basename($sql_file);
179 move_uploaded_file($sql_file, $sql_file_new);
180 $docsql_text = PMA_readFile($sql_file_new, $sql_file_compression);
181 unlink($sql_file_new);
184 else {
185 // read from the normal upload dir
186 $docsql_text = PMA_readFile($sql_file, $sql_file_compression);
189 // Convert the file's charset if necessary
190 if ($cfg['AllowAnywhereRecoding'] && $allow_recoding
191 && isset($charset_of_file) && $charset_of_file != $charset) {
192 $docsql_text = PMA_convert_string($charset_of_file, $charset, $docsql_text);
195 if (!isset($docsql_text) || $docsql_text == FALSE || $docsql_text == '') {
196 echo '<p><font color="red">' . $GLOBALS['strFileCouldNotBeRead'] . '</font></p>' . "\n";
197 } else {
198 docsql_check('', $sql_file_name, $sql_file_name, $docsql_text);
200 } // end uploaded file stuff
201 } else {
203 // echo '<h1>Starting Import</h1>';
204 $docpath = $cfg['docSQLDir'] . preg_replace('@\.\.*@', '.', $docpath);
205 if (substr($docpath, -1) != '/') {
206 $docpath .= '/';
209 $matched_files = 0;
211 if (is_dir($docpath)) {
212 // Do the work
213 $handle = opendir($docpath);
214 while ($file = @readdir($handle)) {
215 $filename = basename($file);
216 // echo '<p>Working on file ' . $filename . '</p>';
217 $matched_files += docsql_check($docpath, $file, $filename);
218 } // end while
219 } else {
220 echo '<p><font color="red">' .$docpath . ': ' . $strThisNotDirectory . "</font></p>\n";
227 * Displays the form
231 <form method="post" action="db_details_importdocsql.php" <?php if ($is_upload) echo ' enctype="multipart/form-data"'; ?>>
232 <?php echo PMA_generate_common_hidden_inputs($db); ?>
233 <input type="hidden" name="submit_show" value="true" />
234 <input type="hidden" name="do" value="import" />
235 <b><?php echo $strAbsolutePathToDocSqlDir; ?>:</b>
236 <br /><br />
237 <?php echo $cfg['docSQLDir']; ?>/<input class="textfield" type="text" name="docpath" size="15" value="<?php echo (isset($orig_docpath) ? $orig_docpath : ''); ?>" />
238 <?php
239 // garvin: displays import dump feature only if file upload available
240 if ($is_upload) {
241 echo '<br /><br />';
242 echo ' <i>' . $strOr . '</i> ' . $strLocationTextfile . '&nbsp;:<br />' . "\n";
244 <div style="margin-bottom: 5px">
245 <input type="file" name="sql_file" class="textfield" /><br />
246 <?php
247 if ($cfg['AllowAnywhereRecoding'] && $allow_recoding) {
248 $temp_charset = reset($cfg['AvailableCharsets']);
249 echo $strCharsetOfFile . "\n"
250 . ' <select name="charset_of_file" size="1">' . "\n"
251 . ' <option value="' . $temp_charset . '"';
252 if ($temp_charset == $charset) {
253 echo ' selected="selected"';
255 echo '>' . $temp_charset . '</option>' . "\n";
256 while ($temp_charset = next($cfg['AvailableCharsets'])) {
257 echo ' <option value="' . $temp_charset . '"';
258 if ($temp_charset == $charset) {
259 echo ' selected="selected"';
261 echo '>' . $temp_charset . '</option>' . "\n";
262 } // end while
263 echo ' </select><br />' . "\n" . ' ';
264 } // end if
265 $is_gzip = ($cfg['GZipDump'] && @function_exists('gzopen'));
266 $is_bzip = ($cfg['BZipDump'] && @function_exists('bzdecompress'));
267 if ($is_bzip || $is_gzip) {
268 echo ' ' . $strCompression . ':' . "\n"
269 . ' <input type="radio" id="radio_sql_file_compression_auto" name="sql_file_compression" value="" checked="checked" />' . "\n"
270 . ' <label for="radio_sql_file_compression_auto">' . $strAutodetect . '</label>&nbsp;&nbsp;&nbsp;' . "\n"
271 . ' <input type="radio" id="radio_sql_file_compression_plain" name="sql_file_compression" value="text/plain" />' . "\n"
272 . ' <label for="radio_sql_file_compression_plain">' . $strNone . '</label>&nbsp;&nbsp;&nbsp;' . "\n";
273 if ($is_gzip) {
274 echo ' <input type="radio" id="radio_sql_file_compression_gzip" name="sql_file_compression" value="application/x-gzip" />' . "\n"
275 . ' <label for="radio_sql_file_compression_gzip">' . $strGzip . '</label>&nbsp;&nbsp;&nbsp;' . "\n";
277 if ($is_bzip) {
278 echo ' <input type="radio" id="radio_sql_file_compression_bzip" name="sql_file_compression" value="application/x-bzip" />' . "\n"
279 . ' <label for="radio_sql_file_compression_bzip">' . $strBzip . '</label>&nbsp;&nbsp;&nbsp;' . "\n";
281 } else {
282 echo ' <input type="hidden" name="sql_file_compression" value="text/plain" />' . "\n";
285 </div>
286 <?php
287 } // end if
288 echo "\n";
290 <br />
291 &nbsp;<input type="submit" value="<?php echo $strImportFiles; ?>" />
292 </form>
294 <?php
296 } // End if use docSQL
299 * Displays the footer
301 echo "\n";
302 require_once('./footer.inc.php');