29 require_once
'Auth/Container.php';
33 require_once
'MDB.php';
92 if (empty($this->options[
'dsn'])) {
96 $this->options[
'dsn'] =
$dsn;
113 if (is_string(
$dsn) || is_array(
$dsn)) {
114 $this->db =& MDB::connect(
$dsn, $this->options[
'db_options']);
115 } elseif (is_subclass_of(
$dsn,
'mdb_common')) {
117 } elseif (is_object(
$dsn) && MDB::isError(
$dsn)) {
120 return PEAR::raiseError(
'The given dsn was not valid in file ' . __FILE__ .
' at line ' . __LINE__,
133 if ($this->options[
'auto_quote']) {
134 if (strpos(
'.', $this->options[
'table']) ===
false) {
135 $this->options[
'final_table'] = $this->db->quoteIdentifier($this->options[
'table']);
137 $t = explode(
'.', $this->options[
'table']);
138 for ($i = 0, $count = count(
$t); $i < $count; $i++)
139 $t[$i] = $this->db->quoteIdentifier(
$t[$i]);
140 $this->options[
'final_table'] = implode(
'.',
$t);
142 $this->options[
'final_usernamecol'] = $this->db->quoteIdentifier($this->options[
'usernamecol']);
143 $this->options[
'final_passwordcol'] = $this->db->quoteIdentifier($this->options[
'passwordcol']);
145 $this->options[
'final_table'] = $this->options[
'table'];
146 $this->options[
'final_usernamecol'] = $this->options[
'usernamecol'];
147 $this->options[
'final_passwordcol'] = $this->options[
'passwordcol'];
167 if (is_subclass_of($this->db,
'mdb_common')) {
170 return $this->
_connect($this->options[
'dsn']);
195 return $this->db->query(
$query);
209 $this->options[
'table'] =
'auth';
210 $this->options[
'usernamecol'] =
'username';
211 $this->options[
'passwordcol'] =
'password';
212 $this->options[
'dsn'] =
'';
213 $this->options[
'db_fields'] =
'';
214 $this->options[
'cryptType'] =
'md5';
215 $this->options[
'db_options'] = array();
216 $this->options[
'db_where'] =
'';
217 $this->options[
'auto_quote'] =
true;
231 foreach ($array as $key => $value) {
232 if (isset($this->options[$key])) {
233 $this->options[$key] = $value;
250 if (isset($this->options[
'db_fields'])) {
251 if (is_array($this->options[
'db_fields'])) {
252 if ($this->options[
'auto_quote']) {
254 foreach ($this->options[
'db_fields'] as $field) {
255 $fields[] = $this->db->quoteIdentifier($field);
257 return implode(
', ', $fields);
259 return implode(
', ', $this->options[
'db_fields']);
262 if (strlen($this->options[
'db_fields']) > 0) {
263 if ($this->options[
'auto_quote']) {
264 return $this->db->quoteIdentifier($this->options[
'db_fields']);
266 return $this->options[
'db_fields'];
294 function fetchData($username, $password, $isChallengeResponse=
false)
304 if (is_string($this->options[
'db_fields'])
305 && strstr($this->options[
'db_fields'],
'*')) {
308 $sql_from = $this->options[
'final_usernamecol'].
309 ", ".$this->options[
'final_passwordcol'];
312 $sql_from .=
', '.$fields;
316 $query = sprintf(
"SELECT %s FROM %s WHERE %s = %s",
318 $this->options[
'final_table'],
319 $this->options[
'final_usernamecol'],
320 $this->db->getTextValue($username)
324 if ($this->options[
'db_where'] !=
'') {
326 $query .=
" AND ".$this->options[
'db_where'];
331 $res = $this->db->getRow(
$query, null, null, null, MDB_FETCHMODE_ASSOC);
336 if (!is_array(
$res)) {
337 $this->activeUser =
'';
342 $password = trim($password,
"\r\n");
343 $res[$this->options[
'passwordcol']] = trim(
$res[$this->options[
'passwordcol']],
"\r\n");
346 if ($isChallengeResponse) {
347 $res[$this->options[
'passwordcol']] =
348 md5(
$res[$this->options[
'passwordcol']].$this->_auth_obj->session[
'loginchallenege']);
350 if ($this->options[
'cryptType'] ==
'md5') {
351 $res[$this->options[
'passwordcol']] = md5(
$res[$this->options[
'passwordcol']]);
356 $res[$this->options[
'passwordcol']],
357 $this->options[
'cryptType'])) {
359 foreach (
$res as $key => $value) {
360 if ($key == $this->options[
'passwordcol'] ||
361 $key == $this->options[
'usernamecol']) {
369 $this->_auth_obj->setAuthData($key, $value);
374 $this->activeUser =
$res[$this->options[
'usernamecol']];
398 if ( is_string($this->options[
'db_fields'])
399 && strstr($this->options[
'db_fields'],
'*')) {
402 $sql_from = $this->options[
'final_usernamecol']
403 .
', '.$this->options[
'final_passwordcol'];
406 $sql_from .=
', '.$fields;
410 $query = sprintf(
'SELECT %s FROM %s',
412 $this->options[
'final_table']
416 if ($this->options[
'db_where'] !=
'') {
418 $query .=
" WHERE ".$this->options[
'db_where'];
423 $res = $this->db->getAll(
$query, null, null, null, MDB_FETCHMODE_ASSOC);
425 if (MDB::isError(
$res)) {
428 foreach (
$res as $user) {
429 $user[
'username'] = $user[$this->options[
'usernamecol']];
458 if (isset($this->options[
'cryptType']) && $this->options[
'cryptType'] ==
'none') {
459 $cryptFunction =
'strval';
460 } elseif (isset($this->options[
'cryptType']) && function_exists($this->options[
'cryptType'])) {
461 $cryptFunction = $this->options[
'cryptType'];
463 $cryptFunction =
'md5';
466 $password = $cryptFunction($password);
468 $additional_key =
'';
469 $additional_value =
'';
473 if ($this->options[
'auto_quote']) {
474 $additional_key .=
', ' . $this->db->quoteIdentifier($key);
476 $additional_key .=
', ' . $key;
478 $additional_value .=
', ' . $this->db->getTextValue($value);
482 $query = sprintf(
"INSERT INTO %s (%s, %s%s) VALUES (%s, %s%s)",
483 $this->options[
'final_table'],
484 $this->options[
'final_usernamecol'],
485 $this->options[
'final_passwordcol'],
487 $this->db->getTextValue($username),
488 $this->db->getTextValue($password),
496 if (MDB::isError(
$res)) {
521 $query = sprintf(
"DELETE FROM %s WHERE %s = %s",
522 $this->options[
'final_table'],
523 $this->options[
'final_usernamecol'],
524 $this->db->getTextValue($username)
528 if ($this->options[
'db_where'] !=
'') {
530 $query .=
" AND ".$this->options[
'db_where'];
537 if (MDB::isError(
$res)) {
560 if (isset($this->options[
'cryptType']) && $this->options[
'cryptType'] ==
'none') {
561 $cryptFunction =
'strval';
562 } elseif (isset($this->options[
'cryptType']) && function_exists($this->options[
'cryptType'])) {
563 $cryptFunction = $this->options[
'cryptType'];
565 $cryptFunction =
'md5';
568 $password = $cryptFunction($password);
570 $query = sprintf(
"UPDATE %s SET %s = %s WHERE %s = %s",
571 $this->options[
'final_table'],
572 $this->options[
'final_passwordcol'],
573 $this->db->getTextValue($password),
574 $this->options[
'final_usernamecol'],
575 $this->db->getTextValue($username)
579 if ($this->options[
'db_where'] !=
'') {
581 $query .=
" AND ".$this->options[
'db_where'];
588 if (MDB::isError(
$res)) {
606 return in_array($this->options[
'cryptType'], array(
'md5',
'none',
''));
619 return $this->options[
'cryptType'];