TYPO3  7.6
ColorpickerController.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Backend\Controller\Wizard;
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 
23 
28 {
35 
41  public $colorValue;
42 
51 
56 
62  public $fieldName;
63 
69  public $formName;
70 
76  public $md5ID;
77 
83  public $showPicker;
84 
88  public $HTMLcolorList = 'aqua,black,blue,fuchsia,gray,green,lime,maroon,navy,olive,purple,red,silver,teal,yellow,white';
89 
93  public $pickerImage = '';
94 
100  public $imageError = '';
101 
107  public $doc;
108 
112  public $content;
113 
117  protected $exampleImg;
118 
122  public function __construct()
123  {
124  parent::__construct();
125  $this->getLanguageService()->includeLLFile('EXT:lang/locallang_wizards.xlf');
126  $GLOBALS['SOBE'] = $this;
127 
128  $this->init();
129  }
130 
136  protected function init()
137  {
138  // Setting GET vars (used in frameset script):
139  $this->wizardParameters = GeneralUtility::_GP('P');
140  // Setting GET vars (used in colorpicker script):
141  $this->colorValue = GeneralUtility::_GP('colorValue');
142  $this->fieldChangeFunc = GeneralUtility::_GP('fieldChangeFunc');
143  $this->fieldChangeFuncHash = GeneralUtility::_GP('fieldChangeFuncHash');
144  $this->fieldName = GeneralUtility::_GP('fieldName');
145  $this->formName = GeneralUtility::_GP('formName');
146  $this->md5ID = GeneralUtility::_GP('md5ID');
147  $this->exampleImg = GeneralUtility::_GP('exampleImg');
148  // Resolving image (checking existence etc.)
149  $this->imageError = '';
150  if ($this->exampleImg) {
151  $this->pickerImage = GeneralUtility::getFileAbsFileName($this->exampleImg, 1, 1);
152  if (!$this->pickerImage || !@is_file($this->pickerImage)) {
153  $this->imageError = 'ERROR: The image, "' . $this->exampleImg . '", could not be found!';
154  }
155  }
156  $update = array();
157  if ($this->areFieldChangeFunctionsValid()) {
158  // Setting field-change functions:
159  $fieldChangeFuncArr = unserialize($this->fieldChangeFunc);
160  unset($fieldChangeFuncArr['alert']);
161  foreach ($fieldChangeFuncArr as $v) {
162  $update[] = 'parent.opener.' . $v;
163  }
164  }
165  // Initialize document object:
166  $this->doc = GeneralUtility::makeInstance(DocumentTemplate::class);
167  $this->getPageRenderer()->loadRequireJsModule(
168  'TYPO3/CMS/Backend/Wizard/Colorpicker',
169  'function(Colorpicker) {
170  Colorpicker.setFieldChangeFunctions({
171  fieldChangeFunctions: function() {'
172  . implode('', $update) .
173  '}
174  });
175  }'
176  );
177  // Start page:
178  $this->content .= $this->doc->startPage($this->getLanguageService()->getLL('colorpicker_title'));
179  }
180 
190  {
191  $this->main();
192 
193  $this->content .= $this->doc->endPage();
194  $this->content = $this->doc->insertStylesAndJS($this->content);
195 
196  $response->getBody()->write($this->content);
197  return $response;
198  }
199 
205  public function main()
206  {
207  // Show frameset by default:
208  if (!GeneralUtility::_GP('showPicker')) {
209  $this->frameSet();
210  } else {
211  // Putting together the items into a form:
212  $content = '
213  <form name="colorform" method="post" action="' . htmlspecialchars(BackendUtility::getModuleUrl('wizard_colorpicker')) . '">
214  ' . $this->colorMatrix() . '
215  ' . $this->colorList() . '
216  ' . $this->colorImage() . '
217 
218  <!-- Value box: -->
219  <p class="c-head">' . $this->getLanguageService()->getLL('colorpicker_colorValue', true) . '</p>
220  <table border="0" cellpadding="0" cellspacing="3">
221  <tr>
222  <td>
223  <input id="colorValue" type="text" ' . $this->doc->formWidth(7) . ' maxlength="10" name="colorValue" value="' . htmlspecialchars($this->colorValue) . '" />
224  </td>
225  <td style="background-color:' . htmlspecialchars($this->colorValue) . '; border: 1px solid black;">
226  <span style="color: black;">' . $this->getLanguageService()->getLL('colorpicker_black', true) . '</span>&nbsp;<span style="color: white;">' . $this->getLanguageService()->getLL('colorpicker_white', true) . '</span>
227  </td>
228  <td>
229  <input class="btn btn-default" type="submit" id="colorpicker-saveclose" value="' . $this->getLanguageService()->getLL('colorpicker_setClose', true) . '" />
230  </td>
231  </tr>
232  </table>
233 
234  <!-- Hidden fields with values that has to be kept constant -->
235  <input type="hidden" name="showPicker" value="1" />
236  <input type="hidden" name="fieldChangeFunc" value="' . htmlspecialchars($this->fieldChangeFunc) . '" />
237  <input type="hidden" name="fieldChangeFuncHash" value="' . htmlspecialchars($this->fieldChangeFuncHash) . '" />
238  <input type="hidden" name="fieldName" value="' . htmlspecialchars($this->fieldName) . '" />
239  <input type="hidden" name="formName" value="' . htmlspecialchars($this->formName) . '" />
240  <input type="hidden" name="md5ID" value="' . htmlspecialchars($this->md5ID) . '" />
241  <input type="hidden" name="exampleImg" value="' . htmlspecialchars($this->exampleImg) . '" />
242  </form>';
243 
244  $this->content .= '<h2>' . $this->getLanguageService()->getLL('colorpicker_title', true) . '</h2>';
245  $this->content .= $content;
246  }
247  }
248 
255  public function printContent()
256  {
258  $this->content .= $this->doc->endPage();
259  $this->content = $this->doc->insertStylesAndJS($this->content);
260  echo $this->content;
261  }
262 
270  public function frameSet()
271  {
272  $this->getDocumentTemplate()->JScode = $this->getDocumentTemplate()->wrapScriptTags('
273  if (!window.opener) {
274  alert("ERROR: Sorry, no link to main window... Closing");
275  close();
276  }
277  ');
278  $this->getDocumentTemplate()->startPage($this->getLanguageService()->getLL('colorpicker_title'));
279 
280  // URL for the inner main frame:
281  $url = BackendUtility::getModuleUrl(
282  'wizard_colorpicker',
283  array(
284  'showPicker' => 1,
285  'colorValue' => $this->wizardParameters['currentValue'],
286  'fieldName' => $this->wizardParameters['itemName'],
287  'formName' => $this->wizardParameters['formName'],
288  'exampleImg' => $this->wizardParameters['exampleImg'],
289  'md5ID' => $this->wizardParameters['md5ID'],
290  'fieldChangeFunc' => serialize($this->wizardParameters['fieldChangeFunc']),
291  'fieldChangeFuncHash' => $this->wizardParameters['fieldChangeFuncHash'],
292  )
293  );
294  $this->content = $this->getPageRenderer()->render(PageRenderer::PART_HEADER) . '
295  <frameset rows="*,1" framespacing="0" frameborder="0" border="0">
296  <frame name="content" src="' . htmlspecialchars($url) . '" marginwidth="0" marginheight="0" frameborder="0" scrolling="auto" noresize="noresize" />
297  <frame name="menu" src="' . htmlspecialchars(BackendUtility::getModuleUrl('dummy')) . '" marginwidth="0" marginheight="0" frameborder="0" scrolling="no" noresize="noresize" />
298  </frameset>
299  </html>';
300  }
301 
302  /************************************
303  *
304  * Rendering of various color selectors
305  *
306  ************************************/
312  public function colorMatrix()
313  {
314  $steps = 51;
315  // Get colors:
316  $color = array();
317  for ($rr = 0; $rr < 256; $rr += $steps) {
318  for ($gg = 0; $gg < 256; $gg += $steps) {
319  for ($bb = 0; $bb < 256; $bb += $steps) {
320  $color[] = '#' . substr(('0' . dechex($rr)), -2) . substr(('0' . dechex($gg)), -2) . substr(('0' . dechex($bb)), -2);
321  }
322  }
323  }
324  // Traverse colors:
325  $columns = 24;
326  $rows = 0;
327  $tRows = array();
328  while (isset($color[$columns * $rows])) {
329  $tCells = array();
330  for ($i = 0; $i < $columns; $i++) {
331  $tCells[] = '<td bgcolor="' . $color[($columns * $rows + $i)] . '" class="t3js-colorpicker-value" data-color-value="' . htmlspecialchars($color[($columns * $rows + $i)]) . '" title="' . htmlspecialchars($color[($columns * $rows + $i)]) . '">&nbsp;&nbsp;</td>';
332  }
333  $tRows[] = '<tr>' . implode('', $tCells) . '</tr>';
334  $rows++;
335  }
336  return '<p class="c-head">' . $this->getLanguageService()->getLL('colorpicker_fromMatrix', true) . '</p>
337  <table style="width:100%; border: 1px solid black; cursor:crosshair;">' . implode('', $tRows) . '</table>';
338  }
339 
345  public function colorList()
346  {
347  // Initialize variables:
348  $colors = explode(',', $this->HTMLcolorList);
349  $currentValue = strtolower($this->colorValue);
350  $opt = array();
351  $opt[] = '<option value=""></option>';
352  // Traverse colors, making option tags for selector box.
353  foreach ($colors as $colorName) {
354  $opt[] = '<option style="background-color: ' . $colorName . ';" value="' . htmlspecialchars($colorName) . '"' . ($currentValue === $colorName ? ' selected="selected"' : '') . '>' . htmlspecialchars($colorName) . '</option>';
355  }
356  // Compile selector box and return result:
357  return '<p class="c-head">' . $this->getLanguageService()->getLL('colorpicker_fromList', true) . '</p>
358  <select class="t3js-colorpicker-selector">' . implode(LF, $opt) . '</select><br />';
359  }
360 
366  public function colorImage()
367  {
368  // Handling color-picker image if any:
369  if (!$this->imageError) {
370  if ($this->pickerImage) {
371  if (GeneralUtility::_POST('coords_x')) {
373  $image = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Imaging\GraphicalFunctions::class);
374  $this->colorValue = '#' . $this->getIndex($image->imageCreateFromFile($this->pickerImage), GeneralUtility::_POST('coords_x'), GeneralUtility::_POST('coords_y'));
375  }
376  $pickerFormImage = '
377  <p class="c-head">' . $this->getLanguageService()->getLL('colorpicker_fromImage', true) . '</p>
378  <input type="image" src="../' . \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix($this->pickerImage) . '" name="coords" style="cursor:crosshair;" /><br />';
379  } else {
380  $pickerFormImage = '';
381  }
382  } else {
383  $pickerFormImage = '
384  <p class="c-head">' . htmlspecialchars($this->imageError) . '</p>';
385  }
386  return $pickerFormImage;
387  }
388 
399  public function getIndex($im, $x, $y)
400  {
401  $rgb = ImageColorAt($im, $x, $y);
402  $colorRgb = imagecolorsforindex($im, $rgb);
403  $index['r'] = dechex($colorRgb['red']);
404  $index['g'] = dechex($colorRgb['green']);
405  $index['b'] = dechex($colorRgb['blue']);
406  $hexValue = array();
407  foreach ($index as $value) {
408  if (strlen($value) === 1) {
409  $hexValue[] = strtoupper('0' . $value);
410  } else {
411  $hexValue[] = strtoupper($value);
412  }
413  }
414  $hex = implode('', $hexValue);
415  return $hex;
416  }
417 
424  protected function areFieldChangeFunctionsValid()
425  {
426  return $this->fieldChangeFunc && $this->fieldChangeFuncHash && $this->fieldChangeFuncHash === GeneralUtility::hmac($this->fieldChangeFunc);
427  }
428 
432  protected function getPageRenderer()
433  {
434  return GeneralUtility::makeInstance(PageRenderer::class);
435  }
436 }