You are here

public function LdapServer::userPictureFromLdapEntry in Lightweight Directory Access Protocol (LDAP) 7.2

Same name and namespace in other branches
  1. 8.2 ldap_servers/LdapServer.class.php \LdapServer::userPictureFromLdapEntry()

Parameters

array $ldap_entry:

Return value

object|bool Drupal file object image user's thumbnail or FALSE if none present or ERROR happens.

File

ldap_servers/LdapServer.class.php, line 1202
Defines server classes and related functions.

Class

LdapServer
LDAP Server Class.

Code

public function userPictureFromLdapEntry($ldap_entry, $drupal_username = FALSE) {
  if ($ldap_entry && $this->picture_attr) {

    // Check if ldap entry has been provisioned.
    $image_data = isset($ldap_entry[$this->picture_attr][0]) ? $ldap_entry[$this->picture_attr][0] : FALSE;
    if (!$image_data) {
      return FALSE;
    }
    $md5thumb = md5($image_data);

    /**
     * If the existing account already has picture check if it has changed. If
     * so remove the old file and create the new one. If a picture is not set
     * but the account has an md5 hash, something is wrong and we exit.
     */
    if ($drupal_username && ($account = user_load_by_name($drupal_username))) {
      if ($account->uid == 0 || $account->uid == 1) {
        return FALSE;
      }
      if (isset($account->picture)) {

        // Check if image has changed.
        if (isset($account->data['ldap_user']['init']['thumb5md']) && $md5thumb === $account->data['ldap_user']['init']['thumb5md']) {

          // No change, return same image.
          $account->picture->md5Sum = $md5thumb;
          return $account->picture;
        }
        else {

          // Image is different, remove file object.
          if (is_object($account->picture)) {
            file_delete($account->picture, TRUE);
          }
          elseif (is_string($account->picture)) {
            $file = file_load(intval($account->picture));
            file_delete($file, TRUE);
          }
        }
      }
      elseif (isset($account->data['ldap_user']['init']['thumb5md'])) {
        watchdog('ldap_servers', "Some error happened during thumbnailPhoto sync.");
        return FALSE;
      }
    }
    return $this
      ->savePictureData($image_data, $md5thumb);
  }
  return FALSE;
}