More robustness, and some little bug fixes.
[moodle.git] / auth / db / lib.php
CommitLineData
dce48fc8 1<?PHP // $Id$
2 // Authentication by looking up an external database table
3
dce48fc8 4
5function auth_user_login ($username, $password) {
6// Returns true if the username and password work
7// and false if they are wrong or don't exist.
8
9 global $CFG;
10
d1b4e172 11 ADOLoadCode($CFG->auth_dbtype);
dce48fc8 12 $authdb = &ADONewConnection();
d1b4e172 13 $authdb->PConnect($CFG->auth_dbhost,$CFG->auth_dbuser,$CFG->auth_dbpass,$CFG->auth_dbname);
dce48fc8 14
50c4bd41 15 switch ($CFG->auth_dbpasstype) { // Re-format password accordingly
16 case "md5":
17 $password = md5($password);
18 break;
19 }
dce48fc8 20
d1b4e172 21 $rs = $authdb->Execute("SELECT * FROM $CFG->auth_dbtable
22 WHERE $CFG->auth_dbfielduser = '$username'
23 AND $CFG->auth_dbfieldpass = '$password' ");
dce48fc8 24 if (!$rs) {
25 notify("Could not connect to the specified authentication database...");
26 return false;
27 }
28
29 if ( $rs->RecordCount() ) {
30 return true;
31 } else {
32 return false;
33 }
34}
35
36
34daec9b 37function auth_get_userinfo($username){
38// Reads any other information for a user from external database,
39// then returns it in an array
40
41 global $CFG;
42 $config = (array) $CFG;
43
44 ADOLoadCode($CFG->auth_dbtype);
45 $authdb = &ADONewConnection();
46 $authdb->PConnect($CFG->auth_dbhost,$CFG->auth_dbuser,$CFG->auth_dbpass,$CFG->auth_dbname);
47
48 $fields = array("firstname", "lastname", "email", "phone1", "phone2",
49 "department", "address", "city", "country", "description",
50 "idnumber", "lang");
51
52 $result = array();
53
54 foreach ($fields as $field) {
55 if ($config["auth_user_$field"]) {
56 if ($rs = $authdb->Execute("SELECT ".$config["auth_user_$field"]." FROM $CFG->auth_dbtable
57 WHERE $CFG->auth_dbfielduser = '$username'")) {
58 if ( $rs->RecordCount() == 1 ) {
59 $result["$field"] = $rs->fields[$config["auth_user_$field"]];
60 }
61 }
62 }
63 }
64
65 return $result;
66}
67
dce48fc8 68?>