<?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));
	if ($affected_rows = delAppAction('share',$id)) {
		countPosts("-$affected_rows");
	}

	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=".$winduid;
		$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);
		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');
	require_once(R_P.'require/showimg.php');
	$comment = array();
	$query = $db->query("SELECT * FROM pw_comment WHERE type=".pwEscape($type)." AND typeid=".pwEscape($id)." ORDER BY id".pwLimit(0,100));
	//$query = $db->query("SELECT c.*,m.icon as face FROM pw_comment c LEFT JOIN pw_members m USING(uid) WHERE c.type=".pwEscape($type)." AND c.typeid=".pwEscape($id)." ORDER BY c.id".pwLimit(0,100));
	while ($rt = $db->fetch_array($query)) {
		$rt['postdate'] = get_date($rt['postdate']);
		//list($rt['face']) = showfacedesign($rt['face'],1,'s');
		$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")) {
		if ($wordsfb) {
			foreach ($wordsfb as $key => $value) {
				$banword = (string) stripslashes($key);
				if (strpos($title,$banword)!==false) {
					Showmsg('title_wordsfb');
				}
			}
		}
		if ($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) {
		countPosts('+1');
		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']));
	}
	countPosts("-$updatenum");
	echo "success\t";
	ajax_footer();
} elseif ($action == 'addfriendtype') {
	InitGP(array('u','name'),'P');
	$u = (int) $u;
	if (!$u) Showmsg('undefined_action');
	if ($u != $winduid && !$isGM) 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 (!$isGM) {
		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 (!$isGM) {
		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 (!isFriend($uid,$winduid)) Showmsg('mode_o_board_not_friend');
	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")) {
		if ($wordsfb) {
			foreach ($wordsfb as $key => $value) {
				$banword = (string) stripslashes($key);
				if (strpos($title,$banword)!==false) {
					Showmsg('title_wordsfb');
				}
			}
		}
		if ($replace) {
			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
							)
		);
		countPosts('+1');
		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));
	$affected_rows = delAppAction('board',$id)+1;
	countPosts("-$affected_rows");
	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();
} elseif ($action == 'adddiarytype') {
	InitGP(array('u','name','b'),'P');
	if ((int)$b == 1) {
		echo "success\t$b";
		ajax_footer();
	}
	$u = (int) $u;
	if (!$u) Showmsg('undefined_action');
	if ($u != $winduid && !$isGM) Showmsg('undefined_action');

	if (strlen($name)<1 || strlen($name)>20) Showmsg('mode_o_adddtype_name_leng');
	$check = $db->get_one("SELECT dtid FROM pw_diarytype WHERE uid=".pwEscape($u)." AND name=".pwEscape($name));
	if ($check) Showmsg('mode_o_adddtype_name_exist');
	$count = $db->get_value("SELECT COUNT(*) AS count FROM pw_diarytype WHERE uid=".pwEscape($u));
	if ($count>20) Showmsg('mode_o_adddtype_length');
	$db->update("INSERT INTO pw_diarytype(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 == 'deldiarytype') {
	InitGP(array('u','dtid'),'P',2);
	$where = '';
	if (!$isGM) {
		if (!$u) Showmsg('undefined_action');
		if ($u != $winduid) Showmsg('undefined_action');
		$where .= " AND uid=".pwEscape($u);
	}

	if (!$dtid) Showmsg('undefined_action');
	$db->update("DELETE FROM pw_diarytype WHERE dtid=".pwEscape($dtid)."$where");
	if ($db->affected_rows()) {
		$db->update("UPDATE pw_diary SET dtid=0 WHERE dtid=".pwEscape($dtid));
		echo "success";
		ajax_footer();
	} else {
		Showmsg('undefined_action');
	}
} elseif ($action == 'eidtdiarytype') {
	InitGP(array('u','dtid','name'),'P');
	$u = (int) $u;
	$dtid = (int) $dtid;
	if (!$u) Showmsg('undefined_action');
	if (!$isGM) {
		if ($u != $winduid) Showmsg('undefined_action');
	}
	if (!$dtid) Showmsg('undefined_action');
	if (strlen($name)<1 || strlen($name)>20) Showmsg('mode_o_adddtype_name_leng');
	$check = $db->get_one("SELECT dtid FROM pw_diarytype WHERE uid=".pwEscape($u)." AND name=".pwEscape($name));
	if ($check) Showmsg('mode_o_adddtype_name_exist');
	$db->update("UPDATE pw_diarytype SET name=".pwEscape($name)." WHERE uid=".pwEscape($u)." AND dtid=".pwEscape($dtid));
	if ($db->affected_rows()) {
		echo "success";
		ajax_footer();
	} else {
		Showmsg('undefined_action');
	}
} elseif ($action == 'deldiary') {
	InitGP(array('id'),'',2);
	if (!$id) Showmsg('undefined_action');
	$diary = $db->get_one("SELECT did,dtid,uid FROM pw_diary WHERE did=".pwEscape($id));
	!$diary && Showmsg('mode_o_no_diary');

	if ($winduid != $diary['uid'] && !$isGM) {
		Showmsg('mode_o_deldiary_permit_err');
	}
	$db->update("DELETE FROM pw_diary WHERE did=".pwEscape($id));
	$db->update("UPDATE pw_diarytype SET num=num-1 WHERE dtid=".pwEscape($diary['dtid']));
	if ($affected_rows = delAppAction('diary',$id)) {
		countPosts("-$affected_rows");
	}
	echo "success\t";ajax_footer();
} elseif ($action == 'copydiary') {
	InitGP(array('id'),'',2);
	if (!$id) Showmsg('undefined_action');
	$diary = $db->get_one("SELECT did,uid,dtid,privacy,subject,content,ifconvert,ifwordsfb,ifcopy FROM pw_diary WHERE did=".pwEscape($id));

	!$diary['ifcopy'] && Showmsg('mode_o_copy_permit_err');
	$diary['copyurl'] = "{$GLOBALS[db_bbsurl]}/mode.php?m=o&q=diary&u={$diary[uid]}&did={$diary[did]}";

	/*$rt = $db->get_one("SELECT name FROM pw_diarytype WHERE dtid=".pwEscape($diary['dtid'])." AND uid=".pwEscape($diary['uid']));
	if ($rt['name']) {
		$check = $db->get_one("SELECT dtid FROM pw_diarytype WHERE uid=".pwEscape($winduid)." AND name=".pwEscape($rt['name']));
		$count = $db->get_value("SELECT COUNT(*) AS count FROM pw_diarytype WHERE uid=".pwEscape($winduid));
		if (!$check && $count <= 20) {
			$db->update("INSERT INTO pw_diarytype(uid,name,num) VALUES(".pwEscape($winduid).",".pwEscape($rt['name']).",1)");
			$dtid = $db->insert_id();
		} elseif ($count > 20) {
			$dtid = 0;
		} else {
			$dtid = $check['dtid'];
			$db->update("UPDATE pw_diarytype SET num=num+1 WHERE dtid=".pwEscape($dtid));
		}
	}*///分類不存在則自動生成分類


	$dtid = 0;
	$pwSQL = pwSqlSingle(array(
		'uid'		=> $winduid,
		'dtid'		=> $dtid,
		'username'	=> $windid,
		'privacy'	=> $diary['privacy'],
		'subject'	=> $diary['subject'],
		'content'	=> $diary['content'],
		'copyurl'	=> $diary['copyurl'],
		'ifcopy'	=> $diary['ifcopy'],
		'ifconvert'	=> $diary['ifconvert'],
		'ifwordsfb'	=> $diary['ifwordsfb'],
		'postdate'	=> $timestamp,
	));


	$db->update("INSERT INTO pw_diary SET $pwSQL");
	$did = $db->insert_id();

	pwAddFeed($winduid,'diary',$did,array(
							'lang'=>'diary_copy',
							'username'=>$windid,
							'uid'=>$winduid,
							'did'=>$did,
							'subject'=>$diary['subject'],)
		);
	countPosts('+1');
	echo "success";ajax_footer();
}
?>