MDL-8857
[moodle-linuxchix.git] / auth / pop3 / auth.php
blob431d20796a1ed2d053e874fa22ed6ac84ffff1f2
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 /**
20 * POP3 authentication plugin.
22 class auth_plugin_pop3 {
24 /**
25 * The configuration details for the plugin.
27 var $config;
29 /**
30 * Constructor.
32 function auth_plugin_pop3() {
33 $this->config = get_config('auth/pop3');
36 /**
37 * Returns true if the username and password work and false if they are
38 * wrong or don't exist.
40 * @param string $username The username (with system magic quotes)
41 * @param string $password The password (with system magic quotes)
42 * @return bool Authentication success or failure.
44 function user_login($username, $password) {
45 if (! function_exists('imap_open')) {
46 print_error('auth_pop3notinstalled','auth');
47 exit;
50 global $CFG;
51 $hosts = split(';', $this->config->host); // Could be multiple hosts
52 foreach ($hosts as $host) { // Try each host in turn
53 $host = trim($host);
55 // remove any trailing slash
56 if (substr($host, -1) == '/') {
57 $host = substr($host, 0, strlen($host) - 1);
60 switch ($this->config->type) {
61 case 'pop3':
62 $host = '{'.$host.":{$this->config->port}/pop3}{$this->config->mailbox}";
63 break;
65 case 'pop3notls':
66 $host = '{'.$host.":{$this->config->port}/pop3/notls}{$this->config->mailbox}";
67 break;
69 case 'pop3cert':
70 $host = '{'.$host.":{$this->config->port}/pop3/ssl/novalidate-cert}{$this->config->mailbox}";
71 break;
74 error_reporting(0);
75 $connection = imap_open($host, stripslashes($username), stripslashes($password));
76 error_reporting($CFG->debug);
78 if ($connection) {
79 imap_close($connection);
80 return true;
83 return false; // No matches found
86 /**
87 * Returns true if this authentication plugin is 'internal'.
89 * @return bool
91 function is_internal() {
92 return false;
95 /**
96 * Returns true if this authentication plugin can change the user's
97 * password.
99 * @return bool
101 function can_change_password() {
102 return !empty($this->config->changepasswordurl);
106 * Returns the URL for changing the user's pw, or false if the default can
107 * be used.
109 * @return bool
111 function change_password_url() {
112 return $this->config->changepasswordurl;
116 * Prints a form for configuring this authentication plugin.
118 * This function is called from admin/auth.php, and outputs a full page with
119 * a form for configuring this plugin.
121 * @param array $page An object containing all the data for this page.
123 function config_form($config, $err, $user_fields) {
124 include "config.html";
128 * Processes and stores configuration data for this authentication plugin.
130 function process_config($config) {
131 // set to defaults if undefined
132 if (!isset ($config->host)) {
133 $config->host = '127.0.0.1';
135 if (!isset ($config->type)) {
136 $config->type = 'pop3notls';
138 if (!isset ($config->port)) {
139 $config->port = '143';
141 if (!isset ($config->mailbox)) {
142 $config->mailbox = 'INBOX';
144 if (!isset($config->changepasswordurl)) {
145 $config->changepasswordurl = '';
148 // save settings
149 set_config('host', $config->host, 'auth/pop3');
150 set_config('type', $config->type, 'auth/pop3');
151 set_config('port', $config->port, 'auth/pop3');
152 set_config('mailbox', $config->mailbox, 'auth/pop3');
153 set_config('changepasswordurl', $config->changepasswordurl, 'auth/pop3');
155 return true;