uploadFile.php
11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/**
* @file uploadFile
* @brief
* @author Elena
* @version $Id: uploadFile.php 2905 2015-05-18 10:08:24Z elena $
*
*/
function file_upload_error_message($error_code) {
switch ($error_code) {
case UPLOAD_ERR_INI_SIZE:
return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
case UPLOAD_ERR_FORM_SIZE:
return 'The uploaded file exceeds max file size of '.maxSize;
case UPLOAD_ERR_PARTIAL:
return 'The uploaded file was only partially uploaded';
case UPLOAD_ERR_NO_FILE:
return 'No file was uploaded';
case UPLOAD_ERR_NO_TMP_DIR:
return 'Missing a temporary folder';
case UPLOAD_ERR_CANT_WRITE:
return 'Failed to write file to disk';
case UPLOAD_ERR_EXTENSION:
return 'File upload stopped by extension';
case UPLOAD_ERR_EMPTY:
return 'File is empty';
default:
return 'Unknown upload error';
}
}
function getUrlFileSize($URL) {
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);
$ContentLength = 1000000000000000000;
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
$ContentLength = (int)$matches[1];
}
return $ContentLength;
}
/**
* Main
*/
define("UPLOAD_ERR_EMPTY",5);
require_once 'config.php';
// error_reporting(E_ERROR | E_WARNING | E_PARSE);
if (!isset($_POST['sessionID']))
{
$response = array( 'success' => false, 'error' => ': Check that uploaded file size is less than '.ini_get('upload_max_filesize'));
die(json_encode($response));
}
define('USERDATADIR', USERPATH."/".$_POST['sessionID']."/DATA/");
define('USERTEMPDIR', USERPATH."/".$_POST['sessionID']."/TEMP/");
define('USERWSDIR', USERPATH."/".$_POST['sessionID']."/WS/");
define('USERWORKINGDIR', USERPATH."/".$_POST['sessionID']."/RES/");
define('ATTACHMENTDIR', DATAPATH."/Feedback/Attach/");
define('maxSize',$_POST['MAX_FILE_SIZE'] < ini_get('upload_max_filesize'));
if (!is_dir(USERTEMPDIR)) mkdir(USERTEMPDIR.'/', 0755, true);
if (!is_dir(ATTACHMENTDIR)) mkdir(ATTACHMENTDIR.'/', 0755, true);
if (!$_FILES['attachment'] && !$_FILES['localFileName'] && !$_FILES['localTTName'] &&
!$_POST['remoteFile'] && !$_POST['remoteTT'])
{
$response = array( 'success' => false, 'error' => 'UNDEFINED ACTION');
die(json_encode($response));
}
$fileFrmt = $_POST['filefrmt'];
$timeFrmt = $_POST['timefrmt'] ? $_POST['timefrmt'] : null;
$timeSmplg = $_POST['timesmpl'] ? $_POST['timesmpl'] : null;
$nonStd = $_POST['nonstd'] ? $_POST['nonstd'] : null;
$timeLength = $_POST['timelength'] ? $_POST['timelength'] : null;
$doy = $_POST['doy'] ? $_POST['doy'] : null;
$allFormats = array('fileFormat' => $fileFrmt, 'timeFormat' => $timeFrmt, 'doy' => $doy,
'timeSampling' => $timeSmplg, 'nonStandard' => $nonStd, 'timeLength' => $timeLength);
if ($_POST['filesrc'] == 'URL')
$fromURL = true;
else
$fromURL = false;
// to check ws sizw
$wsMgr = new UserMgr();
if ($fromURL)
{
// url files check
if ($_POST['remoteFile'])
{
$remoteName = $_POST['remoteFile'];
$fileName = substr(strrchr($remoteName,"/"),1);
$localName = USERDATADIR.$fileName;
$isFile = true;
}
else
{
$remoteName = $_POST['remoteTT'];
$fileName = substr(strrchr($remoteName,"/"),1);
$localName = USERTEMPDIR.$fileName;
$isFile = false;
}
$url = null;
if (substr($remoteName,0,4) == 'http')
$url = 'http';
if (substr($remoteName,0,3) == 'ftp')
$url = 'ftp';
if (!$url)
{
$response = array( 'success' => false, 'error' => 'Unknown net protocol');
die(json_encode($response));
}
if (file_exists($localName) && $isFile)
{
$response = array( 'success' => false, 'error' => 'File '.$fileName.' exists');
die(json_encode($response));
}
if ($url == 'ftp')
$fileSize = filesize($remoteName);
else
$fileSize = getUrlFileSize($remoteName);
if (!$fileSize)
{
$response = array( 'success' => false, 'error' => 'Can\'t estimate file size '.$fileName);
die(json_encode($response));
}
if ($file['size'] > DISK_QUOTA)
{
$response = array( 'success' => false, 'error' => 'The file you selected is too big for allowed disk quota');
die(json_encode($response));
}
if ($wsMgr->getWsSize() + $fileSize > DISK_QUOTA && $isFile)
{
$response = array( 'success' => false, 'error' => 'Please clean up you workspace. You are about to exceed available disk space');
die(json_encode($response));
}
if ($fileSize > maxSize)
{
$response = array( 'success' => false, 'error' => 'The uploaded file exceeds '.maxSize);
die(json_encode($response));
}
if (!copy($remoteName, $localName))
{
$response = array( 'success' => false, 'error' => 'Can\'t copy '.$fileName);
die(json_encode($response));
}
if (is_executable($localName))
{
$response = array( 'success' => false, 'error' => 'File '.$fileName.' is executable');
unlink($localName);
die(json_encode($response));
}
if ($isFile)
{
$fileMgr = new FilesMgr();
$amdaStat = new AmdaStats($_POST['sessionID']);
if ($amdaStat->success)
$amdaStat->addTask('upload', $_POST['sessionID'], null);
}
// Time Table
else
{
$response = array( 'success' => true, 'file' => $fileName, 'format' => $_POST['ttfrmt'] );
die(json_encode($response));
}
}
else
{
// local files check
if ($_FILES['localFileName'])
{
$file = $_FILES['localFileName'];
$localName = USERDATADIR.$file['name'];
$isFile = true;
}
else if ($_FILES['attachment'])
{
$file = $_FILES['attachment'];
$file['name'] = str_replace(" ","_",$file['name']);
$file['name'] = $_POST['sessionID'].'_'.date("Y-m-d\TH:i:s").'_'.$file['name'];
$localName = ATTACHMENTDIR.$file['name'];
$isFile = false;
}
else
{
$file = $_FILES['localTTName'];
$localName = USERTEMPDIR.$file['name'];
$isFile = false;
}
if($file['size'] === 0 && $file['error'] === 0){
$file['error'] = 5;
}
if ($file['error'] !== 0)
{
$response = array( 'success' => false, 'error' => file_upload_error_message($file['error']));
die(json_encode($response));
}
if (file_exists($localName) && $isFile)
{
$response = array( 'success' => false, 'error' => 'File '.$file['name'].' exists');
die(json_encode($response));
}
if (is_executable($file['tmp_name']))
{
$response = array( 'success' => false, 'error' => 'File '.$file['name'].' is executable');
unlink($file['tmp_name']);
die(json_encode($response));
}
if (!is_uploaded_file($file['tmp_name']))
{
$response = array( 'success' => false, 'error' => 'File '.$file['name'].' WASN\'T UPLOADED');
unlink($file['tmp_name']);
die(json_encode($response));
}
if ($file['size'] > DISK_QUOTA)
{
$response = array( 'success' => false, 'error' => 'The file you selected is too big for allowed disk quota');
die(json_encode($response));
}
if ($wsMgr->getWsSize() + $file['size'] > DISK_QUOTA)
{
$response = array( 'success' => false, 'error' => 'Please clean up your workspace. You are about to exceed available disk space');
die(json_encode($response));
}
if (!rename($file['tmp_name'], $localName))
{
$response = array( 'success' => false, 'error' => 'Cannot copy file '.$file['name']);
unlink($file['tmp_name']);
die(json_encode($response));
}
if (file_exists($localName) && !$isFile)
{
if ($_POST['ttfrmt'] == 'VOT')
{
$votMgr = new VOTableMgr();
if (!$votMgr->load($localName) || !$votMgr->isValidSchema())
{
$response = array( 'success' => false, 'error' => 'File '.$file['name'].' is not valid');
unlink($localName);
die(json_encode($response));
}
}
}
$fileName = $file['name'];
if ($isFile)
{
$fileMgr = new FilesMgr();
$amdaStat = new AmdaStats($_POST['sessionID']);
if ($amdaStat->success)
$amdaStat->addTask('upload', $_POST['sessionID'], null);
}
// Time Table
else
{
$response = array( 'success' => true, 'file' => $fileName, 'format' => $_POST['ttfrmt'] );
die(json_encode($response));
}
}
$response = $fileMgr->addFile($fileName, $allFormats);
echo json_encode($response);
?>