<?php
if(!defined('R_P')) exit('Access Denied');
if ( !$user_info['username']) qbase::showmsg( "请登陆为会员", "default", "user.php?ac=login");

if ( $_POST['myFormAc'] == 'update' )
{
	$url = "user.php?ac=profile";
	$user = $_POST['user'];
	$forbidden = array('uid','username','gid', 'point', 'validity');
	foreach($forbidden as $val){
		if(isset($user[$val])) unset($user[$val]);
	}
	
	$user['name'] = trim($user['name']);
	$user['name_purview'] = intval($user['name_purview']) > 0 ? 1 :0;
	$user['sex'] = in_array(intval($user['sex']), array(0,1,2)) ? intval($user['sex']) : 0;
	$user['qq'] = trim($user['qq']);
	$user['birth_year'] = intval($user['birth_year']);
	$user['birth_month'] = intval($user['birth_month']);
	$user['birth_day'] = intval($user['birth_day']);
	if(!empty($user['qq'] ) && (!is_numeric($user['qq']) || strlen( $user['qq'] ) > 20 || strlen( $user['qq'] ) < 5 )){
		qbase::showmsg( "请输入正确的QQ号！" );
	}
	$sql = qbase::createSql(TABLEPRE.'user_extend', $user, 'update') . " where uid = '{$user_info['uid']}' ";
	qdb::query($sql);
	
	/*上传头像*/
	if (isset($_FILES['pic']) && !empty($_FILES['pic']) && $_FILES['pic']["error"] == 0){
		$pic = $_FILES['pic'];
		if (2048 < $pic["size"] / 1024) {
			qbase::showmsg( "头像大小不能超过2M", $_SET['skin'], "user.php?ac=profile" );
		} 	
		
		$extname = qbase::fileext($pic["name"]);
		$filename = $user_info['uid'] .'.'. $extname;
		
		$exts = array('gif','png','jpeg','jpg');
		if (!in_array(strtolower($extname), $exts)) {
			qbase::showmsg( "头像文件格式只能是" . implode('、', $exts), $_SET['skin'], "user.php?ac=profile" );
		} 
		
		$sUploadDir = "attachment/avatar/" . qbase::number2path($user_info['uid']) . '/';
		$real_file_name = $sUploadDir . $filename;

		if (!is_dir(R_P .'/'. $sUploadDir)) {
			qbase::createdir(R_P .'/'. $sUploadDir);
		} 
		
		if (false !== @move_uploaded_file($pic['tmp_name'], R_P .'/'. $real_file_name) || false !== @copy($pic['tmp_name'], R_P .'/'. $real_file_name)){
			$param = array(
				'pic' => $sUploadDir . $user_info['uid'],
				'pic_modify' => $_SERVER['REQUEST_TIME']
			);
			$sql = qbase::createSql(TABLEPRE.'user_extend', $param, 'update') . " where uid = '{$user_info['uid']}' ";
			qdb::query($sql);
			clearstatcache();
			require(R_P . "/include/class.resizeimage.php");
			$resize = new resizeimage(R_P .'/'. $real_file_name);
			$rs = $resize->init();
			if($rs != 0) qbase::showmsg( "头像解析失败，可能这不是一个正常的图像", $_SET['skin'], "user.php?ac=profile" );
			foreach(array(32,64,128) as $size){
				$resize->run($size, $size, 1, R_P .'/'. $sUploadDir . $user_info['uid'] .'_' .$size. '.jpg');
			}
			$resize->close();
			clearstatcache();
		}else{
			qbase::showmsg( "头像移动失败, 请联系管理员", $_SET['skin'], "user.php?ac=profile" );
		}
	}
	qbase::get_object('login')->delete_user_cache($user_info['uid']);
	qbase::showmsg( "个人资料修改成功！", $_SET['skin'], "user.php?ac=profile" );
}else{
	$user_info['birth_year_html'] = $user_info['birth_month_html'] = $user_info['birth_day_html'] = '';
	$year_end = date('Y');
	for ( $i = 1971; $i < $year_end; $i++ ){
		$selected = $i == $user_info['birth_year'] ? "selected" : "";
		$user_info['birth_year_html'].= " <option value=\"$i\" $selected>$i</option>\n";
	}
	for ( $i = 1; $i < 13; $i++ ){
		$selected = $i == $user_info['birth_month'] ? "selected" : "";
		$user_info['birth_month_html'].= " <option value=\"$i\" $selected>$i</option>\n";
	}
	for ( $i = 1; $i < 32; $i++ ){
		$selected = $i == $user_info['birth_day'] ? "selected" : "";
		$user_info['birth_day_html'].= " <option value=\"$i\" $selected>$i</option>\n";
	}

	include qbase::template($_SET['skin'] . '/user/profile' );
}
?>