You are here

function simple_ldap_user_base_field_to_drupal in Simple LDAP 7.2

1 call to simple_ldap_user_base_field_to_drupal()
simple_ldap_user_generate_edit_ldap_to_drupal in simple_ldap_user/simple_ldap_user.module

File

simple_ldap_user/simple_ldap_user.module, line 887
Main simple_ldap_user module file.

Code

function simple_ldap_user_base_field_to_drupal(&$edit, $drupal_user, $ldap_values, $drupal_field_name) {
  @($ldap_value = $ldap_values[0]);
  switch ($drupal_field_name) {
    case 'access':
    case 'login':
    case 'created':

      // Never overwrite timestamps with NULL from a missing attribute
      if ($ldap_value === NULL) {
        if (!empty($drupal_user->{$drupal_field_name})) {
          $edit['#ignored'][] = $drupal_field_name;
        }
        break;
      }

      // translate timestamp
      $tz = date_default_timezone_get();
      $timestamp = strtotime($ldap_value);

      // Bad data?  Report, but don't copy.
      if ($timestamp === FALSE) {
        watchdog('SimpleLDAP', 'Invalid data trying to map LDAP to @drupal_field for user UID @user.', array(
          '@drupal_field' => $drupal_field_name,
          '@user' => $drupal_user->uid,
        ), WATCHDOG_WARNING);
        break;
      }
      if (!property_exists($drupal_user, $drupal_field_name)) {
        $edit[$drupal_field_name] = $timestamp;
      }
      else {
        if ($drupal_user->{$drupal_field_name} > $timestamp) {
          $edit['#ignored'][] = $drupal_field_name;
        }
        $edit[$drupal_field_name] = max($timestamp, $drupal_user->{$drupal_field_name});
      }
      date_default_timezone_set($tz);
      break;
    case 'picture':
      @($drupal_image = $drupal_user->{$drupal_field_name});

      // Skip if both are empty
      if (empty($drupal_image) && empty($ldap_value)) {
        break;
      }

      // Skip if both are identical
      if (!empty($drupal_image) && $drupal_image->filesize == strlen($ldap_value) && md5($ldap_value) == md5(file_get_contents($drupal_image->uri))) {
        break;
      }

      // Otherwise, create a FID
      $name_field = simple_ldap_user_variable_get('simple_ldap_user_attribute_name');
      $filename = file_default_scheme() . '://' . variable_get('user_picture_path', 'pictures') . '/' . preg_replace('/\\W+/', '_', $drupal_user->name) . '.jpg';
      $file_obj = file_save_data($ldap_value, $filename, FILE_EXISTS_RENAME);
      $edit[$drupal_field_name] = $file_obj;
      break;
    case 'status':
      $status = empty($ldap_value) || $ldap_value == 'FALSE' ? 0 : 1;
      if (!property_exists($drupal_user, 'status') || $drupal_user->status != $status) {
        $edit['status'] = $status;
      }
      break;

    // All plain string fields fall to here.
    default:
      if (isset($ldap_value) && (!property_exists($drupal_user, $drupal_field_name) || $drupal_user->{$drupal_field_name} != $ldap_value)) {
        $edit[$drupal_field_name] = $ldap_value;
      }
  }
}