[CLEANUP] Streamline Avatar-related code in EXT:backend
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Backend / Avatar / Avatar.php
1 <?php
2 namespace TYPO3\CMS\Backend\Backend\Avatar;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
18 use TYPO3\CMS\Core\Cache\CacheManager;
19 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
20 use TYPO3\CMS\Core\Service\DependencyOrderingService;
21 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Fluid\View\StandaloneView;
24
25 /**
26 * Main class to render an avatar image of a certain Backend user, resolving any avatar provider
27 * that takes care of fetching the image.
28 *
29 * See render() and getImgTag() as main entry points
30 */
31 class Avatar
32 {
33 /**
34 * Array of sorted and initialized avatar providers
35 *
36 * @var AvatarProviderInterface[]
37 */
38 protected $avatarProviders = [];
39
40 /**
41 * Renders an avatar based on a Fluid template which contains some base wrapper classes and does
42 * a simple caching functionality, used in Avatar ViewHelper for instance
43 *
44 * @param array $backendUser be_users record
45 * @param int $size width and height of the image
46 * @param bool $showIcon show the record icon
47 * @return string
48 */
49 public function render(array $backendUser = null, int $size = 32, bool $showIcon = false)
50 {
51 if (!is_array($backendUser)) {
52 $backendUser = $this->getBackendUser()->user;
53 }
54
55 $cacheId = 'avatar_' . md5(
56 $backendUser['uid'] . '/' .
57 (string)$size . '/' .
58 (string)$showIcon);
59
60 $avatar = static::getCache()->get($cacheId);
61
62 if (!$avatar) {
63 $this->validateSortAndInitiateAvatarProviders();
64 $view = $this->getFluidTemplateObject();
65
66 $view->assignMultiple([
67 'image' => $this->getImgTag($backendUser, $size),
68 'showIcon' => $showIcon,
69 'backendUser' => $backendUser
70 ]);
71 $avatar = $view->render();
72 static::getCache()->set($cacheId, $avatar);
73 }
74
75 return $avatar;
76 }
77
78 /**
79 * Returns an HTML <img> tag for the avatar
80 *
81 * @param array $backendUser be_users record
82 * @param int $size
83 * @return string
84 */
85 public function getImgTag(array $backendUser = null, $size = 32)
86 {
87 if (!is_array($backendUser)) {
88 $backendUser = $this->getBackendUser()->user;
89 }
90
91 $avatarImage = false;
92 if ($backendUser !== null) {
93 $avatarImage = $this->getImage($backendUser, $size);
94 }
95
96 if (!$avatarImage) {
97 $avatarImage = GeneralUtility::makeInstance(
98 Image::class,
99 ExtensionManagementUtility::siteRelPath('core') . 'Resources/Public/Icons/T3Icons/avatar/avatar-default.svg',
100 $size,
101 $size
102 );
103 }
104 $imageTag = '<img src="' . htmlspecialchars($avatarImage->getUrl(true)) . '" ' .
105 'width="' . (int)$avatarImage->getWidth() . '" ' .
106 'height="' . (int)$avatarImage->getHeight() . '" />';
107
108 return $imageTag;
109 }
110
111 /**
112 * Get Image from first provider that returns one
113 *
114 * @param array $backendUser be_users record
115 * @param int $size
116 * @return Image|NULL
117 */
118 public function getImage(array $backendUser, $size)
119 {
120 foreach ($this->avatarProviders as $provider) {
121 $avatarImage = $provider->getImage($backendUser, $size);
122 if (!empty($avatarImage)) {
123 return $avatarImage;
124 }
125 }
126 return null;
127 }
128
129 /**
130 * Validates the registered avatar providers
131 *
132 * @return void
133 * @throws \RuntimeException
134 */
135 protected function validateSortAndInitiateAvatarProviders()
136 {
137 if (
138 empty($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'])
139 || !is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'])
140 ) {
141 return;
142 }
143 $providers = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'];
144 foreach ($providers as $identifier => $configuration) {
145 if (empty($configuration) || !is_array($configuration)) {
146 throw new \RuntimeException('Missing configuration for avatar provider "' . $identifier . '".', 1439317801);
147 }
148 if (!is_string($configuration['provider']) || empty($configuration['provider']) || !class_exists($configuration['provider']) || !is_subclass_of($configuration['provider'], AvatarProviderInterface::class)) {
149 throw new \RuntimeException('The avatar provider "' . $identifier . '" defines an invalid provider. Ensure the class exists and implements the "' . AvatarProviderInterface::class . '".', 1439317802);
150 }
151 }
152
153 $orderedProviders = GeneralUtility::makeInstance(DependencyOrderingService::class)->orderByDependencies($providers);
154
155 // Initializes providers
156 foreach ($orderedProviders as $configuration) {
157 $this->avatarProviders[] = GeneralUtility::makeInstance($configuration['provider']);
158 }
159 }
160
161 /**
162 * Returns the current BE user.
163 *
164 * @return BackendUserAuthentication
165 */
166 protected function getBackendUser()
167 {
168 return $GLOBALS['BE_USER'];
169 }
170
171 /**
172 * Returns a new standalone view, shorthand function
173 *
174 * @param string $filename Which templateFile should be used.
175 * @return StandaloneView
176 */
177 protected function getFluidTemplateObject(string $filename = null): StandaloneView
178 {
179 $view = GeneralUtility::makeInstance(StandaloneView::class);
180 $view->setLayoutRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Layouts')]);
181 $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
182 $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
183
184 if ($filename === null) {
185 $filename = 'Main.html';
186 }
187
188 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/Avatar/' . $filename));
189
190 $view->getRequest()->setControllerExtensionName('Backend');
191 return $view;
192 }
193
194 /**
195 * @return FrontendInterface
196 */
197 protected function getCache()
198 {
199 return GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_runtime');
200 }
201 }