[BUGFIX] Add additional GetVars to workspace preview url
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Backend / Avatar / Avatar.php
1 <?php
2
3 namespace TYPO3\CMS\Backend\Backend\Avatar;
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
18 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
19 use TYPO3\CMS\Core\Cache\CacheManager;
20 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
21 use TYPO3\CMS\Core\Service\DependencyOrderingService;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Core\Utility\PathUtility;
24 use TYPO3\CMS\Fluid\View\StandaloneView;
25
26 /**
27 * Main class to render an avatar image of a certain Backend user, resolving any avatar provider
28 * that takes care of fetching the image.
29 *
30 * See render() and getImgTag() as main entry points
31 */
32 class Avatar
33 {
34 /**
35 * Array of sorted and initialized avatar providers
36 *
37 * @var AvatarProviderInterface[]
38 */
39 protected $avatarProviders = [];
40
41 /**
42 * Renders an avatar based on a Fluid template which contains some base wrapper classes and does
43 * a simple caching functionality, used in Avatar ViewHelper for instance
44 *
45 * @param array $backendUser be_users record
46 * @param int $size width and height of the image
47 * @param bool $showIcon show the record icon
48 * @return string
49 */
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) {
65 $this->validateSortAndInitiateAvatarProviders();
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
80 /**
81 * Returns an HTML <img> tag for the avatar
82 *
83 * @param array $backendUser be_users record
84 * @param int $size
85 * @return string
86 */
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/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
113 /**
114 * Get Image from first provider that returns one
115 *
116 * @param array $backendUser be_users record
117 * @param int $size
118 * @return Image|null
119 */
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
131 /**
132 * Validates the registered avatar providers
133 *
134 * @throws \RuntimeException
135 */
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
168 /**
169 * Returns the current BE user.
170 *
171 * @return BackendUserAuthentication
172 */
173 protected function getBackendUser()
174 {
175 return $GLOBALS['BE_USER'];
176 }
177
178 /**
179 * Returns a new standalone view, shorthand function
180 *
181 * @param string $filename Which templateFile should be used.
182 * @return StandaloneView
183 */
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
201 /**
202 * @return FrontendInterface
203 */
204 protected function getCache()
205 {
206 return GeneralUtility::makeInstance(CacheManager::class)->getCache('runtime');
207 }
208 }