aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/sabre/dav/lib/DAV/Auth/Backend/IMAP.php
blob: 3a1831116af684a120f885a97d27e92b08014f56 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php

namespace Sabre\DAV\Auth\Backend;

/**
 * This is an authentication backend that uses imap.
 *
 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
 * @author Michael Niewöhner (foss@mniewoehner.de)
 * @author rosali (https://github.com/rosali)
 * @author Evert Pot (http://evertpot.com/)
 * @license http://sabre.io/license/ Modified BSD License
 */
class IMAP extends AbstractBasic
{
    /**
     * IMAP server in the form {host[:port][/flag1/flag2...]}.
     *
     * @see http://php.net/manual/en/function.imap-open.php
     *
     * @var string
     */
    protected $mailbox;

    /**
     * Creates the backend object.
     *
     * @param string $mailbox
     */
    public function __construct($mailbox)
    {
        $this->mailbox = $mailbox;
    }

    /**
     * Connects to an IMAP server and tries to authenticate.
     *
     * @param string $username
     * @param string $password
     *
     * @return bool
     */
    protected function imapOpen($username, $password)
    {
        $success = false;

        try {
            $imap = imap_open($this->mailbox, $username, $password, OP_HALFOPEN | OP_READONLY, 1);
            if ($imap) {
                $success = true;
            }
        } catch (\ErrorException $e) {
            error_log($e->getMessage());
        }

        $errors = imap_errors();
        if ($errors) {
            foreach ($errors as $error) {
                error_log($error);
            }
        }

        if (isset($imap) && $imap) {
            imap_close($imap);
        }

        return $success;
    }

    /**
     * Validates a username and password by trying to authenticate against IMAP.
     *
     * @param string $username
     * @param string $password
     *
     * @return bool
     */
    protected function validateUserPass($username, $password)
    {
        return $this->imapOpen($username, $password);
    }
}