Codechange: Klassen im Tabellenkopf eingefügt und via CSS dazugehörige Breitenangaben...
[wmmkf.git] / upload.php
blob48f89a539b9d804204d79808e4b950b785547488
1 <?php
2 include("inc.php");
4 if (file_exists('install.php')
5 or file_exists('update.php')
6 or file_exists('update_content.php'))
8 header("location: ".$settings['forum_address']."service.php");
9 die("<a href=\"service.php\">further...</a>");
12 if ($settings['upload_images']!=1) die('This feature is not activated.');
14 $uploaded_images_path = 'img/uploaded/';
15 $images_per_page = 30;
17 function resize($uploaded_file, $file, $new_width, $new_height, $compression=80) {
19 if (file_exists($file))
21 @chmod($file, 0777);
22 @unlink($file);
25 $image_info = getimagesize($uploaded_file);
27 if (!is_array($image_info) || $image_info[2] != 1 && $image_info[2] != 2 && $image_info[2] != 3) $error = true;
28 if (empty($error))
30 if($image_info[2]==1) // GIF
32 $current_image = @ImageCreateFromGIF($uploaded_file) or $error = true;
33 if (empty($error)) $new_image = @ImageCreate($new_width,$new_height) or $error = true;
34 if (empty($error)) @ImageCopyResampled($new_image,$current_image,0,0,0,0,$new_width,$new_height,$image_info[0],$image_info[1]) or $error=true;
35 if (empty($error)) @ImageGIF($new_image, $file) or $error = true;
37 else if ($image_info[2]==2) // JPG
39 $current_image = @ImageCreateFromJPEG($uploaded_file) or $error = true;
40 if (empty($error)) $new_image=@imagecreatetruecolor($new_width,$new_height) or $error = true;
41 if (empty($error)) @ImageCopyResampled($new_image,$current_image,0,0,0,0,$new_width,$new_height,$image_info[0],$image_info[1]) or $error = true;
42 if (empty($error)) @ImageJPEG($new_image, $file, $compression) or $error = true;
44 else if($image_info[2]==3) // PNG
46 $current_image=ImageCreateFromPNG($uploaded_file) or $error = true;
47 if (empty($error)) $new_image=imagecreatetruecolor($new_width,$new_height) or $error = true;
48 if (empty($error)) ImageCopyResampled($new_image,$current_image,0,0,0,0,$new_width,$new_height,$image_info[0],$image_info[1]) or $error = true;
49 if (empty($error)) ImagePNG($new_image, $file) or $error = $true;
53 if (empty($error)) return true;
54 else return false;
57 $action = (isset($_GET['action'])) ? $_GET['action'] : 'upload';
59 $lang['upload_exp'] = str_replace("[width]", $settings['upload_max_img_width'], $lang['upload_exp']);
60 $lang['upload_exp'] = str_replace("[height]", $settings['upload_max_img_height'], $lang['upload_exp']);
61 $lang['upload_exp'] = str_replace("[size]", $settings['upload_max_img_size'], $lang['upload_exp']);
63 if (isset($_FILES['probe']) && $_FILES['probe']['size'] != 0 && !$_FILES['probe']['error'])
65 unset($errors);
66 $image_info = getimagesize($_FILES['probe']['tmp_name']);
67 if (!is_array($image_info)
68 || ($image_info[2] != 1
69 && $image_info[2] != 2
70 && $image_info[2] != 3))
72 $errors[] = $lang['invalid_file_format'];
74 if (empty($errors))
76 if ($_FILES['probe']['size'] > $settings['upload_max_img_size']*1000
77 || $image_info[0] > $settings['upload_max_img_width']
78 || $image_info[1] > $settings['upload_max_img_height'])
80 $compression = 10;
81 $width=$image_info[0];
82 $height=$image_info[1];
83 if ($width >= $height)
85 $new_width = $settings['upload_max_img_width'];
86 $new_height = intval($height*$new_width/$width);
88 else
90 $new_height = $settings['upload_max_img_height'];
91 $new_width = intval($width*$new_height/$height);
93 $img_tmp_name = uniqid(rand()).'.tmp';
95 for ($compression = 100; $compression>9; $compression=$compression-10)
97 if (!resize($_FILES['probe']['tmp_name'], $uploaded_images_path.$img_tmp_name, $new_width, $new_height, $compression))
99 $file_size = @filesize($uploaded_images_path.$img_tmp_name);
100 break;
102 $file_size = @filesize($uploaded_images_path.$img_tmp_name);
103 if ($image_info[2]!=2 && $file_size > $settings['upload_max_img_size']*1000) break;
104 if ($file_size <= $settings['upload_max_img_size']*1000) break;
106 if ($file_size > $settings['upload_max_img_size']*1000)
108 $file_too_large_dump = str_replace("[width]",$image_info[0],$lang['file_too_large']);
109 $file_too_large_dump = str_replace("[height]",$image_info[1],$file_too_large_dump);
110 $file_too_large_dump = str_replace("[size]",number_format($_FILES['probe']['size']/1000,0,",",""),$file_too_large_dump);
111 $errors[] = $file_too_large_dump;
113 if (isset($errors))
115 if (file_exists($uploaded_images_path.$img_tmp_name))
117 @chmod($uploaded_images_path.$img_tmp_name, 0777);
118 @unlink($uploaded_images_path.$img_tmp_name);
124 if (empty($errors))
126 $nr = 0;
127 switch($image_info[2])
129 case 1:
130 for(;;) { $nr++; if (!file_exists($uploaded_images_path."image".$nr.".gif")) break; }
131 $filename = "image".$nr.".gif";
132 break;
133 case 2:
134 for(;;) { $nr++; if (!file_exists($uploaded_images_path."image".$nr.".jpg")) break; }
135 $filename = "image".$nr.".jpg";
136 break;
137 case 3:
138 for(;;) { $nr++; if (!file_exists($uploaded_images_path."image".$nr.".png")) break; }
139 $filename = "image".$nr.".png";
140 break;
142 if (isset($img_tmp_name))
144 @rename($uploaded_images_path.$img_tmp_name, $uploaded_images_path.$filename) or $errors[] = $lang['upload_error'];
145 $image_manipulated = str_replace('[width]',$new_width,$lang['image_manipulated']);
146 $image_manipulated = str_replace('[height]',$new_height,$image_manipulated);
147 $image_manipulated = str_replace("[size]",number_format($file_size/1000,0,",",""),$image_manipulated);
149 else
151 @move_uploaded_file($_FILES['probe']['tmp_name'], $uploaded_images_path.$filename) or $errors[] = $lang['upload_error'];
154 if (empty($errors))
156 @chmod($uploaded_images_path.$filename, 0644);
157 $action = 'uploaded';
159 else $action = 'upload';
162 if (empty($errors))
164 if (isset($_FILES['probe']['error']))
166 $errors[] = str_replace('[maximum_file_size]',ini_get('upload_max_filesize'),$lang['upload_error_2']);
170 if(isset($_GET['uploaded_image_selected']))
172 $filename = $_GET['uploaded_image_selected'];
173 $action = 'uploaded';
176 ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
177 <html>
178 <head>
179 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
180 <title><?php echo strip_tags($lang['upload_image_title']); ?></title>
181 <style type="text/css">
182 <!--
183 body {
184 font-family:Verdana,Helvetica,sans-serif;
185 color:#000;
186 font-size:13px;
187 background:#fff;
188 margin:0;
189 padding:20px;
191 h1 {
192 margin:0 0 20px 0;
193 font-size:18px;
194 font-weight:bold;
196 .caution {
197 color:red;
198 font-weight:bold;
200 a:link {
201 color:#00c;
202 text-decoration:none;
204 a:visited {
205 color:#00c;
206 text-decoration:none;
208 a:focus, a:hover {
209 color:#00f;
210 text-decoration:underline;
213 </style>
214 <script type="text/javascript">/* <![CDATA[ */
216 function insertCode(code) {
218 if (!code) code = "";
220 if (opener) {
221 // get the textarea of the main document and focus the element
222 var input = opener.document.getElementById("text");
223 input.focus();
224 var txtLen = input.value.length;
225 // for IE
226 if (typeof document.selection != 'undefined')
229 the following code for MSIE is adapted from http://the-stickman.com/web-development/javascript/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/
231 it is licensed unter the terms of the MIT-license
232 see: http://www.opensource.org/licenses/mit-license.php
233 Copyright (c) <year> <copyright holders>
235 Permission is hereby granted, free of charge, to any person obtaining
236 a copy of this software and associated documentation files (the "Software"),
237 to deal in the Software without restriction, including without limitation
238 the rights to use, copy, modify, merge, publish, distribute, sublicense,
239 and/or sell copies of the Software, and to permit persons to whom
240 the Software is furnished to do so, subject to the following conditions:
242 The above copyright notice and this permission notice shall be included
243 in all copies or substantial portions of the Software.
245 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
246 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
247 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
248 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
249 OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
250 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
251 OR OTHER DEALINGS IN THE SOFTWARE.
253 // The current selection
254 var range = opener.document.selection.createRange();
255 // We'll use this as a 'dummy'
256 var stored_range = range.duplicate();
257 // Select all text
258 stored_range.moveToElementText(input);
259 // Now move 'dummy' end point to end point of original range
260 stored_range.setEndPoint( 'EndToEnd', range );
261 // Now we can calculate start and end points
262 var position = input.selectionStart = stored_range.text.length - range.text.length;
263 // input.selectionEnd = input.selectionStart + range.text.length;
265 var txtbefore = input.value.substring(0,position);
266 var txtafter = input.value.substring(position, txtLen);
267 input.value = txtbefore + code + txtafter;
269 // for Mozilla
270 else if ((typeof input.selectionStart) != 'undefined')
272 var selEnd = input.selectionEnd;
273 var txtbefore = input.value.substring(0,selEnd);
274 var txtafter = input.value.substring(selEnd, txtLen);
275 var oldScrollTop = input.scrollTop;
276 input.value = txtbefore + code + txtafter;
277 input.selectionStart = txtbefore.length + code.length;
278 input.selectionEnd = txtbefore.length + code.length;
279 input.scrollTop = oldScrollTop;
281 else
283 input.value += code;
285 input.focus();
286 self.close();
290 /* ]]> */
291 </script>
292 </head>
293 <body>
294 <h1><?php $lang['upload_image_title']; ?></h1>
295 <?php
296 switch($action)
298 case 'upload':
299 if(isset($errors))
301 echo errorMessages($errors);
303 echo '<p>'.$lang['upload_exp'].'</p>'."\n";
304 echo '<form action="'.$_SERVER['SCRIPT_NAME'].'" method="post" '."\n";
305 echo 'enctype="multipart/form-data"><input type="file" name="probe" />'."\n";
306 echo '<input type="submit" value="'.outputLangDebugInAttributes($lang['upload_subm_button']).'">'."\n";
307 echo '</form>'."\n";
308 echo '<p>[ <a href="'.$_SERVER['PHP_SELF'].'?action=show_uploaded_images">';
309 echo $lang['available_images'].'</a> ]</p>'."\n";
310 break;
311 case 'uploaded':
312 if (isset($_FILES['probe']))
314 if(isset($image_manipulated))
316 echo '<p>'.$image_manipulated.'</p>'."\n";
318 else
320 echo '<p>'.$lang['upload_successful'].'</p>'."\n";
323 echo '<img src="img/uploaded/'.$filename.'" alt="" height="100" border="1">'."\n";
324 echo '<p>'.$lang['paste_image'].'</p>'."\n";
325 echo '<p><button style="width:25px; height:25px;" title="'.outputLangDebugInAttributes($lang['insert_image_normal']);
326 echo '" onclick="insertCode(\'[img]'.$uploaded_images_path.$filename.'[/img]\');';
327 echo '"><img src="img/img_normal.png" alt="'.outputLangDebugInAttributes($lang['insert_image_normal']);
328 echo '" width="11" height="11" /></button>&nbsp;<button style="width:25px; height:25px;"';
329 echo ' title="'.outputLangDebugInAttributes($lang['insert_image_left']).'" onclick="insertCode(\'[img|left]';
330 echo $uploaded_images_path.$filename.'[/img]\');"><img';
331 echo ' src="img/img_left.png" alt="'.outputLangDebugInAttributes($lang['insert_image_left']).'" width="11" height="11"';
332 echo ' /></button>&nbsp;<button style="width:25px; height:25px;" title="';
333 echo outputLangDebugInAttributes($lang['insert_image_right']).'" onclick="insertCode(\'[img|right]';
334 echo $uploaded_images_path.$filename.'[/img]\');"><img';
335 echo ' src="img/img_right.png" alt="'.outputLangDebugInAttributes($lang['insert_image_right']).'" width="11"';
336 echo ' height="11" /></button></p>'."\n";
337 break;
338 case 'show_uploaded_images':
339 $p = isset($_GET['p']) ? intval($_GET['p']) : 1;
340 $c=0;
341 $handle = opendir($uploaded_images_path);
342 while ($file = readdir($handle))
344 if (preg_match('/\.jpg$/i', $file)
345 || preg_match('/\.png$/i', $file)
346 || preg_match('/\.gif$/i', $file))
348 $images[] = $file;
351 closedir($handle);
352 if (isset($images))
354 $images_count = count($images);
355 $show_images_from = $p * $images_per_page - $images_per_page;
356 $show_images_to = $p * $images_per_page;
357 if ($show_images_to>$images_count) $show_images_to = $images_count;
359 else
361 $images_count = 0;
363 echo '<p>';
364 if ($p>1)
366 $pageDown = $p - 1;
367 echo '[ <a href="'.$_SERVER['SCRIPT_NAME'].'?action=show_uploaded_images';
368 echo '&amp;p='.$pageDown.'">&laquo;</a> ] ';
370 if ($p*$images_per_page < $images_count)
372 $pageUp = $p + 1;
373 echo '[ <a href="'.$_SERVER['SCRIPT_NAME'].'?action=show_uploaded_images';
374 echo '&amp;p='.$pageUp.'">&raquo;</a> ] ';
376 echo '<hr /><p>';
377 if ($images_count > 0)
379 for ($i=$show_images_from; $i<$show_images_to; $i++)
381 echo '<a href="'.$_SERVER['SCRIPT_NAME'].'?uploaded_image_selected=';
382 echo $images[$i].'"><img style="margin: 0px 15px 15px 0px;" src="';
383 echo $uploaded_images_path.$images[$i].'" alt="'.$images[$i];
384 echo '" height="100" border="0"></a>'."\n";
387 else
389 echo '<i>'.$lang['no_images'].'</i>';
391 echo '</p>'."\n";
392 break;
394 ?></body>
395 </html>