Blame view

php/uploadFile.php 11.7 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
165
166
 	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);
	}
	else if ($fromURL) 
901ba3f3   Elena.Budnik   upload catalog
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
	{
	// 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
184
      
901ba3f3   Elena.Budnik   upload catalog
185
		$url = null;
f1ff330b   Elena.Budnik   https is not impl...
186
187
188
189
190
191
192
		
		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
193
194
		if (substr($remoteName,0,4) == 'http')  $url = 'http';
		if (substr($remoteName,0,3) == 'ftp')   $url = 'ftp';
16035364   Benjamin Renard   First commit
195

901ba3f3   Elena.Budnik   upload catalog
196
197
198
199
200
		if (!$url) 
		{
			$response = array( 'success' => false, 'error' => 'Unknown net protocol'); 
			die(json_encode($response));
		}
16035364   Benjamin Renard   First commit
201

901ba3f3   Elena.Budnik   upload catalog
202
203
204
205
206
		if (file_exists($localName) && $isFile) 
		{
			$response = array( 'success' => false, 'error' => 'File  '.$fileName.' exists'); 
			die(json_encode($response));		 
		}
16035364   Benjamin Renard   First commit
207

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

901ba3f3   Elena.Budnik   upload catalog
211
212
213
214
215
		if (!$fileSize) 
		{
			$response = array( 'success' => false, 'error' => 'Can\'t estimate file size '.$fileName); 
			die(json_encode($response));
		}
16035364   Benjamin Renard   First commit
216
               
4607eaa0   Benjamin Renard   Add proxy support...
217
		if (isset($file['size']) && ($file['size'] > DISK_QUOTA)) 
901ba3f3   Elena.Budnik   upload catalog
218
219
220
221
		{
			$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...
222
		
901ba3f3   Elena.Budnik   upload catalog
223
224
225
226
227
		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
228

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

4607eaa0   Benjamin Renard   Add proxy support...
236
237
238

		// BRE - Add proxy host if exists
		$result = FALSE;
ba35574d   Benjamin Renard   Uniformize proxy ...
239
240
		$context = ProxyUtils::getStreamContextWithProxy();
		if (isset($context)) {
4607eaa0   Benjamin Renard   Add proxy support...
241
242
243
244
245
246
247
			$result = copy($remoteName, $localName, $context);
		}
		else {
			$result = copy($remoteName, $localName);
		}

		if (!$result) 
901ba3f3   Elena.Budnik   upload catalog
248
249
250
251
		{
			$response = array( 'success' => false, 'error' => 'Can\'t copy '.$fileName); 
			die(json_encode($response));
		}
16035364   Benjamin Renard   First commit
252

901ba3f3   Elena.Budnik   upload catalog
253
254
255
256
257
258
		if (is_executable($localName))
		{
			$response = array( 'success' => false, 'error' => 'File '.$fileName.' is executable');		   
			unlink($localName);
			die(json_encode($response));		   
		}
16035364   Benjamin Renard   First commit
259

901ba3f3   Elena.Budnik   upload catalog
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
		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
290
			
901ba3f3   Elena.Budnik   upload catalog
291
292
293
294
295
296
297
298
299
300
301
302
303
			$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
304
 	
901ba3f3   Elena.Budnik   upload catalog
305
306
307
308
		if($file['size'] === 0 && $file['error'] === 0)
		{
			$file['error'] = 5;
		}
9a329c57   Elena.Budnik   setSpecilaSettings
309
	
901ba3f3   Elena.Budnik   upload catalog
310
311
312
313
314
		if ($file['error'] !== 0) 
		{
			$response = array( 'success'=>false, 'error'=>file_upload_error_message($file['error'])); 		   
			die(json_encode($response));
		}
16035364   Benjamin Renard   First commit
315

901ba3f3   Elena.Budnik   upload catalog
316
317
318
319
320
		if (file_exists($localName) && $isFile) 
		{
			$response = array( 'success'=>false, 'error'=>'File  '.$file['name'].' exists'); 		  
			die(json_encode($response));
		}
16035364   Benjamin Renard   First commit
321
      
901ba3f3   Elena.Budnik   upload catalog
322
323
324
325
326
327
		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
328

901ba3f3   Elena.Budnik   upload catalog
329
330
331
332
333
334
		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
335
                
901ba3f3   Elena.Budnik   upload catalog
336
337
338
339
340
		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
341

901ba3f3   Elena.Budnik   upload catalog
342
343
344
345
346
		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
347
                    
901ba3f3   Elena.Budnik   upload catalog
348
349
350
351
352
353
		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
354
 	  
901ba3f3   Elena.Budnik   upload catalog
355
356
357
358
359
360
361
362
363
364
365
366
367
		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
368

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

901ba3f3   Elena.Budnik   upload catalog
371
372
373
		if ($isFile) 
		{
			$fileMgr = new FilesMgr();
9a329c57   Elena.Budnik   setSpecilaSettings
374
375
		//	$amdaStat = new AmdaStats($_POST['sessionID']);
		//	if ($amdaStat->success) $amdaStat->addTask('upload', $_POST['sessionID'], null);
901ba3f3   Elena.Budnik   upload catalog
376
377
378
379
380
381
382
383
384
		}
		// 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
385
 	 
901ba3f3   Elena.Budnik   upload catalog
386
	$response = $fileMgr->addFile($fileName, $allFormats);
16035364   Benjamin Renard   First commit
387
 	         
901ba3f3   Elena.Budnik   upload catalog
388
	echo json_encode($response);
16035364   Benjamin Renard   First commit
389
?>