<?php
error_reporting(0);
global $db, $CcTvAi_com_userid;
$do=SafeRequest("do", "get");
if($do=="add") {
	$CcTvAi_com_userid=SafeRequest("userid", "post");
	$CcTvAi_com_username=base64_decode(SafeRequest("username", "post"));
	if(!IsNumID($CcTvAi_com_userid)) {exit();}
	if($CcTvAi_com_userid) {
		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('1');
					}
				}
				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('1');
				} 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);
					}
				}
				echo '0';
			}
		} else {
			echo 'Invalid file type.';
		}
	}
}
?>