/**
 * Update the user profile icon based on profile_sync data
 *
 * @param string $event  the name of the event
 * @param string $type   the type of the event
 * @param mixed  $object supplied object
 *
 * @return void
 */
function theme_haarlem_intranet_profile_sync_profile_icon($event, $type, $object)
{
    if (empty($object) || !is_array($object)) {
        return;
    }
    $user = elgg_extract('entity', $object);
    if (empty($user) || !elgg_instanceof($user, 'user')) {
        return;
    }
    // handle icons
    $datasource = elgg_extract('datasource', $object);
    $source_row = elgg_extract('source_row', $object);
    if (empty($datasource) || empty($source_row)) {
        return;
    }
    // handle custom icon
    $fh = new ElggFile();
    $fh->owner_guid = $user->getGUID();
    $icon_sizes = elgg_get_config('icon_sizes');
    $icon_path = elgg_extract('profielfoto', $source_row);
    $icon_path = profile_sync_filter_var($icon_path);
    if (empty($icon_path)) {
        // remove icon
        foreach ($icon_sizes as $size => $info) {
            $fh->setFilename("haarlem_icon/{$size}.jpg");
            if ($fh->exists()) {
                $fh->delete();
            }
        }
        unset($user->haarlem_icontime);
        return;
    }
    $csv_location = $datasource->csv_location;
    if (empty($csv_location)) {
        return;
    }
    $csv_filename = basename($csv_location);
    $base_location = rtrim(str_ireplace($csv_filename, "", $csv_location), DIRECTORY_SEPARATOR);
    $icon_path = sanitise_filepath($icon_path, false);
    // prevent abuse (like ../../......)
    $icon_path = ltrim($icon_path, DIRECTORY_SEPARATOR);
    // remove beginning /
    $icon_path = $base_location . DIRECTORY_SEPARATOR . $icon_path;
    // concat base location and rel path
    // icon exists
    if (!file_exists($icon_path)) {
        return;
    }
    // was csv image updated
    $csv_iconsize = @filesize($icon_path);
    if ($csv_iconsize !== false) {
        $csv_iconsize = md5($csv_iconsize);
        $icontime = $user->haarlem_icontime;
        if ($csv_iconsize === $icontime) {
            // icons are the same
            return;
        }
    }
    // try to get the user icon
    $icon_contents = file_get_contents($icon_path);
    if (empty($icon_contents)) {
        return;
    }
    // make sure we have a hash to save
    if ($csv_iconsize === false) {
        $csv_iconsize = strlen($icon_contents);
        $csv_iconsize = md5($csv_iconsize);
    }
    // write icon to a temp location for further handling
    $tmp_icon = tempnam(sys_get_temp_dir(), $user->getGUID());
    file_put_contents($tmp_icon, $icon_contents);
    // resize icon
    $icon_updated = false;
    foreach ($icon_sizes as $size => $icon_info) {
        $icon_contents = get_resized_image_from_existing_file($tmp_icon, $icon_info["w"], $icon_info["h"], $icon_info["square"], 0, 0, 0, 0, $icon_info["upscale"]);
        if (empty($icon_contents)) {
            continue;
        }
        $fh->setFilename("haarlem_icon/{$size}.jpg");
        $fh->open("write");
        $fh->write($icon_contents);
        $fh->close();
        $icon_updated = true;
    }
    // did we have a successfull icon upload?
    if ($icon_updated) {
        $user->haarlem_icontime = $csv_iconsize;
    }
    // cleanup
    unlink($tmp_icon);
}
Example #2
0
/**
 * Find a user based on a profile field and it's value
 *
 * @param stirng     $profile_field profile field name
 * @param string     $field_value   profile field value
 * @param ElggObject $sync_config   sync configuration (for logging)
 * @param array      $log_counters  array with logging counters
 *
 * @return false|ElggUser
 */
function profile_sync_find_user($profile_field, $field_value, ElggObject $sync_config, &$log_counters)
{
    static $profile_fields;
    static $dbprefix;
    if (!isset($profile_fields)) {
        $profile_fields = elgg_get_config('profile_fields');
    }
    if (!isset($dbprefix)) {
        $dbprefix = elgg_get_config('dbprefix');
    }
    if (!elgg_instanceof($sync_config, 'object', 'profile_sync_config')) {
        return false;
    }
    if (empty($log_counters) || !is_array($log_counters)) {
        return false;
    }
    if (!in_array($profile_field, ['name', 'username', 'email']) && !array_key_exists($profile_field, $profile_fields)) {
        return false;
    }
    $field_value = profile_sync_filter_var($field_value);
    if (empty($field_value)) {
        return false;
    }
    $user = false;
    switch ($profile_field) {
        case 'username':
            $user = get_user_by_username($field_value);
            break;
        case 'email':
            $users = get_user_by_email($field_value);
            if (count($users) > 1) {
                $log_counters['duplicate email']++;
                profile_sync_log($sync_config->getGUID(), "Duplicate email address: {$field_value}");
            } elseif (count($users) === 1) {
                $user = $users[0];
            }
            break;
        case 'name':
            $options = ['type' => 'user', 'limit' => false, 'joins' => ["JOIN {$dbprefix}users_entity ue ON e.guid = ue.guid"], 'wheres' => ['ue.name LIKE "' . sanitise_string($field_value) . '"']];
            $users = elgg_get_entities($options);
            if (count($users) > 1) {
                $log_counters['duplicate name']++;
                profile_sync_log($sync_config->getGUID(), "Duplicate name: {$field_value}");
            } elseif (count($users) == 1) {
                $user = $users[0];
            }
            break;
        default:
            $options = ['type' => 'user', 'limit' => false, 'metadata_name_value_pairs' => ['name' => $profile_field, 'value' => $field_value]];
            $users = elgg_get_entities_from_metadata($options);
            if (count($users) > 1) {
                $log_counters['duplicate profile field']++;
                profile_sync_log($sync_config->getGUID(), "Duplicate profile field: {$profile_field} => {$field_value}");
            } elseif (count($users) === 1) {
                $user = $users[0];
            }
            break;
    }
    return $user;
}