<?php
# Name: PHP版程氏音乐CMS管理系统 v3.0
# Author: 程氏<web@chshcms.com> [QQ:848769359]
# Homepage:http://www.chshcms.cn/
include_once("../lib/cs_conn.php");
include_once("../lib/cs_common.php");
$action=CS_Request("ac");
$op=CS_Request("op");
$cid=CS_Request("cid");

if(isset($_COOKIE["cs_name"])){
	$cscms_name=$_COOKIE["cs_name"];
}else{
	exit(Msg_Error('你还没有登入或者登入已经超时!','login.php'));
}


if($action=='upload'){
	$fileType=array('jpg','gif','bmp','png',".jpeg");//允许上传的文件类型	
	$maxSize=2048; //单位：KB

        if($op=='pic'){
             $upload_file=$_FILES["Filedata"];
        }else{
             $upload_file=$_FILES["file1"];
        }

	if(!in_array(substr($upload_file['name'],-3,3),$fileType))
		die("<script>alert('不允许上传该类型的文件！-808');</script>");
	if(strpos($upload_file['type'],'image')===false && $op!='pic')
		die("<script>alert('不允许上传该类型的文件！');</script>");
	if($upload_file['size']> $maxSize*1024)
		die( "<script>alert('文件过大！');</script>");
	if($upload_file['error'] !=0)
		die("<script>alert('未知错误，文件上传失败！');</script>");
        if($op=='bg'){
	          $targetDir='../annex/bg/';
        }elseif($op=='top'){
	          $targetDir='../annex/top/';
        }elseif($op=='topic'){
	          $targetDir='../annex/topic/';
        }elseif($op=='pic' || $op=='xc'){
	          $targetDir='../annex/pic/';
        }
        if($op=='pic' || $op=='xc'){
	    $targetFile=date('Ymd').time().substr($upload_file['name'],-4,4);
        }else{
	    $targetFile="xc_".date('Ymd').time().substr($upload_file['name'],-4,4);
        }
	$realFile=$targetDir.$targetFile;
	if(function_exists('move_uploaded_file')){
		@move_uploaded_file($upload_file['tmp_name'],$realFile);
		if(CS_WaterMark==1){
			imageWaterMark($targetDir.$targetFile,CS_WaterLocation,CS_WaterFont,CS_WaterFontSize,CS_WaterFontColor);
		}
                if($op=='pic'){	
                        $sql="insert into ".Getdbname('photo')." (CS_CID,CS_Name,CS_Url,CS_User,cs_addtime,CS_YID) values (".$cid.",'".$cscms_name."','".$targetFile."','".$cscms_name."','".date('Y-m-d H:i:s')."',".CS_PicFun.")";
		        if($db->query($sql)){
                             if(CS_PicFun==0 && CS_PicaddFun==1){
                                $db->query("update ".Getdbname('user')." set CS_Cion=CS_Cion+CS_PicDs where CS_Name='".$cscms_name."'");
                             }
                             exit('ok');
                        }else{
                             exit('no');
                        }
                }elseif($op=='bg'){	
                        if($db->query("update ".Getdbname('user')." set CS_Bgpic='".$targetFile."' where CS_Name='".$cscms_name."'")){
			    exit("<script type=\"text/javascript\">
                                alert('上传修改成功！');
                                window.parent.location='space.php?ac=edit&op=pic';
		                this.parent.$('.webox').css({display:'none'});
		                this.parent.$('.background').css({display:'none'});</script>");
                        }else{
                                alert('上传失败！');
                        }
                }elseif($op=='top'){
                        if($db->query("update ".Getdbname('user')." set CS_Toppic='".$targetFile."' where CS_Name='".$cscms_name."'")){
			    exit("<script type=\"text/javascript\">
                                alert('上传修改成功！');
                                window.parent.location='space.php?ac=edit&op=pic';
		                this.parent.$('.webox').css({display:'none'});
		                this.parent.$('.background').css({display:'none'});</script>");
                        }else{
                                alert('上传失败！');
                        }
                }else{
			exit("<script type=\"text/javascript\">
                           alert('上传成功,请完成下面信息在发布！');
		           window.parent.document.form.CS_Pic.value='{$targetFile}';
		           this.parent.$('.webox').css({display:'none'});
		           this.parent.$('.background').css({display:'none'});</script>");
                }
	}else{
		@copy($upload_file['tmp_name'],$realFile);
		if(CS_WaterMark==1){
			imageWaterMark($targetDir.$targetFile,CS_WaterLocation,CS_WaterFont,CS_WaterFontSize,CS_WaterFontColor);
		}
                if($op=='pic'){	
                        $sql="insert into ".Getdbname('photo')." (CS_CID,CS_Name,CS_Url,CS_User,cs_addtime,CS_YID) values (".$cid.",'".$cscms_name."','".$targetFile."','".$cscms_name."','".date('Y-m-d H:i:s')."',".CS_PicFun.")";
		        if($db->query($sql)){
                             if(CS_PicFun==0 && CS_PicaddFun==1){
                                $db->query("update ".Getdbname('user')." set CS_Cion=CS_Cion+CS_PicDs where CS_Name='".$cscms_name."'");
                             }
                             exit('ok');
                        }else{
                             exit('no');
                        }
                }elseif($op=='bg'){	
                        if($db->query("update ".Getdbname('user')." set CS_Bgpic='".$targetFile."' where CS_Name='".$cscms_name."'")){
			    exit("<script type=\"text/javascript\">
                                alert('上传修改成功！');
                                window.parent.location='space.php?ac=edit&op=pic';
		                this.parent.$('.webox').css({display:'none'});
		                this.parent.$('.background').css({display:'none'});</script>");
                        }else{
                                alert('上传失败！');
                        }
                }elseif($op=='top'){
                        if($db->query("update ".Getdbname('user')." set CS_Toppic='".$targetFile."' where CS_Name='".$cscms_name."'")){
			    exit("<script type=\"text/javascript\">
                                alert('上传修改成功！');
                                window.parent.location='space.php?ac=edit&op=pic';
		                this.parent.$('.webox').css({display:'none'});
		                this.parent.$('.background').css({display:'none'});</script>");
                        }else{
                                alert('上传失败！');
                        }
                }else{	
			exit("<script type=\"text/javascript\">
                           alert('上传成功,请完成下面信息在发布！');
		           window.parent.document.form.CS_Pic.value='{$targetFile}';
		           this.parent.$('.webox').css({display:'none'});
		           this.parent.$('.background').css({display:'none'});</script>");
                }	
	}
}
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 = "文字区域"; 

      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); 
} 
?>
<!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=gb2312" />
<title>图片上传</title>
<script type="text/javascript" src="../public/js/jquery.js"></script>
<style type="text/css" media="all">
<!--
input{ background:#F9F9F9;color:#555;font:12px, Verdana, Lucida, Helvetica, Arial, "宋体", sans-serif}
-->
</style>
</HEAD>
<BODY>
<script type="text/javascript"> 
document.oncontextmenu=new Function("event.returnValue=false;"); 
document.onselectstart=new Function("event.returnValue=false;"); 
function Preview(imgFile)
{ 
//document.getElementById("pic").filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgFile.value;
}
var right_type=new Array(".jpg",".gif",".png",".jpeg")
function checkImgType(fileURL)
{
var right_typeLen=right_type.length;
var imgUrl=fileURL.toLowerCase();
var postfixLen=imgUrl.length;
var len4=imgUrl.substring(postfixLen-4,postfixLen);
var len5=imgUrl.substring(postfixLen-5,postfixLen);
for (i=0;i<right_typeLen;i++)
{
    if((len4==right_type[i])||(len5==right_type[i]))
    {
    return true;
    }
 }
}

function sub(o){
   if(o.file1.value==""){alert("请选择一个图片文件\n");return false;}

   if(checkImgType(o.file1.value)){
   // perImg(o.file1.value);
    return true;
   }else{
   alert("您选择的文件格式不正确！");o.file1.focus();
   return false;
   }
}
</script> 
<form enctype="multipart/form-data" action="?ac=upload&op=<?php echo $action?>" onSubmit="return sub(this)" name="uploadform" method="post">
&nbsp;<input type="file" size="35" name= "file1" onChange="if(checkImgType(this.value)){Preview(this);}">
<input name="action" type="hidden" value="upload" />
<input type="submit" value="上传图片">
</form>
</BODY>
</HTML>