| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* jQuery File Upload Plugin PHP Example 5.2.7
|
|
|
4 |
* https://github.com/blueimp/jQuery-File-Upload
|
|
|
5 |
*
|
|
|
6 |
* Copyright 2010, Sebastian Tschan
|
|
|
7 |
* https://blueimp.net
|
|
|
8 |
*
|
|
|
9 |
* Licensed under the MIT license:
|
|
|
10 |
* http://creativecommons.org/licenses/MIT/
|
|
|
11 |
*/
|
|
|
12 |
require "common.php";
|
|
|
13 |
error_reporting( E_ALL | E_STRICT );
|
| 2 |
lars |
14 |
define( "__USE_MDB2__", false );
|
| 1 |
lars |
15 |
$GLOBALS["web"] = $webs;
|
| 2 |
lars |
16 |
$GLOBALS["web_rechte"] = $web_rechte;
|
| 1 |
lars |
17 |
$GLOBALS["site"] = $site;
|
|
|
18 |
if ( preg_match( "/\.local$/", $_SERVER["SERVER_NAME"] ) )
|
|
|
19 |
{
|
|
|
20 |
$GLOBALS["site"] .= ".local";
|
|
|
21 |
}
|
|
|
22 |
$options = null;
|
|
|
23 |
$real_url = "http://www." . $GLOBALS["site"] . '/images/upload/';
|
|
|
24 |
$real_folder = $GLOBALS["web"]["verzeichnis"] . "/images/upload/";
|
|
|
25 |
$folder = "";
|
|
|
26 |
$GLOBALS["folder"] = "";
|
|
|
27 |
if ( isset( $_POST["folder"] ) && $_POST["folder"] )
|
|
|
28 |
{
|
|
|
29 |
$folder = Weban_Utils::clean_global_input( "folder" );
|
|
|
30 |
} elseif ( isset( $_GET["folder"] ) && $_GET["folder"] )
|
|
|
31 |
{
|
|
|
32 |
$folder = Weban_Utils::clean_global_input( "folder", "get" );
|
|
|
33 |
}
|
| 2 |
lars |
34 |
$Imagedaten = array_chunk( explode( ";", $GLOBALS["web_rechte"]["admin"]["toolbox"]["bildverwaltung"] ),
|
|
|
35 |
4 );
|
|
|
36 |
|
| 1 |
lars |
37 |
if ( $folder )
|
|
|
38 |
{
|
| 2 |
lars |
39 |
$folder = rtrim( $folder, "/" );
|
| 1 |
lars |
40 |
$GLOBALS["folder"] = $folder;
|
|
|
41 |
$folder .= "/";
|
|
|
42 |
$real_folder .= $folder;
|
|
|
43 |
$real_url .= $folder;
|
|
|
44 |
$options["upload_dir"] = $real_folder;
|
|
|
45 |
$options["upload_url"] = $real_url;
|
| 2 |
lars |
46 |
foreach ( $Imagedaten as $set )
|
|
|
47 |
{
|
|
|
48 |
$options["image_versions"][$set[0]] = array( "upload_dir" => $real_folder .
|
|
|
49 |
$set[1] . "/", "upload_url" => $real_url . $set[1] . "/",
|
|
|
50 |
"max_width" => $set[2], "max_height" => $set[3], );
|
|
|
51 |
}
|
| 1 |
lars |
52 |
}
|
|
|
53 |
|
|
|
54 |
class UploadHandler
|
|
|
55 |
{
|
|
|
56 |
private $options;
|
|
|
57 |
|
|
|
58 |
function __construct( $options = null )
|
|
|
59 |
{
|
| 2 |
lars |
60 |
$Imagedaten = array_chunk( explode( ";", $GLOBALS["web_rechte"]["admin"]["toolbox"]["bildverwaltung"] ),
|
|
|
61 |
4 );
|
| 1 |
lars |
62 |
$this->options = array( 'script_url' => $_SERVER['PHP_SELF'],
|
|
|
63 |
'upload_dir' => $GLOBALS["web"]["verzeichnis"] .
|
|
|
64 |
'/images/upload/', 'upload_url' => "http://www." . $GLOBALS["site"] .
|
|
|
65 |
'/images/upload/', 'param_name' => 'files',
|
|
|
66 |
// The php.ini settings upload_max_filesize and post_max_size
|
|
|
67 |
// take precedence over the following max_file_size setting:
|
| 2 |
lars |
68 |
'max_file_size' => null, 'min_file_size' => 1, 'accept_file_types' =>
|
|
|
69 |
'/(\.|\/)(gif|jpe?g|png)$/i', 'max_number_of_files' => null,
|
|
|
70 |
'discard_aborted_uploads' => true,
|
|
|
71 |
/*'image_versions' => array( // Uncomment the following version to restrict the size of
|
| 1 |
lars |
72 |
// uploaded images. You can also add additional versions with
|
| 2 |
lars |
73 |
// their own upload directories:
|
|
|
74 |
'large' => array( 'upload_dir' => $GLOBALS["web"]["verzeichnis"] .
|
| 1 |
lars |
75 |
'/images/upload/gr/', 'upload_url' => "http://www." . $GLOBALS["site"] .
|
| 2 |
lars |
76 |
'/images/upload/gr/', 'max_width' => 1920, 'max_height' => 1920 ),
|
|
|
77 |
'thumbnail' => array( 'upload_dir' => $GLOBALS["web"]["verzeichnis"] .
|
| 1 |
lars |
78 |
'/images/upload/mini/', 'upload_url' => "http://www." . $GLOBALS["site"] .
|
| 2 |
lars |
79 |
'/images/upload/mini/', 'max_width' => 80, 'max_height' => 80, ), ),*/ );
|
|
|
80 |
foreach ( $Imagedaten as $set )
|
|
|
81 |
{
|
|
|
82 |
$this->options["image_versions"][$set[0]] = array( "upload_dir" =>
|
|
|
83 |
$GLOBALS["web"]["verzeichnis"] . '/images/upload/' . $set[1] .
|
|
|
84 |
"/", "upload_url" => "http://www." . $GLOBALS["site"] .
|
|
|
85 |
'/images/upload/' . $set[1] . "/", "max_width" => $set[2],
|
|
|
86 |
"max_height" => $set[3], );
|
|
|
87 |
}
|
| 1 |
lars |
88 |
if ( $options )
|
|
|
89 |
{
|
|
|
90 |
foreach ( $options as $o_id => $option )
|
|
|
91 |
{
|
|
|
92 |
if ( !is_array( $option ) )
|
|
|
93 |
{
|
|
|
94 |
$this->options[$o_id] = $option;
|
|
|
95 |
}
|
|
|
96 |
else
|
|
|
97 |
{
|
|
|
98 |
foreach ( $option as $o_key => $o_val )
|
|
|
99 |
{
|
|
|
100 |
foreach ( $o_val as $o_key2 => $o_val_2 )
|
|
|
101 |
{
|
|
|
102 |
$this->options[$o_id][$o_key][$o_key2] = $o_val_2;
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
//$this->options = array_merge( $this->options, $options );
|
|
|
108 |
}
|
| 2 |
lars |
109 |
//var_dump( $this->options );
|
|
|
110 |
//exit;
|
| 1 |
lars |
111 |
}
|
|
|
112 |
|
|
|
113 |
private function get_file_object( $file_name )
|
|
|
114 |
{
|
|
|
115 |
$file_path = $this->options['upload_dir'] . $file_name;
|
|
|
116 |
if ( is_file( $file_path ) && $file_name[0] !== '.' )
|
|
|
117 |
{
|
|
|
118 |
$file = new stdClass();
|
|
|
119 |
$file->name = $file_name;
|
|
|
120 |
$file->size = filesize( $file_path );
|
|
|
121 |
$file->url = $this->options['upload_url'] . rawurlencode( $file->
|
|
|
122 |
name );
|
|
|
123 |
foreach ( $this->options['image_versions'] as $version => $options )
|
|
|
124 |
{
|
|
|
125 |
if ( is_file( $options['upload_dir'] . $file_name ) )
|
|
|
126 |
{
|
|
|
127 |
$file->{$version . '_url'} = $options['upload_url'] .
|
|
|
128 |
rawurlencode( $file->name );
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
$file->delete_url = $this->options['script_url'] . '?file=' .
|
| 2 |
lars |
132 |
rawurlencode( $file->name ) . '&folder=' . $GLOBALS["folder"];
|
| 1 |
lars |
133 |
$file->delete_type = 'DELETE';
|
|
|
134 |
return $file;
|
|
|
135 |
}
|
|
|
136 |
return null;
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
private function get_file_objects()
|
|
|
140 |
{
|
|
|
141 |
return array_values( array_filter( array_map( array( $this,
|
|
|
142 |
'get_file_object' ), scandir( $this->options['upload_dir'] ) ) ) );
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
private function create_scaled_image( $file_name, $options )
|
|
|
146 |
{
|
|
|
147 |
$file_path = $this->options['upload_dir'] . $file_name;
|
|
|
148 |
$new_file_path = $options['upload_dir'] . $file_name;
|
|
|
149 |
list( $img_width, $img_height ) = @getimagesize( $file_path );
|
|
|
150 |
if ( !$img_width || !$img_height )
|
|
|
151 |
{
|
|
|
152 |
return false;
|
|
|
153 |
}
|
|
|
154 |
$scale = min( $options['max_width'] / $img_width, $options['max_height'] /
|
|
|
155 |
$img_height );
|
|
|
156 |
if ( $scale > 1 )
|
|
|
157 |
{
|
|
|
158 |
$scale = 1;
|
|
|
159 |
}
|
|
|
160 |
$new_width = $img_width * $scale;
|
|
|
161 |
$new_height = $img_height * $scale;
|
|
|
162 |
$new_img = @imagecreatetruecolor( $new_width, $new_height );
|
|
|
163 |
switch ( strtolower( substr( strrchr( $file_name, '.' ), 1 ) ) )
|
|
|
164 |
{
|
|
|
165 |
case 'jpg':
|
|
|
166 |
case 'jpeg':
|
|
|
167 |
$src_img = @imagecreatefromjpeg( $file_path );
|
|
|
168 |
$write_image = 'imagejpeg';
|
|
|
169 |
break;
|
|
|
170 |
case 'gif':
|
|
|
171 |
@imagecolortransparent( $new_img, @imagecolorallocate( $new_img,
|
|
|
172 |
0, 0, 0 ) );
|
|
|
173 |
$src_img = @imagecreatefromgif( $file_path );
|
|
|
174 |
$write_image = 'imagegif';
|
|
|
175 |
break;
|
|
|
176 |
case 'png':
|
|
|
177 |
@imagecolortransparent( $new_img, @imagecolorallocate( $new_img,
|
|
|
178 |
0, 0, 0 ) );
|
|
|
179 |
@imagealphablending( $new_img, false );
|
|
|
180 |
@imagesavealpha( $new_img, true );
|
|
|
181 |
$src_img = @imagecreatefrompng( $file_path );
|
|
|
182 |
$write_image = 'imagepng';
|
|
|
183 |
break;
|
|
|
184 |
default:
|
|
|
185 |
$src_img = $image_method = null;
|
|
|
186 |
}
|
| 2 |
lars |
187 |
$success = $src_img && @imagecopyresampled( $new_img, $src_img, 0, 0,
|
|
|
188 |
0, 0, $new_width, $new_height, $img_width, $img_height ) && $write_image( $new_img,
|
|
|
189 |
$new_file_path );
|
| 1 |
lars |
190 |
// Free up memory (imagedestroy does not delete files):
|
|
|
191 |
@imagedestroy( $src_img );
|
|
|
192 |
@imagedestroy( $new_img );
|
|
|
193 |
return $success;
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
private function has_error( $uploaded_file, $file, $error )
|
|
|
197 |
{
|
|
|
198 |
if ( $error )
|
|
|
199 |
{
|
|
|
200 |
return $error;
|
|
|
201 |
}
|
| 2 |
lars |
202 |
if ( !preg_match( $this->options['accept_file_types'], $file->name ) )
|
| 1 |
lars |
203 |
{
|
|
|
204 |
return 'acceptFileTypes';
|
|
|
205 |
}
|
|
|
206 |
if ( $uploaded_file && is_uploaded_file( $uploaded_file ) )
|
|
|
207 |
{
|
|
|
208 |
$file_size = filesize( $uploaded_file );
|
|
|
209 |
}
|
|
|
210 |
else
|
|
|
211 |
{
|
|
|
212 |
$file_size = $_SERVER['CONTENT_LENGTH'];
|
|
|
213 |
}
|
|
|
214 |
if ( $this->options['max_file_size'] && ( $file_size > $this->
|
|
|
215 |
options['max_file_size'] || $file->size > $this->options['max_file_size'] ) )
|
|
|
216 |
{
|
|
|
217 |
return 'maxFileSize';
|
|
|
218 |
}
|
| 2 |
lars |
219 |
if ( $this->options['min_file_size'] && $file_size < $this->options['min_file_size'] )
|
| 1 |
lars |
220 |
{
|
|
|
221 |
return 'minFileSize';
|
|
|
222 |
}
|
| 2 |
lars |
223 |
if ( is_int( $this->options['max_number_of_files'] ) && ( count( $this->
|
|
|
224 |
get_file_objects() ) >= $this->options['max_number_of_files'] ) )
|
| 1 |
lars |
225 |
{
|
|
|
226 |
return 'maxNumberOfFiles';
|
|
|
227 |
}
|
|
|
228 |
return $error;
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
private function handle_file_upload( $uploaded_file, $name, $size, $type,
|
|
|
232 |
$error )
|
|
|
233 |
{
|
|
|
234 |
$file = new stdClass();
|
|
|
235 |
// Remove path information and dots around the filename, to prevent uploading
|
|
|
236 |
// into different directories or replacing hidden system files.
|
|
|
237 |
// Also remove control characters and spaces (\x00..\x20) around the filename:
|
|
|
238 |
$file->name = trim( basename( stripslashes( $name ) ), ".\x00..\x20" );
|
|
|
239 |
$file->size = intval( $size );
|
|
|
240 |
$file->type = $type;
|
|
|
241 |
$error = $this->has_error( $uploaded_file, $file, $error );
|
|
|
242 |
if ( !$error && $file->name )
|
|
|
243 |
{
|
|
|
244 |
$file_path = $this->options['upload_dir'] . $file->name;
|
|
|
245 |
$append_file = !$this->options['discard_aborted_uploads'] &&
|
|
|
246 |
is_file( $file_path ) && $file->size > filesize( $file_path );
|
|
|
247 |
clearstatcache();
|
|
|
248 |
if ( $uploaded_file && is_uploaded_file( $uploaded_file ) )
|
|
|
249 |
{
|
|
|
250 |
// multipart/formdata uploads (POST method uploads)
|
|
|
251 |
if ( $append_file )
|
|
|
252 |
{
|
|
|
253 |
file_put_contents( $file_path, fopen( $uploaded_file,
|
|
|
254 |
'r' ), FILE_APPEND );
|
|
|
255 |
}
|
|
|
256 |
else
|
|
|
257 |
{
|
|
|
258 |
move_uploaded_file( $uploaded_file, $file_path );
|
|
|
259 |
}
|
|
|
260 |
}
|
|
|
261 |
else
|
|
|
262 |
{
|
|
|
263 |
// Non-multipart uploads (PUT method support)
|
|
|
264 |
file_put_contents( $file_path, fopen( 'php://input', 'r' ),
|
|
|
265 |
$append_file ? FILE_APPEND : 0 );
|
|
|
266 |
}
|
|
|
267 |
$file_size = filesize( $file_path );
|
|
|
268 |
if ( $file_size === $file->size )
|
|
|
269 |
{
|
|
|
270 |
$file->url = $this->options['upload_url'] . rawurlencode( $file->
|
|
|
271 |
name );
|
| 2 |
lars |
272 |
foreach ( $this->options['image_versions'] as $version => $options )
|
| 1 |
lars |
273 |
{
|
|
|
274 |
if ( $this->create_scaled_image( $file->name, $options ) )
|
|
|
275 |
{
|
|
|
276 |
$file->{$version . '_url'} = $options['upload_url'] .
|
|
|
277 |
rawurlencode( $file->name );
|
|
|
278 |
}
|
|
|
279 |
}
|
|
|
280 |
$sql = "INSERT INTO
|
|
|
281 |
bilder
|
|
|
282 |
SET
|
|
|
283 |
name='$file->name',
|
|
|
284 |
folder='" . $GLOBALS["folder"] . "',
|
|
|
285 |
erstellt_am=UNIX_TIMESTAMP(),
|
|
|
286 |
erstellt_von='" . $_SERVER["PHP_AUTH_USER"] . "'
|
|
|
287 |
";
|
|
|
288 |
mysql_query( $sql );
|
|
|
289 |
}
|
|
|
290 |
else
|
|
|
291 |
if ( $this->options['discard_aborted_uploads'] )
|
|
|
292 |
{
|
|
|
293 |
unlink( $file_path );
|
|
|
294 |
$file->error = 'abort';
|
|
|
295 |
}
|
|
|
296 |
$file->size = $file_size;
|
|
|
297 |
$file->delete_url = $this->options['script_url'] . '?file=' .
|
|
|
298 |
rawurlencode( $file->name );
|
|
|
299 |
$file->delete_type = 'DELETE';
|
|
|
300 |
}
|
|
|
301 |
else
|
|
|
302 |
{
|
|
|
303 |
$file->error = $error;
|
|
|
304 |
}
|
|
|
305 |
return $file;
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
public function get()
|
|
|
309 |
{
|
| 2 |
lars |
310 |
$file_name = isset( $_REQUEST['file'] ) ? basename( stripslashes( $_REQUEST['file'] ) ) : null;
|
| 1 |
lars |
311 |
if ( $file_name )
|
|
|
312 |
{
|
|
|
313 |
$info = $this->get_file_object( $file_name );
|
|
|
314 |
}
|
|
|
315 |
else
|
|
|
316 |
{
|
|
|
317 |
$info = $this->get_file_objects();
|
|
|
318 |
}
|
|
|
319 |
header( 'Content-type: application/json' );
|
|
|
320 |
echo json_encode( $info );
|
|
|
321 |
}
|
|
|
322 |
|
|
|
323 |
public function post()
|
|
|
324 |
{
|
|
|
325 |
$upload = isset( $_FILES[$this->options['param_name']] ) ? $_FILES[$this->
|
|
|
326 |
options['param_name']] : array( 'tmp_name' => null, 'name' => null,
|
|
|
327 |
'size' => null, 'type' => null, 'error' => null );
|
|
|
328 |
$info = array();
|
|
|
329 |
if ( is_array( $upload['tmp_name'] ) )
|
|
|
330 |
{
|
|
|
331 |
foreach ( $upload['tmp_name'] as $index => $value )
|
|
|
332 |
{
|
|
|
333 |
$info[] = $this->handle_file_upload( $upload['tmp_name'][$index],
|
|
|
334 |
isset( $_SERVER['HTTP_X_FILE_NAME'] ) ? $_SERVER['HTTP_X_FILE_NAME'] :
|
|
|
335 |
$upload['name'][$index], isset( $_SERVER['HTTP_X_FILE_SIZE'] ) ?
|
|
|
336 |
$_SERVER['HTTP_X_FILE_SIZE'] : $upload['size'][$index],
|
|
|
337 |
isset( $_SERVER['HTTP_X_FILE_TYPE'] ) ? $_SERVER['HTTP_X_FILE_TYPE'] :
|
|
|
338 |
$upload['type'][$index], $upload['error'][$index] );
|
|
|
339 |
}
|
|
|
340 |
}
|
|
|
341 |
else
|
|
|
342 |
{
|
| 2 |
lars |
343 |
$info[] = $this->handle_file_upload( $upload['tmp_name'], isset
|
|
|
344 |
( $_SERVER['HTTP_X_FILE_NAME'] ) ? $_SERVER['HTTP_X_FILE_NAME'] :
|
|
|
345 |
$upload['name'], isset( $_SERVER['HTTP_X_FILE_SIZE'] ) ? $_SERVER['HTTP_X_FILE_SIZE'] :
|
|
|
346 |
$upload['size'], isset( $_SERVER['HTTP_X_FILE_TYPE'] ) ? $_SERVER['HTTP_X_FILE_TYPE'] :
|
|
|
347 |
$upload['type'], $upload['error'] );
|
| 1 |
lars |
348 |
}
|
|
|
349 |
header( 'Vary: Accept' );
|
|
|
350 |
if ( isset( $_SERVER['HTTP_ACCEPT'] ) && ( strpos( $_SERVER['HTTP_ACCEPT'],
|
|
|
351 |
'application/json' ) !== false ) )
|
|
|
352 |
{
|
|
|
353 |
header( 'Content-type: application/json' );
|
|
|
354 |
}
|
|
|
355 |
else
|
|
|
356 |
{
|
|
|
357 |
header( 'Content-type: text/plain' );
|
|
|
358 |
}
|
|
|
359 |
echo json_encode( $info );
|
|
|
360 |
}
|
|
|
361 |
|
|
|
362 |
public function delete()
|
|
|
363 |
{
|
| 2 |
lars |
364 |
$file_name = isset( $_REQUEST['file'] ) ? basename( stripslashes( $_REQUEST['file'] ) ) : null;
|
| 1 |
lars |
365 |
$file_path = $this->options['upload_dir'] . $file_name;
|
| 2 |
lars |
366 |
$success = is_file( $file_path ) && $file_name[0] !== '.' && unlink( $file_path );
|
| 1 |
lars |
367 |
if ( $success )
|
|
|
368 |
{
|
|
|
369 |
foreach ( $this->options['image_versions'] as $version => $options )
|
|
|
370 |
{
|
|
|
371 |
$file = $options['upload_dir'] . $file_name;
|
|
|
372 |
if ( is_file( $file ) )
|
|
|
373 |
{
|
|
|
374 |
unlink( $file );
|
|
|
375 |
}
|
|
|
376 |
}
|
|
|
377 |
$sql = "DELETE FROM
|
|
|
378 |
bilder
|
|
|
379 |
WHERE
|
|
|
380 |
name='$file_name'
|
|
|
381 |
AND
|
|
|
382 |
folder='" . $GLOBALS["folder"] . "'
|
|
|
383 |
";
|
|
|
384 |
mysql_query( $sql );
|
|
|
385 |
}
|
|
|
386 |
header( 'Content-type: application/json' );
|
|
|
387 |
echo json_encode( $success );
|
|
|
388 |
}
|
|
|
389 |
}
|
|
|
390 |
|
|
|
391 |
$upload_handler = new UploadHandler( $options );
|
|
|
392 |
|
|
|
393 |
header( 'Pragma: no-cache' );
|
|
|
394 |
header( 'Cache-Control: private, no-cache' );
|
|
|
395 |
header( 'Content-Disposition: inline; filename="files.json"' );
|
|
|
396 |
header( 'X-Content-Type-Options: nosniff' );
|
|
|
397 |
|
|
|
398 |
switch ( $_SERVER['REQUEST_METHOD'] )
|
|
|
399 |
{
|
|
|
400 |
case 'HEAD':
|
|
|
401 |
case 'GET':
|
|
|
402 |
$upload_handler->get();
|
|
|
403 |
break;
|
|
|
404 |
case 'POST':
|
|
|
405 |
$upload_handler->post();
|
|
|
406 |
break;
|
|
|
407 |
case 'DELETE':
|
|
|
408 |
$upload_handler->delete();
|
|
|
409 |
break;
|
|
|
410 |
case 'OPTIONS':
|
|
|
411 |
break;
|
|
|
412 |
default:
|
|
|
413 |
header( 'HTTP/1.0 405 Method Not Allowed' );
|
|
|
414 |
}
|
| 2 |
lars |
415 |
?>
|