mnet: Replace hardcoded strings: MDL-8069
[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) {
54 if ($user = get_record('user', 'username', $username)) {
55 return validate_internal_user_password($user, $password);
56 }
57 return false;
58 }
59
60 /*
61 * Updates the user's password.
62 *
63 * called when the user password is updated.
64 *
65 * @param mixed $username Username
66 * @param mixed $newpassword Plaintext password
67 * @return boolean result
68 *
69 */
70 function user_update_password($username, $newpassword) {
71 $user = get_complete_user_data('username', $username);
72 return update_internal_user_password($user, $newpassword);
73 }
74
75 /*
76 * Sign up a new user ready for confirmation.
77 */
78 function user_signup($user, $notify = true) {
79 if (! ($user->id = insert_record('user', $user)) ) {
80 error('Could not add your record to the database!');
81 }
82 if (! send_confirmation_email($user)) {
83 error('Tried to send you an email but failed!');
84 }
85
86 if ($notify) {
87 global $CFG;
88 $emailconfirm = get_string('emailconfirm');
89 print_header($emailconfirm, $emailconfirm, $emailconfirm);
90 notice(get_string('emailconfirmsent', '', $user->email), "$CFG->wwwroot/index.php");
91 }
92 }
93
94 /*
95 * Confirm the new user as registered.
96 */
97 function user_confirm($username, $confirmsecret) {
98 $user = get_complete_user_data('username', $username);
99
100 if (!empty($user)) {
101 if ($user->confirmed) {
102 return AUTH_CONFIRM_ALREADY;
103 }
104 if ($user->secret == $confirmsecret) { // They have provided the secret key to get in
105 if (!set_field("user", "confirmed", 1, "id", $user->id)) {
106 return AUTH_CONFIRM_FAIL;
107 }
108 if (!set_field("user", "firstaccess", time(), "id", $user->id)) {
109 return AUTH_CONFIRM_FAIL;
110 }
111 return AUTH_CONFIRM_OK;
112 }
113 }
114 }
115
116 /**
117 * Returns true if this authentication plugin is 'internal'.
118 *
119 * @returns bool
120 */
121 function is_internal() {
122 return true;
123 }
124
125 /**
126 * Returns true if this authentication plugin can change the user's
127 * password.
128 *
129 * @returns bool
130 */
131 function can_change_password() {
132 return true;
133 }
134
135 /**
136 * Returns the URL for changing the user's pw, or false if the default can
137 * be used.
138 *
139 * @returns bool
140 */
141 function change_password_url() {
142 return false;
143 }
144
145 /**
146 * Prints a form for configuring this authentication plugin.
147 *
148 * This function is called from admin/auth.php, and outputs a full page with
149 * a form for configuring this plugin.
150 *
151 * @param array $page An object containing all the data for this page.
152 */
153 function config_form($config, $err) {
154 include "config.html";
155 }
156
157 /**
158 * Processes and stores configuration data for this authentication plugin.
159 */
160 function process_config($config) {
161 return true;
162 }
163
164}
165
166?>