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