[CLEANUP] The correct case must be used for standard PHP types in phpdoc
[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\ExtensionManagementUtility;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
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 = static::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 static::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 ExtensionManagementUtility::siteRelPath('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 if (
139 empty($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'])
140 || !is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'])
141 ) {
142 return;
143 }
144 $providers = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'];
145 foreach ($providers as $identifier => $configuration) {
146 if (empty($configuration) || !is_array($configuration)) {
147 throw new \RuntimeException(
148 'Missing configuration for avatar provider "' . $identifier . '".',
149 1439317801
150 );
151 }
152 if (!is_string($configuration['provider']) || empty($configuration['provider']) || !class_exists($configuration['provider']) || !is_subclass_of(
153 $configuration['provider'],
154 AvatarProviderInterface::class
155 )) {
156 throw new \RuntimeException(
157 'The avatar provider "' . $identifier . '" defines an invalid provider. Ensure the class exists and implements the "' . AvatarProviderInterface::class . '".',
158 1439317802
159 );
160 }
161 }
162
163 $orderedProviders = GeneralUtility::makeInstance(DependencyOrderingService::class)->orderByDependencies($providers);
164
165 // Initializes providers
166 foreach ($orderedProviders as $configuration) {
167 $this->avatarProviders[] = GeneralUtility::makeInstance($configuration['provider']);
168 }
169 }
170
171 /**
172 * Returns the current BE user.
173 *
174 * @return BackendUserAuthentication
175 */
176 protected function getBackendUser()
177 {
178 return $GLOBALS['BE_USER'];
179 }
180
181 /**
182 * Returns a new standalone view, shorthand function
183 *
184 * @param string $filename Which templateFile should be used.
185 * @return StandaloneView
186 */
187 protected function getFluidTemplateObject(string $filename = null): StandaloneView
188 {
189 $view = GeneralUtility::makeInstance(StandaloneView::class);
190 $view->setLayoutRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Layouts')]);
191 $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
192 $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
193
194 if ($filename === null) {
195 $filename = 'Main.html';
196 }
197
198 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/Avatar/' . $filename));
199
200 $view->getRequest()->setControllerExtensionName('Backend');
201 return $view;
202 }
203
204 /**
205 * @return FrontendInterface
206 */
207 protected function getCache()
208 {
209 return GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_runtime');
210 }
211 }