Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
602 stephane 1
<?php
2
/*
3
 
4
*/
703 stephane 5
if (!(defined('ALCASAR_SESSION') && (ALCASAR_SESSION === 1))){
6
	exit();
7
}
602 stephane 8
require_once('mysql.class.php');// the mysql class in already OK!
703 stephane 9
require_once(ALCASAR_ADMIN_PATH_INC.'/config.inc.php');
10
require_once('attrmap.php');
602 stephane 11
 
12
class radiusMysqlUser
13
{
14
	// public properties
15
	// no public properties
16
 
17
	// private properties
18
	private $database	= null;
19
	private $username	= null;
703 stephane 20
	private $userpassword	= null; //$userpassword attribute = Crypt-Password
21
	private $userInfos	= Array("id"=>"0","Username"=>"","Name"=>"","Mail"=>"","Department"=>"","WorkPhone"=>"","HomePhone"=>"","Mobile"=>"");
602 stephane 22
	private $checkItems	= Array();
23
	private $replyItems	= Array();
703 stephane 24
	private $op			= Array();
25
	private $groups		= Array();
602 stephane 26
 
27
	//TO DO : init $userInfos, $checkItems and $replyItems fields and operator from config file !!!! URGENT
28
 
29
 
30
	// protected properties
31
	// no protected properties
32
 
33
	// Class constructor
703 stephane 34
	public function __construct($dbOptions = Array())//ok
602 stephane 35
	{
703 stephane 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']);
39
		} else {
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);
43
			}
44
		}
45
		$this->_init();
602 stephane 46
	}
47
	// Class destructor
48
	public function __destruct()
49
	{
50
		//$this->mysql->close();	//is private !
51
		$this->database = null;
52
	}
53
	// public methods
54
	public static function find($options = Array(), $escape=false)
55
	{
56
		$database = new mysql("127.0.0.1","root","","radius");
57
		/*
58
		If the options are not xss clean, escape all options string by calling _escapeDatas() method.
59
		*/
60
		if ($escape == true) { 
61
			//$this->_extractArray($options, true); //create variable from $options array and get xss clean for mysql database
62
			$options = $this->_escapeDatas($options); //create variable from $options array and get xss clean for mysql database
63
		}
64
		/*
65
		The differents $options values are :
66
 
67
		$distinct	-> only distinct response ?
68
		$username	-> only for this username
69
		$fields		-> fields to return (default : username)
70
		$search		-> search value to find
71
		$search_IN	-> search in this/those field(s)(text or array)
72
		$limit		-> to limit the resultset
73
		$offset		-> offset (work with $limit for pagination)
74
		$sortby		-> sort by x field (default : no sorting)
75
		$sortdir	-> sort direction (ASC/DESC) (default : no sorting)
76
		$radius_attr-> radius attribute to find (text or array) if search_IN = radius
77
		*/
78
 
79
		//mysql_real_escape_string
80
 
81
 
82
		$sql = "SELECT ";
83
		// distinct option
84
		if ((isset($distinct))&&($distinct=="distinct"))
85
			$sql .= "DISTINCT ";
86
		// field option (make sure that the field exist!)
87
		if ((isset($options['fields']))&&($options['fields']!='')){
88
			$sql .= $options['fields'].", username ";
89
		}else{
90
			$sql .= "username ";
91
		}
92
		$sql .= "FROM userinfo ";
93
		// search option
94
		if ((isset($options['username']))&&($options['username']!='')){
95
 
96
		}
97
		// where option
98
		if ((isset($options['username']))&&($options['username']!=""))
99
		{
100
			$sql .= "WHERE username='".$options['username']."'";
101
			$this->username = $options['username'];
102
		}
103
		// sort
104
		if ((isset($options['sortby']))&&($options['sortby']!='')){
105
			$sql .= "ORDER BY ".$options['sortby']." ";
106
			if ((isset($options['sortdir']))&&($options['sortdir']!='')){
107
				$sql .= "LIMIT ".$options['sortdir']." ";
108
			}
109
		}
110
		// limit / offset
111
		if ((isset($options['limit']))&&($options['limit']!='')){
112
			if ((isset($options['offset']))&&($options['offset']!='')){
113
				$sql .= "LIMIT $offset $limit ";
114
			} else {
115
				$sql .= "LIMIT $limit ";
116
			}
117
 
118
		}
119
		$sql .= ";";
120
 
121
		// query
122
		$result = $database->query($sql);
123
		// return the result values
124
		return $result;
125
	}
126
	public function load($username, $attribute = false) //ok
127
	{
128
		/*
129
		Load an user from mysql database. If $attribute==true, get all chekitems and replyitems attributes too.
130
		*/
131
		$sql = "SELECT * FROM userinfo WHERE UserName='$username';";
132
		$result = $this->database->query($sql);
133
 
134
		if (count($result) != 1) return false;
135
 
136
		$this->userInfos = $result[0];
703 stephane 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
		}
602 stephane 144
 
145
		if ($attribute === true){
146
 
147
			// get from radcheck table
703 stephane 148
			$rows=null;
602 stephane 149
			$sql = "SELECT * FROM radcheck WHERE username='$username';";
703 stephane 150
			$rows = $this->database->query($sql);
602 stephane 151
 
703 stephane 152
			foreach ($rows as $row){
153
				$this->checkItems[$row['attribute']] = $row['value'];
154
			}
155
 
602 stephane 156
			// get from radreply table
703 stephane 157
			$rows=null;
602 stephane 158
			$sql = "SELECT * FROM radreply WHERE username='$username';";
703 stephane 159
			$rows = $this->database->query($sql);
160
			foreach ($rows as $row){
161
				$this->replyItems[$row['attribute']] = $row['value'];
162
			}
602 stephane 163
		}
164
 
165
		return true;
166
	}
703 stephane 167
	public function add()//ok
602 stephane 168
	{
169
		/*
170
		Add the current user with all his attribute in the mysql database
171
		(only if the user not already exist)
172
		*/
173
		$sql = "";
174
		//INSERT INTO table (a,b,c) VALUES (1,2,3)
175
 
176
		//INSERT userinfo table (insert)
703 stephane 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
 
602 stephane 180
		//INSERT radcheck table (insert)
703 stephane 181
		foreach($this->checkItems as $key => $value){
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
		}
602 stephane 187
		//INSERT radreply table (insert)
703 stephane 188
		foreach($this->replyItems as $key => $value){
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
		}
602 stephane 194
		//INSERT radusergroup table (insert)
703 stephane 195
		foreach($this->groups as $group){
196
			$sql = "INSERT INTO radusergroup (userName, groupname, priority) VALUES ($this->username, $group, 1)";
197
			$this->database->exec($sql);
198
		}
199
 
602 stephane 200
		//INSERT radpostauth table (insert)
703 stephane 201
		//$sql = "INSERT INTO radpostauth () VALUES ()";
202
		// NOT YET !
602 stephane 203
 
703 stephane 204
		//FUNCTION SET PASSWORD MUST BE CALLED MANUALLY !!!
602 stephane 205
	}
206
	public function delete() //ok
207
	{
703 stephane 208
		if ($this->username === null)
209
			return false;
210
 
602 stephane 211
		/*
212
		Delete the current user from the mysql database
213
		note : this function doesn't delete any accounting record of the current user
214
		*/
215
		if ($this->userid == 0) return 0; //0 record deleted
216
 
217
		//can be better with transaction
218
		$sql1 = "DELETE FROM radreply WHERE username = $this->username ;";
219
		$sql2 = "DELETE FROM radcheck WHERE username = $this->username ;";
220
		$sql3 = "DELETE FROM radpostauth WHERE username = $this->username ;";
221
		$sql4 = "DELETE FROM radusergroup WHERE username = $this->username ;";
222
		$sql5 = "DELETE FROM userinfo WHERE username = $this->username ;";
223
 
224
		$nb1 = $this->database->exec($sql1);
225
		$nb2 = $this->database->exec($sql2);
226
		$nb3 = $this->database->exec($sql3);
227
		$nb4 = $this->database->exec($sql4);
228
		$nb5 = $this->database->exec($sql5);
229
 
230
		return ($nb1+$nb2+$nb3+$nb4+$nb5); // n record deleted
231
	}
232
	public function update()
233
	{
703 stephane 234
		if ($this->username === null)
235
			return false;
236
 
602 stephane 237
		/*
238
		Update the current user with all his attribute in the mysql database
239
		(only if the user does not already exist)
240
		*/
241
		if ($this->userid == 0) return 0; //0 record deleted
242
 
243
		//UPDATE userinfo table (update)
244
 
245
		//UPDATE radcheck table (update)
703 stephane 246
		foreach ($this->checkItems  as $checkItem){
247
			if ($checkItem == ""){
248
				$this->_deleteItem($checkItem, "radcheck");
249
			} else {
250
				$this->_insertUpdateItem($checkItem, "radcheck");
251
			}
252
		}
602 stephane 253
		//UPDATE radreply table (update)
703 stephane 254
		foreach ($this->replyItems  as $replyItem){
255
			if ($replyItem == ""){
256
				$this->_deleteItem($replyItem, "radreply");
257
			} else {
258
				$this->_insertUpdateItem($replyItem, "radreply");
259
			}
260
		}
602 stephane 261
		//UPDATE radusergroup table (update)
703 stephane 262
		foreach ($this->groups  as $group){
263
			if ($group == ""){
264
				$this->_deletegroup($group);
265
			} else {
266
				$this->_insertUpdateGroup($group);
267
			}
268
		}
602 stephane 269
		//UPDATE radpostauth table (update)
703 stephane 270
		//NOT YET
602 stephane 271
	}
703 stephane 272
	public function save()
602 stephane 273
	{
703 stephane 274
		if ($this->username === null)
275
			return false;
276
 
602 stephane 277
		/*
278
		insert or Update the current user with all his attribute in the mysql database
279
		(use add() and update() method)
280
		*/
703 stephane 281
		if ($this->userInfos['id'] != 0){
282
			// User was loaded, so it exist
602 stephane 283
			return $this->update();
703 stephane 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
			}
602 stephane 299
		}
300
	}
703 stephane 301
 
302
	public function set($key = null, $val=null)//ok
602 stephane 303
	{
304
		/*
703 stephane 305
		Set a value in userInfos, checkItem or replyItem
602 stephane 306
		*/
703 stephane 307
		//exit('hs1');
308
		if (($key == null)||($val == null)){
309
			//exit('hs2');
310
			return false;
602 stephane 311
		} else {
703 stephane 312
			if (array_key_exists($key, $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;
324
			}
325
			return true;
326
		}		
602 stephane 327
	}
703 stephane 328
	public function get($key = null)//ok
602 stephane 329
	{
330
		/*
703 stephane 331
		Get a userInfos, checkItem or replyItem from the user or get the value from the mysql database
602 stephane 332
		*/
703 stephane 333
		if ($key == null){
334
			$tmp = array_merge($this->userInfos,$this->checkItems, $this->replyItems);
335
			return array_change_key_case($tmp);
336
		} else {
337
			if (array_key_exists($key, $this->userInfos)){
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
			}
602 stephane 346
		}
347
	}
703 stephane 348
	public function checkPassword($pwd)
602 stephane 349
	{
703 stephane 350
		//	Check the user password
351
		//	Return true or false
352
	}
353
	public function setPassword($pwd = null, $username = null)
354
	{
355
		if ($pwd==null){
356
			$pwd = $this->_encrypt($this->checkitems);
357
		} else {
602 stephane 358
 
703 stephane 359
		}
602 stephane 360
 
703 stephane 361
		//	Set or change the user password
362
		/*
363
		$sql = 
364
		"SELECT value FROM $config[sql_check_table] WHERE username = '$login'
365
			AND attribute = '$config[sql_password_attribute]';");
602 stephane 366
 
703 stephane 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
 
373
		*/
374
	}
375
 
376
	public function groups()
377
	{
378
		return $this->groups;
379
	}
380
	public function addgroup($groupname)//ok
381
	{
382
		$this->groups[] = $groupname;
383
	}
384
	public function deletegroup($groupname)//ok
385
	{
386
		if (array_key_exists($groupname, $this->groups)){
387
			unset($this->groups[$groupname]);
602 stephane 388
		}
389
	}
703 stephane 390
	// private methods
391
	private function _insertUpdateItem($itemName, $tableName)
602 stephane 392
	{
703 stephane 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
		}
602 stephane 405
	}
703 stephane 406
	private function _deleteItem($itemName, $tableName)
602 stephane 407
	{
703 stephane 408
		$sql1 = "DELETE FROM $tableName WHERE username = $this->username AND attribute = $itemName;";
409
		return $this->database->exec($sql1);
602 stephane 410
	}
703 stephane 411
	private function _insertUpdateGroup($groupName)
602 stephane 412
	{
703 stephane 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
		}
602 stephane 425
	}
703 stephane 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
	}
602 stephane 431
	private function _escapeDatas($options)
432
	{
433
 
434
	}
703 stephane 435
	private function _encrypt()
602 stephane 436
	{
703 stephane 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);
602 stephane 446
	}
703 stephane 447
	private function _init()
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
		}		
464
	}
602 stephane 465
	// protected methods
466
	// no protected method
467
}
468
?>