Merge branch 'MDL-49205_27' of git://github.com/timhunt/moodle into MOODLE_27_STABLE
[moodle.git] / auth / nntp / auth.php
blobc2ad28319db4407c059571c331a13a921539db94
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: NNTP Authentication
19 * Authenticates against an NNTP server.
21 * @package auth_nntp
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 * NNTP authentication plugin.
33 class auth_plugin_nntp extends auth_plugin_base {
35 /**
36 * Constructor.
38 function auth_plugin_nntp() {
39 $this->authtype = 'nntp';
40 $this->config = get_config('auth/nntp');
43 /**
44 * Returns true if the username and password work and false if they are
45 * wrong or don't exist.
47 * @param string $username The username
48 * @param string $password The password
49 * @return bool Authentication success or failure.
51 function user_login ($username, $password) {
52 if (! function_exists('imap_open')) {
53 print_error('auth_nntpnotinstalled','auth_nntp');
54 exit;
57 global $CFG;
59 // try each multiple host
60 $hosts = explode(';', $this->config->host);
61 foreach ($hosts as $host) {
62 $host = '{' . trim($host) . ':' . $this->config->port . '/nntp}';
64 error_reporting(0);
65 $connection = imap_open($host, $username, $password, OP_HALFOPEN);
66 error_reporting($CFG->debug);
68 if ($connection) {
69 imap_close($connection);
70 return true;
73 return false;
76 function prevent_local_passwords() {
77 return true;
80 /**
81 * Returns true if this authentication plugin is 'internal'.
83 * @return bool
85 function is_internal() {
86 return false;
89 /**
90 * Returns true if this authentication plugin can change the user's
91 * password.
93 * @return bool
95 function can_change_password() {
96 return false;
99 /**
100 * Prints a form for configuring this authentication plugin.
102 * This function is called from admin/auth.php, and outputs a full page with
103 * a form for configuring this plugin.
105 * @param array $page An object containing all the data for this page.
107 function config_form($config, $err, $user_fields) {
108 include "config.html";
112 * Processes and stores configuration data for this authentication plugin.
114 function process_config($config) {
115 // set to defaults if undefined
116 if (!isset ($config->host)) {
117 $config->host = '127.0.0.1';
119 if (!isset ($config->port)) {
120 $config->port = '119';
122 if (!isset($config->changepasswordurl)) {
123 $config->changepasswordurl = '';
126 // save settings
127 set_config('host', $config->host, 'auth/nntp');
128 set_config('port', $config->port, 'auth/nntp');
129 set_config('changepasswordurl', $config->changepasswordurl, 'auth/nntp');
131 return true;