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