4 * @author Martin Dougiamas
5 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
6 * @package moodle multiauth
8 * Authentication Plugin: IMAP Authentication
10 * Authenticates against an IMAP server.
12 * 2006-08-31 File created.
15 if (!defined('MOODLE_INTERNAL')) {
16 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
19 require_once($CFG->libdir
.'/authlib.php');
22 * IMAP authentication plugin.
24 class auth_plugin_imap
extends auth_plugin_base
{
29 function auth_plugin_imap() {
30 $this->authtype
= 'imap';
31 $this->config
= get_config('auth/imap');
35 * Returns true if the username and password work and false if they are
36 * wrong or don't exist.
38 * @param string $username The username (with system magic quotes)
39 * @param string $password The password (with system magic quotes)
40 * @return bool Authentication success or failure.
42 function user_login ($username, $password) {
43 if (! function_exists('imap_open')) {
44 print_error('auth_imapnotinstalled','mnet');
49 $hosts = explode(';', $this->config
->host
); // Could be multiple hosts
51 foreach ($hosts as $host) { // Try each host in turn
54 switch ($this->config
->type
) {
56 $host = '{'.$host.":{$this->config->port}/imap/ssl}";
60 $host = '{'.$host.":{$this->config->port}/imap/ssl/novalidate-cert}";
64 $host = '{'.$host.":{$this->config->port}/imap/tls}";
68 $host = '{'.$host.":{$this->config->port}/imap}";
72 $connection = imap_open($host, $username, $password, OP_HALFOPEN
);
73 error_reporting($CFG->debug
);
76 imap_close($connection);
81 return false; // No match
84 function prevent_local_passwords() {
89 * Returns true if this authentication plugin is 'internal'.
93 function is_internal() {
98 * Returns true if this authentication plugin can change the user's
103 function can_change_password() {
104 return !empty($this->config
->changepasswordurl
);
108 * Returns the URL for changing the user's pw, or empty if the default can
113 function change_password_url() {
114 return new moodle_url($this->config
->changepasswordurl
);
118 * Prints a form for configuring this authentication plugin.
120 * This function is called from admin/auth.php, and outputs a full page with
121 * a form for configuring this plugin.
123 * @param array $page An object containing all the data for this page.
125 function config_form($config, $err, $user_fields) {
128 include "config.html";
132 * Processes and stores configuration data for this authentication plugin.
134 function process_config($config) {
135 // set to defaults if undefined
136 if (!isset ($config->host
)) {
137 $config->host
= '127.0.0.1';
139 if (!isset ($config->type
)) {
140 $config->type
= 'imap';
142 if (!isset ($config->port
)) {
143 $config->port
= '143';
145 if (!isset($config->changepasswordurl
)) {
146 $config->changepasswordurl
= '';
150 set_config('host', $config->host
, 'auth/imap');
151 set_config('type', $config->type
, 'auth/imap');
152 set_config('port', $config->port
, 'auth/imap');
153 set_config('changepasswordurl', $config->changepasswordurl
, 'auth/imap');