Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
703 stephane 1
<?php
2
/*
3
 
4
*/
5
if (!(defined('ALCASAR_SESSION') && (ALCASAR_SESSION === 1))){
6
	exit();
7
}
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');
11
 
12
class radiusMysqlUser
13
{
14
	// public properties
15
	// no public properties
16
 
17
	// private properties
18
	private $database	= null;
19
	private $username	= null;
20
	private $userpassword	= null; //$userpassword attribute = Crypt-Password
21
	private $userInfos	= Array("id"=>"0","Username"=>"","Name"=>"","Mail"=>"","Department"=>"","WorkPhone"=>"","HomePhone"=>"","Mobile"=>"");
22
	private $checkItems	= Array();
23
	private $replyItems	= Array();
24
	private $op			= Array();
25
	private $groups		= Array();
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
34
	public function __construct($dbOptions = Array())
35
	{
36
		/*
37
		Db init and config init to do!
38
		*/
39
		if (count($dbOptions) == 0){
40
			global $config;
41
			$this->database = new mysql($config['mysql_host'],$config['mysql_user'],$config['mysql_pwd'],$config['mysql_db']);
42
		} else {
43
			// TO DO
44
			//$this->database = new mysql("127.0.0.1","root","","radius");
45
		}
46
		$this->_init();
47
	}
48
	// Class destructor
49
	public function __destruct()
50
	{
51
		//$this->mysql->close();	//is private !
52
		$this->database = null;
53
	}
54
	// public methods
55
	public static function find($options = Array(), $escape=false)
56
	{
57
		$database = new mysql("127.0.0.1","root","","radius");
58
		/*
59
		If the options are not xss clean, escape all options string by calling _escapeDatas() method.
60
		*/
61
		if ($escape == true) { 
62
			//$this->_extractArray($options, true); //create variable from $options array and get xss clean for mysql database
63
			$options = $this->_escapeDatas($options); //create variable from $options array and get xss clean for mysql database
64
		}
65
		/*
66
		The differents $options values are :
67
 
68
		$distinct	-> only distinct response ?
69
		$username	-> only for this username
70
		$fields		-> fields to return (default : username)
71
		$search		-> search value to find
72
		$search_IN	-> search in this/those field(s)(text or array)
73
		$limit		-> to limit the resultset
74
		$offset		-> offset (work with $limit for pagination)
75
		$sortby		-> sort by x field (default : no sorting)
76
		$sortdir	-> sort direction (ASC/DESC) (default : no sorting)
77
		$radius_attr-> radius attribute to find (text or array) if search_IN = radius
78
		*/
79
 
80
		//mysql_real_escape_string
81
 
82
 
83
		$sql = "SELECT ";
84
		// distinct option
85
		if ((isset($distinct))&&($distinct=="distinct"))
86
			$sql .= "DISTINCT ";
87
		// field option (make sure that the field exist!)
88
		if ((isset($options['fields']))&&($options['fields']!='')){
89
			$sql .= $options['fields'].", username ";
90
		}else{
91
			$sql .= "username ";
92
		}
93
		$sql .= "FROM userinfo ";
94
		// search option
95
		if ((isset($options['username']))&&($options['username']!='')){
96
 
97
		}
98
		// where option
99
		if ((isset($options['username']))&&($options['username']!=""))
100
		{
101
			$sql .= "WHERE username='".$options['username']."'";
102
			$this->username = $options['username'];
103
		}
104
		// sort
105
		if ((isset($options['sortby']))&&($options['sortby']!='')){
106
			$sql .= "ORDER BY ".$options['sortby']." ";
107
			if ((isset($options['sortdir']))&&($options['sortdir']!='')){
108
				$sql .= "LIMIT ".$options['sortdir']." ";
109
			}
110
		}
111
		// limit / offset
112
		if ((isset($options['limit']))&&($options['limit']!='')){
113
			if ((isset($options['offset']))&&($options['offset']!='')){
114
				$sql .= "LIMIT $offset $limit ";
115
			} else {
116
				$sql .= "LIMIT $limit ";
117
			}
118
 
119
		}
120
		$sql .= ";";
121
 
122
		// query
123
		$result = $database->query($sql);
124
		// return the result values
125
		return $result;
126
	}
127
	public function load($username, $attribute = false) //ok
128
	{
129
		/*
130
		Load an user from mysql database. If $attribute==true, get all chekitems and replyitems attributes too.
131
		*/
132
		$sql = "SELECT * FROM userinfo WHERE UserName='$username';";
133
		$result = $this->database->query($sql);
134
 
135
		if (count($result) != 1) return false;
136
 
137
		$this->userInfos = $result[0];
138
 
139
		$sql = "SELECT * FROM radusergroup WHERE UserName='$username';";
140
		$groups = $this->database->query($sql);
141
 
142
		foreach ($groups as $group){
143
			$this->groups[] = $group['groupname'];
144
		}
145
 
146
		if ($attribute === true){
147
 
148
			// get from radcheck table
149
			$rows=null;
150
			$sql = "SELECT * FROM radcheck WHERE username='$username';";
151
			$rows = $this->database->query($sql);
152
 
153
			foreach ($rows as $row){
154
				$this->checkItems[$row['attribute']] = $row['value'];
155
			}
156
 
157
			// get from radreply table
158
			$rows=null;
159
			$sql = "SELECT * FROM radreply WHERE username='$username';";
160
			$rows = $this->database->query($sql);			
161
			foreach ($rows as $row){
162
				$this->replyItems[$row['attribute']] = $row['value'];
163
			}
164
		}
165
 
166
		return true;
167
	}
168
	public function add()
169
	{
170
		/*
171
		Add the current user with all his attribute in the mysql database
172
		(only if the user not already exist)
173
		*/
174
		$sql = "";
175
		//INSERT INTO table (a,b,c) VALUES (1,2,3)
176
 
177
		//INSERT userinfo table (insert)
178
		$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'])";
179
		$this->database->exec($sql);
180
 
181
		//INSERT radcheck table (insert)
182
		foreach($this->checkItems as $key => $value){
183
			if ($value!=""){
184
				$sql = "INSERT INTO radcheck (UserName, attribute, op, value) VALUES ($this->username, $key, $this->op[$key], $value)";
185
				$this->database->exec($sql);
186
			}
187
		}
188
		//INSERT radreply table (insert)
189
		foreach($this->replyItems as $key => $value){
190
			if ($value!=""){
191
				$sql = "INSERT INTO radreply (UserName, attribute, op, value) VALUES ($this->username, $key, $this->op[$key], $value)";
192
				$this->database->exec($sql);
193
			}
194
		}
195
		//INSERT radusergroup table (insert)
196
		foreach($this->groups as $group){
197
			$sql = "INSERT INTO radusergroup (userName, groupname, priority) VALUES ($this->username, $group, 1)";
198
			$this->database->exec($sql);
199
		}
200
 
201
		//INSERT radpostauth table (insert)
202
		//$sql = "INSERT INTO radpostauth () VALUES ()";
203
		// NOT YET !
204
 
205
		//FUNCTION SET PASSWORD MUST BE CALLED MANUALLY !!!
206
	}
207
	public function delete() //ok
208
	{
209
		if ($this->username === null)
210
			return false;
211
 
212
		/*
213
		Delete the current user from the mysql database
214
		note : this function doesn't delete any accounting record of the current user
215
		*/
216
		if ($this->userid == 0) return 0; //0 record deleted
217
 
218
		//can be better with transaction
219
		$sql1 = "DELETE FROM radreply WHERE username = $this->username ;";
220
		$sql2 = "DELETE FROM radcheck WHERE username = $this->username ;";
221
		$sql3 = "DELETE FROM radpostauth WHERE username = $this->username ;";
222
		$sql4 = "DELETE FROM radusergroup WHERE username = $this->username ;";
223
		$sql5 = "DELETE FROM userinfo WHERE username = $this->username ;";
224
 
225
		$nb1 = $this->database->exec($sql1);
226
		$nb2 = $this->database->exec($sql2);
227
		$nb3 = $this->database->exec($sql3);
228
		$nb4 = $this->database->exec($sql4);
229
		$nb5 = $this->database->exec($sql5);
230
 
231
		return ($nb1+$nb2+$nb3+$nb4+$nb5); // n record deleted
232
	}
233
	public function update()
234
	{
235
		if ($this->username === null)
236
			return false;
237
 
238
		/*
239
		Update the current user with all his attribute in the mysql database
240
		(only if the user does not already exist)
241
		*/
242
		if ($this->userid == 0) return 0; //0 record deleted
243
 
244
		//UPDATE userinfo table (update)
245
 
246
		//UPDATE radcheck table (update)
247
 
248
		//UPDATE radreply table (update)
249
 
250
		//UPDATE radusergroup table (update)
251
 
252
		//UPDATE radpostauth table (update)
253
	}
254
	public function save() //ok
255
	{
256
		if ($this->username === null)
257
			return false;
258
 
259
		/*
260
		insert or Update the current user with all his attribute in the mysql database
261
		(use add() and update() method)
262
		*/
263
		if ($this->userInfos['id'] != 0){
264
			// User was loaded, so it exist
265
			return $this->update();
266
		}else{
267
			// load function was not called, we must test if the user exist!
268
			$options['username'] = $this->username;
269
			$users = radiusMysqlUser::find($options);
270
			if (count($users)==0){
271
				//username do not exist
272
 
273
			} elseif (count($users)==1){
274
				//username already exist
275
				return $this->update();
276
			} else {
277
				// error in database, we fixe it
278
				$this->delete();
279
				return $this->add();
280
			}
281
		}
282
	}
283
 
284
	public function get($userInfo = 'null') //ok
285
	{
286
		/*
287
		return userInfos
288
		*/
289
		if (array_key_exists($userInfo, $this->userInfos)){
290
			return $this->userInfos[$userInfo];
291
		} else {
292
			return $this->userInfos;
293
		}
294
	}
295
	public function set($userInfo) //ok
296
	{
297
		/*
298
		Set a value in userInfos
299
		*/
300
		if (array_key_exists($userInfo, $this->userInfos)){
301
			$this->userInfos[$userInfo] = $userInfo;
302
		}
303
		if (strtolower($userInfo) == "username") $this->username = $userInfo;
304
	}
305
	public function getAttribute($attribute = null)
306
	{
307
		/*
308
		Get a checkItem or replyItem from the user or get the value from the mysql database
309
		*/
310
		if ($attribute == null){
311
			return array_merge($this->checkItems, $this->replyItems);
312
		} else {
313
			if (array_key_exists($attribute, $this->userInfos)){
314
				 return $this->userInfos[$attribute];
315
			} elseif (array_key_exists($attribute, $this->checkItems)){
316
				return $this->checkItems[$attribute];
317
			} elseif (array_key_exists($attribute, $this->replyItems)){
318
				return $this->replyItems[$attribute];
319
			} else{
320
				return null;
321
			}
322
		}
323
	}
324
	public function setAttribute($attribute)
325
	{
326
		/*
327
		Set a checkItem or replyItem of the user
328
		*/
329
	}
330
	public function checkPassword($pwd)
331
	{
332
		//	Check the user password
333
		//	Return true or false
334
	}
335
	public function setPassword($pwd = null, $username = null)
336
	{
337
		if ($pwd==null){
338
			$pwd = $this->_encrypt($this->checkitems);
339
		} else {
340
 
341
		}
342
 
343
		//	Set or change the user password
344
		/*
345
		$sql = 
346
		"SELECT value FROM $config[sql_check_table] WHERE username = '$login'
347
			AND attribute = '$config[sql_password_attribute]';");
348
 
349
"UPDATE $config[sql_check_table] SET value = '$passwd' $text3 WHERE
350
				attribute = '$config[sql_password_attribute]' AND username = '$login';"
351
 
352
"INSERT INTO $config[sql_check_table] (attribute,value,username $text1)
353
					VALUES ('$config[sql_password_attribute]','$passwd','$login' $text2);"
354
 
355
		*/
356
	}
357
	// private methods
358
	private function _escapeDatas($options)
359
	{
360
 
361
	}
362
	private function _encrypt()
363
	{
364
		$numargs=func_num_args();
365
		$passwd=func_get_arg(0);
366
		# calcul d'un salt pour forcer le chiffrement en MD5 au lieu de blowfish par defaut dans php version mdva > 2007.1
367
		$salt='$1$passwd$';
368
		if ($numargs == 2){
369
			$salt=func_get_arg(1);
370
			return crypt($passwd,$salt);
371
		}
372
			return crypt($passwd,$salt);
373
	}
374
	private function _init()
375
	{
376
		//TO DO : supprimer les variables globales
377
		global $attrmap, $attr_type, $attr_op;
378
 
379
		foreach ($attrmap as $attr){
380
			if ($attr_type[$attr]=="checkItem"){
381
				$this->checkItems[$attr] = "";
382
			}elseif ($attr_type[$attr]=="replyItem"){
383
				$this->replyItems[$attr] = "";
384
			}
385
			if ($attr_op[$attr] != ""){
386
				$this->op[$attr] = $attr_op[$attr];
387
			} else {
388
				$this->op[$attr] = "=";
389
			}
390
		}		
391
	}
392
	// protected methods
393
	// no protected method
394
}
395
?>