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(!function_exists("ldap_connect")) {
41 $this->set_error("LDAP support missing from PHP");
44 if(is_array($param)) {
45 $this->server
= $param["host"];
46 $this->basedn
= $param["base"];
47 if(!empty($param["port"]))
48 $this->port
= $param["port"];
49 if(!empty($param["charset"]))
50 $this->charset
= strtolower($param["charset"]);
51 if(isset($param["maxrows"]))
52 $this->maxrows
= $param["maxrows"];
53 if(empty($param["name"]))
54 $this->sname
= "LDAP: ".$param["host"];
56 $this->sname
= $param["name"];
60 $this->set_error("Invalid argument to constructor");
65 // Open the LDAP server. New connection if $new is true
66 function open($new = false) {
69 // Connection is already open
70 if($this->linkid
!= false && !$new)
73 $this->linkid
= @ldap_connect
($this->server
, $this->port
);
75 if(function_exists("ldap_error"))
76 return $this->set_error(ldap_error($this->linkid
));
78 return $this->set_error("ldap_connect failed");
80 if(!@ldap_bind
($this->linkid
))
81 if(function_exists("ldap_error"))
82 return $this->set_error(ldap_error($this->linkid
));
84 return $this->set_error("ldap_bind failed");
92 // Encode iso8859-1 string to the charset used by this LDAP server
93 function charset_encode($str) {
94 if($this->charset
== "utf-8") {
95 if(function_exists("utf8_encode"))
96 return utf8_encode($str);
105 // Decode from charset used by this LDAP server to iso8859-1
106 function charset_decode($str) {
107 if($this->charset
== "utf-8") {
108 if(function_exists("utf8_decode"))
109 return utf8_decode($str);
118 // ========================== Public ========================
120 // Search the LDAP server
121 function search($expr) {
123 // To be replaced by advanded search expression parsing
124 if(is_array($expr)) return false;
126 // Encode the expression
127 $expr = $this->charset_encode($expr);
128 if(!ereg("\*", $expr))
130 $expression = "cn=$expr";
132 // Make sure connection is there
137 $sret = @ldap_search
($this->linkid
, $this->basedn
, $expression,
138 array("dn", "o", "ou", "sn", "givenname",
139 "cn", "mail", "telephonenumber"));
141 // Should get error from server using the ldap_error() function,
142 // but it only exist in the PHP LDAP documentation.
144 if(function_exists("ldap_error"))
145 return $this->set_error(ldap_error($this->linkid
));
147 return $this->set_error("ldap_search failed");
149 if(@ldap_count_entries
($this->linkid
, $sret) <= 0)
155 $res = @ldap_get_entries
($this->linkid
, $sret);
156 for($i = 0 ; $i < $res["count"] ; $i++
) {
159 // Extract data common for all e-mail addresses
160 // of an object. Use only the first name
161 $nickname = $this->charset_decode($row["dn"]);
162 $fullname = $this->charset_decode($row["cn"][0]);
164 if(empty($row["telephonenumber"][0])) $phone = "";
165 else $phone = $this->charset_decode($row["telephonenumber"][0]);
167 if(!empty($row["ou"][0]))
168 $label = $this->charset_decode($row["ou"][0]);
169 else if(!empty($row["o"][0]))
170 $label = $this->charset_decode($row["o"][0]);
174 if(empty($row["givenname"][0])) $firstname = "";
175 else $firstname = $this->charset_decode($row["givenname"][0]);
177 if(empty($row["sn"][0])) $surname = "";
178 else $surname = $this->charset_decode($row["sn"][0]);
180 // Add one row to result for each e-mail address
181 for($j = 0 ; $j < $row["mail"]["count"] ; $j++
) {
182 array_push($ret, array("nickname" => $nickname,
184 "firstname" => $firstname,
185 "lastname" => $surname,
186 "email" => $row["mail"][$j],
189 "backend" => $this->bnum
,
190 "source" => &$this->sname
));
192 // Limit number of hits
194 if(($returned_rows >= $this->maxrows
) &&
195 ($this->maxrows
> 0) ) {
196 ldap_free_result($sret);
203 ldap_free_result($sret);