‪TYPO3CMS  10.4
Avatar.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
25 
32 class ‪Avatar
33 {
39  protected ‪$avatarProviders = [];
40 
50  public function ‪render(array $backendUser = null, int $size = 32, bool $showIcon = false)
51  {
52  if (!is_array($backendUser)) {
53  $backendUser = $this->‪getBackendUser()->user;
54  }
55 
56  $cacheId = 'avatar_' . md5(
57  $backendUser['uid'] . '/' .
58  (string)$size . '/' .
59  (string)$showIcon
60  );
61 
62  $avatar = $this->‪getCache()->‪get($cacheId);
63 
64  if (!$avatar) {
66  $view = $this->‪getFluidTemplateObject();
67 
68  $view->assignMultiple([
69  'image' => $this->‪getImgTag($backendUser, $size),
70  'showIcon' => $showIcon,
71  'backendUser' => $backendUser
72  ]);
73  $avatar = $view->render();
74  $this->‪getCache()->‪set($cacheId, $avatar);
75  }
76 
77  return $avatar;
78  }
79 
87  public function ‪getImgTag(array $backendUser = null, $size = 32)
88  {
89  if (!is_array($backendUser)) {
90  $backendUser = $this->‪getBackendUser()->user;
91  }
92 
93  $avatarImage = false;
94  if ($backendUser !== null) {
95  $avatarImage = $this->‪getImage($backendUser, $size);
96  }
97 
98  if (!$avatarImage) {
99  $avatarImage = GeneralUtility::makeInstance(
100  Image::class,
101  ‪PathUtility::stripPathSitePrefix(GeneralUtility::getFileAbsFileName('EXT:core/Resources/Public/Icons/T3Icons/svgs/avatar/avatar-default.svg')),
102  $size,
103  $size
104  );
105  }
106  $imageTag = '<img src="' . htmlspecialchars($avatarImage->getUrl(true)) . '" ' .
107  'width="' . (int)$avatarImage->getWidth() . '" ' .
108  'height="' . (int)$avatarImage->getHeight() . '" />';
109 
110  return $imageTag;
111  }
112 
120  public function ‪getImage(array $backendUser, $size)
121  {
122  foreach ($this->avatarProviders as $provider) {
123  $avatarImage = $provider->getImage($backendUser, $size);
124  if (!empty($avatarImage)) {
125  return $avatarImage;
126  }
127  }
128  return null;
129  }
130 
136  protected function ‪validateSortAndInitiateAvatarProviders()
137  {
138  $providers = ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'] ?? [];
139  if (empty($providers)) {
140  return;
141  }
142  foreach ($providers as $identifier => $configuration) {
143  if (empty($configuration) || !is_array($configuration)) {
144  throw new \RuntimeException(
145  'Missing configuration for avatar provider "' . $identifier . '".',
146  1439317801
147  );
148  }
149  if (!is_string($configuration['provider']) || empty($configuration['provider']) || !class_exists($configuration['provider']) || !is_subclass_of(
150  $configuration['provider'],
151  AvatarProviderInterface::class
152  )) {
153  throw new \RuntimeException(
154  'The avatar provider "' . $identifier . '" defines an invalid provider. Ensure the class exists and implements the "' . AvatarProviderInterface::class . '".',
155  1439317802
156  );
157  }
158  }
159 
160  $orderedProviders = GeneralUtility::makeInstance(DependencyOrderingService::class)->orderByDependencies($providers);
161 
162  // Initializes providers
163  foreach ($orderedProviders as $configuration) {
164  $this->avatarProviders[] = GeneralUtility::makeInstance($configuration['provider']);
165  }
166  }
167 
173  protected function ‪getBackendUser()
174  {
175  return ‪$GLOBALS['BE_USER'];
176  }
177 
184  protected function ‪getFluidTemplateObject(string $filename = null): ‪StandaloneView
185  {
186  $view = GeneralUtility::makeInstance(StandaloneView::class);
187  $view->setLayoutRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Layouts')]);
188  $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
189  $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
190 
191  if ($filename === null) {
192  $filename = 'Main.html';
193  }
194 
195  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/Avatar/' . $filename));
196 
197  $view->getRequest()->setControllerExtensionName('Backend');
198  return $view;
199  }
200 
204  protected function ‪getCache()
205  {
206  return GeneralUtility::makeInstance(CacheManager::class)->getCache('runtime');
207  }
208 }
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\render
‪string render(array $backendUser=null, int $size=32, bool $showIcon=false)
Definition: Avatar.php:49
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:24
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\$avatarProviders
‪AvatarProviderInterface[] $avatarProviders
Definition: Avatar.php:38
‪TYPO3\CMS\Core\Utility\PathUtility\stripPathSitePrefix
‪static string stripPathSitePrefix($path)
Definition: PathUtility.php:372
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar
Definition: Avatar.php:33
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\getCache
‪FrontendInterface getCache()
Definition: Avatar.php:203
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\getImgTag
‪string getImgTag(array $backendUser=null, $size=32)
Definition: Avatar.php:86
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface\get
‪mixed get($entryIdentifier)
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\validateSortAndInitiateAvatarProviders
‪validateSortAndInitiateAvatarProviders()
Definition: Avatar.php:135
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:35
‪TYPO3\CMS\Core\Service\DependencyOrderingService
Definition: DependencyOrderingService.php:32
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\getFluidTemplateObject
‪StandaloneView getFluidTemplateObject(string $filename=null)
Definition: Avatar.php:183
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Backend\Backend\Avatar
Definition: Avatar.php:16
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:34
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: Avatar.php:172
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Backend\Backend\Avatar\AvatarProviderInterface
Definition: AvatarProviderInterface.php:22
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\getImage
‪Image null getImage(array $backendUser, $size)
Definition: Avatar.php:119
‪TYPO3\CMS\Backend\Backend\Avatar\Image
Definition: Image.php:27
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface\set
‪set($entryIdentifier, $data, array $tags=[], $lifetime=null)
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46