<?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");
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
        $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>");

	$folder="../annex/logo/big"; // 保存大照片的文件夹
	$folderx="../annex/logo/small"; // 保存小照片的文件夹
	if(!file_exists($folder)){
		mkdir($folder,0777);
	}else{
		if(!is_writable($folder)){
			@chmod($folder,0777);
		}
	}
	$jpg = $upload_file;
            $pic_id=date('YmdHis').rand(1000,9999).".jpg";
	    $name = $folder."/".$pic_id;
	    $namex = $folderx."/".$pic_id;
	    @move_uploaded_file($upload_file['tmp_name'],$name);
	    if(file_exists($name)){
                //写入数据库
                global $db;
                $row=$db->getrow("Select * from ".Getdbname('user')." where cs_name='".$cscms_name."'");
                $sql="update ".Getdbname('user')." set cs_logo='".$pic_id."' where cs_name='".$cscms_name."'";
                if($db->query($sql)){
                     //生成萎缩图
	    	     if(run_img_resize($name,$namex,0,0,48,48,false,false,100) && run_img_resize($name,$name,0,0,150,150,false,false,100)){
                       	    if(file_exists('../annex/logo/big/'.$row['CS_Logo'])){
                                @unlink('../annex/logo/big/'.$row['CS_Logo']);
                            }
                       	    if(file_exists('../annex/logo/small/'.$row['CS_Logo'])){
                                @unlink('../annex/logo/small/'.$row['CS_Logo']);
                            }
			    exit("<script type=\"text/javascript\">
                                alert('上传修改成功！');
                                window.parent.location='space.php?ac=edit&op=logo';
		                this.parent.$('.webox').css({display:'none'});
		                this.parent.$('.background').css({display:'none'});</script>");
                     }
                }else{
                     @unlink($name);
                     alert('上传失败！');
                }
	    }else{
                alert('上传失败！');
	    }
exit();
}

//处理缩略图
function run_img_resize($img,$resize_img_name,$dx,$dy,$resize_width,$resize_height,$w,$h,$quality){
  $img_info=@getimagesize($img);
  $width=$img_info[0];
  $height=$img_info[1];
  $w=$w==false?$width:$w;
  $h=$h==false?$height:$h;
  switch($img_info[2]){
    case 1:
    $img=@imagecreatefromgif($img);
    break;
    case 2:
    $img=@imagecreatefromjpeg($img);
    break;
    case 3:
    $img=@imagecreatefrompng($img);
    break;
  }
  if(!$img) return false;
  if(function_exists("imagecopyresampled")){
    $resize_img=@imagecreatetruecolor($resize_width,$resize_height);
    $white=@imagecolorallocate($resize_img,255,255,255);
    @imagefilledrectangle($resize_img,0,0,$resize_width,$resize_height,$white);// 填充背景色
    @imagecopyresampled($resize_img,$img,0,0,$dx,$dy,$resize_width,$resize_height,$w,$h);
  }else{
    $resize_img=@imagecreate($resize_width,$resize_height);
    $white=@imagecolorallocate($resize_img,255,255,255);
    @imagefilledrectangle($resize_img,0,0,$resize_width,$resize_height,$white);// 填充背景色
    @imagecopyresized($resize_img,$img,0,0,$dx,$dy,$resize_width,$resize_height,$w,$h);
  }
  switch($img_info[2]){
    case 1:
    @imagegif($resize_img,$resize_img_name);
    break;
    case 2:
    @imagejpeg($resize_img,$resize_img_name,$quality); //100质量最好，默认75
    break;
    case 3:
    @imagepng($resize_img,$resize_img_name);
    break;
  }
  @imagedestroy($resize_img);
  return 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=utf-8" />
<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" 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>
