[ Index ]

PHP Cross Reference of moodle-2.8

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


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