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