[BUGFIX] Throw correct Exceptions everywhere
[Packages/TYPO3.CMS.git] / typo3 / file_upload.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 * Web>File: Upload of files
29 *
30 * $Id$
31 * Revised for TYPO3 3.6 November/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 * 77: class SC_file_upload
41 * 103: function init()
42 * 171: function main()
43 * 241: function printContent()
44 *
45 * TOTAL FUNCTIONS: 3
46 * (This index is automatically created/updated by the extension "extdeveval")
47 *
48 */
49
50 $BACK_PATH = '';
51 require('init.php');
52 require('template.php');
53 $LANG->includeLLFile('EXT:lang/locallang_misc.xml');
54
55
56
57
58
59
60 /**
61 * Script Class for display up to 10 upload fields
62 *
63 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
64 * @package TYPO3
65 * @subpackage core
66 */
67 class SC_file_upload {
68
69 // External, static:
70 var $uploadNumber = 10;
71
72 // Internal, static:
73 /**
74 * Document template object
75 *
76 * @var smallDoc
77 */
78 var $doc;
79
80 /**
81 * File processing object
82 *
83 * @var t3lib_basicFileFunctions
84 */
85 var $basicff;
86 var $icon; // Will be set to the proper icon for the $target value.
87 var $shortPath; // Relative path to current found filemount
88 var $title; // Name of the filemount
89
90 /**
91 * Charset processing object
92 *
93 * @var t3lib_cs
94 */
95 protected $charsetConversion;
96
97 // Internal, static (GPVar):
98 var $number;
99 var $target; // Set with the target path inputted in &target
100 var $returnUrl; // Return URL of list module.
101
102 // Internal, dynamic:
103 var $content; // Accumulating content
104
105
106 /**
107 * Constructor for initializing the class
108 *
109 * @return void
110 */
111 function init() {
112 // Initialize GPvars:
113 $this->number = t3lib_div::_GP('number');
114 $this->target = t3lib_div::_GP('target');
115 $this->returnUrl = t3lib_div::sanitizeLocalUrl(t3lib_div::_GP('returnUrl'));
116 $this->returnUrl = $this->returnUrl ? $this->returnUrl : t3lib_div::getIndpEnv('TYPO3_SITE_URL') . TYPO3_mainDir . t3lib_extMgm::extRelPath('filelist') . 'mod1/file_list.php?id=' . rawurlencode($this->target);
117
118 // set the number of input fields
119 if (empty($this->number)) {
120 $this->number = $GLOBALS['BE_USER']->getTSConfigVal('options.defaultFileUploads');
121 }
122 $this->number = t3lib_div::intInRange($this->number, 1, $this->uploadNumber);
123
124 // Init basic-file-functions object:
125 $this->basicff = t3lib_div::makeInstance('t3lib_basicFileFunctions');
126 $this->basicff->init($GLOBALS['FILEMOUNTS'], $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
127
128 // Init basic-charset-functions object:
129 $this->charsetConversion = t3lib_div::makeInstance('t3lib_cs');
130
131 // Cleaning and checking target
132 $this->target = $this->charsetConversion->conv($this->target, 'utf-8', $GLOBALS['LANG']->charSet);
133 $this->target = $this->basicff->is_directory($this->target);
134 $key = $this->basicff->checkPathAgainstMounts($this->target . '/');
135 if (!$this->target || !$key) {
136 $title = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_mod_file_list.xml:paramError', TRUE);
137 $message = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_mod_file_list.xml:targetNoDir', TRUE);
138 throw new RuntimeException($title . ': ' . $message, 1294586845);
139 }
140
141 // Finding the icon
142 switch ($GLOBALS['FILEMOUNTS'][$key]['type']) {
143 case 'user':
144 $this->icon = 'gfx/i/_icon_ftp_user.gif';
145 break;
146 case 'group':
147 $this->icon = 'gfx/i/_icon_ftp_group.gif';
148 break;
149 default:
150 $this->icon = 'gfx/i/_icon_ftp.gif';
151 break;
152 }
153
154 $this->icon = '<img' . t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], $this->icon, 'width="18" height="16"') . ' title="" alt="" />';
155
156 // Relative path to filemount, $key:
157 $this->shortPath = substr($this->target, strlen($GLOBALS['FILEMOUNTS'][$key]['path']));
158
159 // Setting title:
160 $this->title = $this->icon . htmlspecialchars($GLOBALS['FILEMOUNTS'][$key]['name']) . ': ' . $this->shortPath;
161
162 // Setting template object
163 $this->doc = t3lib_div::makeInstance('template');
164 $this->doc->setModuleTemplate('templates/file_upload.html');
165 $this->doc->backPath = $GLOBALS['BACK_PATH'];
166 $this->doc->form = '<form action="tce_file.php" method="post" name="editform" enctype="' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['form_enctype'] . '">';
167
168 if($GLOBALS['BE_USER']->jsConfirmation(1)) {
169 $confirm = ' && confirm(' . $GLOBALS['LANG']->JScharCode($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.php:mess.redraw')) . ')';
170 } else {
171 $confirm = '';
172 }
173 $this->doc->JScode = $this->doc->wrapScriptTags('
174 var path = "'.$this->target.'";
175
176 function reload(a) { //
177 if (!changed || (changed ' . $confirm . ')) {
178 var params = "&target="+encodeURIComponent(path)+"&number="+a+"&returnUrl='
179 . urlencode($this->charsetConversion->conv($this->returnUrl, $GLOBALS['LANG']->charSet, 'utf-8'))
180 . '";
181 window.location.href = "file_upload.php?"+params;
182 }
183 }
184 function backToList() { //
185 top.goToModule("file_list");
186 }
187 var changed = 0;
188 ');
189 }
190
191
192 /**
193 * Main function, rendering the upload file form fields
194 *
195 * @return void
196 */
197 function main() {
198 // Make page header:
199 $this->content = $this->doc->startPage($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.php:file_upload.php.pagetitle'));
200
201 $form = $this->renderUploadForm();
202
203 $pageContent =
204 $this->doc->header($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.php:file_upload.php.pagetitle')) .
205 $this->doc->section('', $form);
206
207
208 // Header Buttons
209 $docHeaderButtons = array(
210 'csh' => t3lib_BEfunc::cshItem('xMOD_csh_corebe', 'file_upload', $GLOBALS['BACK_PATH'])
211 );
212
213 $markerArray = array(
214 'CSH' => $docHeaderButtons['csh'],
215 'FUNC_MENU' => t3lib_BEfunc::getFuncMenu($this->id, 'SET[function]', $this->MOD_SETTINGS['function'], $this->MOD_MENU['function']),
216 'CONTENT' => $pageContent,
217 'PATH' => $this->title,
218 );
219
220 $this->content .= $this->doc->moduleBody(array(), $docHeaderButtons, $markerArray);
221 $this->content .= $this->doc->endPage();
222 $this->content = $this->doc->insertStylesAndJS($this->content);
223 }
224
225
226 /**
227 * This function renders the upload form
228 *
229 * @return string the HTML form as a string, ready for outputting
230 */
231 function renderUploadForm() {
232 $content = '
233 <div id="c-select">
234 <label for="number-of-uploads">' .
235 $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.php:file_upload.php.number_of_files') .
236 '</label>
237 <select name="number" id="number-of-uploads" onchange="reload(this.options[this.selectedIndex].value);">';
238
239 for ($a = 1; $a <= $this->uploadNumber; $a++) {
240 $content .= '<option value="' . $a . '"' .
241 ($this->number == $a ? ' selected="selected"' : '' ) .
242 '>' . $a . '</option>';
243 }
244 $content .= '
245 </select>
246 </div>
247 ';
248
249
250 // Make checkbox for "overwrite"
251 $content .= '
252 <div id="c-override">
253 <input type="checkbox" class="checkbox" name="overwriteExistingFiles" id="overwriteExistingFiles" value="1" /> <label for="overwriteExistingFiles">' . $GLOBALS['LANG']->getLL('overwriteExistingFiles', 1) . '</label>
254 </div>
255 ';
256
257
258 // Produce the number of upload-fields needed:
259 $content .= '
260 <div id="c-upload">
261 ';
262 for ($a = 0; $a < $this->number; $a++) {
263 // Adding 'size="50" ' for the sake of Mozilla!
264 $content .= '
265 <input type="file" name="upload_' . $a . '"' . $this->doc->formWidth(35) . ' size="50" onclick="changed=1;" />
266 <input type="hidden" name="file[upload][' . $a . '][target]" value="' . htmlspecialchars($this->target) . '" />
267 <input type="hidden" name="file[upload][' . $a . '][data]" value="' . $a . '" /><br />
268 ';
269 }
270 $content .= '
271 </div>
272 ';
273
274 // Submit button:
275 $content .= '
276 <div id="c-submit">
277 <input type="submit" value="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.php:file_upload.php.submit', 1) . '" />
278 <input type="submit" value="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.php:labels.cancel', 1) . '" onclick="backToList(); return false;" />
279 <input type="hidden" name="redirect" value="' . htmlspecialchars($this->returnUrl) . '" />
280 </div>
281 ';
282
283 return $content;
284 }
285
286
287 /**
288 * Outputting the accumulated content to screen
289 *
290 * @return void
291 */
292 function printContent() {
293 echo $this->content;
294 }
295 }
296
297
298 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/file_upload.php'])) {
299 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/file_upload.php']);
300 }
301
302
303 // Make instance:
304 $SOBE = t3lib_div::makeInstance('SC_file_upload');
305 $SOBE->init();
306 $SOBE->main();
307 $SOBE->printContent();
308
309 ?>