MDL-13915: Fixed add_to_log when very long url is specified (now doesn't try to inser...
[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) {
b7b50143 43 global $CFG;
44 if ($user = get_record('user', '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 *
73 * @param object $user new user object (with system magic quotes)
74 * @param boolean $notify print notice with link and terminate
b9ddb2d5 75 */
6bc1e5d5 76 function user_signup($user, $notify=true) {
831d450e 77 global $CFG;
78 require_once($CFG->dirroot.'/user/profile/lib.php');
79
dd0feda5 80 $user->password = hash_internal_user_password($user->password);
81
b9ddb2d5 82 if (! ($user->id = insert_record('user', $user)) ) {
a9ad3633 83 print_error('auth_emailnoinsert','auth');
b9ddb2d5 84 }
831d450e 85
86 /// Save any custom profile field information
87 profile_save_data($user);
88
b9ddb2d5 89 if (! send_confirmation_email($user)) {
a9ad3633 90 print_error('auth_emailnoemail','auth');
b9ddb2d5 91 }
92
93 if ($notify) {
94 global $CFG;
95 $emailconfirm = get_string('emailconfirm');
e295df44 96 $navlinks = array();
97 $navlinks[] = array('name' => $emailconfirm, 'link' => null, 'type' => 'misc');
98 $navigation = build_navigation($navlinks);
99
100 print_header($emailconfirm, $emailconfirm, $navigation);
b9ddb2d5 101 notice(get_string('emailconfirmsent', '', $user->email), "$CFG->wwwroot/index.php");
dd0feda5 102 } else {
103 return true;
139ebfdb 104 }
b9ddb2d5 105 }
106
6bc1e5d5 107 /**
108 * Returns true if plugin allows confirming of new users.
109 *
110 * @return bool
111 */
112 function can_confirm() {
113 return true;
114 }
115
430759a5 116 /**
b9ddb2d5 117 * Confirm the new user as registered.
dd0feda5 118 *
119 * @param string $username (with system magic quotes)
120 * @param string $confirmsecret (with system magic quotes)
b9ddb2d5 121 */
122 function user_confirm($username, $confirmsecret) {
123 $user = get_complete_user_data('username', $username);
124
125 if (!empty($user)) {
126 if ($user->confirmed) {
127 return AUTH_CONFIRM_ALREADY;
dd0feda5 128
129 } else if ($user->auth != 'email') {
130 return AUTH_CONFIRM_ERROR;
131
132 } else if ($user->secret == stripslashes($confirmsecret)) { // They have provided the secret key to get in
b9ddb2d5 133 if (!set_field("user", "confirmed", 1, "id", $user->id)) {
134 return AUTH_CONFIRM_FAIL;
135 }
136 if (!set_field("user", "firstaccess", time(), "id", $user->id)) {
137 return AUTH_CONFIRM_FAIL;
138 }
139 return AUTH_CONFIRM_OK;
140 }
dd0feda5 141 } else {
142 return AUTH_CONFIRM_ERROR;
b9ddb2d5 143 }
144 }
145
146 /**
147 * Returns true if this authentication plugin is 'internal'.
148 *
139ebfdb 149 * @return bool
b9ddb2d5 150 */
151 function is_internal() {
152 return true;
153 }
139ebfdb 154
b9ddb2d5 155 /**
156 * Returns true if this authentication plugin can change the user's
157 * password.
158 *
139ebfdb 159 * @return bool
b9ddb2d5 160 */
161 function can_change_password() {
162 return true;
163 }
139ebfdb 164
b9ddb2d5 165 /**
430759a5 166 * Returns the URL for changing the user's pw, or empty if the default can
b9ddb2d5 167 * be used.
168 *
430759a5 169 * @return mixed
b9ddb2d5 170 */
171 function change_password_url() {
430759a5 172 return ''; // use dafult internal method
173 }
174
175 /**
176 * Returns true if plugin allows resetting of internal password.
177 *
178 * @return bool
179 */
180 function can_reset_password() {
181 return true;
b9ddb2d5 182 }
139ebfdb 183
b9ddb2d5 184 /**
185 * Prints a form for configuring this authentication plugin.
186 *
187 * This function is called from admin/auth.php, and outputs a full page with
188 * a form for configuring this plugin.
189 *
190 * @param array $page An object containing all the data for this page.
191 */
139ebfdb 192 function config_form($config, $err, $user_fields) {
b9ddb2d5 193 include "config.html";
194 }
195
196 /**
197 * Processes and stores configuration data for this authentication plugin.
198 */
199 function process_config($config) {
5d6308d4 200 // set to defaults if undefined
201 if (!isset($config->recaptcha)) {
202 $config->recaptcha = false;
203 }
204
205 // save settings
206 set_config('recaptcha', $config->recaptcha, 'auth/email');
b9ddb2d5 207 return true;
208 }
139ebfdb 209
b9ddb2d5 210}
211
212?>