Moodle release 3.0.3
[moodle.git] / auth / imap / auth.php
blobe43af0f8bd32a7e0af770814450e8689d73333d5
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: IMAP Authentication
19 * Authenticates against an IMAP server.
21 * @package auth_imap
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 * IMAP authentication plugin.
33 class auth_plugin_imap extends auth_plugin_base {
35 /**
36 * Constructor.
38 public function __construct() {
39 $this->authtype = 'imap';
40 $this->config = get_config('auth/imap');
43 /**
44 * Old syntax of class constructor for backward compatibility.
46 public function auth_plugin_imap() {
47 self::__construct();
50 /**
51 * Returns true if the username and password work and false if they are
52 * wrong or don't exist.
54 * @param string $username The username (with system magic quotes)
55 * @param string $password The password (with system magic quotes)
56 * @return bool Authentication success or failure.
58 function user_login ($username, $password) {
59 if (! function_exists('imap_open')) {
60 print_error('auth_imapnotinstalled','mnet');
61 return false;
64 global $CFG;
65 $hosts = explode(';', $this->config->host); // Could be multiple hosts
67 foreach ($hosts as $host) { // Try each host in turn
68 $host = trim($host);
70 switch ($this->config->type) {
71 case 'imapssl':
72 $host = '{'.$host.":{$this->config->port}/imap/ssl}";
73 break;
75 case 'imapcert':
76 $host = '{'.$host.":{$this->config->port}/imap/ssl/novalidate-cert}";
77 break;
79 case 'imaptls':
80 $host = '{'.$host.":{$this->config->port}/imap/tls}";
81 break;
83 case 'imapnosslcert':
84 $host = '{'.$host.":{$this->config->port}/imap/novalidate-cert}";
85 break;
87 default:
88 $host = '{'.$host.":{$this->config->port}/imap}";
91 error_reporting(0);
92 $connection = imap_open($host, $username, $password, OP_HALFOPEN);
93 error_reporting($CFG->debug);
95 if ($connection) {
96 imap_close($connection);
97 return true;
101 return false; // No match
104 function prevent_local_passwords() {
105 return true;
109 * Returns true if this authentication plugin is 'internal'.
111 * @return bool
113 function is_internal() {
114 return false;
118 * Returns true if this authentication plugin can change the user's
119 * password.
121 * @return bool
123 function can_change_password() {
124 return !empty($this->config->changepasswordurl);
128 * Returns the URL for changing the user's pw, or empty if the default can
129 * be used.
131 * @return moodle_url
133 function change_password_url() {
134 if (!empty($this->config->changepasswordurl)) {
135 return new moodle_url($this->config->changepasswordurl);
136 } else {
137 return null;
142 * Prints a form for configuring this authentication plugin.
144 * This function is called from admin/auth.php, and outputs a full page with
145 * a form for configuring this plugin.
147 * @param array $page An object containing all the data for this page.
149 function config_form($config, $err, $user_fields) {
150 global $OUTPUT;
152 include "config.html";
156 * Processes and stores configuration data for this authentication plugin.
158 function process_config($config) {
159 // set to defaults if undefined
160 if (!isset ($config->host)) {
161 $config->host = '127.0.0.1';
163 if (!isset ($config->type)) {
164 $config->type = 'imap';
166 if (!isset ($config->port)) {
167 $config->port = '143';
169 if (!isset($config->changepasswordurl)) {
170 $config->changepasswordurl = '';
173 // save settings
174 set_config('host', $config->host, 'auth/imap');
175 set_config('type', $config->type, 'auth/imap');
176 set_config('port', $config->port, 'auth/imap');
177 set_config('changepasswordurl', $config->changepasswordurl, 'auth/imap');
179 return true;