/[drupal]/contributions/modules/ldapdirectory/ldapdirectory.module
ViewVC logotype

Contents of /contributions/modules/ldapdirectory/ldapdirectory.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.73 - (show annotations) (download) (as text)
Mon Feb 9 20:03:37 2009 UTC (9 months, 2 weeks ago) by rmiddle
Branch: MAIN
CVS Tags: DRUPAL-6--0-1, HEAD
Changes since 1.72: +5 -1 lines
File MIME type: text/x-php
OK that is the last fix.  I have working verison now
1 <?php
2 // $Id: ldapdirectory.module,v 1.72 2009/02/09 18:55:06 rmiddle Exp $
3
4 /**
5 * @file
6 * Ldap Directory module displays a block were you can search an ldap tree
7 * for matching records.
8 */
9
10
11 //include_once('ldap_integration/ldapdirectory.conf.php');
12 //include_once('ldap_integration/libdebug.php');
13 //include_once('ldap_integration/LDAPInterface.php');
14 include_once(drupal_get_path('module', 'ldapauth') .'/ldap_integration/libdebug.php');
15 include_once(drupal_get_path('module', 'ldapauth') .'/ldap_integration/LDAPInterface.php');
16
17 // Private constants. Do not touch
18 define(LDAP_GROUP_IN_DN, 'ldap_groups_in_dn');
19 define(LDAP_GROUP_IN_ATTR, 'ldap_groups_in_attr');
20 define(LDAP_GROUP_AS_ENTRIES, 'ldap_groups_as_entries');
21
22 // Private constants (default values). Do not touch either
23 define(LDAP_DEFAULT_GROUP_DN_ATTRIBUTE, 'ou');
24 define(LDAP_DEFAULT_GROUP_ATTR, 'userGroups');
25 define(LDAP_DEFAULT_GROUP_ENTRIES, 'cn=Group,dc=example,dc=com');
26 define(LDAP_DEFAULT_GROUP_ENTRIES_ATTRIBUTE, 'memberUid');
27
28 /**
29 * Implementation of hook_help().
30 */
31
32 function ldapdirectory_help($path, $arg) {
33 $output = '';
34
35 switch ($path) {
36 case 'admin/modules#ldapdirectory':
37 $output = t('LDAP Groups');
38 case 'admin/help#ldapdirectory':
39 case 'user/help#ldapdirectory':
40 $output = t('Maps LDAP groups to Drupal roles for users authenticated via <strong>ldapauth</strong> module.');
41 break;
42 case 'admin/settings/ldapdirectory':
43 return t('<p style="margin: 1em;"><strong style="color: red;">PLEASE NOTE</strong>: advanced configuration for this module can be set by editing the module\'s config file, located at <em style="font-style: normal; padding: 1px 3px; border: 1px solid #8888CC; background-color: #DDDDFF">modules/ldap_integration/ldapdirectory.conf.php</em> in your Drupal install.</p>');
44 break;
45 }
46 return $output;
47 }
48
49
50 /**
51 * Valid permissions for this module
52 * @return An array of valid permissions for the ldapdirectory module
53 */
54 function ldapdirectory_perm() {
55 return array('view ldap directory block');
56 }
57
58 /**
59 * Implementation of hook_menu().
60 */
61 function ldapdirectory_menu() {
62 // Administration Menu
63 $items['admin/settings/ldapdirectory'] = array(
64 'title' => 'LDAP Directory',
65 'description' => 'Configure LDAP Directory Settings',
66 'page callback' => 'ldapdirectory_admin_list',
67 'access arguments' => array('administer site configuration'),
68 );
69 $items['admin/settings/ldapdirectory/add'] = array(
70 'title' => 'LDAP Directory',
71 'page callback' => 'drupal_get_form',
72 'page arguments' => array('ldapdirectory_admin_edit'),
73 'type' => MENU_CALLBACK,
74 'weight' => 1,
75 'access arguments' => array('administer site configuration'),
76 );
77 $items['admin/settings/ldapdirectory/edit'] = array(
78 'title' => 'LDAP Directory',
79 'page callback' => 'drupal_get_form',
80 'page arguments' => array('ldapdirectory_admin_edit'),
81 'type' => MENU_CALLBACK,
82 'weight' => 1,
83 'access arguments' => array('administer site configuration'),
84 );
85 $items['admin/settings/ldapdirectory/delete'] = array(
86 'title' => 'LDAP Directory',
87 'page callback' => 'drupal_get_form',
88 'page arguments' => array('ldapdirectory_admin_delete'),
89 'type' => MENU_CALLBACK,
90 'weight' => 1,
91 'access arguments' => array('administer site configuration'),
92 );
93 // Normal Menu Items
94 $items['ldapdirectory'] = array(
95 'title' => 'LDAP Directory',
96 'page callback' => 'ldapdirectory_main',
97 'type' => MENU_NORMAL_ITEM,
98 'weight' => 1,
99 'access arguments' => array('view ldap directory block'),
100 );
101
102 // Search for Users
103 $items['ldapdirectory/search'] = array(
104 'title' => 'Search Results',
105 'page callback' => 'ldapdirectory_display_search',
106 'type' => MENU_CALLBACK,
107 'weight' => 1,
108 'access arguments' => array('view ldap directory block'),
109 );
110
111 // Picture Items
112 $items['ldapdirectory/picture'] = array(
113 'title' => 'Picture',
114 'page callback' => 'ldapdirectory_getpicture',
115 'type' => MENU_CALLBACK,
116 'weight' => 1,
117 'access arguments' => array('view ldap directory block'),
118 );
119
120 // VCF Items
121 $items['ldapdirectory/vcard'] = array(
122 'title' => 'Business Card',
123 'page callback' => 'ldapdirectory_getvcard',
124 'type' => MENU_CALLBACK,
125 'weight' => 1,
126 'access arguments' => array('view ldap directory block'),
127 );
128 return $items;
129 }
130
131 function ldapdirectory_admin_list() {
132 $ldapauth = db_query("SELECT sid, name FROM {ldapauth} ORDER BY sid");
133 $lists = array();
134 $output = "";
135
136 while ($row = db_fetch_object($ldapauth)) {
137 $header = array(
138 $row->name,
139 l(t('Add New'), 'admin/settings/ldapdirectory/add/'. $row->sid),
140 array(
141 'data' => t('Operations'),
142 'colspan' => 2
143 )
144 );
145 $ldap_sid = db_query("SELECT did, ld_name, enable_directory FROM {ldapdirectory_config} WHERE sid = '%s' ORDER BY did", $row->sid);
146 while ($row_ld = db_fetch_object($ldap_sid)) {
147 if ($row_ld->enable_directory) {
148 $ld_active = t('Active');
149 }
150 else {
151 $ld_active = t('Deactive');
152 }
153 $lists[] = array(t($row_ld->ld_name), $ld_active, l(t('edit'), 'admin/settings/ldapdirectory/edit/'. $row_ld->did), l(t('delete'), 'admin/settings/ldapdirectory/delete/'. $row_ld->did));
154 }
155 $output .= theme('table', $header, $lists);
156 unset($lists);
157 }
158 return $output;
159 }
160
161 function ldapdirectory_admin_delete(&$form_state, $ldap_directory_did) {
162 if ($ldap_directory_did != NULL) {
163 $form['delete_did'] = array(
164 '#type' => 'value',
165 '#value' => $ldap_directory_did,
166 );
167 return confirm_form(
168 $form,
169 t('Are you sure you want to delete this directory mapping ?'),
170 'admin/settings/ldapdirectory',
171 t('<em>This action cannot be undone.</p>'),
172 t('Delete'),
173 t('Cancel')
174 );
175 }
176 return $form;
177 }
178
179 function ldapdirectory_admin_delete_submit($form, &$form_state) {
180 if ($form_state['values']['confirm']) {
181 db_query("DELETE FROM {ldapdirectory_config} WHERE did = '%s'", $form_state['values']['delete_did']);
182 watchdog('ldap', 'ldapdirectory: ldap config %config Deleted.', array('%config' => $form_state['values']['delete_did']));
183 }
184 $form_state['redirect'] = 'admin/settings/ldapdirectory';
185 }
186
187 function ldapdirectory_admin_edit(&$form_state, $ldap_directory_did) {
188 $edit = array();
189 if ($ldap_directory_did != NULL) {
190 if (arg(3) == 'add') {
191 $form['sid'] = array(
192 '#type' => 'value',
193 '#value' => $ldap_directory_did,
194 );
195 unset($form['did']);
196 }
197 else if (arg(3) == 'edit') {
198 $edit = db_fetch_array(db_query("SELECT * FROM {ldapdirectory_config} WHERE did = %d", $ldap_directory_did));
199 $form['sid'] = array(
200 '#type' => 'value',
201 '#value' => $edit['sid'],
202 );
203 $form['did'] = array(
204 '#type' => 'value',
205 '#value' => $edit['did'],
206 );
207 }
208 $form['general-settings'] = array(
209 '#type' => 'fieldset',
210 '#title' => 'General Settings for '. $edit['ld_name'],
211 '#collapsible' => TRUE,
212 '#collapsed' => FALSE
213 );
214 $form['general-settings']['ld_name'] = array(
215 '#type' => 'textfield',
216 '#title' => t('Search Name'),
217 '#default_value' => $edit['ld_name'],
218 '#description' => t('Choose a <em><strong>unique</strong></em> name for this search configuration.'),
219 '#size' => 50,
220 '#maxlength' => 255,
221 '#required' => TRUE,
222 );
223 $form['general-settings']['enable_directory'] = array(
224 '#type' => 'checkbox',
225 '#title' => t('Enabled'),
226 '#default_value' => $edit['enable_directory'],
227 '#description' => t('Check this option to enable directory searches for this instance.')
228 );
229 $form['general-settings']['collapse_blocks'] = array(
230 '#type' => 'checkbox',
231 '#title' => t('Collapse block'),
232 '#default_value' => $edit['collapse_blocks'],
233 '#description' => t('Check this option to collapse the search block by default..')
234 );
235 $form['general-settings']['search_field_enabled'] = array(
236 '#type' => 'checkbox',
237 '#title' => t('Enable text search'),
238 '#default_value' => $edit['search_field_enabled'],
239 '#description' => t('Check this option to enable a text search.')
240 );
241 $form['general-settings']['attributes'] = array(
242 '#type' => 'textarea',
243 '#title' => t('Directory attributes'),
244 '#default_value' => $edit['attributes'],
245 '#description' => t('Enter the information you want to extract for each record in field=label pairs separated by commas. For example <em style="font-style: normal; padding: 1px 3px; border: 1px solid #8888CC; background-color: #DDDDFF">cn=Name,telephoneNumber=Phone,facsimilieNumber=fax</em>')
246 );
247 $form['picture-settings'] = array(
248 '#type' => 'fieldset',
249 '#title' => 'LDAP Pictures',
250 '#collapsible' => TRUE,
251 '#collapsed' => TRUE
252 );
253 $form['picture-settings']['picture_on'] = array(
254 '#type' => 'checkbox',
255 '#title' => t('Enabled'),
256 '#default_value' => $edit['picture_on'],
257 '#description' => t('Check this option to enable directory pictures for this instance.')
258 );
259 $form['picture-settings']['picture_attr'] = array(
260 '#type' => 'textfield',
261 '#title' => t('Picture attribute'),
262 '#default_value' => $edit['picture_attr'],
263 '#size' => 50,
264 '#maxlength' => 50,
265 '#description' => t('The attribute from the LDAP record containing the picture. For example <em style="font-style: normal; padding: 1px 3px; border: 1px solid #8888CC; background-color: #DDDDFF">jpegPhoto</em>')
266 );
267 $form['picture-settings']['picture_default'] = array(
268 '#type' => 'textfield',
269 '#title' => t('Default picture'),
270 '#default_value' => $edit['picture_default'],
271 '#size' => 50,
272 '#maxlength' => 50,
273 '#description' => t('The URL to the default picture for all users. For example <em style="font-style: normal; padding: 1px 3px; border: 1px solid #8888CC; background-color: #DDDDFF">http://www.example.com/defaultphoto.png</em>')
274 );
275 $form['vacation-settings'] = array(
276 '#type' => 'fieldset',
277 '#title' => 'LDAP Vacation',
278 '#collapsible' => TRUE,
279 '#collapsed' => TRUE
280 );
281 $form['vacation-settings']['vacationcheck_on'] = array(
282 '#type' => 'checkbox',
283 '#title' => t('Enabled'),
284 '#default_value' => $edit['vacationcheck_on'],
285 '#description' => t('Check this option to enable directory vacations for this instance.')
286 );
287 $form['vacation-settings']['vacationcheck_status'] = array(
288 '#type' => 'textfield',
289 '#title' => t('Vacation status attribute'),
290 '#default_value' => $edit['vacationcheck_status'],
291 '#size' => 50,
292 '#maxlength' => 50,
293 '#description' => t('The attribute from the LDAP record containing the vacation status. For example <em style="font-style: normal; padding: 1px 3px; border: 1px solid #8888CC; background-color: #DDDDFF">vacationActive</em>')
294 );
295 $form['vacation-settings']['vacationcheck_message'] = array(
296 '#type' => 'textfield',
297 '#title' => t('Vacation message attribute'),
298 '#default_value' => $edit['vacationcheck_message'],
299 '#size' => 50,
300 '#maxlength' => 50,
301 '#description' => t('The LDAP attribute containing the vacation message. For example <em style="font-style: normal; padding: 1px 3px; border: 1px solid #8888CC; background-color: #DDDDFF">vacationMessage</em>')
302 );
303 $form['vcard-settings'] = array(
304 '#type' => 'fieldset',
305 '#title' => 'LDAP Business Cards',
306 '#collapsible' => TRUE,
307 '#collapsed' => TRUE
308 );
309 $form['vcard-settings']['vcard_on'] = array(
310 '#type' => 'checkbox',
311 '#title' => t('Enabled'),
312 '#default_value' => $edit['vcard_on'],
313 '#description' => t('Check this option to enable directory business cards for this instance.')
314 );
315 $form['dirlink-settings'] = array(
316 '#type' => 'fieldset',
317 '#title' => 'Directory links',
318 '#collapsible' => TRUE,
319 '#collapsed' => TRUE
320 );
321 $form['dirlink-settings']['maillink_on'] = array(
322 '#type' => 'checkbox',
323 '#title' => t('Enabled'),
324 '#default_value' => $edit['maillink_on'],
325 '#description' => t('Check this option to enable directory maillink for this instance.')
326 );
327 $form['dirlink-settings']['maillink_attr'] = array(
328 '#type' => 'textfield',
329 '#title' => t('Mail attribute'),
330 '#default_value' => $edit['maillink_attr'],
331 '#size' => 50,
332 '#maxlength' => 50,
333 '#description' => t('The attribute from the LDAP record containing the mail field. For example <em style="font-style: normal; padding: 1px 3px; border: 1px solid #8888CC; background-color: #DDDDFF">mail</em>')
334 );
335 $form['dirlink-settings']['skypelink_on'] = array(
336 '#type' => 'checkbox',
337 '#title' => t('Enabled'),
338 '#default_value' => $edit['skypelink_on'],
339 '#description' => t('Check this option to enable directory skypelink for this instance.')
340 );
341 $form['dirlink-settings']['skypelink_attr'] = array(
342 '#type' => 'textfield',
343 '#title' => t('Skype attribute'),
344 '#default_value' => $edit['skypelink_attr'],
345 '#size' => 50,
346 '#maxlength' => 50,
347 '#description' => t('The attribute from the LDAP record containing the skype field. For example <em style="font-style: normal; padding: 1px 3px; border: 1px solid #8888CC; background-color: #DDDDFF">skype</em>')
348 );
349 $form['server-settings'] = array(
350 '#type' => 'fieldset',
351 '#title' => 'Organisational Units',
352 '#collapsible' => TRUE,
353 '#collapsed' => TRUE
354 );
355 $form['server-settings']['ou_on'] = array(
356 '#type' => 'checkbox',
357 '#title' => t('Display organizational unit\'s'),
358 '#default_value' => $edit['ou_on'],
359 '#description' => t('Check this option to turn on LDAP organizational units.')
360 );
361
362 $form['server-settings']['ou_filter'] = array(
363 '#type' => 'textfield',
364 '#title' => t('Search filter to use for finding organizational units'),
365 '#default_value' => $edit['ou_filter'],
366 '#size' => 50,
367 '#maxlength' => 255,
368 '#description' => t('The filter to apply when searching for organizational units. For example <em style="font-style: normal; padding: 1px 3px; border: 1px solid #8888CC; background-color: #DDDDFF">offices</em>, as the objectclass which holds details on office locations would be <em style="font-style: normal; padding: 1px 3px; border: 1px solid #8888CC; background-color: #DDDDFF">objectClass=offices</em>.')
369 );
370
371 $form['server-settings']['ou_dn'] = array(
372 '#type' => 'textfield',
373 '#title' => t('Base DN for organizational units'),
374 '#default_value' => $edit['ou_dn'],
375 '#size' => 50,
376 '#maxlength' => 50,
377 '#description' => t('The base dn for searching organizational units.')
378 );
379
380 $form['server-settings']['ou_attribute'] = array(
381 '#type' => 'textfield',
382 '#title' => t('Attribute to display'),
383 '#default_value' => $edit['ou_attribute'],
384 '#size' => 50,
385 '#maxlength' => 50,
386 '#description' => t('The attribute from the LDAP record to display on forms.')
387 );
388
389 $form['server-settings']['ou_userlink'] = array(
390 '#type' => 'textfield',
391 '#title' => t('Attribute to link with users'),
392 '#default_value' => $edit['ou_userlink'],
393 '#size' => 50,
394 '#maxlength' => 50,
395 '#description' => t('The attribute from the users LDAP record to link to organizationalunits.')
396 );
397
398 $form['server-settings']['ou_label'] = array(
399 '#type' => 'textfield',
400 '#title' => t('Label to give to organizational units'),
401 '#default_value' => $edit['ou_label'],
402 '#size' => 50,
403 '#maxlength' => 50,
404 '#description' => t('The label for organizational units can be customised. Specify the name you wish to display here.')
405 );
406
407 $i = 0;
408 while ($i < 21) {
409 $options[$i]=($i-10);
410 $i++;
411 }
412 $form['server-settings']['weight'] = array(
413 '#type' => 'select',
414 '#title' => t('Weight'),
415 '#options' => $options,
416 '#default_value' => $edit['weight'],
417 '#description' => t('Select an office.'),
418 );
419
420 $form['config_name'] = array(
421 '#type' => 'hidden',
422 '#value' => $ldap_directory_name,
423 );
424
425 $form['submit'] = array(
426 '#type' => 'submit',
427 '#value' => 'Save configuration',
428 );
429 }
430 return $form;
431 }
432
433 function ldapdirectory_admin_edit_submit($form, &$form_state) {
434 $submitted_data = $form_state['values'];
435 if (!isset($submitted_data['did'])) {
436 db_query("INSERT INTO {ldapdirectory_config} (sid, ld_name, enable_directory, search_field_enabled, collapse_blocks, ou_attribute, ou_userlink, ou_dn, ou_label, ou_on, ou_filter, attributes, vcard_on, skypelink_on, skypelink_attr, maillink_on, maillink_attr, picture_on, picture_attr, picture_default, vacationcheck_on, vacationcheck_status, vacationcheck_message, weight) VALUES (%d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s', %d, '%s', %d, '%s', '%s', %d, '%s', '%s', %d)", $submitted_data['sid'], $submitted_data['ld_name'], $submitted_data['enable_directory'], $submitted_data['search_field_enabled'], $submitted_data['collapse_blocks'], $submitted_data['ou_attribute'], $submitted_data['ou_userlink'], $submitted_data['ou_dn'], $submitted_data['ou_label'], $submitted_data['ou_on'], $submitted_data['ou_filter'], $submitted_data['attributes'], $submitted_data['vcard_on'], $submitted_data['skypelink_on'], $submitted_data['skypelink_attr'], $submitted_data['maillink_on'], $submitted_data['maillink_attr'], $submitted_data['picture_on'], $submitted_data['picture_attr'], $submitted_data['picture_default'], $submitted_data['vacationcheck_on'], $submitted_data['vacationcheck_status'], $submitted_data['vacationcheck_message'], $submitted_data['weight']);
437 watchdog('ldap', 'ldapdirectory: ldap config %config created.', array('%config' => $submitted_data['ld_name']));
438 }
439 else {
440 // update the ldapdirectory configuration
441 db_query("UPDATE {ldapdirectory_config} SET ld_name = '%s', enable_directory = %d, search_field_enabled = %d, collapse_blocks = %d, ou_attribute = '%s', ou_userlink = '%s', ou_dn = '%s', ou_label = '%s', ou_on = %d, ou_filter = '%s', attributes = '%s', vcard_on = %d, skypelink_on = %d, skypelink_attr = '%s', maillink_on = %d, maillink_attr = '%s', picture_on = %d, picture_attr = '%s', picture_default = '%s', vacationcheck_on = %d, vacationcheck_status = '%s', vacationcheck_message = '%s', weight = %d WHERE did = '%s'", $submitted_data['ld_name'], $submitted_data['enable_directory'], $submitted_data['search_field_enabled'], $submitted_data['collapse_blocks'], $submitted_data['ou_attribute'], $submitted_data['ou_userlink'], $submitted_data['ou_dn'], $submitted_data['ou_label'], $submitted_data['ou_on'], $submitted_data['ou_filter'], $submitted_data['attributes'], $submitted_data['vcard_on'], $submitted_data['skypelink_on'], $submitted_data['skypelink_attr'], $submitted_data['maillink_on'], $submitted_data['maillink_attr'], $submitted_data['picture_on'], $submitted_data['picture_attr'], $submitted_data['picture_default'], $submitted_data['vacationcheck_on'], $submitted_data['vacationcheck_status'], $submitted_data['vacationcheck_message'], $submitted_data['weight'], $submitted_data['did']);
442 watchdog('ldap', 'ldapdirectory: ldap config %config updated.', array('%config' => $submitted_data['ld_name']));
443 }
444 $form_state['redirect'] = 'admin/settings/ldapdirectory';
445 }
446
447 /**
448 * Implementation of hook_block().
449 */
450 function ldapdirectory_block($op = 'list', $delta = 0, $edit = array()) {
451 $block = $form = array();
452 switch ($op) {
453 case 'list':
454 return ldapdirectory_block_list();
455
456 case 'view':
457 return ldapdirectory_block_view($delta);
458
459 case 'configure':
460 return ldapdirectory_block_configure($delta);
461
462 case 'save':
463 ldapdirectory_block_save($delta, $edit);
464 return;
465 }
466 }
467
468 /**
469 * Perform the "list" op for hook_block().
470 *
471 * @return
472 * Array of block definition.
473 */
474 function ldapdirectory_block_list() {
475 $query_result = db_query("SELECT did, ld_name FROM {ldapdirectory_config} WHERE enable_directory = %d order by weight asc", TRUE);
476 while ( $list = db_fetch_object ($query_result) ) {
477 $blocks[$list->did] = array('info' => 'Ldap Search: '. $list->ld_name, 'weight' => 0, 'enabled' => 1, 'region' => 'left');
478 }
479 return $blocks;
480 }
481 /**
482 * Perform the "view" op for hook_block().
483 *
484 * @param $delta
485 * String specifying which block to proocess.
486 *
487 * @return
488 * Array of block contents and title.
489 */
490 function ldapdirectory_block_view($delta) {
491 if (user_access('view ldap directory block')) {
492 $query_result = db_query("SELECT did, ld_name FROM {ldapdirectory_config} where did = '%s'", $delta);
493 if ($ld = db_fetch_object ($query_result)) {
494 // generate a seperate form for each LDAP instance
495 $block['subject'] = t('LDAP Search: %ld_name', array('%ld_name' => $ld->ld_name));
496 $block['content'] = drupal_get_form('ldapdirectory_renderform', $ld->did);
497 }
498 }
499 return $block;
500 }
501
502 /**
503 * Perform the "configure" op for hook_block().
504 *
505 * @param $delta
506 * String specifying which block to proocess.
507 *
508 * @return
509 * Settings form array.
510 */
511 function ldapdirectory_block_configure($delta = 0) {
512 $form['items'] = array(
513 '#type' => 'checkbox',
514 '#title' => t('Show organizationalunits'),
515 '#default_value' => variable_get('ld_ou_block_'. $delta, 0)
516 );
517 return $form;
518 }
519
520 /**
521 * Perform the "save" op for hook_block().
522 *
523 * @param $delta
524 * String specifying which block to proocess.
525 * @param $edit
526 * Array containg the form input.
527 *
528 * @return
529 * None. Values are saved as system variables.
530 */
531 function ldapdirectory_block_save($delta = 0, $edit = array()) {
532 variable_set('ld_ou_block_'. $delta, $edit['items']);
533 }
534
535 function ldapdirectory_renderform(&$form_state, $did) {
536 global $user;
537 $ldap_directory = new LDAPInterface();
538 // Get data for this LDAP instance
539 $edit2 = db_fetch_array(db_query("SELECT * FROM {ldapdirectory_config} WHERE did='%s'", $did));
540 $edit = db_fetch_array(db_query("SELECT * FROM {ldapauth} WHERE sid='%s'", $edit2['sid']));
541
542 // Give the form a label
543 $form[$edit2['did']] = array(
544 '#type' => 'fieldset',
545 '#title' => $edit['name'],
546 '#collapsible' => TRUE,
547 '#collapsed' => $edit2['collapse_blocks']
548 );
549
550 $form[$edit2['did']]['did'] = array(
551 '#type' => 'value',
552 '#value' => $did
553 );
554
555 $form[$edit2['did']]['sid'] = array(
556 '#type' => 'value',
557 '#value' => $edit2['sid']
558 );
559
560 // Check to see if we're supposed to be displaying OrganizationalUnits
561 if ($edit2['ou_on'] != 0) {
562
563 $ldap_directory->setOption('server', $edit['server']);
564 $ldap_directory->setOption('port', $edit['port']);
565 $ldap_directory->setOption('attr_filter', $edit2['ou_filter']);
566 // If there is no BINDDN and BINDPW -- the connect will be an anonymous connect
567 $ldap_directoryconnect = $ldap_directory->connect($edit['binddn'], $edit['bindpw']);
568 $entries = $ldap_directory->search($edit2['ou_dn'], $edit2['ou_filter'], array($edit2['ou_attribute']));
569 $options=array('');
570 $eoptions=array('');
571 $count = 0;
572 while ($count < $entries['count']) {
573 $eoptions[$count] = $entries[$count][$edit2['ou_attribute']][0];
574 $count++;
575 }
576 $options = array_unique($eoptions);
577 natcasesort($options);
578 $options = array_reverse($options);
579 $options[$count] = "All";
580 $options = array_reverse($options);
581 if ($edit2['ou_label'] != '') {
582 $ou_label = $edit2['ou_label'];
583 }
584 else {
585 $ou_label = t('Organizational Unit');
586 }
587 $form[$edit2['did']]['organizationalUnits'] = array(
588 '#type' => 'select',
589 '#title' => $ou_label,
590 '#attributes' => array('style' => 'width: 160px;'),
591 '#options' => $options,
592 '#description' => t('Select an office.'),
593 );
594 }
595 if ($edit2['search_field_enabled'] == '1') {
596 $form[$edit2['did']]['textsearch'] = array(
597 '#type' => 'textfield',
598 '#attributes' => array('style' => 'width: 140px;'),
599 '#title' => t('Search')
600 );
601 }
602
603 $form[$edit2['did']]['submit'] = array(
604 '#type' => 'submit',
605 '#value' => 'Go'
606 );
607 $form['#action'] = url('ldapdirectory');
608 $form['#theme'] = 'ldapdirectory_renderform';
609 $form['#submit'][] = 'ldapdirectory_renderform_submit';
610 return $form;
611 }
612
613 function ldapdirectory_renderform_submit($form, &$form_state) {
614 $form_state['redirect'] = 'ldapdirectory/search/'. $form_state['values']['did'] .'/'. $form_state['values']['organizationalUnits'] .'/'. $form_state['values']['textsearch'];
615 }
616
617
618 function ldapdirectory_display_search($did = NULL, $ou = NULL, $search = NULL) {
619 if (is_null($did)) {
620 return;
621 }
622 $page_content = '';
623 $ldap_directory = new LDAPInterface();
624 // Get a list of all available LDAP instances
625 // Get data for this LDAP instance
626 $ldsettings = db_fetch_array(db_query("SELECT * FROM {ldapdirectory_config} WHERE did = '%s'", $did));
627 $serversettings = db_fetch_array(db_query("SELECT * FROM {ldapauth} WHERE sid = '%s'", $ldsettings['sid']));
628 // Setup LDAP
629 $ldap_directory->setOption('server', $serversettings['server']);
630 $ldap_directory->setOption('port', $serversettings['port']);
631
632 // Check to see if we're supposed to be displaying OrganizationalUnits
633 if ($ldsettings['ou_on'] != 0) {
634 $ldap_directory->setOption('attr_filter', $ldsettings['ou_filter']);
635 // If there is no BINDDN and BINDPW -- the connect will be an anonymous connect
636 $ldap_directoryconnect = $ldap_directory->connect($serversettings['binddn'], $serversettings['bindpw']);
637 $entries = $ldap_directory->search($ldsettings['ou_dn'], $ldsettings['ou_filter'], array($ldsettings['ou_attribute']));
638 $options=array('');
639 $eoptions=array('');
640 $count = 0;
641 while ($count < $entries['count']) {
642 $eoptions[$count] = $entries[$count][$ldsettings['ou_attribute']][0];
643 $count++;
644 }
645 // flip the array so that the "All" option is always key '0'
646 $options = array_unique($eoptions);
647 natcasesort($options);
648 $options = array_reverse($options);
649 $options[$count] = "All";
650 $options = array_reverse($options);
651
652 // If the user has selected "All" then clear the filter, otherwise build one based on the submission.
653 if ($ou == '0') {
654 $filter = 'objectClass=Top';
655 }
656 else {
657 $filter = ($ldsettings['ou_attribute'] .'='. $options[$ou]);
658 }
659 }
660 $userattr=$serversettings['user_attr'];
661 $pictureattr=$ldsettings['picture_attr'];
662
663 $temparr = array();
664 $ldapkeys = array();
665 $ldaptags = array();
666 $ldaptagscount = 0;
667 $temparr = explode(',', $ldsettings['attributes']);
668 foreach ( $temparr as $id => $contents) {
669 list($key, $value) = split('=', $contents);
670 $ldapkeys[drupal_strtolower($key)] = $value;
671 $ldaptags[] = drupal_strtolower($key);
672 $ldaptagscount++;
673 }
674 $ldaptags [] = $userattr;
675 $ldaptags [] = $pictureattr;
676 if ($ldsettings['skypelink_on'] == 1) $ldaptags [] = $ldsettings['skypelink_attr'];
677 if ($ldsettings['vacationcheck_on'] == 1) {
678 $ldaptags [] = $ldsettings['vacationcheck_status'];
679 $ldaptags [] = $ldsettings['vacationcheck_message'];
680 }
681 // If there is no BINDDN and BINDPW -- the connect will be an anonymous connect
682 $ldap_directoryconnect = $ldap_directory->connect($serversettings['binddn'], $serversettings['bindpw']);
683 $ldapentries = $ldap_directory->search($serversettings['basedn'], $filter, $ldaptags);
684 // counter for LDAP entries
685 $entrycount = 0;
686
687 while ($entrycount < $ldapentries['count']) {
688
689 $header = array();
690 $picturerows = array();
691 $rows = array();
692
693 // counter for fields being processed
694 $count = 0;
695
696 // whether a match was returned or not - set to zero (no match) by default
697 $selected = 0;
698
699 while ($count < $ldaptagscount) {
700
701 // counter for keys within this array of fields
702 $keycount = 0;
703
704 while ($keycount < $ldapentries[$entrycount][$ldaptags[$count]]['count']) {
705
706 // if this is the first field/keyset then make it a header for the table
707 // otherwise it's just a row
708 if (($ldsettings['picture_on'] == 1) && ($count == 0)) {
709 $header = (array($ldapkeys[$ldaptags[$count]] .':', $ldapentries[$entrycount][$ldaptags[$count]][$keycount]));
710 if ($ldapentries[$entrycount][$pictureattr]['count'] > 0) {
711 $picture = ('<div class="ldapdirectory-block"><center><img src="'. url('ldapdirectory/picture/'. $sid .'/'. $ldapentries[$entrycount][$userattr][0]) .'" height="200px"></center></div>');
712 }
713 else {
714 $picture = ('<div class="ldapdirectory-block"><center><img src="'. $ldsettings['picture_default'] .'" height="200px"></center></div>');
715 }
716 $icons = array();
717 if ($ldsettings['vcard_on'] == 1) {
718 $icons[] = array(
719 'module' => 'ldapdirectory',
720 'path' => 'ldapdirectory/vcard/'. $serversettings['sid'] .'/'. $ldapentries[$entrycount][$userattr][0],
721 'icon' => drupal_get_path('module', 'ldapdirectory') .'/images/vcard.png',
722 'title' => t('Download Business Card'),
723 'access' => user_access('view ldap directory block'),
724 'description' => t('Download Business Card'),
725 );
726 }
727 if ($ldsettings['maillink_on'] == 1) {
728 $icons[] = array(
729 'module' => 'ldapdirectory',
730 'path' => 'mailto:'. $ldapentries[$entrycount][$ldsettings['maillink_attr']][0],
731 'icon' => drupal_get_path('module', 'ldapdirectory') .'/images/sendmail.png',
732 'title' => t('Send E-mail'),
733 'access' => user_access('view ldap directory block'),
734 'description' => t('Send E-mail'),
735 );
736 }
737 if (($ldsettings['skypelink_on'] == 1) && ($ldapentries[$entrycount][$ldsettings['skypelink_attr']]['count'] > 0)) {
738 $icons[] = array(
739 'module' => 'ldapdirectory',
740 'path' => 'skype://'. $ldapentries[$entrycount][$ldsettings['skypelink_attr']][0],
741 'icon' => 'http://download.skype.com/share/skypebuttons/buttons/call_blue_transparent_34x34.png',
742 'title' => t('Call via Skype'),
743 'access' => user_access('access content'),
744 'description' => t('Call via Skype'),
745 );
746 }
747 $buttons = ldapdirectory_drawicons($sid, $userid, $icons);
748 }
749 else if (($ldsettings['picture_on'] == 0) && ($count == 0)) {
750 $header = (array('', $ldapkeys[$ldaptags[$count]] .':', $ldapentries[$entrycount][$ldaptags[$count]][$keycount]));
751 }
752 else {
753 if (($ldsettings['maillink_on'] == 1) && ($ldaptags[$count] == $ldsettings['maillink_attr'])) {
754 $rows[] = (array($ldapkeys[$ldaptags[$count]] .':', '<a href="mailto:'. $ldapentries[$entrycount][$ldaptags[$count]][$keycount] .'">'. $ldapentries[$entrycount][$ldaptags[$count]][$keycount] .'</a>'));
755 }
756 else if (($ldsettings['skypelink_on'] == 1) && ($ldaptags[$count] == $ldsettings['skypelink_attr'])) {
757 $rows[] = (array($ldapkeys[$ldaptags[$count]] .':', '<a href="skype://'. $ldapentries[$entrycount][$ldaptags[$count]][$keycount] .'">'. $ldapentries[$entrycount][$ldaptags[$count]][$keycount] .'</a>'));
758 }
759 else {
760 $rows[] = (array($ldapkeys[$ldaptags[$count]] .':', $ldapentries[$entrycount][$ldaptags[$count]][$keycount]));
761 }
762 }
763
764 // are we performing a text search? If so, check the keys against the form submission
765 if ($search) {
766 if (stristr (drupal_strtolower($ldapentries[$entrycount][$ldaptags[$count]][$keycount]), drupal_strtolower($search)) !== FALSE) {
767 $selected = 1;
768 //Remove Me from release ###############################################
769 // if (stristr (drupal_strtolower($ldapentries[$entrycount][$ldaptags[$count]][$keycount]), drupal_strtolower('DC=AnswernetNetwork')) !== FALSE) {
770 // $selected = 0;
771 // }
772 }
773 }
774 else {
775 // if we're not performing a text search, flag the entry for addition to the page anyway
776 $selected = 1;
777 }
778 $keycount++;
779 }
780 $count++;
781 }
782 // if we have a match, add it to the page - otherwise carry on.
783 if ($selected == 1) {
784 $picturerow = array();
785 $picturerow[] = (array($picture, array('data' => theme('table', NULL, $rows), 'width' => '100%')));
786 if ($ldsettings['vacationcheck_on'] == 1) {
787 $statusrows = array();
788 if (drupal_strtoupper($ldapentries[$entrycount][$ldsettings['vacationcheck_status']][0]) == 'TRUE') {
789 $statusalert = nl2br($ldapentries[$entrycount][$ldsettings['vacationcheck_message']][0]);
790 $statusrows[] = array('Status:', '<b>Out Of Office</b>');
791 $statusrows[] = array(array('data' => $statusalert, 'colspan' => 2));
792 }
793 $statustable = theme('table', NULL, $statusrows);
794 }
795 $toshow = array('data' => $buttons, 'valign' => 'top');
796 $picturerow[] = array($toshow, $statustable);
797 $page_content .= theme('table', $header, $picturerow);
798 }
799 $entrycount ++;
800 };
801 return $page_content;
802 }
803
804 function ldapdirectory_main() {
805 $content = '';
806 // $data isn't defined anywhere.
807 // $content .= $data;
808
809 // generate a seperate form for each LDAP instance
810 $query_result = db_query("SELECT sid FROM {ldapdirectory_config} where enable_directory = %d order by weight asc", TRUE);
811 while ( $links = db_fetch_object($query_result) ) {
812 $content .= drupal_get_form('ldapdirectory_renderform_sid_'. $links->sid, $links->sid);
813 }
814 return ($content);
815 } // function ldapdirectory_main
816
817 function ldapdirectory_forms() {
818 $query_result = db_query("SELECT sid FROM {ldapdirectory_config} where enable_directory = %d order by weight asc", TRUE);
819 while ( $links = db_fetch_object($query_result) ) {
820 $forms['ldapdirectory_renderform_sid_'. $links->sid]['callback'] = 'ldapdirectory_renderform';
821 }
822 return $forms;
823 }
824
825 function ldapdirectory_getpicture($sid, $userid) {
826
827 // Get a list of all available LDAP instances
828 // Get data for this LDAP instance
829 $serversettings = db_fetch_array(db_query("SELECT * FROM {ldapauth} where sid='%s'", $sid));
830 $ldsettings = db_fetch_array(db_query("SELECT * FROM {ldapdirectory_config} where sid='%s'", $sid));
831 $server = $serversettings['server'];
832 $port = $serversettings['port'];
833 $filter = $serversettings['user_attr'] .'='. $userid;
834 $attr = $ldsettings['picture_attr'];
835 $pictureson = $ldsettings['picture_on'];
836
837 if ($pictureson == 1) {
838 // Setup LDAP
839 $ldap_directory = ldap_connect($server, $port);
840 ldap_bind($ldap_directory);
841
842 $result = ldap_search($ldap_directory, $serversettings['basedn'], $filter, array($attr));
843
844 if (ldap_count_entries($ldap_directory, $result)) {
845 $entries = ldap_get_entries($ldap_directory, $result);
846 $entry = ldap_first_entry($ldap_directory, $result);
847 $file = ldap_get_values_len($ldap_directory, $entry, $attr);
848 header('Content-Disposition: inline');
849 header('Content-Type: image/jpeg');
850 print $file[0];
851 }
852
853 ldap_close($ldap_directory);
854 }
855 else {
856 return ('access denied');
857 }
858 }
859
860 function ldapdirectory_getvcard($sid, $userid) {
861
862 // Get a list of all available LDAP instances
863 // Get data for this LDAP instance
864 $serversettings = db_fetch_array(db_query("SELECT * FROM {ldapauth} where sid='%s'", $sid));
865 $ldsettings = db_fetch_array(db_query("SELECT * FROM {ldapdirectory_config} where sid='%s'", $sid));
866 $server = $serversettings['server'];
867 $port = $serversettings['port'];
868 $filter = $serversettings['user_attr'] .'='. $userid;
869 $attr = $ldsettings['picture_attr'];
870 $vcardson = $ldsettings['vcard_on'];
871
872 if ($vcardson == 1) {
873 // Setup LDAP
874 $ldap_directory = ldap_connect($server, $port);
875 ldap_bind($ldap_directory);
876
877 $result = ldap_search($ldap_directory, $serversettings['basedn'], $filter);
878
879 if (ldap_count_entries($ldap_directory, $result)) {
880 $entries = ldap_get_entries($ldap_directory, $result);
881 $entry = ldap_first_entry($ldap_directory, $result);
882
883 header('Content-Disposition: attachment; filename='. $userid .'.vcf');
884 header('Content-Type: application/vcard');
885
886 // Create vCard
887 $i = 0;
888 $output .= ('BEGIN:VCARDVERSION:3.0 N:'. $entries[$i]["sn"][0] .';'. $entries[$i]["givenname"][0] .';;; FN:'.
889 $entries[$i]["cn"][0] .'ORG:'. $entries[$i]["o"][0] .'; TITLE:'. $entries[$i]["title"][0] .
890 ' EMAIL;type=INTERNET;type=WORK;type=pref:'. $entries[$i]["mail"][0] .' TEL;type=WORK;type=pref:'.
891 $entries[$i]["telephonenumber"][0] .' TEL;type=CELL:'. $entries[$i]["mobile"][0] .
892 ' TEL;type=WORK;type=FAX:'. $entries[$i]["facsimiletelephonenumber"][0] .' item1.ADR;type=WORK;type=pref:'.
893 $entries[$i]["postofficebox"][0] .';'. $entries[$i]["physicaldeliveryofficename"][0] .';'.
894 $entries[$i]["street"][0] .';'. $entries[$i]["l"][0] .';'. $entries[$i]["ou"][0] .';'.
895 $entries[$i]["postalcode"][0] .';'. $entries[$i]["c"][0] .' item1.X-ABADR: item2.X-JABBER;type=pref:'.
896 $entries[$i]["esgjabberusername"][0]);
897 if ($entries[$i]["jpegphoto"][0]) {
898 $file = ldap_get_values_len($ldap_directory, $entry, $attr);
899 $output .= ("\nPHOTO;BASE64:;". base64_encode($file[0]));
900 }
901
902 $output .= "\nEND:VCARD";
903 }
904
905 ldap_close($ldap_directory);
906
907 print $output;
908
909 }
910 else {
911 return ('access denied');
912 }
913 }
914
915 function ldapdirectory_drawicons($sid, $userid, $icons = NULL) {
916 drupal_add_css(drupal_get_path('module', 'ldapdirectory') .'/ldapdirectory.css', 'module');
917 $moreicons = module_invoke_all('ldapdirectory_seticons');
918
919 $page_content = '';
920
921 $i = 0;
922 $page_content .= ('<div class="ldapdirectory-block">');
923 $page_content .= ('<div class="ldapdirectory-box">');
924 while ($icons[$i]) {
925 if ($icons[$i]['access'] == 1) {
926 $page_content .= ('<div class="ldapdirectory-icon" title="'. $icons[$i]['description'] .'" alt="'. $icons[$i]['title'] .'"><a href="'. $icons[$i]['path'] .'"><img src="'. $icons[$i]['icon'] .'"></a></div>');
927 }
928 $i++;
929 }
930 $i = 0;
931 while ($moreicons[$i]) {
932 if ($moreicons[$i]['access'] == 1) {
933 $page_content .= ('<div class="ldapdirectory-icon" title="'. $moreicons[$i]['description'] .'" alt="'. $moreicons[$i]['title'] .'"><a href="'. $moreicons[$i]['path'] .'"><img src="'. $moreicons[$i]['icon'] .'"></a></div>');
934 }
935 $i++;
936 }
937 $page_content .= ('</div>');
938 $page_content .= ('</div>');
939
940 return $page_content;
941 }
942

  ViewVC Help
Powered by ViewVC 1.1.2