‪TYPO3CMS  ‪main
Avatar.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
23 use TYPO3\CMS\Core\Imaging\IconSize;
27 
34 class ‪Avatar
35 {
41  protected array ‪$avatarProviders = [];
42 
43  public function ‪__construct(
44  protected readonly ‪FrontendInterface $cache,
45  protected readonly ‪DependencyOrderingService $dependencyOrderingService,
46  protected readonly ‪IconFactory $iconFactory
47  ) {
49  }
50 
56  public function ‪render(array $backendUser = null, int $size = 32, bool $showIcon = false): string
57  {
58  if (!is_array($backendUser)) {
60  $backendUser = $this->‪getBackendUser()->user;
61  }
62  $cacheId = 'avatar_' . sha1($backendUser['uid'] . $size . $showIcon);
63  $avatar = $this->cache->get($cacheId);
64  if (!$avatar) {
65  $icon = $showIcon ? $this->iconFactory->getIconForRecord('be_users', $backendUser, IconSize::SMALL)->render() : '';
66  $avatar =
67  '<span class="avatar" style="--avatar-size: ' . $size . 'px;">'
68  . '<span class="avatar-image">' . $this->‪getImgTag($backendUser, $size) . '</span>'
69  . ($showIcon ? '<span class="avatar-icon">' . $icon . '</span>' : '')
70  . '</span>';
71  $this->cache->set($cacheId, $avatar);
72  }
73  return $avatar;
74  }
75 
79  protected function ‪getImgTag(array $backendUser, int $size = 32): string
80  {
81  $avatarImage = $this->‪getImage($backendUser, $size);
82  return '<img src="' . htmlspecialchars($avatarImage->getUrl()) . '" ' .
83  'width="' . (int)$avatarImage->getWidth() . '" ' .
84  'height="' . (int)$avatarImage->getHeight() . '" ' .
85  'alt="" ' .
86  'loading="lazy" />';
87  }
88 
92  protected function ‪getImage(array $backendUser, int $size): ‪Image
93  {
94  foreach ($this->avatarProviders as $provider) {
95  $avatarImage = $provider->getImage($backendUser, $size);
96  if (!empty($avatarImage)) {
97  return $avatarImage;
98  }
99  }
100  return GeneralUtility::makeInstance(
101  Image::class,
102  ‪PathUtility::getPublicResourceWebPath('EXT:core/Resources/Public/Icons/T3Icons/svgs/avatar/avatar-default.svg'),
103  $size,
104  $size
105  );
106  }
107 
113  protected function ‪validateSortAndInitiateAvatarProviders(): void
114  {
116  $providers = ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'] ?? [];
117  if (empty($providers)) {
118  return;
119  }
120  foreach ($providers as ‪$identifier => $configuration) {
121  if (empty($configuration) || !is_array($configuration)) {
122  throw new \RuntimeException(
123  'Missing configuration for avatar provider "' . ‪$identifier . '".',
124  1439317801
125  );
126  }
127  if (!is_string($configuration['provider']) || empty($configuration['provider']) || !class_exists($configuration['provider']) || !is_subclass_of(
128  $configuration['provider'],
129  AvatarProviderInterface::class
130  )) {
131  throw new \RuntimeException(
132  'The avatar provider "' . ‪$identifier . '" defines an invalid provider. Ensure the class exists and implements the "' . AvatarProviderInterface::class . '".',
133  1439317802
134  );
135  }
136  }
137  $orderedProviders = $this->dependencyOrderingService->orderByDependencies($providers);
138  foreach ($orderedProviders as $configuration) {
140  $avatarProvider = GeneralUtility::makeInstance($configuration['provider']);
141  $this->avatarProviders[] = $avatarProvider;
142  }
143  }
144 
146  {
147  return ‪$GLOBALS['BE_USER'];
148  }
149 }
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\getImgTag
‪getImgTag(array $backendUser, int $size=32)
Definition: Avatar.php:79
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\render
‪render(array $backendUser=null, int $size=32, bool $showIcon=false)
Definition: Avatar.php:56
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar
Definition: Avatar.php:35
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\getImage
‪getImage(array $backendUser, int $size)
Definition: Avatar.php:92
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Core\Utility\PathUtility\getPublicResourceWebPath
‪static getPublicResourceWebPath(string $resourcePath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:97
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\$avatarProviders
‪array $avatarProviders
Definition: Avatar.php:41
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\validateSortAndInitiateAvatarProviders
‪validateSortAndInitiateAvatarProviders()
Definition: Avatar.php:113
‪TYPO3\CMS\Core\Service\DependencyOrderingService
Definition: DependencyOrderingService.php:32
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:61
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Backend\Backend\Avatar
Definition: Avatar.php:18
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Backend\Avatar\Image
Definition: Image.php:23
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\__construct
‪__construct(protected readonly FrontendInterface $cache, protected readonly DependencyOrderingService $dependencyOrderingService, protected readonly IconFactory $iconFactory)
Definition: Avatar.php:43
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\getBackendUser
‪getBackendUser()
Definition: Avatar.php:145
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37