Subversion Repositories ALCASAR

Rev

Rev 610 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 610 Rev 703
Line 1... Line 1...
1
<?php
1
<?php
2
/*
2
/*
3
 
3
 
4
*/
4
*/
-
 
5
if (!(defined('ALCASAR_SESSION') && (ALCASAR_SESSION === 1))){
-
 
6
	exit();
-
 
7
}
5
require_once('mysql.class.php');// the mysql class in already OK!
8
require_once('mysql.class.php');// the mysql class in already OK!
-
 
9
require_once(ALCASAR_ADMIN_PATH_INC.'/config.inc.php');
-
 
10
require_once('attrmap.php');
6
 
11
 
7
class radiusMysqlUser
12
class radiusMysqlUser
8
{
13
{
9
	// public properties
14
	// public properties
10
	// no public properties
15
	// no public properties
11
	
16
	
12
	// private properties
17
	// private properties
13
	private $database	= null;
18
	private $database	= null;
14
	private $username	= null;
19
	private $username	= null;
15
	private $userid		= 0;
20
	private $userpassword	= null; //$userpassword attribute = Crypt-Password
16
	private $userInfos	= Array("Username"=>"","Name"=>"","Mail"=>"","Department"=>"","WorkPhone"=>"","HomePhone"=>"","Mobile"=>"");
21
	private $userInfos	= Array("id"=>"0","Username"=>"","Name"=>"","Mail"=>"","Department"=>"","WorkPhone"=>"","HomePhone"=>"","Mobile"=>"");
17
	private $checkItems	= Array();
22
	private $checkItems	= Array();
18
	private $replyItems	= Array();
23
	private $replyItems	= Array();
-
 
24
	private $op			= Array();
-
 
25
	private $groups		= Array();
19
	
26
	
20
	//TO DO : init $userInfos, $checkItems and $replyItems fields and operator from config file !!!! URGENT
27
	//TO DO : init $userInfos, $checkItems and $replyItems fields and operator from config file !!!! URGENT
21
	
28
	
22
	
29
	
23
	// protected properties
30
	// protected properties
24
	// no protected properties
31
	// no protected properties
25
	
32
	
26
	// Class constructor
33
	// Class constructor
27
	public function __construct($dbOptions = Array(), $attributeConfig = Array())
34
	public function __construct($dbOptions = Array())//ok
28
	{
35
	{
-
 
36
		if (count($dbOptions) == 0){
-
 
37
			global $config;
-
 
38
			$this->database = new mysql($config['mysql_host'],$config['mysql_user'],$config['mysql_pwd'],$config['mysql_db']);
29
		/*
39
		} else {
30
		Db init and config init to do!
40
			extract($dbOptions);
-
 
41
			if (isset($mysql_host)&&isset($mysql_user)&&isset($mysql_pwd)&&isset($mysql_db)){
-
 
42
				$this->database = new mysql($mysql_host,$mysql_user,$mysql_pwd,$mysql_db);
31
		*/
43
			}
-
 
44
		}
32
		$this->database = new mysql("127.0.0.1","root","","radius");
45
		$this->_init();
33
	}
46
	}
34
	// Class destructor
47
	// Class destructor
35
	public function __destruct()
48
	public function __destruct()
36
	{
49
	{
37
		//$this->mysql->close();	//is private !
50
		//$this->mysql->close();	//is private !
Line 119... Line 132...
119
		$result = $this->database->query($sql);
132
		$result = $this->database->query($sql);
120
	
133
	
121
		if (count($result) != 1) return false;
134
		if (count($result) != 1) return false;
122
		
135
		
123
		$this->userInfos = $result[0];
136
		$this->userInfos = $result[0];
-
 
137
 
-
 
138
		$sql = "SELECT * FROM radusergroup WHERE UserName='$username';";
-
 
139
		$groups = $this->database->query($sql);
-
 
140
	
-
 
141
		foreach ($groups as $group){
-
 
142
			$this->groups[] = $group['groupname'];
-
 
143
		}
124
		
144
		
125
		if ($attribute === true){
145
		if ($attribute === true){
126
		
146
		
127
			// get from radcheck table
147
			// get from radcheck table
128
			$result=null;
148
			$rows=null;
129
			$sql = "SELECT * FROM radcheck WHERE username='$username';";
149
			$sql = "SELECT * FROM radcheck WHERE username='$username';";
130
			$result = $this->database->query($sql);
150
			$rows = $this->database->query($sql);
-
 
151
			
131
			if (count($result) != 1) return false;
152
			foreach ($rows as $row){
132
			$this->checkItems = $result[0];
153
				$this->checkItems[$row['attribute']] = $row['value'];
-
 
154
			}
133
			
155
			
134
			// get from radreply table
156
			// get from radreply table
135
			$result=null;
157
			$rows=null;
136
			$sql = "SELECT * FROM radreply WHERE username='$username';";
158
			$sql = "SELECT * FROM radreply WHERE username='$username';";
137
			$result = $this->database->query($sql);
159
			$rows = $this->database->query($sql);
138
			if (count($result) != 1) return false;
160
			foreach ($rows as $row){
139
			$this->replyItems = $result[0];
161
				$this->replyItems[$row['attribute']] = $row['value'];
-
 
162
			}
140
		}
163
		}
141
		
164
		
142
		return true;
165
		return true;
143
	}
166
	}
144
	public function add()
167
	public function add()//ok
145
	{
168
	{
146
		/*
169
		/*
147
		Add the current user with all his attribute in the mysql database
170
		Add the current user with all his attribute in the mysql database
148
		(only if the user not already exist)
171
		(only if the user not already exist)
149
		*/
172
		*/
150
		$sql = "";
173
		$sql = "";
151
		//INSERT INTO table (a,b,c) VALUES (1,2,3)
174
		//INSERT INTO table (a,b,c) VALUES (1,2,3)
152
		
175
		
153
		//INSERT userinfo table (insert)
176
		//INSERT userinfo table (insert)
154
		$sql = "INSERT INTO userinfo (UserName, Name, Mail, Department, WorkPhone, HomePhone, Mobile) VALUES ()";
177
		$sql = "INSERT INTO userinfo (UserName, Name, Mail, Department, WorkPhone, HomePhone, Mobile) VALUES ($this->username, $this->userInfos['Name'], $this->userInfos['Mail'], $this->userInfos['Department'],$this->userInfos['WorkPhone'],$this->userInfos['HomePhone'],$this->userInfos['Mobile'])";
-
 
178
		$this->database->exec($sql);
-
 
179
		
155
		//INSERT radcheck table (insert)
180
		//INSERT radcheck table (insert)
-
 
181
		foreach($this->checkItems as $key => $value){
156
		$sql = "";
182
			if ($value!=""){
-
 
183
				$sql = "INSERT INTO radcheck (UserName, attribute, op, value) VALUES ($this->username, $key, $this->op[$key], $value)";
-
 
184
				$this->database->exec($sql);
-
 
185
			}
-
 
186
		}
157
		//INSERT radreply table (insert)
187
		//INSERT radreply table (insert)
-
 
188
		foreach($this->replyItems as $key => $value){
158
		$sql = "";
189
			if ($value!=""){
-
 
190
				$sql = "INSERT INTO radreply (UserName, attribute, op, value) VALUES ($this->username, $key, $this->op[$key], $value)";
-
 
191
				$this->database->exec($sql);
-
 
192
			}
-
 
193
		}
159
		//INSERT radusergroup table (insert)
194
		//INSERT radusergroup table (insert)
160
		$sql = "";
195
		foreach($this->groups as $group){
-
 
196
			$sql = "INSERT INTO radusergroup (userName, groupname, priority) VALUES ($this->username, $group, 1)";
161
		//INSERT radpostauth table (insert)
197
			$this->database->exec($sql);
-
 
198
		}
162
		
199
		
-
 
200
		//INSERT radpostauth table (insert)
-
 
201
		//$sql = "INSERT INTO radpostauth () VALUES ()";
-
 
202
		// NOT YET !
163
		
203
		
-
 
204
		//FUNCTION SET PASSWORD MUST BE CALLED MANUALLY !!!
164
	}
205
	}
165
	public function delete() //ok
206
	public function delete() //ok
166
	{
207
	{
-
 
208
		if ($this->username === null)
-
 
209
			return false;
-
 
210
			
167
		/*
211
		/*
168
		Delete the current user from the mysql database
212
		Delete the current user from the mysql database
169
		note : this function doesn't delete any accounting record of the current user
213
		note : this function doesn't delete any accounting record of the current user
170
		*/
214
		*/
171
		if ($this->userid == 0) return 0; //0 record deleted
215
		if ($this->userid == 0) return 0; //0 record deleted
Line 185... Line 229...
185
		
229
		
186
		return ($nb1+$nb2+$nb3+$nb4+$nb5); // n record deleted
230
		return ($nb1+$nb2+$nb3+$nb4+$nb5); // n record deleted
187
	}
231
	}
188
	public function update()
232
	public function update()
189
	{
233
	{
-
 
234
		if ($this->username === null)
-
 
235
			return false;
-
 
236
			
190
		/*
237
		/*
191
		Update the current user with all his attribute in the mysql database
238
		Update the current user with all his attribute in the mysql database
192
		(only if the user does not already exist)
239
		(only if the user does not already exist)
193
		*/
240
		*/
194
		if ($this->userid == 0) return 0; //0 record deleted
241
		if ($this->userid == 0) return 0; //0 record deleted
195
		
242
		
196
		//UPDATE userinfo table (update)
243
		//UPDATE userinfo table (update)
197
		
244
		
198
		//UPDATE radcheck table (update)
245
		//UPDATE radcheck table (update)
-
 
246
		foreach ($this->checkItems  as $checkItem){
-
 
247
			if ($checkItem == ""){
-
 
248
				$this->_deleteItem($checkItem, "radcheck");
-
 
249
			} else {
-
 
250
				$this->_insertUpdateItem($checkItem, "radcheck");
-
 
251
			}
199
		
252
		}
200
		//UPDATE radreply table (update)
253
		//UPDATE radreply table (update)
-
 
254
		foreach ($this->replyItems  as $replyItem){
-
 
255
			if ($replyItem == ""){
-
 
256
				$this->_deleteItem($replyItem, "radreply");
-
 
257
			} else {
-
 
258
				$this->_insertUpdateItem($replyItem, "radreply");
-
 
259
			}
201
		
260
		}
202
		//UPDATE radusergroup table (update)
261
		//UPDATE radusergroup table (update)
-
 
262
		foreach ($this->groups  as $group){
-
 
263
			if ($group == ""){
-
 
264
				$this->_deletegroup($group);
-
 
265
			} else {
-
 
266
				$this->_insertUpdateGroup($group);
-
 
267
			}
203
		
268
		}
204
		//UPDATE radpostauth table (update)
269
		//UPDATE radpostauth table (update)
-
 
270
		//NOT YET
205
	}
271
	}
206
	public function save() //ok
272
	public function save()
207
	{
273
	{
-
 
274
		if ($this->username === null)
-
 
275
			return false;
-
 
276
			
208
		/*
277
		/*
209
		insert or Update the current user with all his attribute in the mysql database
278
		insert or Update the current user with all his attribute in the mysql database
210
		(use add() and update() method)
279
		(use add() and update() method)
211
		*/
280
		*/
212
		if ($this->userid == 0){
281
		if ($this->userInfos['id'] != 0){
213
			return $this->add();
282
			// User was loaded, so it exist
214
		} else {
-
 
215
			return $this->update();
283
			return $this->update();
-
 
284
		}else{
-
 
285
			// load function was not called, we must test if the user exist!
-
 
286
			$options['username'] = $this->username;
-
 
287
			$users = radiusMysqlUser::find($options);
-
 
288
			if (count($users)==0){
-
 
289
				//username do not exist
-
 
290
				
-
 
291
			} elseif (count($users)==1){
-
 
292
				//username already exist
-
 
293
				return $this->update();
-
 
294
			} else {
-
 
295
				// error in database, we fixe it
-
 
296
				$this->delete();
-
 
297
				return $this->add();
-
 
298
			}
216
		}
299
		}
217
	}
300
	}
-
 
301
	
218
	public function get($userInfo = 'null') //ok
302
	public function set($key = null, $val=null)//ok
219
	{
303
	{
220
		/*
304
		/*
221
		return userInfos
305
		Set a value in userInfos, checkItem or replyItem
222
		*/
306
		*/
-
 
307
		//exit('hs1');
223
		if (array_key_exists($userInfo, $this->userInfos)){
308
		if (($key == null)||($val == null)){
-
 
309
			//exit('hs2');
224
			return $this->userInfos[$userInfo];
310
			return false;
225
		} else {
311
		} else {
-
 
312
			if (array_key_exists($key, $this->userInfos)){
226
			return $this->userInfos;
313
				$this->userInfos[$key] = $val;
-
 
314
				//exit('hs3');
-
 
315
			} elseif (array_key_exists($key, $this->checkItems)){
-
 
316
				$this->checkItems[$key] = $val;
-
 
317
				//exit('hs4');
-
 
318
			} elseif (array_key_exists($key, $this->replyItems)){
-
 
319
				$this->replyItems[$key] = $val;
-
 
320
				//exit('hs5');
-
 
321
			} else{
-
 
322
				//exit('hs6');
-
 
323
				return false;
227
		}
324
			}
-
 
325
			return true;
-
 
326
		}		
228
	}
327
	}
229
	public function set($userInfo) //ok
328
	public function get($key = null)//ok
230
	{
329
	{
231
		/*
330
		/*
232
		Set a value in userInfos
331
		Get a userInfos, checkItem or replyItem from the user or get the value from the mysql database
233
		*/
332
		*/
-
 
333
		if ($key == null){
-
 
334
			$tmp = array_merge($this->userInfos,$this->checkItems, $this->replyItems);
-
 
335
			return array_change_key_case($tmp);
-
 
336
		} else {
234
		if (array_key_exists($userInfo, $this->userInfos)){
337
			if (array_key_exists($key, $this->userInfos)){
235
			$this->userInfos[$userInfo] = $userInfo;
338
				 return $this->userInfos[$key];
-
 
339
			} elseif (array_key_exists($key, $this->checkItems)){
-
 
340
				return $this->checkItems[$key];
-
 
341
			} elseif (array_key_exists($key, $this->replyItems)){
-
 
342
				return $this->replyItems[$key];
-
 
343
			} else{
-
 
344
				return null;
-
 
345
			}
236
		}
346
		}
237
		if (strtolower($userInfo) == "username") $this->username = $userInfo;
-
 
238
	}
347
	}
239
	public function getAttribute($attribute = null)
348
	public function checkPassword($pwd)
240
	{
349
	{
241
		/*
350
		//	Check the user password
242
		Get a checkItem or replyItem from the user or get the value from the mysql database
351
		//	Return true or false
243
		*/
352
	}
244
		if (array_key_exists($attribute, $this->userInfos)){
353
	public function setPassword($pwd = null, $username = null)
245
		
354
	{
-
 
355
		if ($pwd==null){
246
		} elseif (array_key_exists($attribute, $this->checkItems)){
356
			$pwd = $this->_encrypt($this->checkitems);
-
 
357
		} else {
247
		
358
		
248
		} elseif (array_key_exists($attribute, $this->replyItems)){
-
 
-
 
359
		}
249
		
360
		
-
 
361
		//	Set or change the user password
-
 
362
		/*
250
		} else{
363
		$sql = 
-
 
364
		"SELECT value FROM $config[sql_check_table] WHERE username = '$login'
-
 
365
			AND attribute = '$config[sql_password_attribute]';");
251
		
366
		
-
 
367
"UPDATE $config[sql_check_table] SET value = '$passwd' $text3 WHERE
-
 
368
				attribute = '$config[sql_password_attribute]' AND username = '$login';"
-
 
369
 
-
 
370
"INSERT INTO $config[sql_check_table] (attribute,value,username $text1)
-
 
371
					VALUES ('$config[sql_password_attribute]','$passwd','$login' $text2);"
-
 
372
					
252
		}
373
		*/
253
	}
374
	}
-
 
375
	
254
	public function setAttribute($attribute)
376
	public function groups()
255
	{
377
	{
256
		/*
-
 
257
		Set a checkItem or replyItem of the user
378
		return $this->groups;
258
		*/
-
 
259
	}
379
	}
260
	public function checkPassword($pwd)
380
	public function addgroup($groupname)//ok
261
	{
381
	{
262
		//	Check the user password
-
 
263
		//	Return true or false
382
		$this->groups[] = $groupname;
264
	}
383
	}
265
	public function setPassword($pwd)
384
	public function deletegroup($groupname)//ok
266
	{
385
	{
-
 
386
		if (array_key_exists($groupname, $this->groups)){
267
		//	Set or change the user password
387
			unset($this->groups[$groupname]);
-
 
388
		}
268
	}
389
	}
269
	// private methods
390
	// private methods
-
 
391
	private function _insertUpdateItem($itemName, $tableName)
-
 
392
	{
-
 
393
		// faire un select
-
 
394
		$sqlSelect = "";
-
 
395
		$result = $database->query($sqlSelect);
-
 
396
		if (count($result) > 0){
-
 
397
			// update si réponse select > 0
-
 
398
			$sqlUpdate = "";
-
 
399
			return $this->database->exec($sqlUpdate);
-
 
400
		} else {
-
 
401
			// insert si réponse select == 0
-
 
402
			$sqlInsert = "";
-
 
403
			return $this->database->exec($sqlInsert);
-
 
404
		}
-
 
405
	}
-
 
406
	private function _deleteItem($itemName, $tableName)
-
 
407
	{
-
 
408
		$sql1 = "DELETE FROM $tableName WHERE username = $this->username AND attribute = $itemName;";
-
 
409
		return $this->database->exec($sql1);
-
 
410
	}
-
 
411
	private function _insertUpdateGroup($groupName)
-
 
412
	{
-
 
413
		// faire un select
-
 
414
		$sqlSelect = "";
-
 
415
		$result = $database->query($sqlSelect);
-
 
416
		if (count($result) > 0){
-
 
417
			// update si réponse select > 0
-
 
418
			$sqlUpdate = "";
-
 
419
			return $this->database->exec($sqlUpdate);
-
 
420
		} else {
-
 
421
			// insert si réponse select == 0
-
 
422
			$sqlInsert = "";
-
 
423
			return $this->database->exec($sqlInsert);
-
 
424
		}
-
 
425
	}
-
 
426
	private function _deletegroup($groupName)
-
 
427
	{
-
 
428
		$sql1 = "DELETE FROM radusergroup WHERE username = $this->username AND groupname = $groupName;";
-
 
429
		return $this->database->exec($sql1);
-
 
430
	}
270
	private function _escapeDatas($options)
431
	private function _escapeDatas($options)
271
	{
432
	{
272
	
433
	
273
	}
434
	}
274
	private function _init($configFile)
435
	private function _encrypt()
275
	{
436
	{
-
 
437
		$numargs=func_num_args();
-
 
438
		$passwd=func_get_arg(0);
-
 
439
		# calcul d'un salt pour forcer le chiffrement en MD5 au lieu de blowfish par defaut dans php version mdva > 2007.1
-
 
440
		$salt='$1$passwd$';
-
 
441
		if ($numargs == 2){
-
 
442
			$salt=func_get_arg(1);
-
 
443
			return crypt($passwd,$salt);
-
 
444
		}
-
 
445
			return crypt($passwd,$salt);
-
 
446
	}
-
 
447
	private function _init()
276
	
448
	{
-
 
449
		//TO DO : supprimer les variables globales
-
 
450
		global $attrmap, $attr_type, $attr_op;
-
 
451
		
-
 
452
		foreach ($attrmap as $attr){
-
 
453
			if ($attr_type[$attr]=="checkItem"){
-
 
454
				$this->checkItems[$attr] = "";
-
 
455
			}elseif ($attr_type[$attr]=="replyItem"){
-
 
456
				$this->replyItems[$attr] = "";
-
 
457
			}
-
 
458
			if ($attr_op[$attr] != ""){
-
 
459
				$this->op[$attr] = $attr_op[$attr];
-
 
460
			} else {
-
 
461
				$this->op[$attr] = "=";
-
 
462
			}
-
 
463
		}		
277
	}
464
	}
278
	// protected methods
465
	// protected methods
279
	// no protected method
466
	// no protected method
280
}
467
}
281
?>
468
?>
282
 
469