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