Blame view

php/uploadFile.php 11.9 KB
16035364   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
<?php
 /**
 * @file uploadFile
 * @brief
 * @author Elena
 * @version $Id: uploadFile.php 2905 2015-05-18 10:08:24Z elena $
 *
 */

901ba3f3   Elena.Budnik   upload catalog
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
	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';
		}
	} 
16035364   Benjamin Renard   First commit
34

901ba3f3   Elena.Budnik   upload catalog
35
36
37
38
39
40
41
	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);
4607eaa0   Benjamin Renard   Add proxy support...
42
		// BRE - Add proxy host if exists
ba35574d   Benjamin Renard   Uniformize proxy ...
43
                ProxyUtils::addProxyForCurl($ch);
901ba3f3   Elena.Budnik   upload catalog
44
45
		$data = curl_exec($ch);
		curl_close($ch);
16035364   Benjamin Renard   First commit
46

901ba3f3   Elena.Budnik   upload catalog
47
48
49
50
51
52
53
		$ContentLength = 1000000000000000000;
		if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
			$ContentLength = (int)$matches[1];
		}
				
		return $ContentLength;
	}
b5c2b014   Elena.Budnik   uploaded_max_file...
54
55
56
57
58
59
60
61
62
63
64
65
66
	
	function return_bytes($val) {
		$val = trim($val);
		$last = strtolower($val[strlen($val)-1]);
		switch($last) {
			// Le modifieur 'G' est disponible depuis PHP 5.1.0
			case 'g':
				$val *= 1024;
			case 'm':
				$val *= 1024;
			case 'k':
				$val *= 1024;
		}
16035364   Benjamin Renard   First commit
67

b5c2b014   Elena.Budnik   uploaded_max_file...
68
69
70
		return $val;
	}
	
901ba3f3   Elena.Budnik   upload catalog
71
72
73
74
	/**
	*  Main
	*/ 
	define("UPLOAD_ERR_EMPTY",5);
16035364   Benjamin Renard   First commit
75

901ba3f3   Elena.Budnik   upload catalog
76
	require_once 'config.php';
16035364   Benjamin Renard   First commit
77

901ba3f3   Elena.Budnik   upload catalog
78
	// error_reporting(E_ERROR | E_WARNING | E_PARSE);
16035364   Benjamin Renard   First commit
79

b5c2b014   Elena.Budnik   uploaded_max_file...
80
	if (!isset($_POST['sessionID'])) {
901ba3f3   Elena.Budnik   upload catalog
81
82
83
		$response = array( 'success' => false, 'error' => ': Check that uploaded file size is less than '.ini_get('upload_max_filesize')); 
		die(json_encode($response));           
	}
16035364   Benjamin Renard   First commit
84

901ba3f3   Elena.Budnik   upload catalog
85
	define('USERDATADIR', USERPATH."/".$_POST['sessionID']."/DATA/");
585c86b4   Benjamin Renard   Minor fixes in up...
86
	define('USERTTDIR', USERPATH."/".$_POST['sessionID']."/TT/");
901ba3f3   Elena.Budnik   upload catalog
87
88
89
90
	define('USERTEMPDIR', USERPATH."/".$_POST['sessionID']."/TEMP/");
	define('USERWSDIR', USERPATH."/".$_POST['sessionID']."/WS/"); 
	define('USERWORKINGDIR', USERPATH."/".$_POST['sessionID']."/RES/");
	define('ATTACHMENTDIR', DATAPATH."/Feedback/Attach/");
16035364   Benjamin Renard   First commit
91

b5c2b014   Elena.Budnik   uploaded_max_file...
92
93
94
95
96
97
98
	$upload_max_filesize = return_bytes(ini_get('upload_max_filesize'));
	
	if ($upload_max_filesize  > $_POST['MAX_FILE_SIZE'])
			define('maxSize',$_POST['MAX_FILE_SIZE']);
	else 
			define('maxSize',$upload_max_filesize); // NEVER !
			
901ba3f3   Elena.Budnik   upload catalog
99
100
	if (!is_dir(USERTEMPDIR)) mkdir(USERTEMPDIR.'/', 0755, true);
	if (!is_dir(ATTACHMENTDIR)) mkdir(ATTACHMENTDIR.'/', 0755, true);
16035364   Benjamin Renard   First commit
101

585c86b4   Benjamin Renard   Minor fixes in up...
102
103
	if (!isset($_FILES['attachment']) && !isset($_FILES['localFileName']) && !isset($_FILES['localTTName']) && !isset($_FILES['localCatName']) && 
		!isset($_POST['remoteFile']) && !isset($_POST['remoteTT']) && !isset($_POST['remoteCat'])) 
901ba3f3   Elena.Budnik   upload catalog
104
105
106
107
	{
		$response = array( 'success' => false, 'error' => 'UNDEFINED ACTION'); 
		die(json_encode($response));
	}
16035364   Benjamin Renard   First commit
108
 
901ba3f3   Elena.Budnik   upload catalog
109
110
111
112
113
	$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;
585c86b4   Benjamin Renard   Minor fixes in up...
114
	$doy = isset($_POST['doy']) ?  $_POST['doy'] : null;
7d539a8c   Elena.Budnik   server side modifs
115
116
117
118
119
120
121
122
123
124
125
	$sampling =  isset($_POST['smpl']) ?  $_POST['smpl'] : null;
	$min_sampling = isset($_POST['min_manual_sampling']) ?  $_POST['min_manual_sampling'] : null;
	$max_sampling = isset($_POST['max_manual_sampling']) ?  $_POST['max_manual_sampling'] : null;
	
	if ($sampling == "auto" && $timeSmplg == "variable" 
				&&  (strtolower($fileFrmt) == "cdf" || strtolower($fileFrmt) == "nc")) {	
				$response = array('success' => false, 'error' => " : Auto-definition of variable sampling for $fileFrmt isn't implemented yet. 
				Use manual sampling definition");
				die(json_encode($response));
	}
	
6de48c3d   Benjamin Renard   Implement data up...
126
127
	$sampData = isset($_POST['sampData']) ? $_POST['sampData'] : null;
	$sampFileName = isset($_POST['sampFileName']) ? $_POST['sampFileName'] : null;
901ba3f3   Elena.Budnik   upload catalog
128
129

	$allFormats = array('fileFormat' => $fileFrmt, 'timeFormat' => $timeFrmt, 'doy' => $doy,
7d539a8c   Elena.Budnik   server side modifs
130
131
								'timeSampling' => $timeSmplg, 'nonStandard' => $nonStd, 'timeLength' => $timeLength,
								'samplingType' => $sampling, 'min_sampling' => $min_sampling, 'max_sampling' => $max_sampling);
16035364   Benjamin Renard   First commit
132

901ba3f3   Elena.Budnik   upload catalog
133
134
	if ($_POST['filesrc'] == 'URL') $fromURL = true;  
	else  $fromURL = false;
9a329c57   Elena.Budnik   setSpecilaSettings
135

51b1b2c0   Elena.Budnik   set user for spec...
136
137
	// to check ws size
	$wsMgr = new UserMgr($_POST['sessionID']);
9a329c57   Elena.Budnik   setSpecilaSettings
138
	$wsMgr->setSpecialSettings();
6de48c3d   Benjamin Renard   Implement data up...
139

51b1b2c0   Elena.Budnik   set user for spec...
140

6de48c3d   Benjamin Renard   Implement data up...
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
 	if (isset($sampData) && !empty($sampData)) {
		$fileMgr = new FilesMgr();
		$fileName = date("Y-m-d\TH:i:s").'_samp.vot';
		$tmpFilePath = USERTEMPDIR.$fileName;
		file_put_contents($tmpFilePath, $sampData);
		$fileSize = filesize($tmpFilePath);

		if ($wsMgr->getWsSize() + $fileSize > DISK_QUOTA)
                {
			$response = array( 'success' => false, 'error' => 'Please clean up you workspace. You are about to exceed available disk space');
			unlink($tmpFilePath);
			die(json_encode($response));
		}

		if ($fileSize > maxSize)
		{
			$maxMB = maxSize/1000000;
			$response = array( 'success' => false, 'error' => 'The uploaded file exceeds '.$maxMB.'MB');
			unlink($tmpFilePath);
			die(json_encode($response));
		}

		$dataFilePath = USERDATADIR.$fileName;
		rename($tmpFilePath,$dataFilePath);
e1e4b8ab   Elena.Budnik   added samp for stat
165
166
167
168
169
		
		$amdaStat = new AmdaStats($_POST['sessionID']);
		if ($amdaStat->success) {
			$amdaStat->addTask($_POST['sessionID'], 'samp', null);
		}
6de48c3d   Benjamin Renard   Implement data up...
170
171
	}
	else if ($fromURL) 
901ba3f3   Elena.Budnik   upload catalog
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
	{
	// url files check
		if ($_POST['remoteFile']) 
		{
			$remoteName = $_POST['remoteFile'];
			$fileName = substr(strrchr($remoteName,"/"),1);
			$localName = USERDATADIR.$fileName; 		 
			$isFile = true;
		}
		else 
		{
			$isTimeTable =  $_POST['remoteTT'] ? true : false;
			$remoteName = $isTimeTable ? $_POST['remoteTT'] : $_POST['remoteCat'];
			$fileName = substr(strrchr($remoteName,"/"),1);
			$localName = USERTEMPDIR.$fileName; 		 		 	     
			$isFile = false;
		}
16035364   Benjamin Renard   First commit
189
      
901ba3f3   Elena.Budnik   upload catalog
190
		$url = null;
f1ff330b   Elena.Budnik   https is not impl...
191
192
193
194
195
196
197
		
		if (substr($remoteName,0,5) == 'https') 
		{
			$response = array( 'success' => false, 'error' => ' : Sorry, https protocol is no implemented yet'); 
			die(json_encode($response));
		}
		
901ba3f3   Elena.Budnik   upload catalog
198
199
		if (substr($remoteName,0,4) == 'http')  $url = 'http';
		if (substr($remoteName,0,3) == 'ftp')   $url = 'ftp';
16035364   Benjamin Renard   First commit
200

901ba3f3   Elena.Budnik   upload catalog
201
202
203
204
205
		if (!$url) 
		{
			$response = array( 'success' => false, 'error' => 'Unknown net protocol'); 
			die(json_encode($response));
		}
16035364   Benjamin Renard   First commit
206

901ba3f3   Elena.Budnik   upload catalog
207
208
209
210
211
		if (file_exists($localName) && $isFile) 
		{
			$response = array( 'success' => false, 'error' => 'File  '.$fileName.' exists'); 
			die(json_encode($response));		 
		}
16035364   Benjamin Renard   First commit
212

901ba3f3   Elena.Budnik   upload catalog
213
214
		if ($url == 'ftp') $fileSize = filesize($remoteName);
		else $fileSize = getUrlFileSize($remoteName);
16035364   Benjamin Renard   First commit
215

901ba3f3   Elena.Budnik   upload catalog
216
217
218
219
220
		if (!$fileSize) 
		{
			$response = array( 'success' => false, 'error' => 'Can\'t estimate file size '.$fileName); 
			die(json_encode($response));
		}
16035364   Benjamin Renard   First commit
221
               
4607eaa0   Benjamin Renard   Add proxy support...
222
		if (isset($file['size']) && ($file['size'] > DISK_QUOTA)) 
901ba3f3   Elena.Budnik   upload catalog
223
224
225
226
		{
			$response = array( 'success' => false, 'error' => 'The file you selected is too big for allowed disk quota'); 
			die(json_encode($response));
		}
4607eaa0   Benjamin Renard   Add proxy support...
227
		
901ba3f3   Elena.Budnik   upload catalog
228
229
230
231
232
		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));
		}
16035364   Benjamin Renard   First commit
233

901ba3f3   Elena.Budnik   upload catalog
234
235
		if ($fileSize > maxSize) 
		{
b5c2b014   Elena.Budnik   uploaded_max_file...
236
237
			$maxMB = maxSize/1000000;
			$response = array( 'success' => false, 'error' => 'The uploaded file exceeds '.$maxMB.'MB'); 
901ba3f3   Elena.Budnik   upload catalog
238
239
			die(json_encode($response));
		}
16035364   Benjamin Renard   First commit
240

4607eaa0   Benjamin Renard   Add proxy support...
241
242
243

		// BRE - Add proxy host if exists
		$result = FALSE;
ba35574d   Benjamin Renard   Uniformize proxy ...
244
245
		$context = ProxyUtils::getStreamContextWithProxy();
		if (isset($context)) {
4607eaa0   Benjamin Renard   Add proxy support...
246
247
248
249
250
251
252
			$result = copy($remoteName, $localName, $context);
		}
		else {
			$result = copy($remoteName, $localName);
		}

		if (!$result) 
901ba3f3   Elena.Budnik   upload catalog
253
254
255
256
		{
			$response = array( 'success' => false, 'error' => 'Can\'t copy '.$fileName); 
			die(json_encode($response));
		}
16035364   Benjamin Renard   First commit
257

901ba3f3   Elena.Budnik   upload catalog
258
259
260
261
262
263
		if (is_executable($localName))
		{
			$response = array( 'success' => false, 'error' => 'File '.$fileName.' is executable');		   
			unlink($localName);
			die(json_encode($response));		   
		}
16035364   Benjamin Renard   First commit
264

901ba3f3   Elena.Budnik   upload catalog
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
		if ($isFile) 
		{
			$fileMgr = new FilesMgr();
			$amdaStat = new AmdaStats($_POST['sessionID']);
			if ($amdaStat->success) $amdaStat->addTask('upload', $_POST['sessionID'], null);
		}
		// Time Table
		else if ($isTimeTable)
		{
			$response = array( 'success'=>true, 'file'=>$fileName, 'format'=>$_POST['ttfrmt'] );	  	     
			die(json_encode($response)); 
		} 
		else
		{
			$response = array( 'success'=>true, 'file'=>$fileName, 'format'=>$_POST['catfrmt'] );	  	     
			die(json_encode($response)); 
		}
	}
	else 
	{
		// local files check
		if ($_FILES['localFileName']) 
		{
				$file = $_FILES['localFileName']; 
				$localName = USERDATADIR.$file['name'];
				$isFile = true;
		}
		else if ($_FILES['attachment']) 
		{
			if (!is_dir(ATTACHMENTDIR)) mkdir(ATTACHMENTDIR,755);
dcd07176   Elena.Budnik   Feedback manager
295
			
901ba3f3   Elena.Budnik   upload catalog
296
297
298
299
300
301
302
303
304
305
306
307
308
			$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 
		{
			$isTimeTable = $_FILES['localTTName'] ? true : false;
			$file = $isTimeTable ? $_FILES['localTTName'] : $_FILES['localCatName'];
			$localName = USERTEMPDIR.$file['name'];		     
			$isFile = false;
		}
16035364   Benjamin Renard   First commit
309
 	
901ba3f3   Elena.Budnik   upload catalog
310
311
312
313
		if($file['size'] === 0 && $file['error'] === 0)
		{
			$file['error'] = 5;
		}
9a329c57   Elena.Budnik   setSpecilaSettings
314
	
901ba3f3   Elena.Budnik   upload catalog
315
316
317
318
319
		if ($file['error'] !== 0) 
		{
			$response = array( 'success'=>false, 'error'=>file_upload_error_message($file['error'])); 		   
			die(json_encode($response));
		}
16035364   Benjamin Renard   First commit
320

901ba3f3   Elena.Budnik   upload catalog
321
322
323
324
325
		if (file_exists($localName) && $isFile) 
		{
			$response = array( 'success'=>false, 'error'=>'File  '.$file['name'].' exists'); 		  
			die(json_encode($response));
		}
16035364   Benjamin Renard   First commit
326
      
901ba3f3   Elena.Budnik   upload catalog
327
328
329
330
331
332
		if (is_executable($file['tmp_name']))
		{
			$response = array( 'success'=>false, 'error'=>'File '.$file['name'].' is executable'); 		  
			unlink($file['tmp_name']);
			die(json_encode($response));		   
		}
16035364   Benjamin Renard   First commit
333

901ba3f3   Elena.Budnik   upload catalog
334
335
336
337
338
339
		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));		    
		}	 
16035364   Benjamin Renard   First commit
340
                
901ba3f3   Elena.Budnik   upload catalog
341
342
343
344
345
		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));
		}
16035364   Benjamin Renard   First commit
346

901ba3f3   Elena.Budnik   upload catalog
347
348
349
350
351
		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));
		}
16035364   Benjamin Renard   First commit
352
                    
901ba3f3   Elena.Budnik   upload catalog
353
354
355
356
357
358
		if (!rename($file['tmp_name'], $localName)) 
		{          
			$response = array( 'success'=>false, 'error'=>'Cannot copy file '.$file['name']);		    
			unlink($file['tmp_name']);
			die(json_encode($response));		    
		}
16035364   Benjamin Renard   First commit
359
 	  
901ba3f3   Elena.Budnik   upload catalog
360
361
362
363
364
365
366
367
368
369
370
371
372
		if (file_exists($localName) && !$isFile) 
		{
			if ($_POST['ttfrmt'] == 'VOT' || $_POST['catfrmt'] == '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));
				} 
			}  
		}
16035364   Benjamin Renard   First commit
373

901ba3f3   Elena.Budnik   upload catalog
374
		$fileName = $file['name'];
16035364   Benjamin Renard   First commit
375

901ba3f3   Elena.Budnik   upload catalog
376
377
378
		if ($isFile) 
		{
			$fileMgr = new FilesMgr();
e7dc265f   Elena.Budnik   stat for upload file
379
			$amdaStat = new AmdaStats($_POST['sessionID']);
2dab1b1b   Elena.Budnik   change input args...
380
381
382
			if ($amdaStat->success) {
				$amdaStat->addTask( $_POST['sessionID'], 'upload', null);
			}
901ba3f3   Elena.Budnik   upload catalog
383
384
385
386
387
388
389
390
391
		}
		// Time Table or Catalog
		else 
		{
		      $format = $isTimeTable ? $_POST['ttfrmt'] : $_POST['catfrmt'];
				$response = array( 'success'=>true, 'file'=>$fileName, 'format'=>$format );	  	    
				die(json_encode($response)); 
		} 
	}
16035364   Benjamin Renard   First commit
392
 	 
901ba3f3   Elena.Budnik   upload catalog
393
	$response = $fileMgr->addFile($fileName, $allFormats);
16035364   Benjamin Renard   First commit
394
 	         
901ba3f3   Elena.Budnik   upload catalog
395
	echo json_encode($response);
16035364   Benjamin Renard   First commit
396
?>