Blame view

php/classes/UserMgr.php 22.6 KB
16035364   Benjamin Renard   First commit
1
2
3
<?php
/**
 * @class UserMgr
aa94fd24   elena   Merge with last svn
4
 * @version $Id: UserMgr.php 2926 2015-06-01 14:01:53Z elena $
16035364   Benjamin Renard   First commit
5
6
7
8
9
10
11
12
13
14
15
 * 
 */

/*
define('DATAPATH', USERPATH.'WS/');
define('REQPATH',USERPATH.'REQ/');
define('RESDIRNAME',USERPATH.'RES/');
define('TTPATH', USERPATH.'TT/');
define('JOBPATH', USERPATH.'jobs/');
*/

d471cf49   Elena.Budnik   format; IMPEX in ...
16
17
18
19
20
21
22
23
class UserMgr 
{
	public $user, $IP, $userdir;
	protected $passwd;
	protected $sessionID;
	protected $userDirs = array('USERWSDIR' => 'WS', 'USERREQDIR' => 'REQ', 'USERDATADIR' => 'DATA',
					'USERWORKINGDIR' =>'RES', 'USERTTDIR' => 'TT', 'USERJOBDIR' => 'JOBS',
					'USERTEMPDIR' => 'TEMP');	
175ca164   Elena.Budnik   bad commit of Use...
24
	protected $userGrps;
d471cf49   Elena.Budnik   format; IMPEX in ...
25
26
27
28
29
30
	protected $amdaClient; //client to dd webservice
	
	public $isFirst = false;
	public $isNewInfo = false;
	public $isSpecialInfo = null;

e57cb025   Benjamin Renard   Fix most of error...
31
	function __construct($username = NULL, $password = NULL, $sessionID = NULL) 
d471cf49   Elena.Budnik   format; IMPEX in ...
32
	{	 
9140bdbd   Myriam Bouchemit   for proxy amdates...
33
      // if magic quotes is on, stripslashes
175ca164   Elena.Budnik   bad commit of Use...
34
		if(get_magic_quotes_gpc()) {
d471cf49   Elena.Budnik   format; IMPEX in ...
35
			$in = array(&$_GET, &$_POST, &$_COOKIE);
175ca164   Elena.Budnik   bad commit of Use...
36
37
38
			while(list($k,$v) = each($in)) {
				foreach($v as $key => $val) {
					if(!is_array($val)) {
d471cf49   Elena.Budnik   format; IMPEX in ...
39
40
41
42
43
44
45
						$in[$k][$key] = stripslashes($val);
						continue;
					}
					$in[] =& $in[$k][$key];
				}
			}
			unset($in);
16035364   Benjamin Renard   First commit
46
		}
16035364   Benjamin Renard   First commit
47
     
175ca164   Elena.Budnik   bad commit of Use...
48
		if (isset($_POST['username'])) {
d471cf49   Elena.Budnik   format; IMPEX in ...
49
			// Process Guest Login
175ca164   Elena.Budnik   bad commit of Use...
50
			if (strcasecmp(trim($_POST['username']),"guest") == 0) {
d471cf49   Elena.Budnik   format; IMPEX in ...
51
52
				$this->processGuestLogin();
			}
175ca164   Elena.Budnik   bad commit of Use...
53
			else {
d471cf49   Elena.Budnik   format; IMPEX in ...
54
55
56
57
58
59
60
				$this->user = trim($_POST['username']);
			}
		}
		else if (isset($username))
			$this->user = trim($username);

		//TODO crypt / decrypt
175ca164   Elena.Budnik   bad commit of Use...
61
		if (!isset($this->passwd)) {
d471cf49   Elena.Budnik   format; IMPEX in ...
62
63
64
65
66
			if (isset($_POST['password'])) 
					$this->passwd = $_POST['password'];
			else if (isset($password))
					$this->passwd = $password;
		}
9140bdbd   Myriam Bouchemit   for proxy amdates...
67

d471cf49   Elena.Budnik   format; IMPEX in ...
68
69
70
71
		if (isset($_GET['sessionID'])) 
			$this->user = $_GET['sessionID'];
		else if (isset($sessionID))
			$this->user = $sessionID;
175ca164   Elena.Budnik   bad commit of Use...
72
73
		//TODO if AmdaClient is needed ?
		$this->amdaClient = new AmdaClient();
d471cf49   Elena.Budnik   format; IMPEX in ...
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
	}

	//TODO if needed?? set session ID
	protected function setID() {}

	public  function getIPclient()
	{
		/*
			REMOTE_ADDR is the only really reliable information,
			as it is transmitted to you by your web server that
			is handling the request. It can be theoretically 
			falsified as well, but that is much, much harder 
			than spoofing a header value, and an entirely different class of attack.
		*/
		if (getenv('REMOTE_ADDR'))  
		{       
175ca164   Elena.Budnik   bad commit of Use...
90
91
92
			$realIP = getenv('REMOTE_ADDR'); 
			
			if ($realIP == '10.10.131.1' || $realIP == '10.10.135.119') { // proxy amdatest et openam; amdadev
d471cf49   Elena.Budnik   format; IMPEX in ...
93
94
95
96
97
98
99
100
101
102
103
104
				$allIPs = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
				$realIP = count($allIPs) > 1 ? trim($allIPs[0]) : $_SERVER['HTTP_X_FORWARDED_FOR'];
			}
		}          
		else 
		{
			//get local IP
			$command="hostname -i";
			$realIP = exec($command);
		}
		return $realIP;
	}
9140bdbd   Myriam Bouchemit   for proxy amdates...
105
      
d471cf49   Elena.Budnik   format; IMPEX in ...
106
107
	public function getUserInfo()
	{
175ca164   Elena.Budnik   bad commit of Use...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
	// 	array("success"     => TRUE,
	// 		       "login"       => $login,
	// 		       "name"        => ($theUser->length > 0) ? $theUser->item(0)->getAttribute("name") : "undefined",
	// 		       "first_name"  => ($theUser->length > 0) ? $theUser->item(0)->getAttribute("first_name") : "undefined",
	// 		       "group"       => $this->getUserMemberGroups($login),
	// 		       "email"       => ($theUser->length > 0) ? $theUser->item(0)->getAttribute("email") : "undefined",
	// 		       "date"        => ($theUser->length > 0) ? $theUser->item(0)->getAttribute("date")  : "undefined",
	// 		       "news"        => ($theUser->length > 0) ? $theUser->item(0)->getAttribute("news")  : "0");
		$info =  $this->amdaClient->getUserInfo($this->user);
		$wsSize = intval($this->getWsSize()/1024./1024.);
		$quota = intval($this->getSpecialSettings()/1024./1024.);
		$info['total'] = $quota;
		$info['available'] = $quota - $wsSize;
		$info['used'] = $wsSize;
		
		return $info;
d471cf49   Elena.Budnik   format; IMPEX in ...
124
	}
9140bdbd   Myriam Bouchemit   for proxy amdates...
125

d471cf49   Elena.Budnik   format; IMPEX in ...
126
127
128
	public function createDir() 
	{ 
		if (!mkdir($this->userdir, 0755, true)) return false;
9140bdbd   Myriam Bouchemit   for proxy amdates...
129

175ca164   Elena.Budnik   bad commit of Use...
130
		foreach ($this->userDirs as $key => $val) {
d471cf49   Elena.Budnik   format; IMPEX in ...
131
132
133
134
			if (!mkdir($this->userdir.$val.'/', 0755, true)) return false;
		}
		return true;
	}
175ca164   Elena.Budnik   bad commit of Use...
135
136
137
138
139
140
141
142
143
144
	
	protected function getUserGrps() 
	{
		$info = $this->amdaClient->getUserInfo($this->user); 
		
		if ($info['group'] == '')
				return null;
		else
				return explode(',',$info['group']);
	}
16035364   Benjamin Renard   First commit
145

175ca164   Elena.Budnik   bad commit of Use...
146
147
148
149
	/*
	* Check if special groups with settings exist and user is from these groups
	* Take the first group from user list
	*/
d471cf49   Elena.Budnik   format; IMPEX in ...
150
151
	protected function isSpecialGroup() 
	{
d471cf49   Elena.Budnik   format; IMPEX in ...
152
		$specialGrps = new DomDocument("1.0");
16035364   Benjamin Renard   First commit
153

d471cf49   Elena.Budnik   format; IMPEX in ...
154
		if (!($specialGrps->load(specialGrpsXml))) return null; 
16035364   Benjamin Renard   First commit
155

d471cf49   Elena.Budnik   format; IMPEX in ...
156
157
		$specialGrpNode = null;

175ca164   Elena.Budnik   bad commit of Use...
158
		foreach ($this->userGrps as $grp) {
d471cf49   Elena.Budnik   format; IMPEX in ...
159
160
161
162
163
164
			$specialGrpNode = $specialGrps->getElementById($grp); 
			if ($specialGrpNode) break;                            
		}
		
		return $specialGrpNode;
	}
7baca0ef   Elena.Budnik   user special sett...
165
  
175ca164   Elena.Budnik   bad commit of Use...
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
	public function getSpecialSettings()
	{
		$userSettings = new DomDocument("1.0");

		if (!file_exists(specialSettingsXml) || !$userSettings->load(specialSettingsXml)) {
			return DISK_QUOTA_standard;    
		}
		
		$theUser = $userSettings->getElementById($this->user);
		if (!$theUser) {
			return  DISK_QUOTA_standard;
		}
		
		$settings = $theUser->getElementsByTagName("setting");        
		if ($settings->length == 0) {
			return DISK_QUOTA_standard;
		}
		
		foreach ($settings as $setting) {                            
			if ($setting->getAttribute("name") == 'DISK_QUOTA')
				return $setting->getAttribute("value");                   
		}	 
		
	}
	
7baca0ef   Elena.Budnik   user special sett...
191
	public function setSpecialSettings() 
e57cb025   Benjamin Renard   Fix most of error...
192
	{	
7baca0ef   Elena.Budnik   user special sett...
193
194
		$userSettings = new DomDocument("1.0");

e57cb025   Benjamin Renard   Fix most of error...
195
		if (!file_exists(specialSettingsXml) || !$userSettings->load(specialSettingsXml)) {
7baca0ef   Elena.Budnik   user special sett...
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
			define("DISK_QUOTA", DISK_QUOTA_standard);    
			return; 
		}

		$theUser = $userSettings->getElementById($this->user);
		if (!$theUser) {
			define("DISK_QUOTA", DISK_QUOTA_standard);
			return;
		}
		$settings = $theUser->getElementsByTagName("setting");        
		if ($settings->length == 0) {
			define("DISK_QUOTA", DISK_QUOTA_standard);
			return;
		}

		foreach ($settings as $setting) {                            
175ca164   Elena.Budnik   bad commit of Use...
212
213
214
215
216
217
218
219
220
221
222
			$key = $setting->getAttribute("name");
			$value =  $setting->getAttribute("value");
			$isSetting = $setting->hasAttribute("isSetting");                   
			if ($isSetting) {
				ini_set("$key",$value);
				}
			else {
				// Attention !!!  CONSTANT cannot be redefined
				define("$key",$value);                    
			}           
		} 		
7baca0ef   Elena.Budnik   user special sett...
223
	}
175ca164   Elena.Budnik   bad commit of Use...
224
225
226
/*
*  Totally replace IMPEX staff in user remote tree
*/
d471cf49   Elena.Budnik   format; IMPEX in ...
227
228
229
	protected function updateImpex()
	{
		$myRemoteBases = new DomDocument("1.0");   
9140bdbd   Myriam Bouchemit   for proxy amdates...
230
                    
9b6c46d9   Elena.Budnik   THEMIS Remote center
231
		if (!@$myRemoteBases->load(USERWSDIR.'RemoteParams.xml')) return false;
37105924   Elena.Budnik   small nettoyage
232

d471cf49   Elena.Budnik   format; IMPEX in ...
233
		$myBases = $myRemoteBases->getElementsByTagName('dataCenter');
9140bdbd   Myriam Bouchemit   for proxy amdates...
234

d471cf49   Elena.Budnik   format; IMPEX in ...
235
236
237
		if ($myBases->length < 1) return false;
		// Delete all impex staff at first
		$i = $myBases->length - 1; 
9140bdbd   Myriam Bouchemit   for proxy amdates...
238
 
d471cf49   Elena.Budnik   format; IMPEX in ...
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
		while ($i > -1) 
		{ 
			$base = $myBases->item($i);
			$id = $base->getAttribute('xml:id'); 

			if ($id == 'FMI_GUMICS')
			{
				$simuRemoteBases = new DomDocument("1.0"); 
				$simuRemoteBases->load(USERWSDIR.'RemoteParams.xml');
			
				$gumicsNode = $simuRemoteBases->getElementById('FMI_GUMICS');
				if ($gumicsNode != NULL)
					$gumicsRuns = $gumicsNode->getElementsByTagName('runID');
			}

			if (($base->hasAttribute('isSimulation')) || $id == "CLWeb") 
			{ 
				$base->parentNode->removeChild($base);   
			}                  
			$i--; 
		} 

		$remoteBases = new DomDocument("1.0");
		if (!$remoteBases->load(RemoteData.'Bases.xml')) return false;
		
		$bases = $remoteBases->getElementsByTagName('dataCenter');
		if ($bases->length < 1) return false;   

		$myRootElement = $myRemoteBases->documentElement; 

		// and add impex
		foreach ($bases as $base) 
		{
			//TODO && $id != 'FMI_GUMICS' or ! hasAttribute('isAddable')
			if ($base->hasAttribute('isSimulation')) 
			{													
				$baseId = $base->getAttribute('xml:id');
6657f68b   Elena.Budnik   monitor impex ser...
276
				 
d471cf49   Elena.Budnik   format; IMPEX in ...
277
278
279
280
281
				$baseXml = new DomDocument("1.0");
				// no data base description ; skip this data base  
				if (!file_exists(RemoteData.$baseId.'/base.xml')) continue;

				// can't read base.xml ; skip this data base
9b6c46d9   Elena.Budnik   THEMIS Remote center
282
				if (!@$baseXml->load(RemoteData.$baseId.'/base.xml'))  continue;                                                          
d471cf49   Elena.Budnik   format; IMPEX in ...
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
																																							
				$myBase = $myRemoteBases->importNode($baseXml->getElementById($baseId), true);
				$myBase->setAttribute('name', $base->getAttribute('name'));
				$myBase->setAttribute('desc', $base->getAttribute('desc'));
					
				if ($baseId == "FMI_GUMICS")
				{
					$gumicsSimuReg = $myRemoteBases->getElementById('FMI_GUMICS_Earth_Magnetosphere');
										
					if (($gumicsSimuReg != NULL)&& ($gumicsRuns->length > 0))
					{ 
						for($c = 0; $c < $gumicsRuns->length; $c++)
						{
							$node = $gumicsRuns->item($c);
							$node = $myRemoteBases->importNode($node, true);
							$gumicsSimuReg->appendChild($node); 
						} 
					}
6657f68b   Elena.Budnik   monitor impex ser...
301
302
303
304
305
				} 
				// Check if WebService is ON				 
				$center = new $baseId(); 			 							  
				$myBase->setAttribute('available',$center->monitor());
 									
d471cf49   Elena.Budnik   format; IMPEX in ...
306
307
				$myRootElement->appendChild($myBase); 
			}
6657f68b   Elena.Budnik   monitor impex ser...
308
309
		} 
		
d471cf49   Elena.Budnik   format; IMPEX in ...
310
311
312
		return $myRemoteBases->save(USERWSDIR.'RemoteParams.xml');
	} 
    
9140bdbd   Myriam Bouchemit   for proxy amdates...
313
/*
d471cf49   Elena.Budnik   format; IMPEX in ...
314
*  make remote data tree from list of distant bases if it doezn't exist
175ca164   Elena.Budnik   bad commit of Use...
315
316
317
*/ 

protected function makeRemoteTree() 
d471cf49   Elena.Budnik   format; IMPEX in ...
318
319
	{  
		$remoteBases = new DomDocument("1.0");
9b6c46d9   Elena.Budnik   THEMIS Remote center
320
		
0dc31ba8   Elena.Budnik   remoteParamManage...
321
unlink(USERWSDIR.'RemoteParams.xml');
d471cf49   Elena.Budnik   format; IMPEX in ...
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
		if (file_exists(USERWSDIR.'RemoteParams.xml')) 
		{
			// replace IMPEX nodes
			$status = $this->updateImpex();
			
			if (!$status) 
				error_log('IMPEX Remote Base can not be updated',1,email);
			// check/change access rights
			$remoteBases->load(USERWSDIR.'RemoteParams.xml');
			$bases = $remoteBases->getElementsByTagName('dataCenter');
			
			$delete = new UserDeleteObsolete();
			if ($bases->length > 0) 
				foreach ($bases as $base) 
				{
					$baseId = $base->getAttribute('xml:id');
6657f68b   Elena.Budnik   monitor impex ser...
338
					if ($base->getAttribute('isSimulation')) continue;
9b6c46d9   Elena.Budnik   THEMIS Remote center
339
340
341
					
// 					$notAvailable = (array_search($baseId,$availableMis) === FALSE);
// 				   $base->setAttribute('available',!$notAvailable);
6657f68b   Elena.Budnik   monitor impex ser...
342
											 							
9b6c46d9   Elena.Budnik   THEMIS Remote center
343
					// Update Info on External Data Sets in RemoteTree.xml for everything except IMPEX
d471cf49   Elena.Budnik   format; IMPEX in ...
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
					$dataSets = $base->getElementsByTagName("dataset");
					if ($dataSets->length > 0) 
					{
						$baseExtDom = new DomDocument("1.0");
						if (!$baseExtDom->load(RemoteData.$baseId.'/base.xml')) 
						{ 
							$base->setAttribute("desc","ATTENTION!!! This Base set DOES NOT ANY MORE EXIST!!! Remove it from your tree");                                                                         
							$base->setAttribute('obsolete', true);
							error_log('NO '.RemoteData.$baseId.'/base.xml',1,email);
							continue;
						}
						foreach ($dataSets as $dataSet) 
						{
							$dataSetID = $dataSet->getAttribute("xml:id");
							$origDataSet = $baseExtDom->getElementById($dataSetID);
							if ($origDataSet != null) 
							{
								$desc = $origDataSet->getAttribute("desc");
								if ($desc != null) $dataSet->setAttribute("desc", $desc);
							}
							else 
							{                                        
								$delete->setVI($dataSet->getAttribute('name'));                               
								$res = $delete->deleteDerived();                                                                           
								$res = $delete->deleteConditions();                                      
								$res = $delete->deleteRequests();                                                                               
								$res = $delete->deleteAliases();                                                                                                              
9b6c46d9   Elena.Budnik   THEMIS Remote center
371
								$dataSet->setAttribute("desc","ATTENTION!!! This data set DOES NOT EXIST ANY MORE !!! Remove it from your tree");                    
d471cf49   Elena.Budnik   format; IMPEX in ...
372
373
374
375
376
377
378
379
								$dataSet->setAttribute('obsolete', true);
							}
						}
					}                  
				} 
																																																																							
			return $remoteBases->save(USERWSDIR.'RemoteParams.xml'); 
		}	 
16035364   Benjamin Renard   First commit
380
             
d471cf49   Elena.Budnik   format; IMPEX in ...
381
382
		$remoteBases->load(RemoteData.'Bases.xml');
		$bases = $remoteBases->getElementsByTagName('dataCenter');
16035364   Benjamin Renard   First commit
383
               
d471cf49   Elena.Budnik   format; IMPEX in ...
384
		if ($bases->length == 0) return 'NO REMOTE BASES';         
16035364   Benjamin Renard   First commit
385
  	   
0dc31ba8   Elena.Budnik   remoteParamManage...
386
		$paramMgr = new RemoteParamManager();
d471cf49   Elena.Budnik   format; IMPEX in ...
387
388
389
		$paramMgr->xmlDom = new DomDocument("1.0");
		$paramMgr->xmlDom->formatOutput = true;
		$paramMgr->xmlDom->preserveWhiteSpace = false;
9b6c46d9   Elena.Budnik   THEMIS Remote center
390
             
d471cf49   Elena.Budnik   format; IMPEX in ...
391
392
		$BASE = $paramMgr->xmlDom->createElement('dataRoot'); 
		$BASE->setAttribute('xml:id','myRemoteData-treeRootNode');
16035364   Benjamin Renard   First commit
393
  
d471cf49   Elena.Budnik   format; IMPEX in ...
394
		$baseXml = new DomDocument("1.0");
16035364   Benjamin Renard   First commit
395
	    	    
d471cf49   Elena.Budnik   format; IMPEX in ...
396
397
		foreach ($bases as $base) 
		{	     
d471cf49   Elena.Budnik   format; IMPEX in ...
398
399
			$baseId = $base->getAttribute('xml:id');
			$paramMgr->baseId = $baseId;
9b6c46d9   Elena.Budnik   THEMIS Remote center
400

d471cf49   Elena.Budnik   format; IMPEX in ...
401
			// no data base description ; skip this data base  
9b6c46d9   Elena.Budnik   THEMIS Remote center
402
			if (!@file_exists(RemoteData.$baseId.'/base.xml')) continue;
16035364   Benjamin Renard   First commit
403
	  	                                    	       
d471cf49   Elena.Budnik   format; IMPEX in ...
404
			// can't read base.xml ; skip this data base
e57cb025   Benjamin Renard   Fix most of error...
405
			if (!@$baseXml->load(RemoteData.$baseId.'/base.xml')) continue;			        			      
d471cf49   Elena.Budnik   format; IMPEX in ...
406

9b6c46d9   Elena.Budnik   THEMIS Remote center
407
408
409
			if ($base->hasAttribute('default'))  
			{ 
				if ($base->hasAttribute('isSimulation'))
d471cf49   Elena.Budnik   format; IMPEX in ...
410
				{
9b6c46d9   Elena.Budnik   THEMIS Remote center
411
412
413
					$centerNode = $paramMgr->xmlDom->importNode($baseXml->getElementById($baseId), true);
					$datasets = $centerNode->getElementsByTagName('dataset');
					foreach ($datasets as $dataset)
d471cf49   Elena.Budnik   format; IMPEX in ...
414
415
416
417
					{
						$infoFileName = $paramMgr->getInfoName($dataset->getAttribute('name'));
						$paramMgr->localInfo = RemoteData.$baseId.'/'.$infoFileName;

9b6c46d9   Elena.Budnik   THEMIS Remote center
418
						if (!@file_exists($paramMgr->localInfo)) continue;				 
d471cf49   Elena.Budnik   format; IMPEX in ...
419

9b6c46d9   Elena.Budnik   THEMIS Remote center
420
421
422
423
						$params = $dataset->getElementsByTagName('parameter');
						$paramMgr->remoteViId = $dataset->getAttribute('name');
						foreach ($params as $param) 
						{
d471cf49   Elena.Budnik   format; IMPEX in ...
424
425
426
427
							$paramMgr->paramId = $param->getAttribute('name');
							$paramGlobalId =  $param->getAttribute('xml:id');				  
							$paramMgr->paramXML = RemoteData.'PARAMS/'.$paramGlobalId.'.xml';	
										
9b6c46d9   Elena.Budnik   THEMIS Remote center
428
							if (!@file_exists($paramMgr->paramXML)) continue;
d471cf49   Elena.Budnik   format; IMPEX in ...
429
							if (!$paramMgr->paramDom) 
9b6c46d9   Elena.Budnik   THEMIS Remote center
430
									$paramMgr->paramDom = new DomDocument("1.0");
d471cf49   Elena.Budnik   format; IMPEX in ...
431
432
433
434
435

							$paramMgr->paramDom->load($paramMgr->paramXML);
							
							if (!$paramMgr->makeComponents($param)) continue;
						}
9b6c46d9   Elena.Budnik   THEMIS Remote center
436
437
438
439
440
441
442
443
444
445
					}
				}
				/*
				*   Some small & well known data centers are included by default: 
				*   Each VI structure and VI (dataset) description at DDBASE for them should exist
				*/
				else 
				{
					$center = new $baseId();				
					$centerNode = $center->makeCenterNode($paramMgr->xmlDom);				
16035364   Benjamin Renard   First commit
446
				}
d471cf49   Elena.Budnik   format; IMPEX in ...
447
448
449
			}
			else 
			{
9b6c46d9   Elena.Budnik   THEMIS Remote center
450
				$centerNode = $paramMgr->xmlDom->importNode($base, true);
d471cf49   Elena.Budnik   format; IMPEX in ...
451
452
			}

9b6c46d9   Elena.Budnik   THEMIS Remote center
453
454
455
456
			$centerNode->setAttribute('name', $base->getAttribute('name'));
			$centerNode->setAttribute('desc', $base->getAttribute('desc'));
			$centerNode->setAttribute('available',!$notAvailable);
			$BASE->appendChild($centerNode);  
d471cf49   Elena.Budnik   format; IMPEX in ...
457
458
459
460
		}  
		$paramMgr->xmlDom->appendChild($BASE);
				
		return $paramMgr->xmlDom->save(USERWSDIR.'RemoteParams.xml');
9140bdbd   Myriam Bouchemit   for proxy amdates...
461
	     
d471cf49   Elena.Budnik   format; IMPEX in ...
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
	}

	protected function processGuestLogin() 
	{
		if (!$this->check_email_address($_POST['password'])) 
		{
			die('<a href="index.html"><h3>Invalid e-mail address. Please, try once more.</h3></a>');
		}
		
		$passwd = $_POST['password'];
		$IP = $this->getIPclient();
		$Guest = new Guest($IP,$passwd);
		// email and IP in guests.login
		$Guest->registerGuest();
		$Guest->checkGuestTimes();
		$user = $Guest->addGuest(); 

		if ($user == "allGuestLoginsInUse") 
		{
			die('<a href="index.html"><h3>Sorry, all guest accounts are currently in use. Please, try to login in 30 min.</h3></a>');  
		}
		
		$this->user = $user;
		$this->passwd = "guest";
	}    
16035364   Benjamin Renard   First commit
487
488
489
490
 
/*****************************************************************
*                           PUBLIC FUNCTIONS
*****************************************************************/
d471cf49   Elena.Budnik   format; IMPEX in ...
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// 	// migration from old to new AMDA
// 	public function convertWS() {
// 
// 		$convert = new UserWsTransfer($this->user); 
// 
// 		$res =  $convert->checkWS();
// 		if (!$res['success']) return $res;
// 
// 		$res =  $convert->transferDerived();
// 		$msg = $res['msg'];
// 
// 		$res = $convert->transferTimeTables();
// 		$msg .= $res['msg'];
// 
// 		$res = $convert->transferConditions();
// 		$msg .= $res['msg'];
// 
// 		$res = $convert->transferRequests();
// 		$msg .= $res['msg'];
// 
// 		$res =  $convert->transferMyData(); 
// 		$msg .= $res['msg'];
// 
// 		error_log('Transfer workspace from old AMDA for '.$this->user, 1, 'amda@irap.omp.eu');
// 		return array('success' => true, 'msg' => $msg);     
// 	}

	public function setPath() 
	{
175ca164   Elena.Budnik   bad commit of Use...
520
		if (isset($_GET['sessionID'])) {
d471cf49   Elena.Budnik   format; IMPEX in ...
521
522
523
524
			$this->user = $_GET['sessionID'];
			$this->userdir = USERPATH."/".$this->user."/";                
		}
		// for testing purposes
175ca164   Elena.Budnik   bad commit of Use...
525
		else if (defined('TRANSFERUSER')) {
d471cf49   Elena.Budnik   format; IMPEX in ...
526
527
			$this->userdir = BASE_PATH."test/".TRANSFERUSER."/";
		}
175ca164   Elena.Budnik   bad commit of Use...
528
		else if (isset($this->user)) {
d471cf49   Elena.Budnik   format; IMPEX in ...
529
530
			$this->userdir = USERPATH."/".$this->user."/";
		}
9140bdbd   Myriam Bouchemit   for proxy amdates...
531

d471cf49   Elena.Budnik   format; IMPEX in ...
532
		$usrdir = $this->userdir;
16035364   Benjamin Renard   First commit
533

d471cf49   Elena.Budnik   format; IMPEX in ...
534
		define ("USERDIR", "$usrdir/");
16035364   Benjamin Renard   First commit
535

175ca164   Elena.Budnik   bad commit of Use...
536
		foreach ($this->userDirs as $key => $val) {
d471cf49   Elena.Budnik   format; IMPEX in ...
537
538
539
540
			$dir = $usrdir.$val;
			define("$key","$dir/");
		} 
	}
16035364   Benjamin Renard   First commit
541

d471cf49   Elena.Budnik   format; IMPEX in ...
542
543
	public function ddCheckUser() 
	{
16035364   Benjamin Renard   First commit
544
545
		$this->IP = $this->getIPclient();

44498189   Benjamin Renard   DDClient lib is n...
546
		$cmdCheckUser =  DDBIN."DDCheckUser ".$this->IP." ".$this->user." 1> /dev/null 2> /dev/null";   
16035364   Benjamin Renard   First commit
547
548
		system($cmdCheckUser, $res);
		return $res;
d471cf49   Elena.Budnik   format; IMPEX in ...
549
	}
16035364   Benjamin Renard   First commit
550
  
d471cf49   Elena.Budnik   format; IMPEX in ...
551
552
553
	public function ddLogin() 
	{
		$this->IP = $this->getIPclient();
16035364   Benjamin Renard   First commit
554

d471cf49   Elena.Budnik   format; IMPEX in ...
555
556
		$loginCommd = DDBIN."DDHtmlLogin ".$this->user." ".$this->passwd." ".$this->IP;
		system($loginCommd, $res);
9140bdbd   Myriam Bouchemit   for proxy amdates...
557

d471cf49   Elena.Budnik   format; IMPEX in ...
558
559
		return $res;
	}
16035364   Benjamin Renard   First commit
560
561
    
          	  	
d471cf49   Elena.Budnik   format; IMPEX in ...
562
563
564
565
	public function init() 
	{ 
		if ($this->ddLogin() != 0) 
			die('<h2>You are trying to log in as '.$this->user.'<br/> Please check that you entered a valid password</h2>');
37105924   Elena.Budnik   small nettoyage
566
            
d471cf49   Elena.Budnik   format; IMPEX in ...
567
568
		$this->userdir = USERPATH."/".$this->user."/";

175ca164   Elena.Budnik   bad commit of Use...
569
570
		if (!is_dir($this->userdir)) {           
			if (!$this->createDir()) { 
d471cf49   Elena.Budnik   format; IMPEX in ...
571
572
573
574
575
				die("Login for ".$this->user." failed: Can't create WS dirs");
			}
			$this->isFirst = true;                
		}

175ca164   Elena.Budnik   bad commit of Use...
576
		if (file_exists($this->userdir.'newLogin')) {
d471cf49   Elena.Budnik   format; IMPEX in ...
577
578
579
580
581
582
583
584
585
586
			copy($this->userdir.'newLogin',$this->userdir.'lastLogin');
			$dt = filemtime($this->userdir.'newLogin');
			touch($this->userdir.'lastLogin', $dt);
		}
		else 
			touch($this->userdir.'lastLogin', time() - 5*86400); // last 5 days
		
		// if new info exists it will be shown to : 
		// public => to all
		// special => to group members
0dc31ba8   Elena.Budnik   remoteParamManage...
587
588
589
590
591
592
593
594
595
596
597
598
// 		if (file_exists($this->userdir.'newInfo')) 
// 		{                
// 			$amdaInfo = new AmdaNews($this->user);
// 			// if status = 0 - it is special info to group members only
// 			// else to all  
// 			$info = $this->getUserInfo($this->user);
// 			$status = $amdaInfo->makeInfo($info['group']); 
// 
// 			if ($status) $this->isNewInfo = true; 
// 					
// 			unlink($this->userdir.'newInfo');
// 		}
d471cf49   Elena.Budnik   format; IMPEX in ...
599
		touch($this->userdir.'newLogin');          
16035364   Benjamin Renard   First commit
600

d471cf49   Elena.Budnik   format; IMPEX in ...
601
		$this->setPath(); 		
175ca164   Elena.Budnik   bad commit of Use...
602
603
604
605
606
	
		$this->userGrps = $this->getUserGrps();
		  		
		if (!$this->makeLocalTree())
				die("Login for ".$this->user." failed: Can't make LocalParams.xml");;
55e11e13   Elena.Budnik   new global AMDAUP...
607
		
d471cf49   Elena.Budnik   format; IMPEX in ...
608
		$ok = $this->makeRemoteTree();	                      		
16035364   Benjamin Renard   First commit
609

d471cf49   Elena.Budnik   format; IMPEX in ...
610
611
		if (!file_exists(USERWSDIR.'Request.xml')) $reqMgr = new RequestMgr();
		if (!file_exists(USERWSDIR.'Tt.xml')) $ttMgr = new TimeTableMgr();
175ca164   Elena.Budnik   bad commit of Use...
612
		if (!file_exists(USERWSDIR.'Alias.xml')) $ttMgr = new AliasMgr();
16035364   Benjamin Renard   First commit
613

d471cf49   Elena.Budnik   format; IMPEX in ...
614
615
		//TODO sessionID = user + WSname
		$sessionID = $this->user;
a21497cc   Elena.Budnik   redmine #5645
616
617
618
619
620
621
		
		/*
		* Special settings are defined in the generic_data/SpecialSettings/Settings.xml
		*/
		$this->setSpecialSettings(); 
		
d471cf49   Elena.Budnik   format; IMPEX in ...
622
		/*
55e11e13   Elena.Budnik   new global AMDAUP...
623
		* Special groups are defined in the generic_data/SpecialSettings/Groups.xml
d471cf49   Elena.Budnik   format; IMPEX in ...
624
		*/
175ca164   Elena.Budnik   bad commit of Use...
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
		if ($this->userGrps) {
		//	$specialGroup = $this->isSpecialGroup();
			$specialGroup = false;
			// Special Info for special groups
			if ($specialGroup) {
				// Special Settings for special groups - first visit just copying
				if ($this->isFirst) {
					$grp = $specialGroup->getAttribute('xml:id');
					$tags = $specialGroup->getElementsByTagName('folder');

					foreach ($tags as $tag) {                 
						$folder = $tag->getAttribute('name');
						
						foreach (glob(SpecialSettingsDir.$grp."/".$folder."/*") as $file) {
							copy($file, $this->userdir.$folder."/".basename($file));
						} 
					}
					// mark to show help information
					touch($this->userdir."$grp"."Help");
d471cf49   Elena.Budnik   format; IMPEX in ...
644
				}
175ca164   Elena.Budnik   bad commit of Use...
645
646
647
648
649
650
651
				// add requests
				else { }
				$grpName = $specialGroup->getAttribute('xml:id');
				$helpName = "$grpName"."Help";
	
				if (file_exists(HELPPATH.$helpName) && file_exists($this->userdir.$helpName))
								$this->isSpecialInfo = $helpName;
d471cf49   Elena.Budnik   format; IMPEX in ...
652
			}
d471cf49   Elena.Budnik   format; IMPEX in ...
653
654
655
		}
		return $sessionID;
	}
16035364   Benjamin Renard   First commit
656

175ca164   Elena.Budnik   bad commit of Use...
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
	protected function makeLocalTree()
	{
		if (file_exists(USERWSDIR.'LocalParams.xml'))
				unlink(USERWSDIR.'LocalParams.xml');  
				
		if (!copy(DATAPATH.'/LocalData/LocalParams.xml', USERWSDIR.'LocalParams.xml'))
				die("Login for ".$this->user." failed: Can't copy LocalParams.xml"); 
				
		if ($this->userGrps)
				return $this->updateTreeForGrps(USERWSDIR.'LocalParams.xml');
				
		return true;
	}
	
	protected function updateTreeForGrps($file)
	{
		$xml = new DomDocument("1.0");
		
		if(!$xml->load($file)) 
			die("Login for ".$this->user." failed: Can't load LocalParams.xml");
		
		$xp = new domxpath($xml);                                                                             
	  
		foreach ($this->userGrps as $grp) {
			$nodes = $xp->query("//*[@group='".$grp."']");
			
			if ($nodes->length > 0)
				foreach ($nodes as $node) {
						$node->removeAttribute('group');
						if ($node->hasAttribute('restriction'))
								$node->removeAttribute('restriction');
				}
		}
		
		return $xml->save($file);
	}
	
d471cf49   Elena.Budnik   format; IMPEX in ...
694
695
696
	public function dirSize($dir) 
	{
		$handle = opendir($dir);
e57cb025   Benjamin Renard   Fix most of error...
697
698

		$mas = 0;
175ca164   Elena.Budnik   bad commit of Use...
699
700
		while ($file = readdir($handle)) {
			if ($file != '..' && $file != '.' && !is_dir($dir.'/'.$file)) {
d471cf49   Elena.Budnik   format; IMPEX in ...
701
702
				$mas += filesize($dir.'/'.$file);
			} 
175ca164   Elena.Budnik   bad commit of Use...
703
			else if (is_dir($dir.'/'.$file) && $file != '..' && $file != '.') {
d471cf49   Elena.Budnik   format; IMPEX in ...
704
705
706
707
708
				$mas += $this->dirSize($dir.'/'.$file);
			}
		}
		return $mas;
	} 
9140bdbd   Myriam Bouchemit   for proxy amdates...
709

d471cf49   Elena.Budnik   format; IMPEX in ...
710
711
712
713
714
715
	public function  getWsSize() 
	{
		$dirsToCheck = array(USERDATADIR, USERTTDIR, USERWORKINGDIR);
		$wsSize = 0;
		foreach ($dirsToCheck as $dir) 
			if (is_dir($dir)) $wsSize += $this->dirSize($dir);
9140bdbd   Myriam Bouchemit   for proxy amdates...
716

d471cf49   Elena.Budnik   format; IMPEX in ...
717
718
719
		return $wsSize;
	}
	
175ca164   Elena.Budnik   bad commit of Use...
720
	// http://www.ilovejackdaniels.com/php/email-address-validation/ 
d471cf49   Elena.Budnik   format; IMPEX in ...
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
	public function check_email_address($email) 
	{
		// First, we check that there's one @ symbol, and that the lengths are right
		if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) 
		{
		// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
			return false;
		}

		// Split it into sections to make life easier
		$email_array = explode("@", $email);
		$local_array = explode(".", $email_array[0]);
		for ($i = 0; $i < sizeof($local_array); $i++) 
		{
			if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
				return false;
			}
		}
16035364   Benjamin Renard   First commit
739

d471cf49   Elena.Budnik   format; IMPEX in ...
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
		if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
			$domain_array = explode(".", $email_array[1]);
			if (sizeof($domain_array) < 2) {
				return false; // Not enough parts to domain
			}
			for ($i = 0; $i < sizeof($domain_array); $i++) 
			{
				if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
					return false;
				}
			}
		}
		return true;
	}         
}
16035364   Benjamin Renard   First commit
755
?>