<?php
!defined('M_P') && exit('Forbidden');
!$winduid && Showmsg('not_login');
define('AJAX','1');
InitGP(array('action'));
$isGM = CkInArray($windid,$manager);
!$isGM && $groupid==3 && $isGM=1;
if ($action == 'delshare') {
	InitGP(array('id'),'',2);
	if (!$id) Showmsg('undefined_action');
	$share = $db->get_one("SELECT * FROM pw_share WHERE id=".pwEscape($id));
	!$share && Showmsg('mode_o_no_share');
	if ($winduid != $share['uid'] && !$isGM) {
		Showmsg('mode_o_delshare_permit_err');
	}
	$db->update("DELETE FROM pw_share WHERE id=".pwEscape($id));
	delComment('share',$id);
	echo "success\t";ajax_footer();
} elseif ($action == 'delfriend') {
	InitGP(array('u'),'',2);
	if ($u) {
		if ($friend = getOneFriend($u)) {
			$db->update("DELETE FROM pw_friends WHERE (uid=".pwEscape($winduid)." AND friendid=".pwEscape($u).") OR (uid=".pwEscape($u)." AND friendid=".pwEscape($winduid).")");
			$db->update("UPDATE pw_memberdata SET f_num=f_num-1 WHERE uid=".pwEscape($winduid)." OR uid=".pwEscape($u));
			echo "success\t";ajax_footer();
		} else {
			Showmsg('mode_o_not_friend');
		}
	} else {
		Showmsg('mode_o_not_uid');
	}
} elseif ($action == 'addfriend') {
	InitGP(array('u'),'',2);
	if ($u == $winduid) {
		Showmsg('friend_selferror');
	}
	if ($u) {
		if (getOneFriend($u)) {
			Showmsg('mode_o_is_friend');
		}
		$pwSQL = array();
		$pwSQL[] = array($winduid,$u,$timestamp,0);
		$pwSQL[] = array($u,$winduid,$timestamp,0);
		$db->update("REPLACE INTO pw_friends(uid,friendid,joindate,status) VALUES ".pwSqlMulti($pwSQL,false));
		$db->update("UPDATE pw_memberdata SET f_num=f_num+1 WHERE uid=".pwEscape($winduid)." OR uid=".pwEscape($u));
		$myurl = $db_bbsurl."/".$basename."q=user&u=".$u;
		$msg = array(
				'toUid'		=> $u,
				'subject'	=> 'o_friend_success_title',
				'content'	=> 'o_friend_success_cotent',
				'other'		=> array(
					'uid'		=> $winduid,
					'username'	=> $windid,
					'myurl'		=> $myurl
				)
			);
		require_once R_P.'require/msg.php';
		pwSendMsg($msg);
		if ($db_share) {//dongtai

		}
		echo "success\t";ajax_footer();
	} else {
		Showmsg('mode_o_not_uid');
	}
} elseif ($action == 'showcommlist') {
	InitGP(array('type','id'),'P');
	$id = (int)$id;
	if (!$id) Showmsg('undefined_action');
	if(!checkCommType($type)) Showmsg('undefined_action');

	$comment = array();
	$query = $db->query("SELECT * FROM pw_comment WHERE type=".pwEscape($type)." AND typeid=".pwEscape($id)." ORDER BY id".pwLimit(0,100));
	while ($rt = $db->fetch_array($query)) {
		$rt['postdate'] = get_date($rt['postdate']);
		$comment[] = $rt;
	}
	$str = '';
	if ($comment) {
		$str = pwJsonEncode($comment);
	}
	echo "success\t".$str;
	ajax_footer();
} elseif ($action == 'commreply') {
	InitGP(array('type','id','title','upid'),'P');
	$id = (int)$id;
	$upid = (int)$upid ? (int)$upid : 0;
	if (!$id) Showmsg('undefined_action');
	if(!checkCommType($type)) Showmsg('undefined_action');
	$app_table = $id_filed = '';
	list($app_table,$id_filed) = getCommTypeTable($type);
	$count = $db->get_value("SELECT COUNT(*) AS count FROM pw_comment WHERE type=".pwEscape($type)." AND typeid=".pwEscape($id));
	if ($count > 99) Showmsg('mode_o_com_count_max');
	if (strlen($title)<3 || strlen($title)>200) {
		Showmsg('mode_o_com_title_error');
	}
	if (@include(D_P."data/bbscache/wordsfb.php")) {
		foreach ($wordsfb as $key => $value) {
			$banword = (string) stripslashes($key);
			if (strpos($title,$banword)!==false) {
				Showmsg('title_wordsfb');
			}
		}
		foreach ($replace as $key => $value) {
			$banword = (string) stripslashes($key);
			if (strpos($title,$banword)!==false) {
				Showmsg('post_wordsfb');
			}
		}
	}
	$data = array(
		'uid' 		=> $winduid,
		'username'	=> $windid,
		'title'		=> $title,
		'type'		=> $type,
		'typeid'	=> $id,
		'upid'		=> $upid,
		'postdate'	=> $timestamp
	);
	$db->update("INSERT INTO pw_comment SET ".pwSqlSingle($data));
	$insertid = $db->insert_id();
	if ($app_table && $id_filed) {
		$db->update("UPDATE $app_table SET c_num=c_num+1 WHERE $id_filed=".pwEscape($id));
	}
	if ($insertid) {
		echo "success\t".$insertid;
		ajax_footer();
	} else {
		Showmsg('undefined_action');
	}
} elseif ($action == 'commdel') {
	InitGP(array('id'),'P',2);
	if (!$id) Showmsg('undefined_action');
	$thiscomm = $db->get_one("SELECT uid,type,typeid FROM pw_comment WHERE id=".pwEscape($id));
	if (!$isGM) {
		if ($thiscomm['uid'] != $winduid) Showmsg('mode_o_com_del_priv');
	}

	$updatenum = 0;
	$db->update("DELETE FROM pw_comment WHERE id=".pwEscape($id));
	$updatenum += $db->affected_rows();
	$db->update("DELETE FROM pw_comment WHERE upid=".pwEscape($id));
	$updatenum += $db->affected_rows();
	list($app_table,$app_filed) = getCommTypeTable($thiscomm['type']);
	if ($updatenum && $app_table && $thiscomm['typeid']) {
		$db->update("UPDATE $app_table SET c_num=c_num-".pwEscape($updatenum)." WHERE $app_filed=".pwEscape($thiscomm['typeid']));
	}
	echo "success\t";
	ajax_footer();
} elseif ($action == 'addfriendtype') {
	InitGP(array('u','name'),'P');
	$u = (int) $u;
	if (!$u) Showmsg('undefined_action');
	if ($u != $winduid) Showmsg('undefined_action');
	if (strlen($name)<1 || strlen($name)>20) Showmsg('mode_o_addftype_name_leng');
	$check = $db->get_one("SELECT ftid FROM pw_friendtype WHERE uid=".pwEscape($u)." AND name=".pwEscape($name));
	if ($check) Showmsg('mode_o_addftype_name_exist');
	$count = $db->get_value("SELECT COUNT(*) AS count FROM pw_friendtype WHERE uid=".pwEscape($u));
	if ($count>=20) Showmsg('mode_o_addftype_length');
	$db->update("INSERT INTO pw_friendtype(uid,name) VALUES(".pwEscape($u).",".pwEscape($name).")");
	$id = $db->insert_id();
	if ($id) {
		echo "success\t$id";
		ajax_footer();
	} else {
		Showmsg('undefined_action');
	}
} elseif ($action == 'delfriendtype') {
	InitGP(array('u','ftid'),'P',2);
	$where = '';
	if (!$isG) {
		if (!$u) Showmsg('undefined_action');
		if ($u != $winduid) Showmsg('undefined_action');
		$where .= " AND uid=".pwEscape($u);
	}

	if (!$ftid) Showmsg('undefined_action');
	$db->update("DELETE FROM pw_friendtype WHERE ftid=".pwEscape($ftid)."$where");
	if ($db->affected_rows()) {
		$db->update("UPDATE pw_friends SET ftid=0 WHERE ftid=".pwEscape($ftid));
		echo "success";
		ajax_footer();
	} else {
		Showmsg('undefined_action');
	}
} elseif ($action == 'eidtfriendtype') {
	InitGP(array('u','ftid','name'),'P');
	$u = (int) $u;
	$ftid = (int) $ftid;
	if (!$u) Showmsg('undefined_action');
	if (!$isG) {
		if ($u != $winduid) Showmsg('mode_o_addftype_n');
	}
	if (!$ftid) Showmsg('undefined_action');
	if (strlen($name)<1 || strlen($name)>20) Showmsg('mode_o_addftype_name_leng');
	$check = $db->get_one("SELECT ftid FROM pw_friendtype WHERE uid=".pwEscape($u)." AND name=".pwEscape($name));
	if ($check) Showmsg('mode_o_addftype_name_exist');
	$db->update("UPDATE pw_friendtype SET name=".pwEscape($name)." WHERE uid=".pwEscape($u)." AND ftid=".pwEscape($ftid));
	if ($db->affected_rows()) {
		echo "success";
		ajax_footer();
	} else {
		Showmsg('undefined_action');
	}
} elseif ($action == 'postboard') {
	InitGP(array('uid','title'),'P');
	$uid = (int)$uid;
	if (!$uid) Showmsg('undefined_action');
	if ($uid == $winduid) Showmsg('mode_o_board_self');
	if (strlen($title)>3 && strlen($title)>200) Showmsg('mode_o_board_too_lang');
	$tousername = $db->get_value("SELECT username FROM pw_members WHERE uid=".pwEscape($uid));
	if (!$tousername) Showmsg('undefined_action');
	if (@include(D_P."data/bbscache/wordsfb.php")) {
		foreach ($wordsfb as $key => $value) {
			$banword = (string) stripslashes($key);
			if (strpos($title,$banword)!==false) {
				Showmsg('title_wordsfb');
			}
		}
		foreach ($replace as $key => $value) {
			$banword = (string) stripslashes($key);
			if (strpos($title,$banword)!==false) {
				Showmsg('post_wordsfb');
			}
		}
	}
	$data = array(
		'uid' 		=> $winduid,
		'username'	=> $windid,
		'touid'		=> $uid,
		'title'		=> $title,
		'postdate'	=> $timestamp
	);
	$db->update("INSERT INTO pw_oboard SET ".pwSqlSingle($data));
	$thisid = $db->insert_id();
	if ($thisid) {
		require_once(R_P.'require/msg.php');
		$msg = array(
			'toUid'		=> $uid,
			'subject'	=> 'o_board_success_title',
			'content'	=> 'o_board_success_cotent',
			'other'		=> array(
				'formuid' 	=> $winduid,
				'formname'	=> $windid,
				'touid'		=> $uid
			)
		);
		pwSendMsg($msg);

		pwAddFeed($winduid,'board','',array(
							'lang'		=>'post_board',
							'uid'		=>$winduid,
							'touid'		=> $uid,
							'tousername'=>$tousername
							)
		);
		require_once(R_P.'require/showimg.php');
		list($myface) = showfacedesign($winddb['icon'],1);
		echo "success\t".$thisid."\t".$myface;
		ajax_footer();
	} else {
		Showmsg('undefined_action');
	}
} elseif ($action == 'delboard') {
	InitGP(array('id'),'P',2);
	if (!$id) Showmsg('undefined_action');
	if (!$isGM) {
		$board = $db->get_one("SELECT * FROM pw_oboard WHERE id=".pwEscape($id));
		if (!$board || ($board['uid'] != $winduid && $board['touid'] != $winduid)) Showmsg('undefined_action');
	}
	$db->update("DELETE FROM pw_oboard WHERE id=".pwEscape($id));
	delComment('board',$id);
	echo "success";
	ajax_footer();
} elseif ($action == 'showftlist') {
	InitGP(array('u'),'P',2);
	if (!$u) Showmsg('undefined_action');
	if ($u != $winduid) Showmsg('undefined_action');
	$query = $db->query("SELECT * FROM pw_friendtype WHERE uid=".pwEscape($u)." ORDER BY ftid");
	$types = array();
	while ($rt = $db->fetch_array($query)) {
		$types[] = $rt;
	}
	if (count($types)) {
		$str = pwJsonEncode($types);
	} else {
		$str = '';
	}
	echo "success\t$str";
	ajax_footer();
} elseif ($action == 'setfriendtype') {
	InitGP(array('friendid','ftid'),'P',2);
	!$ftid && $ftid = 0;
	if (!$friendid) Showmsg('undefined_action');
	$db->update("UPDATE pw_friends SET ftid=".pwEscape($ftid)." WHERE uid=".pwEscape($winduid)." AND friendid=".pwEscape($friendid));
	echo "success";
	ajax_footer();
}
?>