TYPO3 CMS  TYPO3_6-2
ColorpickerController.php
Go to the documentation of this file.
1 <?php
3 
19 
28 
29  // GET vars:
30  // Wizard parameters, coming from TCEforms linking to the wizard.
34  public $P;
35 
36  // Value of the current color picked.
40  public $colorValue;
41 
42  // Serialized functions for changing the field... Necessary to call when the value is transferred to the TCEform since the form might need to do internal processing. Otherwise the value is simply not be saved.
47 
49 
50  // Form name (from opener script)
54  public $fieldName;
55 
56  // Field name (from opener script)
60  public $formName;
61 
62  // ID of element in opener script for which to set color.
66  public $md5ID;
67 
68  // Internal: If FALSE, a frameset is rendered, if TRUE the content of the picker script.
72  public $showPicker;
73 
74  // Static:
78  public $HTMLcolorList = 'aqua,black,blue,fuchsia,gray,green,lime,maroon,navy,olive,purple,red,silver,teal,yellow,white';
79 
80  // Internal:
84  public $pickerImage = '';
85 
86  // Error message if image not found.
90  public $imageError = '';
91 
98  public $doc;
99 
100  // Accumulated content.
104  public $content;
105 
109  public function __construct() {
110  $GLOBALS['LANG']->includeLLFile('EXT:lang/locallang_wizards.xlf');
111  $GLOBALS['SOBE'] = $this;
112 
113  $this->init();
114  }
115 
121  protected function init() {
122  // Setting GET vars (used in frameset script):
123  $this->P = GeneralUtility::_GP('P');
124  // Setting GET vars (used in colorpicker script):
125  $this->colorValue = GeneralUtility::_GP('colorValue');
126  $this->fieldChangeFunc = GeneralUtility::_GP('fieldChangeFunc');
127  $this->fieldChangeFuncHash = GeneralUtility::_GP('fieldChangeFuncHash');
128  $this->fieldName = GeneralUtility::_GP('fieldName');
129  $this->formName = GeneralUtility::_GP('formName');
130  $this->md5ID = GeneralUtility::_GP('md5ID');
131  $this->exampleImg = GeneralUtility::_GP('exampleImg');
132  // Resolving image (checking existence etc.)
133  $this->imageError = '';
134  if ($this->exampleImg) {
135  $this->pickerImage = GeneralUtility::getFileAbsFileName($this->exampleImg, 1, 1);
136  if (!$this->pickerImage || !@is_file($this->pickerImage)) {
137  $this->imageError = 'ERROR: The image, "' . $this->exampleImg . '", could not be found!';
138  }
139  }
140  $update = '';
141  if ($this->areFieldChangeFunctionsValid()) {
142  // Setting field-change functions:
143  $fieldChangeFuncArr = unserialize($this->fieldChangeFunc);
144  unset($fieldChangeFuncArr['alert']);
145  foreach ($fieldChangeFuncArr as $v) {
146  $update .= '
147  parent.opener.' . $v;
148  }
149  }
150  // Initialize document object:
151  $this->doc = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Template\\DocumentTemplate');
152  $this->doc->backPath = $GLOBALS['BACK_PATH'];
153  $this->doc->JScode = $this->doc->wrapScriptTags('
154  function checkReference() { //
155  if (parent.opener && parent.opener.document && parent.opener.document.' . $this->formName . ' && parent.opener.document.' . $this->formName . '["' . $this->fieldName . '"]) {
156  return parent.opener.document.' . $this->formName . '["' . $this->fieldName . '"];
157  } else {
158  close();
159  }
160  }
161  function changeBGcolor(color) { // Changes the color in the table sample back in the TCEform.
162  if (parent.opener.document.layers) {
163  parent.opener.document.layers["' . $this->md5ID . '"].bgColor = color;
164  } else if (parent.opener.document.all) {
165  parent.opener.document.all["' . $this->md5ID . '"].style.background = color;
166  } else if (parent.opener.document.getElementById && parent.opener.document.getElementById("' . $this->md5ID . '")) {
167  parent.opener.document.getElementById("' . $this->md5ID . '").bgColor = color;
168  }
169  }
170  function setValue(input) { //
171  var field = checkReference();
172  if (field) {
173  field.value = input;
174  ' . $update . '
175  changeBGcolor(input);
176  }
177  }
178  function getValue() { //
179  var field = checkReference();
180  return field.value;
181  }
182  ');
183  // Start page:
184  $this->content .= $this->doc->startPage($GLOBALS['LANG']->getLL('colorpicker_title'));
185  }
186 
192  public function main() {
193  // Show frameset by default:
194  if (!GeneralUtility::_GP('showPicker')) {
195  $this->frameSet();
196  } else {
197  // Putting together the items into a form:
198  $content = '
199  <form name="colorform" method="post" action="' . htmlspecialchars(BackendUtility::getModuleUrl('wizard_colorpicker')) . '">
200  ' . $this->colorMatrix() . '
201  ' . $this->colorList() . '
202  ' . $this->colorImage() . '
203 
204  <!-- Value box: -->
205  <p class="c-head">' . $GLOBALS['LANG']->getLL('colorpicker_colorValue', TRUE) . '</p>
206  <table border="0" cellpadding="0" cellspacing="3">
207  <tr>
208  <td><input type="text" ' . $this->doc->formWidth(7) . ' maxlength="10" name="colorValue" value="' . htmlspecialchars($this->colorValue) . '" /></td>
209  <td style="background-color:' . htmlspecialchars($this->colorValue) . '; border: 1px solid black;">&nbsp;<span style="color: black;">' . $GLOBALS['LANG']->getLL('colorpicker_black', TRUE) . '</span>&nbsp;<span style="color: white;">' . $GLOBALS['LANG']->getLL('colorpicker_white', TRUE) . '</span>&nbsp;</td>
210  <td><input type="submit" name="save_close" value="' . $GLOBALS['LANG']->getLL('colorpicker_setClose', TRUE) . '" /></td>
211  </tr>
212  </table>
213 
214  <!-- Hidden fields with values that has to be kept constant -->
215  <input type="hidden" name="showPicker" value="1" />
216  <input type="hidden" name="fieldChangeFunc" value="' . htmlspecialchars($this->fieldChangeFunc) . '" />
217  <input type="hidden" name="fieldChangeFuncHash" value="' . htmlspecialchars($this->fieldChangeFuncHash) . '" />
218  <input type="hidden" name="fieldName" value="' . htmlspecialchars($this->fieldName) . '" />
219  <input type="hidden" name="formName" value="' . htmlspecialchars($this->formName) . '" />
220  <input type="hidden" name="md5ID" value="' . htmlspecialchars($this->md5ID) . '" />
221  <input type="hidden" name="exampleImg" value="' . htmlspecialchars($this->exampleImg) . '" />
222  </form>';
223  // If the save/close button is clicked, then close:
224  if (GeneralUtility::_GP('save_close')) {
225  $content .= $this->doc->wrapScriptTags('
226  setValue(' . GeneralUtility::quoteJSvalue($this->colorValue) . ');
227  parent.close();
228  ');
229  }
230  // Output:
231  $this->content .= $this->doc->section($GLOBALS['LANG']->getLL('colorpicker_title'), $content, 0, 1);
232  }
233  }
234 
241  public function printContent() {
242  $this->content .= $this->doc->endPage();
243  $this->content = $this->doc->insertStylesAndJS($this->content);
244  echo $this->content;
245  }
246 
255  public function frameSet() {
256  // Set doktype:
257  $GLOBALS['TBE_TEMPLATE']->docType = 'xhtml_frames';
258  $GLOBALS['TBE_TEMPLATE']->JScode = $GLOBALS['TBE_TEMPLATE']->wrapScriptTags('
259  if (!window.opener) {
260  alert("ERROR: Sorry, no link to main window... Closing");
261  close();
262  }
263  ');
264  $this->content = $GLOBALS['TBE_TEMPLATE']->startPage($GLOBALS['LANG']->getLL('colorpicker_title'));
265  // URL for the inner main frame:
267  'wizard_colorpicker',
268  array(
269  'showPicker' => 1,
270  'colorValue' => $this->P['currentValue'],
271  'fieldName' => $this->P['itemName'],
272  'formName' => $this->P['formName'],
273  'exampleImg' => $this->P['exampleImg'],
274  'md5ID' => $this->P['md5ID'],
275  'fieldChangeFunc' => serialize($this->P['fieldChangeFunc']),
276  'fieldChangeFuncHash' => $this->P['fieldChangeFuncHash'],
277  )
278  );
279  $this->content .= '
280  <frameset rows="*,1" framespacing="0" frameborder="0" border="0">
281  <frame name="content" src="' . htmlspecialchars($url) . '" marginwidth="0" marginheight="0" frameborder="0" scrolling="auto" noresize="noresize" />
282  <frame name="menu" src="dummy.php" marginwidth="0" marginheight="0" frameborder="0" scrolling="no" noresize="noresize" />
283  </frameset>
284  ';
285  $this->content .= '
286 </html>';
287  }
288 
289  /************************************
290  *
291  * Rendering of various color selectors
292  *
293  ************************************/
300  public function colorMatrix() {
301  $steps = 51;
302  // Get colors:
303  $color = array();
304  for ($rr = 0; $rr < 256; $rr += $steps) {
305  for ($gg = 0; $gg < 256; $gg += $steps) {
306  for ($bb = 0; $bb < 256; $bb += $steps) {
307  $color[] = '#' . substr(('0' . dechex($rr)), -2) . substr(('0' . dechex($gg)), -2) . substr(('0' . dechex($bb)), -2);
308  }
309  }
310  }
311  // Traverse colors:
312  $columns = 24;
313  $rows = 0;
314  $tRows = array();
315  while (isset($color[$columns * $rows])) {
316  $tCells = array();
317  for ($i = 0; $i < $columns; $i++) {
318  $tCells[] = '
319  <td bgcolor="' . $color[($columns * $rows + $i)] . '" onclick="document.colorform.colorValue.value = \'' . $color[($columns * $rows + $i)] . '\'; document.colorform.submit();" title="' . $color[($columns * $rows + $i)] . '">&nbsp;&nbsp;</td>';
320  }
321  $tRows[] = '
322  <tr>' . implode('', $tCells) . '
323  </tr>';
324  $rows++;
325  }
326  $table = '
327  <p class="c-head">' . $GLOBALS['LANG']->getLL('colorpicker_fromMatrix', TRUE) . '</p>
328  <table border="0" cellpadding="1" cellspacing="1" style="width:100%; border: 1px solid black; cursor:crosshair;">' . implode('', $tRows) . '
329  </table>';
330  return $table;
331  }
332 
339  public function colorList() {
340  // Initialize variables:
341  $colors = explode(',', $this->HTMLcolorList);
342  $currentValue = strtolower($this->colorValue);
343  $opt = array();
344  $opt[] = '<option value=""></option>';
345  // Traverse colors, making option tags for selector box.
346  foreach ($colors as $colorName) {
347  $opt[] = '<option style="background-color: ' . $colorName . ';" value="' . htmlspecialchars($colorName) . '"' . ($currentValue == $colorName ? ' selected="selected"' : '') . '>' . htmlspecialchars($colorName) . '</option>';
348  }
349  // Compile selector box and return result:
350  $output = '
351  <p class="c-head">' . $GLOBALS['LANG']->getLL('colorpicker_fromList', TRUE) . '</p>
352  <select onchange="document.colorform.colorValue.value = this.options[this.selectedIndex].value; document.colorform.submit(); return false;">
353  ' . implode('
354  ', $opt) . '
355  </select><br />';
356  return $output;
357  }
358 
365  public function colorImage() {
366  // Handling color-picker image if any:
367  if (!$this->imageError) {
368  if ($this->pickerImage) {
369  if (GeneralUtility::_POST('coords_x')) {
370  $this->colorValue = '#' . $this->getIndex(\TYPO3\CMS\Core\Imaging\GraphicalFunctions::imageCreateFromFile($this->pickerImage), GeneralUtility::_POST('coords_x'), GeneralUtility::_POST('coords_y'));
371  }
372  $pickerFormImage = '
373  <p class="c-head">' . $GLOBALS['LANG']->getLL('colorpicker_fromImage', TRUE) . '</p>
374  <input type="image" src="../' . \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix($this->pickerImage) . '" name="coords" style="cursor:crosshair;" /><br />';
375  } else {
376  $pickerFormImage = '';
377  }
378  } else {
379  $pickerFormImage = '
380  <p class="c-head">' . htmlspecialchars($this->imageError) . '</p>';
381  }
382  return $pickerFormImage;
383  }
384 
396  public function getIndex($im, $x, $y) {
397  $rgb = ImageColorAt($im, $x, $y);
398  $colorrgb = imagecolorsforindex($im, $rgb);
399  $index['r'] = dechex($colorrgb['red']);
400  $index['g'] = dechex($colorrgb['green']);
401  $index['b'] = dechex($colorrgb['blue']);
402  foreach ($index as $value) {
403  if (strlen($value) == 1) {
404  $hexvalue[] = strtoupper('0' . $value);
405  } else {
406  $hexvalue[] = strtoupper($value);
407  }
408  }
409  $hex = implode('', $hexvalue);
410  return $hex;
411  }
412 
419  protected function areFieldChangeFunctionsValid() {
420  return $this->fieldChangeFunc && $this->fieldChangeFuncHash && $this->fieldChangeFuncHash === GeneralUtility::hmac($this->fieldChangeFunc);
421  }
422 
423 }
static getModuleUrl($moduleName, $urlParameters=array(), $backPathOverride=FALSE, $returnAbsoluteUrl=FALSE)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
static getFileAbsFileName($filename, $onlyRelative=TRUE, $relToTYPO3_mainDir=FALSE)