MDL-17772 fixed guest access; MDL-17754 $USER object initialisation cleanup
[moodle.git] / login / confirm.php
1 <?php // $Id$
3     require_once("../config.php");
5     $data = optional_param('data', '', PARAM_CLEAN);  // Formatted as:  secret/username
7     $p = optional_param('p', '', PARAM_ALPHANUM);     // Old parameter:  secret
8     $s = optional_param('s', '', PARAM_CLEAN);        // Old parameter:  username
10     if (empty($CFG->registerauth)) {
11         print_error('cannotusepage2');
12     }
13     $authplugin = get_auth_plugin($CFG->registerauth);
15     if (!$authplugin->can_confirm()) {
16         print_error('cannotusepage2');
17     }
19     if (!empty($data) || (!empty($p) && !empty($s))) {
21         if (!empty($data)) {
22             $dataelements = explode('/',$data);
23             $usersecret = $dataelements[0];
24             $username   = $dataelements[1];
25         } else {
26             $usersecret = $p;
27             $username   = $s;
28         }
30         $confirmed = $authplugin->user_confirm($username, $usersecret);
32         if ($confirmed == AUTH_CONFIRM_ALREADY) {
33             $user = get_complete_user_data('username', $username);
34             print_header(get_string("alreadyconfirmed"), get_string("alreadyconfirmed"), array(), "");
35             print_box_start('generalbox centerpara boxwidthnormal boxaligncenter');
36             echo "<h3>".get_string("thanks").", ". fullname($user) . "</h3>\n";
37             echo "<p>".get_string("alreadyconfirmed")."</p>\n";
38             print_single_button("$CFG->wwwroot/course/", null, get_string('courses'));
39             print_box_end();
40             print_footer();
41             exit;
43         } else if ($confirmed == AUTH_CONFIRM_OK) {
45             // The user has confirmed successfully, let's log them in
47             if (!$user = get_complete_user_data('username', $username)) {
48                 print_error('cannotfinduser', '', '', $username);
49             }
51             complete_user_login($user);
53             if ( ! empty($SESSION->wantsurl) ) {   // Send them where they were going
54                 $goto = $SESSION->wantsurl;
55                 unset($SESSION->wantsurl);
56                 redirect($goto);
57             }
59             print_header(get_string("confirmed"), get_string("confirmed"), array(), "");
60             print_box_start('generalbox centerpara boxwidthnormal boxaligncenter');
61             echo "<h3>".get_string("thanks").", ". fullname($USER) . "</h3>\n";
62             echo "<p>".get_string("confirmed")."</p>\n";
63             print_single_button("$CFG->wwwroot/course/", null, get_string('courses'));
64             print_box_end();
65             print_footer();
66             exit;
67         } else {
68             print_error('invalidconfirmdata');
69         }
70     } else {
71         print_error("errorwhenconfirming");
72     }
74     redirect("$CFG->wwwroot/");
76 ?>