<?php
include "../../include/conn.php";
include "../../include/function.php";
include "../../include/userset.php";
if(isset($_COOKIE["s_name"])){
	$S_Name=$_COOKIE["s_name"];
}
else{
	$S_Name="";
}
if(isset($_COOKIE["s_password"])){
	$S_PWD=$_COOKIE["s_password"];
}
else{
	$S_PWD="";
}
// 注意：使用组件上传，不可以使用 $_FILES["Filedata"]["type"] 来判断文件类型
mb_http_input("gbk");
mb_http_output("gbk");

//---------------------------------------------------------------------------------------------
//组件设置a.MD5File为2，3时 的实例代码

if(getGet('access2008_cmd')=='2'){ // 提交MD5验证后的文件信息进行验证
	//getGet("access2008_File_name") 	'文件名
	//getGet("access2008_File_size")	'文件大小，单位字节
	//getGet("access2008_File_type")	'文件类型 例如.gif .png
	//getGet("access2008_File_md5")		'文件的MD5签名
	
	die('2'); //返回命令  0 = 开始上传文件， 2 = 不上传文件，前台直接显示上传完成
}
if(getGet('access2008_cmd')=='3'){ //提交文件信息进行验证
	//getGet("access2008_File_name") 	'文件名
	//getGet("access2008_File_size")	'文件大小，单位字节
	//getGet("access2008_File_type")	'文件类型 例如.gif .png
	
	die('2'); //返回命令 0 = 开始上传文件,1 = 提交MD5验证后的文件信息进行验证, 2 = 不上传文件，前台直接显示上传完成
}
//---------------------------------------------------------------------------------------------

$type=filekzm($_FILES["Filedata"]["name"]);
if ((($type == ".gif")
|| ($type == ".png")
|| ($type == ".jpeg")
|| ($type == ".jpg")
|| ($type == ".bmp"))
&& ($_FILES["Filedata"]["size"] < 20000000))
  {
  if ($_FILES["Filedata"]["error"] > 0)
    {
    echo "返回错误: " . $_FILES["Filedata"]["error"] . "<br />";
    }
  else
    {
    echo "图片: " . $_FILES["Filedata"]["name"] . "上传成功！<br />";
if(S_PicFun==1){
    echo "系统自动为您增加: " . S_PicPoints . "点<br />";
}
    if (file_exists( $_FILES["Filedata"]["name"]))
      {
      echo $_FILES["Filedata"]["name"] . " already exists. ";
      }
    else
      {
$FileID=date("Ymd-His") . '-' . rand(100,999);
$tpname=$FileID.$type;

      move_uploaded_file($_FILES["Filedata"]["tmp_name"],'../../upload/pic/'.$tpname);
$targetDir="".dirname(__FILE__)."/../../upload/pic/".$tpname."";
if(S_WaterMark==1){
imageWaterMark($targetDir,S_WaterLocation,S_WaterFont,S_WaterFontSize,S_WaterFontColor);
}
$filename = "../../upload/pic/".$tpname."";
$tpyz = file_type($filename);
if($tpyz!="gif" and $tpyz!="png" and $tpyz!="bmp" and $tpyz!="jpg") {
$fileurl=$filename;
$fileurl="../".substr($fileurl,strlen(S_SitePath));
if(file_exists($fileurl)){
unlink($fileurl);
}
}else {
//增加相应点数
if(S_PicFun==1){
global $db;
$sql= "select s_id from s_users where S_Name='".$S_Name."'";
$s_id= $db->Getone($sql);
if($s_id){
$db->query("update s_users set S_Coin=S_Coin+".S_PicPoints." where s_id='".$s_id."'");
}
}
		$filename1 = ''.S_SiteDomain.'/upload/pic/'.$tpname.'' ;
				global $db;
				$S_User=$S_Name;
				$S_Content=$S_Name;
				$S_Url=$filename1;
				$S_Addtime=date('Y-m-d H:i:s');
				$sqlsc="insert into s_pic (S_User,S_Content,S_Url,S_Addtime) values ('".$S_User."','".$S_Content."','".$S_Url."','".$S_Addtime."')";
				if($db->query($sqlsc)){
				//产生动态
				$S_Djid=0;
				$S_Xid=5;
				$S_Name=$filename1;
				$S_Classid=0;
				$S_User=$S_User;
				$S_Addtime=date('Y-m-d H:i:s');
				$sqldt="insert into s_dt (S_Name,S_Djid,S_Classid,S_User,S_Xid,S_Addtime) values ('".$S_Name."',".$S_Djid.",".$S_Classid.",'".$S_User."',".$S_Xid.",'".$S_Addtime."')";
				if($db->query($sqldt)){}
				}
}
      }
    }
  }
else
  {
  echo "上传失败，请检查文件类型和文件大小是否符合标准<br />文件类型：".$type.'<br />文件大小:'.($_FILES["Filedata"]["size"] / 1024) . " Kb";
  }
  
function filekzm($a)
{
	$c=strrchr($a,'.');
	if($c)
	{
		return $c;
	}else{
		return '';
	}
}

function getGet($v)// 获取GET
{
  if(isset($_GET[$v]))
  {
	 return $_GET[$v];
  }else{
	 return '';
  }
}

function getPost($v)// 获取POST
{
  if(isset($_POST[$v]))
  {
	  return $_POST[$v];
  }else{
	  return '';
  }
}
//判断图片类型
function file_type($filename)   
{   
    $file = fopen($filename, "rb");   
    $bin = fread($file, 2); //只读2字节   
    fclose($file);   
    $strInfo = @unpack("C2chars", $bin);   
    $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);   
    $fileType = '';   
    switch ($typeCode)   
    {   
        case 7790:   
            $fileType = 'exe';   
            break;   
       case 7784:   
            $fileType = 'midi';   
            break;   
        case 8297:   
            $fileType = 'rar';   
            break;           
        case 8075:   
            $fileType = 'zip';   
           break;   
        case 255216:   
            $fileType = 'jpg';   
            break;   
        case 7173:   
            $fileType = 'gif';   
            break;   
        case 6677:   
            $fileType = 'bmp';   
            break;   
        case 13780:   
            $fileType = 'png';   
            break;   
        default:   
            $fileType = 'unknown: '.$typeCode;   
    }   
  
    //Fix   
    if ($strInfo['chars1']=='-1' AND $strInfo['chars2']=='-40' ) return 'jpg';   
    if ($strInfo['chars1']=='-119' AND $strInfo['chars2']=='80' ) return 'png';   
  
    return $fileType;   
} 
//图片加水印
function imageWaterMark($groundImage,$waterPos=0,$waterText="", $textFont=5,$textColor="#FF0000") 
{ 
//参数分别为：图片文件名,水印位置,水印文字,文字大小,文字颜色
      $isWaterImage = FALSE; 
      $formatMsg = "暂不支持该文件格式，请用图片处理软件将图片转换为GIF、JPG、PNG格式。"; 
    //  读取图片 
      if(!empty($groundImage) && file_exists($groundImage)) { //背景图片存在
          $ground_info = getimagesize($groundImage); //背景图片信息
          $ground_w      = $ground_info[0];//取得背景图片的宽 
          $ground_h      = $ground_info[1];//取得背景图片的高 

          switch($ground_info[2]) {     //取得背景图片的格式  
              case 1:$ground_im = imagecreatefromgif($groundImage);break; 
              case 2:$ground_im = imagecreatefromjpeg($groundImage);break; 
              case 3:$ground_im = imagecreatefrompng($groundImage);break; 
              default:die($formatMsg); 
          } 
      } else { 
          die("需要加水印的图片不存在！"); 
      } 
      //水印位置 
	$temp = imagettfbbox(ceil($textFont*2.5),0,"arial.ttf",$waterText);//取得使用 TrueType 字体的文本的范围 
          $w = $temp[2] - $temp[6]; 
          $h = $temp[3] - $temp[7];
          unset($temp); 
          //销毁$temp变量
          $label = "文字区域"; 
      //} 
      if( ($ground_w<$w) || ($ground_h<$h) ) { //背景宽度/高度和水印的宽度/高度比较
          	echo "需要加水印的图片的长度或宽度比水印".$label."还小，无法生成水印！"; 
          return; 
      } 
      switch($waterPos) { //水印的显示位置
          case 0://随机 
              $posX = rand(0,($ground_w - $w)); 
              $posY = rand(0,($ground_h - $h)); 
              break; 
          case 1://1为顶端居左 
              $posX = 0; 
              $posY = 0; 
              break; 
          case 2://2为顶端居中 
              $posX = ($ground_w - $w) / 2; 
              $posY = 0; 
              break; 
          case 3://3为顶端居右 
              $posX = $ground_w - $w; 
              $posY = 0; 
              break; 
          case 4://4为中部居左 
              $posX = 0; 
              $posY = ($ground_h - $h) / 2; 
              break; 
          case 5://5为中部居中 
              $posX = ($ground_w - $w) / 2; 
              $posY = ($ground_h - $h) / 2; 
              break; 
          case 6://6为中部居右 
              $posX = $ground_w - $w; 
              $posY = ($ground_h - $h) / 2; 
              break; 
          case 7://7为底端居左 
              $posX = 0; 
              $posY = $ground_h - $h; 
              break; 
          case 8://8为底端居中 
              $posX = ($ground_w - $w) / 2; 
              $posY = $ground_h - $h; 
              break; 
          case 9://9为底端居右 
              $posX = $ground_w - $w; 
              $posY = $ground_h - $h; 
              break; 
          default://随机 
              $posX = rand(0,($ground_w - $w)); 
              $posY = rand(0,($ground_h - $h)); 
              break;     
      } 

      //设定图像的混色模式 
      imagealphablending($ground_im, true); 
          if( !empty($textColor) && (strlen($textColor)==7) ) { 
              $R = hexdec(substr($textColor,1,2)); 
              $G = hexdec(substr($textColor,3,2)); 
              $B = hexdec(substr($textColor,5)); 
          } else { 
              die("水印文字颜色格式不正确！"); 
          } 
          imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));         

      //生成水印后的图片 
      @unlink($groundImage); 
      switch($ground_info[2]) {//取得背景图片的格式 
          case 1:imagegif($ground_im,$groundImage);break; 
          case 2:imagejpeg($ground_im,$groundImage);break; 
          case 3:imagepng($ground_im,$groundImage);break; 
          default:die($errorMsg); 
      } 

      //释放内存 
      if(isset($water_info)) unset($water_info); 
      if(isset($water_im)) imagedestroy($water_im); 
      unset($ground_info); 
      imagedestroy($ground_im); 
} 
?>