MDL-51048 backup: Removing unnecessary var
[moodle.git] / auth / pop3 / auth.php
blob5e25aa95bcf5f4f9b18314afdcf5b0dff1dcc96f
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Authentication Plugin: POP3 Authentication
19 * Authenticates against a POP3 server.
21 * @package auth_pop3
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');
30 /**
31 * POP3 authentication plugin.
33 class auth_plugin_pop3 extends auth_plugin_base {
35 /**
36 * Constructor.
38 function auth_plugin_pop3() {
39 $this->authtype = 'pop3';
40 $this->config = get_config('auth/pop3');
43 /**
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');
54 exit;
57 global $CFG;
58 $hosts = explode(';', $this->config->host); // Could be multiple hosts
59 foreach ($hosts as $host) { // Try each host in turn
60 $host = trim($host);
62 // remove any trailing slash
63 if (substr($host, -1) == '/') {
64 $host = substr($host, 0, strlen($host) - 1);
67 switch ($this->config->type) {
68 case 'pop3':
69 $host = '{'.$host.":{$this->config->port}/pop3}{$this->config->mailbox}";
70 break;
72 case 'pop3notls':
73 $host = '{'.$host.":{$this->config->port}/pop3/notls}{$this->config->mailbox}";
74 break;
76 case 'pop3cert':
77 $host = '{'.$host.":{$this->config->port}/pop3/ssl/novalidate-cert}{$this->config->mailbox}";
78 break;
81 error_reporting(0);
82 $connection = imap_open($host, $username, $password);
83 error_reporting($CFG->debug);
85 if ($connection) {
86 imap_close($connection);
87 return true;
90 return false; // No matches found
93 function prevent_local_passwords() {
94 return true;
97 /**
98 * Returns true if this authentication plugin is 'internal'.
100 * @return bool
102 function is_internal() {
103 return false;
107 * Returns true if this authentication plugin can change the user's
108 * password.
110 * @return bool
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
118 * be used.
120 * @return moodle_url
122 function change_password_url() {
123 if (!empty($this->config->changepasswordurl)) {
124 return new moodle_url($this->config->changepasswordurl);
125 } else {
126 return null;
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) {
139 global $OUTPUT;
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 = '';
165 // save settings
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');
172 return true;