]> BookStack Code Mirror - bookstack/blob - app/Services/Ldap.php
Added auto-suggestions to tag names and values
[bookstack] / app / Services / Ldap.php
1 <?php namespace BookStack\Services;
2
3
4 /**
5  * Class Ldap
6  * An object-orientated thin abstraction wrapper for common PHP LDAP functions.
7  * Allows the standard LDAP functions to be mocked for testing.
8  * @package BookStack\Services
9  */
10 class Ldap
11 {
12
13     /**
14      * Connect to a LDAP server.
15      * @param string $hostName
16      * @param int    $port
17      * @return resource
18      */
19     public function connect($hostName, $port)
20     {
21         return ldap_connect($hostName, $port);
22     }
23
24     /**
25      * Set the value of a LDAP option for the given connection.
26      * @param resource $ldapConnection
27      * @param int $option
28      * @param mixed $value
29      * @return bool
30      */
31     public function setOption($ldapConnection, $option, $value)
32     {
33         return ldap_set_option($ldapConnection, $option, $value);
34     }
35
36     /**
37      * Set the version number for the given ldap connection.
38      * @param $ldapConnection
39      * @param $version
40      * @return bool
41      */
42     public function setVersion($ldapConnection, $version)
43     {
44         return $this->setOption($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, $version);
45     }
46
47     /**
48      * Search LDAP tree using the provided filter.
49      * @param resource   $ldapConnection
50      * @param string     $baseDn
51      * @param string     $filter
52      * @param array|null $attributes
53      * @return resource
54      */
55     public function search($ldapConnection, $baseDn, $filter, array $attributes = null)
56     {
57         return ldap_search($ldapConnection, $baseDn, $filter, $attributes);
58     }
59
60     /**
61      * Get entries from an ldap search result.
62      * @param resource $ldapConnection
63      * @param resource $ldapSearchResult
64      * @return array
65      */
66     public function getEntries($ldapConnection, $ldapSearchResult)
67     {
68         return ldap_get_entries($ldapConnection, $ldapSearchResult);
69     }
70
71     /**
72      * Search and get entries immediately.
73      * @param resource   $ldapConnection
74      * @param string     $baseDn
75      * @param string     $filter
76      * @param array|null $attributes
77      * @return resource
78      */
79     public function searchAndGetEntries($ldapConnection, $baseDn, $filter, array $attributes = null)
80     {
81         $search = $this->search($ldapConnection, $baseDn, $filter, $attributes);
82         return $this->getEntries($ldapConnection, $search);
83     }
84
85     /**
86      * Bind to LDAP directory.
87      * @param resource $ldapConnection
88      * @param string   $bindRdn
89      * @param string   $bindPassword
90      * @return bool
91      */
92     public function bind($ldapConnection, $bindRdn = null, $bindPassword = null)
93     {
94         return ldap_bind($ldapConnection, $bindRdn, $bindPassword);
95     }
96
97 }