weekly release 2.3dev (blame stronk7 for 0202 mistake)
[moodle.git] / auth / db / auth.php
CommitLineData
b9ddb2d5 1<?php
b9ddb2d5 2/**
b9ddb2d5 3 * Authentication Plugin: External Database Authentication
4 *
5 * Checks against an external database.
6 *
7415aed1
PS
7 * @package auth
8 * @subpackage db
9 * @author Martin Dougiamas
10 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
b9ddb2d5 11 */
12
7415aed1 13defined('MOODLE_INTERNAL') || die();
b9ddb2d5 14
6bc1e5d5 15require_once($CFG->libdir.'/authlib.php');
e7721485 16require_once($CFG->libdir.'/adodb/adodb.inc.php');
6bc1e5d5 17
b9ddb2d5 18/**
19 * External database authentication plugin.
20 */
6bc1e5d5 21class auth_plugin_db extends auth_plugin_base {
b9ddb2d5 22
23 /**
24 * Constructor.
25 */
26 function auth_plugin_db() {
6bc1e5d5 27 $this->authtype = 'db';
b9ddb2d5 28 $this->config = get_config('auth/db');
8ae42b8d 29 if (empty($this->config->extencoding)) {
30 $this->config->extencoding = 'utf-8';
31 }
b9ddb2d5 32 }
33
34 /**
35 * Returns true if the username and password work and false if they are
36 * wrong or don't exist.
37 *
576c063b 38 * @param string $username The username
39 * @param string $password The password
8ae42b8d 40 *
41 * @return bool Authentication success or failure.
b9ddb2d5 42 */
139ebfdb 43 function user_login($username, $password) {
576c063b 44 global $CFG, $DB;
b9ddb2d5 45
ab6e0848
PS
46 $extusername = textlib::convert($username, 'utf-8', $this->config->extencoding);
47 $extpassword = textlib::convert($password, 'utf-8', $this->config->extencoding);
8ae42b8d 48
139ebfdb 49 $authdb = $this->db_init();
b9ddb2d5 50
7415aed1 51 if ($this->is_internal()) {
b9ddb2d5 52 // lookup username externally, but resolve
53 // password locally -- to support backend that
54 // don't track passwords
8ae42b8d 55 $rs = $authdb->Execute("SELECT * FROM {$this->config->table}
56 WHERE {$this->config->fielduser} = '".$this->ext_addslashes($extusername)."' ");
b9ddb2d5 57 if (!$rs) {
03cedd62 58 $authdb->Close();
03ea0b32 59 debugging(get_string('auth_dbcantconnect','auth_db'));
b9ddb2d5 60 return false;
61 }
8ae42b8d 62
7415aed1 63 if (!$rs->EOF) {
03cedd62 64 $rs->Close();
65 $authdb->Close();
7415aed1 66 // user exists externally
b9ddb2d5 67 // check username/password internally
576c063b 68 if ($user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
b9ddb2d5 69 return validate_internal_user_password($user, $password);
70 }
71 } else {
03cedd62 72 $rs->Close();
73 $authdb->Close();
b9ddb2d5 74 // user does not exist externally
75 return false;
8ae42b8d 76 }
b9ddb2d5 77
8ae42b8d 78 } else {
7415aed1 79 // normal case: use external db for both usernames and passwords
b9ddb2d5 80
81 if ($this->config->passtype === 'md5') { // Re-format password accordingly
8ae42b8d 82 $extpassword = md5($extpassword);
83 } else if ($this->config->passtype === 'sha1') {
84 $extpassword = sha1($extpassword);
b9ddb2d5 85 }
86
8ae42b8d 87 $rs = $authdb->Execute("SELECT * FROM {$this->config->table}
88 WHERE {$this->config->fielduser} = '".$this->ext_addslashes($extusername)."'
89 AND {$this->config->fieldpass} = '".$this->ext_addslashes($extpassword)."' ");
b9ddb2d5 90 if (!$rs) {
03cedd62 91 $authdb->Close();
03ea0b32 92 debugging(get_string('auth_dbcantconnect','auth_db'));
b9ddb2d5 93 return false;
94 }
8ae42b8d 95
03cedd62 96 if (!$rs->EOF) {
97 $rs->Close();
98 $authdb->Close();
b9ddb2d5 99 return true;
100 } else {
03cedd62 101 $rs->Close();
102 $authdb->Close();
b9ddb2d5 103 return false;
8ae42b8d 104 }
105
b9ddb2d5 106 }
107 }
108
139ebfdb 109 function db_init() {
93901eb4 110 // Connect to the external database (forcing new connection)
ab6e0848 111 $authdb = ADONewConnection($this->config->type);
8ae42b8d 112 if (!empty($this->config->debugauthdb)) {
113 $authdb->debug = true;
114 ob_start();//start output buffer to allow later use of the page headers
115 }
116 $authdb->Connect($this->config->host, $this->config->user, $this->config->pass, $this->config->name, true);
b9ddb2d5 117 $authdb->SetFetchMode(ADODB_FETCH_ASSOC);
8ae42b8d 118 if (!empty($this->config->setupsql)) {
119 $authdb->Execute($this->config->setupsql);
120 }
b9ddb2d5 121
139ebfdb 122 return $authdb;
123 }
7415aed1 124
139ebfdb 125 /**
ab6e0848 126 * Returns user attribute mappings between moodle and ldap
139ebfdb 127 *
128 * @return array
129 */
130 function db_attributes() {
139ebfdb 131 $moodleattributes = array();
4105caff 132 foreach ($this->userfields as $field) {
139ebfdb 133 if (!empty($this->config->{"field_map_$field"})) {
134 $moodleattributes[$field] = $this->config->{"field_map_$field"};
0f02788f 135 }
136 }
5261baf1 137 $moodleattributes['username'] = $this->config->fielduser;
139ebfdb 138 return $moodleattributes;
139 }
140
141 /**
142 * Reads any other information for a user from external database,
143 * then returns it in an array
144 *
be544ec3 145 * @param string $username
139ebfdb 146 *
147 * @return array without magic quotes
148 */
149 function get_userinfo($username) {
139ebfdb 150 global $CFG;
151
ab6e0848 152 $extusername = textlib::convert($username, 'utf-8', $this->config->extencoding);
139ebfdb 153
154 $authdb = $this->db_init();
155
156 //Array to map local fieldnames we want, to external fieldnames
157 $selectfields = $this->db_attributes();
158
0f02788f 159 $result = array();
160 //If at least one field is mapped from external db, get that mapped data:
161 if ($selectfields) {
162 $select = '';
163 foreach ($selectfields as $localname=>$externalname) {
164 $select .= ", $externalname AS $localname";
165 }
166 $select = 'SELECT ' . substr($select,1);
167 $sql = $select .
168 " FROM {$this->config->table}" .
8ae42b8d 169 " WHERE {$this->config->fielduser} = '".$this->ext_addslashes($extusername)."'";
0f02788f 170 if ($rs = $authdb->Execute($sql)) {
03cedd62 171 if ( !$rs->EOF ) {
245ac557 172 $fields_obj = $rs->FetchObj();
1ed6ae07 173 $fields_obj = (object)array_change_key_case((array)$fields_obj , CASE_LOWER);
0f02788f 174 foreach ($selectfields as $localname=>$externalname) {
ab6e0848 175 $result[$localname] = textlib::convert($fields_obj->{$localname}, $this->config->extencoding, 'utf-8');
0f02788f 176 }
177 }
245ac557 178 $rs->Close();
b9ddb2d5 179 }
180 }
181 $authdb->Close();
b9ddb2d5 182 return $result;
183 }
184
fb5c7739 185 /**
186 * Change a user's password
187 *
ae040d4b 188 * @param object $user User table object
189 * @param string $newpassword Plaintext password
8ae42b8d 190 *
fb5c7739 191 * @return bool True on success
192 */
da249a30 193 function user_update_password($user, $newpassword) {
7415aed1 194 if ($this->is_internal()) {
d972bfd0 195 return update_internal_user_password($user, $newpassword);
b9ddb2d5 196 } else {
197 // we should have never been called!
198 return false;
199 }
200 }
201
202 /**
ab6e0848 203 * synchronizes user from external db to moodle user table
b9ddb2d5 204 *
ab6e0848 205 * Sync should be done by using idnumber attribute, not username.
b9ddb2d5 206 * You need to pass firstsync parameter to function to fill in
ab6e0848 207 * idnumbers if they don't exists in moodle user table.
8ae42b8d 208 *
ab6e0848 209 * Syncing users removes (disables) users that don't exists anymore in external db.
8ae42b8d 210 * Creates new users and updates coursecreator status of users.
211 *
b9ddb2d5 212 * This implementation is simpler but less scalable than the one found in the LDAP module.
213 *
7415aed1 214 * @param bool $do_updates Optional: set to true to force an update of existing accounts
ab6e0848
PS
215 * @param bool $verbose
216 * @return int 0 means success, 1 means failure
b9ddb2d5 217 */
ab6e0848 218 function sync_users($do_updates=false, $verbose=false) {
70ca450a 219 global $CFG, $DB;
b9ddb2d5 220
ab6e0848 221 // list external users
b9ddb2d5 222 $userlist = $this->get_userlist();
b9ddb2d5 223
ab6e0848 224 // delete obsolete internal users
139ebfdb 225 if (!empty($this->config->removeuser)) {
b9ddb2d5 226
139ebfdb 227 // find obsolete users
228 if (count($userlist)) {
20d8d5c7
EL
229 list($notin_sql, $params) = $DB->get_in_or_equal($userlist, SQL_PARAMS_NAMED, 'u', false);
230 $params['authtype'] = $this->authtype;
f91f3f63 231 $sql = "SELECT u.*
bc31625a
PS
232 FROM {user} u
233 WHERE u.auth=:authtype AND u.deleted=0 AND u.username $notin_sql";
139ebfdb 234 } else {
f91f3f63 235 $sql = "SELECT u.*
bc31625a
PS
236 FROM {user} u
237 WHERE u.auth=:authtype AND u.deleted=0";
238 $params = array();
20d8d5c7 239 $params['authtype'] = $this->authtype;
139ebfdb 240 }
bc31625a 241 $remove_users = $DB->get_records_sql($sql, $params);
139ebfdb 242
243 if (!empty($remove_users)) {
ab6e0848
PS
244 if ($verbose) {
245 mtrace(print_string('auth_dbuserstoremove','auth_db', count($remove_users)));
246 }
139ebfdb 247
139ebfdb 248 foreach ($remove_users as $user) {
6f87ef52 249 if ($this->config->removeuser == AUTH_REMOVEUSER_FULLDELETE) {
ab6e0848
PS
250 delete_user($user);
251 if ($verbose) {
252 mtrace("\t".get_string('auth_dbdeleteuser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)));
139ebfdb 253 }
6f87ef52 254 } else if ($this->config->removeuser == AUTH_REMOVEUSER_SUSPEND) {
1dffbae2 255 $updateuser = new stdClass();
139ebfdb 256 $updateuser->id = $user->id;
257 $updateuser->auth = 'nologin';
dd88de0e 258 $DB->update_record('user', $updateuser);
ab6e0848
PS
259 if ($verbose) {
260 mtrace("\t".get_string('auth_dbsuspenduser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)));
261 }
139ebfdb 262 }
b9ddb2d5 263 }
8ae42b8d 264 }
139ebfdb 265 unset($remove_users); // free mem!
8ae42b8d 266 }
b9ddb2d5 267
268 if (!count($userlist)) {
269 // exit right here
270 // nothing else to do
ab6e0848 271 return 0;
b9ddb2d5 272 }
273
274 ///
275 /// update existing accounts
276 ///
277 if ($do_updates) {
278 // narrow down what fields we need to update
279 $all_keys = array_keys(get_object_vars($this->config));
280 $updatekeys = array();
281 foreach ($all_keys as $key) {
282 if (preg_match('/^field_updatelocal_(.+)$/',$key, $match)) {
283 if ($this->config->{$key} === 'onlogin') {
284 array_push($updatekeys, $match[1]); // the actual key name
285 }
286 }
287 }
288 // print_r($all_keys); print_r($updatekeys);
289 unset($all_keys); unset($key);
290
291 // only go ahead if we actually
292 // have fields to update locally
293 if (!empty($updatekeys)) {
20d8d5c7 294 list($in_sql, $params) = $DB->get_in_or_equal($userlist, SQL_PARAMS_NAMED, 'u', true);
bc31625a
PS
295 $params['authtype'] = $this->authtype;
296 $sql = "SELECT u.id, u.username
297 FROM {user} u
298 WHERE u.auth=:authtype AND u.deleted=0 AND u.username {$in_sql}";
299 if ($update_users = $DB->get_records_sql($sql, $params)) {
ab6e0848
PS
300 if ($verbose) {
301 mtrace("User entries to update: ".count($update_users));
302 }
8ae42b8d 303
304 foreach ($update_users as $user) {
ab6e0848
PS
305 if ($this->update_user_record($user->username, $updatekeys)) {
306 if ($verbose) {
307 mtrace("\t".get_string('auth_dbupdatinguser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)));
308 }
309 } else {
310 if ($verbose) {
311 mtrace("\t".get_string('auth_dbupdatinguser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id))." - ".get_string('skipped'));
312 }
139ebfdb 313 }
8ae42b8d 314 }
315 unset($update_users); // free memory
b9ddb2d5 316 }
b9ddb2d5 317 }
318 }
319
320
321 ///
322 /// create missing accounts
323 ///
324 // NOTE: this is very memory intensive
325 // and generally inefficient
8ae42b8d 326 $sql = 'SELECT u.id, u.username
70ca450a 327 FROM {user} u
5211c7ec 328 WHERE u.auth=\'' . $this->authtype . '\' AND u.deleted=\'0\'';
b9ddb2d5 329
70ca450a 330 $users = $DB->get_records_sql($sql);
8ae42b8d 331
b9ddb2d5 332 // simplify down to usernames
333 $usernames = array();
2b214bc1 334 if (!empty($users)) {
335 foreach ($users as $user) {
336 array_push($usernames, $user->username);
337 }
338 unset($users);
b9ddb2d5 339 }
b9ddb2d5 340
341 $add_users = array_diff($userlist, $usernames);
342 unset($usernames);
343
344 if (!empty($add_users)) {
ab6e0848
PS
345 if ($verbose) {
346 mtrace(get_string('auth_dbuserstoadd','auth_db',count($add_users)));
347 }
d5a8d9aa 348 $transaction = $DB->start_delegated_transaction();
b9ddb2d5 349 foreach($add_users as $user) {
350 $username = $user;
351 $user = $this->get_userinfo_asobj($user);
8ae42b8d 352
b9ddb2d5 353 // prep a few params
b7b50143 354 $user->username = $username;
355 $user->modified = time();
356 $user->confirmed = 1;
5211c7ec 357 $user->auth = $this->authtype;
b7b50143 358 $user->mnethostid = $CFG->mnet_localhost_id;
8ae42b8d 359 if (empty($user->lang)) {
360 $user->lang = $CFG->lang;
361 }
362
b9ddb2d5 363 // maybe the user has been deleted before
70ca450a 364 if ($old_user = $DB->get_record('user', array('username'=>$user->username, 'deleted'=>1, 'mnethostid'=>$user->mnethostid))) {
b9ddb2d5 365 $user->id = $old_user->id;
70ca450a 366 $DB->set_field('user', 'deleted', 0, array('username'=>$user->username));
ab6e0848
PS
367 if ($verbose) {
368 mtrace("\t".get_string('auth_dbreviveduser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)));
369 }
6b8ad965 370
a9637e7d 371 } else {
ab6e0848
PS
372 $id = $DB->insert_record ('user', $user); // it is truly a new user
373 if ($verbose) {
374 mtrace("\t".get_string('auth_dbinsertuser', 'auth_db', array('name'=>$user->username, 'id'=>$id)));
375 }
b9ddb2d5 376 // if relevant, tag for password generation
7415aed1 377 if ($this->is_internal()) {
b9ddb2d5 378 set_user_preference('auth_forcepasswordchange', 1, $id);
379 set_user_preference('create_password', 1, $id);
380 }
b9ddb2d5 381 }
b9ddb2d5 382 }
d5a8d9aa 383 $transaction->allow_commit();
b9ddb2d5 384 unset($add_users); // free mem
385 }
ab6e0848 386 return 0;
b9ddb2d5 387 }
388
139ebfdb 389 function user_exists($username) {
93901eb4 390
a7e32367 391 /// Init result value
392 $result = false;
393
ab6e0848 394 $extusername = textlib::convert($username, 'utf-8', $this->config->extencoding);
8ae42b8d 395
139ebfdb 396 $authdb = $this->db_init();
b9ddb2d5 397
8ae42b8d 398 $rs = $authdb->Execute("SELECT * FROM {$this->config->table}
399 WHERE {$this->config->fielduser} = '".$this->ext_addslashes($extusername)."' ");
b9ddb2d5 400
401 if (!$rs) {
2b06294b 402 print_error('auth_dbcantconnect','auth_db');
7415aed1 403 } else if (!$rs->EOF) {
ab6e0848 404 // user exists externally
03cedd62 405 $result = true;
8ae42b8d 406 }
a7e32367 407
408 $authdb->Close();
409 return $result;
b9ddb2d5 410 }
411
412
413 function get_userlist() {
93901eb4 414
a7e32367 415 /// Init result value
416 $result = array();
417
139ebfdb 418 $authdb = $this->db_init();
b9ddb2d5 419
420 // fetch userlist
421 $rs = $authdb->Execute("SELECT {$this->config->fielduser} AS username
422 FROM {$this->config->table} ");
b9ddb2d5 423
424 if (!$rs) {
2b06294b 425 print_error('auth_dbcantconnect','auth_db');
7415aed1 426 } else if (!$rs->EOF) {
245ac557 427 while ($rec = $rs->FetchRow()) {
e9366bf8 428 $rec = (object)array_change_key_case((array)$rec , CASE_LOWER);
429 array_push($result, $rec->username);
b9ddb2d5 430 }
8ae42b8d 431 }
a7e32367 432
433 $authdb->Close();
434 return $result;
b9ddb2d5 435 }
436
437 /**
ab6e0848 438 * reads user information from DB and return it in an object
b9ddb2d5 439 *
8ae42b8d 440 * @param string $username username (with system magic quotes)
b9ddb2d5 441 * @return array
442 */
443 function get_userinfo_asobj($username) {
444 $user_array = truncate_userinfo($this->get_userinfo($username));
1dffbae2 445 $user = new stdClass();
b9ddb2d5 446 foreach($user_array as $key=>$value) {
447 $user->{$key} = $value;
448 }
449 return $user;
450 }
451
8ae42b8d 452 /**
453 * will update a local user record from an external source.
454 * is a lighter version of the one in moodlelib -- won't do
b9ddb2d5 455 * expensive ops such as enrolment
456 *
8ae42b8d 457 * If you don't pass $updatekeys, there is a performance hit and
b9ddb2d5 458 * values removed from DB won't be removed from moodle.
8ae42b8d 459 *
185721a4 460 * @param string $username username
ab6e0848
PS
461 * @param bool $updatekeys
462 * @return stdClass
b9ddb2d5 463 */
139ebfdb 464 function update_user_record($username, $updatekeys=false) {
185721a4 465 global $CFG, $DB;
b9ddb2d5 466
b9ddb2d5 467 //just in case check text case
ab6e0848 468 $username = trim(textlib::strtolower($username));
8ae42b8d 469
b9ddb2d5 470 // get the current user record
185721a4 471 $user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id));
b9ddb2d5 472 if (empty($user)) { // trouble
473 error_log("Cannot update non-existent user: $username");
2b06294b 474 print_error('auth_dbusernotexist','auth_db',$username);
b9ddb2d5 475 die;
476 }
477
b7b50143 478 // Ensure userid is not overwritten
479 $userid = $user->id;
480
b9ddb2d5 481 if ($newinfo = $this->get_userinfo($username)) {
482 $newinfo = truncate_userinfo($newinfo);
8ae42b8d 483
b9ddb2d5 484 if (empty($updatekeys)) { // all keys? this does not support removing values
485 $updatekeys = array_keys($newinfo);
486 }
8ae42b8d 487
b9ddb2d5 488 foreach ($updatekeys as $key) {
b9ddb2d5 489 if (isset($newinfo[$key])) {
490 $value = $newinfo[$key];
b9ddb2d5 491 } else {
492 $value = '';
493 }
8ae42b8d 494
495 if (!empty($this->config->{'field_updatelocal_' . $key})) {
bc31625a 496 if (isset($user->{$key}) and $user->{$key} != $value) { // only update if it's changed
185721a4 497 $DB->set_field('user', $key, $value, array('id'=>$userid));
139ebfdb 498 }
b9ddb2d5 499 }
500 }
501 }
185721a4 502 return $DB->get_record('user', array('id'=>$userid, 'deleted'=>0));
139ebfdb 503 }
504
505 /**
506 * Called when the user record is updated.
507 * Modifies user in external database. It takes olduser (before changes) and newuser (after changes)
ab6e0848 508 * compares information saved modified information to external db.
139ebfdb 509 *
70ca450a 510 * @param mixed $olduser Userobject before modifications
511 * @param mixed $newuser Userobject new modified userobject
139ebfdb 512 * @return boolean result
513 *
514 */
515 function user_update($olduser, $newuser) {
516 if (isset($olduser->username) and isset($newuser->username) and $olduser->username != $newuser->username) {
517 error_log("ERROR:User renaming not allowed in ext db");
518 return false;
519 }
520
5211c7ec 521 if (isset($olduser->auth) and $olduser->auth != $this->authtype) {
139ebfdb 522 return true; // just change auth and skip update
523 }
524
525 $curruser = $this->get_userinfo($olduser->username);
526 if (empty($curruser)) {
527 error_log("ERROR:User $olduser->username found in ext db");
528 return false;
529 }
530
ab6e0848 531 $extusername = textlib::convert($olduser->username, 'utf-8', $this->config->extencoding);
139ebfdb 532
533 $authdb = $this->db_init();
534
535 $update = array();
536 foreach($curruser as $key=>$value) {
537 if ($key == 'username') {
538 continue; // skip this
539 }
540 if (empty($this->config->{"field_updateremote_$key"})) {
541 continue; // remote update not requested
542 }
543 if (!isset($newuser->$key)) {
544 continue;
545 }
70ca450a 546 $nuvalue = $newuser->$key;
139ebfdb 547 if ($nuvalue != $value) {
ab6e0848 548 $update[] = $this->config->{"field_map_$key"}."='".$this->ext_addslashes(textlib::convert($nuvalue, 'utf-8', $this->config->extencoding))."'";
139ebfdb 549 }
550 }
551 if (!empty($update)) {
552 $authdb->Execute("UPDATE {$this->config->table}
7415aed1
PS
553 SET ".implode(',', $update)."
554 WHERE {$this->config->fielduser}='".$this->ext_addslashes($extusername)."'");
139ebfdb 555 }
556 $authdb->Close();
557 return true;
b9ddb2d5 558 }
559
8ae42b8d 560 /**
561 * A chance to validate form data, and last chance to
562 * do stuff before it is inserted in config_plugin
ab6e0848
PS
563 *
564 * @param stfdClass config form
565 * @param array $error errors
566 * @return void
8ae42b8d 567 */
ab6e0848 568 function validate_form($form, &$err) {
150b5fb0 569 if ($form->passtype === 'internal') {
b9ddb2d5 570 $this->config->changepasswordurl = '';
571 set_config('changepasswordurl', '', 'auth/db');
572 }
b9ddb2d5 573 }
574
edb5da83 575 function prevent_local_passwords() {
7415aed1 576 return !$this->is_internal();
edb5da83
PS
577 }
578
b9ddb2d5 579 /**
7415aed1
PS
580 * Returns true if this authentication plugin is "internal".
581 *
582 * Internal plugins use password hashes from Moodle user table for authentication.
b9ddb2d5 583 *
139ebfdb 584 * @return bool
b9ddb2d5 585 */
586 function is_internal() {
e79781f7
PS
587 if (!isset($this->config->passtype)) {
588 return true;
589 }
7415aed1
PS
590 return ($this->config->passtype === 'internal');
591 }
592
593 /**
594 * Indicates if moodle should automatically update internal user
595 * records with data from external sources using the information
596 * from auth_plugin_base::get_userinfo().
597 *
598 * @return bool true means automatically copy data from ext to user table
599 */
600 function is_synchronised_with_external() {
601 return true;
b9ddb2d5 602 }
603
604 /**
605 * Returns true if this authentication plugin can change the user's
606 * password.
607 *
139ebfdb 608 * @return bool
b9ddb2d5 609 */
610 function can_change_password() {
7415aed1 611 return ($this->is_internal() or !empty($this->config->changepasswordurl));
b9ddb2d5 612 }
613
614 /**
430759a5 615 * Returns the URL for changing the user's pw, or empty if the default can
b9ddb2d5 616 * be used.
617 *
99f9f85f 618 * @return moodle_url
b9ddb2d5 619 */
620 function change_password_url() {
7415aed1 621 if ($this->is_internal()) {
430759a5 622 // standard form
99f9f85f 623 return null;
430759a5 624 } else {
7415aed1 625 // use admin defined custom url
99f9f85f 626 return new moodle_url($this->config->changepasswordurl);
430759a5 627 }
b9ddb2d5 628 }
629
ab6ff8a4 630 /**
631 * Returns true if plugin allows resetting of internal password.
632 *
633 * @return bool
634 */
635 function can_reset_password() {
7415aed1 636 return $this->is_internal();
ab6ff8a4 637 }
638
b9ddb2d5 639 /**
640 * Prints a form for configuring this authentication plugin.
641 *
642 * This function is called from admin/auth.php, and outputs a full page with
643 * a form for configuring this plugin.
644 *
ab6e0848
PS
645 * @param stdClass $config
646 * @param array $err errors
647 * @param array $user_fields
648 * @return void
b9ddb2d5 649 */
139ebfdb 650 function config_form($config, $err, $user_fields) {
8ae42b8d 651 include 'config.html';
b9ddb2d5 652 }
653
654 /**
655 * Processes and stores configuration data for this authentication plugin.
ab6e0848
PS
656 * @param srdClass $config
657 * @return bool always true or exception
b9ddb2d5 658 */
659 function process_config($config) {
660 // set to defaults if undefined
661 if (!isset($config->host)) {
8ae42b8d 662 $config->host = 'localhost';
b9ddb2d5 663 }
664 if (!isset($config->type)) {
8ae42b8d 665 $config->type = 'mysql';
666 }
667 if (!isset($config->sybasequoting)) {
668 $config->sybasequoting = 0;
b9ddb2d5 669 }
670 if (!isset($config->name)) {
8ae42b8d 671 $config->name = '';
b9ddb2d5 672 }
673 if (!isset($config->user)) {
8ae42b8d 674 $config->user = '';
b9ddb2d5 675 }
676 if (!isset($config->pass)) {
8ae42b8d 677 $config->pass = '';
b9ddb2d5 678 }
679 if (!isset($config->table)) {
8ae42b8d 680 $config->table = '';
b9ddb2d5 681 }
682 if (!isset($config->fielduser)) {
8ae42b8d 683 $config->fielduser = '';
b9ddb2d5 684 }
685 if (!isset($config->fieldpass)) {
8ae42b8d 686 $config->fieldpass = '';
b9ddb2d5 687 }
688 if (!isset($config->passtype)) {
8ae42b8d 689 $config->passtype = 'plaintext';
690 }
691 if (!isset($config->extencoding)) {
692 $config->extencoding = 'utf-8';
693 }
694 if (!isset($config->setupsql)) {
695 $config->setupsql = '';
696 }
697 if (!isset($config->debugauthdb)) {
698 $config->debugauthdb = 0;
b9ddb2d5 699 }
139ebfdb 700 if (!isset($config->removeuser)) {
6f87ef52 701 $config->removeuser = AUTH_REMOVEUSER_KEEP;
139ebfdb 702 }
b9ddb2d5 703 if (!isset($config->changepasswordurl)) {
704 $config->changepasswordurl = '';
705 }
706
707 // save settings
8ae42b8d 708 set_config('host', $config->host, 'auth/db');
709 set_config('type', $config->type, 'auth/db');
710 set_config('sybasequoting', $config->sybasequoting, 'auth/db');
711 set_config('name', $config->name, 'auth/db');
712 set_config('user', $config->user, 'auth/db');
713 set_config('pass', $config->pass, 'auth/db');
714 set_config('table', $config->table, 'auth/db');
715 set_config('fielduser', $config->fielduser, 'auth/db');
716 set_config('fieldpass', $config->fieldpass, 'auth/db');
717 set_config('passtype', $config->passtype, 'auth/db');
718 set_config('extencoding', trim($config->extencoding), 'auth/db');
139ebfdb 719 set_config('setupsql', trim($config->setupsql),'auth/db');
8ae42b8d 720 set_config('debugauthdb', $config->debugauthdb, 'auth/db');
139ebfdb 721 set_config('removeuser', $config->removeuser, 'auth/db');
8ae42b8d 722 set_config('changepasswordurl', trim($config->changepasswordurl), 'auth/db');
723
b9ddb2d5 724 return true;
725 }
726
8ae42b8d 727 function ext_addslashes($text) {
728 // using custom made function for now
729 if (empty($this->config->sybasequoting)) {
730 $text = str_replace('\\', '\\\\', $text);
731 $text = str_replace(array('\'', '"', "\0"), array('\\\'', '\\"', '\\0'), $text);
732 } else {
733 $text = str_replace("'", "''", $text);
734 }
735 return $text;
736 }
b9ddb2d5 737}
738
5117d598 739