TYPO3 API  SVNRelease
wizard_colorpicker.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *  A copy is found in the textfile GPL.txt and important notices to the license
00017 *  from the author is found in LICENSE.txt distributed with these scripts.
00018 *
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 /**
00028  * Colorpicker wizard
00029  *
00030  * $Id $
00031  * Revised for TYPO3 3.7 May/2004 by Kasper Skårhøj
00032  *
00033  * @author  Mathias Schreiber <schreiber@wmdb.de>
00034  * @author  Peter Kühn <peter@kuehn.com>
00035  * @author  Kasper Skårhøj <typo3@typo3.com>
00036  */
00037 /**
00038  * [CLASS/FUNCTION INDEX of SCRIPT]
00039  *
00040  *
00041  *
00042  *   75: class SC_wizard_colorpicker
00043  *  103:     function init()
00044  *  182:     function main()
00045  *  233:     function printContent()
00046  *  246:     function frameSet()
00047  *
00048  *              SECTION: Rendering of various color selectors
00049  *  305:     function colorMatrix()
00050  *  354:     function colorList()
00051  *  384:     function colorImage()
00052  *  417:     function getIndex($im,$x,$y)
00053  *
00054  * TOTAL FUNCTIONS: 8
00055  * (This index is automatically created/updated by the extension "extdeveval")
00056  *
00057  */
00058 
00059 
00060 $BACK_PATH = '';
00061 require('init.php');
00062 require('template.php');
00063 $LANG->includeLLFile('EXT:lang/locallang_wizards.xml');
00064 
00065 /**
00066  * Script Class for colorpicker wizard
00067  *
00068  * @author  Mathias Schreiber <schreiber@wmdb.de>
00069  * @author  Peter Kühn <peter@kuehn.com>
00070  * @author  Kasper Skårhøj <typo3@typo3.com>
00071  * @package TYPO3
00072  * @subpackage core
00073  */
00074 class SC_wizard_colorpicker {
00075 
00076         // GET vars:
00077     var $P;             // Wizard parameters, coming from TCEforms linking to the wizard.
00078     var $colorValue;    // Value of the current color picked.
00079     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.
00080     protected $fieldChangeFuncHash;
00081     var $fieldName;     // Form name (from opener script)
00082     var $formName;      // Field name (from opener script)
00083     var $md5ID;         // ID of element in opener script for which to set color.
00084     var $showPicker;    // Internal: If false, a frameset is rendered, if true the content of the picker script.
00085 
00086         // Static:
00087     var $HTMLcolorList = "aqua,black,blue,fuchsia,gray,green,lime,maroon,navy,olive,purple,red,silver,teal,yellow,white";
00088 
00089         // Internal:
00090     var $pickerImage = '';
00091     var $imageError = '';       // Error message if image not found.
00092 
00093     /**
00094      * document template object
00095      *
00096      * @var smallDoc
00097      */
00098     var $doc;
00099     var $content;               // Accumulated content.
00100 
00101 
00102 
00103 
00104     /**
00105      * Initialises the Class
00106      *
00107      * @return  void
00108      */
00109     function init() {
00110         global $BACK_PATH, $LANG;
00111 
00112             // Setting GET vars (used in frameset script):
00113         $this->P = t3lib_div::_GP('P',1);
00114 
00115             // Setting GET vars (used in colorpicker script):
00116         $this->colorValue = t3lib_div::_GP('colorValue');
00117         $this->fieldChangeFunc = t3lib_div::_GP('fieldChangeFunc');
00118         $this->fieldChangeFuncHash = t3lib_div::_GP('fieldChangeFuncHash');
00119         $this->fieldName = t3lib_div::_GP('fieldName');
00120         $this->formName = t3lib_div::_GP('formName');
00121         $this->md5ID = t3lib_div::_GP('md5ID');
00122         $this->exampleImg = t3lib_div::_GP('exampleImg');
00123 
00124 
00125             // Resolving image (checking existence etc.)
00126         $this->imageError = '';
00127         if ($this->exampleImg)  {
00128             $this->pickerImage = t3lib_div::getFileAbsFileName($this->exampleImg,1,1);
00129             if (!$this->pickerImage || !@is_file($this->pickerImage))   {
00130                 $this->imageError = 'ERROR: The image, "'.$this->exampleImg.'", could not be found!';
00131             }
00132         }
00133 
00134             // Setting field-change functions:
00135         $fieldChangeFuncArr = unserialize($this->fieldChangeFunc);
00136         $update = '';
00137         if ($this->areFieldChangeFunctionsValid()) {
00138             unset($fieldChangeFuncArr['alert']);
00139             foreach($fieldChangeFuncArr as $v)  {
00140                 $update.= '
00141                 parent.opener.'.$v;
00142             }
00143         }
00144 
00145             // Initialize document object:
00146         $this->doc = t3lib_div::makeInstance('smallDoc');
00147         $this->doc->backPath = $BACK_PATH;
00148         $this->doc->JScode = $this->doc->wrapScriptTags('
00149             function checkReference()   {   //
00150                 if (parent.opener && parent.opener.document && parent.opener.document.'.$this->formName.' && parent.opener.document.'.$this->formName.'["'.$this->fieldName.'"])    {
00151                     return parent.opener.document.'.$this->formName.'["'.$this->fieldName.'"];
00152                 } else {
00153                     close();
00154                 }
00155             }
00156             function changeBGcolor(color) { // Changes the color in the table sample back in the TCEform.
00157                 if (parent.opener.document.layers)  {
00158                     parent.opener.document.layers["'.$this->md5ID.'"].bgColor = color;
00159                 } else if (parent.opener.document.all)  {
00160                     parent.opener.document.all["'.$this->md5ID.'"].style.background = color;
00161                 } else if (parent.opener.document.getElementById && parent.opener.document.getElementById("'.$this->md5ID.'"))  {
00162                     parent.opener.document.getElementById("'.$this->md5ID.'").bgColor = color;
00163                 }
00164             }
00165             function setValue(input)    {   //
00166                 var field = checkReference();
00167                 if (field)  {
00168                     field.value = input;
00169                     '.$update.'
00170                     changeBGcolor(input);
00171                 }
00172             }
00173             function getValue() {   //
00174                 var field = checkReference();
00175                 return field.value;
00176             }
00177         ');
00178 
00179             // Start page:
00180         $this->content.=$this->doc->startPage($LANG->getLL('colorpicker_title'));
00181     }
00182 
00183     /**
00184      * Main Method, rendering either colorpicker or frameset depending on ->showPicker
00185      *
00186      * @return  void
00187      */
00188     function main() {
00189         global $LANG;
00190 
00191         if(!t3lib_div::_GP('showPicker')) { // Show frameset by default:
00192             $this->frameSet();
00193         } else {
00194 
00195                 // Putting together the items into a form:
00196             $content = '
00197                 <form name="colorform" method="post" action="wizard_colorpicker.php">
00198                     '.$this->colorMatrix().'
00199                     '.$this->colorList().'
00200                     '.$this->colorImage().'
00201 
00202                         <!-- Value box: -->
00203                     <p class="c-head">'.$LANG->getLL('colorpicker_colorValue',1).'</p>
00204                     <table border="0" cellpadding="0" cellspacing="3">
00205                         <tr>
00206                             <td><input type="text" '.$this->doc->formWidth(7).' maxlength="10" name="colorValue" value="'.htmlspecialchars($this->colorValue).'" /></td>
00207                             <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>
00208                             <td><input type="submit" name="save_close" value="'.$LANG->getLL('colorpicker_setClose',1).'" /></td>
00209                         </tr>
00210                     </table>
00211 
00212                         <!-- Hidden fields with values that has to be kept constant -->
00213                     <input type="hidden" name="showPicker" value="1" />
00214                     <input type="hidden" name="fieldChangeFunc" value="'.htmlspecialchars($this->fieldChangeFunc).'" />
00215                     <input type="hidden" name="fieldChangeFuncHash" value="'.htmlspecialchars($this->fieldChangeFuncHash).'" />
00216                     <input type="hidden" name="fieldName" value="'.htmlspecialchars($this->fieldName).'" />
00217                     <input type="hidden" name="formName" value="'.htmlspecialchars($this->formName).'" />
00218                     <input type="hidden" name="md5ID" value="'.htmlspecialchars($this->md5ID).'" />
00219                     <input type="hidden" name="exampleImg" value="'.htmlspecialchars($this->exampleImg).'" />
00220                 </form>';
00221 
00222                 // If the save/close button is clicked, then close:
00223             if(t3lib_div::_GP('save_close')) {
00224                 $content.=$this->doc->wrapScriptTags('
00225                     setValue(\''.$this->colorValue.'\');
00226                     parent.close();
00227                 ');
00228             }
00229 
00230                 // Output:
00231             $this->content.=$this->doc->section($LANG->getLL('colorpicker_title'), $content, 0,1);
00232         }
00233     }
00234 
00235     /**
00236      * Returnes the sourcecode to the browser
00237      *
00238      * @return  void
00239      */
00240     function printContent() {
00241         $this->content.= $this->doc->endPage();
00242         $this->content = $this->doc->insertStylesAndJS($this->content);
00243         echo $this->content;
00244     }
00245 
00246     /**
00247      * Returnes a frameset so our JavaScript Reference isn't lost
00248      * Took some brains to figure this one out ;-)
00249      * If Peter wouldn't have been I would've gone insane...
00250      *
00251      * @return  void
00252      */
00253     function frameSet() {
00254         global $LANG;
00255 
00256             // Set doktype:
00257         $GLOBALS['TBE_TEMPLATE']->docType = 'xhtml_frames';
00258         $GLOBALS['TBE_TEMPLATE']->JScode = $GLOBALS['TBE_TEMPLATE']->wrapScriptTags('
00259                 if (!window.opener) {
00260                     alert("ERROR: Sorry, no link to main window... Closing");
00261                     close();
00262                 }
00263         ');
00264 
00265         $this->content = $GLOBALS['TBE_TEMPLATE']->startPage($LANG->getLL('colorpicker_title'));
00266 
00267             // URL for the inner main frame:
00268         $url = 'wizard_colorpicker.php?showPicker=1'.
00269                 '&colorValue='.rawurlencode($this->P['currentValue']).
00270                 '&fieldName='.rawurlencode($this->P['itemName']).
00271                 '&formName='.rawurlencode($this->P['formName']).
00272                 '&exampleImg='.rawurlencode($this->P['exampleImg']).
00273                 '&md5ID='.rawurlencode($this->P['md5ID']).
00274                 '&fieldChangeFunc='.rawurlencode(serialize($this->P['fieldChangeFunc'])) .
00275                 '&fieldChangeFuncHash=' . $this->P['fieldChangeFuncHash'];
00276 
00277         $this->content.='
00278             <frameset rows="*,1" framespacing="0" frameborder="0" border="0">
00279                 <frame name="content" src="'.htmlspecialchars($url).'" marginwidth="0" marginheight="0" frameborder="0" scrolling="auto" noresize="noresize" />
00280                 <frame name="menu" src="dummy.php" marginwidth="0" marginheight="0" frameborder="0" scrolling="no" noresize="noresize" />
00281             </frameset>
00282         ';
00283 
00284         $this->content.='
00285 </html>';
00286     }
00287 
00288 
00289 
00290 
00291 
00292 
00293 
00294 
00295 
00296 
00297 
00298 
00299 
00300 
00301 
00302     /************************************
00303      *
00304      * Rendering of various color selectors
00305      *
00306      ************************************/
00307 
00308     /**
00309      * Creates a color matrix table
00310      *
00311      * @return  void
00312      */
00313     function colorMatrix()  {
00314         global $LANG;
00315 
00316         $steps = 51;
00317 
00318             // Get colors:
00319         $color = array();
00320 
00321         for($rr=0;$rr<256;$rr+=$steps)  {
00322             for($gg=0;$gg<256;$gg+=$steps)  {
00323                 for($bb=0;$bb<256;$bb+=$steps)  {
00324                     $color[] = '#'.
00325                         substr('0'.dechex($rr),-2).
00326                         substr('0'.dechex($gg),-2).
00327                         substr('0'.dechex($bb),-2);
00328                 }
00329             }
00330         }
00331 
00332             // Traverse colors:
00333         $columns = 24;
00334 
00335         $rows = 0;
00336         $tRows = array();
00337         while(isset($color[$columns*$rows]))    {
00338             $tCells = array();
00339             for($i=0;$i<$columns;$i++)  {
00340                 $tCells[] = '
00341                     <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>';
00342             }
00343             $tRows[] = '
00344                 <tr>'.implode('',$tCells).'
00345                 </tr>';
00346             $rows++;
00347         }
00348 
00349         $table = '
00350             <p class="c-head">'.$LANG->getLL('colorpicker_fromMatrix',1).'</p>
00351             <table border="0" cellpadding="1" cellspacing="1" style="width:100%; border: 1px solid black; cursor:crosshair;">'.implode('',$tRows).'
00352             </table>';
00353 
00354         return $table;
00355     }
00356 
00357     /**
00358      * Creates a selector box with all HTML color names.
00359      *
00360      * @return  void
00361      */
00362     function colorList()    {
00363         global $LANG;
00364 
00365             // Initialize variables:
00366         $colors = explode(',',$this->HTMLcolorList);
00367         $currentValue = strtolower($this->colorValue);
00368         $opt = array();
00369         $opt[] = '<option value=""></option>';
00370 
00371             // Traverse colors, making option tags for selector box.
00372         foreach($colors as $colorName)  {
00373             $opt[] = '<option style="background-color: '.$colorName.';" value="'.htmlspecialchars($colorName).'"'.($currentValue==$colorName ? ' selected="selected"' : '').'>'.htmlspecialchars($colorName).'</option>';
00374         }
00375 
00376             // Compile selector box and return result:
00377         $output = '
00378             <p class="c-head">'.$LANG->getLL('colorpicker_fromList',1).'</p>
00379             <select onchange="document.colorform.colorValue.value = this.options[this.selectedIndex].value; document.colorform.submit(); return false;">
00380                 '.implode('
00381                 ',$opt).'
00382             </select><br />';
00383 
00384         return $output;
00385     }
00386 
00387     /**
00388      * Creates a color image selector
00389      *
00390      * @return  void
00391      */
00392     function colorImage()   {
00393         global $LANG;
00394 
00395             // Handling color-picker image if any:
00396         if (!$this->imageError) {
00397             if ($this->pickerImage) {
00398                 if(t3lib_div::_POST('coords_x')) {
00399                     $this->colorValue = '#'.$this->getIndex(t3lib_stdgraphic::imageCreateFromFile($this->pickerImage),t3lib_div::_POST('coords_x'),t3lib_div::_POST('coords_y'));
00400                 }
00401                 $pickerFormImage = '
00402                 <p class="c-head">'.$LANG->getLL('colorpicker_fromImage',1).'</p>
00403                 <input type="image" src="../'.substr($this->pickerImage,strlen(PATH_site)).'" name="coords" style="cursor:crosshair;" /><br />';
00404             } else {
00405                 $pickerFormImage = '';
00406             }
00407         } else {
00408             $pickerFormImage = '
00409             <p class="c-head">'.htmlspecialchars($this->imageError).'</p>';
00410         }
00411 
00412         return $pickerFormImage;
00413     }
00414 
00415     /**
00416      * Gets the HTML (Hex) Color Code for the selected pixel of an image
00417      * This method handles the correct imageResource no matter what format
00418      *
00419      * @param   pointer     Valid ImageResource returned by t3lib_stdgraphic::imageCreateFromFile
00420      * @param   integer     X-Coordinate of the pixel that should be checked
00421      * @param   integer     Y-Coordinate of the pixel that should be checked
00422      * @return  string      HEX RGB value for color
00423      * @see colorImage()
00424      */
00425     function getIndex($im,$x,$y) {
00426         $rgb = ImageColorAt($im, $x, $y);
00427         $colorrgb = imagecolorsforindex($im,$rgb);
00428         $index['r'] = dechex($colorrgb['red']);
00429         $index['g'] = dechex($colorrgb['green']);
00430         $index['b'] = dechex($colorrgb['blue']);
00431         foreach ($index as $value) {
00432             if(strlen($value) == 1) {
00433                 $hexvalue[] = strtoupper('0'.$value);
00434             } else {
00435                 $hexvalue[] = strtoupper($value);
00436             }
00437         }
00438         $hex = implode('',$hexvalue);
00439         return $hex;
00440     }
00441 
00442     /**
00443      * Determines whether submitted field change functions are valid
00444      * and are coming from the system and not from an external abuse.
00445      *
00446      * @return boolean Whether the submitted field change functions are valid
00447      */
00448     protected function areFieldChangeFunctionsValid() {
00449         return (
00450             $this->fieldChangeFunc && $this->fieldChangeFuncHash
00451             && $this->fieldChangeFuncHash == t3lib_div::hmac($this->fieldChangeFunc)
00452         );
00453     }
00454 }
00455 
00456 
00457 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/wizard_colorpicker.php'])) {
00458     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/wizard_colorpicker.php']);
00459 }
00460 
00461 
00462 
00463 // Make instance:
00464 $SOBE = t3lib_div::makeInstance('SC_wizard_colorpicker');
00465 $SOBE->init();
00466 $SOBE->main();
00467 $SOBE->printContent();
00468 
00469 ?>