Added addressbook+LDAP functions.
[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) {
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"];
51 else
52 $this->sname = $param["name"];
53
54 $this->open(true);
55 } else {
56 $this->set_error(_("Invalid argument to constructor"));
57 }
58 }
59
60
61 // Open the LDAP server. New connection if $new is true
62 function open($new = false) {
63 $this->error = "";
64
65 // Connection is already open
66 if($this->linkid != false && !$new)
67 return true;
68
69 $this->linkid = @ldap_connect($this->server, $this->port);
70 if(!$this->linkid)
71 if(function_exists("ldap_error"))
72 return $this->set_error(ldap_error($this->linkid));
73 else
74 return $this->set_error("ldap_connect failed");
75
76 if(!@ldap_bind($this->linkid))
77 if(function_exists("ldap_error"))
78 return $this->set_error(ldap_error($this->linkid));
79 else
80 return $this->set_error("ldap_bind failed");
81
82 $this->bound = true;
83
84 return true;
85 }
86
87
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);
93 else
94 return $str;
95 } else {
96 return $str;
97 }
98 }
99
100
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);
106 else
107 return $str;
108 } else {
109 return $str;
110 }
111 }
112
113
114 // ========================== Public ========================
115
116 // Search the LDAP server
117 function search($expr) {
118
119 // To be replaced by advanded search expression parsing
120 if(is_array($expr)) return false;
121
122 // Encode the expression
123 $expr = $this->charset_encode($expr);
124 if(!ereg("\*", $expr))
125 $expr = "*$expr*";
126 $expression = "cn=$expr";
127
128 // Make sure connection is there
129 if(!$this->open())
130 return false;
131
132 // Do the search
133 $sret = @ldap_search($this->linkid, $this->basedn, $expression,
134 array("dn", "o", "ou", "sn", "givenname",
135 "cn", "mail", "telephonenumber"));
136
137 // Should get error from server using the ldap_error() function,
138 // but it only exist in the PHP LDAP documentation.
139 if(!$sret)
140 if(function_exists("ldap_error"))
141 return $this->set_error(ldap_error($this->linkid));
142 else
143 return $this->set_error("ldap_search failed");
144
145 if(@ldap_count_entries($this->linkid, $sret) <= 0)
146 return array();
147
148 // Get results
149 $ret = array();
150 $returned_rows = 0;
151 $res = @ldap_get_entries($this->linkid, $sret);
152 for($i = 0 ; $i < $res["count"] ; $i++) {
153 $row = $res[$i];
154
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]);
159
160 if(empty($row["telephonenumber"][0])) $phone = "";
161 else $phone = $this->charset_decode($row["telephonenumber"][0]);
162
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]);
167 else
168 $label = "";
169
170 if(empty($row["givenname"][0])) $firstname = "";
171 else $firstname = $this->charset_decode($row["givenname"][0]);
172
173 if(empty($row["sn"][0])) $surname = "";
174 else $surname = $this->charset_decode($row["sn"][0]);
175
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,
179 "name" => $fullname,
180 "firstname" => $firstname,
181 "lastname" => $surname,
182 "email" => $row["mail"][$j],
183 "label" => $label,
184 "phone" => $phone,
185 "backend" => $this->bnum,
186 "source" => &$this->sname));
187
188 // Limit number of hits
189 $returned_rows++;
190 if(($returned_rows >= $this->maxrows) &&
191 ($this->maxrows > 0) ) {
192 ldap_free_result($sret);
193 return $ret;
194 }
195
196 }
197 }
198
199 ldap_free_result($sret);
200 return $ret;
201 } // end search()
202
203 }
204?>