Blame view

php/uploadFile.php 8.92 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
42
43
	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);
16035364   Benjamin Renard   First commit
44

901ba3f3   Elena.Budnik   upload catalog
45
46
47
48
49
50
51
		$ContentLength = 1000000000000000000;
		if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
			$ContentLength = (int)$matches[1];
		}
				
		return $ContentLength;
	}
16035364   Benjamin Renard   First commit
52

901ba3f3   Elena.Budnik   upload catalog
53
54
55
56
	/**
	*  Main
	*/ 
	define("UPLOAD_ERR_EMPTY",5);
16035364   Benjamin Renard   First commit
57

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

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

901ba3f3   Elena.Budnik   upload catalog
62
63
64
65
66
	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));           
	}
16035364   Benjamin Renard   First commit
67

901ba3f3   Elena.Budnik   upload catalog
68
69
70
71
72
73
	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'));
16035364   Benjamin Renard   First commit
74

901ba3f3   Elena.Budnik   upload catalog
75
76
	if (!is_dir(USERTEMPDIR)) mkdir(USERTEMPDIR.'/', 0755, true);
	if (!is_dir(ATTACHMENTDIR)) mkdir(ATTACHMENTDIR.'/', 0755, true);
16035364   Benjamin Renard   First commit
77

901ba3f3   Elena.Budnik   upload catalog
78
79
80
81
82
83
	if (!$_FILES['attachment'] && !$_FILES['localFileName'] && !$_FILES['localTTName'] && !$_FILES['localCatName'] && 
		!$_POST['remoteFile'] && !$_POST['remoteTT'] && !$_POST['remoteCat']) 
	{
		$response = array( 'success' => false, 'error' => 'UNDEFINED ACTION'); 
		die(json_encode($response));
	}
16035364   Benjamin Renard   First commit
84
 
901ba3f3   Elena.Budnik   upload catalog
85
86
87
88
89
90
91
92
93
	$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);
16035364   Benjamin Renard   First commit
94

901ba3f3   Elena.Budnik   upload catalog
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
	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 
		{
			$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
119
      
901ba3f3   Elena.Budnik   upload catalog
120
		$url = null;
16035364   Benjamin Renard   First commit
121

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

901ba3f3   Elena.Budnik   upload catalog
125
126
127
128
129
		if (!$url) 
		{
			$response = array( 'success' => false, 'error' => 'Unknown net protocol'); 
			die(json_encode($response));
		}
16035364   Benjamin Renard   First commit
130

901ba3f3   Elena.Budnik   upload catalog
131
132
133
134
135
		if (file_exists($localName) && $isFile) 
		{
			$response = array( 'success' => false, 'error' => 'File  '.$fileName.' exists'); 
			die(json_encode($response));		 
		}
16035364   Benjamin Renard   First commit
136

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

901ba3f3   Elena.Budnik   upload catalog
140
141
142
143
144
		if (!$fileSize) 
		{
			$response = array( 'success' => false, 'error' => 'Can\'t estimate file size '.$fileName); 
			die(json_encode($response));
		}
16035364   Benjamin Renard   First commit
145
               
901ba3f3   Elena.Budnik   upload catalog
146
147
148
149
150
151
152
153
154
155
156
		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));
		}
16035364   Benjamin Renard   First commit
157

901ba3f3   Elena.Budnik   upload catalog
158
159
160
161
162
		if ($fileSize > maxSize) 
		{
			$response = array( 'success' => false, 'error' => 'The uploaded file exceeds '.maxSize); 
			die(json_encode($response));
		}
16035364   Benjamin Renard   First commit
163

901ba3f3   Elena.Budnik   upload catalog
164
165
166
167
168
		if (!copy($remoteName, $localName)) 
		{
			$response = array( 'success' => false, 'error' => 'Can\'t copy '.$fileName); 
			die(json_encode($response));
		}
16035364   Benjamin Renard   First commit
169

901ba3f3   Elena.Budnik   upload catalog
170
171
172
173
174
175
		if (is_executable($localName))
		{
			$response = array( 'success' => false, 'error' => 'File '.$fileName.' is executable');		   
			unlink($localName);
			die(json_encode($response));		   
		}
16035364   Benjamin Renard   First commit
176

901ba3f3   Elena.Budnik   upload catalog
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
		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
207
			
901ba3f3   Elena.Budnik   upload catalog
208
209
210
211
212
213
214
215
216
217
218
219
220
			$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
221
 	
901ba3f3   Elena.Budnik   upload catalog
222
223
224
225
		if($file['size'] === 0 && $file['error'] === 0)
		{
			$file['error'] = 5;
		}
16035364   Benjamin Renard   First commit
226

901ba3f3   Elena.Budnik   upload catalog
227
228
229
230
231
		if ($file['error'] !== 0) 
		{
			$response = array( 'success'=>false, 'error'=>file_upload_error_message($file['error'])); 		   
			die(json_encode($response));
		}
16035364   Benjamin Renard   First commit
232

901ba3f3   Elena.Budnik   upload catalog
233
234
235
236
237
		if (file_exists($localName) && $isFile) 
		{
			$response = array( 'success'=>false, 'error'=>'File  '.$file['name'].' exists'); 		  
			die(json_encode($response));
		}
16035364   Benjamin Renard   First commit
238
      
901ba3f3   Elena.Budnik   upload catalog
239
240
241
242
243
244
		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
245

901ba3f3   Elena.Budnik   upload catalog
246
247
248
249
250
251
		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
252
                
901ba3f3   Elena.Budnik   upload catalog
253
254
255
256
257
		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
258

901ba3f3   Elena.Budnik   upload catalog
259
260
261
262
263
		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
264
                    
901ba3f3   Elena.Budnik   upload catalog
265
266
267
268
269
270
		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
271
 	  
901ba3f3   Elena.Budnik   upload catalog
272
273
274
275
276
277
278
279
280
281
282
283
284
		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
285

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

901ba3f3   Elena.Budnik   upload catalog
288
289
290
291
292
293
294
295
296
297
298
299
300
301
		if ($isFile) 
		{
			$fileMgr = new FilesMgr();
			$amdaStat = new AmdaStats($_POST['sessionID']);
			if ($amdaStat->success) $amdaStat->addTask('upload', $_POST['sessionID'], null);
		}
		// 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
302
 	 
901ba3f3   Elena.Budnik   upload catalog
303
	$response = $fileMgr->addFile($fileName, $allFormats);
16035364   Benjamin Renard   First commit
304
 	         
901ba3f3   Elena.Budnik   upload catalog
305
	echo json_encode($response);
16035364   Benjamin Renard   First commit
306
?>