2022-05-09 02:27:46 -04:00
|
|
|
<?php
|
|
|
|
/**
|
2023-01-01 09:36:24 -05:00
|
|
|
* @copyright Copyright (C) 2010-2023, the Friendica project
|
2022-05-09 02:27:46 -04:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Contact;
|
|
|
|
|
|
|
|
use Friendica\Core\Logger;
|
|
|
|
use Friendica\DI;
|
|
|
|
use Friendica\Model\Item;
|
|
|
|
use Friendica\Network\HTTPClient\Client\HttpClientAccept;
|
|
|
|
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
|
|
|
|
use Friendica\Object\Image;
|
|
|
|
use Friendica\Util\DateTimeFormat;
|
|
|
|
use Friendica\Util\HTTPSignature;
|
|
|
|
use Friendica\Util\Images;
|
|
|
|
use Friendica\Util\Network;
|
|
|
|
use Friendica\Util\Proxy;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* functions for handling contact avatar caching
|
|
|
|
*/
|
|
|
|
class Avatar
|
|
|
|
{
|
2022-05-09 15:16:14 -04:00
|
|
|
const BASE_PATH = '/avatar/';
|
|
|
|
|
2022-05-09 02:27:46 -04:00
|
|
|
/**
|
|
|
|
* Returns a field array with locally cached avatar pictures
|
|
|
|
*
|
2022-05-27 01:36:07 -04:00
|
|
|
* @param array $contact Contact array
|
|
|
|
* @param string $avatar Link to avatar picture
|
|
|
|
* @param bool $force force picture update
|
2022-05-09 02:27:46 -04:00
|
|
|
* @return array
|
|
|
|
*/
|
2022-05-27 01:36:07 -04:00
|
|
|
public static function fetchAvatarContact(array $contact, string $avatar, bool $force = false): array
|
2022-05-09 02:27:46 -04:00
|
|
|
{
|
|
|
|
$fields = ['avatar' => $avatar, 'avatar-date' => DateTimeFormat::utcNow(), 'photo' => '', 'thumb' => '', 'micro' => ''];
|
|
|
|
|
|
|
|
if (!DI::config()->get('system', 'avatar_cache')) {
|
|
|
|
self::deleteCache($contact);
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2022-05-28 10:20:48 -04:00
|
|
|
if (Network::isLocalLink($avatar) || empty($avatar)) {
|
|
|
|
self::deleteCache($contact);
|
2022-05-09 02:27:46 -04:00
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2022-05-27 01:36:07 -04:00
|
|
|
if (($avatar != $contact['avatar']) || $force) {
|
2022-05-09 02:27:46 -04:00
|
|
|
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'])) {
|
|
|
|
$fields['photo'] = $contact['photo'];
|
|
|
|
$fields['thumb'] = $contact['thumb'];
|
|
|
|
$fields['micro'] = $contact['micro'];
|
|
|
|
Logger::debug('Using existing cache files', ['uri-id' => $contact['uri-id'], 'fields' => $fields]);
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]);
|
2022-05-09 02:31:09 -04:00
|
|
|
|
2022-05-09 02:27:46 -04:00
|
|
|
$img_str = $fetchResult->getBody();
|
|
|
|
if (empty($img_str)) {
|
|
|
|
Logger::debug('Avatar is invalid', ['avatar' => $avatar]);
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
$image = new Image($img_str, Images::getMimeTypeByData($img_str));
|
|
|
|
if (!$image->isValid()) {
|
|
|
|
Logger::debug('Avatar picture is invalid', ['avatar' => $avatar]);
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2022-05-28 10:20:48 -04:00
|
|
|
$filename = self::getFilename($contact['url']);
|
|
|
|
$timestamp = time();
|
|
|
|
|
2022-12-11 04:56:30 -05:00
|
|
|
$fields['blurhash'] = $image->getBlurHash();
|
|
|
|
|
2022-05-28 10:20:48 -04:00
|
|
|
$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);
|
2022-05-09 02:27:46 -04:00
|
|
|
|
|
|
|
Logger::debug('Storing new avatar cache', ['uri-id' => $contact['uri-id'], 'fields' => $fields]);
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2022-05-23 08:15:30 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-05-28 10:20:48 -04:00
|
|
|
if (Network::isLocalLink($contact['avatar']) || empty($contact['avatar'])) {
|
|
|
|
self::deleteCache($contact);
|
2022-05-23 08:15:30 -04:00
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2022-05-28 10:20:48 -04:00
|
|
|
$filename = self::getFilename($contact['url']);
|
|
|
|
$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);
|
2022-05-23 08:15:30 -04:00
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2022-06-16 12:28:38 -04:00
|
|
|
private static function getFilename(string $url): string
|
2022-05-23 08:15:30 -04:00
|
|
|
{
|
2022-12-07 22:21:23 -05:00
|
|
|
$guid = Item::guidFromUri($url);
|
2022-05-23 08:15:30 -04:00
|
|
|
|
|
|
|
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) . '-';
|
|
|
|
}
|
|
|
|
|
2022-05-28 10:20:48 -04:00
|
|
|
private static function storeAvatarCache(Image $image, string $filename, int $size, int $timestamp): string
|
2022-05-09 02:27:46 -04:00
|
|
|
{
|
|
|
|
$image->scaleDown($size);
|
|
|
|
if (is_null($image) || !$image->isValid()) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2022-06-14 23:59:26 -04:00
|
|
|
$path = $filename . $size . '.' . $image->getExt();
|
2022-05-09 02:27:46 -04:00
|
|
|
|
2022-06-14 23:59:26 -04:00
|
|
|
$basepath = self::basePath();
|
|
|
|
if (empty($basepath)) {
|
|
|
|
return '';
|
|
|
|
}
|
2022-05-09 02:27:46 -04:00
|
|
|
|
2022-06-14 23:59:26 -04:00
|
|
|
$filepath = $basepath . $path;
|
2022-05-09 02:27:46 -04:00
|
|
|
|
2022-06-14 23:59:26 -04:00
|
|
|
$dirpath = $basepath;
|
2022-05-09 02:27:46 -04:00
|
|
|
|
2022-06-14 23:59:26 -04:00
|
|
|
DI::profiler()->startRecording('file');
|
2022-06-03 01:28:10 -04:00
|
|
|
|
2022-05-09 13:36:46 -04:00
|
|
|
// Fetch the permission and group ownership of the "avatar" path and apply to all files
|
|
|
|
$dir_perm = fileperms($dirpath) & 0777;
|
|
|
|
$file_perm = fileperms($dirpath) & 0666;
|
|
|
|
$group = filegroup($dirpath);
|
|
|
|
|
|
|
|
// Check directory permissions of all parts of the path
|
|
|
|
foreach (explode('/', dirname($filename)) as $part) {
|
|
|
|
$dirpath .= $part . '/';
|
2022-05-10 02:04:37 -04:00
|
|
|
|
2022-05-09 13:36:46 -04:00
|
|
|
if (!file_exists($dirpath)) {
|
2022-10-09 17:16:36 -04:00
|
|
|
if (!@mkdir($dirpath, $dir_perm) && !file_exists($dirpath)) {
|
2022-05-09 18:36:25 -04:00
|
|
|
Logger::warning('Directory could not be created', ['directory' => $dirpath]);
|
|
|
|
}
|
2022-05-11 13:28:28 -04:00
|
|
|
} elseif ((($old_perm = fileperms($dirpath) & 0777) != $dir_perm) && !chmod($dirpath, $dir_perm)) {
|
2022-08-30 15:45:30 -04:00
|
|
|
Logger::warning('Directory permissions could not be changed', ['directory' => $dirpath, 'old' => $old_perm, 'new' => $dir_perm]);
|
2022-05-09 13:36:46 -04:00
|
|
|
}
|
|
|
|
|
2022-05-11 13:28:28 -04:00
|
|
|
if ((($old_group = filegroup($dirpath)) != $group) && !chgrp($dirpath, $group)) {
|
2022-08-30 15:45:30 -04:00
|
|
|
Logger::warning('Directory group could not be changed', ['directory' => $dirpath, 'old' => $old_group, 'new' => $group]);
|
2022-05-09 13:36:46 -04:00
|
|
|
}
|
2022-05-09 02:27:46 -04:00
|
|
|
}
|
|
|
|
|
2022-05-09 18:36:25 -04:00
|
|
|
if (!file_put_contents($filepath, $image->asString())) {
|
|
|
|
Logger::warning('File could not be created', ['file' => $filepath]);
|
|
|
|
}
|
|
|
|
|
2022-05-10 02:04:37 -04:00
|
|
|
$old_perm = fileperms($filepath) & 0666;
|
|
|
|
$old_group = filegroup($filepath);
|
|
|
|
|
|
|
|
if (($old_perm != $file_perm) && !chmod($filepath, $file_perm)) {
|
2022-08-30 15:45:30 -04:00
|
|
|
Logger::warning('File permissions could not be changed', ['file' => $filepath, 'old' => $old_perm, 'new' => $file_perm]);
|
2022-05-09 18:36:25 -04:00
|
|
|
}
|
|
|
|
|
2022-05-10 02:04:37 -04:00
|
|
|
if (($old_group != $group) && !chgrp($filepath, $group)) {
|
2022-08-30 15:45:30 -04:00
|
|
|
Logger::warning('File group could not be changed', ['file' => $filepath, 'old' => $old_group, 'new' => $group]);
|
2022-05-09 18:36:25 -04:00
|
|
|
}
|
2022-05-09 04:20:09 -04:00
|
|
|
|
2022-05-09 02:27:46 -04:00
|
|
|
DI::profiler()->stopRecording();
|
|
|
|
|
2022-05-09 04:47:02 -04:00
|
|
|
if (!file_exists($filepath)) {
|
2022-05-09 10:36:41 -04:00
|
|
|
Logger::warning('Avatar cache file could not be stored', ['file' => $filepath]);
|
2022-05-09 04:47:02 -04:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2022-06-14 23:59:26 -04:00
|
|
|
return self::baseUrl() . $path . '?ts=' . $timestamp;
|
2022-05-09 02:27:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the avatar cache file is locally stored
|
|
|
|
*
|
|
|
|
* @param string $avatar
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2022-05-09 02:57:47 -04:00
|
|
|
private static function isCacheFile(string $avatar): bool
|
2022-05-09 02:27:46 -04:00
|
|
|
{
|
|
|
|
return !empty(self::getCacheFile($avatar));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the name of locally cached avatar pictures
|
|
|
|
*
|
|
|
|
* @param string $avatar
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function getCacheFile(string $avatar): string
|
|
|
|
{
|
2022-05-28 10:20:48 -04:00
|
|
|
$parts = parse_url($avatar);
|
2022-06-14 23:59:26 -04:00
|
|
|
if (empty($parts['host']) || ($parts['host'] != parse_url(self::baseUrl(), PHP_URL_HOST))) {
|
2022-05-09 02:27:46 -04:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2022-06-14 23:59:26 -04:00
|
|
|
$avatarpath = parse_url(self::baseUrl(), PHP_URL_PATH);
|
|
|
|
$pos = strpos($parts['path'], $avatarpath);
|
2022-05-28 10:20:48 -04:00
|
|
|
if ($pos !== 0) {
|
2022-05-09 02:27:46 -04:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2022-06-14 23:59:26 -04:00
|
|
|
$filename = self::basePath() . substr($parts['path'], strlen($avatarpath));
|
2022-05-09 02:27:46 -04:00
|
|
|
|
|
|
|
DI::profiler()->startRecording('file');
|
|
|
|
$exists = file_exists($filename);
|
|
|
|
DI::profiler()->stopRecording();
|
|
|
|
|
|
|
|
if (!$exists) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete locally cached avatar pictures of a contact
|
|
|
|
*
|
|
|
|
* @param string $avatar
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function deleteCache(array $contact)
|
|
|
|
{
|
|
|
|
self::deleteCacheFile($contact['photo']);
|
|
|
|
self::deleteCacheFile($contact['thumb']);
|
|
|
|
self::deleteCacheFile($contact['micro']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a locally cached avatar picture
|
|
|
|
*
|
|
|
|
* @param string $avatar
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private static function deleteCacheFile(string $avatar)
|
|
|
|
{
|
|
|
|
$localFile = self::getCacheFile($avatar);
|
|
|
|
if (!empty($localFile)) {
|
2022-07-17 02:34:37 -04:00
|
|
|
@unlink($localFile);
|
2022-05-09 02:27:46 -04:00
|
|
|
Logger::debug('Unlink avatar', ['avatar' => $avatar]);
|
|
|
|
}
|
|
|
|
}
|
2022-06-14 23:59:26 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2022-05-09 02:27:46 -04:00
|
|
|
}
|