remove control M
[phpmyadmin/sankalp_k.git] / tbl_replace_fields.php3
blob07470be0d7e60371c3319d6ff34ed6255c7ecedc
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 // note: grab_globals has extracted the fields from _FILES
6 // or HTTP_POST_FILES
8 // Check parameters
10 if (!defined('PMA_COMMON_LIB_INCLUDED')) {
11 include('./libraries/common.lib.php3');
14 PMA_checkParameters(array('db','encoded_key'));
17 // f i e l d u p l o a d e d f r o m a f i l e
19 // garvin: original if-clause checked, whether input was stored in a possible fields_upload_XX var.
20 // Now check, if the field is set. If it is empty or a malicious file, do not alter fields contents.
21 // If an empty or invalid file is specified, the binary data gets deleter. Maybe a nice
22 // new text-variable is appropriate to document this behaviour.
24 // garvin: security cautions! You could trick the form and submit any file the webserver has access to
25 // for upload to a binary field. Shouldn't be that easy! ;)
27 // garvin: default is to advance to the field-value parsing. Will only be set to true when a
28 // binary file is uploaded, thus bypassing further manipulation of $val.
30 $check_stop = false;
31 if (isset(${"fields_upload_" . $encoded_key}) && ${"fields_upload_" . $encoded_key} != 'none'){
32 // garvin: This fields content is a blob-file upload.
34 if (!empty(${"fields_upload_" . $encoded_key})) {
35 // garvin: The blob-field is not empty. Check what we have there.
37 $data_file = ${"fields_upload_" . $encoded_key};
39 if (is_uploaded_file($data_file)) {
40 // garvin: A valid uploaded file is found. Look into the file...
42 $val = fread(fopen($data_file, "rb"), filesize($data_file));
43 // nijel: This is probably the best way how to put binary data
44 // into MySQL and it also allow not to care about charset
45 // conversion that would otherwise corrupt the data.
47 if (!empty($val)) {
48 // garvin: The upload was valid. Check in new blob-field's contents.
49 $val = '0x' . bin2hex($val);
50 $seen_binary = TRUE;
51 $check_stop = TRUE;
53 // garvin: ELSE: an empty file was uploaded. Remove blob-field's contents.
54 // Blob-fields are preserved, see below. ($protected$)
56 } else {
57 // garvin: Danger, will robinson. File is malicious. Blob-fields are preserved, see below. ($protected$)
58 // void
61 } elseif (!empty(${'fields_uploadlocal_' . $encoded_key})) {
62 if (substr($cfg['UploadDir'], -1) != '/') {
63 $cfg['UploadDir'] .= '/';
65 $file_to_upload = $cfg['UploadDir'] . eregi_replace('\.\.*', '.', ${'fields_uploadlocal_' . $encoded_key});
67 // A local file will be uploaded.
68 $open_basedir = '';
69 if (PMA_PHP_INT_VERSION >= 40000) {
70 $open_basedir = @ini_get('open_basedir');
72 if (empty($open_basedir)) {
73 $open_basedir = @get_cfg_var('open_basedir');
76 // If we are on a server with open_basedir, we must move the file
77 // before opening it. The doc explains how to create the "./tmp"
78 // directory
80 $unlink = false;
81 if (!empty($open_basedir)) {
83 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : './tmp/');
85 // function is_writeable() is valid on PHP3 and 4
86 if (!is_writeable($tmp_subdir)) {
87 // if we cannot move the file don't change blob fields
88 $file_to_upload = '';
89 } else {
90 $new_file_to_upload = $tmp_subdir . basename($file_to_upload);
91 if (PMA_PHP_INT_VERSION < 40003) {
92 copy($file_to_upload, $new_file_to_upload);
93 } else {
94 move_uploaded_file($file_to_upload, $new_file_to_upload);
97 $file_to_upload = $new_file_to_upload;
98 $unlink = true;
102 if ($file_to_upload != '') {
104 $val = fread(fopen($file_to_upload, "rb"), filesize($file_to_upload));
105 if (!empty($val)) {
106 $val = '0x' . bin2hex($val);
107 $seen_binary = TRUE;
108 $check_stop = TRUE;
111 if ($unlink == TRUE) {
112 unlink($file_to_upload);
117 // garvin: else: Post-field contains no data. Blob-fields are preserved, see below. ($protected$)
121 if (!$check_stop) {
122 // f i e l d v a l u e i n t h e f o r m
123 if (isset($fields_type[$encoded_key])) $type = $fields_type[$encoded_key];
124 else $type = '';
125 switch (strtolower($val)) {
126 case 'null':
127 break;
128 case '':
129 switch ($type) {
130 case 'enum':
131 // if we have an enum, then construct the value
132 $f = 'field_' . md5($key);
133 if (!empty($$f)) {
134 $val = implode(',', $$f);
135 if ($val == 'null') {
136 // void
137 } else {
138 // the data here is not urlencoded!
139 //$val = "'" . PMA_sqlAddslashes(urldecode($val)) . "'";
140 $val = "'" . PMA_sqlAddslashes($val) . "'";
142 } else {
143 $val = "''";
145 break;
146 case 'set':
147 // if we have a set, then construct the value
148 $f = 'field_' . md5($key);
149 if (!empty($$f)) {
150 $val = implode(',', $$f);
151 // the data here is not urlencoded!
152 //$val = "'" . PMA_sqlAddslashes(urldecode($val)) . "'";
153 $val = "'" . PMA_sqlAddslashes($val) . "'";
154 } else {
155 $val = "''";
157 break;
158 case 'foreign':
159 // if we have a foreign key, then construct the value
160 $f = 'field_' . md5($key);
161 if (!empty($$f)) {
162 $val = implode(',', $$f);
163 if ($val == 'null') {
164 // void
165 } else {
166 // the data here is not urlencoded!
167 //$val = "'" . PMA_sqlAddslashes(urldecode($val)) . "'";
168 $val = "'" . PMA_sqlAddslashes($val) . "'";
170 } else {
171 $val = "''";
173 break;
174 case 'protected':
175 // here we are in protected mode (asked in the config)
176 // so tbl_change has put this special value in the
177 // fields array, so we do not change the field value
178 // but we can still handle field upload
180 // garvin: when in UPDATE mode, do not alter field's contents. When in INSERT
181 // mode, insert empty field because no values were submitted. If protected
182 // blobs where set, insert original fields content.
183 if (isset($fieldlist)) {
184 if (isset($prot_row) && isset($prot_row[$key]) && !empty($prot_row[$key])) {
185 $val = '0x' . bin2hex($prot_row[$key]);
186 $seen_binary = TRUE;
187 } else {
188 $val = "''";
190 } else {
191 unset($val);
194 break;
195 default:
196 $val = "'" . PMA_sqlAddslashes($val) . "'";
197 break;
199 break;
200 default:
201 $val = "'" . PMA_sqlAddslashes($val) . "'";
202 break;
203 } // end switch
205 // Was the Null checkbox checked for this field?
206 // (if there is a value, we ignore the Null checkbox: this could
207 // be possible if Javascript is disabled in the browser)
208 if (isset($fields_null) && isset($fields_null[$encoded_key])
209 && $val=="''") {
210 $val = 'NULL';
212 } // end else (field value in the form)