[BUGFIX] imagecopyresized: correct invalid parameters
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_iconworks.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 /**
29 * Contains class for icon generation in the backend
30 * This library has functions that returns - and if necessary creates - the icon for an element in TYPO3
31 *
32 * Expects global vars:
33 * - $BACK_PATH
34 * - PATH_typo3
35 * - $TCA, $PAGES_TYPES
36 *
37 *
38 * Notes:
39 * These functions are strongly related to the interface of TYPO3.
40 * The class is included in eg. init.php
41 * ALL functions called without making a class instance, eg. "t3lib_iconWorks::getIconImage()"
42 *
43 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
44 * @package TYPO3
45 * @subpackage t3lib
46 */
47 final class t3lib_iconWorks {
48
49 public static $fileSpriteIconNames = array(
50 'htm' => 'mimetypes-text-html',
51 'html' => 'mimetypes-text-html',
52 'css' => 'mimetypes-text-css',
53 'js' => 'mimetypes-text-js',
54 'csv' => 'mimetypes-text-csv',
55 'php' => 'mimetypes-text-php',
56 'php6' => 'mimetypes-text-php',
57 'php5' => 'mimetypes-text-php',
58 'php4' => 'mimetypes-text-php',
59 'php3' => 'mimetypes-text-php',
60 'inc' => 'mimetypes-text-php',
61 'ts' => 'mimetypes-text-ts',
62 'txt' => 'mimetypes-text-text',
63 'class' => 'mimetypes-text-text',
64 'tmpl' => 'mimetypes-text-text',
65 'jpg' => 'mimetypes-media-image',
66 'jpeg' => 'mimetypes-media-image',
67 'gif' => 'mimetypes-media-image',
68 'png' => 'mimetypes-media-image',
69 'bmp' => 'mimetypes-media-image',
70 'tif' => 'mimetypes-media-image',
71 'tga' => 'mimetypes-media-image',
72 'psd' => 'mimetypes-media-image',
73 'eps' => 'mimetypes-media-image',
74 'avi' => 'mimetypes-media-video',
75 'mpg' => 'mimetypes-media-video',
76 'mpeg' => 'mimetypes-media-video',
77 'mov' => 'mimetypes-media-video',
78 'wav' => 'mimetypes-media-audio',
79 'mp3' => 'mimetypes-media-audio',
80 'mid' => 'mimetypes-media-audio',
81 'swf' => 'mimetypes-media-flash',
82 'swa' => 'mimetypes-media-flash',
83 'exe' => 'mimetypes-executable-executable',
84 'com' => 'mimetypes-executable-executable',
85 't3x' => 'mimetypes-compressed',
86 't3d' => 'mimetypes-compressed',
87 'zip' => 'mimetypes-compressed',
88 'tgz' => 'mimetypes-compressed',
89 'gz' => 'mimetypes-compressed',
90 'pdf' => 'mimetypes-pdf',
91 'doc' => 'mimetypes-word',
92 'dot' => 'mimetypes-word',
93 'docm' => 'mimetypes-word',
94 'docx' => 'mimetypes-word',
95 'dotm' => 'mimetypes-word',
96 'dotx' => 'mimetypes-word',
97 'sxw' => 'mimetypes-word',
98 'rtf' => 'mimetypes-word',
99 'xls' => 'mimetypes-excel',
100 'xlsm' => 'mimetypes-excel',
101 'xlsx' => 'mimetypes-excel',
102 'xltm' => 'mimetypes-excel',
103 'xltx' => 'mimetypes-excel',
104 'sxc' => 'mimetypes-excel',
105 'ppt' => 'mimetypes-powerpoint',
106 'pptm' => 'mimetypes-powerpoint',
107 'pptx' => 'mimetypes-powerpoint',
108 'potm' => 'mimetypes-powerpoint',
109 'potx' => 'mimetypes-powerpoint',
110 'mount' => 'apps-filetree-mount',
111 'folder' => 'apps-filetree-folder-default',
112 'default' => 'mimetypes-other-other'
113 );
114
115 /**
116 * Returns an icon image tag, 18x16 pixels, based on input information.
117 * This function is recommended to use in your backend modules.
118 *
119 * @param string The table name
120 * @param array The table row ("enablefields" are at least needed for correct icon display and for pages records some more fields in addition!)
121 * @param string The backpath to the main TYPO3 directory (relative path back to PATH_typo3)
122 * @param string Additional attributes for the image tag
123 * @param boolean If set, the icon will be grayed/shaded
124 * @return string <img>-tag
125 * @see getIcon()
126 */
127 public static function getIconImage($table, $row = array(), $backPath, $params = '', $shaded = FALSE) {
128 $str = '<img' .
129 self::skinImg($backPath, self::getIcon($table, $row, $shaded), 'width="18" height="16"') .
130 (trim($params) ? ' ' . trim($params) : '');
131 if (!stristr($str, 'alt="')) {
132 $str .= ' alt=""';
133 }
134 $str .= ' />';
135 return $str;
136 }
137
138 /**
139 * Creates the icon for input table/row
140 * Returns filename for the image icon, relative to PATH_typo3
141 *
142 * @param string The table name
143 * @param array The table row ("enablefields" are at least needed for correct icon display and for pages records some more fields in addition!)
144 * @param boolean If set, the icon will be grayed/shaded
145 * @return string Icon filename
146 * @see getIconImage()
147 */
148 public static function getIcon($table, $row = array(), $shaded = FALSE) {
149 // Flags:
150 $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...
151
152 // Shadow:
153 if ($GLOBALS['TCA'][$table]['ctrl']['versioningWS']) {
154 switch ((int) $row['t3ver_state']) {
155 case 1:
156 return 'gfx/i/shadow_hide.png';
157 break;
158 case 2:
159 return 'gfx/i/shadow_delete.png';
160 break;
161 case 3:
162 return 'gfx/i/shadow_moveto_plh.png';
163 break;
164 case 4:
165 return 'gfx/i/shadow_moveto_pointer.png';
166 break;
167 }
168 }
169
170 // First, find the icon file name. This can depend on configuration in TCA, field values and more:
171 if ($table == 'pages') {
172 $iconfile = $GLOBALS['PAGES_TYPES'][$row['doktype']]['icon'];
173 if (!$iconfile) {
174 $iconfile = $GLOBALS['PAGES_TYPES']['default']['icon'];
175 }
176 } else {
177 if (!$iconfile = $GLOBALS['TCA'][$table]['ctrl']['typeicons'][$row[$GLOBALS['TCA'][$table]['ctrl']['typeicon_column']]]) {
178 $iconfile = (($GLOBALS['TCA'][$table]['ctrl']['iconfile']) ? $GLOBALS['TCA'][$table]['ctrl']['iconfile'] : $table . '.gif');
179 }
180 }
181
182 // Setting path of iconfile if not already set. Default is "gfx/i/"
183 if (!strstr($iconfile, '/')) {
184 $iconfile = 'gfx/i/' . $iconfile;
185 }
186
187 // Setting the absolute path where the icon should be found as a file:
188 if (substr($iconfile, 0, 3) == '../') {
189 $absfile = PATH_site . substr($iconfile, 3);
190 } else {
191 $absfile = PATH_typo3 . $iconfile;
192 }
193
194 // Initializing variables, all booleans except otherwise stated:
195 $hidden = FALSE;
196 $timing = FALSE;
197 $futuretiming = FALSE;
198 $user = FALSE; // In fact an integer value...
199 $deleted = FALSE;
200 $protectSection = FALSE; // Set, if a page-record (only pages!) has the extend-to-subpages flag set.
201 $noIconFound = $row['_NO_ICON_FOUND'] ? TRUE : FALSE;
202 // + $shaded which is also boolean!
203
204 // Icon state based on "enableFields":
205 if (is_array($GLOBALS['TCA'][$table]['ctrl']['enablecolumns'])) {
206 $enCols = $GLOBALS['TCA'][$table]['ctrl']['enablecolumns'];
207 // If "hidden" is enabled:
208 if ($enCols['disabled']) {
209 if ($row[$enCols['disabled']]) {
210 $hidden = TRUE;
211 }
212 }
213 // If a "starttime" is set and higher than current time:
214 if ($enCols['starttime']) {
215 if ($GLOBALS['EXEC_TIME'] < intval($row[$enCols['starttime']])) {
216 $timing = TRUE;
217 // ...And if "endtime" is NOT set:
218 if (intval($row[$enCols['endtime']]) == 0) {
219 $futuretiming = TRUE;
220 }
221 }
222 }
223 // If an "endtime" is set:
224 if ($enCols['endtime']) {
225 if (intval($row[$enCols['endtime']]) > 0) {
226 if (intval($row[$enCols['endtime']]) < $GLOBALS['EXEC_TIME']) {
227 $timing = TRUE; // End-timing applies at this point.
228 } else {
229 $futuretiming = TRUE; // End-timing WILL apply in the future for this element.
230 }
231 }
232 }
233 // If a user-group field is set:
234 if ($enCols['fe_group']) {
235 $user = $row[$enCols['fe_group']];
236 if ($user && $doNotRenderUserGroupNumber) {
237 $user = 100;
238 } // Limit for user number rendering!
239 }
240 }
241
242 // If "deleted" flag is set (only when listing records which are also deleted!)
243 if ($col = $row[$GLOBALS['TCA'][$table]['ctrl']['delete']]) {
244 $deleted = TRUE;
245 }
246 // Detecting extendToSubpages (for pages only)
247 if ($table == 'pages' && $row['extendToSubpages'] && ($hidden || $timing || $futuretiming || $user)) {
248 $protectSection = TRUE;
249 }
250
251 // If ANY of the booleans are set it means we have to alter the icon:
252 if ($hidden || $timing || $futuretiming || $user || $deleted || $shaded || $noIconFound) {
253 $flags = '';
254 $string = '';
255 if ($deleted) {
256 $string = 'deleted';
257 $flags = 'd';
258 } elseif ($noIconFound) { // This is ONLY for creating icons with "?" on easily...
259 $string = 'no_icon_found';
260 $flags = 'x';
261 } else {
262 if ($hidden) {
263 $string .= 'hidden';
264 }
265 if ($timing) {
266 $string .= 'timing';
267 }
268 if (!$string && $futuretiming) {
269 $string = 'futuretiming';
270 }
271
272 $flags .=
273 ($hidden ? 'h' : '') .
274 ($timing ? 't' : '') .
275 ($futuretiming ? 'f' : '') .
276 ($user ? 'u' : '') .
277 ($protectSection ? 'p' : '') .
278 ($shaded ? 's' : '');
279 }
280
281 // Create tagged icon file name:
282 $iconFileName_stateTagged = preg_replace('/.([[:alnum:]]+)$/', '__' . $flags . '.\1', basename($iconfile));
283
284 // 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)
285 if (@is_file(dirname($absfile) . '/' . $iconFileName_stateTagged) || @is_file($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['absDir'] . '/' . dirname($iconfile) . '/' . $iconFileName_stateTagged)) { // Look for [iconname]_xxxx.[ext]
286 return dirname($iconfile) . '/' . $iconFileName_stateTagged;
287 } else { // Otherwise, create the icon:
288 $theRes = self::makeIcon($GLOBALS['BACK_PATH'] . $iconfile, $string, $user, $protectSection, $absfile, $iconFileName_stateTagged);
289 return $theRes;
290 }
291 } else {
292 return $iconfile;
293 }
294 }
295
296 /**
297 * Returns the src=... for the input $src value OR any alternative found in $TBE_STYLES['skinImg']
298 * Used for skinning the TYPO3 backend with an alternative set of icons
299 *
300 * @param string Current backpath to PATH_typo3 folder
301 * @param string Icon file name relative to PATH_typo3 folder
302 * @param string Default width/height, defined like 'width="12" height="14"'
303 * @param integer Mode: 0 (zero) is default and returns src/width/height. 1 returns value of src+backpath, 2 returns value of w/h.
304 * @return string Returns ' src="[backPath][src]" [wHattribs]'
305 * @see skinImgFile()
306 */
307 public static function skinImg($backPath, $src, $wHattribs = '', $outputMode = 0) {
308
309 static $cachedSkinImages = array();
310
311 $imageId = md5($backPath . $src . $wHattribs . $outputMode);
312
313 if (isset($cachedSkinImages[$imageId])) {
314 return $cachedSkinImages[$imageId];
315 }
316 // Setting source key. If the icon is refered to inside an extension, we homogenize the prefix to "ext/":
317 $srcKey = preg_replace('/^(\.\.\/typo3conf\/ext|sysext|ext)\//', 'ext/', $src);
318 #if ($src!=$srcKey)debug(array($src, $srcKey));
319
320 // LOOKING for alternative icons:
321 if ($GLOBALS['TBE_STYLES']['skinImg'][$srcKey]) { // Slower or faster with is_array()? Could be used.
322 list($src, $wHattribs) = $GLOBALS['TBE_STYLES']['skinImg'][$srcKey];
323 } elseif ($GLOBALS['TBE_STYLES']['skinImgAutoCfg']) { // Otherwise, test if auto-detection is enabled:
324
325 // Search for alternative icon automatically:
326 $fExt = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['forceFileExtension'];
327 $scaleFactor = ($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['scaleFactor'] ? $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['scaleFactor'] : 1); // Scaling factor
328 $lookUpName = ($fExt ? preg_replace('/\.[[:alnum:]]+$/', '', $srcKey) . '.' . $fExt : $srcKey); // Set filename to look for
329
330 if ($fExt && !@is_file($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['absDir'] . $lookUpName)) {
331 // fallback to original filename if icon with forced extension doesn't exists
332 $lookUpName = $srcKey;
333 }
334 // If file is found:
335 if (@is_file($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['absDir'] . $lookUpName)) { // If there is a file...
336 $iInfo = @getimagesize($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['absDir'] . $lookUpName); // Get width/height:
337
338 // Set $src and $wHattribs:
339 $src = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir'] . $lookUpName;
340 $wHattribs = 'width="' . round($iInfo[0] * $scaleFactor) . '" height="' . round($iInfo[1] * $scaleFactor) . '"';
341 }
342
343 // In any case, set currect src / wHattrib - this way we make sure that an entry IS found next time we hit the function,
344 // regardless of whether it points to a alternative icon or just the current.
345 $GLOBALS['TBE_STYLES']['skinImg'][$srcKey] = array($src, $wHattribs); // Set default...
346 }
347
348 // DEBUG: This doubles the size of all icons - for testing/debugging:
349 // if (preg_match('/^width="([0-9]+)" height="([0-9]+)"$/', $wHattribs, $reg)) $wHattribs='width="'.($reg[1]*2).'" height="'.($reg[2]*2).'"';
350
351
352 // rendering disabled (greyed) icons using _i (inactive) as name suffix ("_d" is already used)
353 $matches = array();
354 $srcBasename = basename($src);
355 if (preg_match('/(.*)_i(\....)$/', $srcBasename, $matches)) {
356 $temp_path = dirname(PATH_thisScript) . '/';
357 if (!@is_file($temp_path . $backPath . $src)) {
358 $srcOrg = preg_replace('/_i' . preg_quote($matches[2]) . '$/', $matches[2], $src);
359 $src = self::makeIcon($backPath . $srcOrg, 'disabled', 0, FALSE, $temp_path . $backPath . $srcOrg, $srcBasename);
360 }
361 }
362
363
364 // Return icon source/wHattributes:
365 $output = '';
366 switch ($outputMode) {
367 case 0:
368 $output = ' src="' . $backPath . $src . '" ' . $wHattribs;
369 break;
370 case 1:
371 $output = $backPath . $src;
372 break;
373 case 2:
374 $output = $wHattribs;
375 break;
376 }
377
378 $cachedSkinImages[$imageId] = $output;
379 return $output;
380 }
381
382
383 /***********************************
384 *
385 * Other functions
386 *
387 ***********************************/
388
389 /**
390 * Creates the icon file for the function getIcon()
391 *
392 * @param string Original unprocessed Icon file, relative path to PATH_typo3
393 * @param string Mode string, eg. "deleted" or "futuretiming" determining how the icon will look
394 * @param integer The number of the fe_group record uid if applicable
395 * @param boolean Flag determines if the protected-section icon should be applied.
396 * @param string Absolute path to file from which to create the icon.
397 * @param string The filename that this icon should have had, basically [icon base name]_[flags].[extension] - used for part of temporary filename
398 * @return string Filename relative to PATH_typo3
399 * @access private
400 */
401 public static function makeIcon($iconfile, $mode, $user, $protectSection, $absFile, $iconFileName_stateTagged) {
402 $iconFileName = 'icon_' . t3lib_div::shortMD5($iconfile . '|' . $mode . '|-' . $user . '|' . $protectSection) . '_' . $iconFileName_stateTagged . '.' . ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png'] ? 'png' : 'gif');
403 $mainpath = '../typo3temp/' . $iconFileName;
404 $path = PATH_site . 'typo3temp/' . $iconFileName;
405
406
407 if (file_exists(PATH_typo3 . 'icons/' . $iconFileName)) { // Returns if found in typo3/icons/
408 return 'icons/' . $iconFileName;
409 } elseif (file_exists($path)) { // Returns if found in ../typo3temp/icons/
410 return $mainpath;
411 } else { // Makes icon:
412 if (file_exists($absFile)) {
413 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib']) {
414
415 // Create image pointer, if possible
416 $im = self::imagecreatefrom($absFile);
417 if ($im < 0) {
418 return $iconfile;
419 }
420
421 // Converting to gray scale, dimming the icon:
422 if (($mode == 'disabled') OR ($mode != 'futuretiming' && $mode != 'no_icon_found' && !(!$mode && $user))) {
423 for ($c = 0; $c < ImageColorsTotal($im); $c++) {
424 $cols = ImageColorsForIndex($im, $c);
425 $newcol = round(($cols['red'] + $cols['green'] + $cols['blue']) / 3);
426 $lighten = ($mode == 'disabled') ? 2.5 : 2;
427 $newcol = round(255 - ((255 - $newcol) / $lighten));
428 ImageColorSet($im, $c, $newcol, $newcol, $newcol);
429 }
430 }
431 // Applying user icon, if there are access control on the item:
432 if ($user) {
433 if ($user < 100) { // Apply user number only if lower than 100
434 $black = ImageColorAllocate($im, 0, 0, 0);
435 imagefilledrectangle($im, 0, 0, (($user > 10) ? 9 : 5), 8, $black);
436
437 $white = ImageColorAllocate($im, 255, 255, 255);
438 imagestring($im, 1, 1, 1, $user, $white);
439 }
440
441 $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_group.gif');
442 if ($ol_im < 0) {
443 return $iconfile;
444 }
445
446 self::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im));
447 }
448 // Applying overlay based on mode:
449 if ($mode) {
450 unset($ol_im);
451 switch ($mode) {
452 case 'deleted':
453 $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_deleted.gif');
454 break;
455 case 'futuretiming':
456 $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_timing.gif');
457 break;
458 case 'timing':
459 $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_timing.gif');
460 break;
461 case 'hiddentiming':
462 $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_hidden_timing.gif');
463 break;
464 case 'no_icon_found':
465 $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_no_icon_found.gif');
466 break;
467 case 'disabled':
468 // is already greyed - nothing more
469 $ol_im = 0;
470 break;
471 case 'hidden':
472 default:
473 $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_hidden.gif');
474 break;
475 }
476 if ($ol_im < 0) {
477 return $iconfile;
478 }
479 if ($ol_im) {
480 self::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im));
481 }
482 }
483 // Protect-section icon:
484 if ($protectSection) {
485 $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_sub5.gif');
486 if ($ol_im < 0) {
487 return $iconfile;
488 }
489 self::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im));
490 }
491
492 // Create the image as file, destroy GD image and return:
493 @self::imagemake($im, $path);
494 t3lib_div::gif_compress($path, 'IM');
495 ImageDestroy($im);
496 return $mainpath;
497 } else {
498 return $iconfile;
499 }
500 } else {
501 return $GLOBALS['BACK_PATH'] . 'gfx/fileicons/default.gif';
502 }
503 }
504 }
505
506 /**
507 * The necessity of using this function for combining two images if GD is version 2 is that
508 * GD2 cannot manage to combine two indexed-color images without totally spoiling everything.
509 * In class.t3lib_stdgraphic this was solved by combining the images onto a first created true color image
510 * However it has turned out that this method will not work if the indexed png-files contains transparency.
511 * So I had to turn my attention to ImageMagick - my 'enemy of death'.
512 * And so it happend - ImageMagick is now used to combine my two indexed-color images with transparency. And that works.
513 * 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+)
514 * 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.
515 *
516 * @param resource $dstImg destination image
517 * @param resource $srcImg source image
518 * @param integer $dstX destination x-coordinate
519 * @param integer $dstY destination y-coordinate
520 * @param integer $srcX source x-coordinate
521 * @param integer $srcY source y-coordinate
522 * @param integer $dstWidth destination width
523 * @param integer $dstHeight destination height
524 * @param integer $srcWidth source width
525 * @param integer $srcHeight source height
526 * @return void
527 * @access private
528 * @see t3lib_stdGraphic::imagecopyresized()
529 */
530 public static function imagecopyresized(&$dstImg, $srcImg, $dstX, $dstY, $srcX, $srcY, $dstWidth, $dstHeight, $srcWidth, $srcHeight) {
531 imagecopyresized($dstImg, $srcImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $dstWidth, $dstHeight, $srcWidth, $srcHeight);
532 }
533
534 /**
535 * Create new image pointer from input file (either gif/png, in case the wrong format it is converted by t3lib_div::read_png_gif())
536 *
537 * @param string Absolute filename of the image file from which to start the icon creation.
538 * @return mixed If success, image pointer, otherwise "-1"
539 * @access private
540 * @see t3lib_div::read_png_gif
541 */
542 public static function imagecreatefrom($file) {
543 $file = t3lib_div::read_png_gif($file, $GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']);
544 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']) {
545 return $file ? imagecreatefrompng($file) : -1;
546 } else {
547 return $file ? imagecreatefromgif($file) : -1;
548 }
549 }
550
551 /**
552 * Write the icon in $im pointer to $path
553 *
554 * @param pointer Pointer to GDlib image resource
555 * @param string Absolute path to the filename in which to write the icon.
556 * @return void
557 * @access private
558 */
559 public static function imagemake($im, $path) {
560 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']) {
561 @ImagePng($im, $path);
562 } else {
563 @ImageGif($im, $path);
564 }
565 if (@is_file($path)) {
566 t3lib_div::fixPermissions($path);
567 }
568 }
569
570
571 /**********************************************
572 * SPRITE ICON API
573 *
574 * The Sprite Icon API helps you to quickly get the HTML for any icon you want
575 * this is typically wrapped in a <span> tag with corresponding CSS classes that
576 * will be responsible for the
577 *
578 * There are three ways to use this API:
579 *
580 * 1) for any given TCA record
581 * $spriteIconHtml = t3lib_iconWorks::getSpriteIconForRecord('pages', $row);
582 *
583 * 2) for any given file
584 * $spriteIconHtml = t3lib_iconWorks::getSpriteIconForFile('myimage.png');
585 *
586 * 3) for any other icon you know the name
587 * $spriteIconHtml = t3lib_iconWorks::getSpriteIcon('actions-document-open');
588 *
589 **********************************************/
590
591 /**
592 * This generic method is used throughout the TYPO3 Backend to show icons in any variation which are not
593 * bound to any file type (see getSpriteIconForFile) or database record (see getSpriteIconForRecord)
594 *
595 * Generates a HTML tag with proper CSS classes. The TYPO3 skin has defined these CSS classes
596 * already to have a pre-defined background image, and the correct background-position to show
597 * the necessary icon.
598 *
599 * @param string $iconName the name of the icon to fetch
600 * @param array $options an associative array with additional options and attributes for the tag. by default, the key is the name of the attribute, and the value is the parameter string that is set. However, there are some additional special reserved keywords that can be used as keys: "html" (which is the HTML that will be inside the icon HTML tag), "tagName" (which is an alternative tagName than "span"), and "class" (additional class names that will be merged with the sprite icon CSS classes)
601 * @param array $overlays an associative array with the icon-name as key, and the options for this overlay as an array again (see the parameter $options again)
602 * @return string the full HTML tag (usually a <span>)
603 * @access public
604 */
605 public static function getSpriteIcon($iconName, array $options = array(), array $overlays = array()) {
606 $innerHtml = (isset($options['html']) ? $options['html'] : NULL);
607 $tagName = (isset($options['tagName']) ? $options['tagName'] : NULL);
608
609 // deal with the overlays
610 if (count($overlays)) {
611 foreach ($overlays as $overlayIconName => $overlayOptions) {
612 $overlayOptions['html'] = $innerHtml;
613 $overlayOptions['class'] = (isset($overlayOptions['class']) ? $overlayOptions['class'] . ' ' : '') . 't3-icon-overlay';
614 $innerHtml = self::getSpriteIcon($overlayIconName, $overlayOptions);
615 }
616 }
617
618 // check if whished icon is available
619 $iconName = (in_array($iconName, $GLOBALS['TBE_STYLES']['spriteIconApi']['iconsAvailable']) || $iconName == 'empty-empty' ? $iconName : 'status-status-icon-missing');
620
621 // create the CSS class
622 $options['class'] = self::getSpriteIconClasses($iconName) . (isset($options['class']) ? ' ' . $options['class'] : '');
623
624 unset($options['html']);
625 unset($options['tagName']);
626 return self::buildSpriteHtmlIconTag($options, $innerHtml, $tagName);
627 }
628
629
630 /**
631 * This method is used throughout the TYPO3 Backend to show icons for a file type
632 *
633 * Generates a HTML tag with proper CSS classes. The TYPO3 skin has defined these CSS classes
634 * already to have a pre-defined background image, and the correct background-position to show
635 * the necessary icon.
636 *
637 * @param string $fileExtension the name of the icon to fetch, can be a file extension, full file path or one of the special keywords "folder" or "mount"
638 * @param array $options an associative array with additional options and attributes for the tag. by default, the key is the name of the attribute, and the value is the parameter string that is set. However, there are some additional special reserved keywords that can be used as keys: "html" (which is the HTML that will be inside the icon HTML tag), "tagName" (which is an alternative tagName than "span"), and "class" (additional class names that will be merged with the sprite icon CSS classes)
639 * @return string the full HTML tag (usually a <span>)
640 * @access public
641 */
642 public static function getSpriteIconForFile($fileExtension, array $options = array()) {
643 $innerHtml = (isset($options['html']) ? $options['html'] : NULL);
644 $tagName = (isset($options['tagName']) ? $options['tagName'] : NULL);
645
646 // create the CSS class
647 $options['class'] = self::mapFileExtensionToSpriteIconClass($fileExtension) . (isset($options['class']) ? ' ' . $options['class'] : '');
648
649 unset($options['html']);
650 unset($options['tagName']);
651 return self::buildSpriteHtmlIconTag($options, $innerHtml, $tagName);
652 }
653
654
655 /**
656 * Generates the spriteicon css classes name for a given path or fileExtension
657 * usually called from getSpriteIconForFile or ExtJs Provider
658 *
659 * @param string fileExtension can be jpg, gif etc, but also be 'mount' or 'folder', but can also be a full path which will be resolved then
660 * @return string the string of the CSS class, see t3lib_iconworks::$fileSpriteIconNames
661 * @access private
662 */
663 public static function mapFileExtensionToSpriteIconClass($fileExtension) {
664 return self::getSpriteIconClasses(self::mapFileExtensionToSpriteIconName($fileExtension));
665 }
666
667 /**
668 * Generates the spriteicon name for a given path or fileExtension
669 * usually called from mapFileExtensionToSpriteIconClass and tceforms
670 *
671 * @param string fileExtension can be jpg, gif etc, but also be 'mount' or 'folder', but can also be a full path which will be resolved then
672 * @return string the string of the CSS class, see t3lib_iconworks::$fileSpriteIconNames
673 * @access private
674 */
675 public static function mapFileExtensionToSpriteIconName($fileExtension) {
676
677 // if the file is a whole file with name etc (mainly, if it has a "." or a "/"),
678 // then it is checked whether it is a valid directory
679 if (strpos($fileExtension, '.') !== FALSE || strpos($fileExtension, '/') !== FALSE) {
680
681 // check if it is a directory
682 $filePath = dirname(t3lib_div::getIndpEnv('SCRIPT_FILENAME')) . '/' . $GLOBALS['BACK_PATH'] . $fileExtension;
683 $path = t3lib_div::resolveBackPath($filePath);
684 if (is_dir($path) || substr($fileExtension, -1) === '/' || substr($fileExtension, -1) === '\\') {
685 $fileExtension = 'folder';
686 } else {
687 if (($pos = strrpos($fileExtension, '.')) !== FALSE) {
688 $fileExtension = strtolower(substr($fileExtension, $pos + 1));
689 } else {
690 $fileExtension = 'default';
691 }
692 }
693 }
694
695 // if the file extension is not valid
696 // then use the default one
697 if (!isset(self::$fileSpriteIconNames[$fileExtension])) {
698 $fileExtension = 'default';
699 }
700 $iconName = self::$fileSpriteIconNames[$fileExtension];
701
702 return $iconName;
703 }
704
705
706 /**
707 * This method is used throughout the TYPO3 Backend to show icons for a DB record
708 *
709 * Generates a HTML tag with proper CSS classes. The TYPO3 skin has defined these CSS classes
710 * already to have a pre-defined background image, and the correct background-position to show
711 * the necessary icon.
712 *
713 * @param string $table the TCA table name
714 * @param array $row the DB record of the TCA table
715 * @param array $options an associative array with additional options and attributes for the tag. by default, the key is the name of the attribute, and the value is the parameter string that is set. However, there are some additional special reserved keywords that can be used as keys: "html" (which is the HTML that will be inside the icon HTML tag), "tagName" (which is an alternative tagName than "span"), and "class" (additional class names that will be merged with the sprite icon CSS classes)
716 * @return string the full HTML tag (usually a <span>)
717 * @access public
718 */
719 public static function getSpriteIconForRecord($table, array $row, array $options = array()) {
720 $innerHtml = (isset($options['html']) ? $options['html'] : NULL);
721 $tagName = (isset($options['tagName']) ? $options['tagName'] : NULL);
722
723 // overlay this record icon with the status of the row
724 $overlaySpriteIconName = self::mapRecordOverlayToSpriteIconName($table, $row);
725 if ($overlaySpriteIconName) {
726 $overlayOptions = array(
727 'html' => $innerHtml,
728 'class' => 't3-icon-overlay'
729 );
730 $innerHtml = self::getSpriteIcon($overlaySpriteIconName, $overlayOptions);
731 }
732
733 // fetch the name for the CSS class, based on the $row
734 $options['class'] = self::mapRecordTypeToSpriteIconClass($table, $row) . (isset($options['class']) ? ' ' . $options['class'] : '');
735
736 unset($options['html']);
737 unset($options['tagName']);
738 return self::buildSpriteHtmlIconTag($options, $innerHtml, $tagName);
739 }
740
741
742 /**
743 * this helper functions looks up the column that is used for the type of
744 * the chosen TCA table. And then fetches the corresponding class
745 * based on the chosen iconsprite class in this TCA
746 * The TCA looks up
747 * - [ctrl][typeicon_column]
748 * -
749 * This method solely takes care of the type of this record, not any
750 * statuses, used for overlays.
751 * You should not use this directly besides if you need classes for ExtJS iconCls.
752 *
753 * see t3lib/stddb/tables.php for an example with the TCA table "pages"
754 *
755 * @param string $table the TCA table
756 * @param array $row the selected record
757 * @return string the CSS class for the sprite icon of that DB record
758 * @access private
759 **/
760 public static function mapRecordTypeToSpriteIconClass($table, array $row) {
761 return self::getSpriteIconClasses(self::mapRecordTypeToSpriteIconName($table, $row));
762 }
763
764 /**
765 * this helper functions looks up the column that is used for the type of
766 * the chosen TCA table. And then fetches the corresponding iconname
767 * based on the chosen iconsprite class in this TCA
768 * The TCA looks up
769 * - [ctrl][typeicon_column]
770 * -
771 * This method solely takes care of the type of this record, not any
772 * statuses, used for overlays.
773 * You should not use this directly besides if you need it in tceforms/core classes
774 *
775 * see t3lib/stddb/tables.php for an example with the TCA table "pages"
776 *
777 * @param string $table the TCA table
778 * @param array $row the selected record
779 * @return string the CSS class for the sprite icon of that DB record
780 * @access private
781 **/
782 public static function mapRecordTypeToSpriteIconName($table, array $row) {
783 $recordType = array();
784 $ref = NULL;
785 if (isset($GLOBALS['TCA'][$table]['ctrl']['typeicon_column'])) {
786 $column = $GLOBALS['TCA'][$table]['ctrl']['typeicon_column'];
787
788 if (isset($row[$column])) {
789 $recordType[1] = $row[$column];
790 } else {
791 $recordType[1] = 'default';
792 }
793
794 // workaround to give nav_hide pages a complete different icon
795 // although it's not a separate doctype
796 // and to give root-pages an own icon
797 if ($table === 'pages') {
798 if ($row['nav_hide']) {
799 $recordType[2] = $recordType[1] . '-hideinmenu';
800 }
801 if ($row['is_siteroot']) {
802 $recordType[3] = $recordType[1] . '-root';
803 }
804 if ($row['module']) {
805 $recordType[4] = 'contains-' . $row['module'];
806 }
807 }
808
809 if (is_array($GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'])) {
810 foreach ($recordType AS $key => $type) {
811 if (isset($GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'][$type])) {
812 $recordType[$key] = $GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'][$type];
813 } else {
814 unset($recordType[$key]);
815 }
816 }
817 $recordType[0] = $GLOBALS['TCA'][$table]['ctrl']['typeicon_classes']['default'];
818 if (isset($GLOBALS['TCA'][$table]['ctrl']['typeicon_classes']['mask'])) {
819 $recordType[5] = str_replace('###TYPE###', $row[$column], $GLOBALS['TCA'][$table]['ctrl']['typeicon_classes']['mask']);
820 }
821 if (isset($GLOBALS['TCA'][$table]['ctrl']['typeicon_classes']['userFunc'])) {
822 $parameters = array('row' => $row);
823 $recordType[6] = t3lib_div::callUserFunction(
824 $GLOBALS['TCA'][$table]['ctrl']['typeicon_classes']['userFunc'],
825 $parameters,
826 $ref
827 );
828 }
829 } else {
830 foreach ($recordType as &$type) {
831 $type = 'tcarecords-' . $table . '-' . $type;
832 }
833 unset($type);
834 $recordType[0] = 'tcarecords-' . $table . '-default';
835 }
836 } else {
837 if (is_array($GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'])) {
838 $recordType[0] = $GLOBALS['TCA'][$table]['ctrl']['typeicon_classes']['default'];
839 } else {
840 $recordType[0] = 'tcarecords-' . $table . '-default';
841 }
842 }
843 krsort($recordType);
844 foreach ($recordType as $iconName) {
845 if (in_array($iconName, $GLOBALS['TBE_STYLES']['spriteIconApi']['iconsAvailable'])) {
846 return $iconName;
847 }
848 }
849 return 'status-status-icon-missing';
850 }
851
852
853 /**
854 * this helper functions checks if the DB record ($row) has any special status
855 * based on the TCA settings like hidden, starttime etc, and then returns a specific
856 * Sprite icon class for the overlay of this DB record
857 * This method solely takes care of the overlay of this record, not any type
858 *
859 * Please note that this only returns one overlay, one status, that is prioritized
860 * by $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconRecordOverlayPriorities']
861 * We wanted to not have these icons blown over by tons of overlays, so this is limited
862 * to just one.
863 *
864 * see t3lib/config_default.php for the default options, you will find
865 * $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconRecordOverlayNames'] that shows
866 * the list of CSS classes that will be used for the sprites, mapped to the statuses here
867 *
868 * @param string $table the TCA table
869 * @param array $row the selected record
870 * @return string the CSS class for the sprite icon of that DB record
871 * @access private
872 */
873 public static function mapRecordOverlayToSpriteIconName($table, array $row) {
874 $tcaCtrl = $GLOBALS['TCA'][$table]['ctrl'];
875
876 // Calculate for a given record the actual visibility at the moment
877 $status = array(
878 'hidden' => FALSE,
879 'starttime' => FALSE,
880 'endtime' => FALSE,
881 'futureendtime' => FALSE,
882 'fe_group' => FALSE,
883 'deleted' => FALSE,
884 'protectedSection' => FALSE,
885 'nav_hide' => ($row['nav_hide'] ? TRUE : FALSE),
886 'noIconFound' => ($row['_NO_ICON_FOUND'] ? TRUE : FALSE),
887 );
888
889 // Icon state based on "enableFields":
890 if (is_array($tcaCtrl['enablecolumns'])) {
891 $enCols = $tcaCtrl['enablecolumns'];
892 // If "hidden" is enabled:
893 if ($tcaCtrl['enablecolumns']['disabled'] && $row[$tcaCtrl['enablecolumns']['disabled']]) {
894 $status['hidden'] = TRUE;
895 }
896 // If a "starttime" is set and higher than current time:
897 if ($tcaCtrl['enablecolumns']['starttime'] && $GLOBALS['EXEC_TIME'] < intval($row[$tcaCtrl['enablecolumns']['starttime']])) {
898 $status['starttime'] = TRUE;
899 }
900
901 // If an "endtime" is set
902 if ($tcaCtrl['enablecolumns']['endtime']) {
903 if (intval($row[$tcaCtrl['enablecolumns']['endtime']]) > 0) {
904 if (intval($row[$tcaCtrl['enablecolumns']['endtime']]) < $GLOBALS['EXEC_TIME']) {
905 // End-timing applies at this point.
906 $status['endtime'] = TRUE;
907 } else {
908 // End-timing WILL apply in the future for this element.
909 $status['futureendtime'] = TRUE;
910 }
911 }
912 }
913 // If a user-group field is set
914 if ($tcaCtrl['enablecolumns']['fe_group'] && $row[$tcaCtrl['enablecolumns']['fe_group']]) {
915 $status['fe_group'] = TRUE;
916 }
917 }
918
919 // If "deleted" flag is set (only when listing records which are also deleted!)
920 if ($row[$tcaCtrl['delete']]) {
921 $status['deleted'] = TRUE;
922 }
923
924 // Detecting extendToSubpages (for pages only)
925 if ($table == 'pages' && $row['extendToSubpages']) {
926 $status['protectedSection'] = TRUE;
927 }
928
929 // Hook: allow some other process to influence the choice of icon overlay
930 // The method called receives the table name, the current row and the current status array as parameters
931 // The status array should be passed as a reference and in order to be modified within the hook
932 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_iconworks.php']['overrideIconOverlay'])) {
933 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_iconworks.php']['overrideIconOverlay'] as $classRef) {
934 $hookObject = t3lib_div::getUserObj($classRef);
935 if (method_exists($hookObject, 'overrideIconOverlay')) {
936 $hookObject->overrideIconOverlay($table, $row, $status);
937 }
938 }
939 }
940
941 // now only show the status with the highest priority
942 $priorities = $GLOBALS['TBE_STYLES']['spriteIconApi']['spriteIconRecordOverlayPriorities'];
943
944 $iconName = '';
945 foreach ($priorities as $priority) {
946 if ($status[$priority]) {
947 $iconName = $GLOBALS['TBE_STYLES']['spriteIconApi']['spriteIconRecordOverlayNames'][$priority];
948 break;
949 }
950 }
951
952 return $iconName;
953 }
954
955
956 /**
957 * generic method to create the final CSS classes based on the sprite icon name
958 * with the base class and splits the name into parts
959 * is usually called by the methods that are responsible for fetching the names
960 * out of the file name, or the record type
961 *
962 * @param string $name iconname like 'actions-document-new'
963 * @return string a list of all CSS classes needed for the HTML tag
964 * @access public
965 */
966 public static function getSpriteIconClasses($iconName) {
967 $cssClasses = $baseCssClass = 't3-icon';
968 $parts = explode('-', $iconName);
969
970 if (count($parts) > 1) {
971 // will be something like "t3-icon-actions"
972 $cssClasses .= ' ' . ($baseCssClass . '-' . $parts[0]);
973 // will be something like "t3-icon-actions-document"
974 $cssClasses .= ' ' . ($baseCssClass . '-' . $parts[0] . '-' . $parts[1]);
975 // will be something like "t3-icon-document-new"
976 $cssClasses .= ' ' . ($baseCssClass . '-' . substr($iconName, strlen($parts[0]) + 1));
977 }
978 return $cssClasses;
979 }
980
981
982 /**
983 * low level function that generates the HTML tag for the sprite icon
984 * is usually called by the three API classes (getSpriteIcon, getSpriteIconForFile, getSpriteIconForRecord)
985 * it does not care about classes or anything else, but just plainly builds the HTML tag
986 *
987 * @param array $tagAttributes an associative array of additional tagAttributes for the HTML tag
988 * @param string $innerHtml (optional) the content within the tag, a "&nbsp;" by default
989 * @param string $tagName (optional) the name of the HTML element that should be used (span by default)
990 */
991 protected static function buildSpriteHtmlIconTag(array $tagAttributes, $innerHtml = NULL, $tagName = NULL) {
992 $innerHtml = ($innerHtml === NULL ? '&nbsp;' : $innerHtml);
993 $tagName = ($tagName === NULL ? 'span' : $tagName);
994 $attributes = '';
995 foreach ($tagAttributes as $attribute => $value) {
996 $attributes .= ' ' . htmlspecialchars($attribute) . '="' . htmlspecialchars($value) . '"';
997 }
998 return '<' . $tagName . $attributes . '>' . $innerHtml . '</' . $tagName . '>';
999 }
1000
1001 }
1002
1003 ?>