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