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