Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / auth / imap / auth.php
blobc2c3cf0ae69de288a3d7c17cf8ff82fb7001f1af
1 <?php
3 /**
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');
21 /**
22 * IMAP authentication plugin.
24 class auth_plugin_imap extends auth_plugin_base {
26 /**
27 * Constructor.
29 function auth_plugin_imap() {
30 $this->authtype = 'imap';
31 $this->config = get_config('auth/imap');
34 /**
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');
45 return false;
48 global $CFG;
49 $hosts = split(';', $this->config->host); // Could be multiple hosts
51 foreach ($hosts as $host) { // Try each host in turn
52 $host = trim($host);
54 switch ($this->config->type) {
55 case 'imapssl':
56 $host = '{'.$host.":{$this->config->port}/imap/ssl}";
57 break;
59 case 'imapcert':
60 $host = '{'.$host.":{$this->config->port}/imap/ssl/novalidate-cert}";
61 break;
63 case 'imaptls':
64 $host = '{'.$host.":{$this->config->port}/imap/tls}";
65 break;
67 default:
68 $host = '{'.$host.":{$this->config->port}/imap}";
71 error_reporting(0);
72 $connection = imap_open($host, stripslashes($username), stripslashes($password), OP_HALFOPEN);
73 error_reporting($CFG->debug);
75 if ($connection) {
76 imap_close($connection);
77 return true;
81 return false; // No match
84 /**
85 * Returns true if this authentication plugin is 'internal'.
87 * @return bool
89 function is_internal() {
90 return false;
93 /**
94 * Returns true if this authentication plugin can change the user's
95 * password.
97 * @return bool
99 function can_change_password() {
100 return !empty($this->config->changepasswordurl);
104 * Returns the URL for changing the user's pw, or empty if the default can
105 * be used.
107 * @return string
109 function change_password_url() {
110 return $this->config->changepasswordurl;
114 * Prints a form for configuring this authentication plugin.
116 * This function is called from admin/auth.php, and outputs a full page with
117 * a form for configuring this plugin.
119 * @param array $page An object containing all the data for this page.
121 function config_form($config, $err, $user_fields) {
122 include "config.html";
126 * Processes and stores configuration data for this authentication plugin.
128 function process_config($config) {
129 // set to defaults if undefined
130 if (!isset ($config->host)) {
131 $config->host = '127.0.0.1';
133 if (!isset ($config->type)) {
134 $config->type = 'imap';
136 if (!isset ($config->port)) {
137 $config->port = '143';
139 if (!isset($config->changepasswordurl)) {
140 $config->changepasswordurl = '';
143 // save settings
144 set_config('host', $config->host, 'auth/imap');
145 set_config('type', $config->type, 'auth/imap');
146 set_config('port', $config->port, 'auth/imap');
147 set_config('changepasswordurl', $config->changepasswordurl, 'auth/imap');
149 return true;