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