1 |
<?php |
<?php |
2 |
// |
// |
3 |
// +----------------------------------------------------------------------+ |
// +----------------------------------------------------------------------+ |
4 |
// | PHP Version 4 | |
// | PHP Version 4 | |
5 |
// +----------------------------------------------------------------------+ |
// +----------------------------------------------------------------------+ |
6 |
// | | |
// | | |
7 |
// +----------------------------------------------------------------------+ |
// +----------------------------------------------------------------------+ |
8 |
// | This source file is subject to version 2.02 of the PHP license, | |
// | This source file is subject to version 2.02 of the PHP license, | |
9 |
// | that is bundled with this package in the file LICENSE, and is | |
// | that is bundled with this package in the file LICENSE, and is | |
10 |
// | available at through the world-wide-web at | |
// | available at through the world-wide-web at | |
11 |
// | http://www.php.net/license/2_02.txt. | |
// | http://www.php.net/license/2_02.txt. | |
12 |
// | If you did not receive a copy of the PHP license and are unable to | |
// | If you did not receive a copy of the PHP license and are unable to | |
13 |
// | obtain it through the world-wide-web, please send a note to | |
// | obtain it through the world-wide-web, please send a note to | |
14 |
// | license@php.net so we can mail you a copy immediately. | |
// | license@php.net so we can mail you a copy immediately. | |
15 |
// +----------------------------------------------------------------------+ |
// +----------------------------------------------------------------------+ |
16 |
// | Authors: Martin Jansen <mj@php.net> | |
// | Authors: Martin Jansen <mj@php.net> | |
17 |
// +----------------------------------------------------------------------+ |
// +----------------------------------------------------------------------+ |
18 |
// |
// |
19 |
// $Id$ |
// $Id$ |
20 |
// |
// |
21 |
|
|
22 |
//require_once 'Auth/Container.php'; |
//require_once 'Auth/Container.php'; |
23 |
require_once DOL_DOCUMENT_ROOT."/includes/pear/Auth/Container.php"; |
require_once DOL_DOCUMENT_ROOT."/includes/pear/Auth/Container.php"; |
24 |
//require_once 'DB.php'; |
//require_once 'DB.php'; |
25 |
require_once DOL_DOCUMENT_ROOT."/includes/pear/DB.php"; |
require_once DOL_DOCUMENT_ROOT."/includes/pear/DB.php"; |
26 |
|
|
27 |
/** |
/** |
28 |
* Storage driver for fetching login data from a database |
* Storage driver for fetching login data from a database |
29 |
* |
* |
30 |
* This storage driver can use all databases which are supported |
* This storage driver can use all databases which are supported |
31 |
* by the PEAR DB abstraction layer to fetch login data. |
* by the PEAR DB abstraction layer to fetch login data. |
32 |
* |
* |
33 |
* @author Martin Jansen <mj@php.net> |
* @author Martin Jansen <mj@php.net> |
34 |
* @package Auth |
* @package Auth |
35 |
* @version $Revision$ |
* @version $Revision$ |
36 |
*/ |
*/ |
37 |
class Auth_Container_DB extends Auth_Container |
class Auth_Container_DB extends Auth_Container |
38 |
{ |
{ |
39 |
|
|
40 |
/** |
/** |
41 |
* Additional options for the storage container |
* Additional options for the storage container |
42 |
* @var array |
* @var array |
43 |
*/ |
*/ |
44 |
var $options = array(); |
var $options = array(); |
45 |
|
|
46 |
/** |
/** |
47 |
* DB object |
* DB object |
48 |
* @var object |
* @var object |
49 |
*/ |
*/ |
50 |
var $db = null; |
var $db = null; |
51 |
var $dsn = ''; |
var $dsn = ''; |
52 |
|
|
53 |
/** |
/** |
54 |
* User that is currently selected from the DB. |
* User that is currently selected from the DB. |
55 |
* @var string |
* @var string |
56 |
*/ |
*/ |
57 |
var $activeUser = ''; |
var $activeUser = ''; |
58 |
|
|
59 |
// {{{ Constructor |
// {{{ Constructor |
60 |
|
|
61 |
/** |
/** |
62 |
* Constructor of the container class |
* Constructor of the container class |
63 |
* |
* |
64 |
* Initate connection to the database via PEAR::DB |
* Initate connection to the database via PEAR::DB |
65 |
* |
* |
66 |
* @param string Connection data or DB object |
* @param string Connection data or DB object |
67 |
* @return object Returns an error object if something went wrong |
* @return object Returns an error object if something went wrong |
68 |
*/ |
*/ |
69 |
function Auth_Container_DB($dsn) |
function Auth_Container_DB($dsn) |
70 |
{ |
{ |
71 |
$this->_setDefaults(); |
$this->_setDefaults(); |
72 |
|
|
73 |
if (is_array($dsn)) { |
if (is_array($dsn)) { |
74 |
$this->_parseOptions($dsn); |
$this->_parseOptions($dsn); |
75 |
|
|
76 |
if (empty($this->options['dsn'])) { |
if (empty($this->options['dsn'])) { |
77 |
PEAR::raiseError('No connection parameters specified!'); |
PEAR::raiseError('No connection parameters specified!'); |
78 |
} |
} |
79 |
} else { |
} else { |
80 |
$this->options['dsn'] = $dsn; |
$this->options['dsn'] = $dsn; |
81 |
} |
} |
82 |
} |
} |
83 |
|
|
84 |
// }}} |
// }}} |
85 |
// {{{ _connect() |
// {{{ _connect() |
86 |
|
|
87 |
/** |
/** |
88 |
* Connect to database by using the given DSN string |
* Connect to database by using the given DSN string |
89 |
* |
* |
90 |
* @access private |
* @access private |
91 |
* @param string DSN string |
* @param string DSN string |
92 |
* @return mixed Object on error, otherwise bool |
* @return mixed Object on error, otherwise bool |
93 |
*/ |
*/ |
94 |
function _connect($dsn) |
function _connect($dsn) |
95 |
{ |
{ |
96 |
if (is_string($dsn) || is_array($dsn)) { |
if (is_string($dsn) || is_array($dsn)) { |
97 |
$this->db = DB::Connect($dsn); |
$this->db = DB::Connect($dsn); |
98 |
} elseif (get_parent_class($dsn) == "db_common") { |
} elseif (get_parent_class($dsn) == "db_common") { |
99 |
$this->db = $dsn; |
$this->db = $dsn; |
100 |
} elseif (DB::isError($dsn)) { |
} elseif (DB::isError($dsn)) { |
101 |
return PEAR::raiseError($dsn->getMessage(), $dsn->getCode()); |
return PEAR::raiseError($dsn->getMessage(), $dsn->getCode()); |
102 |
} else { |
} else { |
103 |
return PEAR::raiseError('The given dsn was not valid in file ' . __FILE__ . ' at line ' . __LINE__, |
return PEAR::raiseError('The given dsn was not valid in file ' . __FILE__ . ' at line ' . __LINE__, |
104 |
41, |
41, |
105 |
PEAR_ERROR_RETURN, |
PEAR_ERROR_RETURN, |
106 |
null, |
null, |
107 |
null |
null |
108 |
); |
); |
109 |
} |
} |
110 |
|
|
111 |
if (DB::isError($this->db) || DOLIPEAR::isError($this->db)) { |
if (DB::isError($this->db) || DOLIPEAR::isError($this->db)) { |
112 |
return DOLIPEAR::raiseError($this->db->getMessage(), $this->db->getCode()); |
return DOLIPEAR::raiseError($this->db->getMessage(), $this->db->getCode()); |
113 |
} else { |
} else { |
114 |
return true; |
return true; |
115 |
} |
} |
116 |
} |
} |
117 |
|
|
118 |
// }}} |
// }}} |
119 |
// {{{ _prepare() |
// {{{ _prepare() |
120 |
|
|
121 |
/** |
/** |
122 |
* Prepare database connection |
* Prepare database connection |
123 |
* |
* |
124 |
* This function checks if we have already opened a connection to |
* This function checks if we have already opened a connection to |
125 |
* the database. If that's not the case, a new connection is opened. |
* the database. If that's not the case, a new connection is opened. |
126 |
* |
* |
127 |
* @access private |
* @access private |
128 |
* @return mixed True or a DB error object. |
* @return mixed True or a DB error object. |
129 |
*/ |
*/ |
130 |
function _prepare() |
function _prepare() |
131 |
{ |
{ |
132 |
if (!DB::isConnection($this->db)) { |
if (!DB::isConnection($this->db)) { |
133 |
$res = $this->_connect($this->options['dsn']); |
$res = $this->_connect($this->options['dsn']); |
134 |
if(DB::isError($res) || DOLIPEAR::isError($res)){ |
if(DB::isError($res) || DOLIPEAR::isError($res)){ |
135 |
return $res; |
return $res; |
136 |
} |
} |
137 |
} |
} |
138 |
return true; |
return true; |
139 |
} |
} |
140 |
|
|
141 |
// }}} |
// }}} |
142 |
// {{{ query() |
// {{{ query() |
143 |
|
|
144 |
/** |
/** |
145 |
* Prepare query to the database |
* Prepare query to the database |
146 |
* |
* |
147 |
* This function checks if we have already opened a connection to |
* This function checks if we have already opened a connection to |
148 |
* the database. If that's not the case, a new connection is opened. |
* the database. If that's not the case, a new connection is opened. |
149 |
* After that the query is passed to the database. |
* After that the query is passed to the database. |
150 |
* |
* |
151 |
* @access public |
* @access public |
152 |
* @param string Query string |
* @param string Query string |
153 |
* @return mixed a DB_result object or DB_OK on success, a DB |
* @return mixed a DB_result object or DB_OK on success, a DB |
154 |
* or PEAR error on failure |
* or PEAR error on failure |
155 |
*/ |
*/ |
156 |
function query($query) |
function query($query) |
157 |
{ |
{ |
158 |
$err = $this->_prepare(); |
$err = $this->_prepare(); |
159 |
if ($err !== true) { |
if ($err !== true) { |
160 |
return $err; |
return $err; |
161 |
} |
} |
162 |
return $this->db->query($query); |
return $this->db->query($query); |
163 |
} |
} |
164 |
|
|
165 |
// }}} |
// }}} |
166 |
// {{{ _setDefaults() |
// {{{ _setDefaults() |
167 |
|
|
168 |
/** |
/** |
169 |
* Set some default options |
* Set some default options |
170 |
* |
* |
171 |
* @access private |
* @access private |
172 |
* @return void |
* @return void |
173 |
*/ |
*/ |
174 |
function _setDefaults() |
function _setDefaults() |
175 |
{ |
{ |
176 |
$this->options['table'] = 'auth'; |
$this->options['table'] = 'auth'; |
177 |
$this->options['usernamecol'] = 'username'; |
$this->options['usernamecol'] = 'username'; |
178 |
$this->options['passwordcol'] = 'password'; |
$this->options['passwordcol'] = 'password'; |
179 |
$this->options['dsn'] = ''; |
$this->options['dsn'] = ''; |
180 |
$this->options['db_fields'] = ''; |
$this->options['db_fields'] = ''; |
181 |
$this->options['cryptType'] = 'md5'; |
$this->options['cryptType'] = 'md5'; |
182 |
} |
} |
183 |
|
|
184 |
// }}} |
// }}} |
185 |
// {{{ _parseOptions() |
// {{{ _parseOptions() |
186 |
|
|
187 |
/** |
/** |
188 |
* Parse options passed to the container class |
* Parse options passed to the container class |
189 |
* |
* |
190 |
* @access private |
* @access private |
191 |
* @param array |
* @param array |
192 |
*/ |
*/ |
193 |
function _parseOptions($array) |
function _parseOptions($array) |
194 |
{ |
{ |
195 |
foreach ($array as $key => $value) { |
foreach ($array as $key => $value) { |
196 |
if (isset($this->options[$key])) { |
if (isset($this->options[$key])) { |
197 |
$this->options[$key] = $value; |
$this->options[$key] = $value; |
198 |
} |
} |
199 |
} |
} |
200 |
|
|
201 |
/* Include additional fields if they exist */ |
/* Include additional fields if they exist */ |
202 |
if(!empty($this->options['db_fields'])){ |
if(!empty($this->options['db_fields'])){ |
203 |
if(is_array($this->options['db_fields'])){ |
if(is_array($this->options['db_fields'])){ |
204 |
$this->options['db_fields'] = join($this->options['db_fields'], ', '); |
$this->options['db_fields'] = join($this->options['db_fields'], ', '); |
205 |
} |
} |
206 |
$this->options['db_fields'] = ', '.$this->options['db_fields']; |
$this->options['db_fields'] = ', '.$this->options['db_fields']; |
207 |
} |
} |
208 |
} |
} |
209 |
|
|
210 |
// }}} |
// }}} |
211 |
// {{{ fetchData() |
// {{{ fetchData() |
212 |
|
|
213 |
/** |
/** |
214 |
* Get user information from database |
* Get user information from database |
215 |
* |
* |
216 |
* This function uses the given username to fetch |
* This function uses the given username to fetch |
217 |
* the corresponding login data from the database |
* the corresponding login data from the database |
218 |
* table. If an account that matches the passed username |
* table. If an account that matches the passed username |
219 |
* and password is found, the function returns true. |
* and password is found, the function returns true. |
220 |
* Otherwise it returns false. |
* Otherwise it returns false. |
221 |
* |
* |
222 |
* @param string Username |
* @param string Username |
223 |
* @param string Password |
* @param string Password |
224 |
* @return mixed Error object or boolean |
* @return mixed Error object or boolean |
225 |
*/ |
*/ |
226 |
function fetchData($username, $password) |
function fetchData($username, $password) |
227 |
{ |
{ |
228 |
// Prepare for a database query |
// Prepare for a database query |
229 |
$err = $this->_prepare(); |
$err = $this->_prepare(); |
230 |
if ($err !== true) { |
if ($err !== true) { |
231 |
return PEAR::raiseError($err->getMessage(), $err->getCode()); |
return PEAR::raiseError($err->getMessage(), $err->getCode()); |
232 |
} |
} |
233 |
|
|
234 |
// Find if db_fileds contains a *, i so assume all col are selected |
// Find if db_fileds contains a *, i so assume all col are selected |
235 |
if(strstr($this->options['db_fields'], '*')){ |
if(strstr($this->options['db_fields'], '*')){ |
236 |
$sql_from = "*"; |
$sql_from = "*"; |
237 |
} |
} |
238 |
else{ |
else{ |
239 |
$sql_from = $this->options['usernamecol'] . ", ".$this->options['passwordcol'].$this->options['db_fields']; |
$sql_from = $this->options['usernamecol'] . ", ".$this->options['passwordcol'].$this->options['db_fields']; |
240 |
} |
} |
241 |
|
|
242 |
$query = "SELECT ! FROM ! WHERE ! = ?"; |
$query = "SELECT ! FROM ! WHERE ! = ?"; |
243 |
$query_params = array( |
$query_params = array( |
244 |
$sql_from, |
$sql_from, |
245 |
$this->options['table'], |
$this->options['table'], |
246 |
$this->options['usernamecol'], |
$this->options['usernamecol'], |
247 |
$username |
$username |
248 |
); |
); |
249 |
$res = $this->db->getRow($query, $query_params, DB_FETCHMODE_ASSOC); |
$res = $this->db->getRow($query, $query_params, DB_FETCHMODE_ASSOC); |
250 |
|
|
251 |
if (DB::isError($res)) { |
if (DB::isError($res)) { |
252 |
return PEAR::raiseError($res->getMessage(), $res->getCode()); |
return PEAR::raiseError($res->getMessage(), $res->getCode()); |
253 |
} |
} |
254 |
if (!is_array($res)) { |
if (!is_array($res)) { |
255 |
$this->activeUser = ''; |
$this->activeUser = ''; |
256 |
return false; |
return false; |
257 |
} |
} |
258 |
if ($this->verifyPassword(trim($password), |
if ($this->verifyPassword(trim($password), |
259 |
trim($res[$this->options['passwordcol']]), |
trim($res[$this->options['passwordcol']]), |
260 |
$this->options['cryptType'])) { |
$this->options['cryptType'])) { |
261 |
// Store additional field values in the session |
// Store additional field values in the session |
262 |
foreach ($res as $key => $value) { |
foreach ($res as $key => $value) { |
263 |
if ($key == $this->options['passwordcol'] || |
if ($key == $this->options['passwordcol'] || |
264 |
$key == $this->options['usernamecol']) { |
$key == $this->options['usernamecol']) { |
265 |
continue; |
continue; |
266 |
} |
} |
267 |
Auth::setAuthData($key, $value); |
Auth::setAuthData($key, $value); |
268 |
} |
} |
269 |
|
|
270 |
return true; |
return true; |
271 |
} |
} |
272 |
|
|
273 |
$this->activeUser = $res[$this->options['usernamecol']]; |
$this->activeUser = $res[$this->options['usernamecol']]; |
274 |
return false; |
return false; |
275 |
} |
} |
276 |
|
|
277 |
// }}} |
// }}} |
278 |
// {{{ listUsers() |
// {{{ listUsers() |
279 |
|
|
280 |
function listUsers() |
function listUsers() |
281 |
{ |
{ |
282 |
$err = $this->_prepare(); |
$err = $this->_prepare(); |
283 |
if ($err !== true) { |
if ($err !== true) { |
284 |
return PEAR::raiseError($err->getMessage(), $err->getCode()); |
return PEAR::raiseError($err->getMessage(), $err->getCode()); |
285 |
} |
} |
286 |
|
|
287 |
$retVal = array(); |
$retVal = array(); |
288 |
|
|
289 |
// Find if db_fileds contains a *, i so assume all col are selected |
// Find if db_fileds contains a *, i so assume all col are selected |
290 |
if(strstr($this->options['db_fields'], '*') || empty($this->options['db_fields'])){ |
if(strstr($this->options['db_fields'], '*') || empty($this->options['db_fields'])){ |
291 |
$sql_from = "*"; |
$sql_from = "*"; |
292 |
} |
} |
293 |
else{ |
else{ |
294 |
$sql_from = $this->options['usernamecol'] . ", ".$this->options['passwordcol'].$this->options['db_fields']; |
$sql_from = $this->options['usernamecol'] . ", ".$this->options['passwordcol'].$this->options['db_fields']; |
295 |
} |
} |
296 |
|
|
297 |
$query = sprintf("SELECT %s FROM %s", |
$query = sprintf("SELECT %s FROM %s", |
298 |
$sql_from, |
$sql_from, |
299 |
$this->options['table'] |
$this->options['table'] |
300 |
); |
); |
301 |
$res = $this->db->getAll($query, null, DB_FETCHMODE_ASSOC); |
$res = $this->db->getAll($query, null, DB_FETCHMODE_ASSOC); |
302 |
|
|
303 |
if (DB::isError($res)) { |
if (DB::isError($res)) { |
304 |
return PEAR::raiseError($res->getMessage(), $res->getCode()); |
return PEAR::raiseError($res->getMessage(), $res->getCode()); |
305 |
} else { |
} else { |
306 |
foreach ($res as $user) { |
foreach ($res as $user) { |
307 |
$user['username'] = $user[$this->options['usernamecol']]; |
$user['username'] = $user[$this->options['usernamecol']]; |
308 |
$retVal[] = $user; |
$retVal[] = $user; |
309 |
} |
} |
310 |
} |
} |
311 |
return $retVal; |
return $retVal; |
312 |
} |
} |
313 |
|
|
314 |
// }}} |
// }}} |
315 |
// {{{ addUser() |
// {{{ addUser() |
316 |
|
|
317 |
/** |
/** |
318 |
* Add user to the storage container |
* Add user to the storage container |
319 |
* |
* |
320 |
* @access public |
* @access public |
321 |
* @param string Username |
* @param string Username |
322 |
* @param string Password |
* @param string Password |
323 |
* @param mixed Additional information that are stored in the DB |
* @param mixed Additional information that are stored in the DB |
324 |
* |
* |
325 |
* @return mixed True on success, otherwise error object |
* @return mixed True on success, otherwise error object |
326 |
*/ |
*/ |
327 |
function addUser($username, $password, $additional = "") |
function addUser($username, $password, $additional = "") |
328 |
{ |
{ |
329 |
if (function_exists($this->options['cryptType'])) { |
if (function_exists($this->options['cryptType'])) { |
330 |
$cryptfunction = $this->options['cryptType']; |
$cryptfunction = $this->options['cryptType']; |
331 |
} else { |
} else { |
332 |
$cryptfunction = 'md5'; |
$cryptfunction = 'md5'; |
333 |
} |
} |
334 |
|
|
335 |
$additional_key = ''; |
$additional_key = ''; |
336 |
$additional_value = ''; |
$additional_value = ''; |
337 |
|
|
338 |
if (is_array($additional)) { |
if (is_array($additional)) { |
339 |
foreach ($additional as $key => $value) { |
foreach ($additional as $key => $value) { |
340 |
$additional_key .= ', ' . $key; |
$additional_key .= ', ' . $key; |
341 |
$additional_value .= ", '" . $value . "'"; |
$additional_value .= ", '" . $value . "'"; |
342 |
} |
} |
343 |
} |
} |
344 |
|
|
345 |
$query = sprintf("INSERT INTO %s (%s, %s%s) VALUES ('%s', '%s'%s)", |
$query = sprintf("INSERT INTO %s (%s, %s%s) VALUES ('%s', '%s'%s)", |
346 |
$this->options['table'], |
$this->options['table'], |
347 |
$this->options['usernamecol'], |
$this->options['usernamecol'], |
348 |
$this->options['passwordcol'], |
$this->options['passwordcol'], |
349 |
$additional_key, |
$additional_key, |
350 |
$username, |
$username, |
351 |
$cryptfunction($password), |
$cryptfunction($password), |
352 |
$additional_value |
$additional_value |
353 |
); |
); |
354 |
|
|
355 |
$res = $this->query($query); |
$res = $this->query($query); |
356 |
|
|
357 |
if (DB::isError($res)) { |
if (DB::isError($res)) { |
358 |
return PEAR::raiseError($res->getMessage(), $res->getCode()); |
return PEAR::raiseError($res->getMessage(), $res->getCode()); |
359 |
} else { |
} else { |
360 |
return true; |
return true; |
361 |
} |
} |
362 |
} |
} |
363 |
|
|
364 |
// }}} |
// }}} |
365 |
// {{{ removeUser() |
// {{{ removeUser() |
366 |
|
|
367 |
/** |
/** |
368 |
* Remove user from the storage container |
* Remove user from the storage container |
369 |
* |
* |
370 |
* @access public |
* @access public |
371 |
* @param string Username |
* @param string Username |
372 |
* |
* |
373 |
* @return mixed True on success, otherwise error object |
* @return mixed True on success, otherwise error object |
374 |
*/ |
*/ |
375 |
function removeUser($username) |
function removeUser($username) |
376 |
{ |
{ |
377 |
$query = sprintf("DELETE FROM %s WHERE %s = '%s'", |
$query = sprintf("DELETE FROM %s WHERE %s = '%s'", |
378 |
$this->options['table'], |
$this->options['table'], |
379 |
$this->options['usernamecol'], |
$this->options['usernamecol'], |
380 |
$username |
$username |
381 |
); |
); |
382 |
|
|
383 |
$res = $this->query($query); |
$res = $this->query($query); |
384 |
|
|
385 |
if (DB::isError($res)) { |
if (DB::isError($res)) { |
386 |
return PEAR::raiseError($res->getMessage(), $res->getCode()); |
return PEAR::raiseError($res->getMessage(), $res->getCode()); |
387 |
} else { |
} else { |
388 |
return true; |
return true; |
389 |
} |
} |
390 |
} |
} |
391 |
|
|
392 |
// }}} |
// }}} |
393 |
} |
} |
394 |
?> |
?> |