Merge branch 'MDL-28684b-21' of git://github.com/bostelm/moodle into MOODLE_21_STABLE
[moodle.git] / auth / pop3 / auth.php
blob8fe4f94ad8dde5eb72fc96b3110bc9195e4c7a6a
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: POP3 Authentication
10 * Authenticates against a POP3 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 * POP3 authentication plugin.
24 class auth_plugin_pop3 extends auth_plugin_base {
26 /**
27 * Constructor.
29 function auth_plugin_pop3() {
30 $this->authtype = 'pop3';
31 $this->config = get_config('auth/pop3');
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
39 * @param string $password The password
40 * @return bool Authentication success or failure.
42 function user_login($username, $password) {
43 if (! function_exists('imap_open')) {
44 print_error('auth_pop3notinstalled','auth_pop3');
45 exit;
48 global $CFG;
49 $hosts = explode(';', $this->config->host); // Could be multiple hosts
50 foreach ($hosts as $host) { // Try each host in turn
51 $host = trim($host);
53 // remove any trailing slash
54 if (substr($host, -1) == '/') {
55 $host = substr($host, 0, strlen($host) - 1);
58 switch ($this->config->type) {
59 case 'pop3':
60 $host = '{'.$host.":{$this->config->port}/pop3}{$this->config->mailbox}";
61 break;
63 case 'pop3notls':
64 $host = '{'.$host.":{$this->config->port}/pop3/notls}{$this->config->mailbox}";
65 break;
67 case 'pop3cert':
68 $host = '{'.$host.":{$this->config->port}/pop3/ssl/novalidate-cert}{$this->config->mailbox}";
69 break;
72 error_reporting(0);
73 $connection = imap_open($host, $username, $password);
74 error_reporting($CFG->debug);
76 if ($connection) {
77 imap_close($connection);
78 return true;
81 return false; // No matches found
84 function prevent_local_passwords() {
85 return true;
88 /**
89 * Returns true if this authentication plugin is 'internal'.
91 * @return bool
93 function is_internal() {
94 return false;
97 /**
98 * Returns true if this authentication plugin can change the user's
99 * password.
101 * @return bool
103 function can_change_password() {
104 return !empty($this->config->changepasswordurl);
108 * Returns the URL for changing the user's pw, or false if the default can
109 * be used.
111 * @return moodle_url
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) {
126 global $OUTPUT;
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 = 'pop3notls';
142 if (!isset ($config->port)) {
143 $config->port = '143';
145 if (!isset ($config->mailbox)) {
146 $config->mailbox = 'INBOX';
148 if (!isset($config->changepasswordurl)) {
149 $config->changepasswordurl = '';
152 // save settings
153 set_config('host', $config->host, 'auth/pop3');
154 set_config('type', $config->type, 'auth/pop3');
155 set_config('port', $config->port, 'auth/pop3');
156 set_config('mailbox', $config->mailbox, 'auth/pop3');
157 set_config('changepasswordurl', $config->changepasswordurl, 'auth/pop3');
159 return true;