A little fix to workaround what seems to be an ADOdb bug when using
[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
a657c48b 11 // This is a hack to workaround what seems to be a bug in ADOdb with accessing
12 // two databases of the same kind ... it seems to get confused when trying to access
13 // the first database again, after having accessed the second.
14 // The following hack will make the database explicit which keeps it happy
15 $CFG->prefix = "$CFG->dbname.$CFG->prefix";
16
17 // Connect to the external database
fa3ba0f6 18 $authdb = &ADONewConnection($CFG->auth_dbtype);
d1b4e172 19 $authdb->PConnect($CFG->auth_dbhost,$CFG->auth_dbuser,$CFG->auth_dbpass,$CFG->auth_dbname);
dce48fc8 20
50c4bd41 21 switch ($CFG->auth_dbpasstype) { // Re-format password accordingly
22 case "md5":
23 $password = md5($password);
24 break;
25 }
dce48fc8 26
d1b4e172 27 $rs = $authdb->Execute("SELECT * FROM $CFG->auth_dbtable
28 WHERE $CFG->auth_dbfielduser = '$username'
29 AND $CFG->auth_dbfieldpass = '$password' ");
dce48fc8 30 if (!$rs) {
31 notify("Could not connect to the specified authentication database...");
32 return false;
33 }
34
35 if ( $rs->RecordCount() ) {
36 return true;
37 } else {
38 return false;
39 }
40}
41
42
34daec9b 43function auth_get_userinfo($username){
44// Reads any other information for a user from external database,
45// then returns it in an array
46
47 global $CFG;
a657c48b 48
34daec9b 49 $config = (array) $CFG;
50
51 ADOLoadCode($CFG->auth_dbtype);
52 $authdb = &ADONewConnection();
53 $authdb->PConnect($CFG->auth_dbhost,$CFG->auth_dbuser,$CFG->auth_dbpass,$CFG->auth_dbname);
54
55 $fields = array("firstname", "lastname", "email", "phone1", "phone2",
56 "department", "address", "city", "country", "description",
57 "idnumber", "lang");
58
59 $result = array();
60
61 foreach ($fields as $field) {
62 if ($config["auth_user_$field"]) {
63 if ($rs = $authdb->Execute("SELECT ".$config["auth_user_$field"]." FROM $CFG->auth_dbtable
64 WHERE $CFG->auth_dbfielduser = '$username'")) {
65 if ( $rs->RecordCount() == 1 ) {
66 $result["$field"] = $rs->fields[$config["auth_user_$field"]];
67 }
68 }
69 }
70 }
71
72 return $result;
73}
74
dce48fc8 75?>