Blame view

php/classes/UserMgr.php 21.9 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
	protected $amdaClient; //client to dd webservice
82193dd4   Elena.Budnik   update RemotePara...
26
	protected $paramMgr, $baseExtXml;
d471cf49   Elena.Budnik   format; IMPEX in ...
27
28
	
	public $isFirst = false;
d95e8421   Elena.Budnik   ws transfer : ini...
29
	public $isOldWS = true;
d471cf49   Elena.Budnik   format; IMPEX in ...
30
31
	public $isNewInfo = false;
	public $isSpecialInfo = null;
b6e4778f   Elena.Budnik   if isGuest - no o...
32
	public $isGuest = false;
d471cf49   Elena.Budnik   format; IMPEX in ...
33

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

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

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

	//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...
93
94
95
			$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 ...
96
97
98
99
100
101
102
103
104
105
106
107
				$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...
108
      
d471cf49   Elena.Budnik   format; IMPEX in ...
109
110
	public function getUserInfo()
	{
175ca164   Elena.Budnik   bad commit of Use...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
	// 	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 ...
127
	}
9140bdbd   Myriam Bouchemit   for proxy amdates...
128

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

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

175ca164   Elena.Budnik   bad commit of Use...
149
150
151
152
	/*
	* 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 ...
153
154
	protected function isSpecialGroup() 
	{
d471cf49   Elena.Budnik   format; IMPEX in ...
155
		$specialGrps = new DomDocument("1.0");
16035364   Benjamin Renard   First commit
156

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

d471cf49   Elena.Budnik   format; IMPEX in ...
159
160
		$specialGrpNode = null;

175ca164   Elena.Budnik   bad commit of Use...
161
		foreach ($this->userGrps as $grp) {
d471cf49   Elena.Budnik   format; IMPEX in ...
162
163
164
165
166
167
			$specialGrpNode = $specialGrps->getElementById($grp); 
			if ($specialGrpNode) break;                            
		}
		
		return $specialGrpNode;
	}
7baca0ef   Elena.Budnik   user special sett...
168
  
175ca164   Elena.Budnik   bad commit of Use...
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
	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...
194
	public function setSpecialSettings() 
e57cb025   Benjamin Renard   Fix most of error...
195
	{	
7baca0ef   Elena.Budnik   user special sett...
196
197
		$userSettings = new DomDocument("1.0");

e57cb025   Benjamin Renard   Fix most of error...
198
		if (!file_exists(specialSettingsXml) || !$userSettings->load(specialSettingsXml)) {
7baca0ef   Elena.Budnik   user special sett...
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
			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...
215
216
217
218
219
220
221
222
223
224
225
			$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...
226
	}
d471cf49   Elena.Budnik   format; IMPEX in ...
227

82193dd4   Elena.Budnik   update RemotePara...
228
229
230
231
	/*
	*  make remote data tree from list of distant bases if it doezn't exist$center->monitor()
	*/ 
	protected function makeRemoteTree() 
d471cf49   Elena.Budnik   format; IMPEX in ...
232
	{  
82193dd4   Elena.Budnik   update RemotePara...
233
234
235
236
237
238
239
	// unlink(USERWSDIR.'RemoteParams.xml');
		$this->paramMgr = new RemoteParamManager();
		$init_res = $this->paramMgr->init();
  
		$this->baseExtXml = new DomDocument("1.0");

		if ($init_res['success']) { // USERWSDIR.'RemoteParams.xml' exists
d471cf49   Elena.Budnik   format; IMPEX in ...
240
			// check/change access rights
82193dd4   Elena.Budnik   update RemotePara...
241
			$basesWS = $this->paramMgr->xmlDom->getElementsByTagName('dataCenter');  // RemoteParams.xml
d471cf49   Elena.Budnik   format; IMPEX in ...
242
			$delete = new UserDeleteObsolete();
82193dd4   Elena.Budnik   update RemotePara...
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
			
			$basesWsId = array();
			$basesToAdd = array();
			
			if ($basesWS->length > 0) {
	
				foreach ($basesWS  as $base) {
					$basesWsId[] = $base->getAttribute('xml:id'); 
				}
				
				foreach ($this->paramMgr->Bases as $baseNew) {
					if (!in_array($baseNew, $basesWsId)) {
						$basesToAdd[] = $baseNew;
					}
				}
				
				foreach ($basesWS  as $base) 
d471cf49   Elena.Budnik   format; IMPEX in ...
260
261
				{
					$baseId = $base->getAttribute('xml:id');
82193dd4   Elena.Budnik   update RemotePara...
262
263
264
265
266
267
268
269
270
					 
					if (!$this->baseExtXml->load(RemoteData.$baseId.'/base.xml')) 
					{ 
						$base->setAttribute("desc","ATTENTION!!! This DataCenter DOES NOT EXIST ANY MORE!!! Remove it from your tree");                                                                         
						$base->setAttribute('obsolete', true);
						
						error_log('NO '.RemoteData.$baseId.'/base.xml',1,email);	
						continue;
					}
9b6c46d9   Elena.Budnik   THEMIS Remote center
271
					
82193dd4   Elena.Budnik   update RemotePara...
272
					if ($base->hasAttribute('addable')) {
7c76261d   Elena.Budnik   check if makecent...
273
					  //   keep this base tree 
82193dd4   Elena.Budnik   update RemotePara...
274
275
276
277
278
					  //	 error_log($baseId,1,email);
					}
					else {
						// totally rewrite
						if ($base->hasAttribute('isSimulation')) {
d675a6e8   Hacene SI HADJ MOHAND   Fix UserMgr
279
							$centerNode = $this->makeSimulationBase($baseId);
82193dd4   Elena.Budnik   update RemotePara...
280
							$center = new $baseId();
d675a6e8   Hacene SI HADJ MOHAND   Fix UserMgr
281
							$centerNode->setAttribute('available', $center->monitor());
d471cf49   Elena.Budnik   format; IMPEX in ...
282
						}
82193dd4   Elena.Budnik   update RemotePara...
283
284
						else {
							$center = new $baseId();
7c76261d   Elena.Budnik   check if makecent...
285
286
287
288
289
290
291
							if (method_exists($center, 'makeCenterNode'))
								$centerNode = $center->makeCenterNode($this->paramMgr->xmlDom);
							else { 
								// keep this base tree 
								error_log("Attention : $baseId has no makeCenterNode method", 1, email);
								continue;
							}
82193dd4   Elena.Budnik   update RemotePara...
292
							$centerNode->setAttribute('available', TRUE);
d471cf49   Elena.Budnik   format; IMPEX in ...
293
						}
82193dd4   Elena.Budnik   update RemotePara...
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
						
						$centerNode->setAttribute('name', $base->getAttribute('name'));
						$centerNode->setAttribute('desc', $base->getAttribute('desc'));
						
						$base->parentNode->removeChild($base);
						
						$this->paramMgr->xmlDom->documentElement->appendChild($centerNode);
					}

					if ($baseId !== "FMI_GUMICS") {
						// Update Info on External Data Sets in RemoteTree.xml  
						$dataSets = $base->getElementsByTagName("dataset");
						if ($dataSets->length > 0) {
							foreach ($dataSets as $dataSet) {
								$dataSetID = $dataSet->getAttribute("xml:id");
								$origDataSet = $this->baseExtXml->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();                                                                                                              
									$dataSet->setAttribute("desc","ATTENTION!!! This data set DOES NOT EXIST ANY MORE !!! Remove it from your tree");                    
									$dataSet->setAttribute('obsolete', true);
								}
							}
						}                
9b6c46d9   Elena.Budnik   THEMIS Remote center
325
326
					}
				}
82193dd4   Elena.Budnik   update RemotePara...
327
328
329
330
				
				// New Bases to add
				if (count($basesToAdd) > 0) {
					foreach ($basesToAdd as $baseToAdd) { 
c198a068   Elena.Budnik   check if remote b...
331
332
333
						$centerNode = $this->makeNewBase($baseToAdd);  
                        if (isset($centerNode))
                            $this->paramMgr->xmlDom->documentElement->appendChild($centerNode);
82193dd4   Elena.Budnik   update RemotePara...
334
					}
16035364   Benjamin Renard   First commit
335
				}
82193dd4   Elena.Budnik   update RemotePara...
336
337
338
339
340
341
342
343
				
				return $this->paramMgr->xmlDom->save($this->paramMgr->xmlName); 
			}
		}
      else if ($init_res['err'] == "RemoteParams file error") { // new RemoteParams.xml
      
			$this->paramMgr->xmlDom->formatOutput = true;
			$this->paramMgr->xmlDom->preserveWhiteSpace = false;
d675a6e8   Hacene SI HADJ MOHAND   Fix UserMgr
344

82193dd4   Elena.Budnik   update RemotePara...
345
346
347
348
349
350
			$BASE = $this->paramMgr->xmlDom->createElement('dataRoot'); 
			$BASE->setAttribute('xml:id','myRemoteData-treeRootNode');

			foreach ($this->paramMgr->Bases as $baseId) 
			{    
				$centerNode = $this->makeNewBase($baseId);
d675a6e8   Hacene SI HADJ MOHAND   Fix UserMgr
351
352
				if (isset($centerNode))
					$BASE->appendChild($centerNode);  
82193dd4   Elena.Budnik   update RemotePara...
353
			} 
d675a6e8   Hacene SI HADJ MOHAND   Fix UserMgr
354

82193dd4   Elena.Budnik   update RemotePara...
355
			$this->paramMgr->xmlDom->appendChild($BASE);
d675a6e8   Hacene SI HADJ MOHAND   Fix UserMgr
356

82193dd4   Elena.Budnik   update RemotePara...
357
358
359
360
361
362
363
			return $this->paramMgr->xmlDom->save($this->paramMgr->xmlName);
	   }  
	}
	
	protected function makeNewBase($baseId)
	{
	// no data base description ; skip this data base  
d675a6e8   Hacene SI HADJ MOHAND   Fix UserMgr
364
		if (!@file_exists(RemoteData.$baseId.'/base.xml')) return NULL;
82193dd4   Elena.Budnik   update RemotePara...
365
366

		// can't read base.xml ; skip this data base
d675a6e8   Hacene SI HADJ MOHAND   Fix UserMgr
367
368
		if (!@$this->baseExtXml->load(RemoteData.$baseId.'/base.xml')) return NULL;

82193dd4   Elena.Budnik   update RemotePara...
369
370
371
372
373
374
375
376
		$base = $this->paramMgr->basesDom->getElementById($baseId);
		
		if ($base->hasAttribute('default'))  
		{ 
			if ($base->hasAttribute('isSimulation')) {
					$centerNode = $this->makeSimulationBase($baseId);
					$center = new $baseId();
					$centerNode->setAttribute('available',$center->monitor());
d471cf49   Elena.Budnik   format; IMPEX in ...
377
			}
82193dd4   Elena.Budnik   update RemotePara...
378
379
380
381
			/*
			*   Some small & well known data centers are included by default: 
			*   Each VI structure and VI (dataset) description at DDBASE for them should exist
			*/
d471cf49   Elena.Budnik   format; IMPEX in ...
382
383
			else 
			{
82193dd4   Elena.Budnik   update RemotePara...
384
385
386
				$center = new $baseId();				
				$centerNode = $center->makeCenterNode($this->paramMgr->xmlDom);
				$centerNode->setAttribute('available', TRUE);
d471cf49   Elena.Budnik   format; IMPEX in ...
387
			}
82193dd4   Elena.Budnik   update RemotePara...
388
			
9b6c46d9   Elena.Budnik   THEMIS Remote center
389
390
			$centerNode->setAttribute('name', $base->getAttribute('name'));
			$centerNode->setAttribute('desc', $base->getAttribute('desc'));
82193dd4   Elena.Budnik   update RemotePara...
391
392
393
394
395
396
397
398
399
400
401
402
			
			if ($base->hasAttribute('addable')) {
				$centerNode->setAttribute('addable', TRUE);
			}
		}
		else 
		{
			$centerNode = $this->paramMgr->xmlDom->importNode($base, true);
			$centerNode->setAttribute('available', TRUE);
		}

      return $centerNode;
d471cf49   Elena.Budnik   format; IMPEX in ...
403
	}
82193dd4   Elena.Budnik   update RemotePara...
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
	
	protected function makeSimulationBase($baseId)
	{
		$centerNode = $this->paramMgr->xmlDom->importNode($this->baseExtXml->getElementById($baseId), true);
		$datasets = $centerNode->getElementsByTagName('dataset');
		foreach ($datasets as $dataset)
		{
			$infoFileName = $this->paramMgr->getInfoName($dataset->getAttribute('name'));
			$this->paramMgr->localInfo = RemoteData.$baseId.'/'.$infoFileName;

			if (!@file_exists($this->paramMgr->localInfo)) continue;				 

			$params = $dataset->getElementsByTagName('parameter');
			$this->paramMgr->remoteViId = $dataset->getAttribute('name');
			foreach ($params as $param) 
			{
				$this->paramMgr->paramId = $param->getAttribute('name');
				$paramGlobalId =  $param->getAttribute('xml:id');				  
				$this->paramMgr->paramXML = RemoteData.'PARAMS/'.$paramGlobalId.'.xml';	
							
				if (!@file_exists($this->paramMgr->paramXML)) continue;
				if (!$this->paramMgr->paramDom) 
						$this->paramMgr->paramDom = new DomDocument("1.0");
d471cf49   Elena.Budnik   format; IMPEX in ...
427

82193dd4   Elena.Budnik   update RemotePara...
428
429
430
431
432
433
434
435
436
				$this->paramMgr->paramDom->load($this->paramMgr->paramXML);
				
				if (!$this->paramMgr->makeComponents($param)) continue;
			}
		}
		return $centerNode;
	}
	
	
d471cf49   Elena.Budnik   format; IMPEX in ...
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
	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
460
461
462
463
 
/*****************************************************************
*                           PUBLIC FUNCTIONS
*****************************************************************/
d95e8421   Elena.Budnik   ws transfer : ini...
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
	// migration from old to new AMDA
	public function convertWS() 
	{
		$convert = new UserWsTransfer($this->user); 

		$res = $convert->checkWS();
		if (!$res['success']) return $res;
		
		$res = $convert->transferMyData(); 
		$msg = $res['msg'];
	 
		$res = $convert->transferDerived();
		$msg .= $res['msg'];

		$res = $convert->transferTimeTables();
		$msg .= $res['msg'];

		$res = $convert->transferConditions();
		$msg .= $res['msg'];

d471cf49   Elena.Budnik   format; IMPEX in ...
484
485
// 		$res = $convert->transferRequests();
// 		$msg .= $res['msg'];
d95e8421   Elena.Budnik   ws transfer : ini...
486
487
488
 
		$res = $convert->transferFilters();
		$msg .= $res['msg'];
bcb4c408   Elena.Budnik   send mail for cop...
489
490
491
492
493
		
		$info = $this->amdaClient->getUserInfo($this->user);
		$email = $info["email"];
		
      $convert->sendMail($email,$msg);   
d95e8421   Elena.Budnik   ws transfer : ini...
494
495
496
	//	error_log('Transfer workspace from old AMDA for '.$this->user, 1, 'amda@irap.omp.eu');
		return array('success' => true, 'msg' => $msg);     
	}
d471cf49   Elena.Budnik   format; IMPEX in ...
497
498
499

	public function setPath() 
	{
175ca164   Elena.Budnik   bad commit of Use...
500
		if (isset($_GET['sessionID'])) {
d471cf49   Elena.Budnik   format; IMPEX in ...
501
502
503
			$this->user = $_GET['sessionID'];
			$this->userdir = USERPATH."/".$this->user."/";                
		}
175ca164   Elena.Budnik   bad commit of Use...
504
		else if (isset($this->user)) {
d471cf49   Elena.Budnik   format; IMPEX in ...
505
506
			$this->userdir = USERPATH."/".$this->user."/";
		}
9140bdbd   Myriam Bouchemit   for proxy amdates...
507

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

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

175ca164   Elena.Budnik   bad commit of Use...
512
		foreach ($this->userDirs as $key => $val) {
d471cf49   Elena.Budnik   format; IMPEX in ...
513
514
515
516
			$dir = $usrdir.$val;
			define("$key","$dir/");
		} 
	}
16035364   Benjamin Renard   First commit
517

d471cf49   Elena.Budnik   format; IMPEX in ...
518
519
	public function ddCheckUser() 
	{
16035364   Benjamin Renard   First commit
520
		$this->IP = $this->getIPclient();
a22ecd3b   Benjamin Renard   Keep only necessa...
521
		$cmdCheckUser =  "DDCheckUser ".$this->IP." ".$this->user." 1> /dev/null 2> /dev/null";   
16035364   Benjamin Renard   First commit
522
523
		system($cmdCheckUser, $res);
		return $res;
d471cf49   Elena.Budnik   format; IMPEX in ...
524
	}
16035364   Benjamin Renard   First commit
525
  
d471cf49   Elena.Budnik   format; IMPEX in ...
526
527
528
	public function ddLogin() 
	{
		$this->IP = $this->getIPclient();
16035364   Benjamin Renard   First commit
529

a22ecd3b   Benjamin Renard   Keep only necessa...
530
		$loginCommd = "DDHtmlLogin ".$this->user." ".$this->passwd." ".$this->IP;
d471cf49   Elena.Budnik   format; IMPEX in ...
531
		system($loginCommd, $res);
9140bdbd   Myriam Bouchemit   for proxy amdates...
532

d471cf49   Elena.Budnik   format; IMPEX in ...
533
534
		return $res;
	}
16035364   Benjamin Renard   First commit
535
536
    
          	  	
d471cf49   Elena.Budnik   format; IMPEX in ...
537
	public function init() 
d675a6e8   Hacene SI HADJ MOHAND   Fix UserMgr
538
	{ 
1f3ee1cd   Benjamin Renard   Improve login pag...
539
540
541
		if ($this->ddLogin() != 0) {
			return FALSE;
		} 
37105924   Elena.Budnik   small nettoyage
542
            
d471cf49   Elena.Budnik   format; IMPEX in ...
543
544
		$this->userdir = USERPATH."/".$this->user."/";

175ca164   Elena.Budnik   bad commit of Use...
545
546
		if (!is_dir($this->userdir)) {           
			if (!$this->createDir()) { 
d471cf49   Elena.Budnik   format; IMPEX in ...
547
548
				die("Login for ".$this->user." failed: Can't create WS dirs");
			}
b6e4778f   Elena.Budnik   if isGuest - no o...
549
550
551
			$this->isFirst = true;
			
			if ($this->isGuest) {
d95e8421   Elena.Budnik   ws transfer : ini...
552
				$this->isOldWS = false;
b6e4778f   Elena.Budnik   if isGuest - no o...
553
554
555
556
557
558
559
			}
			else {
				$convert = new UserWsTransfer($this->user); 
				$res = $convert->checkWS();
				if (!$res['success']) 
					$this->isOldWS = false;
			}
d471cf49   Elena.Budnik   format; IMPEX in ...
560
561
		}

175ca164   Elena.Budnik   bad commit of Use...
562
		if (file_exists($this->userdir.'newLogin')) {
ab5aab56   Elena.Budnik   delete not needed...
563
			touch($this->userdir.'lastLogin', filemtime($this->userdir.'newLogin'));
d471cf49   Elena.Budnik   format; IMPEX in ...
564
		}
ab5aab56   Elena.Budnik   delete not needed...
565
		else {
d471cf49   Elena.Budnik   format; IMPEX in ...
566
			touch($this->userdir.'lastLogin', time() - 5*86400); // last 5 days
ab5aab56   Elena.Budnik   delete not needed...
567
		}
d471cf49   Elena.Budnik   format; IMPEX in ...
568
569
570
571
		
		// if new info exists it will be shown to : 
		// public => to all
		// special => to group members
b6e4778f   Elena.Budnik   if isGuest - no o...
572
		if (file_exists($this->userdir.'newInfo')) {                
0323cf9d   Elena.Budnik   new info
573
			$amdaInfo = new AmdaNews($this->user);
0323cf9d   Elena.Budnik   new info
574
575
576
577
			$info = $this->getUserInfo($this->user);
			$status = $amdaInfo->makeInfo($info['group']); 

			if ($status) $this->isNewInfo = true; 
0323cf9d   Elena.Budnik   new info
578
579
			unlink($this->userdir.'newInfo');
		}
d471cf49   Elena.Budnik   format; IMPEX in ...
580
		touch($this->userdir.'newLogin');          
16035364   Benjamin Renard   First commit
581

d471cf49   Elena.Budnik   format; IMPEX in ...
582
		$this->setPath(); 		
175ca164   Elena.Budnik   bad commit of Use...
583
584
	
		$this->userGrps = $this->getUserGrps();
d675a6e8   Hacene SI HADJ MOHAND   Fix UserMgr
585
		  		
175ca164   Elena.Budnik   bad commit of Use...
586
587
		if (!$this->makeLocalTree())
				die("Login for ".$this->user." failed: Can't make LocalParams.xml");;
d675a6e8   Hacene SI HADJ MOHAND   Fix UserMgr
588
589
		
		$ok = $this->makeRemoteTree();	                      		
16035364   Benjamin Renard   First commit
590

638db58d   Benjamin Renard   RequestMgr constr...
591
		/*if (!file_exists(USERWSDIR.'Request.xml')) $reqMgr = new RequestMgr();
d471cf49   Elena.Budnik   format; IMPEX in ...
592
		if (!file_exists(USERWSDIR.'Tt.xml')) $ttMgr = new TimeTableMgr();
638db58d   Benjamin Renard   RequestMgr constr...
593
		if (!file_exists(USERWSDIR.'Alias.xml')) $ttMgr = new AliasMgr();*/
16035364   Benjamin Renard   First commit
594

d471cf49   Elena.Budnik   format; IMPEX in ...
595
596
		//TODO sessionID = user + WSname
		$sessionID = $this->user;
a21497cc   Elena.Budnik   redmine #5645
597
598
599
600
601
602
		
		/*
		* Special settings are defined in the generic_data/SpecialSettings/Settings.xml
		*/
		$this->setSpecialSettings(); 
		
d471cf49   Elena.Budnik   format; IMPEX in ...
603
		/*
55e11e13   Elena.Budnik   new global AMDAUP...
604
		* Special groups are defined in the generic_data/SpecialSettings/Groups.xml
d471cf49   Elena.Budnik   format; IMPEX in ...
605
		*/
175ca164   Elena.Budnik   bad commit of Use...
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
		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 ...
625
				}
175ca164   Elena.Budnik   bad commit of Use...
626
627
628
629
630
631
632
				// 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 ...
633
			}
d471cf49   Elena.Budnik   format; IMPEX in ...
634
635
636
		}
		return $sessionID;
	}
16035364   Benjamin Renard   First commit
637

175ca164   Elena.Budnik   bad commit of Use...
638
639
640
641
	protected function makeLocalTree()
	{
		if (file_exists(USERWSDIR.'LocalParams.xml'))
				unlink(USERWSDIR.'LocalParams.xml');  
d95e8421   Elena.Budnik   ws transfer : ini...
642

99ae8744   Benjamin Renard   Use config variab...
643
		if (!copy(LocalData.'/LocalParams.xml', USERWSDIR.'LocalParams.xml'))
175ca164   Elena.Budnik   bad commit of Use...
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
				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');
f29c5e66   Elena.Budnik   plot only option
667
668
						if ($node->hasAttribute('restriction')) {
							if ($node->getAttribute('restriction') != "plotOnly") {
175ca164   Elena.Budnik   bad commit of Use...
669
								$node->removeAttribute('restriction');
f29c5e66   Elena.Budnik   plot only option
670
671
							}
						}
175ca164   Elena.Budnik   bad commit of Use...
672
673
674
675
676
677
				}
		}
		
		return $xml->save($file);
	}
	
2c7f2cb6   Benjamin Renard   Remove old log fi...
678
	public function dirSize($dir, $files_to_delete = array()) 
d471cf49   Elena.Budnik   format; IMPEX in ...
679
680
	{
		$handle = opendir($dir);
e57cb025   Benjamin Renard   Fix most of error...
681
682

		$mas = 0;
175ca164   Elena.Budnik   bad commit of Use...
683
684
		while ($file = readdir($handle)) {
			if ($file != '..' && $file != '.' && !is_dir($dir.'/'.$file)) {
2c7f2cb6   Benjamin Renard   Remove old log fi...
685
686
687
688
				if (in_array($file, $files_to_delete)) {
					unlink($dir.'/'.$file);
					continue;
				}
d471cf49   Elena.Budnik   format; IMPEX in ...
689
690
				$mas += filesize($dir.'/'.$file);
			} 
175ca164   Elena.Budnik   bad commit of Use...
691
			else if (is_dir($dir.'/'.$file) && $file != '..' && $file != '.') {
2c7f2cb6   Benjamin Renard   Remove old log fi...
692
				$mas += $this->dirSize($dir.'/'.$file, $files_to_delete);
d471cf49   Elena.Budnik   format; IMPEX in ...
693
694
695
696
			}
		}
		return $mas;
	} 
9140bdbd   Myriam Bouchemit   for proxy amdates...
697

2c7f2cb6   Benjamin Renard   Remove old log fi...
698
	public function  getWsSize($delete_log_files = FALSE) 
d471cf49   Elena.Budnik   format; IMPEX in ...
699
700
701
	{
		$dirsToCheck = array(USERDATADIR, USERTTDIR, USERWORKINGDIR);
		$wsSize = 0;
2c7f2cb6   Benjamin Renard   Remove old log fi...
702
703
704
705
706
707
708
709
710
		if ($delete_log_files) {
			$files_to_delete = array(
				'example.log',
				'cmd_output',
			);
		}
		else {
			$files_to_delete = array();
		}
d471cf49   Elena.Budnik   format; IMPEX in ...
711
		foreach ($dirsToCheck as $dir) 
2c7f2cb6   Benjamin Renard   Remove old log fi...
712
			if (is_dir($dir)) $wsSize += $this->dirSize($dir, $files_to_delete);
9140bdbd   Myriam Bouchemit   for proxy amdates...
713

d471cf49   Elena.Budnik   format; IMPEX in ...
714
715
716
		return $wsSize;
	}
	
c5ef1a5d   Elena.Budnik   ereg => preg_match
717
    // http://www.ilovejackdaniels.com/php/email-address-validation/ 
d471cf49   Elena.Budnik   format; IMPEX in ...
718
719
720
	public function check_email_address($email) 
	{
		// First, we check that there's one @ symbol, and that the lengths are right
c5ef1a5d   Elena.Budnik   ereg => preg_match
721
		 if (!preg_match("/^[^@]{1,64}@[^@]{1,255}$/", $email)) 
d471cf49   Elena.Budnik   format; IMPEX in ...
722
723
724
725
		{
		// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
			return false;
		}
c5ef1a5d   Elena.Budnik   ereg => preg_match
726
		
d471cf49   Elena.Budnik   format; IMPEX in ...
727
728
729
730
731
		// 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++) 
		{
c5ef1a5d   Elena.Budnik   ereg => preg_match
732
			 if (!preg_match("@^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$@", $local_array[$i])) {
d471cf49   Elena.Budnik   format; IMPEX in ...
733
734
735
				return false;
			}
		}
16035364   Benjamin Renard   First commit
736

c5ef1a5d   Elena.Budnik   ereg => preg_match
737
		if (!preg_match("/^\[?[0-9\.]+\]?$/", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
d471cf49   Elena.Budnik   format; IMPEX in ...
738
739
740
741
742
743
			$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++) 
			{
c5ef1a5d   Elena.Budnik   ereg => preg_match
744
				if (!preg_match("/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/", $domain_array[$i])) {
d471cf49   Elena.Budnik   format; IMPEX in ...
745
746
747
748
749
750
751
					return false;
				}
			}
		}
		return true;
	}         
}
16035364   Benjamin Renard   First commit
752
?>