<?php
error_reporting(0);
header("Content-type: text/html; charset=gb2312");
/*
'**************************************************************************************************
' 程序名称: CCTVAI开源音乐管理系统
' 官方网站: http://www.cctvai.com
' 联系 Q Q: 1542367792
' QQ交流群：136687984
' 版本：(终结版)
' 备注：未经书面授权，不得向任何第三方提供本软件系统！
' 功能，模板，插件，扩展，定制请联系QQ：1542367792
'**************************************************************************************************
*/
if(!eregi($_SERVER['SERVER_NAME'], $_SERVER['HTTP_REFERER'])) {exit('');}
include "../../include/cctvai.conn.php";
include "../../include/cctvai.inc.php";
include "source/function_common.php";
ob_start();
$ac=SafeRequest("ac", "get");
$op=SafeRequest("op", "get");
if(!IsNul($ac)) {$ac="home";}
?>
<?php
if($ac=="pic") {//上传照片
if(!$userlogined==true){exit('10000');}
		if(!empty($_FILES)) {
			$randnum=rand(2, pow(2, 24));
			$tempFile=$_FILES['Filedata']['tmp_name'];
			$filepath="../../upload/album/" . date('Y', time()) . "/" . date('m', time()) . "/" . date('d', time()) . "/";
			creatdir($filepath);
			$targetFile=$filepath . time() . mt_rand(1000, 9999) . "_" . $CcTvAi_com_userid . "." . fileext($_FILES['Filedata']['name']);
			$fileTypes=str_replace('*.', '', '*.jpg;*.jpeg;*.gif;*.png');
			$fileTypes=str_replace(';', '|', $fileTypes);
			$typesArray=preg_split('/\|/', $fileTypes);
			$fileParts=pathinfo($_FILES['Filedata']['name']);
			if(in_array($fileParts['extension'], $typesArray)) {
				if(function_exists('getimagesize')) {
					$tmp_imagesize=@getimagesize($tempFile);
					list($tmp_width, $tmp_height, $tmp_type) = (array) $tmp_imagesize;
					$tmp_size=$tmp_width*$tmp_height;
					if($tmp_size>16777216||$tmp_size<4||empty($tmp_type)||strpos($tmp_imagesize['mime'], 'flash')>0) {
						@unlink($tempFile);
						exit('10001');
					}
				}
				move_uploaded_file($tempFile, $targetFile);
				$targetDir=str_replace('../../', '', $targetFile);
				resizeImage(_cctvai_root_ . $targetDir, 80, 80, $targetFile . '.thumb.' . fileext($_FILES['Filedata']['name']));
				if(!file_exists(_cctvai_root_ . $targetDir . '.thumb.' . fileext($_FILES['Filedata']['name']))) {
					@unlink($tempFile);
					@unlink(_cctvai_root_ . $targetDir);
					exit('10002');
				} else {
					$img_info=getimagesize(_cctvai_root_ . $targetDir);
					resizeImage(_cctvai_root_ . $targetDir, 180, 180, $targetFile . '.thumb_180x180.' . fileext($_FILES['Filedata']['name']));
					resizeImage(_cctvai_root_ . $targetDir, $img_info[0], $img_info[1], $targetFile);
					$setarrs=array(
						'cd_uid'=>$CcTvAi_com_userid,
						'cd_uname'=>$CcTvAi_com_username,
						'cd_uip'=>getonlineip(),
						'cd_title'=>'暂无说明',
						'cd_url'=>str_replace("../../upload/album/", "", $targetFile),
						'cd_hits'=>0,
						'cd_praisenum'=>0,
						'cd_replynum'=>0,
						'cd_theorder'=>0,
						'cd_weborder'=>0,
						'cd_width'=>$img_info[0],
						'cd_height'=>$img_info[1],
						'cd_addtime'=>time()
					);
					inserttable('pic', $setarrs, 1);
					$sql="select cd_id from " . tname('pic') . " where cd_url='" . $targetFile . "'";
					if($row=$db->getrow($sql)) {
						$db->query("update " . tname('pic') . " set cd_theorder='" . $row['cd_id'] . "' where cd_id='" . $row['cd_id'] . "'");
					}
					$setarrss=array(
						'cd_userid'=>$CcTvAi_com_userid,
						'cd_username'=>$CcTvAi_com_username,
						'cd_userip'=>getonlineip(),
						'cd_filetype'=>'图片文件',
						'cd_filename'=>time() . mt_rand(1000, 9999) . "_" . $CcTvAi_com_userid . "." . fileext($_FILES['Filedata']['name']),
						'cd_filesize'=>$_FILES["Filedata"]["size"],
						'cd_fileurl'=>$targetFile,
						'cd_filetime'=>time()
					);
					inserttable('upload', $setarrss, 1);
					$timea=date('Y', time());
					$timeb=date('m', time());
					$timec=date('d', time());
					$timed=mktime(0, 0, 0, $timeb, $timec, $timea);
					$picnum=$db->num_rows($db->query("select cd_id from " . tname('pic') . " where cd_uid='" . $CcTvAi_com_userid . "' and cd_addtime >= '" . $timed . "'"));
					if(($picnum*cd_useruppicpoints)<cd_useruppicceiling) {
						if(cd_pointsuca>=1) {
							$setarr=array(
								'cd_type'=>1,
								'cd_uid'=>$CcTvAi_com_userid,
								'cd_uname'=>$CcTvAi_com_username,
								'cd_icon'=>'album',
								'cd_title'=>'分享照片',
								'cd_points'=>cd_useruppicpoints,
								'cd_state'=>0,
								'cd_addtime'=>date('Y-m-d H:i:s'),
								'cd_endtime'=>getendtime()
							);
							inserttable('bill', $setarr, 1);
						}
						$db->query("update " . tname('user') . " set cd_points=cd_points+'" . cd_useruppicpoints . "',cd_rank=cd_rank+'" . cd_useruppicrank . "' where cd_id='" . $CcTvAi_com_userid . "'");
					}
					$feedsql="select cd_id from " . tname('feed') . " where cd_icon='album' and cd_title='更新了照片' and cd_uid='" . $CcTvAi_com_userid . "'";
					$feed=$db->getrow($feedsql);
					if($feed) {
						$db->query("update " . tname('feed') . " set cd_addtime='" . date('Y-m-d H:i:s') . "' where cd_id='" . $feed['cd_id'] . "'");
					} else {
						$setarrs=array(
							'cd_uid'=>$CcTvAi_com_userid,
							'cd_uname'=>$CcTvAi_com_username,
							'cd_icon'=>'album',
							'cd_title'=>'更新了照片',
							'cd_data'=>'',
							'cd_image'=>'',
							'cd_imagelink'=>'',
							'cd_dataid'=>0,
							'cd_addtime'=>date('Y-m-d H:i:s')
						);
						inserttable('feed', $setarrs, 1);
					}
				}
				exit('10004');
			}
		} else {
			exit('10001');
		}
		exit('10001');
}elseif($ac=="profile") {//上传头像
if(!$userlogined==true){exit('10000');}
if(!empty($_FILES)) {
	$tempFile=$_FILES['Filedata']['tmp_name'];
	if(move_uploaded_file($tempFile, "../../upload/userpic/" . $CcTvAi_com_userid . ".jpg")===false) {
		@unlink($_FILES['Filedata']['tmp_name']);
		exit('10002');
	}
	$tmpPath=_cctvai_root_ . "upload/userpic/" . $CcTvAi_com_userid . ".jpg";
	if(isImage($tmpPath)===false) {@unlink($tmpPath);exit('10001');}
	$IMG48="../../upload/userpic/" . $CcTvAi_com_userid . "_48x48.jpg";
	$IMG120="../../upload/userpic/" . $CcTvAi_com_userid . "_120x120.jpg";
	$IMG200="../../upload/userpic/" . $CcTvAi_com_userid . "_200x200.jpg";
	resizeImage($tmpPath, 48, 48, $IMG48);
	resizeImage($tmpPath, 120, 120, $IMG120);
	resizeImage($tmpPath, 200, 200, $IMG200);
	@unlink($tmpPath);
	$db->query("update " . tname('user') . " set cd_photo='upload/userpic/" . $CcTvAi_com_userid . "_200x200.jpg' where cd_id='" . $CcTvAi_com_userid . "'");
	exit('10004');
} else {
	exit('10001');
}
}elseif($ac=="dance") {//上传音乐
if(!$userlogined==true){exit('10000');}
if(cd_userupload=="0") {exit('10004');}
//if ($_FILES['Filedata']['size']>cd_upmusictype*1024*1024) {@unlink($_FILES['Filedata']['tmp_name']);exit('10005');}
$m="upload/dance";
if(!file_exists(_cctvai_root_ . $m)) {exit('10002');}
if(strpos(cd_upmusictype, fileext($_FILES['Filedata']['name']))!==false) {} else {exit('10001');}
if(!empty($_FILES)) {
	$tempFile=$_FILES['Filedata']['tmp_name'];
	$file=$_FILES['Filedata']['name'];
	$size=$_FILES['Filedata']['size'];
	$zmd5=$size . $file;
	$timemd5='' . substr(md5($zmd5), 0, 32) . '';
	$md5=strtoupper($timemd5);
	$form=strtolower(trim(substr(strrchr($file, "."), 1)));
	$rdanceName=str_replace('.' . $form . '', '', $file);
	$targetFile="" . $md5 . "." . $form . "";
	$targetFiles="../../upload/dance/" . $targetFile . "";
	move_uploaded_file($tempFile, $targetFiles);
	if(!file_exists(_cctvai_root_.$m.'/'.$targetFile)){exit('10002');}
	@unlink($_FILES['Filedata']['tmp_name']);
	//记录上传信息
	$setarrss=array(
		'cd_userid'=>$CcTvAi_com_userid,
		'cd_username'=>$CcTvAi_com_username,
		'cd_userip'=>$_SERVER["REMOTE_ADDR"],
		'cd_filetype'=>'音频文件',
		'cd_filename'=>$targetFile,
		'cd_filesize'=>$_FILES["Filedata"]["size"],
		'cd_fileurl'=>ReplaceStr($targetFiles, "../../../", ""),
		'cd_filetime'=>time()
	);
	inserttable('upload', $setarrss, 1);
	echo "tips=上传成功&cd_url=upload/dance/$targetFile&rServer=1&rsiz=" . formatsize($size) . "&rmd5=$md5";
}else{
	exit('10003');
}
}elseif($ac=="video") {//上传视频
if(!$userlogined==true){exit('10000');}
if(cd_userupload=="0") {exit('10004');}
//if ($_FILES['Filedata']['size']>cd_upvideotype*1024*1024) {@unlink($_FILES['Filedata']['tmp_name']);exit('10005');}
$m="upload/video";
if(!file_exists(_cctvai_root_ . $m)) {exit('10002');}
if(strpos(cd_upvideotype, fileext($_FILES['Filedata']['name']))!==false) {} else {exit('10001');}
if(!empty($_FILES)) {
	$tempFile=$_FILES['Filedata']['tmp_name'];
	$file=$_FILES['Filedata']['name'];
	$size=$_FILES['Filedata']['size'];
	$zmd5=$size . $file;
	$timemd5='' . substr(md5($zmd5), 0, 32) . '';
	$md5=strtoupper($timemd5);
	$form=strtolower(trim(substr(strrchr($file, "."), 1)));
	$rdanceName=str_replace('.' . $form . '', '', $file);
	$targetFile="" . $md5 . "." . $form . "";
	$targetFiles="../../upload/video/" . $targetFile . "";
	move_uploaded_file($tempFile, $targetFiles);
	if(!file_exists(_cctvai_root_.$m.'/'.$targetFile)){exit('10002');}
	@unlink($_FILES['Filedata']['tmp_name']);
	//记录上传信息
	$setarrss=array(
		'cd_userid'=>$CcTvAi_com_userid,
		'cd_username'=>$CcTvAi_com_username,
		'cd_userip'=>$_SERVER["REMOTE_ADDR"],
		'cd_filetype'=>'视频文件',
		'cd_filename'=>$targetFile,
		'cd_filesize'=>$_FILES["Filedata"]["size"],
		'cd_fileurl'=>ReplaceStr($targetFiles, "../../../", ""),
		'cd_filetime'=>time()
	);
	inserttable('upload', $setarrss, 1);
	echo "tips=上传成功&cd_url=upload/video/$targetFile&rServer=1&rsiz=" . formatsize($size) . "&rmd5=$md5";
}else{
	exit('10003');
}
}elseif($ac=="addpic") {//上传封面照片
$id=SafeRequest("id", "get");
switch ($op) {
	case 'malbum':
		$targetDir="../../upload/malbum/";
		$targetDirW="upload/malbum/";
		$targetDirM=date('Ymd')."/".$id.".".fileext($_FILES['Filedata']['name']);
		$targetDirL="" . date('Ymd') . "/" . rand(10,100).$id . ".".fileext($_FILES['Filedata']['name'])."";
		$fileexts=cd_uppictype;
		$filetypes="音乐专辑图片";
		break;
	case 'valbum':
		$targetDir="../../upload/valbum/";
		$targetDirW="upload/valbum/";
		$targetDirM=date('Ymd')."/".$id.".".fileext($_FILES['Filedata']['name']);
		$targetDirL="" . date('Ymd') . "/" . rand(10,100).$id . ".".fileext($_FILES['Filedata']['name'])."";
		$fileexts=cd_uppictype;
		$filetypes="视频专辑图片";
		break;
	case 'videopic':
		$targetDir="../../upload/videopic/";
		$targetDirW="upload/videopic/";
		$targetDirM=date('Ymd')."/".$id.".".fileext($_FILES['Filedata']['name']);
		$targetDirL="" . date('Ymd') . "/" . rand(10,100).$id . ".".fileext($_FILES['Filedata']['name'])."";
		$fileexts=cd_uppictype;
		$filetypes="视频图片";
		break;
	case 'dancepic':
		$targetDir="../../upload/dancepic/";
		$targetDirW="upload/dancepic/";
		$targetDirM=date('Ymd')."/".$id.".".fileext($_FILES['Filedata']['name']);
		$targetDirL="" . date('Ymd') . "/" . rand(10,100).$id . ".".fileext($_FILES['Filedata']['name'])."";
		$fileexts=cd_uppictype;
		$filetypes="音乐图片";
		break;
	default:
	exit('1');
	break;
}
if (!empty($_FILES)) {
	if ($_FILES['Filedata']['size']>cd_uppicsize*1024*1024) {@unlink($_FILES['Filedata']['tmp_name']);exit('10004');}
	if (!$_FILES['Filedata']['name']) {@unlink($_FILES['Filedata']['tmp_name']);exit('10003');}
	if (strpos($fileexts, fileext($_FILES['Filedata']['name']))!==false) {} else {@unlink($_FILES['Filedata']['tmp_name']);exit('10001');}
	if (isImage($_FILES['Filedata']['tmp_name'])===false) {@unlink($_FILES['Filedata']['tmp_name']);exit('10001');}
	if (!file_exists(_cctvai_root_.$targetDirW.date('Ymd'))) {@mkdir(_cctvai_root_.$targetDirW.date('Ymd'), 0777, true);}
	if (@is_dir(_cctvai_root_.$targetDirW.date('Ymd'))===false) {@unlink($_FILES['Filedata']['tmp_name']);exit('10002');}
	if (move_uploaded_file($_FILES['Filedata']['tmp_name'], $targetDir.$targetDirL)===false) {@unlink($_FILES['Filedata']['tmp_name']);exit('10002');}
	@chmod($targetDir.$targetDirL, 0644);
	$img_info = getimagesize(_cctvai_root_.$targetDirW.$targetDirL);
	resizeImage(_cctvai_root_.$targetDirW.$targetDirL, $img_info[0], $img_info[1], $targetDir.$targetDirM);
	@unlink(_cctvai_root_.$targetDirW.$targetDirL);
	@unlink($_FILES['Filedata']['tmp_name']);
	if(!file_exists(_cctvai_root_.$targetDirW.$targetDirM)){
		exit('10001');
		}else{
			//记录上传信息
			$setarrss=array(
			'cd_userid' => $CcTvAi_com_userid,
			'cd_username' => $CcTvAi_com_username,
			'cd_userip' => $_SERVER["REMOTE_ADDR"],
			'cd_filetype' => $filetypes,
			'cd_filename' => "" . date('Ymd') . "/" . $id . "." . fileext($_FILES['Filedata']['name']) . "",
			'cd_filesize' => $_FILES["Filedata"]["size"],
			'cd_fileurl' => $targetDirW.$targetDirM,
			'cd_filetime' => time()
			);
			inserttable('upload', $setarrss, 1);
			echo "tips=上传成功&cd_pic=".$targetDirW.$targetDirM."";
		}
}else{
	exit('10003');
}
	
	
} 
?>