Updated documentation.
[openemr.git] / interface / main / myadmin / ldi_check.php
blob87c50b2e34a2b99f058eacff81b4d777ac549ca7
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
6 /**
7 * This file checks and builds the sql-string for
8 * LOAD DATA INFILE 'file_name.txt' [REPLACE | IGNORE] INTO TABLE table_name
9 * [FIELDS
10 * [TERMINATED BY '\t']
11 * [OPTIONALLY] ENCLOSED BY "]
12 * [ESCAPED BY '\\' ]]
13 * [LINES TERMINATED BY '\n']
14 * [(column_name,...)]
18 /**
19 * Gets some core scripts
21 require_once('./libraries/grab_globals.lib.php');
22 require_once('./libraries/common.lib.php');
24 // Check parameters
26 PMA_checkParameters(array('db', 'table'));
28 /**
29 * If a file from UploadDir was submitted, use this file
31 $unlink_local_textfile = false;
32 if (isset($btnLDI) && isset($local_textfile) && $local_textfile != '') {
33 if (empty($DOCUMENT_ROOT)) {
34 if (!empty($_SERVER) && isset($_SERVER['DOCUMENT_ROOT'])) {
35 $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
37 else if (!empty($_ENV) && isset($_ENV['DOCUMENT_ROOT'])) {
38 $DOCUMENT_ROOT = $_ENV['DOCUMENT_ROOT'];
40 else if (@getenv('DOCUMENT_ROOT')) {
41 $DOCUMENT_ROOT = getenv('DOCUMENT_ROOT');
43 else {
44 $DOCUMENT_ROOT = '.';
46 } // end if
48 if (substr($cfg['UploadDir'], -1) != '/') {
49 $cfg['UploadDir'] .= '/';
51 $textfile = $DOCUMENT_ROOT . dirname($PHP_SELF) . '/' . preg_replace('@^./@s', '', $cfg['UploadDir']) . preg_replace('@\.\.*@', '.', $local_textfile);
52 if (file_exists($textfile)) {
53 $open_basedir = @ini_get('open_basedir');
55 // If we are on a server with open_basedir, we must move the file
56 // before opening it. The doc explains how to create the "./tmp"
57 // directory
59 if (!empty($open_basedir)) {
61 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : './tmp/');
63 // function is_writeable() is valid on PHP3 and 4
64 if (!is_writeable($tmp_subdir)) {
65 echo $strWebServerUploadDirectoryError . ': ' . $tmp_subdir
66 . '<br />';
67 exit();
68 } else {
69 $textfile_new = $tmp_subdir . basename($textfile);
70 move_uploaded_file($textfile, $textfile_new);
71 $textfile = $textfile_new;
72 $unlink_local_textfile = true;
78 /**
79 * The form used to define the query has been submitted -> do the work
81 if (isset($btnLDI) && empty($textfile)) {
82 $js_to_run = 'functions.js';
83 require_once('./header.inc.php');
84 $message = $strMustSelectFile;
85 require('./ldi_table.php');
86 } elseif (isset($btnLDI) && ($textfile != 'none')) {
87 if (!isset($replace)) {
88 $replace = '';
91 // the error message does not correspond exactly to the error...
92 if (!@chmod($textfile, 0644)) {
93 echo $strFileCouldNotBeRead . ' ' . $textfile . '<br />';
94 require_once('./footer.inc.php');
97 // Kanji encoding convert appended by Y.Kawada
98 if (function_exists('PMA_kanji_file_conv')) {
99 $textfile = PMA_kanji_file_conv($textfile, $knjenc, isset($xkana) ? $xkana : '');
102 // Convert the file's charset if necessary
103 if ($cfg['AllowAnywhereRecoding'] && $allow_recoding
104 && isset($charset_of_file) && $charset_of_file != $charset) {
105 $textfile = PMA_convert_file($charset_of_file, $convcharset, $textfile);
108 // Formats the data posted to this script
109 $textfile = PMA_sqlAddslashes($textfile);
110 $enclosed = PMA_sqlAddslashes($enclosed);
111 $escaped = PMA_sqlAddslashes($escaped);
112 $column_name = PMA_sqlAddslashes($column_name);
114 // (try to) make sure the file is readable:
115 chmod($textfile, 0777);
117 // Builds the query
118 $sql_query = 'LOAD DATA';
120 if ($local_option == "1") {
121 $sql_query .= ' LOCAL';
124 $sql_query .= ' INFILE \'' . $textfile . '\'';
125 if (!empty($replace)) {
126 $sql_query .= ' ' . $replace;
128 $sql_query .= ' INTO TABLE ' . PMA_backquote($into_table);
129 if (isset($field_terminater)) {
130 $sql_query .= ' FIELDS TERMINATED BY \'' . $field_terminater . '\'';
132 if (isset($enclose_option) && strlen($enclose_option) > 0) {
133 $sql_query .= ' OPTIONALLY';
135 if (strlen($enclosed) > 0) {
136 $sql_query .= ' ENCLOSED BY \'' . $enclosed . '\'';
138 if (strlen($escaped) > 0) {
139 $sql_query .= ' ESCAPED BY \'' . $escaped . '\'';
141 if (strlen($line_terminator) > 0){
142 $sql_query .= ' LINES TERMINATED BY \'' . $line_terminator . '\'';
144 if (strlen($column_name) > 0) {
145 $sql_query .= ' (';
146 $tmp = split(',( ?)', $column_name);
147 $cnt_tmp = count($tmp);
148 for ($i = 0; $i < $cnt_tmp; $i++) {
149 if ($i > 0) {
150 $sql_query .= ', ';
152 $sql_query .= PMA_backquote(trim($tmp[$i]));
153 } // end for
154 $sql_query .= ')';
157 // We could rename the ldi* scripts to tbl_properties_ldi* to improve
158 // consistency with the other sub-pages.
160 // The $goto in ldi_table.php is set to tbl_properties.php but maybe
161 // if would be better to Browse the latest inserted data.
162 require('./sql.php');
163 if ($unlink_local_textfile) {
164 unlink($textfile);
170 * The form used to define the query hasn't been yet submitted -> loads it
172 else {
173 require('./ldi_table.php');