You are here

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

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

*

Parameters

ldap entry array $ldap_entry: * * @return drupal file object image user's thumbnail or FALSE if none present or ERROR happens.

File

ldap_servers/LdapServer.class.php, line 1019
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.
    $thumb = isset($ldap_entry[$this->picture_attr][0]) ? $ldap_entry[$this->picture_attr][0] : FALSE;
    if (!$thumb) {
      return false;
    }

    //Create md5 check.
    $md5thumb = md5($thumb);

    /**
     * If existing account already has picture check if it has changed if so remove old file and create the new one
     * If picture is not set but account has md5 something is wrong 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
          return $account->picture;
        }
        else {

          //Image is different check wether is obj/str and remove fileobject
          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_server', "Some error happened during thumbnailPhoto sync");
        return false;
      }
    }

    //Create tmp file to get image format.
    $filename = uniqid();
    $fileuri = file_directory_temp() . '/' . $filename;
    $size = file_put_contents($fileuri, $thumb);
    $info = image_get_info($fileuri);
    unlink($fileuri);

    // create file object

    //@todo needs to change to reflect new approach to user picture: http://drupal.org/node/1851200
    $file = file_save_data($thumb, 'public://' . variable_get('user_picture_path') . '/' . $filename . '.' . $info['extension']);
    $file->md5Sum = $md5thumb;

    // standard Drupal validators for user pictures

    //@todo needs to change to reflect new approach to user picture: http://drupal.org/node/1851200
    $validators = array(
      'file_validate_is_image' => array(),
      'file_validate_image_resolution' => array(
        variable_get('user_picture_dimensions', '85x85'),
      ),
      'file_validate_size' => array(
        variable_get('user_picture_file_size', '30') * 1024,
      ),
    );
    $errors = file_validate($file, $validators);
    if (empty($errors)) {
      return $file;
    }
    else {
      foreach ($errors as $err => $err_val) {
        watchdog('ldap_server', "Error storing picture: %{$err}", "%{$err_val}", WATCHDOG_ERROR);
      }
      return FALSE;
    }
  }
}