/auth/imap/auth.php

https://bitbucket.org/kudutest1/moodlegit · PHP · 160 lines · 78 code · 27 blank · 55 comment · 8 complexity · dc3ba58314d722970139dd6bce6f9a66 MD5 · raw file

  1. <?php
  2. /**
  3. * @author Martin Dougiamas
  4. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  5. * @package moodle multiauth
  6. *
  7. * Authentication Plugin: IMAP Authentication
  8. *
  9. * Authenticates against an IMAP server.
  10. *
  11. * 2006-08-31 File created.
  12. */
  13. if (!defined('MOODLE_INTERNAL')) {
  14. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  15. }
  16. require_once($CFG->libdir.'/authlib.php');
  17. /**
  18. * IMAP authentication plugin.
  19. */
  20. class auth_plugin_imap extends auth_plugin_base {
  21. /**
  22. * Constructor.
  23. */
  24. function auth_plugin_imap() {
  25. $this->authtype = 'imap';
  26. $this->config = get_config('auth/imap');
  27. }
  28. /**
  29. * Returns true if the username and password work and false if they are
  30. * wrong or don't exist.
  31. *
  32. * @param string $username The username (with system magic quotes)
  33. * @param string $password The password (with system magic quotes)
  34. * @return bool Authentication success or failure.
  35. */
  36. function user_login ($username, $password) {
  37. if (! function_exists('imap_open')) {
  38. print_error('auth_imapnotinstalled','mnet');
  39. return false;
  40. }
  41. global $CFG;
  42. $hosts = explode(';', $this->config->host); // Could be multiple hosts
  43. foreach ($hosts as $host) { // Try each host in turn
  44. $host = trim($host);
  45. switch ($this->config->type) {
  46. case 'imapssl':
  47. $host = '{'.$host.":{$this->config->port}/imap/ssl}";
  48. break;
  49. case 'imapcert':
  50. $host = '{'.$host.":{$this->config->port}/imap/ssl/novalidate-cert}";
  51. break;
  52. case 'imaptls':
  53. $host = '{'.$host.":{$this->config->port}/imap/tls}";
  54. break;
  55. default:
  56. $host = '{'.$host.":{$this->config->port}/imap}";
  57. }
  58. error_reporting(0);
  59. $connection = imap_open($host, $username, $password, OP_HALFOPEN);
  60. error_reporting($CFG->debug);
  61. if ($connection) {
  62. imap_close($connection);
  63. return true;
  64. }
  65. }
  66. return false; // No match
  67. }
  68. function prevent_local_passwords() {
  69. return true;
  70. }
  71. /**
  72. * Returns true if this authentication plugin is 'internal'.
  73. *
  74. * @return bool
  75. */
  76. function is_internal() {
  77. return false;
  78. }
  79. /**
  80. * Returns true if this authentication plugin can change the user's
  81. * password.
  82. *
  83. * @return bool
  84. */
  85. function can_change_password() {
  86. return !empty($this->config->changepasswordurl);
  87. }
  88. /**
  89. * Returns the URL for changing the user's pw, or empty if the default can
  90. * be used.
  91. *
  92. * @return moodle_url
  93. */
  94. function change_password_url() {
  95. return new moodle_url($this->config->changepasswordurl);
  96. }
  97. /**
  98. * Prints a form for configuring this authentication plugin.
  99. *
  100. * This function is called from admin/auth.php, and outputs a full page with
  101. * a form for configuring this plugin.
  102. *
  103. * @param array $page An object containing all the data for this page.
  104. */
  105. function config_form($config, $err, $user_fields) {
  106. global $OUTPUT;
  107. include "config.html";
  108. }
  109. /**
  110. * Processes and stores configuration data for this authentication plugin.
  111. */
  112. function process_config($config) {
  113. // set to defaults if undefined
  114. if (!isset ($config->host)) {
  115. $config->host = '127.0.0.1';
  116. }
  117. if (!isset ($config->type)) {
  118. $config->type = 'imap';
  119. }
  120. if (!isset ($config->port)) {
  121. $config->port = '143';
  122. }
  123. if (!isset($config->changepasswordurl)) {
  124. $config->changepasswordurl = '';
  125. }
  126. // save settings
  127. set_config('host', $config->host, 'auth/imap');
  128. set_config('type', $config->type, 'auth/imap');
  129. set_config('port', $config->port, 'auth/imap');
  130. set_config('changepasswordurl', $config->changepasswordurl, 'auth/imap');
  131. return true;
  132. }
  133. }