[TASK] Remove trailing newlines from EOF
[Packages/TYPO3.CMS.git] / typo3 / sysext / cms / tslib / showpic.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 * Shows a picture from uploads/* in enlarged format in a separate window.
29 * Picture file and settings is supplied by GET-parameters: file, width, height, sample, alternativeTempPath, effects, frame, bodyTag, title, wrap, md5
30 *
31 * Revised for TYPO3 3.6 June/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 * 112: class SC_tslib_showpic
41 * 133: function init()
42 * 190: function main()
43 * 237: function printContent()
44 *
45 * TOTAL FUNCTIONS: 3
46 * (This index is automatically created/updated by the extension "extdeveval")
47 *
48 */
49
50
51
52
53 // *******************************
54 // Set error reporting
55 // *******************************
56 error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED);
57
58
59 // ***********************
60 // Paths are setup
61 // ***********************
62 define('TYPO3_OS', stristr(PHP_OS,'win')&&!stristr(PHP_OS,'darwin')?'WIN':'');
63 define('TYPO3_MODE','FE');
64
65 if(!defined('PATH_thisScript')) {
66 define('PATH_thisScript', str_replace('//', '/', str_replace('\\', '/',
67 (PHP_SAPI == 'fpm-fcgi' || PHP_SAPI == 'cgi' || PHP_SAPI == 'isapi' || PHP_SAPI == 'cgi-fcgi') &&
68 ($_SERVER['ORIG_PATH_TRANSLATED'] ? $_SERVER['ORIG_PATH_TRANSLATED'] : $_SERVER['PATH_TRANSLATED']) ?
69 ($_SERVER['ORIG_PATH_TRANSLATED'] ? $_SERVER['ORIG_PATH_TRANSLATED'] : $_SERVER['PATH_TRANSLATED']) :
70 ($_SERVER['ORIG_SCRIPT_FILENAME'] ? $_SERVER['ORIG_SCRIPT_FILENAME'] : $_SERVER['SCRIPT_FILENAME']))));
71 }
72
73 if (!defined('PATH_site')) define('PATH_site', dirname(PATH_thisScript).'/');
74 if (!defined('PATH_t3lib')) define('PATH_t3lib', PATH_site.'t3lib/');
75 define('PATH_tslib', PATH_site.'tslib/');
76 define('PATH_typo3conf', PATH_site.'typo3conf/');
77 define('TYPO3_mainDir', 'typo3/'); // This is the directory of the backend administration for the sites of this TYPO3 installation.
78
79 if (!@is_dir(PATH_typo3conf)) die('Cannot find configuration. This file is probably executed from the wrong location.');
80
81 require_once(PATH_t3lib.'class.t3lib_div.php');
82 require_once(PATH_t3lib.'class.t3lib_extmgm.php');
83
84 // ******************
85 // Including config
86 // ******************
87 require_once(PATH_t3lib.'config_default.php');
88 if (!defined ('TYPO3_db')) die ('The configuration file was not included.');
89
90 require_once(PATH_t3lib.'class.t3lib_db.php');
91 $TYPO3_DB = t3lib_div::makeInstance('t3lib_DB');
92
93
94 // *********************
95 // Error & Exception handling
96 // *********************
97 if ($TYPO3_CONF_VARS['SC_OPTIONS']['errors']['exceptionHandler'] !== '') {
98 if ($TYPO3_CONF_VARS['SYS']['errorHandler'] !== '') {
99 // register an error handler for the given errorHandlerErrors
100 $errorHandler = t3lib_div::makeInstance($TYPO3_CONF_VARS['SYS']['errorHandler'], $TYPO3_CONF_VARS['SYS']['errorHandlerErrors']);
101 // set errors which will be converted in an exception
102 $errorHandler->setExceptionalErrors($TYPO3_CONF_VARS['SC_OPTIONS']['errors']['exceptionalErrors']);
103 }
104 $exceptionHandler = t3lib_div::makeInstance($TYPO3_CONF_VARS['SC_OPTIONS']['errors']['exceptionHandler']);
105 }
106
107
108
109
110
111
112 # NOTICE: ALL LINES above can be commented out since this script is now used via the ?eID=tx_cms_showpic parameter passed to index.php!
113 # For backwards compatibility in extensions using showpic.php directly this is kept for the version 4.0 until 4.5 where it is planned removed!
114
115 if (!defined ('PATH_typo3conf')) die ('The configuration path was not properly defined!');
116 require_once(PATH_t3lib.'class.t3lib_stdgraphic.php');
117
118
119
120
121
122 /**
123 * Script Class, generating the page output.
124 * Instantiated in the bottom of this script.
125 *
126 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
127 * @package TYPO3
128 * @subpackage tslib
129 */
130 class SC_tslib_showpic {
131 var $content; // Page content accumulated here.
132
133 // Parameters loaded into these internal variables:
134 var $file;
135 var $width;
136 var $height;
137 var $sample;
138 var $alternativeTempPath;
139 var $effects;
140 var $frame;
141 var $bodyTag;
142 var $title;
143 var $wrap;
144 var $md5;
145
146 /**
147 * @var string
148 */
149 protected $parametersEncoded;
150
151 /**
152 * Init function, setting the input vars in the global space.
153 *
154 * @return void
155 */
156 function init() {
157 // Loading internal vars with the GET/POST parameters from outside:
158 $this->file = t3lib_div::_GP('file');
159 $parametersArray = t3lib_div::_GP('parameters');
160 $this->frame = t3lib_div::_GP('frame');
161 $this->md5 = t3lib_div::_GP('md5');
162
163 // ***********************
164 // Check parameters
165 // ***********************
166 // If no file-param or parameters are given, we must exit
167 if (!$this->file || !isset($parametersArray) || !is_array($parametersArray)) {
168 throw new UnexpectedValueException('Parameter Error: No file or no parameters given.', 1299514081);
169 }
170
171 $this->parametersEncoded = implode($parametersArray);
172
173 // Chech md5-checksum: If this md5-value does not match the one submitted, then we fail... (this is a kind of security that somebody don't just hit the script with a lot of different parameters
174 $md5_value = t3lib_div::hmac(
175 implode(
176 '|',
177 array($this->file, $this->parametersEncoded)
178 )
179 );
180
181 if ($md5_value!=$this->md5) {
182 throw new UnexpectedValueException('Parameter Error: Wrong parameters sent.', 1299514082);
183 }
184
185 $parameters = unserialize(base64_decode($this->parametersEncoded));
186 foreach ($parameters as $parameterName => $parameterValue) {
187 $this->$parameterName = $parameterValue;
188 }
189
190 // ***********************
191 // Check the file. If must be in a directory beneath the dir of this script...
192 // $this->file remains unchanged, because of the code in stdgraphic, but we do check if the file exists within the current path
193 // ***********************
194
195 $test_file=PATH_site.$this->file;
196 if (!t3lib_div::validPathStr($test_file)) {
197 throw new UnexpectedValueException('Parameter Error: No valid filepath', 1299514083);
198 }
199 if (!@is_file($test_file)) {
200 throw new UnexpectedValueException('The given file was not found', 1299514084);
201 }
202 }
203
204 /**
205 * Main function which creates the image if needed and outputs the HTML code for the page displaying the image.
206 * Accumulates the content in $this->content
207 *
208 * @return void
209 */
210 function main() {
211
212 // Creating stdGraphic object, initialize it and make image:
213 $img = t3lib_div::makeInstance('t3lib_stdGraphic');
214 $img->mayScaleUp = 0;
215 $img->init();
216 if ($this->sample) {$img->scalecmd = '-sample';}
217 if ($this->alternativeTempPath && t3lib_div::inList($GLOBALS['TYPO3_CONF_VARS']['FE']['allowedTempPaths'],$this->alternativeTempPath)) {
218 $img->tempPath = $this->alternativeTempPath;
219 }
220
221 // Need to connect to database, because this is used (typo3temp_db_tracking, cached image dimensions).
222 $GLOBALS['TYPO3_DB']->sql_pconnect(TYPO3_db_host, TYPO3_db_username, TYPO3_db_password);
223 $GLOBALS['TYPO3_DB']->sql_select_db(TYPO3_db);
224
225 if (strstr($this->width.$this->height, 'm')) {$max='m';} else {$max='';}
226
227 $this->height = t3lib_div::intInRange($this->height,0);
228 $this->width = t3lib_div::intInRange($this->width,0);
229 if ($this->frame) {$this->frame = intval($this->frame);}
230 $imgInfo = $img->imageMagickConvert($this->file,'web',$this->width.$max,$this->height,$img->IMparams($this->effects),$this->frame,'');
231
232 // Create HTML output:
233 $this->content='';
234 $this->content.='
235 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
236
237 <html>
238 <head>
239 <title>'.htmlspecialchars($this->title ? $this->title : "Image").'</title>
240 ' . ($this->title ? '' : '<meta name="robots" content="noindex,follow" />') . '
241 </head>
242 '.($this->bodyTag ? $this->bodyTag : '<body>');
243
244 if (is_array($imgInfo)) {
245 $wrapParts = explode('|',$this->wrap);
246 $this->content.=trim($wrapParts[0]).$img->imgTag($imgInfo).trim($wrapParts[1]);
247 }
248 $this->content.='
249 </body>
250 </html>';
251 }
252
253 /**
254 * Outputs the content from $this->content
255 *
256 * @return void
257 */
258 function printContent() {
259 echo $this->content;
260 }
261 }
262
263
264 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['tslib/showpic.php'])) {
265 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['tslib/showpic.php']);
266 }
267
268
269
270 // Make instance:
271 $SOBE = t3lib_div::makeInstance('SC_tslib_showpic');
272 $SOBE->init();
273 $SOBE->main();
274 $SOBE->printContent();
275 ?>