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
19 ** NOTE. This class should not be used directly. Use the
20 ** "AddressBook" class instead.
23 class abook_ldap_server
extends addressbook_backend
{
24 var $btype = "remote";
25 var $bname = "ldap_server";
27 // Parameters changed by class
28 var $sname = "LDAP"; // Service name
29 var $server = ""; // LDAP server name
30 var $port = 389; // LDAP server port
31 var $basedn = ""; // LDAP base DN
32 var $charset = "utf-8"; // LDAP server charset
33 var $linkid = false; // PHP LDAP link ID
34 var $bound = false; // True if LDAP server is bound
35 var $maxrows = 250; // Max rows in result
38 // Constructor. Connects to database
39 function abook_ldap_server($param) {
40 if(is_array($param)) {
41 $this->server
= $param["host"];
42 $this->basedn
= $param["base"];
43 if(!empty($param["port"]))
44 $this->port
= $param["port"];
45 if(!empty($param["charset"]))
46 $this->charset
= strtolower($param["charset"]);
47 if(isset($param["maxrows"]))
48 $this->maxrows
= $param["maxrows"];
49 if(empty($param["name"]))
50 $this->sname
= "LDAP: ".$param["host"];
52 $this->sname
= $param["name"];
56 $this->set_error("Invalid argument to constructor");
61 // Open the LDAP server. New connection if $new is true
62 function open($new = false) {
65 // Connection is already open
66 if($this->linkid
!= false && !$new)
69 $this->linkid
= @ldap_connect
($this->server
, $this->port
);
71 if(function_exists("ldap_error"))
72 return $this->set_error(ldap_error($this->linkid
));
74 return $this->set_error("ldap_connect failed");
76 if(!@ldap_bind
($this->linkid
))
77 if(function_exists("ldap_error"))
78 return $this->set_error(ldap_error($this->linkid
));
80 return $this->set_error("ldap_bind failed");
88 // Encode iso8859-1 string to the charset used by this LDAP server
89 function charset_encode($str) {
90 if($this->charset
== "utf-8") {
91 if(function_exists("utf8_encode"))
92 return utf8_encode($str);
101 // Decode from charset used by this LDAP server to iso8859-1
102 function charset_decode($str) {
103 if($this->charset
== "utf-8") {
104 if(function_exists("utf8_decode"))
105 return utf8_decode($str);
114 // ========================== Public ========================
116 // Search the LDAP server
117 function search($expr) {
119 // To be replaced by advanded search expression parsing
120 if(is_array($expr)) return false;
122 // Encode the expression
123 $expr = $this->charset_encode($expr);
124 if(!ereg("\*", $expr))
126 $expression = "cn=$expr";
128 // Make sure connection is there
133 $sret = @ldap_search
($this->linkid
, $this->basedn
, $expression,
134 array("dn", "o", "ou", "sn", "givenname",
135 "cn", "mail", "telephonenumber"));
137 // Should get error from server using the ldap_error() function,
138 // but it only exist in the PHP LDAP documentation.
140 if(function_exists("ldap_error"))
141 return $this->set_error(ldap_error($this->linkid
));
143 return $this->set_error("ldap_search failed");
145 if(@ldap_count_entries
($this->linkid
, $sret) <= 0)
151 $res = @ldap_get_entries
($this->linkid
, $sret);
152 for($i = 0 ; $i < $res["count"] ; $i++
) {
155 // Extract data common for all e-mail addresses
156 // of an object. Use only the first name
157 $nickname = $this->charset_decode($row["dn"]);
158 $fullname = $this->charset_decode($row["cn"][0]);
160 if(empty($row["telephonenumber"][0])) $phone = "";
161 else $phone = $this->charset_decode($row["telephonenumber"][0]);
163 if(!empty($row["ou"][0]))
164 $label = $this->charset_decode($row["ou"][0]);
165 else if(!empty($row["o"][0]))
166 $label = $this->charset_decode($row["o"][0]);
170 if(empty($row["givenname"][0])) $firstname = "";
171 else $firstname = $this->charset_decode($row["givenname"][0]);
173 if(empty($row["sn"][0])) $surname = "";
174 else $surname = $this->charset_decode($row["sn"][0]);
176 // Add one row to result for each e-mail address
177 for($j = 0 ; $j < $row["mail"]["count"] ; $j++
) {
178 array_push($ret, array("nickname" => $nickname,
180 "firstname" => $firstname,
181 "lastname" => $surname,
182 "email" => $row["mail"][$j],
185 "backend" => $this->bnum
,
186 "source" => &$this->sname
));
188 // Limit number of hits
190 if(($returned_rows >= $this->maxrows
) &&
191 ($this->maxrows
> 0) ) {
192 ldap_free_result($sret);
199 ldap_free_result($sret);