<?
include("../../common.php");
include("../../include/security.php");
include("../../include/class/Image.php");
include("../../include/class/ImageUpload.php");
include("include/Photo.php");
$field = $_REQUEST["field"];
if(!$field)
	$field = "des";
//=================
$photo_w = $_REQUEST["photo_w"]==""?"600":$_REQUEST["photo_w"];
//$photo_h = 600;
$photo_w1 = $_REQUEST["photo_w1"]==""?"80":$_REQUEST["photo_w1"];
$photo_h1 = $_REQUEST["photo_h1"]==""?"80":$_REQUEST["photo_h1"];
//=================
$memberID = $sys["memberID"];
$Product = new Photo();
$jsStr = "";
$uploaded = false;
if($_POST["st"]=="submit"){
	//upload image -----------
	request(array("photo_num"),"");
	request($Product->fields,array("uDate","pDate","memberID"));	
	$photoRemainArr = array();
	//=====================photo	
	//***************************************** editable start
	$path = path($pDate);//pDate is from hidden field
	$ID = $memberID;
	$photoFolder = "images/$path";
	//***************************************** editable end
	$photoArr = array();
	for($i=0;$i<$photo_num;$i++){
		$photoArr[$i] = "photo".$i;
		$photoRemainArr[$i] = $_REQUEST["photoRemain".$i];
	}
	for($i=0;$i<$photo_num;$i++){
		$picture = $_FILES["picture".$i];
		if($picture["size"]>0){			
			if(!file_exists($photoFolder))
				mkdir($photoFolder,0777);
			$$photoArr[$i] =  $picture["name"];
			$imageUpload = new ImageUpload($picture);
			$imgType = $imageUpload->getImgType($$photoArr[$i]);
			if($imgType){
				$timestamp = time();
				$$photoArr[$i] = $ID."_".$timestamp.".".$imgType;						
				$newName = "$photoFolder/".$$photoArr[$i];				
				if($imageUpload->upload($newName)){
					$msg = $imageUpload->getMsg();
					if($imageUpload->getWidth()>$photo_w){
						resize($newName,$photo_w,"","$photoFolder/","");					
					}
					//auto small photo1_s
					$s = $_FILES["picture".$timestamp."_s"];
					if($imgType!="swf" && $s["size"]==0){
					    if($thum_type==0)				
							resize_fix($newName,$photo_w1,$photo_h1,0,0,"$photoFolder/","_s");
						else
							resize($newName,$photo_w1,$photo_h1,"$photoFolder/","_s");	
					}
					$photoRemainArr[$i] = $imgType;
					$uploaded = true;									
				}	
    		}
			else
				$msg = $imageUpload->getMsg();
		}
	}	
	//============================ photo end

	//=====================thumbnail photo

	$photoArr = array();
	for($i=0;$i<$photo_num;$i++){
		$photoArr[$i] = "photo".$i."_s";
	}
	for($i=0;$i<count($photoArr);$i++){
		$picture = $_FILES["picture".$i."_s"];
		if($picture["size"]>0){
			if(!file_exists($photoFolder))
				mkdir($photoFolder,0777);
			$$photoArr[$i] =  $picture["name"];
			$imageUpload = new ImageUpload($picture);
			$imgType = $imageUpload->getImgType($$photoArr[$i]);
			if($imgType){
				$$photoArr[$i] = $ID."_".$i."_s.".$imgType;						
				$newName = "$photoFolder/".$$photoArr[$i];
				if($imageUpload->upload($newName)){
					$msg = $imageUpload->getMsg();
					if($imageUpload->getWidth()!=$photo_w1 || $imageUpload->getHeight()!=$photo_h1){
						if($thum_type==0)
							resize_fix($newName,$photo_w1,$photo_h1,0,0,"$photoFolder/","");
						else
							resize($newName,$photo_w1,$photo_h1,"$photoFolder/","");					
					}									
				}	
    		}
			else
				$msg = $imageUpload->getMsg();
		}
	}
	//============================ thumbnail photo end	
	if($uploaded){	
		$title = norm_ubb($title);
		$des = norm($des);
		$tag = norm_ubb($tag);
		$uDate = $pTime;
		$pDate = $pTime;
		$active = "checked";	
		$sql = sqlInsert($Product->table,$Product->fields,array(""));
		if(mysql_query($sql)){			
			$msg = "You have uploaded the photo successfully!";
			$maxID = $Product->getMaxID($memberID);
			$url = $Product->getURL();
			$info = "[img]{$url}[/img]";
			$jsStr = "loadmsg(\"{$info}\")";
		}
		else{
			$msg = "Failed to upload the photo.";
			$error = true;
		}
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
<title>upload</title>
<meta name="keywords" content="<?=$_keywords?>" />
<meta name="description" content="<?=$_des?>" />
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript">
var par = parent.document;
function loadmsg(msg){
	var area1 = par.getElementById("<?=$field?>");
	if(area1.value=="")
		area1.value = msg;
	else	
		area1.value = area1.value+"\n\n"+msg;
}
<?=$jsStr?>
</script>
</head>
<body>
	<!-- main start -->
	<div>
<div class="msg"><?=$msg?></div>	
<form method=post name=form1 action="upload" ENCTYPE="multipart/form-data">	
    <table cellpadding=3 cellspacing=0>
<?
$path = path($pDate);
$photo_num = 1;
for($i=0;$i<$photo_num;$i++){
?>
      <tr valign="top"> 
        <td width="75"  height="43" align="right">* Photo 
          <?=$i+1?>
          :</td>
        <td width="155" ><input size=10 type="file" name="picture<?=$i?>"> </td>
      </tr>
      <tr valign="top">
        <td height="43" align="right" >&nbsp;</td>
        <td><input type="hidden" name="photo_num" value="<?=$photo_num?>" />
          <input type="hidden" name="st" value="submit" /> 
		  <input name="field" type="hidden" value="<?=$field?>" /> 
          <input type="Submit" name="Submit" value="Upload a photo" /></td>
      </tr>
<?
}
?>
    </table>
  </form>
  	</div>
 	<!--main end -->
</body>
</html>
<? $db->close()?>