Latest versions from Sebastien
[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
fa3ba0f6 11 $authdb = &ADONewConnection($CFG->auth_dbtype);
d1b4e172 12 $authdb->PConnect($CFG->auth_dbhost,$CFG->auth_dbuser,$CFG->auth_dbpass,$CFG->auth_dbname);
dce48fc8 13
50c4bd41 14 switch ($CFG->auth_dbpasstype) { // Re-format password accordingly
15 case "md5":
16 $password = md5($password);
17 break;
18 }
dce48fc8 19
d1b4e172 20 $rs = $authdb->Execute("SELECT * FROM $CFG->auth_dbtable
21 WHERE $CFG->auth_dbfielduser = '$username'
22 AND $CFG->auth_dbfieldpass = '$password' ");
dce48fc8 23 if (!$rs) {
24 notify("Could not connect to the specified authentication database...");
25 return false;
26 }
27
28 if ( $rs->RecordCount() ) {
29 return true;
30 } else {
31 return false;
32 }
33}
34
35
34daec9b 36function auth_get_userinfo($username){
37// Reads any other information for a user from external database,
38// then returns it in an array
39
40 global $CFG;
41 $config = (array) $CFG;
42
43 ADOLoadCode($CFG->auth_dbtype);
44 $authdb = &ADONewConnection();
45 $authdb->PConnect($CFG->auth_dbhost,$CFG->auth_dbuser,$CFG->auth_dbpass,$CFG->auth_dbname);
46
47 $fields = array("firstname", "lastname", "email", "phone1", "phone2",
48 "department", "address", "city", "country", "description",
49 "idnumber", "lang");
50
51 $result = array();
52
53 foreach ($fields as $field) {
54 if ($config["auth_user_$field"]) {
55 if ($rs = $authdb->Execute("SELECT ".$config["auth_user_$field"]." FROM $CFG->auth_dbtable
56 WHERE $CFG->auth_dbfielduser = '$username'")) {
57 if ( $rs->RecordCount() == 1 ) {
58 $result["$field"] = $rs->fields[$config["auth_user_$field"]];
59 }
60 }
61 }
62 }
63
64 return $result;
65}
66
dce48fc8 67?>