Esempio n. 1
0
function avatar_get_from_db($user, $size = 0)
{
    global $db, $globals;
    if (!in_array($size, $globals['avatars_allowed_sizes'])) {
        return false;
    }
    $time = $db->get_var("select user_avatar from users where user_id={$user}");
    if (!$time > 0) {
        return false;
    }
    if (!$globals['Amazon_S3_local_cache'] && $globals['Amazon_S3_media_bucket'] && is_writable('/tmp')) {
        $subdir = '/tmp';
    } else {
        if (!Upload::create_cache_dir($user)) {
            return false;
        }
        $subdir = Upload::get_cache_dir($user);
    }
    if (!is_writable($subdir)) {
        return false;
    }
    $file_base = $subdir . "/{$user}-{$time}";
    $delete = false;
    $original = false;
    $http_code = 0;
    if ($globals['Amazon_S3_media_bucket']) {
        $original == false;
        if (Media::get("{$user}-{$time}-{$size}.jpg", 'avatars', "{$file_base}-{$size}.jpg")) {
            return file_get_contents("{$file_base}-{$size}.jpg");
        }
        if (Media::get("{$user}-{$time}.jpg", 'avatars', "{$file_base}-orig.jpg")) {
            $delete_it = true;
            $original = "{$file_base}-orig.jpg";
        } else {
            $http_code = Media::$lastHTTPCode;
            if (is_readable($file_base . '-80.jpg') && filesize($file_base . '-80.jpg') > 0 || Media::get("{$user}-{$time}-80.jpg", 'avatars', "{$file_base}-80.jpg")) {
                $original = $file_base . '-80.jpg';
            }
        }
        if ($globals['Amazon_S3_delete_allowed'] && !$original && $http_code == 404 && Media::$lastHTTPCode == 404) {
            // The images were not found in S3
            if (is_writable(mnmpath . '/' . $globals['cache_dir'])) {
                // Double check
                syslog(LOG_INFO, "Meneame, removing avatars not found in S3 user {$user} time {$time}");
                avatars_remove($user);
            }
            return false;
        }
    } else {
        //Get from DB
        if (!is_readable($file_base . '-80.jpg')) {
            $img = $db->get_var("select avatar_image from avatars where avatar_id={$user}");
            if (!strlen($img) > 0) {
                if (is_writable(mnmpath . '/' . $globals['cache_dir'])) {
                    // Double check
                    avatars_remove($user);
                }
                return false;
            }
            file_put_contents($file_base . '-80.jpg', $img);
            $original = $file_base . '-80.jpg';
        }
    }
    if ($original && $size > 0 && $size != 80) {
        avatar_resize($original, "{$file_base}-{$size}.jpg", $size);
        if ($delete_it) {
            @unlink($original);
        }
    }
    return @file_get_contents("{$file_base}-{$size}.jpg");
}
Esempio n. 2
0
function avatar_get_from_db($user, $size = 0)
{
    global $db, $globals;
    if (!in_array($size, $globals['avatars_allowed_sizes'])) {
        return false;
    }
    $time = $db->get_var("select user_avatar from users where user_id={$user}");
    if (!$globals['Amazon_S3_local_cache'] && $globals['Amazon_S3_media_bucket'] && is_writable('/tmp')) {
        $subdir = '/tmp';
    } else {
        $chain = get_cache_dir_chain($user);
        create_cache_dir_chain(get_avatars_dir(), $chain);
        $subdir = get_avatars_dir() . '/' . $chain;
    }
    if (!is_writable($subdir)) {
        return false;
    }
    $file_base = $subdir . "/{$user}-{$time}";
    $delete = false;
    $original = false;
    if ($globals['Amazon_S3_media_bucket']) {
        // Get avatar from S3
        // Try up to 3 times to download from Amazon
        $try = 0;
        while ($original == false && $try < 3) {
            if (Media::get("{$user}-{$time}-{$size}.jpg", 'avatars', "{$file_base}-{$size}.jpg")) {
                return file_get_contents("{$file_base}-{$size}.jpg");
            }
            if (Media::get("{$user}-{$time}.jpg", 'avatars', "{$file_base}-orig.jpg")) {
                $delete_it = true;
                $original = "{$file_base}-orig.jpg";
            } elseif (is_readable($file_base . '-80.jpg') && filesize($file_base . '-80.jpg') > 0 || Media::get("{$user}-{$time}-80.jpg", 'avatars', "{$file_base}-80.jpg")) {
                $original = $file_base . '-80.jpg';
            } else {
                $try++;
                usleep(rand(1, 20));
                // Wait a little to minimize race-conditions
            }
        }
        if (!$original) {
            // The images were not found in S3
            if (($buckets = Media::buckets(false)) && in_array($globals['Amazon_S3_media_bucket'], $buckets) && is_writable(mnmpath . '/' . $globals['cache_dir'])) {
                // Double check
                avatars_remove($user);
            }
            return false;
        }
    } else {
        //Get from DB
        if (!is_readable($file_base . '-80.jpg')) {
            $img = $db->get_var("select avatar_image from avatars where avatar_id={$user}");
            if (!strlen($img) > 0) {
                if (is_writable(mnmpath . '/' . $globals['cache_dir'])) {
                    // Double check
                    avatars_remove($user);
                }
                return false;
            }
            file_put_contents($file_base . '-80.jpg', $img);
            $original = $file_base . '-80.jpg';
        }
    }
    if ($size > 0 && $size != 80) {
        avatar_resize($original, "{$file_base}-{$size}.jpg", $size);
        if ($delete_it) {
            @unlink($original);
        }
    }
    return file_get_contents("{$file_base}-{$size}.jpg");
}
Esempio n. 3
0
function avatar_get_from_db($user, $size = 0)
{
    global $db, $globals;
    $img = $db->get_var("select avatar_image from avatars where avatar_id={$user}");
    if (!strlen($img) > 0) {
        return false;
    }
    $subdir = get_avatars_dir() . '/' . intval($user / $globals['avatars_files_per_dir']);
    $file_base = $subdir . '/' . $user;
    @mkdir(get_avatars_dir());
    @mkdir($subdir);
    if (!is_writable($subdir)) {
        return false;
    }
    file_put_contents($file_base . '-80.jpg', $img);
    if ($size > 0 && $size != 80 && in_array($size, $globals['avatars_allowed_sizes'])) {
        avatar_resize("{$file_base}-80.jpg", "{$file_base}-{$size}.jpg", $size);
        return file_get_contents("{$file_base}-{$size}.jpg");
    }
    return $img;
}