<?php
if(!IN_SLUM) exit;
function get_avatar($uid, $size = 'middle', $type = '') {
	return $uid.'_avatar_'.$size.'.jpg';
}
	function onuploadavatar($uid){	
	
		$uid = abs(intval($uid));
		if(empty($uid)) {
			return -1;
		}
		if(empty($_FILES['Filedata'])) {
			return -3;
		}
		list($width, $height, $type, $attr) = getimagesize($_FILES['Filedata']['tmp_name']);
		$imgtype = array(1 => '.gif', 2 => '.jpg', 3 => '.png');
		$filetype = $imgtype[$type];
		if(!$filetype) $filetype = '.jpg';
		$tmpavatar = 'tmp/upload/upload'.$uid.$filetype;
		file_exists($tmpavatar) && @unlink($tmpavatar);
		if(@copy($_FILES['Filedata']['tmp_name'], $tmpavatar) || @move_uploaded_file($_FILES['Filedata']['tmp_name'], $tmpavatar)) {
			@unlink($_FILES['Filedata']['tmp_name']);
			list($width, $height, $type, $attr) = getimagesize($tmpavatar);
			if($width < 10 || $height < 10 || $type == 4) {
				@unlink($tmpavatar);
				return -2;
			}
		} else {
			@unlink($_FILES['Filedata']['tmp_name']);
			return -4;
		}		
		
		$avatarurl =SLUM_DATAURL.'/tmp/upload/upload'.$uid.$filetype;
		return $avatarurl;		
	}
    function onrectavatar($rid) {
		/*添加*/
		global $empire,$public_r,$dbtbpre;
		$ur=user_returnlogin();
		$uid=$ur[userid];
		$uid = abs(intval($uid));
		if(empty($uid)) {
			return '<root><message type="error" value="-1" /></root>';
		}
		$avatartype = getgpc('avatartype', 'G') == 'real' ? 'real' : 'virtual';
		$bigavatarfile = SLUM_SAVEDIR.get_avatar($uid, 'big', $avatartype);
		$middleavatarfile = SLUM_SAVEDIR.get_avatar($uid, 'middle', $avatartype);
		$smallavatarfile = SLUM_SAVEDIR.get_avatar($uid, 'small', $avatartype);
		$bigavatar = flashdata_decode(getgpc('avatar1', 'P'));
		$middleavatar = flashdata_decode(getgpc('avatar2', 'P'));
		$smallavatar = flashdata_decode(getgpc('avatar3', 'P'));	
		if(!$bigavatar || !$middleavatar || !$smallavatar) {
			return '<root><message type="error" value="-2" /></root>';
		}
		
		$success = 1;
		@file_put_contents($bigavatarfile,$bigavatar);
		@file_put_contents($middleavatarfile,$middleavatar);
		@file_put_contents($smallavatarfile,$smallavatar);
		$biginfo = @getimagesize($bigavatarfile);
		$middleinfo = @getimagesize($middleavatarfile);
		$smallinfo = @getimagesize($smallavatarfile);
		if(!$biginfo || !$middleinfo || !$smallinfo || $biginfo[2] == 4 || $middleinfo[2] == 4 || $smallinfo[2] == 4) {
			file_exists($bigavatarfile) && unlink($bigavatarfile);
			file_exists($middleavatarfile) && unlink($middleavatarfile);
			file_exists($smallavatarfile) && unlink($smallavatarfile);
			$success = 0;
		}

		$filetype = '.jpg';
		@unlink(SLUM_ROOT.'/tmp/upload/upload'.$rid.$filetype);
		if($success){
			/*写入数据库*/
			$userpic='/user/header/avatar/'.get_avatar($uid,'small',$avatartype);
			$up=$empire->query("update {$dbtbpre}enewsmemberadd set userpic='$userpic' where userid='$uid'");
			/*写入数据库结束*/
			return '<?xml version="1.0" ?><root><face success="1"/></root>';
		} else {
			return '<?xml version="1.0" ?><root><face success="0"/></root>';
		}		
		}



	function flashdata_decode($s) {
		$r = '';
		$l = strlen($s);
		for($i=0; $i<$l; $i=$i+2) {
			$k1 = ord($s[$i]) - 48;
			$k1 -= $k1 > 9 ? 7 : 0;
			$k2 = ord($s[$i+1]) - 48;
			$k2 -= $k2 > 9 ? 7 : 0;
			$r .= chr($k1 << 4 | $k2);
		}
		return $r;
	}
function daddslashes($string, $force = 0, $strip = FALSE) {
	if(!MAGIC_QUOTES_GPC || $force) {
		if(is_array($string)) {
			foreach($string as $key => $val) {
				$string[$key] = daddslashes($val, $force, $strip);
			}
		} else {
			$string = addslashes($strip ? stripslashes($string) : $string);
		}
	}
	return $string;
}

function getgpc($k, $var='R') {
	switch($var) {
		case 'G': $var = &$_GET; break;
		case 'P': $var = &$_POST; break;
		case 'C': $var = &$_COOKIE; break;
		case 'R': $var = &$_REQUEST; break;
	}
	return isset($var[$k]) ? $var[$k] : NULL;
}
?>