Tons of changes made to the CORE. All scripts has more a less been modified. Primaril...
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_iconworks.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2003 Kasper Skaarhoj (kasper@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 * Contains class for icon generation in the backend
29 *
30 * $Id$
31 * Revised for TYPO3 3.6 July/2003 by Kasper Skaarhoj
32 * XHTML compliant
33 *
34 * @author Kasper Skaarhoj <kasper@typo3.com>
35 */
36 /**
37 * [CLASS/FUNCTION INDEX of SCRIPT]
38 *
39 *
40 *
41 * 85: class t3lib_iconWorks
42 * 99: function getIconImage($table,$row=array(),$backPath,$params='',$shaded=0)
43 * 116: function getIcon($table,$row=array(),$shaded=0)
44 * 198: function skinImg($backPath,$src,$wHattribs='',$outputMode=0)
45 *
46 * SECTION: Other functions
47 * 267: function makeIcon($iconfile,$mode, $user, $protectSection=0,$absFile='')
48 * 370: function imagecopyresized(&$im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h)
49 * 403: function imagecreatefrom($file)
50 * 420: function imagemake($im, $path)
51 *
52 * TOTAL FUNCTIONS: 7
53 * (This index is automatically created/updated by the extension "extdeveval")
54 *
55 */
56
57
58
59
60
61
62
63
64
65
66 /**
67 * Icon generation, backend
68 * This library has functions that returns - and if necessary creates - the icon for an element in TYPO3
69 *
70 * Expects global vars:
71 * - $BACK_PATH
72 * - PATH_typo3
73 * - $TCA, $PAGES_TYPES
74 *
75 *
76 * Notes:
77 * These functions are strongly related to the interface of TYPO3.
78 * The class is included in eg. init.php
79 * ALL functions called without making a class instance, eg. "t3lib_iconWorks::getIconImage()"
80 *
81 * @author Kasper Skaarhoj <kasper@typo3.com>
82 * @package TYPO3
83 * @subpackage t3lib
84 */
85 class t3lib_iconWorks {
86
87 /**
88 * Returns an icon image tag, 18x16 pixels, based on input information.
89 * This function is recommended to use in your backend modules.
90 *
91 * @param string The table name
92 * @param array The table row ("enablefields" are at least needed for correct icon display and for pages records some more fields in addition!)
93 * @param string The backpath to the main TYPO3 directory (relative path back to PATH_typo3)
94 * @param string Additional attributes for the image tag
95 * @param boolean If set, the icon will be grayed/shaded.
96 * @return string <img>-tag
97 * @see getIcon()
98 */
99 function getIconImage($table,$row=array(),$backPath,$params='',$shaded=0) {
100 $str='<img'.t3lib_iconWorks::skinImg($backPath,t3lib_iconWorks::getIcon($table,$row,$shaded),'width="18" height="16"').(trim($params)?' '.trim($params):'');
101 if (!stristr($str,'alt="')) $str.=' alt=""';
102 $str.=' />';
103 return $str;
104 }
105
106 /**
107 * Creates the icon for input table/row
108 * Returns filename for the image icon, relative to PATH_typo3
109 *
110 * @param string The table name
111 * @param array The table row ("enablefields" are at least needed for correct icon display and for pages records some more fields in addition!)
112 * @param boolean If set, the icon will be grayed/shaded.
113 * @return string Icon filename
114 * @see getIconImage()
115 */
116 function getIcon($table,$row=array(),$shaded=0) {
117 global $TCA, $PAGES_TYPES, $ICON_TYPES;
118 $user=false;
119 if ($table=='pages') {
120 if (!$iconfile = $PAGES_TYPES[$row['doktype']]['icon']) {
121 $iconfile = $PAGES_TYPES['default']['icon'];
122 }
123 if ($row['module'] && $ICON_TYPES[$row['module']]['icon']) {
124 $iconfile = $ICON_TYPES[$row['module']]['icon'];
125 }
126 } else {
127 if (!$iconfile = $TCA[$table]['ctrl']['typeicons'][$row[$TCA[$table]['ctrl']['typeicon_column']]]) {
128 $iconfile = (($TCA[$table]['ctrl']['iconfile']) ? $TCA[$table]['ctrl']['iconfile'] : $table.'.gif');
129 }
130 }
131 // Setting path if not already set:
132 if (!strstr($iconfile,'/')) {
133 $iconfile = 'gfx/i/'.$iconfile;
134 }
135
136 if (substr($iconfile,0,3)=='../') {
137 $absfile=PATH_site.substr($iconfile,3);
138 } else {
139 $absfile=PATH_typo3.$iconfile;
140 }
141
142
143 $hidden = false;
144 $timing = false;
145 $futuretiming = false;
146 $deleted = false;
147 $protectSection=0;
148 if ($enCols=$TCA[$table]['ctrl']['enablecolumns']) {
149 if ($enCol=$row[$enCols['disabled']]) {if ($enCol){$hidden=true;}}
150 if ($enCol=$row[$enCols['starttime']]) {if (time() < $enCol){$timing=true;}}
151 if ($enCol=$row[$enCols['endtime']]) {
152 if ($enCol!=0){
153 if ($enCol < time()) {
154 $timing=true;
155 } else {
156 $futuretiming=true;
157 }
158 }
159 }
160 if ($enCol=$row[$enCols['fe_group']]) {
161 $user=$enCol;
162 }
163 }
164 if ($col=$row[$TCA[$table]['ctrl']['delete']]) {
165 $deleted = true;
166 }
167 if ($table=='pages' && $row['extendToSubpages'] && ($hidden || $timing || $futuretiming || $user)) {
168 $protectSection=1;
169 }
170 if ($hidden || $timing || $futuretiming || $deleted || $user || $shaded) {
171 $string='';
172 if ($deleted) {
173 $string='deleted';
174 } else {
175 if ($hidden) $string.='hidden';
176 if ($timing) $string.='timing';
177 if (!$string && $futuretiming) {
178 $string='futuretiming';
179 }
180 }
181 $theRes= t3lib_iconWorks::makeIcon($GLOBALS['BACK_PATH'].$iconfile, $string, $user, $protectSection, $absfile);
182 return $theRes;
183 } else {
184 return $iconfile;
185 }
186 }
187
188 /**
189 * Returns the src=... for the input $src value OR any alternative found in $TBE_STYLES['skinImg']
190 * Used for skinning the TYPO3 backend with an alternative set of icons
191 *
192 * @param string Current backpath to PATH_typo3 folder
193 * @param string Icon file name relative to PATH_typo3 folder
194 * @param string Default width/height, defined like 'width="12" height="14"'
195 * @param integer Mode: 0 (zero) is default and returns src/width/height. 1 returns value of src+backpath, 2 returns value of w/h.
196 * @return string Returns ' src="[backPath][src]" [wHattribs]'
197 * @see skinImgFile()
198 */
199 function skinImg($backPath,$src,$wHattribs='',$outputMode=0) {
200
201 // LOOKING for alternative icons:
202 if ($GLOBALS['TBE_STYLES']['skinImg'][$src]) { // Slower or faster with is_array()? Could be used.
203 list($src,$wHattribs) = $GLOBALS['TBE_STYLES']['skinImg'][$src];
204 } elseif ($GLOBALS['TBE_STYLES']['skinImgAutoCfg']) { // Otherwise, test if auto-detection is enabled:
205
206 // Search for alternative icon automatically:
207 $fExt = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['forceFileExtension'];
208 $scaleFactor = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['scaleFactor'] ? $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['scaleFactor'] : 1; // Scaling factor
209 $lookUpName = $fExt ? ereg_replace('\.[[:alnum:]]+$','',$src).'.'.$fExt : $src; // Set filename to look for
210
211 // If file is found:
212 if (@is_file($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['absDir'].$lookUpName)) { // If there is a file...
213 $iInfo = @getimagesize($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['absDir'].$lookUpName); // Get width/height:
214
215 // Set $src and $wHattribs:
216 $src = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir'].$lookUpName;
217 $wHattribs = 'width="'.round($iInfo[0]*$scaleFactor).'" height="'.round($iInfo[1]*$scaleFactor).'"';
218 }
219
220 // In anycase, set currect src / wHattrib - this way we make sure that an entry IS found next time we hit the function, regardless of whether it points to a alternative icon or just the current.
221 $GLOBALS['TBE_STYLES']['skinImg'][$src] = array($src,$wHattribs); // Set default...
222 }
223
224 // DEBUG: This doubles the size of all icons - for testing/debugging:
225 # if (ereg('^width="([0-9]+)" height="([0-9]+)"$',$wHattribs,$reg)) $wHattribs='width="'.($reg[1]*2).'" height="'.($reg[2]*2).'"';
226
227 // Return icon source/wHattributes:
228 switch($outputMode) {
229 case 0:
230 return ' src="'.$backPath.$src.'" '.$wHattribs;
231 break;
232 case 1:
233 return $backPath.$src;
234 break;
235 case 2:
236 return $wHattribs;
237 break;
238 }
239 }
240
241
242
243
244
245
246
247
248
249
250
251 /***********************************
252 *
253 * Other functions
254 *
255 ***********************************/
256
257 /**
258 * Creates the icon file for the function getIcon()
259 *
260 * @param string Original unprocessed Icon file, relative path to PATH_typo3
261 * @param string Mode string, eg. "deleted" or "futuretiming" determining how the icon will look
262 * @param integer The number of the fe_group record uid if applicable
263 * @param boolean Flag determines if the protected-section icon should be applied.
264 * @param string Absolute path to file from which to create the icon.
265 * @return string Filename relative to PATH_typo3
266 * @access private
267 */
268 function makeIcon($iconfile,$mode, $user, $protectSection=0,$absFile='') {
269 $iconFileName = 'icon_'.t3lib_div::shortMD5($iconfile.'|'.$mode.'|-'.$user.'|'.$protectSection).'.'.($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']?'png':'gif');
270 $mainpath = '../typo3temp/'.$iconFileName;
271 $path = PATH_site.'typo3temp/'.$iconFileName;
272
273
274 if (@file_exists(PATH_typo3.'icons/'.$iconFileName)) { // Returns if found in typo3/icons/
275 return 'icons/'.$iconFileName;
276 } elseif (@file_exists($path)) { // Returns if found in ../typo3temp/icons/
277 return $mainpath;
278 } else { // Makes icon:
279 if (@file_exists($absFile)) {
280 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib']) {
281 $im = t3lib_iconworks::imagecreatefrom($absFile);
282 if ($im<0) return $iconfile;
283
284 if ($mode!='futuretiming' && !(!$mode && $user)) {
285 for ($c=0; $c<ImageColorsTotal($im); $c++) {
286 $cols = ImageColorsForIndex($im,$c);
287 $newcol = round(($cols['red']+$cols['green']+$cols['blue'])/3);
288 $lighten = 2;
289 $newcol = round (255-((255-$newcol)/$lighten));
290 ImageColorSet($im,$c,$newcol,$newcol,$newcol);
291 }
292 }
293 if ($user) {
294 $black = ImageColorAllocate($im, 0,0,0);
295 imagefilledrectangle($im, 0,0,(($user>10)?9:5),8,$black);
296
297 $white = ImageColorAllocate($im, 255,255,255);
298 imagestring($im, 1, 1, 1, $user, $white);
299
300 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_group.gif');
301 if ($ol_im<0) return $iconfile;
302
303 t3lib_iconworks::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im));
304 }
305 if ($mode) {
306 unset($ol_im);
307 switch($mode) {
308 case 'deleted':
309 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_deleted.gif');
310 break;
311 case 'futuretiming':
312 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_timing.gif');
313 break;
314 case 'timing':
315 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_timing.gif');
316 break;
317 case 'hiddentiming':
318 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_hidden_timing.gif');
319 break;
320 case 'hidden':
321 default:
322 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_hidden.gif');
323 break;
324 }
325 if ($ol_im<0) return $iconfile;
326 t3lib_iconworks::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im));
327 }
328 if ($protectSection) {
329 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_sub5.gif');
330 if ($ol_im<0) return $iconfile;
331 t3lib_iconworks::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im));
332 }
333 @t3lib_iconWorks::imagemake($im, $path);
334 t3lib_div::gif_compress($path, 'IM');
335 ImageDestroy($im);
336 return $mainpath;
337 } else {
338 return $iconfile;
339 }
340 } else {
341 return $GLOBALS['BACK_PATH'].'gfx/fileicons/default.gif';
342 }
343 }
344 }
345
346 /**
347 * The necessity of using this function for combining two images if GD is version 2 is that
348 * GD2 cannot manage to combine two indexed-color images without totally spoiling everything.
349 * In class.t3lib_stdgraphic this was solved by combining the images onto a first created true color image
350 * However it has turned out that this method will not work if the indexed png-files contains transparency.
351 * So I had to turn my attention to ImageMagick - my 'enemy of death'.
352 * And so it happend - ImageMagick is now used to combine my two indexed-color images with transparency. And that works.
353 * Of course it works only if ImageMagick is able to create valid png-images - which you cannot be sure of with older versions (still 5+)
354 * The only drawback is (apparently) that IM creates true-color png's. The transparency of these will not be shown by MSIE on windows at this time (although it's straight 0%/100% transparency!) and the file size may be larger.
355 *
356 * For parameters, see PHP function "imagecopyresized()"
357 *
358 * @param pointer see PHP function "imagecopyresized()"
359 * @param pointer see PHP function "imagecopyresized()"
360 * @param integer see PHP function "imagecopyresized()"
361 * @param integer see PHP function "imagecopyresized()"
362 * @param integer see PHP function "imagecopyresized()"
363 * @param integer see PHP function "imagecopyresized()"
364 * @param integer see PHP function "imagecopyresized()"
365 * @param integer see PHP function "imagecopyresized()"
366 * @param integer see PHP function "imagecopyresized()"
367 * @param integer see PHP function "imagecopyresized()"
368 * @return void
369 * @access private
370 */
371 function imagecopyresized(&$im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h) {
372 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_2'] && $GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']) { // Maybe I'll have to change this if GD2/gif does not work either...
373 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path']) {
374 $cmd=$GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path'].
375 ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_combine_filename']?$GLOBALS['TYPO3_CONF_VARS']['GFX']['im_combine_filename']:'combine').
376 ' -compose over ';
377 $tempBaseName = PATH_site.'typo3temp/ICRZ_'.md5(uniqid('.'));
378
379 ImagePng($im, $tempBaseName.'_im.png');
380 ImagePng($cpImg, $tempBaseName.'_cpImg.png');
381 exec($cmd.
382 $tempBaseName.'_cpImg.png '.
383 $tempBaseName.'_im.png '.
384 $tempBaseName.'_out.png '
385 );
386 $im = imagecreatefrompng($tempBaseName.'_out.png');
387 unlink($tempBaseName.'_im.png');
388 unlink($tempBaseName.'_cpImg.png');
389 unlink($tempBaseName.'_out.png');
390 }
391 } else {
392 imagecopyresized($im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h);
393 }
394 }
395
396 /**
397 * Create new image pointer from input file (either gif/png, in case the wrong format it is converted by t3lib_div::read_png_gif())
398 *
399 * @param string Absolute filename of the image file from which to start the icon creation.
400 * @return mixed If success, image pointer, otherwise "-1"
401 * @access private
402 * @see t3lib_div::read_png_gif
403 */
404 function imagecreatefrom($file) {
405 $file = t3lib_div::read_png_gif($file,$GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']);
406 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']) {
407 return $file ? imagecreatefrompng($file) : -1;
408 } else {
409 return $file ? imagecreatefromgif($file) : -1;
410 }
411 }
412
413 /**
414 * Write the icon in $im pointer to $path
415 *
416 * @param pointer Pointer to GDlib image resource
417 * @param string Absolute path to the filename in which to write the icon.
418 * @return void
419 * @access private
420 */
421 function imagemake($im, $path) {
422 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']) {
423 @ImagePng($im, $path);
424 } else {
425 @ImageGif($im, $path);
426 }
427 }
428 }
429 ?>