[TASK] Fix CGL violations against CharacterAfterPHPClosingTag
[Packages/TYPO3.CMS.git] / typo3 / wizard_colorpicker.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Colorpicker wizard
29 *
30 * Revised for TYPO3 3.7 May/2004 by Kasper Skårhøj
31 *
32 * @author Mathias Schreiber <schreiber@wmdb.de>
33 * @author Peter Kühn <peter@kuehn.com>
34 * @author Kasper Skårhøj <typo3@typo3.com>
35 */
36
37 $GLOBALS['BACK_PATH'] = '';
38 require('init.php');
39 $GLOBALS['LANG']->includeLLFile('EXT:lang/locallang_wizards.xml');
40
41 /**
42 * Script Class for colorpicker wizard
43 *
44 * @author Mathias Schreiber <schreiber@wmdb.de>
45 * @author Peter Kühn <peter@kuehn.com>
46 * @author Kasper Skårhøj <typo3@typo3.com>
47 * @package TYPO3
48 * @subpackage core
49 */
50 class SC_wizard_colorpicker {
51
52 // GET vars:
53 // Wizard parameters, coming from TCEforms linking to the wizard.
54 var $P;
55 // Value of the current color picked.
56 var $colorValue;
57 // 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.
58 var $fieldChangeFunc;
59 protected $fieldChangeFuncHash;
60 // Form name (from opener script)
61 var $fieldName;
62 // Field name (from opener script)
63 var $formName;
64 // ID of element in opener script for which to set color.
65 var $md5ID;
66 // Internal: If FALSE, a frameset is rendered, if TRUE the content of the picker script.
67 var $showPicker;
68
69 // Static:
70 var $HTMLcolorList = 'aqua,black,blue,fuchsia,gray,green,lime,maroon,navy,olive,purple,red,silver,teal,yellow,white';
71
72 // Internal:
73 var $pickerImage = '';
74 // Error message if image not found.
75 var $imageError = '';
76
77 /**
78 * Document template object
79 *
80 * @var smallDoc
81 */
82 var $doc;
83 // Accumulated content.
84 var $content;
85
86 /**
87 * Initialises the Class
88 *
89 * @return void
90 */
91 function init() {
92 // Setting GET vars (used in frameset script):
93 $this->P = t3lib_div::_GP('P', 1);
94
95 // Setting GET vars (used in colorpicker script):
96 $this->colorValue = t3lib_div::_GP('colorValue');
97 $this->fieldChangeFunc = t3lib_div::_GP('fieldChangeFunc');
98 $this->fieldChangeFuncHash = t3lib_div::_GP('fieldChangeFuncHash');
99 $this->fieldName = t3lib_div::_GP('fieldName');
100 $this->formName = t3lib_div::_GP('formName');
101 $this->md5ID = t3lib_div::_GP('md5ID');
102 $this->exampleImg = t3lib_div::_GP('exampleImg');
103
104 // Resolving image (checking existence etc.)
105 $this->imageError = '';
106 if ($this->exampleImg) {
107 $this->pickerImage = t3lib_div::getFileAbsFileName($this->exampleImg, 1, 1);
108 if (!$this->pickerImage || !@is_file($this->pickerImage)) {
109 $this->imageError = 'ERROR: The image, "'.$this->exampleImg.'", could not be found!';
110 }
111 }
112
113 // Setting field-change functions:
114 $fieldChangeFuncArr = unserialize($this->fieldChangeFunc);
115 $update = '';
116 if ($this->areFieldChangeFunctionsValid()) {
117 unset($fieldChangeFuncArr['alert']);
118 foreach ($fieldChangeFuncArr as $v) {
119 $update.= '
120 parent.opener.'.$v;
121 }
122 }
123
124 // Initialize document object:
125 $this->doc = t3lib_div::makeInstance('smallDoc');
126 $this->doc->backPath = $GLOBALS['BACK_PATH'];
127 $this->doc->JScode = $this->doc->wrapScriptTags('
128 function checkReference() { //
129 if (parent.opener && parent.opener.document && parent.opener.document.'.$this->formName.' && parent.opener.document.'.$this->formName.'["'.$this->fieldName.'"]) {
130 return parent.opener.document.'.$this->formName.'["'.$this->fieldName.'"];
131 } else {
132 close();
133 }
134 }
135 function changeBGcolor(color) { // Changes the color in the table sample back in the TCEform.
136 if (parent.opener.document.layers) {
137 parent.opener.document.layers["'.$this->md5ID.'"].bgColor = color;
138 } else if (parent.opener.document.all) {
139 parent.opener.document.all["'.$this->md5ID.'"].style.background = color;
140 } else if (parent.opener.document.getElementById && parent.opener.document.getElementById("'.$this->md5ID.'")) {
141 parent.opener.document.getElementById("'.$this->md5ID.'").bgColor = color;
142 }
143 }
144 function setValue(input) { //
145 var field = checkReference();
146 if (field) {
147 field.value = input;
148 '.$update.'
149 changeBGcolor(input);
150 }
151 }
152 function getValue() { //
153 var field = checkReference();
154 return field.value;
155 }
156 ');
157
158 // Start page:
159 $this->content.=$this->doc->startPage($GLOBALS['LANG']->getLL('colorpicker_title'));
160 }
161
162 /**
163 * Main Method, rendering either colorpicker or frameset depending on ->showPicker
164 *
165 * @return void
166 */
167 function main() {
168 // Show frameset by default:
169 if (!t3lib_div::_GP('showPicker')) {
170 $this->frameSet();
171 } else {
172
173 // Putting together the items into a form:
174 $content = '
175 <form name="colorform" method="post" action="wizard_colorpicker.php">
176 '.$this->colorMatrix().'
177 '.$this->colorList().'
178 '.$this->colorImage().'
179
180 <!-- Value box: -->
181 <p class="c-head">'.$GLOBALS['LANG']->getLL('colorpicker_colorValue', 1).'</p>
182 <table border="0" cellpadding="0" cellspacing="3">
183 <tr>
184 <td><input type="text" '.$this->doc->formWidth(7).' maxlength="10" name="colorValue" value="'.htmlspecialchars($this->colorValue).'" /></td>
185 <td style="background-color:'.htmlspecialchars($this->colorValue).'; border: 1px solid black;">&nbsp;<span style="color: black;">'.$GLOBALS['LANG']->getLL('colorpicker_black', 1).'</span>&nbsp;<span style="color: white;">'.$GLOBALS['LANG']->getLL('colorpicker_white', 1).'</span>&nbsp;</td>
186 <td><input type="submit" name="save_close" value="'.$GLOBALS['LANG']->getLL('colorpicker_setClose', 1).'" /></td>
187 </tr>
188 </table>
189
190 <!-- Hidden fields with values that has to be kept constant -->
191 <input type="hidden" name="showPicker" value="1" />
192 <input type="hidden" name="fieldChangeFunc" value="'.htmlspecialchars($this->fieldChangeFunc).'" />
193 <input type="hidden" name="fieldChangeFuncHash" value="'.htmlspecialchars($this->fieldChangeFuncHash).'" />
194 <input type="hidden" name="fieldName" value="'.htmlspecialchars($this->fieldName).'" />
195 <input type="hidden" name="formName" value="'.htmlspecialchars($this->formName).'" />
196 <input type="hidden" name="md5ID" value="'.htmlspecialchars($this->md5ID).'" />
197 <input type="hidden" name="exampleImg" value="'.htmlspecialchars($this->exampleImg).'" />
198 </form>';
199
200 // If the save/close button is clicked, then close:
201 if (t3lib_div::_GP('save_close')) {
202 $content .= $this->doc->wrapScriptTags('
203 setValue(\''.$this->colorValue.'\');
204 parent.close();
205 ');
206 }
207
208 // Output:
209 $this->content.=$this->doc->section($GLOBALS['LANG']->getLL('colorpicker_title'), $content, 0, 1);
210 }
211 }
212
213 /**
214 * Returnes the sourcecode to the browser
215 *
216 * @return void
217 */
218 function printContent() {
219 $this->content.= $this->doc->endPage();
220 $this->content = $this->doc->insertStylesAndJS($this->content);
221 echo $this->content;
222 }
223
224 /**
225 * Returnes a frameset so our JavaScript Reference isn't lost
226 * Took some brains to figure this one out ;-)
227 * If Peter wouldn't have been I would've gone insane...
228 *
229 * @return void
230 */
231 function frameSet() {
232 // Set doktype:
233 $GLOBALS['TBE_TEMPLATE']->docType = 'xhtml_frames';
234 $GLOBALS['TBE_TEMPLATE']->JScode = $GLOBALS['TBE_TEMPLATE']->wrapScriptTags('
235 if (!window.opener) {
236 alert("ERROR: Sorry, no link to main window... Closing");
237 close();
238 }
239 ');
240
241 $this->content = $GLOBALS['TBE_TEMPLATE']->startPage($GLOBALS['LANG']->getLL('colorpicker_title'));
242
243 // URL for the inner main frame:
244 $url = 'wizard_colorpicker.php?showPicker=1'.
245 '&colorValue='.rawurlencode($this->P['currentValue']).
246 '&fieldName='.rawurlencode($this->P['itemName']).
247 '&formName='.rawurlencode($this->P['formName']).
248 '&exampleImg='.rawurlencode($this->P['exampleImg']).
249 '&md5ID='.rawurlencode($this->P['md5ID']).
250 '&fieldChangeFunc='.rawurlencode(serialize($this->P['fieldChangeFunc'])) .
251 '&fieldChangeFuncHash=' . $this->P['fieldChangeFuncHash'];
252
253 $this->content.='
254 <frameset rows="*,1" framespacing="0" frameborder="0" border="0">
255 <frame name="content" src="'.htmlspecialchars($url).'" marginwidth="0" marginheight="0" frameborder="0" scrolling="auto" noresize="noresize" />
256 <frame name="menu" src="dummy.php" marginwidth="0" marginheight="0" frameborder="0" scrolling="no" noresize="noresize" />
257 </frameset>
258 ';
259
260 $this->content.='
261 </html>';
262 }
263
264 /************************************
265 *
266 * Rendering of various color selectors
267 *
268 ************************************/
269
270 /**
271 * Creates a color matrix table
272 *
273 * @return void
274 */
275 function colorMatrix() {
276 $steps = 51;
277
278 // Get colors:
279 $color = array();
280
281 for ($rr = 0; $rr < 256; $rr+=$steps) {
282 for ($gg = 0; $gg < 256; $gg+=$steps) {
283 for ($bb = 0; $bb < 256; $bb+=$steps) {
284 $color[] = '#'.
285 substr('0'.dechex($rr), -2).
286 substr('0'.dechex($gg), -2).
287 substr('0'.dechex($bb), -2);
288 }
289 }
290 }
291
292 // Traverse colors:
293 $columns = 24;
294
295 $rows = 0;
296 $tRows = array();
297 while (isset($color[$columns*$rows])) {
298 $tCells = array();
299 for ($i = 0; $i < $columns; $i++) {
300 $tCells[] = '
301 <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>';
302 }
303 $tRows[] = '
304 <tr>'.implode('', $tCells).'
305 </tr>';
306 $rows++;
307 }
308
309 $table = '
310 <p class="c-head">'.$GLOBALS['LANG']->getLL('colorpicker_fromMatrix', 1).'</p>
311 <table border="0" cellpadding="1" cellspacing="1" style="width:100%; border: 1px solid black; cursor:crosshair;">'.implode('', $tRows).'
312 </table>';
313
314 return $table;
315 }
316
317 /**
318 * Creates a selector box with all HTML color names.
319 *
320 * @return void
321 */
322 function colorList() {
323 // Initialize variables:
324 $colors = explode(',', $this->HTMLcolorList);
325 $currentValue = strtolower($this->colorValue);
326 $opt = array();
327 $opt[] = '<option value=""></option>';
328
329 // Traverse colors, making option tags for selector box.
330 foreach ($colors as $colorName) {
331 $opt[] = '<option style="background-color: '.$colorName.';" value="'.htmlspecialchars($colorName).'"'.($currentValue==$colorName ? ' selected="selected"' : '').'>'.htmlspecialchars($colorName).'</option>';
332 }
333
334 // Compile selector box and return result:
335 $output = '
336 <p class="c-head">'.$GLOBALS['LANG']->getLL('colorpicker_fromList', 1).'</p>
337 <select onchange="document.colorform.colorValue.value = this.options[this.selectedIndex].value; document.colorform.submit(); return false;">
338 ' . implode('
339 ', $opt) . '
340 </select><br />';
341
342 return $output;
343 }
344
345 /**
346 * Creates a color image selector
347 *
348 * @return void
349 */
350 function colorImage() {
351 // Handling color-picker image if any:
352 if (!$this->imageError) {
353 if ($this->pickerImage) {
354 if (t3lib_div::_POST('coords_x')) {
355 $this->colorValue = '#'.$this->getIndex(t3lib_stdgraphic::imageCreateFromFile($this->pickerImage), t3lib_div::_POST('coords_x'), t3lib_div::_POST('coords_y'));
356 }
357 $pickerFormImage = '
358 <p class="c-head">'.$GLOBALS['LANG']->getLL('colorpicker_fromImage', 1).'</p>
359 <input type="image" src="../'.substr($this->pickerImage, strlen(PATH_site)).'" name="coords" style="cursor:crosshair;" /><br />';
360 } else {
361 $pickerFormImage = '';
362 }
363 } else {
364 $pickerFormImage = '
365 <p class="c-head">'.htmlspecialchars($this->imageError).'</p>';
366 }
367
368 return $pickerFormImage;
369 }
370
371 /**
372 * Gets the HTML (Hex) Color Code for the selected pixel of an image
373 * This method handles the correct imageResource no matter what format
374 *
375 * @param pointer $im Valid ImageResource returned by t3lib_stdgraphic::imageCreateFromFile
376 * @param integer $x X-Coordinate of the pixel that should be checked
377 * @param integer $y Y-Coordinate of the pixel that should be checked
378 * @return string HEX RGB value for color
379 * @see colorImage()
380 */
381 function getIndex($im, $x, $y) {
382 $rgb = ImageColorAt($im, $x, $y);
383 $colorrgb = imagecolorsforindex($im, $rgb);
384 $index['r'] = dechex($colorrgb['red']);
385 $index['g'] = dechex($colorrgb['green']);
386 $index['b'] = dechex($colorrgb['blue']);
387 foreach ($index as $value) {
388 if (strlen($value) == 1) {
389 $hexvalue[] = strtoupper('0'.$value);
390 } else {
391 $hexvalue[] = strtoupper($value);
392 }
393 }
394 $hex = implode('', $hexvalue);
395 return $hex;
396 }
397
398 /**
399 * Determines whether submitted field change functions are valid
400 * and are coming from the system and not from an external abuse.
401 *
402 * @return boolean Whether the submitted field change functions are valid
403 */
404 protected function areFieldChangeFunctionsValid() {
405 return (
406 $this->fieldChangeFunc && $this->fieldChangeFuncHash
407 && $this->fieldChangeFuncHash === t3lib_div::hmac($this->fieldChangeFunc)
408 );
409 }
410 }
411
412 // Make instance:
413 $SOBE = t3lib_div::makeInstance('SC_wizard_colorpicker');
414 $SOBE->init();
415 $SOBE->main();
416 $SOBE->printContent();
417
418 ?>