X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FContact%2FAvatar.php;h=fc4b7e38cba62279c37d05ca04877e5fa71f5163;hb=15eb8407928c24a96c17528304e23b0558c37bba;hp=9d06995ec5ab779689208ba02e8817bcdeedff5d;hpb=22da88b43f770c69d78d059165b333459e7f88d7;p=friendica.git diff --git a/src/Contact/Avatar.php b/src/Contact/Avatar.php index 9d06995ec5..fc4b7e38cb 100644 --- a/src/Contact/Avatar.php +++ b/src/Contact/Avatar.php @@ -1,6 +1,6 @@ $avatar, 'avatar-date' => DateTimeFormat::utcNow(), 'photo' => '', 'thumb' => '', 'micro' => '']; @@ -57,11 +57,12 @@ class Avatar return $fields; } - if (Network::isLocalLink($avatar)) { + if (Network::isLocalLink($avatar) || empty($avatar)) { + self::deleteCache($contact); return $fields; } - if ($avatar != $contact['avatar']) { + if (($avatar != $contact['avatar']) || $force) { self::deleteCache($contact); Logger::debug('Avatar file name changed', ['new' => $avatar, 'old' => $contact['avatar']]); } elseif (self::isCacheFile($contact['photo']) && self::isCacheFile($contact['thumb']) && self::isCacheFile($contact['micro'])) { @@ -72,12 +73,12 @@ class Avatar return $fields; } - $guid = Item::guidFromUri($contact['url'], parse_url($contact['url'], PHP_URL_HOST)); - - $filename = substr($guid, 0, 2) . '/' . substr($guid, 3, 2) . '/' . substr($guid, 5, 3) . '/' . - substr($guid, 9, 2) .'/' . substr($guid, 11, 2) . '/' . substr($guid, 13, 4). '/' . substr($guid, 18) . '-'; - - $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]); + try { + $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]); + } catch (\Exception $exception) { + Logger::notice('Avatar is invalid', ['avatar' => $avatar, 'exception' => $exception]); + return $fields; + } $img_str = $fetchResult->getBody(); if (empty($img_str)) { @@ -91,27 +92,69 @@ class Avatar return $fields; } - $fields['photo'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_SMALL); - $fields['thumb'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_THUMB); - $fields['micro'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_MICRO); + $filename = self::getFilename($contact['url'], $avatar); + $timestamp = time(); + + $fields['blurhash'] = $image->getBlurHash(); + + $fields['photo'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_SMALL, $timestamp); + $fields['thumb'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_THUMB, $timestamp); + $fields['micro'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_MICRO, $timestamp); Logger::debug('Storing new avatar cache', ['uri-id' => $contact['uri-id'], 'fields' => $fields]); return $fields; } - private static function storeAvatarCache(Image $image, string $filename, int $size): string + public static function storeAvatarByImage(array $contact, Image $image): array + { + $fields = ['photo' => '', 'thumb' => '', 'micro' => '']; + + if (!DI::config()->get('system', 'avatar_cache')) { + self::deleteCache($contact); + return $fields; + } + + if (Network::isLocalLink($contact['avatar']) || empty($contact['avatar'])) { + self::deleteCache($contact); + return $fields; + } + + $filename = self::getFilename($contact['url'], $contact['avatar']); + $timestamp = time(); + + $fields['photo'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_SMALL, $timestamp); + $fields['thumb'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_THUMB, $timestamp); + $fields['micro'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_MICRO, $timestamp); + + return $fields; + } + + private static function getFilename(string $url, string $host): string + { + $guid = Item::guidFromUri($url, $host); + + return substr($guid, 0, 2) . '/' . substr($guid, 3, 2) . '/' . substr($guid, 5, 3) . '/' . + substr($guid, 9, 2) .'/' . substr($guid, 11, 2) . '/' . substr($guid, 13, 4). '/' . substr($guid, 18) . '-'; + } + + private static function storeAvatarCache(Image $image, string $filename, int $size, int $timestamp): string { $image->scaleDown($size); if (is_null($image) || !$image->isValid()) { return ''; } - $path = self::BASE_PATH . $filename . $size . '.' . $image->getExt(); + $path = $filename . $size . '.' . $image->getExt(); + + $basepath = self::basePath(); + if (empty($basepath)) { + return ''; + } - $filepath = DI::basePath() . $path; + $filepath = $basepath . $path; - $dirpath = DI::basePath() . self::BASE_PATH; + $dirpath = $basepath; DI::profiler()->startRecording('file'); @@ -123,20 +166,17 @@ class Avatar // Check directory permissions of all parts of the path foreach (explode('/', dirname($filename)) as $part) { $dirpath .= $part . '/'; + if (!file_exists($dirpath)) { - if (!mkdir($dirpath, $dir_perm)) { + if (!@mkdir($dirpath, $dir_perm) && !file_exists($dirpath)) { Logger::warning('Directory could not be created', ['directory' => $dirpath]); } - } elseif (fileperms($dirpath) & 0777 != $dir_perm) { - if (!chmod($dirpath, $dir_perm)) { - Logger::info('Directory permissions could not be changed', ['directory' => $dirpath]); - } + } elseif ((($old_perm = fileperms($dirpath) & 0777) != $dir_perm) && !chmod($dirpath, $dir_perm)) { + Logger::warning('Directory permissions could not be changed', ['directory' => $dirpath, 'old' => $old_perm, 'new' => $dir_perm]); } - if (filegroup($dirpath) != $group) { - if (!chgrp($dirpath, $group)) { - Logger::info('Directory group could not be changed', ['directory' => $dirpath]); - } + if ((($old_group = filegroup($dirpath)) != $group) && !chgrp($dirpath, $group)) { + Logger::warning('Directory group could not be changed', ['directory' => $dirpath, 'old' => $old_group, 'new' => $group]); } } @@ -144,12 +184,15 @@ class Avatar Logger::warning('File could not be created', ['file' => $filepath]); } - if (!chmod($filepath, $file_perm)) { - Logger::warning('File permissions could not be changed', ['file' => $filepath]); + $old_perm = fileperms($filepath) & 0666; + $old_group = filegroup($filepath); + + if (($old_perm != $file_perm) && !chmod($filepath, $file_perm)) { + Logger::warning('File permissions could not be changed', ['file' => $filepath, 'old' => $old_perm, 'new' => $file_perm]); } - if (!chgrp($filepath, $group)) { - Logger::warning('File group could not be changed', ['file' => $filepath]); + if (($old_group != $group) && !chgrp($filepath, $group)) { + Logger::warning('File group could not be changed', ['file' => $filepath, 'old' => $old_group, 'new' => $group]); } DI::profiler()->stopRecording(); @@ -159,7 +202,7 @@ class Avatar return ''; } - return DI::baseUrl() . $path; + return self::baseUrl() . $path . '?ts=' . $timestamp; } /** @@ -181,17 +224,18 @@ class Avatar */ private static function getCacheFile(string $avatar): string { - if (empty($avatar) || !Network::isLocalLink($avatar)) { + $parts = parse_url($avatar); + if (empty($parts['host']) || ($parts['host'] != parse_url(self::baseUrl(), PHP_URL_HOST))) { return ''; } - $path = Strings::normaliseLink(DI::baseUrl() . self::BASE_PATH); - - if (Network::getUrlMatch($path, $avatar) != $path) { + $avatarpath = parse_url(self::baseUrl(), PHP_URL_PATH); + $pos = strpos($parts['path'], $avatarpath); + if ($pos !== 0) { return ''; } - $filename = str_replace($path, DI::basePath(). self::BASE_PATH, Strings::normaliseLink($avatar)); + $filename = self::basePath() . substr($parts['path'], strlen($avatarpath)); DI::profiler()->startRecording('file'); $exists = file_exists($filename); @@ -207,13 +251,16 @@ class Avatar * Delete locally cached avatar pictures of a contact * * @param string $avatar - * @return void + * @return bool */ - public static function deleteCache(array $contact) + public static function deleteCache(array $contact): bool { + $existed = (self::isCacheFile($contact['photo']) || self::isCacheFile($contact['thumb']) || self::isCacheFile($contact['micro'])); self::deleteCacheFile($contact['photo']); self::deleteCacheFile($contact['thumb']); self::deleteCacheFile($contact['micro']); + + return $existed; } /** @@ -226,8 +273,51 @@ class Avatar { $localFile = self::getCacheFile($avatar); if (!empty($localFile)) { - unlink($localFile); + @unlink($localFile); Logger::debug('Unlink avatar', ['avatar' => $avatar]); } } + + /** + * Fetch the avatar base path + * + * @return string + */ + private static function basePath(): string + { + $basepath = DI::config()->get('system', 'avatar_cache_path'); + if (empty($basepath)) { + $basepath = DI::basePath() . self::BASE_PATH; + } + $basepath = rtrim($basepath, '/') . '/'; + + if (!file_exists($basepath)) { + // We only automatically create the folder when it is in the web root + if (strpos($basepath, DI::basePath()) !== 0) { + Logger::warning('Base directory does not exist', ['directory' => $basepath]); + return ''; + } + if (!mkdir($basepath, 0775)) { + Logger::warning('Base directory could not be created', ['directory' => $basepath]); + return ''; + } + } + + return $basepath; + } + + /** + * Fetch the avatar base url + * + * @return string + */ + private static function baseUrl(): string + { + $baseurl = DI::config()->get('system', 'avatar_cache_url'); + if (!empty($baseurl)) { + return rtrim($baseurl, '/') . '/'; + } + + return DI::baseUrl() . self::BASE_PATH; + } }