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.
25 class abook_ldap_server
extends addressbook_backend
{
26 var $btype = "remote";
27 var $bname = "ldap_server";
29 // Parameters changed by class
30 var $sname = "LDAP"; // Service name
31 var $server = ""; // LDAP server name
32 var $port = 389; // LDAP server port
33 var $basedn = ""; // LDAP base DN
34 var $charset = "utf-8"; // LDAP server charset
35 var $linkid = false; // PHP LDAP link ID
36 var $bound = false; // True if LDAP server is bound
37 var $maxrows = 250; // Max rows in result
38 var $timeout = 30; // Timeout for LDAP operations (in seconds)
40 // Constructor. Connects to database
41 function abook_ldap_server($param) {
42 if(!function_exists("ldap_connect")) {
43 $this->set_error("LDAP support missing from PHP");
46 if(is_array($param)) {
47 $this->server
= $param["host"];
48 $this->basedn
= $param["base"];
49 if(!empty($param["port"]))
50 $this->port
= $param["port"];
51 if(!empty($param["charset"]))
52 $this->charset
= strtolower($param["charset"]);
53 if(isset($param["maxrows"]))
54 $this->maxrows
= $param["maxrows"];
55 if(isset($param["timeout"]))
56 $this->timeout
= $param["timeout"];
57 if(empty($param["name"]))
58 $this->sname
= "LDAP: ".$param["host"];
60 $this->sname
= $param["name"];
64 $this->set_error("Invalid argument to constructor");
69 // Open the LDAP server. New connection if $new is true
70 function open($new = false) {
73 // Connection is already open
74 if($this->linkid
!= false && !$new)
77 $this->linkid
= @ldap_connect
($this->server
, $this->port
);
79 if(function_exists("ldap_error"))
80 return $this->set_error(ldap_error($this->linkid
));
82 return $this->set_error("ldap_connect failed");
84 if(!@ldap_bind
($this->linkid
))
85 if(function_exists("ldap_error"))
86 return $this->set_error(ldap_error($this->linkid
));
88 return $this->set_error("ldap_bind failed");
96 // Encode iso8859-1 string to the charset used by this LDAP server
97 function charset_encode($str) {
98 if($this->charset
== "utf-8") {
99 if(function_exists("utf8_encode"))
100 return utf8_encode($str);
109 // Decode from charset used by this LDAP server to iso8859-1
110 function charset_decode($str) {
111 if($this->charset
== "utf-8") {
112 if(function_exists("utf8_decode"))
113 return utf8_decode($str);
122 // ========================== Public ========================
124 // Search the LDAP server
125 function search($expr) {
127 // To be replaced by advanded search expression parsing
128 if(is_array($expr)) return false;
130 // Encode the expression
131 $expr = $this->charset_encode($expr);
132 if(!ereg("\*", $expr))
134 $expression = "cn=$expr";
136 // Make sure connection is there
140 // Do the search. Use improved ldap_search() if PHP version is
142 if(sqCheckPHPVersion(4, 0, 2)) {
143 $sret = @ldap_search
($this->linkid
, $this->basedn
, $expression,
144 array("dn", "o", "ou", "sn", "givenname",
145 "cn", "mail", "telephonenumber"),
146 0, $this->maxrows
, $this->timeout
);
148 $sret = @ldap_search
($this->linkid
, $this->basedn
, $expression,
149 array("dn", "o", "ou", "sn", "givenname",
150 "cn", "mail", "telephonenumber"));
153 // Should get error from server using the ldap_error() function,
154 // but it only exist in the PHP LDAP documentation.
156 if(function_exists("ldap_error"))
157 return $this->set_error(ldap_error($this->linkid
));
159 return $this->set_error("ldap_search failed");
161 if(@ldap_count_entries
($this->linkid
, $sret) <= 0)
167 $res = @ldap_get_entries
($this->linkid
, $sret);
168 for($i = 0 ; $i < $res["count"] ; $i++
) {
171 // Extract data common for all e-mail addresses
172 // of an object. Use only the first name
173 $nickname = $this->charset_decode($row["dn"]);
174 $fullname = $this->charset_decode($row["cn"][0]);
176 if(empty($row["telephonenumber"][0])) $phone = "";
177 else $phone = $this->charset_decode($row["telephonenumber"][0]);
179 if(!empty($row["ou"][0]))
180 $label = $this->charset_decode($row["ou"][0]);
181 else if(!empty($row["o"][0]))
182 $label = $this->charset_decode($row["o"][0]);
186 if(empty($row["givenname"][0])) $firstname = "";
187 else $firstname = $this->charset_decode($row["givenname"][0]);
189 if(empty($row["sn"][0])) $surname = "";
190 else $surname = $this->charset_decode($row["sn"][0]);
192 // Add one row to result for each e-mail address
193 for($j = 0 ; $j < $row["mail"]["count"] ; $j++
) {
194 array_push($ret, array("nickname" => $nickname,
196 "firstname" => $firstname,
197 "lastname" => $surname,
198 "email" => $row["mail"][$j],
201 "backend" => $this->bnum
,
202 "source" => &$this->sname
));
204 // Limit number of hits
206 if(($returned_rows >= $this->maxrows
) &&
207 ($this->maxrows
> 0) ) {
208 ldap_free_result($sret);
215 ldap_free_result($sret);