<?php
@set_time_limit(0);
!defined("MONKEYMAO") && die("Accessed Defined!");

if(cpchecksubmit('upload')){
		$attach_saved = false;
		$upfiles = $attacharray = array();

		if(isset($_FILES['upfiles']) && is_array($_FILES['upfiles'])) {
			foreach($_FILES['upfiles'] as $key => $var) {
				foreach($var as $id => $val) {
					$upfiles[$id][$key] = $val;
				}
			}
		}
	
		foreach($upfiles as $upfile){
			if(!disuploadedfile($upfile['tmp_name']) || !($upfile['tmp_name'] != 'none' && $upfile['tmp_name'] && $upfile['name'])) {
				continue;
			}
			$filename = filter($upfile['name']);
			$upfile['ext'] = $extension = strtolower(fileext($upfile['name']));
			if(strlen($upfile['name']) > 60) {
				$upfile['name'] = 'abbr_'.substr($upfile['name'], -60);
			}
			if(empty($upfile['size'])) {
				cpmsg('文件尺寸不允许操作');
			}
			if($attachsave) {
				switch($attachsave) {			
					case 1: $attach_subdir = 'ext_'.$extension; break;
					case 2: $attach_subdir = 'month_'.date('ym'); break;
					case 3: $attach_subdir = 'day_'.date('ymd'); break;
				}
				$attach_dir = MODEPATH.$attachdir.'/'.$attach_subdir;
				if(!is_dir($attach_dir)) {
					mkdir($attach_dir, 0777);
					fclose(fopen($attach_dir.'/index.htm', 'w'));
				}
				$upfile['attachment'] = $attach_subdir.'/';
			} else {
				$upfile['attachment']= '';
			}

			$filename = substr($filename, 0, strlen($filename) - strlen($extension) - 1);
			if(preg_match("/[\x7f-\xff\%\'\"\(\)\#]+/", $filename) || strlen($filename) > 60) {
				$filename = str_replace('/', '', base64_encode(substr($filename, 0, 10)));
			}

			$upfile['attachment'] .= preg_replace("/(php|phtml|php3|jsp|exe|dll|asp|aspx|cgi|fcgi|pl)(\.|$)/i", "_\\1\\2",
				substr($filename, 0, 64).'_'.random(6).'.'.$extension);

			$target = MODEPATH.$attachdir.'/'.stripslashes($upfile['attachment']);

			if(@copy($upfile['tmp_name'], $target) || (function_exists('move_uploaded_file') && @move_uploaded_file($upfile['tmp_name'], $target))) {
				@unlink($upfile['tmp_name']);
				$attach_saved = true;
			}

			if(!$attach_saved && @is_readable($upfile['tmp_name'])) {
				@$fp = fopen($upfile, 'rb');
				@flock($fp, 2);
				@$attachedfile = fread($fp, $upfile['size']);
				@fclose($fp);

				@$fp = fopen($target, 'wb');
				@flock($fp, 2);
				if(@fwrite($fp, $attachedfile)) {
					@unlink($upfile['tmp_name']);
					$attach_saved = true;
				}
				@fclose($fp);
			}

			if($upfile) {
				if(in_array($upfile['ext'], array('jpg', 'jpeg', 'gif', 'png', 'swf', 'bmp')) && function_exists('getimagesize') && !@getimagesize($target)) {
					@unlink($target);
					cpmsg('文件类型不允许上传');
				} else {
					if($watermarkstatus && in_array($upfile['ext'], array('jpg', 'jpeg', 'gif', 'png')) && function_exists('getimagesize') && function_exists('imageCreateFromJPEG') && function_exists('imageCreateFromPNG') && function_exists('imageCopyMerge')) {
						$attachinfo	= getimagesize($target);
						$watermark_logo = imageCreateFromGIF($watermark_file);
						$logo_w		= imageSX($watermark_logo);
						$logo_h		= imageSY($watermark_logo);
						$img_w		= $attachinfo[0];
						$img_h		= $attachinfo[1];
						$wmwidth	= $img_w - $logo_w;
						$wmheight	= $img_h - $logo_h;

						$animatedgif = 0;
						if($attachinfo['mime'] == 'image/gif') {
							if(empty($attachedfile)) {
								$fp = fopen($target, 'rb');
								$attachedfile = fread($fp, $upfile['size']);
								fclose($fp);
							}
							$animatedgif = strpos($attachedfile, 'NETSCAPE2.0') === FALSE ? 0 : 1;
						}
							
						if(is_readable($watermark_file) && $wmwidth > 10 && $wmheight > 10 && !$animatedgif) {
							switch ($attachinfo['mime']) {
								case 'image/jpeg':
									$dst_photo = imageCreateFromJPEG($target);
									break;
								case 'image/gif':
									$dst_photo = imageCreateFromGIF($target);
									break;
								case 'image/png':
									$dst_photo = imageCreateFromPNG($target);
									break;
							}

							switch($watermarkstatus) {
								case 1:
									$x = +5;
									$y = +5;
									break;
								case 2:
									$x = ($logo_w +	$img_w)	/ 2;
									$y = +5;
									break;
								case 3:
									$x = $img_w - $logo_w-5;
									$y = +5;
									break;
								case 4:
									$x = +5;
									$y = ($logo_h +	$img_h)	/ 2;
									break;
								case 5:
									$x = ($logo_w +	$img_w)	/ 2;
									$y = ($logo_h +	$img_h)	/ 2;
									break;
								case 6:
									$x = $img_w - $logo_w;
									$y = ($logo_h +	$img_h)	/ 2;
									break;
								case 7:
									$x = +5;
									$y = $img_h - $logo_h-5;
									break;
								case 8:
									$x = ($logo_w +	$img_w)	/ 2;
									$y = $img_h - $logo_h;
									break;
								case 9:
									$x = $img_w - $logo_w-5;
									$y = $img_h - $logo_h-5;
									break;
							}

							imageAlphaBlending($watermark_logo, true);
							imageCopyMerge($dst_photo, $watermark_logo, $x,	$y, 0, 0, $logo_w, $logo_h, $watermarktrans);

							switch($attachinfo['mime']) {
								case 'image/jpeg':
									imageJPEG($dst_photo, $target, $watermarkquality);
									break;
								case 'image/gif':
									imageGIF($dst_photo, $target);
									break;
								case 'image/png':
									imagePNG($dst_photo, $target);
									break;	      
							}

						}

						$upfile['size'] = filesize($target);
					}

					$upfile['description'] = textcut(htmlconvert($attachdesc[$key]), 100);
					$db -> query("INSERT INTO {$tablepre}uploads  (mid, type, filename, filesize, filetype, refile, attachment, description, uptime) VALUES ( '$mm_mid', '$upfile[ext]', '$upfile[name]', '$upfile[size]', '$upfile[type]', '$refile', '$upfile[attachment]', '$upfile[description]', '$time') ");
				}
			} else {
				cpmsg('上传错误');
			}
		}
		cpmsg('上传成功', "index.php?action=uploads");
	
}else{
	
		$sql = $adminid!=1 ?  " WHERE f.mid='$mm_mid' " : '';
		$query = $db -> query("SELECT COUNT(*) FROM {$tablepre}uploads f  $sql");
		$filenum = $db -> result($query,0);
		$query = $db -> query("SELECT f.*, u.username AS upuser FROM {$tablepre}uploads f LEFT JOIN {$tablepre}members u USING(mid) $sql ORDER BY uptime DESC");
		$files = array();
		while($file = $db -> fetch_array($query)){
			$file['uptime'] = gmdate("$dateformat $timeformat", $file['uptime'] + $timeoffset*3600);
			$file['filesize'] = sizecount($file['filesize'] );
			$file[type] = file_exists('images/icon/'.$file['type'].'.gif') ? 'images/icon/'.$file['type'].'.gif' : 'images/icon/none.gif';
			$files[$file['uid']] = $file;
		}
		
		include theme('uploads', 'themes/admin', 'admin');
}
?>