1 <?php namespace BookStack\Services;
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
14 * Connect to a LDAP server.
15 * @param string $hostName
19 public function connect($hostName, $port)
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
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"
37 $hostName = "ldaps://".$hostName;
39 return ldap_connect($hostName, $port);
43 * Set the value of a LDAP option for the given connection.
44 * @param resource $ldapConnection
49 public function setOption($ldapConnection, $option, $value)
51 return ldap_set_option($ldapConnection, $option, $value);
55 * Set the version number for the given ldap connection.
56 * @param $ldapConnection
60 public function setVersion($ldapConnection, $version)
62 return $this->setOption($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, $version);
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
73 public function search($ldapConnection, $baseDn, $filter, array $attributes = null)
75 return ldap_search($ldapConnection, $baseDn, $filter, $attributes);
79 * Get entries from an ldap search result.
80 * @param resource $ldapConnection
81 * @param resource $ldapSearchResult
84 public function getEntries($ldapConnection, $ldapSearchResult)
86 return ldap_get_entries($ldapConnection, $ldapSearchResult);
90 * Search and get entries immediately.
91 * @param resource $ldapConnection
92 * @param string $baseDn
93 * @param string $filter
94 * @param array|null $attributes
97 public function searchAndGetEntries($ldapConnection, $baseDn, $filter, array $attributes = null)
99 $search = $this->search($ldapConnection, $baseDn, $filter, $attributes);
100 return $this->getEntries($ldapConnection, $search);
104 * Bind to LDAP directory.
105 * @param resource $ldapConnection
106 * @param string $bindRdn
107 * @param string $bindPassword
110 public function bind($ldapConnection, $bindRdn = null, $bindPassword = null)
112 return ldap_bind($ldapConnection, $bindRdn, $bindPassword);