]> BookStack Code Mirror - bookstack/blob - app/Auth/Access/Ldap/LdapConnection.php
f9eb9f17ea49d361994a34ca6d411fda862cb64a
[bookstack] / app / Auth / Access / Ldap / LdapConnection.php
1 <?php
2
3 namespace BookStack\Auth\Access\Ldap;
4
5 use ErrorException;
6
7 /**
8  * An object-orientated wrapper for core ldap functions,
9  * holding an internal connection instance.
10  */
11 class LdapConnection
12 {
13     /**
14      * The core ldap connection resource.
15      * @var resource
16      */
17     protected $connection;
18
19     public function __construct(string $hostName, int $port)
20     {
21         $this->connection = $this->connect($hostName, $port);
22     }
23
24     /**
25      * Connect to an LDAP server.
26      *
27      * @return resource
28      */
29     protected function connect(string $hostName, int $port)
30     {
31         return ldap_connect($hostName, $port);
32     }
33
34     /**
35      * Set the value of a LDAP option for the current connection.
36      *
37      * @param mixed    $value
38      */
39     public function setOption(int $option, $value): bool
40     {
41         return ldap_set_option($this->connection, $option, $value);
42     }
43
44     /**
45      * Start TLS for this LDAP connection.
46      */
47     public function startTls(): bool
48     {
49         return ldap_start_tls($this->connection);
50     }
51
52     /**
53      * Set the version number for this ldap connection.
54      */
55     public function setVersion(int $version): bool
56     {
57         return $this->setOption(LDAP_OPT_PROTOCOL_VERSION, $version);
58     }
59
60     /**
61      * Search LDAP tree using the provided filter.
62      *
63      * @return resource
64      */
65     public function search(string $baseDn, string $filter, array $attributes = null)
66     {
67         return ldap_search($this->connection, $baseDn, $filter, $attributes);
68     }
69
70     /**
71      * Get entries from an ldap search result.
72      *
73      * @param resource $ldapSearchResult
74      * @return array|false
75      */
76     public function getEntries($ldapSearchResult)
77     {
78         return ldap_get_entries($this->connection, $ldapSearchResult);
79     }
80
81     /**
82      * Search and get entries immediately.
83      *
84      * @return array|false
85      */
86     public function searchAndGetEntries(string $baseDn, string $filter, array $attributes = null)
87     {
88         $search = $this->search($baseDn, $filter, $attributes);
89
90         return $this->getEntries($search);
91     }
92
93     /**
94      * Bind to LDAP directory.
95      *
96      * @throws ErrorException
97      */
98     public function bind(string $bindRdn = null, string $bindPassword = null): bool
99     {
100         return ldap_bind($this->connection, $bindRdn, $bindPassword);
101     }
102
103     /**
104      * Explode a LDAP dn string into an array of components.
105      *
106      * @return array|false
107      */
108     public static function explodeDn(string $dn, int $withAttrib)
109     {
110         return ldap_explode_dn($dn, $withAttrib);
111     }
112
113     /**
114      * Escape a string for use in an LDAP filter.
115      */
116     public static function escape(string $value, string $ignore = '', int $flags = 0): string
117     {
118         return ldap_escape($value, $ignore, $flags);
119     }
120
121     /**
122      * Set a non-connection-specific LDAP option.
123      * @param mixed $value
124      */
125     public static function setGlobalOption(int $option, $value): bool
126     {
127         return ldap_set_option(null, $option, $value);
128     }
129 }