Auth - DB_Crypt - any interest?
| From: | Alan Knowles | Date: | Mon, 11 Mar 2002 07:44:45 +0000 |
| Subject: | Auth - DB_Crypt - any interest? | ||
| References: | 1 2 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-4951@lists.php.net to get a copy of this message | ||
below is a Auth Container for doing Crypt rather than md5
- Its useful if you already have a database that uses crypt rather than md5's
(i needed to work with midgard tables)
eg. password field is either
crypt($password) or "**$password" <- clear text..
any interest in fixing it up and adding?
regards
alan
require_once(dirname(__FILE__)."/DB.php");
class Auth_Container_DB_Midgard extends Auth_Container_DB {
function Auth_Container_DB_Midgard($dsn)
{
$this->Auth_Container_DB($dsn);} function fetchData($username, $password) {
/* Include additional fields if they exist */
if ($this->options["db_fields"] != "*") {
$cols = "," . $this->options["db_fields"];
} else {
$cols = "";
}
//echo "QUERY";
// echo serialize($this->db);
$query = sprintf("SELECT %s FROM %s
WHERE %s = '%s'",
$this->options['usernamecol'] . ", "
. $this->options['passwordcol']
. $cols,
$this->options['table'],
$this->options['usernamecol'],
$this->db->quoteString($username)
);
$res = $this->db->query($query);
if (DB::isError($res)) {
return new DB_Error($res->code, PEAR_ERROR_DIE);
} else {
$entry = $res->fetchRow(DB_FETCHMODE_ASSOC);
if (is_array($entry)) {
if (($entry[$this->options['passwordcol']] == "**".$password) ||
($entry[$this->options['passwordcol']] == crypt($password,substr($entry[$this->options['passwordcol']],0,2)))
) {
Auth::setAuth($entry[$this->options['usernamecol']]);
$res->free();
return true;
} else {
$this->activeUser = $entry[$this->options['usernamecol']];
return false;
}
} else {
$this->activeUser = "";
return false;
}
}
}
function addUser($username, $password, $additional = "")
{
$additional_key = "";
$additional_value = "";
if (is_array($additional)) {
foreach ($additional as $key => $value) {
$additional_key .= ", " . $key;
$additional_value .= ", '" . $value . "'";
}
}
$query = sprintf("INSERT INTO %s (%s, %s%s) VALUES ('%s', '%s'%s)",
$this->options['table'],
$this->options['usernamecol'],
$this->options['passwordcol'],
$additional_key,
$username,
crypt($password),
$additional_value
);
$res = $this->db->query($query);
if (DB::isError($res)) {
return new DB_Error($res->code, PEAR_ERROR_DIE);
} else {
return true;
}
}
}