|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace OC\Avatar; |
| 11 | + |
| 12 | +use OCP\Federation\ICloudId; |
| 13 | +use OCP\Federation\ICloudIdManager; |
| 14 | +use OCP\Files\SimpleFS\ISimpleFile; |
| 15 | +use OCP\Files\SimpleFS\ISimpleFolder; |
| 16 | +use OCP\Http\Client\IClientService; |
| 17 | +use OCP\IConfig; |
| 18 | +use Psr\Log\LoggerInterface; |
| 19 | + |
| 20 | +class RemoteAvatar extends Avatar { |
| 21 | + private const IMAGE_CACHE_AGE = 60 * 60 * 24; // One day |
| 22 | + |
| 23 | + private ICloudId $cloudId; |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + protected readonly ISimpleFolder $folder, |
| 27 | + protected readonly string $userId, |
| 28 | + protected IConfig $config, |
| 29 | + protected LoggerInterface $logger, |
| 30 | + ) { |
| 31 | + parent::__construct($config, $logger); |
| 32 | + |
| 33 | + $cloudIdManager = \OCP\Server::get(ICloudIdManager::class); |
| 34 | + $this->cloudId = $cloudIdManager->resolveCloudId($userId); |
| 35 | + } |
| 36 | + |
| 37 | + #[\Override] |
| 38 | + public function exists(): bool { |
| 39 | + return true; |
| 40 | + } |
| 41 | + |
| 42 | + #[\Override] |
| 43 | + public function getDisplayName(): string { |
| 44 | + return $this->cloudId->getDisplayId(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Setting avatars isn't implemented for remote accounts |
| 49 | + */ |
| 50 | + #[\Override] |
| 51 | + public function set($data): void { |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Removing avatars isn't implemented for remote accounts |
| 56 | + */ |
| 57 | + #[\Override] |
| 58 | + public function remove(bool $silent = false): void { |
| 59 | + } |
| 60 | + |
| 61 | + #[\Override] |
| 62 | + public function getFile(int $size, bool $darkTheme = false): ISimpleFile { |
| 63 | + if ($size === -1) { |
| 64 | + $filename = 'avatar' . ($darkTheme ? '-dark' : ''); |
| 65 | + } else { |
| 66 | + $filename = 'avatar' . ($darkTheme ? '-dark' : '') . '.' . $size; |
| 67 | + } |
| 68 | + |
| 69 | + $avatar = null; |
| 70 | + $files = $this->folder->getDirectoryListing(); |
| 71 | + foreach ($files as $file) { |
| 72 | + if (pathinfo($file->getName(), PATHINFO_FILENAME) === $filename) { |
| 73 | + $avatar = $file; |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if ($avatar !== null) { |
| 79 | + // check if a remote avatar is at least a day old, in case a new avatar was uploaded |
| 80 | + $isAvatarOld = (time() - $avatar->getMTime()) >= self::IMAGE_CACHE_AGE; |
| 81 | + if (!$isAvatarOld) { |
| 82 | + return $avatar; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + $url = rtrim($this->cloudId->getRemote(), '/') . '/index.php/avatar/' . rawurlencode($this->cloudId->getUser()) . '/' . $size; |
| 87 | + if ($darkTheme) { |
| 88 | + $url .= '/dark'; |
| 89 | + } |
| 90 | + |
| 91 | + $clientService = \OCP\Server::get(IClientService::class); |
| 92 | + $client = $clientService->newClient(); |
| 93 | + $response = $client->get($url, [ |
| 94 | + 'verify' => !$this->config->getSystemValueBool('sharing.federation.allowSelfSignedCertificates', false) |
| 95 | + ]); |
| 96 | + |
| 97 | + $contentType = $response->getHeader('Content-Type'); |
| 98 | + if (str_starts_with($contentType, 'image/') === false) { |
| 99 | + throw new \Exception('Unknown filetype'); |
| 100 | + } |
| 101 | + |
| 102 | + $avatar = $response->getBody(); |
| 103 | + if ($avatar === null) { |
| 104 | + throw new \Exception('Failed to fetch remote avatar'); |
| 105 | + } |
| 106 | + |
| 107 | + if (is_resource($avatar)) { |
| 108 | + $avatar = stream_get_contents($avatar); |
| 109 | + if ($avatar === false) { |
| 110 | + throw new \Exception('Failed to fetch remote avatar'); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + $ext = match ($contentType) { |
| 115 | + 'image/png' => 'png', |
| 116 | + 'image/jpg', 'image/jpeg' => 'jpg', |
| 117 | + 'image/gif' => 'gif', |
| 118 | + 'image/webp' => 'webp', |
| 119 | + default => 'png', |
| 120 | + }; |
| 121 | + return $this->folder->newFile($filename . '.' . $ext, $avatar); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Handling user changes isn't implemented for remote accounts |
| 126 | + */ |
| 127 | + #[\Override] |
| 128 | + public function userChanged(string $feature, $oldValue, $newValue): void { |
| 129 | + } |
| 130 | + |
| 131 | + #[\Override] |
| 132 | + public function isCustomAvatar(): bool { |
| 133 | + return true; |
| 134 | + } |
| 135 | +} |
0 commit comments