fixed inactivity timeout failure
[openemr.git] / interface / main / myadmin / tbl_replace_fields.php
blobfebd8f2affc4086f1c79ec82968b6289aa7a6e5f
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 require_once('./libraries/common.lib.php');
12 PMA_checkParameters(array('db','encoded_key'));
15 // f i e l d u p l o a d e d f r o m a f i l e
17 // garvin: original if-clause checked, whether input was stored in a possible fields_upload_XX var.
18 // Now check, if the field is set. If it is empty or a malicious file, do not alter fields contents.
19 // If an empty or invalid file is specified, the binary data gets deleter. Maybe a nice
20 // new text-variable is appropriate to document this behaviour.
22 // garvin: security cautions! You could trick the form and submit any file the webserver has access to
23 // for upload to a binary field. Shouldn't be that easy! ;)
25 // garvin: default is to advance to the field-value parsing. Will only be set to true when a
26 // binary file is uploaded, thus bypassing further manipulation of $val.
28 $check_stop = false;
30 // Check if a multi-edit row was found
31 ${'me_fields_upload_' . $encoded_key} = (isset($enc_primary_key) && isset(${'fields_upload_' . $encoded_key}['multi_edit']) ? ${'fields_upload_' . $encoded_key}['multi_edit'][$enc_primary_key] : (isset(${'fields_upload_' . $encoded_key}) ? ${'fields_upload_' . $encoded_key} : null));
32 ${'me_fields_uploadlocal_' . $encoded_key} = (isset($enc_primary_key) && isset(${'fields_uploadlocal_' . $encoded_key}['multi_edit']) ? ${'fields_uploadlocal_' . $encoded_key}['multi_edit'][$enc_primary_key] : (isset(${'fields_uploadlocal_' . $encoded_key}) ? ${'fields_uploadlocal_' . $encoded_key} : null));
34 if (isset(${'me_fields_upload_' . $encoded_key}) && ${'me_fields_upload_' . $encoded_key} != 'none'){
35 // garvin: This fields content is a blob-file upload.
37 if (!empty(${'me_fields_upload_' . $encoded_key})) {
38 // garvin: The blob-field is not empty. Check what we have there.
40 $data_file = ${'me_fields_upload_' . $encoded_key};
42 if (is_uploaded_file($data_file)) {
43 // garvin: A valid uploaded file is found. Look into the file...
45 $val = fread(fopen($data_file, 'rb'), filesize($data_file));
46 // nijel: This is probably the best way how to put binary data
47 // into MySQL and it also allow not to care about charset
48 // conversion that would otherwise corrupt the data.
50 if (!empty($val)) {
51 // garvin: The upload was valid. Check in new blob-field's contents.
52 $val = '0x' . bin2hex($val);
53 $seen_binary = TRUE;
54 $check_stop = TRUE;
56 // garvin: ELSE: an empty file was uploaded. Remove blob-field's contents.
57 // Blob-fields are preserved, see below. ($protected$)
59 } else {
60 // garvin: Danger, will robinson. File is malicious. Blob-fields are preserved, see below. ($protected$)
61 // void
64 } elseif (!empty(${'me_fields_uploadlocal_' . $encoded_key})) {
65 if (substr($cfg['UploadDir'], -1) != '/') {
66 $cfg['UploadDir'] .= '/';
68 $file_to_upload = $cfg['UploadDir'] . preg_replace('@\.\.*@', '.', ${'me_fields_uploadlocal_' . $encoded_key});
70 // A local file will be uploaded.
71 $open_basedir = @ini_get('open_basedir');
73 // If we are on a server with open_basedir, we must move the file
74 // before opening it. The doc explains how to create the "./tmp"
75 // directory
77 $unlink = false;
78 if (!empty($open_basedir)) {
80 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : './tmp/');
82 // function is_writeable() is valid on PHP3 and 4
83 if (!is_writeable($tmp_subdir)) {
84 // if we cannot move the file don't change blob fields
85 $file_to_upload = '';
86 } else {
87 $new_file_to_upload = $tmp_subdir . basename($file_to_upload);
88 move_uploaded_file($file_to_upload, $new_file_to_upload);
90 $file_to_upload = $new_file_to_upload;
91 $unlink = true;
95 if ($file_to_upload != '') {
97 $val = fread(fopen($file_to_upload, 'rb'), filesize($file_to_upload));
98 if (!empty($val)) {
99 $val = '0x' . bin2hex($val);
100 $seen_binary = TRUE;
101 $check_stop = TRUE;
104 if ($unlink == TRUE) {
105 unlink($file_to_upload);
110 // garvin: else: Post-field contains no data. Blob-fields are preserved, see below. ($protected$)
114 if (!$check_stop) {
116 // f i e l d v a l u e i n t h e f o r m
118 if (isset($me_fields_type[$encoded_key])) $type = $me_fields_type[$encoded_key];
119 else $type = '';
121 $f = 'field_' . md5($key);
122 $t_fval = (isset($$f) ? $$f : null);
124 if (isset($t_fval['multi_edit']) && isset($t_fval['multi_edit'][$enc_primary_key])) {
125 $fval = &$t_fval['multi_edit'][$enc_primary_key];
126 } else {
127 $fval = &$t_fval;
130 switch (strtolower($val)) {
131 // let users type NULL or null to input this string and not a NULL value
132 //case 'null':
133 // break;
134 case '':
135 switch ($type) {
136 case 'enum':
137 // if we have an enum, then construct the value
138 if (!empty($fval)) {
139 $val = implode(',', $fval);
140 if ($val == 'null') {
141 // void
142 } else {
143 // the data here is not urlencoded!
144 //$val = "'" . PMA_sqlAddslashes(urldecode($val)) . "'";
145 $val = "'" . PMA_sqlAddslashes($val) . "'";
147 } else {
148 $val = "''";
150 break;
151 case 'set':
152 // if we have a set, then construct the value
153 if (!empty($fval)) {
154 $val = implode(',', $fval);
155 // the data here is not urlencoded!
156 //$val = "'" . PMA_sqlAddslashes(urldecode($val)) . "'";
157 $val = "'" . PMA_sqlAddslashes($val) . "'";
158 } else {
159 $val = "''";
161 break;
162 case 'foreign':
163 // if we have a foreign key, then construct the value
164 if (!empty($fval)) {
165 $val = implode(',', $fval);
166 if ($val == 'null') {
167 // void
168 } else {
169 // the data here is not urlencoded!
170 //$val = "'" . PMA_sqlAddslashes(urldecode($val)) . "'";
171 $val = "'" . PMA_sqlAddslashes($val) . "'";
173 } else {
174 $val = "''";
176 break;
177 case 'protected':
178 // here we are in protected mode (asked in the config)
179 // so tbl_change has put this special value in the
180 // fields array, so we do not change the field value
181 // but we can still handle field upload
183 // garvin: when in UPDATE mode, do not alter field's contents. When in INSERT
184 // mode, insert empty field because no values were submitted. If protected
185 // blobs where set, insert original fields content.
186 if (isset($fieldlist)) {
187 if (isset($prot_row) && isset($prot_row[$key]) && !empty($prot_row[$key])) {
188 $val = '0x' . bin2hex($prot_row[$key]);
189 $seen_binary = TRUE;
190 } else {
191 $val = "''";
193 } else {
194 unset($val);
197 break;
198 default:
199 $val = "'" . PMA_sqlAddslashes($val) . "'";
200 break;
202 break;
203 default:
204 $val = "'" . PMA_sqlAddslashes($val) . "'";
205 break;
206 } // end switch
208 // Was the Null checkbox checked for this field?
209 // (if there is a value, we ignore the Null checkbox: this could
210 // be possible if Javascript is disabled in the browser)
211 if (isset($me_fields_null) && isset($me_fields_null[$encoded_key])
212 && $val=="''") {
213 $val = 'NULL';
215 } // end else (field value in the form)