MDL-20700 coding style cleanup - cvs keywords removed, closign php tag removed, trail...
[moodle.git] / auth / email / auth.php
CommitLineData
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: Email Authentication
9 *
10 * Standard authentication function.
11 *
12 * 2006-08-28 File created.
13 */
14
139ebfdb 15if (!defined('MOODLE_INTERNAL')) {
16 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
17}
b9ddb2d5 18
6bc1e5d5 19require_once($CFG->libdir.'/authlib.php');
b9ddb2d5 20
21/**
22 * Email authentication plugin.
23 */
6bc1e5d5 24class auth_plugin_email extends auth_plugin_base {
b9ddb2d5 25
26 /**
27 * Constructor.
28 */
29 function auth_plugin_email() {
6bc1e5d5 30 $this->authtype = 'email';
b9ddb2d5 31 $this->config = get_config('auth/email');
32 }
33
34 /**
35 * Returns true if the username and password work and false if they are
36 * wrong or don't exist.
37 *
38 * @param string $username The username
39 * @param string $password The password
139ebfdb 40 * @return bool Authentication success or failure.
b9ddb2d5 41 */
42 function user_login ($username, $password) {
be544ec3 43 global $CFG, $DB;
44 if ($user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
b9ddb2d5 45 return validate_internal_user_password($user, $password);
46 }
47 return false;
48 }
49
fb5c7739 50 /**
b9ddb2d5 51 * Updates the user's password.
52 *
53 * called when the user password is updated.
54 *
c57dcb62 55 * @param object $user User table object (with system magic quotes)
56 * @param string $newpassword Plaintext password (with system magic quotes)
b9ddb2d5 57 * @return boolean result
58 *
59 */
da249a30 60 function user_update_password($user, $newpassword) {
61 $user = get_complete_user_data('id', $user->id);
b9ddb2d5 62 return update_internal_user_password($user, $newpassword);
63 }
64
6bc1e5d5 65 function can_signup() {
66 return true;
67 }
68
430759a5 69 /**
b9ddb2d5 70 * Sign up a new user ready for confirmation.
dd0feda5 71 * Password is passed in plaintext.
72 *
5d910388 73 * @param object $user new user object
dd0feda5 74 * @param boolean $notify print notice with link and terminate
b9ddb2d5 75 */
6bc1e5d5 76 function user_signup($user, $notify=true) {
5d910388 77 global $CFG, $DB;
831d450e 78 require_once($CFG->dirroot.'/user/profile/lib.php');
79
dd0feda5 80 $user->password = hash_internal_user_password($user->password);
81
5d910388 82 if (! ($user->id = $DB->insert_record('user', $user)) ) {
2b06294b 83 print_error('auth_emailnoinsert','auth_email');
b9ddb2d5 84 }
831d450e 85
86 /// Save any custom profile field information
87 profile_save_data($user);
88
14482ff9 89 $user = $DB->get_record('user', array('id'=>$user->id));
2942a5cd 90 events_trigger('user_created', $user);
91
b9ddb2d5 92 if (! send_confirmation_email($user)) {
2b06294b 93 print_error('auth_emailnoemail','auth_email');
b9ddb2d5 94 }
95
96 if ($notify) {
97 global $CFG;
98 $emailconfirm = get_string('emailconfirm');
cfc5b79b 99 $PAGE->navbar->add($emailconfirm);
100 $PAGE->set_title($emailconfirm);
101 $PAGE->set_heading($emailconfirm);
102 echo $OUTPUT->header();
b9ddb2d5 103 notice(get_string('emailconfirmsent', '', $user->email), "$CFG->wwwroot/index.php");
dd0feda5 104 } else {
105 return true;
139ebfdb 106 }
b9ddb2d5 107 }
108
6bc1e5d5 109 /**
110 * Returns true if plugin allows confirming of new users.
111 *
112 * @return bool
113 */
114 function can_confirm() {
115 return true;
116 }
117
430759a5 118 /**
b9ddb2d5 119 * Confirm the new user as registered.
dd0feda5 120 *
b9a66360 121 * @param string $username
122 * @param string $confirmsecret
b9ddb2d5 123 */
124 function user_confirm($username, $confirmsecret) {
b9a66360 125 global $DB;
b9ddb2d5 126 $user = get_complete_user_data('username', $username);
127
128 if (!empty($user)) {
129 if ($user->confirmed) {
130 return AUTH_CONFIRM_ALREADY;
dd0feda5 131
132 } else if ($user->auth != 'email') {
133 return AUTH_CONFIRM_ERROR;
134
b9a66360 135 } else if ($user->secret == $confirmsecret) { // They have provided the secret key to get in
136 if (!$DB->set_field("user", "confirmed", 1, array("id"=>$user->id))) {
b9ddb2d5 137 return AUTH_CONFIRM_FAIL;
138 }
b9a66360 139 if (!$DB->set_field("user", "firstaccess", time(), array("id"=>$user->id))) {
b9ddb2d5 140 return AUTH_CONFIRM_FAIL;
141 }
142 return AUTH_CONFIRM_OK;
143 }
dd0feda5 144 } else {
145 return AUTH_CONFIRM_ERROR;
b9ddb2d5 146 }
147 }
148
149 /**
150 * Returns true if this authentication plugin is 'internal'.
151 *
139ebfdb 152 * @return bool
b9ddb2d5 153 */
154 function is_internal() {
155 return true;
156 }
139ebfdb 157
b9ddb2d5 158 /**
159 * Returns true if this authentication plugin can change the user's
160 * password.
161 *
139ebfdb 162 * @return bool
b9ddb2d5 163 */
164 function can_change_password() {
165 return true;
166 }
139ebfdb 167
b9ddb2d5 168 /**
430759a5 169 * Returns the URL for changing the user's pw, or empty if the default can
b9ddb2d5 170 * be used.
171 *
430759a5 172 * @return mixed
b9ddb2d5 173 */
174 function change_password_url() {
430759a5 175 return ''; // use dafult internal method
176 }
177
178 /**
179 * Returns true if plugin allows resetting of internal password.
180 *
181 * @return bool
182 */
183 function can_reset_password() {
184 return true;
b9ddb2d5 185 }
139ebfdb 186
b9ddb2d5 187 /**
188 * Prints a form for configuring this authentication plugin.
189 *
190 * This function is called from admin/auth.php, and outputs a full page with
191 * a form for configuring this plugin.
192 *
193 * @param array $page An object containing all the data for this page.
194 */
139ebfdb 195 function config_form($config, $err, $user_fields) {
b9ddb2d5 196 include "config.html";
197 }
198
199 /**
200 * Processes and stores configuration data for this authentication plugin.
201 */
202 function process_config($config) {
5d6308d4 203 // set to defaults if undefined
204 if (!isset($config->recaptcha)) {
205 $config->recaptcha = false;
206 }
207
208 // save settings
209 set_config('recaptcha', $config->recaptcha, 'auth/email');
b9ddb2d5 210 return true;
211 }
9b5f87d2 212
213 /**
214 * Returns whether or not the captcha element is enabled, and the admin settings fulfil its requirements.
215 * @return bool
216 */
217 function is_captcha_enabled() {
218 global $CFG;
219 return isset($CFG->recaptchapublickey) && isset($CFG->recaptchaprivatekey) && get_config("auth/{$this->authtype}", 'recaptcha');
220 }
139ebfdb 221
b9ddb2d5 222}
223
224?>