* Fixed bug #1861: Call of inexinsting t3lib_exec::imageMagickCommand() instead of...
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_iconworks.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2005 Kasper Skaarhoj (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 * 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 <kasperYYYY@typo3.com>
35 */
36 /**
37 * [CLASS/FUNCTION INDEX of SCRIPT]
38 *
39 *
40 *
41 * 85: class t3lib_iconWorks
42 * 100: function getIconImage($table,$row=array(),$backPath,$params='',$shaded=FALSE)
43 * 118: function getIcon($table,$row=array(),$shaded=FALSE)
44 * 254: function skinImg($backPath,$src,$wHattribs='',$outputMode=0)
45 *
46 * SECTION: Other functions
47 * 328: function makeIcon($iconfile,$mode, $user, $protectSection,$absFile,$iconFileName_stateTagged)
48 * 444: function imagecopyresized(&$im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h)
49 * 477: function imagecreatefrom($file)
50 * 494: 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 <kasperYYYY@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 * Usage: 60
91 *
92 * @param string The table name
93 * @param array The table row ("enablefields" are at least needed for correct icon display and for pages records some more fields in addition!)
94 * @param string The backpath to the main TYPO3 directory (relative path back to PATH_typo3)
95 * @param string Additional attributes for the image tag
96 * @param boolean If set, the icon will be grayed/shaded
97 * @return string <img>-tag
98 * @see getIcon()
99 */
100 function getIconImage($table,$row=array(),$backPath,$params='',$shaded=FALSE) {
101 $str='<img'.t3lib_iconWorks::skinImg($backPath,t3lib_iconWorks::getIcon($table,$row,$shaded),'width="18" height="16"').(trim($params)?' '.trim($params):'');
102 if (!stristr($str,'alt="')) $str.=' alt=""';
103 $str.=' />';
104 return $str;
105 }
106
107 /**
108 * Creates the icon for input table/row
109 * Returns filename for the image icon, relative to PATH_typo3
110 * Usage: 24
111 *
112 * @param string The table name
113 * @param array The table row ("enablefields" are at least needed for correct icon display and for pages records some more fields in addition!)
114 * @param boolean If set, the icon will be grayed/shaded
115 * @return string Icon filename
116 * @see getIconImage()
117 */
118 function getIcon($table,$row=array(),$shaded=FALSE) {
119 global $TCA, $PAGES_TYPES, $ICON_TYPES;
120
121 // Flags:
122 $doNotGenerateIcon = $GLOBALS['TYPO3_CONF_VARS']['GFX']['noIconProc']; // If set, the icon will NOT be generated with GDlib. Rather the icon will be looked for as [iconfilename]_X.[extension]
123 $doNotRenderUserGroupNumber = TRUE; // If set, then the usergroup number will NOT be printed unto the icon. NOTICE. the icon is generated only if a default icon for groups is not found... So effectively this is ineffective...
124
125 // Shadow:
126 if ($TCA[$table]['ctrl']['versioningWS'] && (int)$row['t3ver_state']===1) {
127 return 'gfx/i/shadow_hide.png';
128 }
129 if ($TCA[$table]['ctrl']['versioningWS'] && (int)$row['t3ver_state']===2) {
130 return 'gfx/i/shadow_delete.png';
131 }
132
133 // First, find the icon file name. This can depend on configuration in TCA, field values and more:
134 if ($table=='pages') {
135 if (!$iconfile = $PAGES_TYPES[$row['doktype']]['icon']) {
136 $iconfile = $PAGES_TYPES['default']['icon'];
137 }
138 if ($row['module'] && $ICON_TYPES[$row['module']]['icon']) {
139 $iconfile = $ICON_TYPES[$row['module']]['icon'];
140 }
141 } else {
142 if (!$iconfile = $TCA[$table]['ctrl']['typeicons'][$row[$TCA[$table]['ctrl']['typeicon_column']]]) {
143 $iconfile = (($TCA[$table]['ctrl']['iconfile']) ? $TCA[$table]['ctrl']['iconfile'] : $table.'.gif');
144 }
145 }
146
147 // Setting path of iconfile if not already set. Default is "gfx/i/"
148 if (!strstr($iconfile,'/')) {
149 $iconfile = 'gfx/i/'.$iconfile;
150 }
151
152 // Setting the absolute path where the icon should be found as a file:
153 if (substr($iconfile,0,3)=='../') {
154 $absfile=PATH_site.substr($iconfile,3);
155 } else {
156 $absfile=PATH_typo3.$iconfile;
157 }
158
159 // Initializing variables, all booleans except otherwise stated:
160 $hidden = FALSE;
161 $timing = FALSE;
162 $futuretiming = FALSE;
163 $user = FALSE; // In fact an integer value...
164 $deleted = FALSE;
165 $protectSection = FALSE; // Set, if a page-record (only pages!) has the extend-to-subpages flag set.
166 $noIconFound = $row['_NO_ICON_FOUND'] ? TRUE : FALSE;
167 // + $shaded which is also boolean!
168
169 // Icon state based on "enableFields":
170 if (is_array($TCA[$table]['ctrl']['enablecolumns'])) {
171 $enCols = $TCA[$table]['ctrl']['enablecolumns'];
172 // If "hidden" is enabled:
173 if ($enCols['disabled']) { if ($row[$enCols['disabled']]) { $hidden = TRUE; }}
174 // If a "starttime" is set and higher than current time:
175 if ($enCols['starttime']) { if (time() < intval($row[$enCols['starttime']])) { $timing = TRUE; }}
176 // If an "endtime" is set:
177 if ($enCols['endtime']) {
178 if (intval($row[$enCols['endtime']]) > 0) {
179 if (intval($row[$enCols['endtime']]) < time()) {
180 $timing = TRUE; // End-timing applies at this point.
181 } else {
182 $futuretiming = TRUE; // End-timing WILL apply in the future for this element.
183 }
184 }
185 }
186 // If a user-group field is set:
187 if ($enCols['fe_group']) {
188 $user = $row[$enCols['fe_group']];
189 if ($user && $doNotRenderUserGroupNumber) $user=100; // Limit for user number rendering!
190 }
191 }
192
193 // If "deleted" flag is set (only when listing records which are also deleted!)
194 if ($col=$row[$TCA[$table]['ctrl']['delete']]) {
195 $deleted = TRUE;
196 }
197 // Detecting extendToSubpages (for pages only)
198 if ($table=='pages' && $row['extendToSubpages'] && ($hidden || $timing || $futuretiming || $user)) {
199 $protectSection = TRUE;
200 }
201
202 // If ANY of the booleans are set it means we have to alter the icon:
203 if ($hidden || $timing || $futuretiming || $user || $deleted || $shaded || $noIconFound) {
204 $flags='';
205 $string='';
206 if ($deleted) {
207 $string='deleted';
208 $flags='d';
209 } elseif ($noIconFound) { // This is ONLY for creating icons with "?" on easily...
210 $string='no_icon_found';
211 $flags='x';
212 } else {
213 if ($hidden) $string.='hidden';
214 if ($timing) $string.='timing';
215 if (!$string && $futuretiming) {
216 $string='futuretiming';
217 }
218
219 $flags.=
220 ($hidden ? 'h' : '').
221 ($timing ? 't' : '').
222 ($futuretiming ? 'f' : '').
223 ($user ? 'u' : '').
224 ($protectSection ? 'p' : '').
225 ($shaded ? 's' : '');
226 }
227
228 // Create tagged icon file name:
229 $iconFileName_stateTagged = ereg_replace('.([[:alnum:]]+)$','__'.$flags.'.\1',basename($iconfile));
230
231 // Check if tagged icon file name exists (a tagget icon means the icon base name with the flags added between body and extension of the filename, prefixed with underscore)
232 if (@is_file(dirname($absfile).'/'.$iconFileName_stateTagged)) { // Look for [iconname]_xxxx.[ext]
233 return dirname($iconfile).'/'.$iconFileName_stateTagged;
234 } elseif ($doNotGenerateIcon) { // If no icon generation can be done, try to look for the _X icon:
235 $iconFileName_X = ereg_replace('.([[:alnum:]]+)$','__x.\1',basename($iconfile));
236 if (@is_file(dirname($absfile).'/'.$iconFileName_X)) {
237 return dirname($iconfile).'/'.$iconFileName_X;
238 } else {
239 return 'gfx/i/no_icon_found.gif';
240 }
241 } else { // Otherwise, create the icon:
242 $theRes= t3lib_iconWorks::makeIcon($GLOBALS['BACK_PATH'].$iconfile, $string, $user, $protectSection, $absfile, $iconFileName_stateTagged);
243 return $theRes;
244 }
245 } else {
246 return $iconfile;
247 }
248 }
249
250 /**
251 * Returns the src=... for the input $src value OR any alternative found in $TBE_STYLES['skinImg']
252 * Used for skinning the TYPO3 backend with an alternative set of icons
253 * Usage: 336
254 *
255 * @param string Current backpath to PATH_typo3 folder
256 * @param string Icon file name relative to PATH_typo3 folder
257 * @param string Default width/height, defined like 'width="12" height="14"'
258 * @param integer Mode: 0 (zero) is default and returns src/width/height. 1 returns value of src+backpath, 2 returns value of w/h.
259 * @return string Returns ' src="[backPath][src]" [wHattribs]'
260 * @see skinImgFile()
261 */
262 function skinImg($backPath,$src,$wHattribs='',$outputMode=0) {
263
264 // Setting source key. If the icon is refered to inside an extension, we homogenize the prefix to "ext/":
265 $srcKey = ereg_replace('^(\.\.\/typo3conf\/ext|sysext|ext)\/','ext/',$src);
266 #if ($src!=$srcKey)debug(array($src,$srcKey));
267
268 // LOOKING for alternative icons:
269 if ($GLOBALS['TBE_STYLES']['skinImg'][$srcKey]) { // Slower or faster with is_array()? Could be used.
270 list($src,$wHattribs) = $GLOBALS['TBE_STYLES']['skinImg'][$srcKey];
271 } elseif ($GLOBALS['TBE_STYLES']['skinImgAutoCfg']) { // Otherwise, test if auto-detection is enabled:
272
273 // Search for alternative icon automatically:
274 $fExt = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['forceFileExtension'];
275 $scaleFactor = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['scaleFactor'] ? $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['scaleFactor'] : 1; // Scaling factor
276 $lookUpName = $fExt ? ereg_replace('\.[[:alnum:]]+$','',$srcKey).'.'.$fExt : $srcKey; // Set filename to look for
277
278 // If file is found:
279 if (@is_file($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['absDir'].$lookUpName)) { // If there is a file...
280 $iInfo = @getimagesize($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['absDir'].$lookUpName); // Get width/height:
281
282 // Set $src and $wHattribs:
283 $src = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir'].$lookUpName;
284 $wHattribs = 'width="'.round($iInfo[0]*$scaleFactor).'" height="'.round($iInfo[1]*$scaleFactor).'"';
285 }
286
287 // 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.
288 $GLOBALS['TBE_STYLES']['skinImg'][$srcKey] = array($src,$wHattribs); // Set default...
289 }
290
291 // DEBUG: This doubles the size of all icons - for testing/debugging:
292 # if (ereg('^width="([0-9]+)" height="([0-9]+)"$',$wHattribs,$reg)) $wHattribs='width="'.($reg[1]*2).'" height="'.($reg[2]*2).'"';
293
294 // Return icon source/wHattributes:
295 switch($outputMode) {
296 case 0:
297 return ' src="'.$backPath.$src.'" '.$wHattribs;
298 break;
299 case 1:
300 return $backPath.$src;
301 break;
302 case 2:
303 return $wHattribs;
304 break;
305 }
306 }
307
308
309
310
311
312
313
314
315
316
317
318 /***********************************
319 *
320 * Other functions
321 *
322 ***********************************/
323
324 /**
325 * Creates the icon file for the function getIcon()
326 *
327 * @param string Original unprocessed Icon file, relative path to PATH_typo3
328 * @param string Mode string, eg. "deleted" or "futuretiming" determining how the icon will look
329 * @param integer The number of the fe_group record uid if applicable
330 * @param boolean Flag determines if the protected-section icon should be applied.
331 * @param string Absolute path to file from which to create the icon.
332 * @param string The filename that this icon should have had, basically [icon base name]_[flags].[extension] - used for part of temporary filename
333 * @return string Filename relative to PATH_typo3
334 * @access private
335 */
336 function makeIcon($iconfile,$mode, $user, $protectSection,$absFile,$iconFileName_stateTagged) {
337 $iconFileName = 'icon_'.t3lib_div::shortMD5($iconfile.'|'.$mode.'|-'.$user.'|'.$protectSection).'_'.$iconFileName_stateTagged.'.'.($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']?'png':'gif');
338 $mainpath = '../typo3temp/'.$iconFileName;
339 $path = PATH_site.'typo3temp/'.$iconFileName;
340
341
342 if (@file_exists(PATH_typo3.'icons/'.$iconFileName)) { // Returns if found in typo3/icons/
343 return 'icons/'.$iconFileName;
344 } elseif (@file_exists($path)) { // Returns if found in ../typo3temp/icons/
345 return $mainpath;
346 } else { // Makes icon:
347 if (@file_exists($absFile)) {
348 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib']) {
349
350 // Create image pointer, if possible:
351 $im = t3lib_iconworks::imagecreatefrom($absFile);
352 if ($im<0) return $iconfile;
353
354 // Converting to gray scale, dimming the icon:
355 if ($mode!='futuretiming' && $mode!='no_icon_found' && !(!$mode && $user)) {
356 for ($c=0; $c<ImageColorsTotal($im); $c++) {
357 $cols = ImageColorsForIndex($im,$c);
358 $newcol = round(($cols['red']+$cols['green']+$cols['blue'])/3);
359 $lighten = 2;
360 $newcol = round(255-((255-$newcol)/$lighten));
361 ImageColorSet($im,$c,$newcol,$newcol,$newcol);
362 }
363 }
364 // Applying user icon, if there are access control on the item:
365 if ($user) {
366 if ($user < 100) { // Apply user number only if lower than 100
367 $black = ImageColorAllocate($im, 0,0,0);
368 imagefilledrectangle($im, 0,0,(($user>10)?9:5),8,$black);
369
370 $white = ImageColorAllocate($im, 255,255,255);
371 imagestring($im, 1, 1, 1, $user, $white);
372 }
373
374 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_group.gif');
375 if ($ol_im<0) return $iconfile;
376
377 t3lib_iconworks::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im));
378 }
379 // Applying overlay based on mode:
380 if ($mode) {
381 unset($ol_im);
382 switch($mode) {
383 case 'deleted':
384 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_deleted.gif');
385 break;
386 case 'futuretiming':
387 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_timing.gif');
388 break;
389 case 'timing':
390 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_timing.gif');
391 break;
392 case 'hiddentiming':
393 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_hidden_timing.gif');
394 break;
395 case 'no_icon_found':
396 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_no_icon_found.gif');
397 break;
398 case 'hidden':
399 default:
400 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_hidden.gif');
401 break;
402 }
403 if ($ol_im<0) return $iconfile;
404 t3lib_iconworks::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im));
405 }
406 // Protect-section icon:
407 if ($protectSection) {
408 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_sub5.gif');
409 if ($ol_im<0) return $iconfile;
410 t3lib_iconworks::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im));
411 }
412
413 // Create the image as file, destroy GD image and return:
414 @t3lib_iconWorks::imagemake($im, $path);
415 t3lib_div::gif_compress($path, 'IM');
416 ImageDestroy($im);
417 return $mainpath;
418 } else {
419 return $iconfile;
420 }
421 } else {
422 return $GLOBALS['BACK_PATH'].'gfx/fileicons/default.gif';
423 }
424 }
425 }
426
427 /**
428 * The necessity of using this function for combining two images if GD is version 2 is that
429 * GD2 cannot manage to combine two indexed-color images without totally spoiling everything.
430 * In class.t3lib_stdgraphic this was solved by combining the images onto a first created true color image
431 * However it has turned out that this method will not work if the indexed png-files contains transparency.
432 * So I had to turn my attention to ImageMagick - my 'enemy of death'.
433 * And so it happend - ImageMagick is now used to combine my two indexed-color images with transparency. And that works.
434 * 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+)
435 * 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.
436 *
437 * For parameters, see PHP function "imagecopyresized()"
438 *
439 * @param pointer see PHP function "imagecopyresized()"
440 * @param pointer see PHP function "imagecopyresized()"
441 * @param integer see PHP function "imagecopyresized()"
442 * @param integer see PHP function "imagecopyresized()"
443 * @param integer see PHP function "imagecopyresized()"
444 * @param integer see PHP function "imagecopyresized()"
445 * @param integer see PHP function "imagecopyresized()"
446 * @param integer see PHP function "imagecopyresized()"
447 * @param integer see PHP function "imagecopyresized()"
448 * @param integer see PHP function "imagecopyresized()"
449 * @return void
450 * @access private
451 */
452 function imagecopyresized(&$im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h) {
453 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...
454 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path']) {
455
456 $tempBaseName = PATH_site.'typo3temp/ICRZ_'.md5(uniqid('.'));
457
458 ImagePng($im, $tempBaseName.'_im.png');
459 ImagePng($cpImg, $tempBaseName.'_cpImg.png');
460
461 $cmd = t3lib_div::imageMagickCommand('combine', '-compose over '.$tempBaseName.'_cpImg.png '.$tempBaseName.'_im.png '.$tempBaseName.'_out.png ');
462 exec($cmd);
463
464 $im = imagecreatefrompng($tempBaseName.'_out.png');
465 unlink($tempBaseName.'_im.png');
466 unlink($tempBaseName.'_cpImg.png');
467 unlink($tempBaseName.'_out.png');
468 }
469 } else {
470 imagecopyresized($im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h);
471 }
472 }
473
474 /**
475 * Create new image pointer from input file (either gif/png, in case the wrong format it is converted by t3lib_div::read_png_gif())
476 *
477 * @param string Absolute filename of the image file from which to start the icon creation.
478 * @return mixed If success, image pointer, otherwise "-1"
479 * @access private
480 * @see t3lib_div::read_png_gif
481 */
482 function imagecreatefrom($file) {
483 $file = t3lib_div::read_png_gif($file,$GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']);
484 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']) {
485 return $file ? imagecreatefrompng($file) : -1;
486 } else {
487 return $file ? imagecreatefromgif($file) : -1;
488 }
489 }
490
491 /**
492 * Write the icon in $im pointer to $path
493 *
494 * @param pointer Pointer to GDlib image resource
495 * @param string Absolute path to the filename in which to write the icon.
496 * @return void
497 * @access private
498 */
499 function imagemake($im, $path) {
500 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']) {
501 @ImagePng($im, $path);
502 } else {
503 @ImageGif($im, $path);
504 }
505 }
506 }
507 ?>