| 1 |
<?php
|
| 2 |
// $Id: ldapdata.module,v 1.30 2009/08/25 14:17:59 miglius Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* ldapdata provides data maping against ldap server.
|
| 7 |
*/
|
| 8 |
|
| 9 |
//////////////////////////////////////////////////////////////////////////////
|
| 10 |
|
| 11 |
define('LDAPDATA_SYNC', variable_get('ldapdata_sync', 2));
|
| 12 |
define('LDAPDATA_PROFILE', 'LDAP attributes');
|
| 13 |
define('LDAPDATA_PROFILE_WEIGHT', 5);
|
| 14 |
define('LDAPDATA_USER_TAB', 'LDAP entry');
|
| 15 |
define('LDAPDATA_USER_DATA', 'ldapdata_user_data');
|
| 16 |
|
| 17 |
// Changed the values to be more unix-line. 6 = rw, 4 = ro, 2 = nothing.
|
| 18 |
define('LDAPDATA_MAP_ATTRIBUTES', 6);
|
| 19 |
define('LDAPDATA_MAP_ATTRIBUTES_READ_ONLY', 4);
|
| 20 |
define('LDAPDATA_MAP_NOTHING', 2);
|
| 21 |
|
| 22 |
//////////////////////////////////////////////////////////////////////////////
|
| 23 |
// Core API hooks
|
| 24 |
|
| 25 |
/**
|
| 26 |
* Implements hook_init()
|
| 27 |
*/
|
| 28 |
function ldapdata_init() {
|
| 29 |
require_once(drupal_get_path('module', 'ldapdata') .'/includes/LDAPInterface.inc');
|
| 30 |
}
|
| 31 |
|
| 32 |
/**
|
| 33 |
* Implements hook_theme().
|
| 34 |
*/
|
| 35 |
function ldapdata_theme() {
|
| 36 |
return array(
|
| 37 |
'ldapdata_admin_edit' => array(
|
| 38 |
'arguments' => array('form' => NULL),
|
| 39 |
'file' => 'ldapdata.theme.inc'
|
| 40 |
),
|
| 41 |
'ldapdata_ldap_attribute' => array(
|
| 42 |
'arguments' => array('value' => NULL, 'type' => NULL),
|
| 43 |
'file' => 'ldapdata.theme.inc'
|
| 44 |
),
|
| 45 |
);
|
| 46 |
}
|
| 47 |
|
| 48 |
/**
|
| 49 |
* Implements hook_menu().
|
| 50 |
*/
|
| 51 |
function ldapdata_menu() {
|
| 52 |
return array(
|
| 53 |
'admin/settings/ldap/ldapdata' => array(
|
| 54 |
'title' => 'Data',
|
| 55 |
'description' => 'Configure LDAP data to Drupal profiles synchronization settings.',
|
| 56 |
'page callback' => 'drupal_get_form',
|
| 57 |
'page arguments' => array('ldapdata_admin_settings'),
|
| 58 |
'access arguments' => array('administer ldap modules'),
|
| 59 |
'file' => 'ldapdata.admin.inc',
|
| 60 |
),
|
| 61 |
'admin/settings/ldap/ldapdata/edit' => array(
|
| 62 |
'title' => 'Data',
|
| 63 |
'page callback' => 'drupal_get_form',
|
| 64 |
'page arguments' => array('ldapdata_admin_edit', 4, 5),
|
| 65 |
'type' => MENU_CALLBACK,
|
| 66 |
'access arguments' => array('administer ldap modules'),
|
| 67 |
'file' => 'ldapdata.admin.inc',
|
| 68 |
),
|
| 69 |
'admin/settings/ldap/ldapdata/edit/%/test' => array(
|
| 70 |
'title' => 'Test LDAP Server',
|
| 71 |
'page callback' => '_ldapdata_ajax_test',
|
| 72 |
'page arguments' => array(5),
|
| 73 |
'type' => MENU_CALLBACK,
|
| 74 |
'access arguments' => array('administer ldap modules'),
|
| 75 |
'file' => 'ldapdata.admin.inc',
|
| 76 |
),
|
| 77 |
'admin/settings/ldap/ldapdata/reset' => array(
|
| 78 |
'title' => 'Data',
|
| 79 |
'page callback' => 'drupal_get_form',
|
| 80 |
'page arguments' => array('ldapdata_admin_edit', 4, 5),
|
| 81 |
'type' => MENU_CALLBACK,
|
| 82 |
'access arguments' => array('administer ldap modules'),
|
| 83 |
'file' => 'ldapdata.admin.inc',
|
| 84 |
),
|
| 85 |
);
|
| 86 |
}
|
| 87 |
|
| 88 |
/**
|
| 89 |
* Implements hook_user().
|
| 90 |
*/
|
| 91 |
function ldapdata_user($op, &$edit, &$account, $category = NULL) {
|
| 92 |
switch ($op) {
|
| 93 |
case 'categories':
|
| 94 |
return _ldapdata_user_categories();
|
| 95 |
case 'form':
|
| 96 |
return _ldapdata_user_form($account, $category);
|
| 97 |
case 'load':
|
| 98 |
_ldapdata_user_load($account);
|
| 99 |
break;
|
| 100 |
case 'login':
|
| 101 |
_ldapdata_user_login($account);
|
| 102 |
break;
|
| 103 |
case 'submit':
|
| 104 |
_ldapdata_user_submit($edit, $account, $category);
|
| 105 |
break;
|
| 106 |
case 'view':
|
| 107 |
return _ldapdata_user_view($account);
|
| 108 |
}
|
| 109 |
}
|
| 110 |
|
| 111 |
//////////////////////////////////////////////////////////////////////////////
|
| 112 |
// hook_user() functions
|
| 113 |
|
| 114 |
/**
|
| 115 |
* Implements hook_user() categories operation.
|
| 116 |
*/
|
| 117 |
function _ldapdata_user_categories() {
|
| 118 |
return array(
|
| 119 |
array(
|
| 120 |
'name' => LDAPDATA_USER_DATA,
|
| 121 |
'title' => t(LDAPDATA_USER_TAB),
|
| 122 |
'weight' => 50,
|
| 123 |
'access callback' => 'ldapdata_category_access',
|
| 124 |
'access arguments' => array(1)
|
| 125 |
)
|
| 126 |
);
|
| 127 |
}
|
| 128 |
|
| 129 |
/**
|
| 130 |
* Checks if LDAP data category should be printed.
|
| 131 |
*/
|
| 132 |
function ldapdata_category_access($account) {
|
| 133 |
global $user;
|
| 134 |
|
| 135 |
if (!($user->uid > 0 && $user->uid == $account->uid || user_access('administer users')))
|
| 136 |
return FALSE;
|
| 137 |
|
| 138 |
if (!isset($account->ldap_authentified))
|
| 139 |
return FALSE;
|
| 140 |
|
| 141 |
return (_ldapdata_ldap_info($account, 'mapping_type') == LDAPDATA_MAP_ATTRIBUTES && count(_ldapdata_ldap_info($account, 'ldapdata_rwattrs')) > 0) ? TRUE : FALSE;
|
| 142 |
}
|
| 143 |
|
| 144 |
/**
|
| 145 |
* Implements hook_user() categories operation.
|
| 146 |
* Only used for editable LDAP attributes with no Drupal equivalents.
|
| 147 |
*/
|
| 148 |
function _ldapdata_user_form(&$user, $category) {
|
| 149 |
global $_ldapdata_ldap;
|
| 150 |
|
| 151 |
// Force LDAP sync.
|
| 152 |
_ldapdata_user_load($user, TRUE);
|
| 153 |
|
| 154 |
$attributes = _ldapdata_ldap_info($user, 'ldapdata_rwattrs');
|
| 155 |
|
| 156 |
if (!isset($user->ldap_dn) || $category != LDAPDATA_USER_DATA || _ldapdata_ldap_info($user, 'mapping_type') != LDAPDATA_MAP_ATTRIBUTES || !$attributes)
|
| 157 |
return;
|
| 158 |
|
| 159 |
$bind_info = _ldapdata_edition($user);
|
| 160 |
if (!$_ldapdata_ldap->connect($bind_info['dn'], $bind_info['pass'])) {
|
| 161 |
watchdog('ldapdata', "User form: user %name's data could not be read in the LDAP directory", array('%name' => $user->name), WATCHDOG_WARNING);
|
| 162 |
return;
|
| 163 |
}
|
| 164 |
|
| 165 |
$entry = $_ldapdata_ldap->retrieveAttributes($user->ldap_dn);
|
| 166 |
|
| 167 |
$form['ldap_attributes'] = array(
|
| 168 |
'#title' => t(LDAPDATA_PROFILE),
|
| 169 |
'#type' => 'fieldset',
|
| 170 |
);
|
| 171 |
|
| 172 |
foreach (_ldapdata_ldap_info($user, 'ldapdata_attrs') as $attr_name => $attr_info) {
|
| 173 |
if (in_array($attr_name, $attributes)) {
|
| 174 |
array_shift($attr_info);
|
| 175 |
$value = isset($entry[drupal_strtolower($attr_name)]) ? $entry[drupal_strtolower($attr_name)][0] : '';
|
| 176 |
$form['ldap_attributes']['ldap_'. $attr_name] = _ldapdata_attribute_form($value, $attr_info);
|
| 177 |
}
|
| 178 |
}
|
| 179 |
|
| 180 |
$_ldapdata_ldap->disconnect();
|
| 181 |
|
| 182 |
return $form;
|
| 183 |
}
|
| 184 |
|
| 185 |
/**
|
| 186 |
* Implements hook_user() load operation.
|
| 187 |
*/
|
| 188 |
function _ldapdata_user_load(&$account, $sync = FALSE) {
|
| 189 |
global $user, $_ldapdata_ldap;
|
| 190 |
|
| 191 |
// Setup the global $_ldapdata_ldap object.
|
| 192 |
if (!_ldapdata_init($account))
|
| 193 |
return;
|
| 194 |
|
| 195 |
if (!$sync && (LDAPDATA_SYNC < 1 || LDAPDATA_SYNC < 2 && $user->uid != $account->uid))
|
| 196 |
return;
|
| 197 |
|
| 198 |
static $accounts_synced = array();
|
| 199 |
if (isset($accounts_synced[$account->uid])) {
|
| 200 |
return;
|
| 201 |
}
|
| 202 |
|
| 203 |
// See http://drupal.org/node/91786 about user_node().
|
| 204 |
// User can be edited by the user or by other authorized users.
|
| 205 |
$authmap = user_get_authmaps($account->name);
|
| 206 |
if (!isset($authmap['ldapauth']) || (_ldapdata_ldap_info($account, 'mapping_type') == LDAPDATA_MAP_NOTHING)) {
|
| 207 |
return;
|
| 208 |
}
|
| 209 |
|
| 210 |
$accounts_synced[$account->uid] = TRUE;
|
| 211 |
$bind_info = _ldapdata_edition($account);
|
| 212 |
if (!$_ldapdata_ldap->connect($bind_info['dn'], $bind_info['pass'])) {
|
| 213 |
watchdog('ldapdata', "User load: user %name's data could not be read in the LDAP directory", array('%name' => $account->name), WATCHDOG_WARNING);
|
| 214 |
return;
|
| 215 |
}
|
| 216 |
|
| 217 |
if ($entry = $_ldapdata_ldap->retrieveAttributes($account->ldap_dn)) {
|
| 218 |
$ldap_drupal_reverse_mappings = _ldapdata_reverse_mappings($account->ldap_config);
|
| 219 |
|
| 220 |
// Retrieve profile fields list.
|
| 221 |
$profile_fields = _ldapdata_retrieve_profile_fields();
|
| 222 |
|
| 223 |
$drupal_fields = array();
|
| 224 |
foreach (_ldapdata_reverse_mappings($account->ldap_config) as $drupal_field => $ldap_attr) {
|
| 225 |
$value = isset($entry[strtolower($ldap_attr)]) ? $entry[strtolower($ldap_attr)][0] : '';
|
| 226 |
|
| 227 |
// Is it a profile field?
|
| 228 |
if (is_numeric($drupal_field)) {
|
| 229 |
if ($profile_field = isset($profile_fields[$drupal_field]) ? $profile_fields[$drupal_field] : NULL) {
|
| 230 |
if ($row = db_fetch_array(db_query("SELECT value FROM {profile_values} WHERE fid = '%d' AND uid = '%d'", $drupal_field, $account->uid))) {
|
| 231 |
if ($row['value'] != $value)
|
| 232 |
db_query("UPDATE {profile_values} SET value = '%s' WHERE fid = '%d' AND uid = '%d'", $value, $drupal_field, $account->uid);
|
| 233 |
}
|
| 234 |
else {
|
| 235 |
db_query("INSERT INTO {profile_values} (value, fid, uid) VALUES ('%s', '%d', '%d')", $value, $drupal_field, $account->uid);
|
| 236 |
}
|
| 237 |
$account->$drupal_field = $value;
|
| 238 |
}
|
| 239 |
}
|
| 240 |
// Then it might be a Drupal field.
|
| 241 |
else if (isset($account->$drupal_field) && !in_array($drupal_field, array('pass'))) {
|
| 242 |
$drupal_fields = array_merge($drupal_fields, array($drupal_field => $value));
|
| 243 |
}
|
| 244 |
}
|
| 245 |
if (!empty($drupal_fields))
|
| 246 |
$account = user_save($account, $drupal_fields);
|
| 247 |
}
|
| 248 |
$_ldapdata_ldap->disconnect();
|
| 249 |
}
|
| 250 |
|
| 251 |
/**
|
| 252 |
* Implements hook_user() login operation.
|
| 253 |
*/
|
| 254 |
function _ldapdata_user_login(&$user) {
|
| 255 |
global $_ldapdata_ldap;
|
| 256 |
|
| 257 |
// Force LDAP sync.
|
| 258 |
if (LDAPDATA_SYNC == 0)
|
| 259 |
_ldapdata_user_load($user, TRUE);
|
| 260 |
}
|
| 261 |
|
| 262 |
/**
|
| 263 |
* Implements hook_user() submit operation.
|
| 264 |
*/
|
| 265 |
function _ldapdata_user_submit(&$edit, &$user, $category) {
|
| 266 |
global $_ldapdata_ldap;
|
| 267 |
|
| 268 |
// Setup the global $_ldapdata_ldap object.
|
| 269 |
if (!_ldapdata_init($user))
|
| 270 |
return;
|
| 271 |
|
| 272 |
$authmap = user_get_authmaps($user->name);
|
| 273 |
if (!isset($authmap['ldapauth']))
|
| 274 |
return;
|
| 275 |
|
| 276 |
// Three cases here:
|
| 277 |
// 1. User logged on and editing his LDAP entry attributes ($category == LDAPDATA_USER_DATA).
|
| 278 |
// 2. User logged on and editing his Drupal account settings ($category == 'account').
|
| 279 |
// 3. OBSOLETE FROM 4.7: Password lost and being updated (category == 'account').
|
| 280 |
// Additionally:
|
| 281 |
// 4. User logged on and editing his profile.module fields ($category == *any*).
|
| 282 |
$writeout = array();
|
| 283 |
|
| 284 |
$editables = _ldapdata_ldap_info($user, 'ldapdata_rwattrs');
|
| 285 |
if ($category == LDAPDATA_USER_DATA && $editables) {
|
| 286 |
// Case 1:
|
| 287 |
$writeout = array_merge($writeout, _ldapdata_user_update_ldap_attributes($edit, $user));
|
| 288 |
}
|
| 289 |
else if ($category == 'account') {
|
| 290 |
// Cases 2 && 3:
|
| 291 |
$writeout = array_merge($writeout, _ldapdata_user_update_drupal_account($edit, $user));
|
| 292 |
}
|
| 293 |
|
| 294 |
// And now, case 4:
|
| 295 |
$writeout = array_merge($writeout, _ldapdata_user_update_profile($edit, $user));
|
| 296 |
if ($writeout) {
|
| 297 |
$bind_info = _ldapdata_edition($user);
|
| 298 |
if (!$_ldapdata_ldap->connect($bind_info['dn'], $bind_info['pass'])) {
|
| 299 |
watchdog('ldapdata', "User update: user %name's data could not be updated in the LDAP directory", array('%name' => $user->name), WATCHDOG_NOTICE);
|
| 300 |
return;
|
| 301 |
}
|
| 302 |
if (!($_ldapdata_ldap->writeAttributes($user->ldap_dn, $writeout))) {
|
| 303 |
drupal_set_message(t('The data was not written to LDAP.'), 'error');
|
| 304 |
}
|
| 305 |
}
|
| 306 |
$_ldapdata_ldap->disconnect();
|
| 307 |
}
|
| 308 |
|
| 309 |
/**
|
| 310 |
* Implements hook_user() view operation.
|
| 311 |
*/
|
| 312 |
function _ldapdata_user_view(&$user) {
|
| 313 |
global $_ldapdata_ldap;
|
| 314 |
|
| 315 |
// Setup the global $_ldapdata_ldap object.
|
| 316 |
if (!_ldapdata_init($user))
|
| 317 |
return;
|
| 318 |
|
| 319 |
$authmap = user_get_authmaps($user->name);
|
| 320 |
if (!isset($authmap['ldapauth']))
|
| 321 |
return;
|
| 322 |
|
| 323 |
$bind_info = _ldapdata_edition($user);
|
| 324 |
if (!$_ldapdata_ldap->connect($bind_info['dn'], $bind_info['pass'])) {
|
| 325 |
watchdog('ldapdata', "User view: user %name's data could not be read in the LDAP directory", array('%name' => $user->name), WATCHDOG_WARNING);
|
| 326 |
return;
|
| 327 |
}
|
| 328 |
|
| 329 |
$entry = $_ldapdata_ldap->retrieveAttributes($user->ldap_dn);
|
| 330 |
$allowed_attrs = _ldapdata_ldap_info($user, 'ldapdata_roattrs');
|
| 331 |
$items = array();
|
| 332 |
$i = 0;
|
| 333 |
foreach (_ldapdata_ldap_info($user, 'ldapdata_attrs') as $attr_name => $attr_info) {
|
| 334 |
if (in_array($attr_name, $allowed_attrs)) {
|
| 335 |
$item = array(
|
| 336 |
'#type' => 'user_profile_item',
|
| 337 |
'#title' => t($attr_info[2]),
|
| 338 |
'#value' => theme('ldapdata_ldap_attribute', $entry[drupal_strtolower($attr_name)][0], $attr_info[0]),
|
| 339 |
'#weight' => $i++,
|
| 340 |
);
|
| 341 |
$items[$attr_name] = $item;
|
| 342 |
}
|
| 343 |
}
|
| 344 |
if (!empty($items)) {
|
| 345 |
$user->content[t(LDAPDATA_PROFILE)] = array_merge(array(
|
| 346 |
'#type' => 'user_profile_category',
|
| 347 |
'#title' => t(LDAPDATA_PROFILE),
|
| 348 |
'#attributes' => array('class' => 'ldapdata-entry'),
|
| 349 |
'#weight' => LDAPDATA_PROFILE_WEIGHT,
|
| 350 |
), $items);
|
| 351 |
}
|
| 352 |
}
|
| 353 |
|
| 354 |
//////////////////////////////////////////////////////////////////////////////
|
| 355 |
// Auxiliary functions
|
| 356 |
|
| 357 |
/**
|
| 358 |
* Find out which LDAP attributes should be synced back to LDAP..
|
| 359 |
*
|
| 360 |
* @param $edit
|
| 361 |
* A submitted form data.
|
| 362 |
* @param $user
|
| 363 |
* A user object.
|
| 364 |
*
|
| 365 |
* @return
|
| 366 |
* An associated array of attributes to write to LDAP.
|
| 367 |
*/
|
| 368 |
function _ldapdata_user_update_ldap_attributes(&$edit, &$user) {
|
| 369 |
$writeout = array();
|
| 370 |
$editables = _ldapdata_ldap_info($user, 'ldapdata_rwattrs');
|
| 371 |
|
| 372 |
foreach ($edit as $edit_attr => $value) {
|
| 373 |
// Preventing a POST data injection: we check allowance to write value.
|
| 374 |
if (($ldap_attr = preg_replace('/^ldap_(.*)$/', '$1', $edit_attr)) && in_array($ldap_attr, $editables))
|
| 375 |
$writeout[$ldap_attr] = $value;
|
| 376 |
unset($edit[$edit_attr]);
|
| 377 |
}
|
| 378 |
|
| 379 |
return $writeout;
|
| 380 |
}
|
| 381 |
|
| 382 |
/**
|
| 383 |
* Find out which Drupal attributes should be synced back to LDAP..
|
| 384 |
*
|
| 385 |
* @param $edit
|
| 386 |
* A submitted form data.
|
| 387 |
* @param $user
|
| 388 |
* A user object.
|
| 389 |
*
|
| 390 |
* @return
|
| 391 |
* An associated array of attributes to write to LDAP.
|
| 392 |
*/
|
| 393 |
function _ldapdata_user_update_drupal_account(&$edit, &$user) {
|
| 394 |
global $_ldapdata_ldap;
|
| 395 |
|
| 396 |
$writeout = array();
|
| 397 |
if (isset($user->ldap_dn) && _ldapdata_ldap_info($user, 'mapping_type') == LDAPDATA_MAP_ATTRIBUTES) {
|
| 398 |
// Case 2: updating account data.
|
| 399 |
$d2l_map = _ldapdata_reverse_mappings($user->ldap_config);
|
| 400 |
foreach ($edit as $key => $value) {
|
| 401 |
if ($ldap_attr = isset($d2l_map[$key]) ? $d2l_map[$key] : NULL) {
|
| 402 |
if ($key == 'pass') {
|
| 403 |
if ($value) {
|
| 404 |
$pw = $_ldapdata_ldap->getOption('encrypted') ? '{md5}'. base64_encode(pack('H*', md5($value))) : $value;
|
| 405 |
$writeout[$ldap_attr] = $pw;
|
| 406 |
}
|
| 407 |
}
|
| 408 |
else {
|
| 409 |
$writeout[$ldap_attr] = $value;
|
| 410 |
}
|
| 411 |
}
|
| 412 |
}
|
| 413 |
}
|
| 414 |
return $writeout;
|
| 415 |
}
|
| 416 |
|
| 417 |
/**
|
| 418 |
* Find out which profile attributes should be synced back to LDAP.
|
| 419 |
*
|
| 420 |
* @param $edit
|
| 421 |
* A submitted form data.
|
| 422 |
* @param $user
|
| 423 |
* A user object.
|
| 424 |
*
|
| 425 |
* @return
|
| 426 |
* An associated array of attributes to write to LDAP.
|
| 427 |
*/
|
| 428 |
function _ldapdata_user_update_profile(&$edit, &$user) {
|
| 429 |
if (_ldapdata_ldap_info($user, 'mapping_type') != LDAPDATA_MAP_ATTRIBUTES)
|
| 430 |
return array();
|
| 431 |
|
| 432 |
$ldap_drupal_reverse_mappings = _ldapdata_reverse_mappings($user->ldap_config);
|
| 433 |
|
| 434 |
// Retrieve profile fields list.
|
| 435 |
$profile_fields = _ldapdata_retrieve_profile_fields();
|
| 436 |
|
| 437 |
// Compare against $edit list.
|
| 438 |
$writeout = array();
|
| 439 |
foreach ($profile_fields as $key => $field) {
|
| 440 |
if (isset($edit[$field]) && isset($ldap_drupal_reverse_mappings[$key])) {
|
| 441 |
$writeout[$ldap_drupal_reverse_mappings[$key]] = $edit[$field];
|
| 442 |
}
|
| 443 |
}
|
| 444 |
return $writeout;
|
| 445 |
}
|
| 446 |
|
| 447 |
/**
|
| 448 |
* Create HTML form element for the writtable LDAP attribute.
|
| 449 |
*
|
| 450 |
* @param $value
|
| 451 |
* A current value in LDAP.
|
| 452 |
* @param $info
|
| 453 |
* An array with the HTML from element definitions.
|
| 454 |
*
|
| 455 |
* @return
|
| 456 |
* An array of the form element.
|
| 457 |
*/
|
| 458 |
function _ldapdata_attribute_form($value, $info) {
|
| 459 |
switch (array_shift($info)) {
|
| 460 |
case 'textfield':
|
| 461 |
$form = array(
|
| 462 |
'#type' => 'textfield',
|
| 463 |
'#title' => array_shift($info),
|
| 464 |
'#default_value' => $value,
|
| 465 |
'#size' => array_shift($info),
|
| 466 |
'#maxlength' => array_shift($info),
|
| 467 |
'#description' => array_shift($info),
|
| 468 |
'#attributes' => array_shift($info),
|
| 469 |
'#required' => array_shift($info),
|
| 470 |
);
|
| 471 |
break;
|
| 472 |
case 'password':
|
| 473 |
$form = array(
|
| 474 |
'#type' => 'password',
|
| 475 |
'#title' => array_shift($info),
|
| 476 |
'#default_value' => $value,
|
| 477 |
'#size' => array_shift($info),
|
| 478 |
'#maxlength' => array_shift($info),
|
| 479 |
'#description' => array_shift($info),
|
| 480 |
);
|
| 481 |
break;
|
| 482 |
}
|
| 483 |
return $form;
|
| 484 |
}
|
| 485 |
|
| 486 |
/**
|
| 487 |
* Retrieve profile fields.
|
| 488 |
*
|
| 489 |
* @return
|
| 490 |
* An array of the form element.
|
| 491 |
*/
|
| 492 |
function _ldapdata_retrieve_profile_fields() {
|
| 493 |
$fields = array();
|
| 494 |
if (module_exists('profile')) {
|
| 495 |
$result = db_query("SELECT * FROM {profile_fields}");
|
| 496 |
while ($row = db_fetch_object($result)) {
|
| 497 |
$fields[$row->fid] = $row->name;
|
| 498 |
}
|
| 499 |
}
|
| 500 |
return $fields;
|
| 501 |
}
|
| 502 |
|
| 503 |
/**
|
| 504 |
* Retrieve drupal user fields which can be synced with LDAP.
|
| 505 |
*
|
| 506 |
* @return
|
| 507 |
* An array of user fields.
|
| 508 |
*/
|
| 509 |
function _ldapdata_retrieve_standard_user_fields() {
|
| 510 |
|
| 511 |
// pablom -
|
| 512 |
// This commented code below would return all possible values,
|
| 513 |
// but maybe that's not appropriate.
|
| 514 |
//
|
| 515 |
// $fields = array();
|
| 516 |
// $result = db_query('SHOW COLUMNS FROM {users}');
|
| 517 |
// while ($row = db_fetch_object($result)) {
|
| 518 |
// $fields[] = $row->Field;
|
| 519 |
// }
|
| 520 |
|
| 521 |
// Rather, I'll use my benevolent dictator powers
|
| 522 |
// to return only sensible ones.
|
| 523 |
$fields = array(
|
| 524 |
'mail' => 'mail',
|
| 525 |
'pass' => 'pass',
|
| 526 |
'signature' => 'signature',
|
| 527 |
);
|
| 528 |
return $fields;
|
| 529 |
}
|
| 530 |
|
| 531 |
/**
|
| 532 |
* Retrieve reverse LDAP to drupal mappings.
|
| 533 |
*
|
| 534 |
* @return
|
| 535 |
* An array of drupal keys pointing to LDAP attributes.
|
| 536 |
*/
|
| 537 |
function _ldapdata_reverse_mappings($sid) {
|
| 538 |
$map = array();
|
| 539 |
foreach (_ldapdata_ldap_info($sid, 'ldapdata_mappings') as $key => $value) {
|
| 540 |
if (($drupal_key = preg_replace('/^ldap_amap-(.*)$/', '$1', $key)) && !in_array($drupal_key, array('access', 'status')))
|
| 541 |
$map[$drupal_key] = $value;
|
| 542 |
}
|
| 543 |
return $map;
|
| 544 |
}
|
| 545 |
|
| 546 |
/**
|
| 547 |
* Retrieve LDAP write credentials.
|
| 548 |
*
|
| 549 |
* @param $sid
|
| 550 |
* A server ID or user object.
|
| 551 |
*
|
| 552 |
* @return
|
| 553 |
* An array with the LDAP write username and password.
|
| 554 |
*/
|
| 555 |
function _ldapdata_edition($sid) {
|
| 556 |
if (!($sid = is_object($sid) ? (isset($sid->ldap_config) ? $sid->ldap_config : NULL) : $sid))
|
| 557 |
return;
|
| 558 |
|
| 559 |
$row = db_fetch_object(db_query("SELECT ldapdata_binddn, ldapdata_bindpw FROM {ldapauth} WHERE sid = %d", $sid));
|
| 560 |
|
| 561 |
return array(
|
| 562 |
'dn' => $row->ldapdata_binddn ? $row->ldapdata_binddn : (isset($_SESSION['ldap_login']['dn']) ? $_SESSION['ldap_login']['dn'] : ''),
|
| 563 |
'pass' => $row->ldapdata_bindpw ? $row->ldapdata_bindpw : (isset($_SESSION['ldap_login']['pass']) ? $_SESSION['ldap_login']['pass'] : ''),
|
| 564 |
);
|
| 565 |
}
|
| 566 |
|
| 567 |
/**
|
| 568 |
* Filter LDAP attributes.
|
| 569 |
*
|
| 570 |
* @param $sid
|
| 571 |
* A LDAP server ID.
|
| 572 |
* @param $attributes
|
| 573 |
* An array of LDAP attributes.
|
| 574 |
*
|
| 575 |
* @return
|
| 576 |
* A filtered array of LDAP attributes.
|
| 577 |
*/
|
| 578 |
function _ldapdata_attribute_filter($sid, $attributes) {
|
| 579 |
if ($code = _ldapdata_ldap_info($sid, 'ldapdata_filter_php'))
|
| 580 |
$attributes = eval($code);
|
| 581 |
|
| 582 |
return $attributes;
|
| 583 |
}
|
| 584 |
|
| 585 |
/**
|
| 586 |
* Initiates the LDAPInterfase class.
|
| 587 |
*
|
| 588 |
* @param $sid
|
| 589 |
* A server ID or user object.
|
| 590 |
*
|
| 591 |
* @return
|
| 592 |
*/
|
| 593 |
function _ldapdata_init($sid) {
|
| 594 |
global $_ldapdata_ldap;
|
| 595 |
|
| 596 |
if (!($sid = is_object($sid) ? (isset($sid->ldap_config) ? $sid->ldap_config : NULL) : $sid))
|
| 597 |
return;
|
| 598 |
|
| 599 |
static $servers = array();
|
| 600 |
if (!isset($servers[$sid]))
|
| 601 |
$servers[$sid] = db_fetch_object(db_query("SELECT * FROM {ldapauth} WHERE status = 1 AND sid = %d", $sid));
|
| 602 |
|
| 603 |
if ($servers[$sid]) {
|
| 604 |
// Other modules can invoke user load from hook_init() before ldapdata.
|
| 605 |
require_once(drupal_get_path('module', 'ldapdata') .'/includes/LDAPInterface.inc');
|
| 606 |
|
| 607 |
$_ldapdata_ldap = new LDAPInterface();
|
| 608 |
$_ldapdata_ldap->setOption('sid', $sid);
|
| 609 |
$_ldapdata_ldap->setOption('name', $servers[$sid]->name);
|
| 610 |
$_ldapdata_ldap->setOption('server', $servers[$sid]->server);
|
| 611 |
$_ldapdata_ldap->setOption('port', $servers[$sid]->port);
|
| 612 |
$_ldapdata_ldap->setOption('tls', $servers[$sid]->tls);
|
| 613 |
$_ldapdata_ldap->setOption('encrypted', $servers[$sid]->encrypted);
|
| 614 |
$_ldapdata_ldap->setOption('basedn', $servers[$sid]->basedn);
|
| 615 |
$_ldapdata_ldap->setOption('user_attr', $servers[$sid]->user_attr);
|
| 616 |
$_ldapdata_ldap->setOption('attr_filter', '_ldapdata_attribute_filter');
|
| 617 |
return $_ldapdata_ldap;
|
| 618 |
}
|
| 619 |
return FALSE;
|
| 620 |
}
|
| 621 |
|
| 622 |
/**
|
| 623 |
* Retrieve the saved ldapdata saved setting.
|
| 624 |
*
|
| 625 |
* @param $sid
|
| 626 |
* A server ID or user object.
|
| 627 |
* @param $req
|
| 628 |
* An attribute name.
|
| 629 |
*
|
| 630 |
* @return
|
| 631 |
* The attribute value.
|
| 632 |
*/
|
| 633 |
function _ldapdata_ldap_info($sid, $req) {
|
| 634 |
if (!($sid = is_object($sid) ? (isset($sid->ldap_config) ? $sid->ldap_config : NULL) : $sid))
|
| 635 |
return;
|
| 636 |
|
| 637 |
static $servers = array();
|
| 638 |
if (!isset($servers[$sid]))
|
| 639 |
$servers[$sid] = db_fetch_object(db_query("SELECT * FROM {ldapauth} WHERE sid = %d", $sid));
|
| 640 |
|
| 641 |
switch ($req) {
|
| 642 |
case 'mapping_type':
|
| 643 |
$ldapdata_mappings = !empty($servers[$sid]->ldapdata_mappings) ? unserialize($servers[$sid]->ldapdata_mappings) : array();
|
| 644 |
return isset($ldapdata_mappings['access']) ? $ldapdata_mappings['access'] : LDAPDATA_MAP_NOTHING;
|
| 645 |
case 'ldapdata_mappings':
|
| 646 |
return !empty($servers[$sid]->ldapdata_mappings) ? unserialize($servers[$sid]->ldapdata_mappings) : array();
|
| 647 |
case 'ldapdata_roattrs':
|
| 648 |
return !empty($servers[$sid]->ldapdata_roattrs) ? unserialize($servers[$sid]->ldapdata_roattrs) : array();
|
| 649 |
case 'ldapdata_rwattrs':
|
| 650 |
return !empty($servers[$sid]->ldapdata_rwattrs) ? unserialize($servers[$sid]->ldapdata_rwattrs) : array();
|
| 651 |
case 'ldapdata_binddn':
|
| 652 |
return $servers[$sid]->ldapdata_binddn;
|
| 653 |
case 'ldapdata_bindpw':
|
| 654 |
return $servers[$sid]->ldapdata_bindpw;
|
| 655 |
case 'ldapdata_attrs':
|
| 656 |
return !empty($servers[$sid]->ldapdata_attrs) ? unserialize($servers[$sid]->ldapdata_attrs) : array();
|
| 657 |
case 'ldapdata_filter_php':
|
| 658 |
return $servers[$sid]->ldapdata_filter_php;
|
| 659 |
}
|
| 660 |
}
|
| 661 |
|