2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Authentication Plugin: POP3 Authentication
19 * Authenticates against a POP3 server.
22 * @author Martin Dougiamas
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
26 defined('MOODLE_INTERNAL') ||
die();
28 require_once($CFG->libdir
.'/authlib.php');
31 * POP3 authentication plugin.
33 class auth_plugin_pop3
extends auth_plugin_base
{
38 function auth_plugin_pop3() {
39 $this->authtype
= 'pop3';
40 $this->config
= get_config('auth/pop3');
44 * Returns true if the username and password work and false if they are
45 * wrong or don't exist.
47 * @param string $username The username
48 * @param string $password The password
49 * @return bool Authentication success or failure.
51 function user_login($username, $password) {
52 if (! function_exists('imap_open')) {
53 print_error('auth_pop3notinstalled','auth_pop3');
58 $hosts = explode(';', $this->config
->host
); // Could be multiple hosts
59 foreach ($hosts as $host) { // Try each host in turn
62 // remove any trailing slash
63 if (substr($host, -1) == '/') {
64 $host = substr($host, 0, strlen($host) - 1);
67 switch ($this->config
->type
) {
69 $host = '{'.$host.":{$this->config->port}/pop3}{$this->config->mailbox}";
73 $host = '{'.$host.":{$this->config->port}/pop3/notls}{$this->config->mailbox}";
77 $host = '{'.$host.":{$this->config->port}/pop3/ssl/novalidate-cert}{$this->config->mailbox}";
82 $connection = imap_open($host, $username, $password);
83 error_reporting($CFG->debug
);
86 imap_close($connection);
90 return false; // No matches found
93 function prevent_local_passwords() {
98 * Returns true if this authentication plugin is 'internal'.
102 function is_internal() {
107 * Returns true if this authentication plugin can change the user's
112 function can_change_password() {
113 return !empty($this->config
->changepasswordurl
);
117 * Returns the URL for changing the user's pw, or false if the default can
122 function change_password_url() {
123 if (!empty($this->config
->changepasswordurl
)) {
124 return new moodle_url($this->config
->changepasswordurl
);
131 * Prints a form for configuring this authentication plugin.
133 * This function is called from admin/auth.php, and outputs a full page with
134 * a form for configuring this plugin.
136 * @param array $page An object containing all the data for this page.
138 function config_form($config, $err, $user_fields) {
141 include "config.html";
145 * Processes and stores configuration data for this authentication plugin.
147 function process_config($config) {
148 // set to defaults if undefined
149 if (!isset ($config->host
)) {
150 $config->host
= '127.0.0.1';
152 if (!isset ($config->type
)) {
153 $config->type
= 'pop3notls';
155 if (!isset ($config->port
)) {
156 $config->port
= '143';
158 if (!isset ($config->mailbox
)) {
159 $config->mailbox
= 'INBOX';
161 if (!isset($config->changepasswordurl
)) {
162 $config->changepasswordurl
= '';
166 set_config('host', $config->host
, 'auth/pop3');
167 set_config('type', $config->type
, 'auth/pop3');
168 set_config('port', $config->port
, 'auth/pop3');
169 set_config('mailbox', $config->mailbox
, 'auth/pop3');
170 set_config('changepasswordurl', $config->changepasswordurl
, 'auth/pop3');