]> BookStack Code Mirror - bookstack/blob - app/Services/Ldap.php
Update Ldap.php
[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         /*
22         * LDAPS is not working because even if port 363 is specified, 
23         * BookStack tries to open a LDAP connection on the LDAPS channel.
24         * The if-clause below fixed this, although it would be better to
25         * change the settings in .env from
26         *   LDAP_SERVER=servername:port
27         * to
28         *   LDAP_SERVER=ldap://servername:389
29         *   LDAP_SERVER=ldaps://servername:363
30         * in order to be compatible with non-standard setups. Currently, 
31         * specifying ldap:// or ldaps:// results in an error because BookStack
32         * splits at ":" and takes the seconds chunk (in this case "//servername"
33         * as the port value.
34         */
35         if ($port == 363)
36         {
37                 $hostName = "ldaps://".$hostName;
38         }
39         return ldap_connect($hostName, $port);
40     }
41
42     /**
43      * Set the value of a LDAP option for the given connection.
44      * @param resource $ldapConnection
45      * @param int $option
46      * @param mixed $value
47      * @return bool
48      */
49     public function setOption($ldapConnection, $option, $value)
50     {
51         return ldap_set_option($ldapConnection, $option, $value);
52     }
53
54     /**
55      * Set the version number for the given ldap connection.
56      * @param $ldapConnection
57      * @param $version
58      * @return bool
59      */
60     public function setVersion($ldapConnection, $version)
61     {
62         return $this->setOption($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, $version);
63     }
64
65     /**
66      * Search LDAP tree using the provided filter.
67      * @param resource   $ldapConnection
68      * @param string     $baseDn
69      * @param string     $filter
70      * @param array|null $attributes
71      * @return resource
72      */
73     public function search($ldapConnection, $baseDn, $filter, array $attributes = null)
74     {
75         return ldap_search($ldapConnection, $baseDn, $filter, $attributes);
76     }
77
78     /**
79      * Get entries from an ldap search result.
80      * @param resource $ldapConnection
81      * @param resource $ldapSearchResult
82      * @return array
83      */
84     public function getEntries($ldapConnection, $ldapSearchResult)
85     {
86         return ldap_get_entries($ldapConnection, $ldapSearchResult);
87     }
88
89     /**
90      * Search and get entries immediately.
91      * @param resource   $ldapConnection
92      * @param string     $baseDn
93      * @param string     $filter
94      * @param array|null $attributes
95      * @return resource
96      */
97     public function searchAndGetEntries($ldapConnection, $baseDn, $filter, array $attributes = null)
98     {
99         $search = $this->search($ldapConnection, $baseDn, $filter, $attributes);
100         return $this->getEntries($ldapConnection, $search);
101     }
102
103     /**
104      * Bind to LDAP directory.
105      * @param resource $ldapConnection
106      * @param string   $bindRdn
107      * @param string   $bindPassword
108      * @return bool
109      */
110     public function bind($ldapConnection, $bindRdn = null, $bindPassword = null)
111     {
112         return ldap_bind($ldapConnection, $bindRdn, $bindPassword);
113     }
114
115 }