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. |
23 | **/ |
24 | |
25 | class abook_ldap_server extends addressbook_backend { |
26 | var $btype = "remote"; |
27 | var $bname = "ldap_server"; |
28 | |
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 |
b33afbcf |
38 | var $timeout = 30; // Timeout for LDAP operations (in seconds) |
5100704d |
39 | |
40 | // Constructor. Connects to database |
41 | function abook_ldap_server($param) { |
1688203f |
42 | if(!function_exists("ldap_connect")) { |
43 | $this->set_error("LDAP support missing from PHP"); |
44 | return; |
45 | } |
5100704d |
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"]; |
b33afbcf |
55 | if(isset($param["timeout"])) |
56 | $this->timeout = $param["timeout"]; |
5100704d |
57 | if(empty($param["name"])) |
58 | $this->sname = "LDAP: ".$param["host"]; |
59 | else |
60 | $this->sname = $param["name"]; |
61 | |
62 | $this->open(true); |
63 | } else { |
1e62e50e |
64 | $this->set_error("Invalid argument to constructor"); |
5100704d |
65 | } |
66 | } |
67 | |
68 | |
69 | // Open the LDAP server. New connection if $new is true |
70 | function open($new = false) { |
71 | $this->error = ""; |
72 | |
73 | // Connection is already open |
74 | if($this->linkid != false && !$new) |
75 | return true; |
76 | |
77 | $this->linkid = @ldap_connect($this->server, $this->port); |
78 | if(!$this->linkid) |
79 | if(function_exists("ldap_error")) |
80 | return $this->set_error(ldap_error($this->linkid)); |
81 | else |
82 | return $this->set_error("ldap_connect failed"); |
83 | |
84 | if(!@ldap_bind($this->linkid)) |
85 | if(function_exists("ldap_error")) |
86 | return $this->set_error(ldap_error($this->linkid)); |
87 | else |
88 | return $this->set_error("ldap_bind failed"); |
89 | |
90 | $this->bound = true; |
91 | |
92 | return true; |
93 | } |
94 | |
95 | |
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); |
101 | else |
102 | return $str; |
103 | } else { |
104 | return $str; |
105 | } |
106 | } |
107 | |
108 | |
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); |
114 | else |
115 | return $str; |
116 | } else { |
117 | return $str; |
118 | } |
119 | } |
120 | |
121 | |
122 | // ========================== Public ======================== |
123 | |
124 | // Search the LDAP server |
125 | function search($expr) { |
126 | |
127 | // To be replaced by advanded search expression parsing |
128 | if(is_array($expr)) return false; |
129 | |
130 | // Encode the expression |
131 | $expr = $this->charset_encode($expr); |
132 | if(!ereg("\*", $expr)) |
133 | $expr = "*$expr*"; |
134 | $expression = "cn=$expr"; |
135 | |
136 | // Make sure connection is there |
137 | if(!$this->open()) |
138 | return false; |
139 | |
b33afbcf |
140 | // Do the search. Use improved ldap_search() if PHP version is |
141 | // 4.0.2 or newer. |
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); |
147 | } else { |
148 | $sret = @ldap_search($this->linkid, $this->basedn, $expression, |
149 | array("dn", "o", "ou", "sn", "givenname", |
150 | "cn", "mail", "telephonenumber")); |
151 | } |
5100704d |
152 | |
153 | // Should get error from server using the ldap_error() function, |
154 | // but it only exist in the PHP LDAP documentation. |
155 | if(!$sret) |
156 | if(function_exists("ldap_error")) |
157 | return $this->set_error(ldap_error($this->linkid)); |
158 | else |
159 | return $this->set_error("ldap_search failed"); |
160 | |
161 | if(@ldap_count_entries($this->linkid, $sret) <= 0) |
162 | return array(); |
163 | |
164 | // Get results |
165 | $ret = array(); |
166 | $returned_rows = 0; |
167 | $res = @ldap_get_entries($this->linkid, $sret); |
168 | for($i = 0 ; $i < $res["count"] ; $i++) { |
169 | $row = $res[$i]; |
170 | |
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]); |
175 | |
176 | if(empty($row["telephonenumber"][0])) $phone = ""; |
177 | else $phone = $this->charset_decode($row["telephonenumber"][0]); |
178 | |
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]); |
183 | else |
184 | $label = ""; |
185 | |
186 | if(empty($row["givenname"][0])) $firstname = ""; |
187 | else $firstname = $this->charset_decode($row["givenname"][0]); |
188 | |
189 | if(empty($row["sn"][0])) $surname = ""; |
190 | else $surname = $this->charset_decode($row["sn"][0]); |
191 | |
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, |
195 | "name" => $fullname, |
196 | "firstname" => $firstname, |
197 | "lastname" => $surname, |
198 | "email" => $row["mail"][$j], |
199 | "label" => $label, |
200 | "phone" => $phone, |
201 | "backend" => $this->bnum, |
202 | "source" => &$this->sname)); |
203 | |
204 | // Limit number of hits |
205 | $returned_rows++; |
206 | if(($returned_rows >= $this->maxrows) && |
207 | ($this->maxrows > 0) ) { |
208 | ldap_free_result($sret); |
209 | return $ret; |
210 | } |
211 | |
212 | } |
213 | } |
214 | |
215 | ldap_free_result($sret); |
216 | return $ret; |
217 | } // end search() |
218 | |
219 | } |
220 | ?> |