commit | author | age
|
d1d2c4
|
1 |
<?php |
S |
2 |
|
|
3 |
/* |
|
4 |
+-----------------------------------------------------------------------+ |
|
5 |
| program/steps/addressbook/ldapsearch.inc | |
|
6 |
| | |
|
7 |
| This file is part of the RoundCube Webmail client | |
|
8 |
| Copyright (C) 2005, RoundCube Dev. - Switzerland | |
|
9 |
| Licensed under the GNU GPL | |
|
10 |
| | |
|
11 |
| PURPOSE: | |
|
12 |
| Show an ldap search form in the addressbook | |
|
13 |
| | |
|
14 |
+-----------------------------------------------------------------------+ |
|
15 |
| Author: Justin Randell <justin.randell@gmail.com> | |
|
16 |
+-----------------------------------------------------------------------+ |
|
17 |
|
|
18 |
$Id$ |
|
19 |
|
|
20 |
*/ |
|
21 |
require_once 'include/rcube_ldap.inc'; |
|
22 |
|
|
23 |
/** |
|
24 |
* draw the ldap public search form |
|
25 |
*/ |
|
26 |
function rcmail_ldap_public_search_form($attrib) |
|
27 |
{ |
|
28 |
global $CONFIG, $JS_OBJECT_NAME, $OUTPUT; |
|
29 |
if (!$CONFIG['ldap_public']) |
|
30 |
{ |
|
31 |
// no ldap servers to search |
|
32 |
show_message('noldapserver', 'warning'); |
|
33 |
rcmail_overwrite_action('add'); |
|
34 |
return false; |
|
35 |
} |
|
36 |
else |
|
37 |
{ |
|
38 |
// store some information in the session |
|
39 |
$_SESSION['ldap_public']['server_count'] = $server_count = count($CONFIG['ldap_public']); |
|
40 |
$_SESSION['ldap_public']['server_names'] = $server_names = array_keys($CONFIG['ldap_public']); |
|
41 |
} |
|
42 |
|
|
43 |
list($form_start, $form_end) = get_form_tags($attrib); |
|
44 |
$out = "$form_start<table id=\"ldap_public_search_table\">\n\n"; |
|
45 |
|
|
46 |
// search name field |
|
47 |
$search_name = new textfield(array('name' => '_ldap_public_search_name', |
|
48 |
'id' => 'rcmfd_ldap_public_search_name')); |
|
49 |
$out .= "<tr><td class=\"title\"><label for=\"rcmfd_ldap_public_search_name\">" . |
|
50 |
rep_specialchars_output(rcube_label('ldappublicsearchname')) . |
|
51 |
"</label></td><td>" . $search_name->show() . "</td></tr>\n"; |
|
52 |
|
|
53 |
|
|
54 |
// there's more than one server to search for, show a dropdown menu |
|
55 |
if ($server_count > 1) |
|
56 |
{ |
|
57 |
$select_server = new select(array('name' => '_ldap_public_servers', |
|
58 |
'id' => 'rcfmd_ldap_public_servers')); |
|
59 |
|
|
60 |
$select_server->add($server_names, $server_names); |
|
61 |
|
|
62 |
$out .= '<tr><td class="title"><label for="rcfmd_ldap_public_servers">' . |
|
63 |
rep_specialchars_output(rcube_label('ldappublicserverselect')) . |
|
64 |
"</label></td><td>" . $select_server->show() . "</td></tr>\n"; |
|
65 |
} |
|
66 |
|
|
67 |
// foreach configured ldap server, set up the search fields |
|
68 |
for ($i = 0; $i < $server_count; $i++) |
|
69 |
{ |
|
70 |
$server = $CONFIG['ldap_public'][$server_names[$i]]; |
|
71 |
|
|
72 |
// only display one search fields select - js takes care of the rest |
|
73 |
if (!$i) |
|
74 |
{ |
|
75 |
$field_name = '_ldap_public_search_field'; |
|
76 |
$field_id = 'rcfmd_ldap_public_search_field'; |
|
77 |
|
|
78 |
$search_fields = new select(array('name' => $field_name, |
|
79 |
'id' => $field_id)); |
|
80 |
|
c4a621
|
81 |
$search_fields->add(array_keys($server['search_fields']), array_values($server['search_fields'])); |
d1d2c4
|
82 |
$out .= '<tr><td class="title"><label for="' . $field_id . '">' . |
S |
83 |
rep_specialchars_output(rcube_label('ldappublicsearchfield')) . |
|
84 |
"</label></td><td>" . $search_fields->show() . "</td></tr>\n"; |
9d2c0d
|
85 |
|
S |
86 |
$attributes = array('name' => '_ldap_public_search_type', |
|
87 |
'id' => 'rcmfd_ldap_public_search_type'); |
d1d2c4
|
88 |
|
9d2c0d
|
89 |
// if there's only one server, and it doesn't accept fuzzy searches, |
S |
90 |
// then check and disable the check box - thanks pieter |
|
91 |
if ($server_count == 1 && !$server['fuzzy_search']) |
|
92 |
{ |
|
93 |
$attributes['CHECKED'] = 'CHECKED'; |
|
94 |
$attributes['disabled'] = 'disabled'; |
|
95 |
} |
|
96 |
|
|
97 |
$search_type = new checkbox($attributes); |
d1d2c4
|
98 |
|
S |
99 |
$out .= '<tr id="ldap_fuzzy_search"><td class="title"><label for="rcmfd_ldap_public_search_type">' . |
|
100 |
rep_specialchars_output(rcube_label('ldappublicsearchtype')) . |
|
101 |
"</label></td><td>" . $search_type->show() . "</td></tr>\n"; |
|
102 |
} |
9d2c0d
|
103 |
|
S |
104 |
if ($server_count > 1) |
|
105 |
{ |
|
106 |
// store the search fields in a js array for each server |
|
107 |
$js = ''; |
39ecb7
|
108 |
foreach ($server['search_fields'] as $search_name => $search_value) |
S |
109 |
$js .= "['$search_name', '$search_value'], "; |
d1d2c4
|
110 |
|
9d2c0d
|
111 |
// store whether this server accepts fuzzy search as last item in array |
S |
112 |
$js .= $server['fuzzy_search'] ? "'fuzzy'" : "'exact'"; |
|
113 |
$OUTPUT->add_script("rcmail.set_env('{$server_names[$i]}_search_fields', new Array($js));"); |
|
114 |
} |
d1d2c4
|
115 |
} |
S |
116 |
|
|
117 |
// add contact button label text |
|
118 |
$OUTPUT->add_script("rcmail.set_env('addcontact', '" . rcube_label('addcontact') . "');"); |
|
119 |
|
|
120 |
$out .= "\n</table>$form_end"; |
|
121 |
return $out; |
|
122 |
} |
|
123 |
|
|
124 |
/** |
|
125 |
* get search values and return ldap contacts |
|
126 |
*/ |
|
127 |
function rcmail_ldap_public_list() |
|
128 |
{ |
|
129 |
// just return if we are not being called from a search form |
|
130 |
if (!isset($_POST['_action'])) |
|
131 |
return null; |
|
132 |
|
|
133 |
global $CONFIG, $OUTPUT, $JS_OBJECT_NAME; |
|
134 |
|
|
135 |
// show no search name warning and exit |
|
136 |
if (empty($_POST['_ldap_public_search_name']) || trim($_POST['_ldap_public_search_name']) == '') |
|
137 |
{ |
|
138 |
show_message('nosearchname', 'warning'); |
|
139 |
return false; |
|
140 |
} |
|
141 |
|
|
142 |
// set up ldap server(s) array or bail |
|
143 |
if ($_SESSION['ldap_public']['server_count'] > 1) |
|
144 |
// show no ldap server warning and exit |
|
145 |
if (empty($_POST['_ldap_public_servers'])) |
|
146 |
{ |
|
147 |
show_message('noldappublicserver', 'warning'); |
|
148 |
return false; |
|
149 |
} |
|
150 |
else |
|
151 |
$server_name = $_POST['_ldap_public_servers']; |
|
152 |
else if ($_SESSION['ldap_public']['server_count'] == 1) |
|
153 |
$server_name = $_SESSION['ldap_public']['server_names'][0]; |
|
154 |
else |
|
155 |
return false; |
|
156 |
|
|
157 |
// get search parameters |
|
158 |
$search_value = $_POST['_ldap_public_search_name']; |
|
159 |
$search_field = $_POST['_ldap_public_search_field']; |
|
160 |
|
|
161 |
// only use the post var for search type if the ldap server allows 'like' |
|
162 |
$exact = true; |
|
163 |
if ($CONFIG['ldap_public'][$server_name]['fuzzy_search']) |
|
164 |
$exact = isset($_POST['_ldap_public_search_type']) ? true : false; |
|
165 |
|
|
166 |
// perform an ldap search |
|
167 |
$contacts = rcmail_ldap_contact_search($search_value, |
|
168 |
$search_field, |
|
169 |
$CONFIG['ldap_public'][$server_name], |
|
170 |
$exact); |
|
171 |
|
|
172 |
// if no results, show a warning and return |
|
173 |
if (!$contacts) |
|
174 |
{ |
|
175 |
show_message('nocontactsreturned', 'warning'); |
|
176 |
return false; |
|
177 |
} |
|
178 |
|
|
179 |
// add id to message list table if not specified |
|
180 |
if (!strlen($attrib['id'])) |
|
181 |
$attrib['id'] = 'ldapAddressList'; |
|
182 |
|
|
183 |
// define table class |
|
184 |
$attrib['class'] = 'records-table'; |
|
185 |
$attrib['cellspacing'] = 0; |
|
186 |
|
|
187 |
// define list of cols to be displayed |
|
188 |
$a_show_cols = array('name', 'email'); |
|
189 |
|
|
190 |
// create XHTML table |
|
191 |
$out = rcube_table_output($attrib, $contacts, $a_show_cols, 'row_id'); |
|
192 |
|
|
193 |
// set client env |
|
194 |
$javascript = "$JS_OBJECT_NAME.gui_object('ldapcontactslist', '{$attrib['id']}');\n"; |
|
195 |
$OUTPUT->add_script($javascript); |
|
196 |
|
|
197 |
return $out; |
|
198 |
} |
|
199 |
|
|
200 |
/** |
|
201 |
* perform search for contacts from given public ldap server |
|
202 |
*/ |
|
203 |
function rcmail_ldap_contact_search($search_value, $search_field, $server, $exact=true) |
|
204 |
{ |
|
205 |
global $CONFIG; |
|
206 |
|
|
207 |
$attributes = array($server['name_field'], $server['mail_field']); |
|
208 |
|
|
209 |
$LDAP = new rcube_ldap(); |
|
210 |
if ($LDAP->connect($server['hosts'], $server['port'], $server['protocol'])) |
|
211 |
{ |
|
212 |
$filter = "$search_field=" . ($exact ? $search_value : "*$search_value*"); |
|
213 |
$result = $LDAP->search($server['base_dn'], |
|
214 |
$filter, |
|
215 |
$attributes, |
|
216 |
$server['scope'], |
|
217 |
$sort=null); |
|
218 |
|
|
219 |
// add any results to contact array |
|
220 |
if ($result['count']) |
|
221 |
{ |
|
222 |
for ($n = 0; $n < $result['count']; $n++) |
|
223 |
{ |
|
224 |
$contacts[$n]['name'] = $result[$n][$server['name_field']][0]; |
|
225 |
$contacts[$n]['email'] = $result[$n][$server['mail_field']][0]; |
|
226 |
$contacts[$n]['row_id'] = $n + 1; |
|
227 |
} |
|
228 |
} |
|
229 |
} |
|
230 |
else |
|
231 |
return false; |
|
232 |
|
|
233 |
// cleanup |
|
234 |
$LDAP->close(); |
|
235 |
|
|
236 |
if (!$result['count']) |
|
237 |
return false; |
|
238 |
|
|
239 |
// weed out duplicate emails |
|
240 |
for ($n = 0; $n < $result['count']; $n++) |
|
241 |
for ($i = 0; $i < $result['count']; $i++) |
|
242 |
if ($contacts[$i]['email'] == $contacts[$n]['email'] && $i != $n) |
|
243 |
unset($contacts[$n]); |
|
244 |
|
|
245 |
return $contacts; |
|
246 |
} |
|
247 |
|
|
248 |
function get_form_tags($attrib) |
|
249 |
{ |
|
250 |
global $OUTPUT, $JS_OBJECT_NAME, $EDIT_FORM, $SESS_HIDDEN_FIELD; |
|
251 |
|
|
252 |
$form_start = ''; |
|
253 |
if (!strlen($EDIT_FORM)) |
|
254 |
{ |
|
255 |
$hiddenfields = new hiddenfield(array('name' => '_task', 'value' => $GLOBALS['_task'])); |
|
256 |
$hiddenfields->add(array('name' => '_action', 'value' => 'ldappublicsearch')); |
|
257 |
|
|
258 |
if ($_GET['_framed'] || $_POST['_framed']) |
|
259 |
$hiddenfields->add(array('name' => '_framed', 'value' => 1)); |
|
260 |
|
|
261 |
$form_start .= !strlen($attrib['form']) ? '<form name="form" action="./" method="post">' : ''; |
|
262 |
$form_start .= "\n$SESS_HIDDEN_FIELD\n"; |
|
263 |
$form_start .= $hiddenfields->show(); |
|
264 |
} |
|
265 |
|
|
266 |
$form_end = (strlen($EDIT_FORM) && !strlen($attrib['form'])) ? '</form>' : ''; |
|
267 |
$form_name = strlen($attrib['form']) ? $attrib['form'] : 'form'; |
|
268 |
|
|
269 |
$OUTPUT->add_script("$JS_OBJECT_NAME.gui_object('ldappublicsearchform', '$form_name');"); |
|
270 |
|
|
271 |
$EDIT_FORM = $form_name; |
|
272 |
|
|
273 |
return array($form_start, $form_end); |
|
274 |
} |
|
275 |
|
|
276 |
parse_template('ldappublicsearch'); |
|
277 |
?> |