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 |
b33afbcf |
18 | ** ? timeout => Timeout for LDAP operations (in seconds, default: 30) |
19 | ** Might not work for all LDAP libraries or servers. |
5100704d |
20 | ** |
21 | ** NOTE. This class should not be used directly. Use the |
22 | ** "AddressBook" class instead. |
245a6892 |
23 | ** |
24 | ** $Id$ |
5100704d |
25 | **/ |
26 | |
27 | class abook_ldap_server extends addressbook_backend { |
91be2362 |
28 | var $btype = 'remote'; |
29 | var $bname = 'ldap_server'; |
5100704d |
30 | |
31 | // Parameters changed by class |
91be2362 |
32 | var $sname = 'LDAP'; // Service name |
33 | var $server = ''; // LDAP server name |
5100704d |
34 | var $port = 389; // LDAP server port |
91be2362 |
35 | var $basedn = ''; // LDAP base DN |
36 | var $charset = 'utf-8'; // LDAP server charset |
5100704d |
37 | var $linkid = false; // PHP LDAP link ID |
38 | var $bound = false; // True if LDAP server is bound |
39 | var $maxrows = 250; // Max rows in result |
b33afbcf |
40 | var $timeout = 30; // Timeout for LDAP operations (in seconds) |
5100704d |
41 | |
42 | // Constructor. Connects to database |
43 | function abook_ldap_server($param) { |
91be2362 |
44 | if(!function_exists('ldap_connect')) { |
45 | $this->set_error('LDAP support missing from PHP'); |
1688203f |
46 | return; |
47 | } |
5100704d |
48 | if(is_array($param)) { |
91be2362 |
49 | $this->server = $param['host']; |
50 | $this->basedn = $param['base']; |
51 | if(!empty($param['port'])) |
52 | $this->port = $param['port']; |
53 | if(!empty($param['charset'])) |
54 | $this->charset = strtolower($param['charset']); |
55 | if(isset($param['maxrows'])) |
56 | $this->maxrows = $param['maxrows']; |
57 | if(isset($param['timeout'])) |
58 | $this->timeout = $param['timeout']; |
59 | if(empty($param['name'])) |
60 | $this->sname = 'LDAP: ' . $param['host']; |
5100704d |
61 | else |
91be2362 |
62 | $this->sname = $param['name']; |
5100704d |
63 | |
64 | $this->open(true); |
65 | } else { |
91be2362 |
66 | $this->set_error('Invalid argument to constructor'); |
5100704d |
67 | } |
68 | } |
69 | |
70 | |
71 | // Open the LDAP server. New connection if $new is true |
72 | function open($new = false) { |
91be2362 |
73 | $this->error = ''; |
5100704d |
74 | |
75 | // Connection is already open |
76 | if($this->linkid != false && !$new) |
77 | return true; |
78 | |
79 | $this->linkid = @ldap_connect($this->server, $this->port); |
80 | if(!$this->linkid) |
91be2362 |
81 | if(function_exists('ldap_error')) |
5100704d |
82 | return $this->set_error(ldap_error($this->linkid)); |
83 | else |
91be2362 |
84 | return $this->set_error('ldap_connect failed'); |
5100704d |
85 | |
86 | if(!@ldap_bind($this->linkid)) |
91be2362 |
87 | if(function_exists('ldap_error')) |
5100704d |
88 | return $this->set_error(ldap_error($this->linkid)); |
89 | else |
91be2362 |
90 | return $this->set_error('ldap_bind failed'); |
5100704d |
91 | |
92 | $this->bound = true; |
93 | |
94 | return true; |
95 | } |
96 | |
97 | |
98 | // Encode iso8859-1 string to the charset used by this LDAP server |
99 | function charset_encode($str) { |
91be2362 |
100 | if($this->charset == 'utf-8') { |
101 | if(function_exists('utf8_encode')) |
5100704d |
102 | return utf8_encode($str); |
103 | else |
104 | return $str; |
105 | } else { |
106 | return $str; |
107 | } |
108 | } |
109 | |
110 | |
111 | // Decode from charset used by this LDAP server to iso8859-1 |
112 | function charset_decode($str) { |
91be2362 |
113 | if($this->charset == 'utf-8') { |
114 | if(function_exists('utf8_decode')) |
5100704d |
115 | return utf8_decode($str); |
116 | else |
117 | return $str; |
118 | } else { |
119 | return $str; |
120 | } |
121 | } |
122 | |
123 | |
124 | // ========================== Public ======================== |
125 | |
126 | // Search the LDAP server |
127 | function search($expr) { |
128 | |
129 | // To be replaced by advanded search expression parsing |
130 | if(is_array($expr)) return false; |
131 | |
132 | // Encode the expression |
133 | $expr = $this->charset_encode($expr); |
146e0c45 |
134 | if(strstr($expr, "*") === false) |
84db4e6c |
135 | $expr = "*$expr*"; |
5100704d |
136 | $expression = "cn=$expr"; |
137 | |
138 | // Make sure connection is there |
139 | if(!$this->open()) |
140 | return false; |
141 | |
b33afbcf |
142 | // Do the search. Use improved ldap_search() if PHP version is |
143 | // 4.0.2 or newer. |
144 | if(sqCheckPHPVersion(4, 0, 2)) { |
145 | $sret = @ldap_search($this->linkid, $this->basedn, $expression, |
91be2362 |
146 | array('dn', 'o', 'ou', 'sn', 'givenname', |
147 | 'cn', 'mail', 'telephonenumber'), |
b33afbcf |
148 | 0, $this->maxrows, $this->timeout); |
149 | } else { |
150 | $sret = @ldap_search($this->linkid, $this->basedn, $expression, |
91be2362 |
151 | array('dn', 'o', 'ou', 'sn', 'givenname', |
152 | 'cn', 'mail', 'telephonenumber')); |
b33afbcf |
153 | } |
5100704d |
154 | |
155 | // Should get error from server using the ldap_error() function, |
156 | // but it only exist in the PHP LDAP documentation. |
157 | if(!$sret) |
91be2362 |
158 | if(function_exists('ldap_error')) |
5100704d |
159 | return $this->set_error(ldap_error($this->linkid)); |
160 | else |
91be2362 |
161 | return $this->set_error('ldap_search failed'); |
5100704d |
162 | |
163 | if(@ldap_count_entries($this->linkid, $sret) <= 0) |
164 | return array(); |
165 | |
166 | // Get results |
167 | $ret = array(); |
168 | $returned_rows = 0; |
169 | $res = @ldap_get_entries($this->linkid, $sret); |
91be2362 |
170 | for($i = 0 ; $i < $res['count'] ; $i++) { |
5100704d |
171 | $row = $res[$i]; |
172 | |
173 | // Extract data common for all e-mail addresses |
174 | // of an object. Use only the first name |
91be2362 |
175 | $nickname = $this->charset_decode($row['dn']); |
176 | $fullname = $this->charset_decode($row['cn'][0]); |
5100704d |
177 | |
91be2362 |
178 | if(empty($row['telephonenumber'][0])) $phone = ''; |
179 | else $phone = $this->charset_decode($row['telephonenumber'][0]); |
5100704d |
180 | |
91be2362 |
181 | if(!empty($row['ou'][0])) |
182 | $label = $this->charset_decode($row['ou'][0]); |
183 | else if(!empty($row['o'][0])) |
184 | $label = $this->charset_decode($row['o'][0]); |
5100704d |
185 | else |
91be2362 |
186 | $label = ''; |
5100704d |
187 | |
91be2362 |
188 | if(empty($row['givenname'][0])) $firstname = ''; |
189 | else $firstname = $this->charset_decode($row['givenname'][0]); |
5100704d |
190 | |
91be2362 |
191 | if(empty($row['sn'][0])) $surname = ''; |
192 | else $surname = $this->charset_decode($row['sn'][0]); |
5100704d |
193 | |
194 | // Add one row to result for each e-mail address |
7609273d |
195 | if(isset($row['mail']['count'])) { |
196 | for($j = 0 ; $j < $row['mail']['count'] ; $j++) { |
197 | array_push($ret, array('nickname' => $nickname, |
198 | 'name' => $fullname, |
199 | 'firstname' => $firstname, |
200 | 'lastname' => $surname, |
201 | 'email' => $row['mail'][$j], |
202 | 'label' => $label, |
203 | 'phone' => $phone, |
204 | 'backend' => $this->bnum, |
205 | 'source' => &$this->sname)); |
206 | |
207 | // Limit number of hits |
208 | $returned_rows++; |
209 | if(($returned_rows >= $this->maxrows) && |
210 | ($this->maxrows > 0) ) { |
211 | ldap_free_result($sret); |
212 | return $ret; |
213 | } |
214 | |
215 | } // for($j ...) |
216 | |
217 | } // isset($row['mail']['count']) |
218 | |
5100704d |
219 | } |
220 | |
221 | ldap_free_result($sret); |
222 | return $ret; |
223 | } // end search() |
224 | |
aeee871f |
225 | |
226 | // If you run a tiny LDAP server and you want the "List All" button |
227 | // to show EVERYONE, then uncomment this tiny block of code: |
228 | // |
229 | // function list_addr() { |
230 | // return $this->search("*"); |
231 | // } |
232 | // |
233 | // Careful with this -- it could get quite large for big sites. |
5100704d |
234 | } |
235 | ?> |