[ Index ]

PHP Cross Reference of Unnamed Project

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


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