Another wretched adslashes problem. Roll on placeholder. Thanks to Howard Miller...
[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
15// This page cannot be called directly
16if (!isset($CFG)) exit;
17
18/**
19 * Error codes for user confirm
20 */
21define('AUTH_CONFIRM_FAIL', 0);
22define('AUTH_CONFIRM_OK', 1);
23define('AUTH_CONFIRM_ALREADY', 2);
24define('AUTH_CONFIRM_ERROR', 3);
25// TODO: instead of integers these could be the language keys?
26
27
28/**
29 * Email authentication plugin.
30 */
31class auth_plugin_email {
32
33 /**
34 * The configuration details for the plugin.
35 */
36 var $config;
37
38 /**
39 * Constructor.
40 */
41 function auth_plugin_email() {
42 $this->config = get_config('auth/email');
43 }
44
45 /**
46 * Returns true if the username and password work and false if they are
47 * wrong or don't exist.
48 *
49 * @param string $username The username
50 * @param string $password The password
51 * @returns bool Authentication success or failure.
52 */
53 function user_login ($username, $password) {
b7b50143 54 global $CFG;
55 if ($user = get_record('user', 'username', $username, 'mnethostid', $CFG->mnet_localhost_id)) {
b9ddb2d5 56 return validate_internal_user_password($user, $password);
57 }
58 return false;
59 }
60
fb5c7739 61 /**
b9ddb2d5 62 * Updates the user's password.
63 *
64 * called when the user password is updated.
65 *
fb5c7739 66 * @param object $user User
67 * @param string $newpassword Plaintext password
b9ddb2d5 68 * @return boolean result
69 *
70 */
da249a30 71 function user_update_password($user, $newpassword) {
72 $user = get_complete_user_data('id', $user->id);
b9ddb2d5 73 return update_internal_user_password($user, $newpassword);
74 }
75
76 /*
77 * Sign up a new user ready for confirmation.
78 */
79 function user_signup($user, $notify = true) {
80 if (! ($user->id = insert_record('user', $user)) ) {
a9ad3633 81 print_error('auth_emailnoinsert','auth');
b9ddb2d5 82 }
83 if (! send_confirmation_email($user)) {
a9ad3633 84 print_error('auth_emailnoemail','auth');
b9ddb2d5 85 }
86
87 if ($notify) {
88 global $CFG;
89 $emailconfirm = get_string('emailconfirm');
90 print_header($emailconfirm, $emailconfirm, $emailconfirm);
91 notice(get_string('emailconfirmsent', '', $user->email), "$CFG->wwwroot/index.php");
92 }
93 }
94
95 /*
96 * Confirm the new user as registered.
97 */
98 function user_confirm($username, $confirmsecret) {
99 $user = get_complete_user_data('username', $username);
100
101 if (!empty($user)) {
102 if ($user->confirmed) {
103 return AUTH_CONFIRM_ALREADY;
104 }
105 if ($user->secret == $confirmsecret) { // They have provided the secret key to get in
106 if (!set_field("user", "confirmed", 1, "id", $user->id)) {
107 return AUTH_CONFIRM_FAIL;
108 }
109 if (!set_field("user", "firstaccess", time(), "id", $user->id)) {
110 return AUTH_CONFIRM_FAIL;
111 }
112 return AUTH_CONFIRM_OK;
113 }
114 }
115 }
116
117 /**
118 * Returns true if this authentication plugin is 'internal'.
119 *
120 * @returns bool
121 */
122 function is_internal() {
123 return true;
124 }
125
126 /**
127 * Returns true if this authentication plugin can change the user's
128 * password.
129 *
130 * @returns bool
131 */
132 function can_change_password() {
133 return true;
134 }
135
136 /**
137 * Returns the URL for changing the user's pw, or false if the default can
138 * be used.
139 *
140 * @returns bool
141 */
142 function change_password_url() {
143 return false;
144 }
145
146 /**
147 * Prints a form for configuring this authentication plugin.
148 *
149 * This function is called from admin/auth.php, and outputs a full page with
150 * a form for configuring this plugin.
151 *
152 * @param array $page An object containing all the data for this page.
153 */
154 function config_form($config, $err) {
155 include "config.html";
156 }
157
158 /**
159 * Processes and stores configuration data for this authentication plugin.
160 */
161 function process_config($config) {
162 return true;
163 }
164
165}
166
167?>