[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/auth/pop3/ -> auth.php (source)

   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Authentication Plugin: POP3 Authentication
  19   * Authenticates against a POP3 server.
  20   *
  21   * @package auth_pop3
  22   * @author Martin Dougiamas
  23   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->libdir.'/authlib.php');
  29  
  30  /**
  31   * POP3 authentication plugin.
  32   */
  33  class auth_plugin_pop3 extends auth_plugin_base {
  34  
  35      /**
  36       * Constructor.
  37       */
  38      function auth_plugin_pop3() {
  39          $this->authtype = 'pop3';
  40          $this->config = get_config('auth/pop3');
  41      }
  42  
  43      /**
  44       * Returns true if the username and password work and false if they are
  45       * wrong or don't exist.
  46       *
  47       * @param string $username The username
  48       * @param string $password The password
  49       * @return bool Authentication success or failure.
  50       */
  51      function user_login($username, $password) {
  52          if (! function_exists('imap_open')) {
  53              print_error('auth_pop3notinstalled','auth_pop3');
  54              exit;
  55          }
  56  
  57          global $CFG;
  58          $hosts = explode(';', $this->config->host);   // Could be multiple hosts
  59          foreach ($hosts as $host) {                 // Try each host in turn
  60              $host = trim($host);
  61  
  62              // remove any trailing slash
  63              if (substr($host, -1) == '/') {
  64                  $host = substr($host, 0, strlen($host) - 1);
  65              }
  66  
  67              switch ($this->config->type) {
  68                  case 'pop3':
  69                      $host = '{'.$host.":{$this->config->port}/pop3}{$this->config->mailbox}";
  70                  break;
  71  
  72                  case 'pop3notls':
  73                      $host = '{'.$host.":{$this->config->port}/pop3/notls}{$this->config->mailbox}";
  74                  break;
  75  
  76                  case 'pop3cert':
  77                      $host = '{'.$host.":{$this->config->port}/pop3/ssl/novalidate-cert}{$this->config->mailbox}";
  78                  break;
  79              }
  80  
  81              error_reporting(0);
  82              $connection = imap_open($host, $username, $password);
  83              error_reporting($CFG->debug);
  84  
  85              if ($connection) {
  86                  imap_close($connection);
  87                  return true;
  88              }
  89          }
  90          return false;  // No matches found
  91      }
  92  
  93      function prevent_local_passwords() {
  94          return true;
  95      }
  96  
  97      /**
  98       * Returns true if this authentication plugin is 'internal'.
  99       *
 100       * @return bool
 101       */
 102      function is_internal() {
 103          return false;
 104      }
 105  
 106      /**
 107       * Returns true if this authentication plugin can change the user's
 108       * password.
 109       *
 110       * @return bool
 111       */
 112      function can_change_password() {
 113          return !empty($this->config->changepasswordurl);
 114      }
 115  
 116      /**
 117       * Returns the URL for changing the user's pw, or false if the default can
 118       * be used.
 119       *
 120       * @return moodle_url
 121       */
 122      function change_password_url() {
 123          if (!empty($this->config->changepasswordurl)) {
 124              return new moodle_url($this->config->changepasswordurl);
 125          } else {
 126              return null;
 127          }
 128      }
 129  
 130      /**
 131       * Prints a form for configuring this authentication plugin.
 132       *
 133       * This function is called from admin/auth.php, and outputs a full page with
 134       * a form for configuring this plugin.
 135       *
 136       * @param array $page An object containing all the data for this page.
 137       */
 138      function config_form($config, $err, $user_fields) {
 139          global $OUTPUT;
 140  
 141          include  "config.html";
 142      }
 143  
 144      /**
 145       * Processes and stores configuration data for this authentication plugin.
 146       */
 147      function process_config($config) {
 148          // set to defaults if undefined
 149          if (!isset ($config->host)) {
 150              $config->host = '127.0.0.1';
 151          }
 152          if (!isset ($config->type)) {
 153              $config->type = 'pop3notls';
 154          }
 155          if (!isset ($config->port)) {
 156              $config->port = '143';
 157          }
 158          if (!isset ($config->mailbox)) {
 159              $config->mailbox = 'INBOX';
 160          }
 161          if (!isset($config->changepasswordurl)) {
 162              $config->changepasswordurl = '';
 163          }
 164  
 165          // save settings
 166          set_config('host',    $config->host,    'auth/pop3');
 167          set_config('type',    $config->type,    'auth/pop3');
 168          set_config('port',    $config->port,    'auth/pop3');
 169          set_config('mailbox', $config->mailbox, 'auth/pop3');
 170          set_config('changepasswordurl', $config->changepasswordurl, 'auth/pop3');
 171  
 172          return true;
 173      }
 174  
 175  }
 176  
 177  


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1