[FEATURE] Introduce AvatarProvider
[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\Backend\Utility\IconUtility;
18 use TYPO3\CMS\Core\Service\DependencyOrderingService;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Avatar renderer class
23 */
24 class Avatar {
25
26 /**
27 * Array of sorted and initiated avatar providers
28 *
29 * @var AvatarProviderInterface[]
30 */
31 protected $avatarProviders = [];
32
33 /**
34 * Construct
35 */
36 public function __construct() {
37 $this->validateSortAndInitiateAvatarProviders();
38 }
39
40 /**
41 * Render avatar tag
42 *
43 * @param array $backendUser be_user record
44 * @param int $size width and height of the image
45 * @param bool $showIcon show the record icon
46 * @return string
47 */
48 public function render(array $backendUser = NULL, $size = 32, $showIcon = FALSE) {
49 if (!is_array($backendUser)) {
50 $backendUser = $this->getBackendUser()->user;
51 }
52
53 // Icon
54 $icon = '';
55 if ($showIcon) {
56 $icon = '<span class="avatar-icon">' . IconUtility::getSpriteIconForRecord('be_users', $backendUser) . '</span>';
57 }
58
59 $image = $this->getImgTag($backendUser, $size);
60
61 return '<span class="avatar"><span class="avatar-image">' . $image . '</span>' . $icon . '</span>';
62 }
63
64 /**
65 * Get avatar img tag
66 *
67 * @param array $backendUser be_user record
68 * @param int $size
69 * @return string
70 */
71 public function getImgTag(array $backendUser = NULL, $size = 32) {
72 if (!is_array($backendUser)) {
73 $backendUser = $this->getBackendUser()->user;
74 }
75
76 $imageTag = '';
77 $avatarImage = $this->getImage($backendUser, $size);
78
79 if ($avatarImage) {
80 $imageTag = '<img src="' . htmlspecialchars($avatarImage->getUrl()) . '"' .
81 'width="' . (int)$avatarImage->getWidth() . '" ' .
82 'height="' . (int)$avatarImage->getHeight() . '" />';
83 }
84
85 return $imageTag;
86 }
87
88 /**
89 * Get Image from first provider that returns one
90 *
91 * @param array $backendUser be_user record
92 * @param int $size
93 * @return Image|NULL
94 */
95 public function getImage(array $backendUser, $size) {
96 foreach ($this->avatarProviders as $provider) {
97 $avatarImage = $provider->getImage($backendUser, $size);
98 if (!empty($avatarImage)) {
99 return $avatarImage;
100 }
101 }
102 return NULL;
103 }
104
105 /**
106 * Validates the registered avatar providers
107 *
108 * @return void
109 * @throws \RuntimeException
110 */
111 protected function validateSortAndInitiateAvatarProviders() {
112 if (
113 empty($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'])
114 || !is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'])
115 ) {
116 return;
117 }
118 $providers = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'];
119 foreach ($providers as $identifier => $configuration) {
120 if (empty($configuration) || !is_array($configuration)) {
121 throw new \RuntimeException('Missing configuration for avatar provider "' . $identifier . '".', 1439317801);
122 }
123 if (!is_string($configuration['provider']) || empty($configuration['provider']) || !class_exists($configuration['provider']) || !is_subclass_of($configuration['provider'], AvatarProviderInterface::class)) {
124 throw new \RuntimeException('The avatar provider "' . $identifier . '" defines an invalid provider. Ensure the class exists and implements the "' . AvatarProviderInterface::class . '".', 1439317802);
125 }
126 }
127
128 $orderedProviders = GeneralUtility::makeInstance(DependencyOrderingService::class)->orderByDependencies($providers);
129
130 // Initiate providers
131 foreach ($orderedProviders as $configuration) {
132 $this->avatarProviders[] = GeneralUtility::makeInstance($configuration['provider']);
133 }
134 }
135
136 /**
137 * Returns the current BE user.
138 *
139 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
140 */
141 protected function getBackendUser() {
142 return $GLOBALS['BE_USER'];
143 }
144
145 }