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