Subversion Repositories ALCASAR

Rev

Rev 602 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 602 Rev 610
1
<?php
1
<?php
2
/*
2
/*
3
 
3
 
4
*/
4
*/
5
require_once('mysql.class.php');// the mysql class in already OK!
5
require_once('mysql.class.php');// the mysql class in already OK!
6
 
6
 
7
class radiusMysqlUser
7
class radiusMysqlUser
8
{
8
{
9
	// public properties
9
	// public properties
10
	// no public properties
10
	// no public properties
11
	
11
	
12
	// private properties
12
	// private properties
13
	private $database	= null;
13
	private $database	= null;
14
	private $username	= null;
14
	private $username	= null;
15
	private $userid		= 0;
15
	private $userid		= 0;
16
	private $userInfos	= Array("Username"=>"","Name"=>"","Mail"=>"","Department"=>"","WorkPhone"=>"","HomePhone"=>"","Mobile"=>"");
16
	private $userInfos	= Array("Username"=>"","Name"=>"","Mail"=>"","Department"=>"","WorkPhone"=>"","HomePhone"=>"","Mobile"=>"");
17
	private $checkItems	= Array();
17
	private $checkItems	= Array();
18
	private $replyItems	= Array();
18
	private $replyItems	= Array();
19
	
19
	
20
	//TO DO : init $userInfos, $checkItems and $replyItems fields and operator from config file !!!! URGENT
20
	//TO DO : init $userInfos, $checkItems and $replyItems fields and operator from config file !!!! URGENT
21
	
21
	
22
	
22
	
23
	// protected properties
23
	// protected properties
24
	// no protected properties
24
	// no protected properties
25
	
25
	
26
	// Class constructor
26
	// Class constructor
27
	public function __construct($dbOptions = Array(), $attributeConfig = Array())
27
	public function __construct($dbOptions = Array(), $attributeConfig = Array())
28
	{
28
	{
29
		/*
29
		/*
30
		Db init and config init to do!
30
		Db init and config init to do!
31
		*/
31
		*/
32
		$this->database = new mysql("127.0.0.1","root","","radius");
32
		$this->database = new mysql("127.0.0.1","root","","radius");
33
	}
33
	}
34
	// Class destructor
34
	// Class destructor
35
	public function __destruct()
35
	public function __destruct()
36
	{
36
	{
37
		//$this->mysql->close();	//is private !
37
		//$this->mysql->close();	//is private !
38
		$this->database = null;
38
		$this->database = null;
39
	}
39
	}
40
	// public methods
40
	// public methods
41
	public static function find($options = Array(), $escape=false)
41
	public static function find($options = Array(), $escape=false)
42
	{
42
	{
43
		$database = new mysql("127.0.0.1","root","","radius");
43
		$database = new mysql("127.0.0.1","root","","radius");
44
		/*
44
		/*
45
		If the options are not xss clean, escape all options string by calling _escapeDatas() method.
45
		If the options are not xss clean, escape all options string by calling _escapeDatas() method.
46
		*/
46
		*/
47
		if ($escape == true) { 
47
		if ($escape == true) { 
48
			//$this->_extractArray($options, true); //create variable from $options array and get xss clean for mysql database
48
			//$this->_extractArray($options, true); //create variable from $options array and get xss clean for mysql database
49
			$options = $this->_escapeDatas($options); //create variable from $options array and get xss clean for mysql database
49
			$options = $this->_escapeDatas($options); //create variable from $options array and get xss clean for mysql database
50
		}
50
		}
51
		/*
51
		/*
52
		The differents $options values are :
52
		The differents $options values are :
53
		
53
		
54
		$distinct	-> only distinct response ?
54
		$distinct	-> only distinct response ?
55
		$username	-> only for this username
55
		$username	-> only for this username
56
		$fields		-> fields to return (default : username)
56
		$fields		-> fields to return (default : username)
57
		$search		-> search value to find
57
		$search		-> search value to find
58
		$search_IN	-> search in this/those field(s)(text or array)
58
		$search_IN	-> search in this/those field(s)(text or array)
59
		$limit		-> to limit the resultset
59
		$limit		-> to limit the resultset
60
		$offset		-> offset (work with $limit for pagination)
60
		$offset		-> offset (work with $limit for pagination)
61
		$sortby		-> sort by x field (default : no sorting)
61
		$sortby		-> sort by x field (default : no sorting)
62
		$sortdir	-> sort direction (ASC/DESC) (default : no sorting)
62
		$sortdir	-> sort direction (ASC/DESC) (default : no sorting)
63
		$radius_attr-> radius attribute to find (text or array) if search_IN = radius
63
		$radius_attr-> radius attribute to find (text or array) if search_IN = radius
64
		*/
64
		*/
65
		
65
		
66
		//mysql_real_escape_string
66
		//mysql_real_escape_string
67
		
67
		
68
		
68
		
69
		$sql = "SELECT ";
69
		$sql = "SELECT ";
70
		// distinct option
70
		// distinct option
71
		if ((isset($distinct))&&($distinct=="distinct"))
71
		if ((isset($distinct))&&($distinct=="distinct"))
72
			$sql .= "DISTINCT ";
72
			$sql .= "DISTINCT ";
73
		// field option (make sure that the field exist!)
73
		// field option (make sure that the field exist!)
74
		if ((isset($options['fields']))&&($options['fields']!='')){
74
		if ((isset($options['fields']))&&($options['fields']!='')){
75
			$sql .= $options['fields'].", username ";
75
			$sql .= $options['fields'].", username ";
76
		}else{
76
		}else{
77
			$sql .= "username ";
77
			$sql .= "username ";
78
		}
78
		}
79
		$sql .= "FROM userinfo ";
79
		$sql .= "FROM userinfo ";
80
		// search option
80
		// search option
81
		if ((isset($options['username']))&&($options['username']!='')){
81
		if ((isset($options['username']))&&($options['username']!='')){
82
		
82
		
83
		}
83
		}
84
		// where option
84
		// where option
85
		if ((isset($options['username']))&&($options['username']!=""))
85
		if ((isset($options['username']))&&($options['username']!=""))
86
		{
86
		{
87
			$sql .= "WHERE username='".$options['username']."'";
87
			$sql .= "WHERE username='".$options['username']."'";
88
			$this->username = $options['username'];
88
			$this->username = $options['username'];
89
		}
89
		}
90
		// sort
90
		// sort
91
		if ((isset($options['sortby']))&&($options['sortby']!='')){
91
		if ((isset($options['sortby']))&&($options['sortby']!='')){
92
			$sql .= "ORDER BY ".$options['sortby']." ";
92
			$sql .= "ORDER BY ".$options['sortby']." ";
93
			if ((isset($options['sortdir']))&&($options['sortdir']!='')){
93
			if ((isset($options['sortdir']))&&($options['sortdir']!='')){
94
				$sql .= "LIMIT ".$options['sortdir']." ";
94
				$sql .= "LIMIT ".$options['sortdir']." ";
95
			}
95
			}
96
		}
96
		}
97
		// limit / offset
97
		// limit / offset
98
		if ((isset($options['limit']))&&($options['limit']!='')){
98
		if ((isset($options['limit']))&&($options['limit']!='')){
99
			if ((isset($options['offset']))&&($options['offset']!='')){
99
			if ((isset($options['offset']))&&($options['offset']!='')){
100
				$sql .= "LIMIT $offset $limit ";
100
				$sql .= "LIMIT $offset $limit ";
101
			} else {
101
			} else {
102
				$sql .= "LIMIT $limit ";
102
				$sql .= "LIMIT $limit ";
103
			}
103
			}
104
			
104
			
105
		}
105
		}
106
		$sql .= ";";
106
		$sql .= ";";
107
 
107
 
108
		// query
108
		// query
109
		$result = $database->query($sql);
109
		$result = $database->query($sql);
110
		// return the result values
110
		// return the result values
111
		return $result;
111
		return $result;
112
	}
112
	}
113
	public function load($username, $attribute = false) //ok
113
	public function load($username, $attribute = false) //ok
114
	{
114
	{
115
		/*
115
		/*
116
		Load an user from mysql database. If $attribute==true, get all chekitems and replyitems attributes too.
116
		Load an user from mysql database. If $attribute==true, get all chekitems and replyitems attributes too.
117
		*/
117
		*/
118
		$sql = "SELECT * FROM userinfo WHERE UserName='$username';";
118
		$sql = "SELECT * FROM userinfo WHERE UserName='$username';";
119
		$result = $this->database->query($sql);
119
		$result = $this->database->query($sql);
120
	
120
	
121
		if (count($result) != 1) return false;
121
		if (count($result) != 1) return false;
122
		
122
		
123
		$this->userInfos = $result[0];
123
		$this->userInfos = $result[0];
124
		
124
		
125
		if ($attribute === true){
125
		if ($attribute === true){
126
		
126
		
127
			// get from radcheck table
127
			// get from radcheck table
128
			$result=null;
128
			$result=null;
129
			$sql = "SELECT * FROM radcheck WHERE username='$username';";
129
			$sql = "SELECT * FROM radcheck WHERE username='$username';";
130
			$result = $this->database->query($sql);
130
			$result = $this->database->query($sql);
131
			if (count($result) != 1) return false;
131
			if (count($result) != 1) return false;
132
			$this->checkItems = $result[0];
132
			$this->checkItems = $result[0];
133
			
133
			
134
			// get from radreply table
134
			// get from radreply table
135
			$result=null;
135
			$result=null;
136
			$sql = "SELECT * FROM radreply WHERE username='$username';";
136
			$sql = "SELECT * FROM radreply WHERE username='$username';";
137
			$result = $this->database->query($sql);
137
			$result = $this->database->query($sql);
138
			if (count($result) != 1) return false;
138
			if (count($result) != 1) return false;
139
			$this->replyItems = $result[0];
139
			$this->replyItems = $result[0];
140
		}
140
		}
141
		
141
		
142
		return true;
142
		return true;
143
	}
143
	}
144
	public function add()
144
	public function add()
145
	{
145
	{
146
		/*
146
		/*
147
		Add the current user with all his attribute in the mysql database
147
		Add the current user with all his attribute in the mysql database
148
		(only if the user not already exist)
148
		(only if the user not already exist)
149
		*/
149
		*/
150
		$sql = "";
150
		$sql = "";
151
		//INSERT INTO table (a,b,c) VALUES (1,2,3)
151
		//INSERT INTO table (a,b,c) VALUES (1,2,3)
152
		
152
		
153
		//INSERT userinfo table (insert)
153
		//INSERT userinfo table (insert)
154
		$sql = "INSERT INTO userinfo (UserName, Name, Mail, Department, WorkPhone, HomePhone, Mobile) VALUES ()";
154
		$sql = "INSERT INTO userinfo (UserName, Name, Mail, Department, WorkPhone, HomePhone, Mobile) VALUES ()";
155
		//INSERT radcheck table (insert)
155
		//INSERT radcheck table (insert)
156
		$sql = "";
156
		$sql = "";
157
		//INSERT radreply table (insert)
157
		//INSERT radreply table (insert)
158
		$sql = "";
158
		$sql = "";
159
		//INSERT radusergroup table (insert)
159
		//INSERT radusergroup table (insert)
160
		$sql = "";
160
		$sql = "";
161
		//INSERT radpostauth table (insert)
161
		//INSERT radpostauth table (insert)
162
		
162
		
163
		
163
		
164
	}
164
	}
165
	public function delete() //ok
165
	public function delete() //ok
166
	{
166
	{
167
		/*
167
		/*
168
		Delete the current user from the mysql database
168
		Delete the current user from the mysql database
169
		note : this function doesn't delete any accounting record of the current user
169
		note : this function doesn't delete any accounting record of the current user
170
		*/
170
		*/
171
		if ($this->userid == 0) return 0; //0 record deleted
171
		if ($this->userid == 0) return 0; //0 record deleted
172
		
172
		
173
		//can be better with transaction
173
		//can be better with transaction
174
		$sql1 = "DELETE FROM radreply WHERE username = $this->username ;";
174
		$sql1 = "DELETE FROM radreply WHERE username = $this->username ;";
175
		$sql2 = "DELETE FROM radcheck WHERE username = $this->username ;";
175
		$sql2 = "DELETE FROM radcheck WHERE username = $this->username ;";
176
		$sql3 = "DELETE FROM radpostauth WHERE username = $this->username ;";
176
		$sql3 = "DELETE FROM radpostauth WHERE username = $this->username ;";
177
		$sql4 = "DELETE FROM radusergroup WHERE username = $this->username ;";
177
		$sql4 = "DELETE FROM radusergroup WHERE username = $this->username ;";
178
		$sql5 = "DELETE FROM userinfo WHERE username = $this->username ;";
178
		$sql5 = "DELETE FROM userinfo WHERE username = $this->username ;";
179
		
179
		
180
		$nb1 = $this->database->exec($sql1);
180
		$nb1 = $this->database->exec($sql1);
181
		$nb2 = $this->database->exec($sql2);
181
		$nb2 = $this->database->exec($sql2);
182
		$nb3 = $this->database->exec($sql3);
182
		$nb3 = $this->database->exec($sql3);
183
		$nb4 = $this->database->exec($sql4);
183
		$nb4 = $this->database->exec($sql4);
184
		$nb5 = $this->database->exec($sql5);
184
		$nb5 = $this->database->exec($sql5);
185
		
185
		
186
		return ($nb1+$nb2+$nb3+$nb4+$nb5); // n record deleted
186
		return ($nb1+$nb2+$nb3+$nb4+$nb5); // n record deleted
187
	}
187
	}
188
	public function update()
188
	public function update()
189
	{
189
	{
190
		/*
190
		/*
191
		Update the current user with all his attribute in the mysql database
191
		Update the current user with all his attribute in the mysql database
192
		(only if the user does not already exist)
192
		(only if the user does not already exist)
193
		*/
193
		*/
194
		if ($this->userid == 0) return 0; //0 record deleted
194
		if ($this->userid == 0) return 0; //0 record deleted
195
		
195
		
196
		//UPDATE userinfo table (update)
196
		//UPDATE userinfo table (update)
197
		
197
		
198
		//UPDATE radcheck table (update)
198
		//UPDATE radcheck table (update)
199
		
199
		
200
		//UPDATE radreply table (update)
200
		//UPDATE radreply table (update)
201
		
201
		
202
		//UPDATE radusergroup table (update)
202
		//UPDATE radusergroup table (update)
203
		
203
		
204
		//UPDATE radpostauth table (update)
204
		//UPDATE radpostauth table (update)
205
	}
205
	}
206
	public function save() //ok
206
	public function save() //ok
207
	{
207
	{
208
		/*
208
		/*
209
		insert or Update the current user with all his attribute in the mysql database
209
		insert or Update the current user with all his attribute in the mysql database
210
		(use add() and update() method)
210
		(use add() and update() method)
211
		*/
211
		*/
212
		if ($this->userid == 0){
212
		if ($this->userid == 0){
213
			return $this->add();
213
			return $this->add();
214
		} else {
214
		} else {
215
			return $this->update();
215
			return $this->update();
216
		}
216
		}
217
	}
217
	}
218
	public function get($userInfo = 'null') //ok
218
	public function get($userInfo = 'null') //ok
219
	{
219
	{
220
		/*
220
		/*
221
		return userInfos
221
		return userInfos
222
		*/
222
		*/
223
		if (array_key_exists($userInfo, $this->userInfos)){
223
		if (array_key_exists($userInfo, $this->userInfos)){
224
			return $this->userInfos[$userInfo];
224
			return $this->userInfos[$userInfo];
225
		} else {
225
		} else {
226
			return $this->userInfos;
226
			return $this->userInfos;
227
		}
227
		}
228
	}
228
	}
229
	public function set($userInfo) //ok
229
	public function set($userInfo) //ok
230
	{
230
	{
231
		/*
231
		/*
232
		Set a checkItem or replyItem of the user
232
		Set a value in userInfos
233
		*/
233
		*/
234
		if (array_key_exists($userInfo, $this->userInfos)){
234
		if (array_key_exists($userInfo, $this->userInfos)){
235
			$this->userInfos[$userInfo] = $userInfo;
235
			$this->userInfos[$userInfo] = $userInfo;
236
		}
236
		}
237
		if (strtolower($userInfo) == "username") $this->username = $userInfo;
237
		if (strtolower($userInfo) == "username") $this->username = $userInfo;
238
	}
238
	}
239
	public function getAttribute($attribute = null)
239
	public function getAttribute($attribute = null)
240
	{
240
	{
241
		/*
241
		/*
242
		Get a checkItem or replyItem from the user or get the value from the mysql database
242
		Get a checkItem or replyItem from the user or get the value from the mysql database
243
		*/
243
		*/
244
		if (array_key_exists($attribute, $this->userInfos)){
244
		if (array_key_exists($attribute, $this->userInfos)){
245
		
245
		
246
		} elseif (array_key_exists($attribute, $this->checkItems)){
246
		} elseif (array_key_exists($attribute, $this->checkItems)){
247
		
247
		
248
		} elseif (array_key_exists($attribute, $this->replyItems)){
248
		} elseif (array_key_exists($attribute, $this->replyItems)){
249
		
249
		
250
		} else{
250
		} else{
251
		
251
		
252
		}
252
		}
253
	}
253
	}
254
	public function setAttribute($attribute)
254
	public function setAttribute($attribute)
255
	{
255
	{
256
		/*
256
		/*
257
		Set a checkItem or replyItem of the user
257
		Set a checkItem or replyItem of the user
258
		*/
258
		*/
259
	}
259
	}
260
	public function checkPassword($pwd)
260
	public function checkPassword($pwd)
261
	{
261
	{
262
		//	Check the user password
262
		//	Check the user password
263
		//	Return true or false
263
		//	Return true or false
264
	}
264
	}
265
	public function setPassword($pwd)
265
	public function setPassword($pwd)
266
	{
266
	{
267
		//	Set or change the user password
267
		//	Set or change the user password
268
	}
268
	}
269
	// private methods
269
	// private methods
270
	private function _escapeDatas($options)
270
	private function _escapeDatas($options)
271
	{
271
	{
272
	
272
	
273
	}
273
	}
274
	private function _init($configFile)
274
	private function _init($configFile)
275
	{
275
	{
276
	
276
	
277
	}
277
	}
278
	// protected methods
278
	// protected methods
279
	// no protected method
279
	// no protected method
280
}
280
}
281
?>
281
?>
282
 
282
 
283

Generated by GNU Enscript 1.6.6.
283

Generated by GNU Enscript 1.6.6.
284
 
284
 
285
 
285
 
286
 
286