[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/auth/imap/ -> 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: IMAP Authentication
  19   * Authenticates against an IMAP server.
  20   *
  21   * @package auth_imap
  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   * IMAP authentication plugin.
  32   */
  33  class auth_plugin_imap extends auth_plugin_base {
  34  
  35      /**
  36       * Constructor.
  37       */
  38      public function __construct() {
  39          $this->authtype = 'imap';
  40          $this->config = get_config('auth/imap');
  41      }
  42  
  43      /**
  44       * Old syntax of class constructor. Deprecated in PHP7.
  45       *
  46       * @deprecated since Moodle 3.1
  47       */
  48      public function auth_plugin_imap() {
  49          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  50          self::__construct();
  51      }
  52  
  53      /**
  54       * Returns true if the username and password work and false if they are
  55       * wrong or don't exist.
  56       *
  57       * @param string $username The username (with system magic quotes)
  58       * @param string $password The password (with system magic quotes)
  59       * @return bool Authentication success or failure.
  60       */
  61      function user_login ($username, $password) {
  62          if (! function_exists('imap_open')) {
  63              print_error('auth_imapnotinstalled','mnet');
  64              return false;
  65          }
  66  
  67          global $CFG;
  68          $hosts = explode(';', $this->config->host);   // Could be multiple hosts
  69  
  70          foreach ($hosts as $host) {                 // Try each host in turn
  71              $host = trim($host);
  72  
  73              switch ($this->config->type) {
  74                  case 'imapssl':
  75                      $host = '{'.$host.":{$this->config->port}/imap/ssl}";
  76                  break;
  77  
  78                  case 'imapcert':
  79                      $host = '{'.$host.":{$this->config->port}/imap/ssl/novalidate-cert}";
  80                  break;
  81  
  82                  case 'imaptls':
  83                      $host = '{'.$host.":{$this->config->port}/imap/tls}";
  84                  break;
  85  
  86                  case 'imapnosslcert':
  87                      $host = '{'.$host.":{$this->config->port}/imap/novalidate-cert}";
  88                  break;
  89  
  90                  default:
  91                      $host = '{'.$host.":{$this->config->port}/imap}";
  92              }
  93  
  94              error_reporting(0);
  95              $connection = imap_open($host, $username, $password, OP_HALFOPEN);
  96              error_reporting($CFG->debug);
  97  
  98              if ($connection) {
  99                  imap_close($connection);
 100                  return true;
 101              }
 102          }
 103  
 104          return false;  // No match
 105      }
 106  
 107      function prevent_local_passwords() {
 108          return true;
 109      }
 110  
 111      /**
 112       * Returns true if this authentication plugin is 'internal'.
 113       *
 114       * @return bool
 115       */
 116      function is_internal() {
 117          return false;
 118      }
 119  
 120      /**
 121       * Returns true if this authentication plugin can change the user's
 122       * password.
 123       *
 124       * @return bool
 125       */
 126      function can_change_password() {
 127          return !empty($this->config->changepasswordurl);
 128      }
 129  
 130      /**
 131       * Returns the URL for changing the user's pw, or empty if the default can
 132       * be used.
 133       *
 134       * @return moodle_url
 135       */
 136      function change_password_url() {
 137          if (!empty($this->config->changepasswordurl)) {
 138              return new moodle_url($this->config->changepasswordurl);
 139          } else {
 140              return null;
 141          }
 142      }
 143  
 144      /**
 145       * Prints a form for configuring this authentication plugin.
 146       *
 147       * This function is called from admin/auth.php, and outputs a full page with
 148       * a form for configuring this plugin.
 149       *
 150       * @param array $page An object containing all the data for this page.
 151       */
 152      function config_form($config, $err, $user_fields) {
 153          global $OUTPUT;
 154  
 155          include  "config.html";
 156      }
 157  
 158      /**
 159       * Processes and stores configuration data for this authentication plugin.
 160       */
 161      function process_config($config) {
 162          // set to defaults if undefined
 163          if (!isset ($config->host)) {
 164              $config->host = '127.0.0.1';
 165          }
 166          if (!isset ($config->type)) {
 167              $config->type = 'imap';
 168          }
 169          if (!isset ($config->port)) {
 170              $config->port = '143';
 171          }
 172          if (!isset($config->changepasswordurl)) {
 173              $config->changepasswordurl = '';
 174          }
 175  
 176          // save settings
 177          set_config('host', $config->host, 'auth/imap');
 178          set_config('type', $config->type, 'auth/imap');
 179          set_config('port', $config->port, 'auth/imap');
 180          set_config('changepasswordurl', $config->changepasswordurl, 'auth/imap');
 181  
 182          return true;
 183      }
 184  
 185  }
 186  
 187  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1