<?php
include "../core.php";
saxue_checklogin();
include SAXUE_ROOT_PATH . "/model/member_users.php";
$users_handler = &saxuememberusershandler :: getinstance( "saxuememberusershandler" );
$saxueUsers = $users_handler -> get( $_SESSION['saxueUserId'] );
if ( !$saxueUsers ) {
		saxue_printfail( LANG_NO_USER );
} 
if ( !isset( $_REQUEST['action'] ) ) {
		$GLOBALS['_REQUEST']['action'] = "show";
} 
switch ( $_REQUEST['action'] ) {
		case "cutsave" :
				$basedir = saxue_uploadpath( 'avatar' ) . saxue_getsubdir( $saxueUsers -> getvar( "uid", "n" ) );
				saxue_checkdir( $basedir, true );
				$newfile = $basedir . "/" . $saxueUsers -> getvar( "uid", "n" ) . '.jpg';
				$smallfile = $basedir . "/" . $saxueUsers -> getvar( "uid", "n" ) . 's.jpg';
				$iconfile = $basedir . "/" . $saxueUsers -> getvar( "uid", "n" ) . 'i.jpg';
				$big_file_data = base64_decode( $_POST['big_avatar'] );
				$middle_file_data = base64_decode( $_POST['middle_avatar'] );
				$little_avatar_data = base64_decode( $_POST['little_avatar'] );
				if ( !empty( $big_file_data ) ) {
						file_put_contents( $newfile, $big_file_data );
						file_put_contents( $smallfile, $middle_file_data );
						file_put_contents( $iconfile, $little_avatar_data );
						
						if ( $saxueUsers -> getvar( "avatar", "n" ) != 1 ) {
								$saxueUsers -> unsetnew();
								$saxueUsers -> setvar( "avatar", 1 );
								if ( !$users_handler -> insert( $saxueUsers ) ) {
										exit( '更新数据库错误' );
								}
						}
						exit( '200' ); 
				} else {
						exit( '头像上传失败' );
				} 
				break;
		case "upload" :
				if ( !function_exists( "gd_info" ) ) {
						saxue_delfile( $_FILES['avatarimage']['tmp_name'] );
						exit( json_encode( array( 'flag' => 0, 'msg' => '没有加载GD图像库' ) ) );
				}
				if ( empty( $_FILES['avatarimage']['name'] ) ) {
						saxue_delfile( $_FILES['avatarimage']['tmp_name'] );
						exit( json_encode( array( 'flag' => 0, 'msg' => '请选择图片' ) ) );
				} 
				if ( 0 < $_FILES['avatarimage']['error'] ) {
						saxue_delfile( $_FILES['avatarimage']['tmp_name'] );
						exit( json_encode( array( 'flag' => 0, 'msg' => '图片上传失败' ) ) );
				} 
				if ( !preg_match( "/\.(gif|jpg|jpeg|png)$/i", $_FILES["avatarimage"]["name"] ) ) {
						saxue_delfile( $_FILES['avatarimage']['tmp_name'] );
						exit( json_encode( array( 'flag' => 0, 'msg' => '图片格式错误' ) ) );
				}
				if ( 10485760 < $_FILES['avatarimage']['size'] ) {
						saxue_delfile( $_FILES['avatarimage']['tmp_name'] );
						exit( json_encode( array( 'flag' => 0, 'msg' => '图片大小不能超过 10M' ) ) );
				} 
				$basedir = saxue_uploadpath( 'avatar' ) . saxue_getsubdir( $saxueUsers -> getvar( "uid", "n" ) );
				saxue_checkdir( $basedir, true );
				$newfile = $basedir . "/" . $saxueUsers -> getvar( "uid", "n" ) . '.jpg';
				$smallfile = $basedir . "/" . $saxueUsers -> getvar( "uid", "n" ) . 's.jpg';
				$iconfile = $basedir . "/" . $saxueUsers -> getvar( "uid", "n" ) . 'i.jpg';
				$tmpfile = $basedir . "/tmp_" . $_SESSION['saxueUserId'] . "_" . $_FILES['avatarimage']['name'];
				@move_uploaded_file( $_FILES['avatarimage']['tmp_name'], $tmpfile );
				@chmod( $tmpfile, 511 );
				include SAXUE_ROOT_PATH . "/lib/util/imageresize.php";
				$imgresize = new imageresize();
				$imgresize -> load( $tmpfile );
				if ( $imgresize -> _width != $imgresize -> _height ) {
						if ( $imgresize -> _width > $imgresize -> _height ) $_cut = $imgresize -> _height;
						else $_cut = $imgresize -> _width;
						$imgresize -> cut( $_cut, $_cut );
				} 
				$imgresize -> resize( '150', '150' );
				$imgresize -> save( $newfile );
				$imgresize -> resize( '50', '50' );
				$imgresize -> save( $smallfile );
				$imgresize -> resize( '16', '16' );
				$imgresize -> save( $iconfile, true );
				saxue_delfile( $tmpfile );
				$saxueUsers -> unsetnew();
				$saxueUsers -> setvar( "avatar", 1 );
				$users_handler -> insert( $saxueUsers );
				exit( json_encode( array( 'flag' => 1 ) ) );
				break;
		case "show" :
				include SAXUE_WEB_PATH . "/member/header.php";
				$urls = saxue_geturl( "user_avatar", $saxueUsers -> getvar( "uid", "n" ), "a", $saxueUsers -> getvar( "avatar", "n" ) );
				if ( is_array( $urls ) ) {
						$saxueTpl -> assign( "url_avatar", $urls['l'] );
						$saxueTpl -> assign( "url_avatars", $urls['s'] );
						$saxueTpl -> assign( "url_avatari", $urls['i'] );
				} 
				$saxueTpl -> setcaching( 0 );
				$saxueTset['saxue_contents_template'] = SAXUE_THEME_PATH . "/member/avatar.html";
				include SAXUE_WEB_PATH . "/member/footer.php";
				break;
} 
