TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
SelectImageController.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Rtehtmlarea\Controller;
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 Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
26 
31 {
37  const PLAIN_MODE_IMAGE_FILE_EXTENSIONS = 'jpg,jpeg,gif,png';
38 
57  protected $bparams;
58 
64  protected $RTEProperties = [];
65 
72  protected $RTEtsConfigParams;
73 
77  protected $editorNo;
78 
85 
89  protected $buttonConfig = [];
90 
94  protected function init()
95  {
96  parent::init();
97  $this->getLanguageService()->includeLLFile('EXT:rtehtmlarea/Resources/Private/Language/locallang_dialogs.xlf');
98  }
99 
103  protected function initVariables(ServerRequestInterface $request)
104  {
105  parent::initVariables($request);
106 
107  $queryParameters = $request->getQueryParams();
108  $this->bparams = isset($queryParameters['bparams']) ? $queryParameters['bparams'] : '';
109  $this->currentLinkParts['currentImage'] = !empty($queryParameters['fileUid']) ? $queryParameters['fileUid'] : 0;
110 
111  // Process bparams
112  $pArr = explode('|', $this->bparams);
113  $pRteArr = explode(':', $pArr[1]);
114  $this->editorNo = $pRteArr[0];
115  $this->contentTypo3Language = $pRteArr[1];
116  $this->RTEtsConfigParams = $pArr[2];
117  if (!$this->editorNo) {
118  $this->editorNo = GeneralUtility::_GP('editorNo');
119  $this->contentTypo3Language = GeneralUtility::_GP('contentTypo3Language');
120  $this->RTEtsConfigParams = GeneralUtility::_GP('RTEtsConfigParams');
121  }
122  $pArr[1] = implode(':', [$this->editorNo, $this->contentTypo3Language]);
123  $pArr[2] = $this->RTEtsConfigParams;
124  $pArr[3] = $this->displayedLinkHandlerId === 'plain'
125  ? self::PLAIN_MODE_IMAGE_FILE_EXTENSIONS
126  : '';
127  $this->bparams = implode('|', $pArr);
128 
129  $RTEtsConfigParts = explode(':', $this->RTEtsConfigParams);
130  $RTEsetup = $this->getBackendUser()->getTSConfig('RTE', BackendUtility::getPagesTSconfig($RTEtsConfigParts[5]));
131  $this->RTEProperties = $RTEsetup['properties'];
132 
133  $thisConfig = BackendUtility::RTEsetup($this->RTEProperties, $RTEtsConfigParts[0], $RTEtsConfigParts[2], $RTEtsConfigParts[4]);
134  $this->buttonConfig = isset($thisConfig['buttons.']['image.'])
135  ? $thisConfig['buttons.']['image.']
136  : [];
137  }
138 
145  protected function initHookObjects()
146  {
147  if (
148  isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['RteImageSelector']['hooks'])
149  && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['RteImageSelector']['hooks'])
150  ) {
151  $hooks = GeneralUtility::makeInstance(DependencyOrderingService::class)->orderByDependencies(
152  $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['RteImageSelector']['hooks']
153  );
154  foreach ($hooks as $key => $hook) {
155  $this->hookObjects[] = GeneralUtility::makeInstance($hook['handler']);
156  }
157  }
158  }
159 
166  protected function getLinkHandlers()
167  {
168  $imageHandler = $this->buttonConfig['options.']['imageHandler.'];
169 
170  foreach ($this->hookObjects as $hookObject) {
171  if (method_exists($hookObject, 'modifyImageHandlers')) {
172  $imageHandler = $hookObject->modifyImageHandlers($imageHandler, $this->currentLinkParts);
173  }
174  }
175 
176  if (empty($imageHandler)) {
177  throw new \UnexpectedValueException('No image handlers are configured. Check page TSconfig RTE.default.buttons.image.options.imageHandler.', 1455499673);
178  }
179 
180  return $imageHandler;
181  }
182 
188  protected function initCurrentUrl()
189  {
190  if (empty($this->currentLinkParts)) {
191  return;
192  }
193 
194  $orderedHandlers = GeneralUtility::makeInstance(DependencyOrderingService::class)->orderByDependencies($this->linkHandlers, 'scanBefore', 'scanAfter');
195 
196  // find responsible handler for current image
197  foreach ($orderedHandlers as $key => $configuration) {
199  $handler = $configuration['handlerInstance'];
200  if ($handler->canHandleLink($this->currentLinkParts)) {
201  $this->currentLinkHandler = $handler;
202  $this->currentLinkHandlerId = $key;
203  break;
204  }
205  }
206  // reset the image reference if we have no handler for it
207  if (!$this->currentLinkHandler) {
208  $this->currentLinkParts = [];
209  }
210  }
211 
217  protected function renderCurrentUrl()
218  {
219  return '<!-- Print current URL -->
220  <div class="link-browser-section link-browser-current-link">
221  <strong>' .
222  htmlspecialchars($this->getLanguageService()->getLL('currentImage')) .
223  ': ' .
224  htmlspecialchars($this->currentLinkHandler->formatCurrentUrl()) .
225  '</strong>' .
226  '</div>';
227  }
228 
234  protected function getAllowedItems()
235  {
236  $allowedItems = array_keys($this->linkHandlers);
237 
238  foreach ($this->hookObjects as $hookObject) {
239  if (method_exists($hookObject, 'modifyAllowedItems')) {
240  $allowedItems = $hookObject->modifyAllowedItems($allowedItems, $this->currentLinkParts);
241  }
242  }
243 
244  return $allowedItems;
245  }
246 
252  public function getUrlParameters(array $overrides = null)
253  {
254  return [
255  'act' => isset($overrides['act']) ? $overrides['act'] : $this->displayedLinkHandlerId,
256  'bparams' => $this->bparams,
257  'editorNo' => $this->editorNo
258  ];
259  }
260 
264  public function getButtonConfiguration()
265  {
266  return $this->buttonConfig;
267  }
268 
272  public function getRteProperties()
273  {
274  return $this->RTEProperties;
275  }
276 
284  public function buildImageMarkup(ServerRequestInterface $request, ResponseInterface $response)
285  {
286  $this->initVariables($request);
287  $uidList = GeneralUtility::_GP('uidList');
288  // handle ajax request for
289  $uids = explode('|', $uidList);
290  $tags = [];
291  foreach ($uids as $uid) {
292  $fileObject = ResourceFactory::getInstance()->getFileObject((int)$uid);
293  // Get default values for alt and title attributes from file properties
294  $altText = $fileObject->getProperty('alternative');
295  $titleText = $fileObject->getProperty('title');
296  if ($this->displayedLinkHandlerId === 'magic') {
297  // Create the magic image service
298  $magicImageService = GeneralUtility::makeInstance(MagicImageService::class);
299  $magicImageService->setMagicImageMaximumDimensions($this->RTEProperties['default.']);
300  // Create the magic image
301  $imageConfiguration = [
302  'width' => GeneralUtility::_GP('cWidth'),
303  'height' => GeneralUtility::_GP('cHeight')
304  ];
305  $fileObject = $magicImageService->createMagicImage($fileObject, $imageConfiguration);
306  $width = $fileObject->getProperty('width');
307  $height = $fileObject->getProperty('height');
308  } else {
309  $width = $fileObject->getProperty('width');
310  $height = $fileObject->getProperty('height');
311  if (!$width || !$height) {
312  $filePath = $fileObject->getForLocalProcessing(false);
313  $imageInfo = @getimagesize($filePath);
314  $width = $imageInfo[0];
315  $height = $imageInfo[1];
316  }
317  }
318  $imageUrl = $fileObject->getPublicUrl();
319  // If file is local, make the url absolute
320  if (strpos($imageUrl, 'http') !== 0) {
321  $imageUrl = GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . $imageUrl;
322  }
323  $tags[] = '<img src="' . htmlspecialchars($imageUrl) . '" width="' . htmlspecialchars($width) . '" height="' . htmlspecialchars($height) . '"'
324  . (isset($this->buttonConfig['properties.']['class.']['default'])
325  ? ' class="' . trim($this->buttonConfig['properties.']['class.']['default']) . '"'
326  : '')
327  . ' alt = "' . ($altText ? htmlspecialchars($altText) : '') . '"'
328  . ($titleText ? ' title="' . htmlspecialchars($titleText) . '"' : '')
329  . ' data-htmlarea-file-uid="' . (int)$uid . '" />';
330  }
331  $finalHtmlCode = implode(' ', $tags);
332 
333  $response->getBody()->write(json_encode(['images' => $finalHtmlCode]));
334  return $response;
335  }
336 
343  protected function getCurrentPageId()
344  {
345  throw new \RuntimeException('Invalid method call. This function is not supported for image handlers', 1455499679);
346  }
347 }
buildImageMarkup(ServerRequestInterface $request, ResponseInterface $response)
static getPagesTSconfig($id, $rootLine=null, $returnPartArray=false)
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static RTEsetup($RTEprop, $table, $field, $type= '')
static makeInstance($className,...$constructorArguments)