2.5.4-rc1
[phpmyadmin/crack.git] / ldi_check.php3
blob49a8d2e8d46e7a432b87e35f210a032ac6cca202
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('./libraries/grab_globals.lib.php3');
22 require('./libraries/common.lib.php3');
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($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['DOCUMENT_ROOT'])) {
38 $DOCUMENT_ROOT = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];
40 else if (!empty($_ENV) && isset($_ENV['DOCUMENT_ROOT'])) {
41 $DOCUMENT_ROOT = $_ENV['DOCUMENT_ROOT'];
43 else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['DOCUMENT_ROOT'])) {
44 $DOCUMENT_ROOT = $HTTP_ENV_VARS['DOCUMENT_ROOT'];
46 else if (@getenv('DOCUMENT_ROOT')) {
47 $DOCUMENT_ROOT = getenv('DOCUMENT_ROOT');
49 else {
50 $DOCUMENT_ROOT = '.';
52 } // end if
54 if (substr($cfg['UploadDir'], -1) != '/') {
55 $cfg['UploadDir'] .= '/';
57 $textfile = $DOCUMENT_ROOT . dirname($PHP_SELF) . '/' . eregi_replace('^./', '', $cfg['UploadDir']) . eregi_replace('\.\.*', '.', $local_textfile);
58 if (file_exists($textfile)) {
59 $open_basedir = '';
60 if (PMA_PHP_INT_VERSION >= 40000) {
61 $open_basedir = @ini_get('open_basedir');
63 if (empty($open_basedir)) {
64 $open_basedir = @get_cfg_var('open_basedir');
67 // If we are on a server with open_basedir, we must move the file
68 // before opening it. The doc explains how to create the "./tmp"
69 // directory
71 if (!empty($open_basedir)) {
73 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : './tmp/');
75 // function is_writeable() is valid on PHP3 and 4
76 if (!is_writeable($tmp_subdir)) {
77 echo $strWebServerUploadDirectoryError . ': ' . $tmp_subdir
78 . '<br />';
79 exit();
80 } else {
81 $textfile_new = $tmp_subdir . basename($textfile);
82 if (PMA_PHP_INT_VERSION < 40003) {
83 copy($textfile, $textfile_new);
84 } else {
85 move_uploaded_file($textfile, $textfile_new);
87 $textfile = $textfile_new;
88 $unlink_local_textfile = true;
94 /**
95 * The form used to define the query has been submitted -> do the work
97 if (isset($btnLDI) && empty($textfile)) {
98 $js_to_run = 'functions.js';
99 include('./header.inc.php3');
100 $message = $strMustSelectFile;
101 include('./ldi_table.php3');
102 } elseif (isset($btnLDI) && ($textfile != 'none')) {
103 if (!isset($replace)) {
104 $replace = '';
107 // the error message does not correspond exactly to the error...
108 if (!@chmod($textfile, 0644)) {
109 echo $strFileCouldNotBeRead . ' ' . $textfile . '<br />';
110 exit();
113 // Kanji encoding convert appended by Y.Kawada
114 if (function_exists('PMA_kanji_file_conv')) {
115 $textfile = PMA_kanji_file_conv($textfile, $knjenc, isset($xkana) ? $xkana : '');
118 // Convert the file's charset if necessary
119 if ($cfg['AllowAnywhereRecoding'] && $allow_recoding
120 && isset($charset_of_file) && $charset_of_file != $charset) {
121 $textfile = PMA_convert_file($charset_of_file, $convcharset, $textfile);
124 // Formats the data posted to this script
125 $textfile = PMA_sqlAddslashes($textfile);
126 $enclosed = PMA_sqlAddslashes($enclosed);
127 $escaped = PMA_sqlAddslashes($escaped);
128 $column_name = PMA_sqlAddslashes($column_name);
130 // (try to) make sure the file is readable:
131 chmod($textfile, 0777);
133 // Builds the query
134 $sql_query = 'LOAD DATA';
136 if ($local_option == "1") {
137 $sql_query .= ' LOCAL';
140 $sql_query .= ' INFILE \'' . $textfile . '\'';
141 if (!empty($replace)) {
142 $sql_query .= ' ' . $replace;
144 $sql_query .= ' INTO TABLE ' . PMA_backquote($into_table);
145 if (isset($field_terminater)) {
146 $sql_query .= ' FIELDS TERMINATED BY \'' . $field_terminater . '\'';
148 if (isset($enclose_option) && strlen($enclose_option) > 0) {
149 $sql_query .= ' OPTIONALLY';
151 if (strlen($enclosed) > 0) {
152 $sql_query .= ' ENCLOSED BY \'' . $enclosed . '\'';
154 if (strlen($escaped) > 0) {
155 $sql_query .= ' ESCAPED BY \'' . $escaped . '\'';
157 if (strlen($line_terminator) > 0){
158 $sql_query .= ' LINES TERMINATED BY \'' . $line_terminator . '\'';
160 if (strlen($column_name) > 0) {
161 if (PMA_MYSQL_INT_VERSION >= 32306) {
162 $sql_query .= ' (';
163 $tmp = split(',( ?)', $column_name);
164 for ($i = 0; $i < count($tmp); $i++) {
165 if ($i > 0) {
166 $sql_query .= ', ';
168 $sql_query .= PMA_backquote(trim($tmp[$i]));
169 } // end for
170 $sql_query .= ')';
171 } else {
172 $sql_query .= ' (' . $column_name . ')';
176 // We could rename the ldi* scripts to tbl_properties_ldi* to improve
177 // consistency with the other sub-pages.
179 // The $goto in ldi_table.php3 is set to tbl_properties.php3 but maybe
180 // if would be better to Browse the latest inserted data.
181 include('./sql.php3');
182 if ($unlink_local_textfile) {
183 unlink($textfile);
189 * The form used to define the query hasn't been yet submitted -> loads it
191 else {
192 include('./ldi_table.php3');