Blame view

php/classes/UserMgr.php 26 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;
aa2786b4   Benjamin Renard   Opimization aroun...
25
	protected $userGrpsCheckSum; //use to know if there is a modification in user groups
ca35ee3d   Benjamin Renard   Init AmdaClient o...
26
	protected $amdaClient = NULL; //client to dd webservice
82193dd4   Elena.Budnik   update RemotePara...
27
	protected $paramMgr, $baseExtXml;
3afe4f59   Benjamin Renard   Add datasets time...
28
29
	protected $datasetsTimeRestriction;
	protected $datasetsTimeRestrictionCheckSum;
d471cf49   Elena.Budnik   format; IMPEX in ...
30
31
	
	public $isFirst = false;
c3976709   Elena.Budnik   rm 7589 + clean u...
32
	public $isOldWS = false;
d471cf49   Elena.Budnik   format; IMPEX in ...
33
34
	public $isNewInfo = false;
	public $isSpecialInfo = null;
b6e4778f   Elena.Budnik   if isGuest - no o...
35
	public $isGuest = false;
d471cf49   Elena.Budnik   format; IMPEX in ...
36

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

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

d471cf49   Elena.Budnik   format; IMPEX in ...
74
75
76
77
		if (isset($_GET['sessionID'])) 
			$this->user = $_GET['sessionID'];
		else if (isset($sessionID))
			$this->user = $sessionID;
d471cf49   Elena.Budnik   format; IMPEX in ...
78
79
	}

ca35ee3d   Benjamin Renard   Init AmdaClient o...
80
81
82
83
84
85
86
    private function getAmdaClient() {
        if (!isset($this->amdaClient)) {
            $this->amdaClient = new AmdaClient();
        }
        return $this->amdaClient;
    }

d471cf49   Elena.Budnik   format; IMPEX in ...
87
88
89
90
91
92
93
94
95
96
97
98
99
100
	//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...
101
102
103
			$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 ...
104
105
106
107
108
109
110
111
112
113
114
115
				$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...
116
      
d471cf49   Elena.Budnik   format; IMPEX in ...
117
118
	public function getUserInfo()
	{
175ca164   Elena.Budnik   bad commit of Use...
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");
ca35ee3d   Benjamin Renard   Init AmdaClient o...
127
		$info =  $this->getAmdaClient()->getUserInfo($this->user);
175ca164   Elena.Budnik   bad commit of Use...
128
129
130
131
132
133
134
		$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 ...
135
	}
9140bdbd   Myriam Bouchemit   for proxy amdates...
136

0f9ec8f6   Benjamin Renard   Add reset passwor...
137
138
    public function resetPwd($crt_pwd, $new_pwd)
    {
ca35ee3d   Benjamin Renard   Init AmdaClient o...
139
        return $this->getAmdaClient()->resetUserPwd($this->user, $crt_pwd, $new_pwd);
0f9ec8f6   Benjamin Renard   Add reset passwor...
140
141
    }

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

175ca164   Elena.Budnik   bad commit of Use...
146
		foreach ($this->userDirs as $key => $val) {
d471cf49   Elena.Budnik   format; IMPEX in ...
147
148
149
150
			if (!mkdir($this->userdir.$val.'/', 0755, true)) return false;
		}
		return true;
	}
175ca164   Elena.Budnik   bad commit of Use...
151
	
aa2786b4   Benjamin Renard   Opimization aroun...
152
	protected function loadUserGrps() 
175ca164   Elena.Budnik   bad commit of Use...
153
	{
aa2786b4   Benjamin Renard   Opimization aroun...
154
155
156
		if (isset($this->userGrps)) {
			return;
		}
ca35ee3d   Benjamin Renard   Init AmdaClient o...
157
		$info = $this->getAmdaClient()->getUserInfo($this->user); 
aa2786b4   Benjamin Renard   Opimization aroun...
158
159
160
161
162
163

		$this->userGrpsCheckSum = '';
		if (!empty($info['group'])) {
			$this->userGrpsCheckSum = md5($info['group']);
			$this->userGrps = explode(',',$info['group']);
		}
175ca164   Elena.Budnik   bad commit of Use...
164
	}
16035364   Benjamin Renard   First commit
165

3afe4f59   Benjamin Renard   Add datasets time...
166
167
168
169
170
171
172
173
	protected function loadDatasetsTimeRestriction()
	{
		if (isset($this->datasetsTimeRestriction)) {
			return;
		}
		$restrictionFilePath = datasetsTimeRestrictionJson;
		if (file_exists($restrictionFilePath)) {
			// Re-use the cache file if it's not older than 3600s.
e2a002b9   Benjamin Renard   Save parsed timeR...
174
			if (abs(time() - filemtime($restrictionFilePath)) < 3600) {
3afe4f59   Benjamin Renard   Add datasets time...
175
176
177
178
179
180
181
182
183
184
185
186
				$fileContent = file_get_contents($restrictionFilePath);
				if (!empty($fileContent)) {
					$this->datasetsTimeRestriction = json_decode($fileContent, TRUE);
					if (isset($this->datasetsTimeRestriction) && ($this->datasetsTimeRestriction !== FALSE)) {
						$this->datasetsTimeRestrictionCheckSum = md5($fileContent);
						return;
					}
				}
			}
		}

		$this->datasetsTimeRestrictionCheckSum = '';
ca35ee3d   Benjamin Renard   Init AmdaClient o...
187
		$this->datasetsTimeRestriction = $this->getAmdaClient()->getDatasetsWithTimeRestriction();
3afe4f59   Benjamin Renard   Add datasets time...
188
189

		if ($this->datasetsTimeRestriction !== FALSE) {
e2a002b9   Benjamin Renard   Save parsed timeR...
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
			foreach ($this->datasetsTimeRestriction as &$datasetTimeRestriction) {
				$timeRestriction = $datasetTimeRestriction['restriction'];
				//Parse time restriction
				$n = sscanf($timeRestriction, "%04d-%02d-%02d", $yy, $mm, $dd);
				if ($n != 3) {
					$n = sscanf($timeRestriction, "%d", $ndays);
					if ($n != 1) {
						//Cannot parse the time restriction
						$timeRestriction = '';
					}
					else {
						$timeRestriction = date("Y-m-d", strtotime("-$ndays days"))."T23:59:59.999Z";
					}
				}
				else {
					$timeRestriction .= "T23:59:59.999Z";
				}
				$datasetTimeRestriction['restriction'] = $timeRestriction;
			}
3afe4f59   Benjamin Renard   Add datasets time...
209
210
211
212
213
214
			$content = json_encode($this->datasetsTimeRestriction);
			$this->datasetsTimeRestrictionCheckSum = md5($content);
			file_put_contents($restrictionFilePath, $content);
		}
	}

175ca164   Elena.Budnik   bad commit of Use...
215
216
217
218
	/*
	* 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 ...
219
220
	protected function isSpecialGroup() 
	{
aa2786b4   Benjamin Renard   Opimization aroun...
221
222
223
		if (!isset($this->userGrps))
			return NULL;

d471cf49   Elena.Budnik   format; IMPEX in ...
224
		$specialGrps = new DomDocument("1.0");
16035364   Benjamin Renard   First commit
225

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

d471cf49   Elena.Budnik   format; IMPEX in ...
228
229
		$specialGrpNode = null;

175ca164   Elena.Budnik   bad commit of Use...
230
		foreach ($this->userGrps as $grp) {
d471cf49   Elena.Budnik   format; IMPEX in ...
231
232
233
234
235
236
			$specialGrpNode = $specialGrps->getElementById($grp); 
			if ($specialGrpNode) break;                            
		}
		
		return $specialGrpNode;
	}
7baca0ef   Elena.Budnik   user special sett...
237
  
175ca164   Elena.Budnik   bad commit of Use...
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
	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...
263
	public function setSpecialSettings() 
e57cb025   Benjamin Renard   Fix most of error...
264
	{	
7baca0ef   Elena.Budnik   user special sett...
265
266
		$userSettings = new DomDocument("1.0");

e57cb025   Benjamin Renard   Fix most of error...
267
		if (!file_exists(specialSettingsXml) || !$userSettings->load(specialSettingsXml)) {
7baca0ef   Elena.Budnik   user special sett...
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
			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...
284
285
286
287
288
289
290
291
292
293
294
			$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...
295
	}
d471cf49   Elena.Budnik   format; IMPEX in ...
296

82193dd4   Elena.Budnik   update RemotePara...
297
298
299
300
	/*
	*  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 ...
301
	{  
82193dd4   Elena.Budnik   update RemotePara...
302
303
304
305
306
	// unlink(USERWSDIR.'RemoteParams.xml');
		$this->paramMgr = new RemoteParamManager();
		$init_res = $this->paramMgr->init();
  
		$this->baseExtXml = new DomDocument("1.0");
01bb61f8   Benjamin Renard   Remove guest id w...
307
	
82193dd4   Elena.Budnik   update RemotePara...
308
		if ($init_res['success']) { // USERWSDIR.'RemoteParams.xml' exists
d471cf49   Elena.Budnik   format; IMPEX in ...
309
			// check/change access rights
b3ed6bd4   Benjamin Renard   Optimize remote t...
310
311
312
313

            $xpath = new DOMXpath($this->paramMgr->xmlDom);

			$basesWS = $xpath->query('//dataCenter');  // RemoteParams.xml
d471cf49   Elena.Budnik   format; IMPEX in ...
314
			$delete = new UserDeleteObsolete();
82193dd4   Elena.Budnik   update RemotePara...
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
			
			$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 ...
332
333
				{
					$baseId = $base->getAttribute('xml:id');
aa2786b4   Benjamin Renard   Opimization aroun...
334
335
336
337
338
339
340
					$lastModif = $base->getAttribute('lastModif');
					if (!empty($lastModif)) {
						$lastModif = intval($lastModif);
						if (($lastModif > 0) && ((time() - $lastModif) < REMOTE_PARAMS_UPDATE_DELAY)) {
							continue;
						} 
					}
82193dd4   Elena.Budnik   update RemotePara...
341
342
343
344
345
346
347
348
					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
349
					
82193dd4   Elena.Budnik   update RemotePara...
350
					if ($base->hasAttribute('addable')) {
aa2786b4   Benjamin Renard   Opimization aroun...
351
						$base->setAttribute('lastModif', time());
7c76261d   Elena.Budnik   check if makecent...
352
					  //   keep this base tree 
82193dd4   Elena.Budnik   update RemotePara...
353
354
355
356
357
					  //	 error_log($baseId,1,email);
					}
					else {
						// totally rewrite
						if ($base->hasAttribute('isSimulation')) {
d675a6e8   Hacene SI HADJ MOHAND   Fix UserMgr
358
							$centerNode = $this->makeSimulationBase($baseId);
82193dd4   Elena.Budnik   update RemotePara...
359
							$center = new $baseId();
d675a6e8   Hacene SI HADJ MOHAND   Fix UserMgr
360
							$centerNode->setAttribute('available', $center->monitor());
d471cf49   Elena.Budnik   format; IMPEX in ...
361
						}
82193dd4   Elena.Budnik   update RemotePara...
362
363
						else {
							$center = new $baseId();
7c76261d   Elena.Budnik   check if makecent...
364
365
366
367
368
369
370
							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...
371
							$centerNode->setAttribute('available', TRUE);
d471cf49   Elena.Budnik   format; IMPEX in ...
372
						}
82193dd4   Elena.Budnik   update RemotePara...
373
374
375
						
						$centerNode->setAttribute('name', $base->getAttribute('name'));
						$centerNode->setAttribute('desc', $base->getAttribute('desc'));
aa2786b4   Benjamin Renard   Opimization aroun...
376
377
						$centerNode->setAttribute('lastModif', time());
						
82193dd4   Elena.Budnik   update RemotePara...
378
379
380
381
382
383
384
						
						$base->parentNode->removeChild($base);
						
						$this->paramMgr->xmlDom->documentElement->appendChild($centerNode);
					}

					if ($baseId !== "FMI_GUMICS") {
b3ed6bd4   Benjamin Renard   Optimize remote t...
385
386
387
388
						// Update Info on External Data Sets in RemoteTree.xml
                        $dataSets = $xpath->query('.//dataset', $base);
                        if ($dataSets->length > 0) {
                            foreach ($dataSets as $dataSet) {
82193dd4   Elena.Budnik   update RemotePara...
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
								$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
406
407
					}
				}
82193dd4   Elena.Budnik   update RemotePara...
408
409
410
411
				
				// New Bases to add
				if (count($basesToAdd) > 0) {
					foreach ($basesToAdd as $baseToAdd) { 
c198a068   Elena.Budnik   check if remote b...
412
413
414
						$centerNode = $this->makeNewBase($baseToAdd);  
                        if (isset($centerNode))
                            $this->paramMgr->xmlDom->documentElement->appendChild($centerNode);
82193dd4   Elena.Budnik   update RemotePara...
415
					}
16035364   Benjamin Renard   First commit
416
				}
82193dd4   Elena.Budnik   update RemotePara...
417
418
419
420
421
422
423
424
				
				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
425

82193dd4   Elena.Budnik   update RemotePara...
426
427
428
429
430
431
			$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
432
433
				if (isset($centerNode))
					$BASE->appendChild($centerNode);  
82193dd4   Elena.Budnik   update RemotePara...
434
			} 
d675a6e8   Hacene SI HADJ MOHAND   Fix UserMgr
435

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

82193dd4   Elena.Budnik   update RemotePara...
438
			return $this->paramMgr->xmlDom->save($this->paramMgr->xmlName);
aa2786b4   Benjamin Renard   Opimization aroun...
439
	   }
82193dd4   Elena.Budnik   update RemotePara...
440
441
442
443
444
	}
	
	protected function makeNewBase($baseId)
	{
	// no data base description ; skip this data base  
d675a6e8   Hacene SI HADJ MOHAND   Fix UserMgr
445
		if (!@file_exists(RemoteData.$baseId.'/base.xml')) return NULL;
82193dd4   Elena.Budnik   update RemotePara...
446
447

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

82193dd4   Elena.Budnik   update RemotePara...
450
451
452
453
454
455
456
457
		$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 ...
458
			}
82193dd4   Elena.Budnik   update RemotePara...
459
460
461
462
			/*
			*   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 ...
463
464
			else 
			{
82193dd4   Elena.Budnik   update RemotePara...
465
466
467
				$center = new $baseId();				
				$centerNode = $center->makeCenterNode($this->paramMgr->xmlDom);
				$centerNode->setAttribute('available', TRUE);
d471cf49   Elena.Budnik   format; IMPEX in ...
468
			}
82193dd4   Elena.Budnik   update RemotePara...
469
			
9b6c46d9   Elena.Budnik   THEMIS Remote center
470
471
			$centerNode->setAttribute('name', $base->getAttribute('name'));
			$centerNode->setAttribute('desc', $base->getAttribute('desc'));
82193dd4   Elena.Budnik   update RemotePara...
472
473
474
475
476
477
478
479
480
481
482
483
			
			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 ...
484
	}
82193dd4   Elena.Budnik   update RemotePara...
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
	
	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 ...
508

82193dd4   Elena.Budnik   update RemotePara...
509
510
511
512
513
514
515
516
517
				$this->paramMgr->paramDom->load($this->paramMgr->paramXML);
				
				if (!$this->paramMgr->makeComponents($param)) continue;
			}
		}
		return $centerNode;
	}
	
	
d471cf49   Elena.Budnik   format; IMPEX in ...
518
519
	protected function processGuestLogin() 
	{
32710720   Elena.Budnik   HomePage draft
520
521
522
523
// 		if (!$this->check_email_address($_POST['password'])) 
// 		{
// 			die('<a href="index.html"><h3>Invalid e-mail address. Please, try once more.</h3></a>');
// 		}
d471cf49   Elena.Budnik   format; IMPEX in ...
524
		
32710720   Elena.Budnik   HomePage draft
525
526
//		$passwd = $_POST['password'];
        $passwd = "public";
d471cf49   Elena.Budnik   format; IMPEX in ...
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
		$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
542
543
544
545
 
/*****************************************************************
*                           PUBLIC FUNCTIONS
*****************************************************************/
d471cf49   Elena.Budnik   format; IMPEX in ...
546
547
548

	public function setPath() 
	{
175ca164   Elena.Budnik   bad commit of Use...
549
		if (isset($_GET['sessionID'])) {
d471cf49   Elena.Budnik   format; IMPEX in ...
550
551
552
			$this->user = $_GET['sessionID'];
			$this->userdir = USERPATH."/".$this->user."/";                
		}
175ca164   Elena.Budnik   bad commit of Use...
553
		else if (isset($this->user)) {
d471cf49   Elena.Budnik   format; IMPEX in ...
554
555
			$this->userdir = USERPATH."/".$this->user."/";
		}
9140bdbd   Myriam Bouchemit   for proxy amdates...
556

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

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

175ca164   Elena.Budnik   bad commit of Use...
561
		foreach ($this->userDirs as $key => $val) {
d471cf49   Elena.Budnik   format; IMPEX in ...
562
563
564
565
			$dir = $usrdir.$val;
			define("$key","$dir/");
		} 
	}
16035364   Benjamin Renard   First commit
566

d471cf49   Elena.Budnik   format; IMPEX in ...
567
568
	public function ddCheckUser() 
	{
16035364   Benjamin Renard   First commit
569
		$this->IP = $this->getIPclient();
a22ecd3b   Benjamin Renard   Keep only necessa...
570
		$cmdCheckUser =  "DDCheckUser ".$this->IP." ".$this->user." 1> /dev/null 2> /dev/null";   
16035364   Benjamin Renard   First commit
571
572
		system($cmdCheckUser, $res);
		return $res;
d471cf49   Elena.Budnik   format; IMPEX in ...
573
	}
16035364   Benjamin Renard   First commit
574
  
d471cf49   Elena.Budnik   format; IMPEX in ...
575
576
577
	public function ddLogin() 
	{
		$this->IP = $this->getIPclient();
16035364   Benjamin Renard   First commit
578

a22ecd3b   Benjamin Renard   Keep only necessa...
579
		$loginCommd = "DDHtmlLogin ".$this->user." ".$this->passwd." ".$this->IP;
d471cf49   Elena.Budnik   format; IMPEX in ...
580
		system($loginCommd, $res);
01bb61f8   Benjamin Renard   Remove guest id w...
581
582
583
584
585
586
		
		if ($this->isGuest && ($res != 0)) {
			$guest = new Guest($this->user);
			$guest->deleteGuest();
		} 
		
9140bdbd   Myriam Bouchemit   for proxy amdates...
587

d471cf49   Elena.Budnik   format; IMPEX in ...
588
589
		return $res;
	}
16035364   Benjamin Renard   First commit
590
591
    
          	  	
d471cf49   Elena.Budnik   format; IMPEX in ...
592
	public function init() 
d675a6e8   Hacene SI HADJ MOHAND   Fix UserMgr
593
	{ 
1f3ee1cd   Benjamin Renard   Improve login pag...
594
595
596
		if ($this->ddLogin() != 0) {
			return FALSE;
		} 
37105924   Elena.Budnik   small nettoyage
597
            
d471cf49   Elena.Budnik   format; IMPEX in ...
598
599
		$this->userdir = USERPATH."/".$this->user."/";

175ca164   Elena.Budnik   bad commit of Use...
600
601
		if (!is_dir($this->userdir)) {           
			if (!$this->createDir()) { 
d471cf49   Elena.Budnik   format; IMPEX in ...
602
603
				die("Login for ".$this->user." failed: Can't create WS dirs");
			}
b6e4778f   Elena.Budnik   if isGuest - no o...
604
			$this->isFirst = true;
d471cf49   Elena.Budnik   format; IMPEX in ...
605
606
		}

13a84f26   Benjamin Renard   Call WS init duri...
607
608
609
610
611
612
613
		$requestManager = new RequestManagerClass();
		try {
			$res = $requestManager->runIHMRequest($this->user, $this->getIPclient(), FunctionTypeEnumClass::USERWSINIT, array());
		} catch (Exception $e) {
			die("Login for ".$this->user." failed: Error in WS dirs - ".$e->getMessage());
		}

175ca164   Elena.Budnik   bad commit of Use...
614
		if (file_exists($this->userdir.'newLogin')) {
ab5aab56   Elena.Budnik   delete not needed...
615
			touch($this->userdir.'lastLogin', filemtime($this->userdir.'newLogin'));
d471cf49   Elena.Budnik   format; IMPEX in ...
616
		}
ab5aab56   Elena.Budnik   delete not needed...
617
		else {
d471cf49   Elena.Budnik   format; IMPEX in ...
618
			touch($this->userdir.'lastLogin', time() - 5*86400); // last 5 days
ab5aab56   Elena.Budnik   delete not needed...
619
		}
d471cf49   Elena.Budnik   format; IMPEX in ...
620
621
622
623
		
		// if new info exists it will be shown to : 
		// public => to all
		// special => to group members
b6e4778f   Elena.Budnik   if isGuest - no o...
624
		if (file_exists($this->userdir.'newInfo')) {                
0323cf9d   Elena.Budnik   new info
625
			$amdaInfo = new AmdaNews($this->user);
0323cf9d   Elena.Budnik   new info
626
627
628
629
			$info = $this->getUserInfo($this->user);
			$status = $amdaInfo->makeInfo($info['group']); 

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

d471cf49   Elena.Budnik   format; IMPEX in ...
634
		$this->setPath(); 		
175ca164   Elena.Budnik   bad commit of Use...
635
	
175ca164   Elena.Budnik   bad commit of Use...
636
637
		if (!$this->makeLocalTree())
				die("Login for ".$this->user." failed: Can't make LocalParams.xml");;
d675a6e8   Hacene SI HADJ MOHAND   Fix UserMgr
638
639
		
		$ok = $this->makeRemoteTree();	                      		
16035364   Benjamin Renard   First commit
640

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

d471cf49   Elena.Budnik   format; IMPEX in ...
645
646
		//TODO sessionID = user + WSname
		$sessionID = $this->user;
a21497cc   Elena.Budnik   redmine #5645
647
648
649
650
651
652
		
		/*
		* Special settings are defined in the generic_data/SpecialSettings/Settings.xml
		*/
		$this->setSpecialSettings(); 
		
d471cf49   Elena.Budnik   format; IMPEX in ...
653
		/*
55e11e13   Elena.Budnik   new global AMDAUP...
654
		* Special groups are defined in the generic_data/SpecialSettings/Groups.xml
d471cf49   Elena.Budnik   format; IMPEX in ...
655
		*/
aa2786b4   Benjamin Renard   Opimization aroun...
656
		if (isset($this->userGrps)) {
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
		//	$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 ...
675
				}
175ca164   Elena.Budnik   bad commit of Use...
676
677
678
679
680
681
682
				// 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 ...
683
			}
d471cf49   Elena.Budnik   format; IMPEX in ...
684
685
686
		}
		return $sessionID;
	}
16035364   Benjamin Renard   First commit
687

175ca164   Elena.Budnik   bad commit of Use...
688
689
	protected function makeLocalTree()
	{
3afe4f59   Benjamin Renard   Add datasets time...
690
691
		$this->loadDatasetsTimeRestriction();

aa2786b4   Benjamin Renard   Opimization aroun...
692
693
694
		$this->loadUserGrps();

		if (file_exists(USERWSDIR.'LocalParams.xml')) {
3afe4f59   Benjamin Renard   Add datasets time...
695
			if (file_exists(USERWSDIR.'userGrpsChecksum') && file_exists(USERWSDIR.'datasetsTimeRestrictionCheckSum')) {
aa2786b4   Benjamin Renard   Opimization aroun...
696
				$lastGrpsChecksum = file_get_contents(USERWSDIR.'userGrpsChecksum');
3afe4f59   Benjamin Renard   Add datasets time...
697
698
				$lastDatasetsTimeRestrictionCheckSum = file_get_contents(USERWSDIR.'datasetsTimeRestrictionCheckSum');
				if (($lastGrpsChecksum == $this->userGrpsCheckSum) && ($lastDatasetsTimeRestrictionCheckSum == $this->datasetsTimeRestrictionCheckSum)) {
aa2786b4   Benjamin Renard   Opimization aroun...
699
700
701
702
703
704
705
706
707
					//No modification in groups for this user
					if (filemtime(USERWSDIR.'LocalParams.xml') == filemtime(LocalData.'/LocalParams.xml')) {
						//And no modification in LocalParams file => skip makeLocalTree
						return true;
					}
				}
			}
			unlink(USERWSDIR.'LocalParams.xml');
		}
d95e8421   Elena.Budnik   ws transfer : ini...
708

99ae8744   Benjamin Renard   Use config variab...
709
		if (!copy(LocalData.'/LocalParams.xml', USERWSDIR.'LocalParams.xml'))
aa2786b4   Benjamin Renard   Opimization aroun...
710
711
712
				die("Login for ".$this->user." failed: Can't copy LocalParams.xml");
		//Save groups checksum used to generate this user local tree
		file_put_contents(USERWSDIR.'userGrpsChecksum', $this->userGrpsCheckSum);
3afe4f59   Benjamin Renard   Add datasets time...
713
714
		//Save datasets time restrictions used to generate this user local tree
		file_put_contents(USERWSDIR.'datasetsTimeRestrictionCheckSum', $this->datasetsTimeRestrictionCheckSum);
175ca164   Elena.Budnik   bad commit of Use...
715
				
3afe4f59   Benjamin Renard   Add datasets time...
716
		$result = $this->updateTreeForGrpsAndTimeRestrictions(USERWSDIR.'LocalParams.xml');
aa2786b4   Benjamin Renard   Opimization aroun...
717
718
719
720
721

		//Set modif time to the original one
		touch(USERWSDIR.'LocalParams.xml', filemtime(LocalData.'/LocalParams.xml'));

		return TRUE;
175ca164   Elena.Budnik   bad commit of Use...
722
723
	}
	
3afe4f59   Benjamin Renard   Add datasets time...
724
	protected function updateTreeForGrpsAndTimeRestrictions($file)
175ca164   Elena.Budnik   bad commit of Use...
725
	{
175ca164   Elena.Budnik   bad commit of Use...
726
727
728
729
730
731
732
		$xml = new DomDocument("1.0");
		
		if(!$xml->load($file)) 
			die("Login for ".$this->user." failed: Can't load LocalParams.xml");
		
		$xp = new domxpath($xml);                                                                             
	  
81ba8c23   Benjamin Renard   Fix time restrict...
733
734
735
		if (!empty($this->userGrps)) {
			foreach ($this->userGrps as $grp) {
				$nodes = $xp->query("//*[@group='".$grp."']");
175ca164   Elena.Budnik   bad commit of Use...
736
			
81ba8c23   Benjamin Renard   Fix time restrict...
737
738
739
740
741
742
743
				if ($nodes->length > 0)
					foreach ($nodes as $node) {
							$node->removeAttribute('group');
							if ($node->hasAttribute('restriction')) {
								if ($node->getAttribute('restriction') != "plotOnly") {
									$node->removeAttribute('restriction');
								}
f29c5e66   Elena.Budnik   plot only option
744
							}
81ba8c23   Benjamin Renard   Fix time restrict...
745
746
					}
			}
175ca164   Elena.Budnik   bad commit of Use...
747
		}
3afe4f59   Benjamin Renard   Add datasets time...
748
749
750

		if (!empty($this->datasetsTimeRestriction)) {
			foreach ($this->datasetsTimeRestriction as $datasetTimeRestriction) {
a478e892   Benjamin Renard   Apply dataset tim...
751
752
				$dataset_id = str_replace('_','-',$datasetTimeRestriction['vi']);
				$nodes = $xp->query("//dataset[@xml:id='".$dataset_id."' or @PriorID='".$dataset_id."']");
3afe4f59   Benjamin Renard   Add datasets time...
753
754
755
				if ($nodes->length > 0) {
					foreach ($nodes as $node) {
						$timeRestriction = $datasetTimeRestriction['restriction'];
3afe4f59   Benjamin Renard   Add datasets time...
756
757
758
759
760
761
762
						if (!empty($timeRestriction)) {
							$node->setAttribute('timeRestriction', $timeRestriction);
						}
					}
				}
			}
		}
175ca164   Elena.Budnik   bad commit of Use...
763
764
765
766
		
		return $xml->save($file);
	}
	
2c7f2cb6   Benjamin Renard   Remove old log fi...
767
	public function dirSize($dir, $files_to_delete = array()) 
d471cf49   Elena.Budnik   format; IMPEX in ...
768
769
	{
		$handle = opendir($dir);
e57cb025   Benjamin Renard   Fix most of error...
770
771

		$mas = 0;
175ca164   Elena.Budnik   bad commit of Use...
772
773
		while ($file = readdir($handle)) {
			if ($file != '..' && $file != '.' && !is_dir($dir.'/'.$file)) {
2c7f2cb6   Benjamin Renard   Remove old log fi...
774
775
776
777
				if (in_array($file, $files_to_delete)) {
					unlink($dir.'/'.$file);
					continue;
				}
d471cf49   Elena.Budnik   format; IMPEX in ...
778
779
				$mas += filesize($dir.'/'.$file);
			} 
175ca164   Elena.Budnik   bad commit of Use...
780
			else if (is_dir($dir.'/'.$file) && $file != '..' && $file != '.') {
2c7f2cb6   Benjamin Renard   Remove old log fi...
781
				$mas += $this->dirSize($dir.'/'.$file, $files_to_delete);
d471cf49   Elena.Budnik   format; IMPEX in ...
782
783
784
785
			}
		}
		return $mas;
	} 
9140bdbd   Myriam Bouchemit   for proxy amdates...
786

2c7f2cb6   Benjamin Renard   Remove old log fi...
787
	public function  getWsSize($delete_log_files = FALSE) 
d471cf49   Elena.Budnik   format; IMPEX in ...
788
789
790
	{
		$dirsToCheck = array(USERDATADIR, USERTTDIR, USERWORKINGDIR);
		$wsSize = 0;
2c7f2cb6   Benjamin Renard   Remove old log fi...
791
792
793
794
		if ($delete_log_files) {
			$files_to_delete = array(
				'example.log',
				'cmd_output',
00e83786   Benjamin Renard   Fix
795
796
797
798
799
800
801
802
803
804
				'plotFunction0_data.txt',
				'plotFunction1_data.txt',
				'plotFunction2_data.txt',
				'plotFunction3_data.txt',
				'plotFunction4_data.txt',
				'plotFunction5_data.txt',
				'plotFunction6_data.txt',
				'plotFunction7_data.txt',
				'plotFunction8_data.txt',
				'plotFunction9_data.txt',
2c7f2cb6   Benjamin Renard   Remove old log fi...
805
806
807
808
809
			);
		}
		else {
			$files_to_delete = array();
		}
d471cf49   Elena.Budnik   format; IMPEX in ...
810
		foreach ($dirsToCheck as $dir) 
2c7f2cb6   Benjamin Renard   Remove old log fi...
811
			if (is_dir($dir)) $wsSize += $this->dirSize($dir, $files_to_delete);
9140bdbd   Myriam Bouchemit   for proxy amdates...
812

d471cf49   Elena.Budnik   format; IMPEX in ...
813
814
815
		return $wsSize;
	}
	
c5ef1a5d   Elena.Budnik   ereg => preg_match
816
    // http://www.ilovejackdaniels.com/php/email-address-validation/ 
d471cf49   Elena.Budnik   format; IMPEX in ...
817
818
819
	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
820
		 if (!preg_match("/^[^@]{1,64}@[^@]{1,255}$/", $email)) 
d471cf49   Elena.Budnik   format; IMPEX in ...
821
822
823
824
		{
		// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
			return false;
		}
c5ef1a5d   Elena.Budnik   ereg => preg_match
825
		
d471cf49   Elena.Budnik   format; IMPEX in ...
826
827
828
829
830
		// 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
831
			 if (!preg_match("@^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$@", $local_array[$i])) {
d471cf49   Elena.Budnik   format; IMPEX in ...
832
833
834
				return false;
			}
		}
16035364   Benjamin Renard   First commit
835

c5ef1a5d   Elena.Budnik   ereg => preg_match
836
		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 ...
837
838
839
840
841
842
			$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
843
				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 ...
844
845
846
847
848
849
850
					return false;
				}
			}
		}
		return true;
	}         
}
16035364   Benjamin Renard   First commit
851
?>