Added function to check what PHP version we have.
[squirrelmail.git] / functions / abook_ldap_server.php
CommitLineData
5100704d 1<?php
2
3 /**
4 ** abook_ldap_server.php
5 **
6 ** Address book backend for LDAP server
7 **
8 ** An array with the following elements must be passed to
9 ** the class constructor (elements marked ? are optional):
10 **
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 **
19 ** NOTE. This class should not be used directly. Use the
20 ** "AddressBook" class instead.
21 **/
22
23 class abook_ldap_server extends addressbook_backend {
24 var $btype = "remote";
25 var $bname = "ldap_server";
26
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
36
37
38 // Constructor. Connects to database
39 function abook_ldap_server($param) {
1688203f 40 if(!function_exists("ldap_connect")) {
41 $this->set_error("LDAP support missing from PHP");
42 return;
43 }
5100704d 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"];
55 else
56 $this->sname = $param["name"];
57
58 $this->open(true);
59 } else {
1e62e50e 60 $this->set_error("Invalid argument to constructor");
5100704d 61 }
62 }
63
64
65 // Open the LDAP server. New connection if $new is true
66 function open($new = false) {
67 $this->error = "";
68
69 // Connection is already open
70 if($this->linkid != false && !$new)
71 return true;
72
73 $this->linkid = @ldap_connect($this->server, $this->port);
74 if(!$this->linkid)
75 if(function_exists("ldap_error"))
76 return $this->set_error(ldap_error($this->linkid));
77 else
78 return $this->set_error("ldap_connect failed");
79
80 if(!@ldap_bind($this->linkid))
81 if(function_exists("ldap_error"))
82 return $this->set_error(ldap_error($this->linkid));
83 else
84 return $this->set_error("ldap_bind failed");
85
86 $this->bound = true;
87
88 return true;
89 }
90
91
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);
97 else
98 return $str;
99 } else {
100 return $str;
101 }
102 }
103
104
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);
110 else
111 return $str;
112 } else {
113 return $str;
114 }
115 }
116
117
118 // ========================== Public ========================
119
120 // Search the LDAP server
121 function search($expr) {
122
123 // To be replaced by advanded search expression parsing
124 if(is_array($expr)) return false;
125
126 // Encode the expression
127 $expr = $this->charset_encode($expr);
128 if(!ereg("\*", $expr))
129 $expr = "*$expr*";
130 $expression = "cn=$expr";
131
132 // Make sure connection is there
133 if(!$this->open())
134 return false;
135
136 // Do the search
137 $sret = @ldap_search($this->linkid, $this->basedn, $expression,
138 array("dn", "o", "ou", "sn", "givenname",
139 "cn", "mail", "telephonenumber"));
140
141 // Should get error from server using the ldap_error() function,
142 // but it only exist in the PHP LDAP documentation.
143 if(!$sret)
144 if(function_exists("ldap_error"))
145 return $this->set_error(ldap_error($this->linkid));
146 else
147 return $this->set_error("ldap_search failed");
148
149 if(@ldap_count_entries($this->linkid, $sret) <= 0)
150 return array();
151
152 // Get results
153 $ret = array();
154 $returned_rows = 0;
155 $res = @ldap_get_entries($this->linkid, $sret);
156 for($i = 0 ; $i < $res["count"] ; $i++) {
157 $row = $res[$i];
158
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]);
163
164 if(empty($row["telephonenumber"][0])) $phone = "";
165 else $phone = $this->charset_decode($row["telephonenumber"][0]);
166
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]);
171 else
172 $label = "";
173
174 if(empty($row["givenname"][0])) $firstname = "";
175 else $firstname = $this->charset_decode($row["givenname"][0]);
176
177 if(empty($row["sn"][0])) $surname = "";
178 else $surname = $this->charset_decode($row["sn"][0]);
179
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,
183 "name" => $fullname,
184 "firstname" => $firstname,
185 "lastname" => $surname,
186 "email" => $row["mail"][$j],
187 "label" => $label,
188 "phone" => $phone,
189 "backend" => $this->bnum,
190 "source" => &$this->sname));
191
192 // Limit number of hits
193 $returned_rows++;
194 if(($returned_rows >= $this->maxrows) &&
195 ($this->maxrows > 0) ) {
196 ldap_free_result($sret);
197 return $ret;
198 }
199
200 }
201 }
202
203 ldap_free_result($sret);
204 return $ret;
205 } // end search()
206
207 }
208?>