<?php
header( "Content-Type:text/html;charset=utf-8" );
header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
header( "Cache-Control: no-cache, must-revalidate" );
header( "Pramga: no-cache" );
include "../core.php";
if ( empty( $_SESSION['saxueUserId'] ) ) {
		exit( json_encode( array( 'flag' => 0, 'msg' => '请先登录您的账户' ) ) );
} 
$toid = intval( $_REQUEST['toid'] );
if ( empty( $toid ) ) {
		exit( json_encode( array( 'flag' => 0, 'msg' => '参数错误' ) ) );
}
include SAXUE_ROOT_PATH . "/model/member_users.php";
$users_handler = &saxuememberusershandler :: getinstance( "saxuememberusershandler" );
$user = $users_handler -> get( $toid );
if ( !$user || $user -> getvar( 'status' ) != 1 ) {
		exit( json_encode( array( 'flag' => 0, 'msg' => '用户不存在' ) ) );
} 
switch ( trim( $_REQUEST['do'] ) ) {
		case 'follow': 
				if ( $_SESSION['saxueUserId'] == $toid ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '不能关注自己' ) ) );
				}
				$sql = 'INSERT INTO ' . saxue_dbprefix( 'member_follow' ) . '(uid, toid, followtime) VALUES (' . $_SESSION['saxueUserId'] . ', ' . $toid . ', ' . SAXUE_NOW_TIME . ')';
				if ( !$users_handler -> db -> query( $sql ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '您已关注该用户' ) ) );
				} 
				$users_handler -> updatefields( 'fans=fans+1', 'uid=' . $toid );
				$users_handler -> updatefields( 'follows=follows+1', 'uid=' . $_SESSION['saxueUserId'] );
				exit( json_encode( array( 'flag' => 1, 'msg' => '关注成功' ) ) );
		case 'unfollow': 
				$sql = 'DELETE FROM ' . saxue_dbprefix( 'member_follow' ) . ' WHERE uid=' . $_SESSION['saxueUserId'] . ' AND toid=' . $toid;
				$res = $users_handler -> db -> query( $sql );
				if ( !$res ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '您没有关注该用户' ) ) );
				} 
				$users_handler -> updatefields( 'fans=fans-1', 'uid=' . $toid );
				$users_handler -> updatefields( 'follows=follows-1', 'uid=' . $_SESSION['saxueUserId'] );
				exit( json_encode( array( 'flag' => 1, 'msg' => '已取消关注' ) ) );
		case 'message': 
				if ( $_SESSION['saxueUserId'] == $toid ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '不能给自己发短信' ) ) );
				}
				$_POST['content'] = trim( $_POST['content'] );
				if ( strlen( $_POST['content'] ) == 0 ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '短信内容不能为空' ) ) );
				} 
				$_POST['title'] = trim( $_POST['title'] );
				if ( strlen( $_POST['title'] ) == 0 ) {
						$_POST['title'] = saxue_substr( strip_tags( $_POST['content'] ), 0, 50 );
				} elseif ( strlen( $_POST['title'] ) > 50 ) {
						$_POST['title'] = saxue_substr( strip_tags( $_POST['title'] ), 0, 50 );
				}
				include SAXUE_ROOT_PATH . "/model/member_message.php";
				$message_handler = &saxuemembermessagehandler :: getinstance( "saxuemembermessagehandler" );
				$newMessage = $message_handler -> create();
				$newMessage -> setvar( "postdate", SAXUE_NOW_TIME );
				$newMessage -> setvar( "fromid", $_SESSION['saxueUserId'] );
				$newMessage -> setvar( "fromname", $_SESSION['saxueUserName'] );
				$newMessage -> setvar( "toid", $toid );
				$newMessage -> setvar( "toname", $user -> getvar( 'uname' ) );
				if ( !$message_handler -> insert( $newMessage ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '短信发送失败' ) ) );
				} 
				exit( json_encode( array( 'flag' => 1, 'msg' => '发送成功' ) ) );
} 
exit( json_encode( array( 'flag' => 0, 'msg' => '操作错误' ) ) );
