<?php
require_once("../../include/label.php");
if(isset($_COOKIE["s_id"])){
	$S_UID=$_COOKIE["s_id"];
}
else{
	$S_UID="";
}
if(isset($_COOKIE["s_name"])){
	$S_Name=$_COOKIE["s_name"];
}
else{
	$S_Name="";
}
$sql= "select s_id from s_users where S_Name='".$S_Name."' and S_id=$S_UID";
$s_id= $db->Getone($sql);
if(!$s_id){
exit('非法操作！');
}
session_start();
if($_GET['action']=='upload'){
	$fileType=array('jpg','gif','bmp','png',".jpeg");//允许上传的文件类型	
	$maxSize=2048; //单位：KB
	if(!in_array(substr($_FILES['file1']['name'],-3,3),$fileType))
		die("<script>alert('不允许上传该类型的文件！-808');window.parent.\$('divProcessing').style.display='none';history.back();</script>");
	if(strpos($_FILES['file1']['type'],'image')===false)
		die("<script>alert('不允许上传该类型的文件！');window.parent.\$('divProcessing').style.display='none';history.back();</script>");
	if($_FILES['file1']['size']> $maxSize*1024)
		die( "<script>alert('文件过大！');window.parent.\$('divProcessing').style.display='none';history.back();</script>");
	if($_FILES['file1']['error'] !=0)
		die("<script>alert('未知错误，文件上传失败！');window.parent.$('divProcessing').style.display='none';history.back();</script>");
        if($_GET['tid']=='bg'){
	          $targetDir='../../Upload/bg/';
        }elseif($_GET['tid']=='top'){
	          $targetDir='../../Upload/top/';
        }
	$targetFile=date('Ymd').time().substr($_FILES['file1']['name'],-4,4);
	$realFile=$targetDir.$targetFile;
	if(function_exists('move_uploaded_file')){
		move_uploaded_file($_FILES['file1']['tmp_name'],$realFile);
		if(isset($_GET['types'])=="single"){
		echo "<font color=red>上传成功,请刷新该页面即可看到!</font>";
                             if($_GET['tid']=='bg'){
                                      //删除原来的图片
                                      $pic=$db->getrow("select s_bgpic from S_users where S_name='".$S_Name."'");
                                      if(IsNul($pic['s_bgpic'])){
                                      $fileurl="../../upload/bg/".$pic['s_bgpic'];
                                      $fileurl="../".substr($fileurl,strlen(S_SitePath));
                                        if(file_exists($fileurl)){
                                          unlink($fileurl);
                                        }
                                      }
                                      $db->query("update s_users set s_bgpic='".$targetFile."' where s_name='".$S_Name."'");
	                     }elseif($_GET['tid']=='top'){
                                      //删除原来的图片
                                      $pic=$db->getrow("select s_toppic from S_users where S_name='".$S_Name."'");
                                      if(IsNul($pic['s_toppic'])){
                                      $fileurl="../../upload/top/".$pic['s_toppic'];
                                      $fileurl="../".substr($fileurl,strlen(S_SitePath));
                                        if(file_exists($fileurl)){
                                          unlink($fileurl);
                                        }
                                      }
                                   $db->query("update s_users set s_toppic='".$targetFile."' where s_name='".$S_Name."'");
	                     }
			}
		else{			
			die("<script>window.parent.LoadIMG('".S_SitePath."images/{$upfileDir}/{$targetFile}');</script>");
		}
	}
	else{
		@copy($_FILES['file1']['tmp_name'],$realFile);
		if(isset($_GET['types'])=="single"){
			echo "<font color=red>上传成功,请刷新该页面即可看到!</font>";
                             if($_GET['tid']=='bg'){
                                      //删除原来的图片
                                      $pic=$db->getrow("select s_bgpic from S_users where S_name='".$S_Name."'");
                                      if(IsNul($pic['s_bgpic'])){
                                      $fileurl="../../upload/bg/".$pic['s_bgpic'];
                                      $fileurl="../".substr($fileurl,strlen(S_SitePath));
                                        if(file_exists($fileurl)){
                                          unlink($fileurl);
                                        }
                                      }
                                      $db->query("update s_users set s_bgpic='".$targetFile."' where s_name='".$S_Name."'");
	                     }elseif($_GET['tid']=='top'){
                                      //删除原来的图片
                                      $pic=$db->getrow("select s_toppic from S_users where S_name='".$S_Name."'");
                                      if(IsNul($pic['s_toppic'])){
                                      $fileurl="../../upload/top/".$pic['s_toppic'];
                                      $fileurl="../".substr($fileurl,strlen(S_SitePath));
                                        if(file_exists($fileurl)){
                                          unlink($fileurl);
                                        }
                                      }
                                   $db->query("update s_users set s_toppic='".$targetFile."' where s_name='".$S_Name."'");
	                     }
			}
		else{
			die("<script>window.parent.LoadIMG('".S_SitePath."images/{$upfileDir}/{$targetFile}');</script>");
			}	
	}
}
?>
<script type="text/javascript"> 
document.oncontextmenu=new Function("event.returnValue=false;"); 
document.onselectstart=new Function("event.returnValue=false;"); 
</script>