Blame view

php/uploadFile.php 11.4 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
43
44
45
46
47
48
		// BRE - Add proxy host if exists
		if (defined('HTTP_PROXY_HOST')) {
			curl_setopt($ch, CURLOPT_PROXY, "http://".HTTP_PROXY_HOST);
			if (defined('HTTP_PROXY_USER')) {
				curl_setopt($ch, CURLOPT_PROXYUSERPWD, HTTP_PROXY_USER);
			}
		}
901ba3f3   Elena.Budnik   upload catalog
49
50
		$data = curl_exec($ch);
		curl_close($ch);
16035364   Benjamin Renard   First commit
51

901ba3f3   Elena.Budnik   upload catalog
52
53
54
55
56
57
58
		$ContentLength = 1000000000000000000;
		if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
			$ContentLength = (int)$matches[1];
		}
				
		return $ContentLength;
	}
b5c2b014   Elena.Budnik   uploaded_max_file...
59
60
61
62
63
64
65
66
67
68
69
70
71
	
	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
72

b5c2b014   Elena.Budnik   uploaded_max_file...
73
74
75
		return $val;
	}
	
901ba3f3   Elena.Budnik   upload catalog
76
77
78
79
	/**
	*  Main
	*/ 
	define("UPLOAD_ERR_EMPTY",5);
16035364   Benjamin Renard   First commit
80

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

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

b5c2b014   Elena.Budnik   uploaded_max_file...
85
	if (!isset($_POST['sessionID'])) {
901ba3f3   Elena.Budnik   upload catalog
86
87
88
		$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
89

901ba3f3   Elena.Budnik   upload catalog
90
	define('USERDATADIR', USERPATH."/".$_POST['sessionID']."/DATA/");
585c86b4   Benjamin Renard   Minor fixes in up...
91
	define('USERTTDIR', USERPATH."/".$_POST['sessionID']."/TT/");
901ba3f3   Elena.Budnik   upload catalog
92
93
94
95
	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
96

b5c2b014   Elena.Budnik   uploaded_max_file...
97
98
99
100
101
102
103
	$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
104
105
	if (!is_dir(USERTEMPDIR)) mkdir(USERTEMPDIR.'/', 0755, true);
	if (!is_dir(ATTACHMENTDIR)) mkdir(ATTACHMENTDIR.'/', 0755, true);
16035364   Benjamin Renard   First commit
106

585c86b4   Benjamin Renard   Minor fixes in up...
107
108
	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
109
110
111
112
	{
		$response = array( 'success' => false, 'error' => 'UNDEFINED ACTION'); 
		die(json_encode($response));
	}
16035364   Benjamin Renard   First commit
113
 
901ba3f3   Elena.Budnik   upload catalog
114
115
116
117
118
	$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...
119
	$doy = isset($_POST['doy']) ?  $_POST['doy'] : null;
6de48c3d   Benjamin Renard   Implement data up...
120
121
	$sampData = isset($_POST['sampData']) ? $_POST['sampData'] : null;
	$sampFileName = isset($_POST['sampFileName']) ? $_POST['sampFileName'] : null;
901ba3f3   Elena.Budnik   upload catalog
122
123
124

	$allFormats = array('fileFormat' => $fileFrmt, 'timeFormat' => $timeFrmt, 'doy' => $doy,
								'timeSampling' => $timeSmplg, 'nonStandard' => $nonStd, 'timeLength' => $timeLength);
16035364   Benjamin Renard   First commit
125

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

901ba3f3   Elena.Budnik   upload catalog
129
130
	// to check ws sizw
	$wsMgr = new UserMgr();
9a329c57   Elena.Budnik   setSpecilaSettings
131
	$wsMgr->setSpecialSettings();
6de48c3d   Benjamin Renard   Implement data up...
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

 	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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
	{
	// 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
176
      
901ba3f3   Elena.Budnik   upload catalog
177
		$url = null;
16035364   Benjamin Renard   First commit
178

901ba3f3   Elena.Budnik   upload catalog
179
180
		if (substr($remoteName,0,4) == 'http')  $url = 'http';
		if (substr($remoteName,0,3) == 'ftp')   $url = 'ftp';
16035364   Benjamin Renard   First commit
181

901ba3f3   Elena.Budnik   upload catalog
182
183
184
185
186
		if (!$url) 
		{
			$response = array( 'success' => false, 'error' => 'Unknown net protocol'); 
			die(json_encode($response));
		}
16035364   Benjamin Renard   First commit
187

901ba3f3   Elena.Budnik   upload catalog
188
189
190
191
192
		if (file_exists($localName) && $isFile) 
		{
			$response = array( 'success' => false, 'error' => 'File  '.$fileName.' exists'); 
			die(json_encode($response));		 
		}
16035364   Benjamin Renard   First commit
193

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

901ba3f3   Elena.Budnik   upload catalog
197
198
199
200
201
		if (!$fileSize) 
		{
			$response = array( 'success' => false, 'error' => 'Can\'t estimate file size '.$fileName); 
			die(json_encode($response));
		}
16035364   Benjamin Renard   First commit
202
               
4607eaa0   Benjamin Renard   Add proxy support...
203
		if (isset($file['size']) && ($file['size'] > DISK_QUOTA)) 
901ba3f3   Elena.Budnik   upload catalog
204
205
206
207
		{
			$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...
208
		
901ba3f3   Elena.Budnik   upload catalog
209
210
211
212
213
		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
214

901ba3f3   Elena.Budnik   upload catalog
215
216
		if ($fileSize > maxSize) 
		{
b5c2b014   Elena.Budnik   uploaded_max_file...
217
218
			$maxMB = maxSize/1000000;
			$response = array( 'success' => false, 'error' => 'The uploaded file exceeds '.$maxMB.'MB'); 
901ba3f3   Elena.Budnik   upload catalog
219
220
			die(json_encode($response));
		}
16035364   Benjamin Renard   First commit
221

4607eaa0   Benjamin Renard   Add proxy support...
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

		// BRE - Add proxy host if exists
		$result = FALSE;
		if (defined('HTTP_PROXY_HOST')) {
			$options = array(
				'http' => array(
					'method' => 'GET',
					'timeout' => '5',
					'user_agent' => 'PHP libxml agent',
					'ignore_errors' => true,
				)
			);
			$options['http']['proxy'] = 'tcp://'.HTTP_PROXY_HOST;
			$options['http']['request_fulluri'] = TRUE;
			if (defined('HTTP_PROXY_USER')) {
				$options['http']['header'] = "Proxy-Authorization: Basic ".base64_encode(HTTP_PROXY_USER);
			}
			$context = stream_context_create($options);
			$result = copy($remoteName, $localName, $context);
		}
		else {
			$result = copy($remoteName, $localName);
		}

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

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

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

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

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

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

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

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