4863b7bb65cb771fbd1c7308a93f9c9b95042cd4
4 ** abook_ldap_server.php
6 ** Address book backend for LDAP server
8 ** An array with the following elements must be passed to
9 ** the class constructor (elements marked ? are optional):
11 ** host => LDAP server hostname/IP-address
12 ** base => LDAP server root (base dn). Empty string allowed.
13 ** ? port => LDAP server TCP port number (default: 389)
14 ** ? charset => LDAP server charset (default: utf-8)
15 ** ? name => Name for LDAP server (default "LDAP: hostname")
16 ** Used to tag the result data
17 ** ? maxrows => Maximum # of rows in search result
18 ** ? timeout => Timeout for LDAP operations (in seconds, default: 30)
19 ** Might not work for all LDAP libraries or servers.
21 ** NOTE. This class should not be used directly. Use the
22 ** "AddressBook" class instead.
27 class abook_ldap_server
extends addressbook_backend
{
28 var $btype = "remote";
29 var $bname = "ldap_server";
31 // Parameters changed by class
32 var $sname = "LDAP"; // Service name
33 var $server = ""; // LDAP server name
34 var $port = 389; // LDAP server port
35 var $basedn = ""; // LDAP base DN
36 var $charset = "utf-8"; // LDAP server charset
37 var $linkid = false; // PHP LDAP link ID
38 var $bound = false; // True if LDAP server is bound
39 var $maxrows = 250; // Max rows in result
40 var $timeout = 30; // Timeout for LDAP operations (in seconds)
42 // Constructor. Connects to database
43 function abook_ldap_server($param) {
44 if(!function_exists("ldap_connect")) {
45 $this->set_error("LDAP support missing from PHP");
48 if(is_array($param)) {
49 $this->server
= $param["host"];
50 $this->basedn
= $param["base"];
51 if(!empty($param["port"]))
52 $this->port
= $param["port"];
53 if(!empty($param["charset"]))
54 $this->charset
= strtolower($param["charset"]);
55 if(isset($param["maxrows"]))
56 $this->maxrows
= $param["maxrows"];
57 if(isset($param["timeout"]))
58 $this->timeout
= $param["timeout"];
59 if(empty($param["name"]))
60 $this->sname
= "LDAP: ".$param["host"];
62 $this->sname
= $param["name"];
66 $this->set_error("Invalid argument to constructor");
71 // Open the LDAP server. New connection if $new is true
72 function open($new = false) {
75 // Connection is already open
76 if($this->linkid
!= false && !$new)
79 $this->linkid
= @ldap_connect
($this->server
, $this->port
);
81 if(function_exists("ldap_error"))
82 return $this->set_error(ldap_error($this->linkid
));
84 return $this->set_error("ldap_connect failed");
86 if(!@ldap_bind
($this->linkid
))
87 if(function_exists("ldap_error"))
88 return $this->set_error(ldap_error($this->linkid
));
90 return $this->set_error("ldap_bind failed");
98 // Encode iso8859-1 string to the charset used by this LDAP server
99 function charset_encode($str) {
100 if($this->charset
== "utf-8") {
101 if(function_exists("utf8_encode"))
102 return utf8_encode($str);
111 // Decode from charset used by this LDAP server to iso8859-1
112 function charset_decode($str) {
113 if($this->charset
== "utf-8") {
114 if(function_exists("utf8_decode"))
115 return utf8_decode($str);
124 // ========================== Public ========================
126 // Search the LDAP server
127 function search($expr) {
129 // To be replaced by advanded search expression parsing
130 if(is_array($expr)) return false;
132 // Encode the expression
133 $expr = $this->charset_encode($expr);
134 if(!ereg("\*", $expr))
136 $expression = "cn=$expr";
138 // Make sure connection is there
142 // Do the search. Use improved ldap_search() if PHP version is
144 if(sqCheckPHPVersion(4, 0, 2)) {
145 $sret = @ldap_search
($this->linkid
, $this->basedn
, $expression,
146 array("dn", "o", "ou", "sn", "givenname",
147 "cn", "mail", "telephonenumber"),
148 0, $this->maxrows
, $this->timeout
);
150 $sret = @ldap_search
($this->linkid
, $this->basedn
, $expression,
151 array("dn", "o", "ou", "sn", "givenname",
152 "cn", "mail", "telephonenumber"));
155 // Should get error from server using the ldap_error() function,
156 // but it only exist in the PHP LDAP documentation.
158 if(function_exists("ldap_error"))
159 return $this->set_error(ldap_error($this->linkid
));
161 return $this->set_error("ldap_search failed");
163 if(@ldap_count_entries
($this->linkid
, $sret) <= 0)
169 $res = @ldap_get_entries
($this->linkid
, $sret);
170 for($i = 0 ; $i < $res["count"] ; $i++
) {
173 // Extract data common for all e-mail addresses
174 // of an object. Use only the first name
175 $nickname = $this->charset_decode($row["dn"]);
176 $fullname = $this->charset_decode($row["cn"][0]);
178 if(empty($row["telephonenumber"][0])) $phone = "";
179 else $phone = $this->charset_decode($row["telephonenumber"][0]);
181 if(!empty($row["ou"][0]))
182 $label = $this->charset_decode($row["ou"][0]);
183 else if(!empty($row["o"][0]))
184 $label = $this->charset_decode($row["o"][0]);
188 if(empty($row["givenname"][0])) $firstname = "";
189 else $firstname = $this->charset_decode($row["givenname"][0]);
191 if(empty($row["sn"][0])) $surname = "";
192 else $surname = $this->charset_decode($row["sn"][0]);
194 // Add one row to result for each e-mail address
195 for($j = 0 ; $j < $row["mail"]["count"] ; $j++
) {
196 array_push($ret, array("nickname" => $nickname,
198 "firstname" => $firstname,
199 "lastname" => $surname,
200 "email" => $row["mail"][$j],
203 "backend" => $this->bnum
,
204 "source" => &$this->sname
));
206 // Limit number of hits
208 if(($returned_rows >= $this->maxrows
) &&
209 ($this->maxrows
> 0) ) {
210 ldap_free_result($sret);
217 ldap_free_result($sret);