<?php
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../../core.php";
} 
saxue_checkpower( 'users' );
if ( empty( $_REQUEST['uid'] ) ) {
		saxue_printfail( LANG_NO_USER );
} 
$GLOBALS['_REQUEST']['uid'] = intval( $_REQUEST['uid'] );
include_once( SAXUE_ROOT_PATH . "/model/member_users.php" );
$users_handler = &saxuememberusershandler :: getinstance( "saxuememberusershandler" );
$user = $users_handler -> get( $_REQUEST['uid'] );
if ( !is_object( $user ) ) {
		saxue_printfail( LANG_NO_USER );
} 
if ( !isset( $_REQUEST['action'] ) ) {
		$GLOBALS['_REQUEST']['action'] = "edit";
} 
switch ( $_REQUEST['action'] ) {
		case "delete" :
				$basedir = saxue_uploadpath( 'avatar' ) . saxue_getsubdir( $_REQUEST['uid'] );
				saxue_delfile( $basedir . "/" . $_REQUEST['uid'] . '.jpg' );
				saxue_delfile( $basedir . "/" . $_REQUEST['uid'] . 's.jpg' );
				saxue_delfile( $basedir . "/" . $_REQUEST['uid'] . 'i.jpg' );
				$users_handler -> db -> query( 'UPDATE ' . saxue_dbprefix( 'article' ) . ' SET uid=0 WHERE uid=' . $_REQUEST['uid'] );
				$users_handler -> db -> query( 'DELETE FROM ' . saxue_dbprefix( 'member_account' ) . ' WHERE uid=' . $_REQUEST['uid'] );
				$users_handler -> db -> query( 'DELETE FROM ' . saxue_dbprefix( 'member_bill' ) . ' WHERE uid=' . $_REQUEST['uid'] );
				$users_handler -> db -> query( 'DELETE FROM ' . saxue_dbprefix( 'member_doing' ) . ' WHERE uid=' . $_REQUEST['uid'] );
				$users_handler -> db -> query( 'DELETE FROM ' . saxue_dbprefix( 'member_follow' ) . ' WHERE uid=' . $_REQUEST['uid'] . ' OR toid=' . $_REQUEST['uid'] );
				$users_handler -> db -> query( 'DELETE FROM ' . saxue_dbprefix( 'member_message' ) . ' WHERE fromid=' . $_REQUEST['uid'] . ' OR toid=' . $_REQUEST['uid'] );
				$users_handler -> db -> query( 'DELETE FROM ' . saxue_dbprefix( 'member_oauth' ) . ' WHERE uid=' . $_REQUEST['uid'] );
				$users_handler -> db -> query( 'DELETE FROM ' . saxue_dbprefix( 'member_money' ) . ' WHERE uid=' . $_REQUEST['uid'] );
				$users_handler -> db -> query( 'DELETE FROM ' . saxue_dbprefix( 'member_score' ) . ' WHERE uid=' . $_REQUEST['uid'] );
				$users_handler -> db -> query( 'DELETE FROM ' . saxue_dbprefix( 'member_userdata' ) . ' WHERE uid=' . $_REQUEST['uid'] );
				$users_handler -> db -> query( 'DELETE FROM ' . saxue_dbprefix( 'member_users' ) . ' WHERE uid=' . $_REQUEST['uid'] );
				if ( !empty( $_SERVER['HTTP_REFERER'] ) ) {
						saxue_jumppage( $_SERVER['HTTP_REFERER'] );
				} else {
						saxue_jumppage( "users.php" );
				}
		case "edit" :
				include SAXUE_ADMIN_PATH . "/header.php";
				$saxueTpl -> assign_by_ref( "row", $user -> getvars( "n" ) );
				$saxueTpl -> setcaching( 0 );
				$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/member/templates/user_edit.html";
				include SAXUE_ADMIN_PATH . "/footer.php";
				exit;
		case "update" :
				$pass = trim( $_POST['pass'] );
				$repass = trim( $_POST['repass'] );
				if ( $pass != $repass ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '两次密码输入不同' ) ) );
				} 
				$up = array();
				$up['uname'] = trim( $_POST['uname'] );
				$up['email'] = trim( $_POST['email'] );
				if ( strlen( $pass ) > 0 ) {
						$up['password'] = $users_handler -> encryptpass( $pass );
				}
				if ( strlen( $up['uname'] ) == 0 ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '用户名不能为空' ) ) );
				} else if ( preg_match( "/^\\s*\$|^c:\\con\\con\$|[@%,;:\\.\\|\\*\\\"'\\\\\\/\\s\\t\\<\\>\\&]|　/is", $up['uname'] ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '用户名格式错误' ) ) );
				}
				if ( strlen( $up['email'] ) == 0 ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '邮箱不能为空' ) ) );
				} else if ( !preg_match( "/^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+([\\.][a-z0-9-]+)+\$/i", $up['email'] ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '邮箱格式错误' ) ) );
				}
				if ( $up['uname'] != $user -> getvar( "uname" ) && 0 < $users_handler -> getcount( new criteria( 'uname', $up['uname'] ) ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '该用户名已被使用' ) ) );
				} 
				if ( $up['email'] != $users -> getvar( "email" ) && 0 < $users_handler -> getcount( new criteria( 'email', $up['email'] ) ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '该邮箱已被使用' ) ) );
				} 
				$user -> unsetnew();
				$user -> setvars( $up, true );
				if ( !$users_handler -> insert( $user ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '修改失败' ) ) );
				}
				exit( json_encode( array( 'flag' => 1 ) ) );
		case "score" :
				saxue_getconfigs( "scoretype", "member" );
				include SAXUE_ADMIN_PATH . "/header.php";
				$saxueTpl -> assign_by_ref( "row", $user -> getvars( "n" ) );
				$saxueTpl -> assign_by_ref( "scoretype", $saxueScoretype );
				$saxueTpl -> setcaching( 0 );
				$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/member/templates/user_score.html";
				include SAXUE_ADMIN_PATH . "/footer.php";
				exit;
		case "setscore" :
				$_POST['op'] = intval( $_POST['op'] );
				$_POST['type'] = intval( $_POST['type'] );
				$_POST['score'] = intval( $_POST['score'] );
				$_POST['aid'] = intval( $_POST['aid'] );
				if ( empty( $_POST['score'] ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '请正确输入操作积分数' ) ) );
				}
				if ( $_POST['op'] ) {
						$_POST['score'] = -$_POST['score'];
				}
				if ( !$users_handler -> changescore( $_REQUEST['uid'], $_POST['score'], $_POST['type'], $_POST['itemid'] ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '更改会员积分失败' ) ) );
				}
				exit( json_encode( array( 'flag' => 1 ) ) );
		case "money" :
				saxue_getconfigs( "moneytype", "member" );
				include SAXUE_ADMIN_PATH . "/header.php";
				$saxueTpl -> assign_by_ref( "row", $user -> getvars( "n" ) );
				$saxueTpl -> assign_by_ref( "moneytype", $saxueMoneytype );
				$saxueTpl -> setcaching( 0 );
				$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/member/templates/user_money.html";
				include SAXUE_ADMIN_PATH . "/footer.php";
				exit;
		case "setmoney" :
				$_POST['op'] = intval( $_POST['op'] );
				$_POST['type'] = intval( $_POST['type'] );
				$_POST['money'] = saxue_dround( trim( $_POST['money'] ) );
				$_POST['aid'] = intval( $_POST['aid'] );
				if ( empty( $_POST['money'] ) || $_POST['money'] == 0 ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '请正确输入操作资金数' ) ) );
				}
				if ( $_POST['op'] ) {
						$_POST['money'] = -$_POST['money'];
				}
				if ( !$users_handler -> changemoney( $_REQUEST['uid'], $_POST['money'], $_POST['type'], $_POST['itemid'] ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '更改会员资金失败' ) ) );
				}
				exit( json_encode( array( 'flag' => 1 ) ) );
} 
