summaryrefslogtreecommitdiff
path: root/app/Core/Ldap/Client.php
blob: a523428c4001994aefaca803c89341847b55c5a2 (plain)
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
83
84
<?php

namespace Kanboard\Core\Ldap;

/**
 * LDAP Client
 *
 * @package ldap
 * @author  Frederic Guillot
 */
class Client
{
    /**
     * Get server connection
     *
     * @access public
     * @param  string   $server  LDAP server hostname or IP
     * @param  integer  $port    LDAP port
     * @param  boolean  $tls     Start TLS
     * @param  boolean  $verify  Skip SSL certificate verification
     * @return resource
     */
    public function getConnection($server, $port = LDAP_PORT, $tls = LDAP_START_TLS, $verify = LDAP_SSL_VERIFY)
    {
        if (! function_exists('ldap_connect')) {
            throw new ClientException('LDAP: The PHP LDAP extension is required');
        }

        if (! $verify) {
            putenv('LDAPTLS_REQCERT=never');
        }

        $ldap = ldap_connect($server, $port);

        if ($ldap === false) {
            throw new ClientException('LDAP: Unable to connect to the LDAP server');
        }

        ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
        ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
        ldap_set_option($ldap, LDAP_OPT_NETWORK_TIMEOUT, 1);
        ldap_set_option($ldap, LDAP_OPT_TIMELIMIT, 1);

        if ($tls && ! @ldap_start_tls($ldap)) {
            throw new ClientException('LDAP: Unable to start TLS');
        }

        return $ldap;
    }

    /**
     * Anonymous authentication
     *
     * @access public
     * @param  resource $ldap
     * @return boolean
     */
    public function useAnonymousAuthentication($ldap)
    {
        if (! ldap_bind($ldap)) {
            throw new ClientException('Unable to perform anonymous binding');
        }

        return true;
    }

    /**
     * Authentication with username/password
     *
     * @access public
     * @param  resource $ldap
     * @param  string   $username
     * @param  string   $password
     * @return boolean
     */
    public function authenticate($ldap, $username, $password)
    {
        if (! ldap_bind($ldap, $username, $password)) {
            throw new ClientException('Unable to perform anonymous binding');
        }

        return true;
    }
}