IMAP.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Sabre\DAV\Auth\Backend;
  3. /**
  4. * This is an authentication backend that uses imap.
  5. *
  6. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  7. * @author Michael Niewöhner (foss@mniewoehner.de)
  8. * @author rosali (https://github.com/rosali)
  9. * @author Evert Pot (http://evertpot.com/)
  10. * @license http://sabre.io/license/ Modified BSD License
  11. */
  12. class IMAP extends AbstractBasic
  13. {
  14. /**
  15. * IMAP server in the form {host[:port][/flag1/flag2...]}.
  16. *
  17. * @see http://php.net/manual/en/function.imap-open.php
  18. *
  19. * @var string
  20. */
  21. protected $mailbox;
  22. /**
  23. * Creates the backend object.
  24. *
  25. * @param string $mailbox
  26. */
  27. public function __construct($mailbox)
  28. {
  29. $this->mailbox = $mailbox;
  30. }
  31. /**
  32. * Connects to an IMAP server and tries to authenticate.
  33. *
  34. * @param string $username
  35. * @param string $password
  36. *
  37. * @return bool
  38. */
  39. protected function imapOpen($username, $password)
  40. {
  41. $success = false;
  42. try {
  43. $imap = imap_open($this->mailbox, $username, $password, OP_HALFOPEN | OP_READONLY, 1);
  44. if ($imap) {
  45. $success = true;
  46. }
  47. } catch (\ErrorException $e) {
  48. error_log($e->getMessage());
  49. }
  50. $errors = imap_errors();
  51. if ($errors) {
  52. foreach ($errors as $error) {
  53. error_log($error);
  54. }
  55. }
  56. if (isset($imap) && $imap) {
  57. imap_close($imap);
  58. }
  59. return $success;
  60. }
  61. /**
  62. * Validates a username and password by trying to authenticate against IMAP.
  63. *
  64. * @param string $username
  65. * @param string $password
  66. *
  67. * @return bool
  68. */
  69. protected function validateUserPass($username, $password)
  70. {
  71. return $this->imapOpen($username, $password);
  72. }
  73. }