Fixed bug #17150: Workspaces non-admin user cannot send to next stage (Thanks to...
[Packages/TYPO3.CMS.git] / t3lib / thumbs.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 * Generates a thumbnail and returns an image stream, either GIF/PNG or JPG
29 *
30 * $Id$
31 * Revised for TYPO3 3.6 July/2003 by Kasper Skårhøj
32 *
33 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
34 */
35 /**
36 * [CLASS/FUNCTION INDEX of SCRIPT]
37 *
38 *
39 *
40 * 113: class SC_t3lib_thumbs
41 * 134: function init()
42 * 164: function main()
43 *
44 * SECTION: OTHER FUNCTIONS:
45 * 267: function errorGif($l1,$l2,$l3)
46 * 319: function fontGif($font)
47 * 366: function wrapFileName($inputName)
48 *
49 * TOTAL FUNCTIONS: 5
50 * (This index is automatically created/updated by the extension "extdeveval")
51 *
52 */
53
54
55 // *******************************
56 // Set error reporting
57 // *******************************
58 if (defined('E_DEPRECATED')) {
59 error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED);
60 } else {
61 error_reporting(E_ALL ^ E_NOTICE);
62 }
63
64
65
66 // ******************
67 // Constants defined
68 // ******************
69 define('TYPO3_OS', stristr(PHP_OS,'win')&&!stristr(PHP_OS,'darwin')?'WIN':'');
70 define('TYPO3_MODE','BE');
71
72 if(!defined('PATH_thisScript')) {
73 define('PATH_thisScript', str_replace('//', '/', str_replace('\\', '/',
74 (PHP_SAPI == 'fpm-fcgi' || PHP_SAPI == 'cgi' || PHP_SAPI == 'isapi' || PHP_SAPI == 'cgi-fcgi') &&
75 ($_SERVER['ORIG_PATH_TRANSLATED'] ? $_SERVER['ORIG_PATH_TRANSLATED'] : $_SERVER['PATH_TRANSLATED']) ?
76 ($_SERVER['ORIG_PATH_TRANSLATED'] ? $_SERVER['ORIG_PATH_TRANSLATED'] : $_SERVER['PATH_TRANSLATED']) :
77 ($_SERVER['ORIG_SCRIPT_FILENAME'] ? $_SERVER['ORIG_SCRIPT_FILENAME'] : $_SERVER['SCRIPT_FILENAME']))));
78 }
79
80 if(!defined('PATH_site')) define('PATH_site', preg_replace('/[^\/]*.[^\/]*$/','',PATH_thisScript)); // the path to the website folder (see init.php)
81 if(!defined('PATH_t3lib')) define('PATH_t3lib', PATH_site.'t3lib/');
82 define('PATH_typo3conf', PATH_site.'typo3conf/');
83 define('TYPO3_mainDir', 'typo3/'); // This is the directory of the backend administration for the sites of this TYPO3 installation.
84 define('PATH_typo3', PATH_site.TYPO3_mainDir);
85
86
87 // ******************
88 // Including config
89 // ******************
90 require_once(PATH_t3lib.'class.t3lib_div.php');
91 require_once(PATH_t3lib.'class.t3lib_extmgm.php');
92
93 require(PATH_t3lib.'config_default.php');
94 if (!defined ('TYPO3_db')) die ('The configuration file was not included.');
95 if (!$TYPO3_CONF_VARS['GFX']['image_processing']) die ('ImageProcessing was disabled!');
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 /**
114 * Class for generating a thumbnail from the input parameters given to the script
115 *
116 * Input GET var, &file: relative or absolute reference to an imagefile. WILL be validated against PATH_site / lockRootPath
117 * Input GET var, &size: integer-values defining size of thumbnail, format '[int]' or '[int]x[int]'
118 *
119 * Relative paths MUST BE the first two characters ONLY: eg: '../dir/file.gif', otherwise it is expect to be absolute
120 *
121 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
122 * @package TYPO3
123 * @subpackage t3lib
124 */
125 class SC_t3lib_thumbs {
126 var $include_once = array();
127
128 var $outdir = 'typo3temp/'; // The output directory of temporary files in PATH_site
129 var $output = '';
130 var $sizeDefault='56x56';
131
132 var $imageList; // Coming from $TYPO3_CONF_VARS['GFX']['imagefile_ext']
133 var $input; // Contains the absolute path to the file for which to make a thumbnail (after init())
134
135 // Internal, static: GPvar:
136 var $file; // Holds the input filename (GET: file)
137 var $size; // Holds the input size (GET: size)
138 var $mtime = 0; // Last modification time of the supplied file
139
140
141 /**
142 * Initialize; reading parameters with GPvar and checking file path
143 * Results in internal var, $this->input, being set to the absolute path of the file for which to make the thumbnail.
144 *
145 * @return void
146 */
147 function init() {
148 global $TYPO3_CONF_VARS;
149
150 // Setting GPvars:
151 $file = t3lib_div::_GP('file');
152 $size = t3lib_div::_GP('size');
153 $md5sum = t3lib_div::_GP('md5sum');
154
155 // Image extension list is set:
156 $this->imageList = $TYPO3_CONF_VARS['GFX']['imagefile_ext']; // valid extensions. OBS: No spaces in the list, all lowercase...
157
158 // If the filereference $this->file is relative, we correct the path
159 if (substr($file,0,3)=='../') {
160 $file = PATH_site.substr($file,3);
161 }
162
163 // Now the path is absolute.
164 // Checking for backpath and double slashes + the thumbnail can be made from files which are in the PATH_site OR the lockRootPath only!
165 if (t3lib_div::isAllowedAbsPath($file)) {
166 $mtime = filemtime($file);
167 }
168
169 // Do an MD5 check to prevent viewing of images without permission
170 $OK = FALSE;
171 if ($mtime) {
172 // Always use the absolute path for this check!
173 $check = basename($file).':'.$mtime.':'.$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
174 $md5_real = t3lib_div::shortMD5($check);
175 if (!strcmp($md5_real,$md5sum)) {
176 $OK = TRUE;
177 }
178 }
179
180 if ($OK) {
181 $this->input = $file;
182 $this->size = $size;
183 $this->mtime = $mtime;
184 } else {
185 throw new RuntimeException(
186 'TYPO3 Fatal Error: Image does not exist and/or MD5 checksum did not match.',
187 1270853950
188 );
189 }
190 }
191
192 /**
193 * Create the thumbnail
194 * Will exit before return if all is well.
195 *
196 * @return void
197 */
198 function main() {
199 global $TYPO3_CONF_VARS;
200
201 // If file exists, we make a thumbsnail of the file.
202 if ($this->input && file_exists($this->input)) {
203
204 // Check file extension:
205 $reg = array();
206 if (preg_match('/(.*)\.([^\.]*$)/',$this->input,$reg)) {
207 $ext=strtolower($reg[2]);
208 $ext=($ext=='jpeg')?'jpg':$ext;
209 if ($ext=='ttf') {
210 $this->fontGif($this->input); // Make font preview... (will not return)
211 } elseif (!t3lib_div::inList($this->imageList, $ext)) {
212 $this->errorGif('Not imagefile!',$ext,basename($this->input));
213 }
214 } else {
215 $this->errorGif('Not imagefile!','No ext!',basename($this->input));
216 }
217
218 // ... so we passed the extension test meaning that we are going to make a thumbnail here:
219 if (!$this->size) $this->size = $this->sizeDefault; // default
220
221 // I added extra check, so that the size input option could not be fooled to pass other values. That means the value is exploded, evaluated to an integer and the imploded to [value]x[value]. Furthermore you can specify: size=340 and it'll be translated to 340x340.
222 $sizeParts = explode('x', $this->size.'x'.$this->size); // explodes the input size (and if no "x" is found this will add size again so it is the same for both dimensions)
223 $sizeParts = array(t3lib_div::intInRange($sizeParts[0],1,1000),t3lib_div::intInRange($sizeParts[1],1,1000)); // Cleaning it up, only two parameters now.
224 $this->size = implode('x',$sizeParts); // Imploding the cleaned size-value back to the internal variable
225 $sizeMax = max($sizeParts); // Getting max value
226
227 // Init
228 $outpath = PATH_site.$this->outdir;
229
230 // Should be - ? 'png' : 'gif' - , but doesn't work (ImageMagick prob.?)
231 // René: png work for me
232 $thmMode = t3lib_div::intInRange($TYPO3_CONF_VARS['GFX']['thumbnails_png'],0);
233 $outext = ($ext!='jpg' || ($thmMode & 2)) ? ($thmMode & 1 ? 'png' : 'gif') : 'jpg';
234
235 $outfile = 'tmb_'.substr(md5($this->input.$this->mtime.$this->size),0,10).'.'.$outext;
236 $this->output = $outpath.$outfile;
237
238 if ($TYPO3_CONF_VARS['GFX']['im']) {
239 // If thumbnail does not exist, we generate it
240 if (!file_exists($this->output)) {
241 $parameters = '-sample ' . $this->size . ' ' . $this->wrapFileName($this->input) . '[0] ' . $this->wrapFileName($this->output);
242 $cmd = t3lib_div::imageMagickCommand('convert', $parameters);
243 t3lib_utility_Command::exec($cmd);
244 if (!file_exists($this->output)) {
245 $this->errorGif('No thumb','generated!',basename($this->input));
246 } else {
247 t3lib_div::fixPermissions($this->output);
248 }
249 }
250 // The thumbnail is read and output to the browser
251 if($fd = @fopen($this->output,'rb')) {
252 header('Content-type: image/'.$outext);
253 fpassthru($fd);
254 fclose($fd);
255 } else {
256 $this->errorGif('Read problem!','',$this->output);
257 }
258 } else exit;
259 } else {
260 $this->errorGif('No valid','inputfile!',basename($this->input));
261 }
262 }
263
264
265
266
267
268
269
270
271
272
273
274 /***************************
275 *
276 * OTHER FUNCTIONS:
277 *
278 ***************************/
279
280 /**
281 * Creates error image based on gfx/notfound_thumb.png
282 * Requires GD lib enabled, otherwise it will exit with the three textstrings outputted as text.
283 * Outputs the image stream to browser and exits!
284 *
285 * @param string Text line 1
286 * @param string Text line 2
287 * @param string Text line 3
288 * @return void
289 */
290 function errorGif($l1,$l2,$l3) {
291 global $TYPO3_CONF_VARS;
292
293 if (!$TYPO3_CONF_VARS['GFX']['gdlib']) {
294 throw new RuntimeException(
295 'TYPO3 Fatal Error: No gdlib. ' . $l1 . ' ' . $l2 . ' ' . $l3,
296 1270853952
297 );
298 }
299
300 // Creates the basis for the error image
301 if ($TYPO3_CONF_VARS['GFX']['gdlib_png']) {
302 header('Content-type: image/png');
303 $im = imagecreatefrompng(PATH_typo3.'gfx/notfound_thumb.png');
304 } else {
305 header('Content-type: image/gif');
306 $im = imagecreatefromgif(PATH_typo3.'gfx/notfound_thumb.gif');
307 }
308 // Sets background color and print color.
309 $white = imageColorAllocate($im, 0,0,0);
310 $black = imageColorAllocate($im, 255,255,0);
311
312 // Prints the text strings with the build-in font functions of GD
313 $x=0;
314 $font=0;
315 if ($l1) {
316 imagefilledrectangle($im, $x, 9, 56, 16, $black);
317 imageString($im,$font,$x,9,$l1,$white);
318 }
319 if ($l2) {
320 imagefilledrectangle($im, $x, 19, 56, 26, $black);
321 imageString($im,$font,$x,19,$l2,$white);
322 }
323 if ($l3) {
324 imagefilledrectangle($im, $x, 29, 56, 36, $black);
325 imageString($im,$font,$x,29,substr($l3,-14),$white);
326 }
327
328 // Outputting the image stream and exit
329 if ($TYPO3_CONF_VARS['GFX']['gdlib_png']) {
330 imagePng($im);
331 } else {
332 imageGif($im);
333 }
334 imagedestroy($im);
335 exit;
336 }
337
338 /**
339 * Creates a font-preview thumbnail.
340 * This means a PNG/GIF file with the text "AaBbCc...." set with the font-file given as input and in various sizes to show how the font looks
341 * Requires GD lib enabled.
342 * Outputs the image stream to browser and exits!
343 *
344 * @param string The filepath to the font file (absolute, probably)
345 * @return void
346 */
347 function fontGif($font) {
348 global $TYPO3_CONF_VARS;
349
350 if (!$TYPO3_CONF_VARS['GFX']['gdlib']) {
351 throw new RuntimeException(
352 'TYPO3 Fatal Error: No gdlib.',
353 1270853953
354 );
355 }
356
357 // Create image and set background color to white.
358 $im = imageCreate(250,76);
359 $white = imageColorAllocate($im, 255,255,255);
360 $col = imageColorAllocate($im, 0,0,0);
361
362 // The test string and offset in x-axis.
363 $string = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZzÆæØøÅåÄäÖöÜüß';
364 $x=13;
365
366 // Print (with non-ttf font) the size displayed
367 imagestring ($im, 1, 0, 2, '10', $col);
368 imagestring ($im, 1, 0, 15, '12', $col);
369 imagestring ($im, 1, 0, 30, '14', $col);
370 imagestring ($im, 1, 0, 47, '18', $col);
371 imagestring ($im, 1, 0, 68, '24', $col);
372
373 // Print with ttf-font the test string
374 imagettftext ($im, t3lib_div::freetypeDpiComp(10), 0, $x, 8, $col, $font, $string);
375 imagettftext ($im, t3lib_div::freetypeDpiComp(12), 0, $x, 21, $col, $font, $string);
376 imagettftext ($im, t3lib_div::freetypeDpiComp(14), 0, $x, 36, $col, $font, $string);
377 imagettftext ($im, t3lib_div::freetypeDpiComp(18), 0, $x, 53, $col, $font, $string);
378 imagettftext ($im, t3lib_div::freetypeDpiComp(24), 0, $x, 74, $col, $font, $string);
379
380 // Output PNG or GIF based on $TYPO3_CONF_VARS['GFX']['gdlib_png']
381 if ($TYPO3_CONF_VARS['GFX']['gdlib_png']) {
382 header('Content-type: image/png');
383 imagePng($im);
384 } else {
385 header('Content-type: image/gif');
386 imageGif($im);
387 }
388 imagedestroy($im);
389 exit;
390 }
391
392 /**
393 * Escapes a file name so it can safely be used on the command line.
394 *
395 * @param string $inputName filename to safeguard, must not be empty
396 *
397 * @return string $inputName escaped as needed
398 */
399 protected function wrapFileName($inputName) {
400 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']) {
401 $currentLocale = setlocale(LC_CTYPE, 0);
402 setlocale(LC_CTYPE, $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemLocale']);
403 }
404 $escapedInputName = escapeshellarg($inputName);
405 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']) {
406 setlocale(LC_CTYPE, $currentLocale);
407 }
408 return $escapedInputName;
409 }
410 }
411
412 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/thumbs.php'])) {
413 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/thumbs.php']);
414 }
415
416
417
418
419 // Make instance:
420 $SOBE = t3lib_div::makeInstance('SC_t3lib_thumbs');
421 $SOBE->init();
422 $SOBE->main();
423
424 ?>