<?php
define('AJAX','1');
require_once('global.php');

InitGP(array('action'));

if (!$windid && !in_array($action,array('login','showface','showsmile','getverify'))) {
	Showmsg('not_login');
}

if ($action == 'leaveword') {

	!$_G['leaveword'] && Showmsg('leaveword_right');

	if (empty($_POST['step'])) {

		InitGP(array('pid'));
		$tpc = $db->get_one('SELECT authorid,ptable FROM pw_threads WHERE tid='.pwEscape($tid));
		if ($tpc['authorid'] != $winduid) {
			Showmsg('leaveword_error');
		}
		$pw_posts = GetPtable($tpc['ptable']);
		$rt = $db->get_one("SELECT leaveword FROM $pw_posts WHERE pid=".pwEscape($pid).' AND tid='.pwEscape($tid));
		$reason_sel = '';
		$reason_a = explode("\n",$db_adminreason);
		foreach ($reason_a as $k => $v) {
			if ($v = trim($v)) {
				$reason_sel .= "<option value=\"$v\">$v</option>";
			} else {
				$reason_sel .= "<option value=\"\">-------</option>";
			}
		}
		$rt['leaveword'] = str_replace('&nbsp;',' ',$rt['leaveword']);
		require_once PrintEot('ajax');ajax_footer();

	} else {

		PostCheck();
		InitGP(array('pid','atc_content','ifmsg'),'P');
		$tpc = $db->get_one("SELECT t.authorid,t.ptable,f.forumadmin,f.fupadmin FROM pw_threads t LEFT JOIN pw_forums f USING(fid) WHERE t.tid=".pwEscape($tid));
		if ($tpc['authorid'] != $winduid && !CkInArray($windid,$manager) && !admincheck($tpc['forumadmin'],$tpc['fupadmin'],$windid)) {
			Showmsg('leaveword_error');
		}
		require_once(R_P.'require/bbscode.php');
		$atc_content = str_replace('&#61;','=',$atc_content);
		$ptable   = $tpc['ptable'];
		$content  = convert($atc_content,$db_windpost);
		$sqladd   = $atc_content == $content ? '' : ",ifconvert='2'";
		$pw_posts = GetPtable($ptable);
		if ($ifmsg && !empty($atc_content)) {
			require_once(R_P.'require/msg.php');
			include_once(D_P.'data/bbscache/forum_cache.php');
			$atc = $db->get_one("SELECT author,fid,subject,content,postdate FROM $pw_posts WHERE pid=".pwEscape($pid).' AND tid='.pwEscape($tid));
			!$atc['subject'] && $atc['subject'] = substrs($atc['content'],35);
			$msg = array(
				'toUser'	=> $atc['author'],
				'subject'	=> 'leaveword_title',
				'content'	=> 'leaveword_content',
				'other'		=> array(
					'fid'		=> $atc['fid'],
					'tid'		=> $tid,
					'author'	=> $windid,
					'subject'	=> $atc['subject'],
					'postdate'	=> get_date($atc['postdate']),
					'forum'		=> strip_tags($forum[$atc['fid']]['name']),
					'affect'    => '',
					'admindate'	=> get_date($timestamp),
					'reason'	=> stripslashes($atc_content)
				)
			);
			pwSendMsg($msg);
		}
		if(@include(D_P."data/bbscache/wordsfb.php")){
	        foreach ($wordsfb as $key => $value) {
	            $banword = (string) stripslashes($key);
	            if (strpos($atc_content,$banword)!==false) {
	                Showmsg('content_wordsfb');

	            }
	        }
	        foreach ($replace as $key => $value) {
	            $banword = (string) stripslashes($key);
	            if (strpos($atc_content,$banword)!==false) {
	                Showmsg('content_wordsfb');
	            }
	        }
        }
		$db->update("UPDATE $pw_posts SET leaveword=".pwEscape($atc_content)." $sqladd WHERE pid=".pwEscape($pid).' AND tid='.pwEscape($tid));
		echo "success\t".str_replace(array("\n","\t"),array('<br />',''),stripslashes($content));
		ajax_footer();
	}
} elseif ($action == 'favor') {

	PostCheck();
	$rs = $db->get_one('SELECT tids,type FROM pw_favors WHERE uid='.pwEscape($winduid));
	if ($rs) {
		$count = 0;
		$tiddb = getfavor($rs['tids']);
		foreach ($tiddb as $key => $t) {
			if (is_array($t)) {
				if (CkInArray($tid,$t)) {
					Showmsg('job_favor_error');
				}
				$count += count($t);
			} else {
				unset($tiddb[$key]);
			}
		}
		$count > $_G['maxfavor'] && Showmsg('job_favor_full');

		InitGP(array('type'),2);
		$typeid = array('0'=>'default');
		if ($rs['type']) {
			$typeid = array_merge($typeid,explode(',',$rs['type']));
			if (!isset($type)) {
				require_once PrintEot('ajax');ajax_footer();
			}
		} else {
			$type = 0;
		}
		!isset($typeid[$type]) && Showmsg('data_error');
		$read = $db->get_one('SELECT subject FROM pw_threads WHERE tid='.pwEscape($tid));
		!$read && Showmsg('data_error');
		require_once(R_P.'require/posthost.php');
		PostHost("http://push.phpwind.net/push.php?type=collect&url=".rawurlencode("$db_bbsurl/read.php?tid=$tid")."&tocharset=$db_charset&title=".rawurlencode($read['subject'])."&bbsname=".rawurlencode($db_bbsname),"");
		$tiddb[$type][] = $tid;
		$newtids = makefavor($tiddb);
		$db->update("UPDATE pw_favors SET tids=".pwEscape($newtids).' WHERE uid='.pwEscape($winddb['uid']));
	} else {
		$db->update("INSERT INTO pw_favors SET ".pwSqlSingle(array('uid'=>$winddb['uid'],'tids'=>$tid)));
	}
	Showmsg('job_favor_success');

} elseif ($action == 'tag') {

	$cachetime = pwFilemtime(D_P."data/bbscache/tagdb.php");
	if (!file_exists(D_P."data/bbscache/tagdb.php") || $timestamp-$cachetime>3600) {
		$tagnum=max($db_tagindex,200);
		$tagdb = array();
		$query = $db->query("SELECT * FROM pw_tags WHERE ifhot='0' ORDER BY num DESC".pwLimit($tagnum));
		while ($rs = $db->fetch_array($query)) {
			$tagdb[$rs['tagname']] = $rs['num'];
		}
		writeover(D_P."data/bbscache/tagdb.php","<?php\r\n\$tagdb=".pw_var_export($tagdb).";\r\n?>");
	} else {
		include_once(D_P."data/bbscache/tagdb.php");
	}
	foreach ($tagdb as $key => $num) {
		echo $key.','.$num."\t";
	}
	ajax_footer();

} elseif ($action == 'relatetag') {

	InitGP(array('tagname'));
	$rs = $db->get_one("SELECT tagid,num FROM pw_tags WHERE tagname=".pwEscape($tagname));
	if (!$rs || $rs['num']<1) {
		Showmsg('tag_limit');
	}
	$query = $db->query("SELECT tg.tid,t.subject FROM pw_tagdata tg LEFT JOIN pw_threads t USING(tid) WHERE tg.tagid=".pwEscape($rs['tagid'])." LIMIT 5");
	$readdb = array();
	while ($rt = $db->fetch_array($query)) {
		$rt['subject'] = substrs($rt['subject'],65);
		$readdb[] = $rt;
	}
	require_once PrintEot('ajax');ajax_footer();

} elseif ($action == 'deldownfile') {

	PostCheck();
	InitGP(array('aid','pid','page'));
	(!$tid || !is_numeric($aid)) && Showmsg('job_attach_error');
	if (is_numeric($pid)) {
		$table = $pw_posts = GetPtable('N',$tid);
		$where = 'pid='.pwEscape($pid);
		$post  = $db->get_one("SELECT fid,tid,aid AS oldaid,authorid FROM $pw_posts WHERE pid=".pwEscape($pid));
	} else {
		$table = $pw_tmsgs = GetTtable($tid);
		$where = 'tid='.pwEscape($tid);
		$post  = $db->get_one("SELECT t.tid,t.fid,t.authorid,t.ptable,tm.aid AS oldaid FROM pw_threads t LEFT JOIN $pw_tmsgs tm USING(tid) WHERE t.tid=".pwEscape($tid));
		$pw_posts = GetPtable($post['ptable']);
	}
	$tid    = $post['tid'];
	$fid    = $post['fid'];
	$attach = unserialize(stripslashes($post['oldaid']));
	$attachurl = '';
	!$attach[$aid] && Showmsg('job_attach_error');
	@extract($attach[$aid]);
	if (!$attachurl || strpos($attachurl,'..') !== false) {
		Showmsg('job_attach_error');
	}
	require_once(R_P.'require/forum.php');
	require_once(R_P.'require/updateforum.php');

	$foruminfo = $db->get_one("SELECT name,f_type,style,password,allowvisit,forumadmin,fupadmin,allowhtm,cms FROM pw_forums WHERE fid=".pwEscape($fid));
	!$foruminfo && Showmsg('data_error');
	wind_forumcheck($foruminfo);
	/*
	*  获取管理权限
	*/
	$isGM = CkInArray($windid,$manager);
	$isBM = admincheck($foruminfo['forumadmin'],$foruminfo['fupadmin'],$windid);
	if ($isGM || pwRights($isBM,'delattach')) {
		$admincheck = 1;
	} else {
		$admincheck = 0;
	}
	if ($groupid != 'guest' && ($admincheck || $post['authorid'] == $winduid)) {
		pwDelatt($attachurl,$db_ifftp);
		pwFtpClose($ftp);
		$attach = unserialize(stripslashes($post['oldaid']));
		unset($attach[$aid]);
		if ($attach) {
			$attach = addslashes(serialize($attach));
		} else {
			$attach = '';
		}
		$db->update("UPDATE $table SET aid=".pwEscape($attach)." WHERE $where");
		$db->update("DELETE FROM pw_attachs WHERE aid=".pwEscape($aid));

		$ifupload=getattachtype($tid);
		$db->update('UPDATE pw_threads SET ifupload='.pwEscape($ifupload).' WHERE tid='.pwEscape($tid));
		if ($foruminfo['allowhtm'] && $page==1) {
			require_once(R_P.'require/template.php');
		}
		echo 'success';ajax_footer();
	} else {
		Showmsg('job_attach_right');
	}
} elseif ($action == 'draft') {

	!$_G['maxgraft'] && Showmsg('draft_right');

	if (empty($_POST['step'])) {

		$db_showperpage = 5;
		InitGP(array('page'),'GP',2);
		$page<1 && $page = 1;
		$rt = $db->get_one("SELECT COUNT(*) AS sum FROM pw_draft WHERE uid=".pwEscape($winduid));
		$maxpage = ceil($rt['sum']/$db_showperpage);
		$maxpage && $page > $maxpage && $page = $maxpage;
		$limit = pwLimit(($page-1)*$db_showperpage,$db_showperpage);

		$query = $db->query("SELECT * FROM pw_draft WHERE uid=".pwEscape($winduid).$limit);
		if ($db->num_rows($query) == 0) {
			Showmsg('draft_error');
		}
		$drdb = array();
		while ($rt = $db->fetch_array($query)) {
			$drdb[] = $rt;
		}
		require_once PrintEot('ajax');ajax_footer();

	} elseif ($_POST['step'] == 2) {

		PostCheck();
		InitGP(array('atc_content'),'P');
		!$atc_content && Showmsg('content_empty');
		$atc_content = str_replace('%26','&',$atc_content);
		$rt = $db->get_one("SELECT COUNT(*) AS sum FROM pw_draft WHERE uid=".pwEscape($winduid));
		if ($rt['sum']>=$_G['maxgraft']) {
			Showmsg('draft_full');
		}
		$db->update("INSERT INTO pw_draft SET ".pwSqlSingle(array('uid'=>$winduid,'content'=>$atc_content)));
		Showmsg('save_success');

	} elseif ($_POST['step'] == 3) {

		PostCheck();
		InitGP(array('atc_content','did'),'P');
		!$atc_content && Showmsg('content_empty');
		$db->update('UPDATE pw_draft SET content='.pwEscape($atc_content).' WHERE uid='.pwEscape($winduid).' AND did='.pwEscape($did));
		Showmsg('update_success');

	} else {

		PostCheck();
		InitGP(array('did'));
		$db->update('DELETE FROM pw_draft WHERE uid='.pwEscape($winduid).' AND did='.pwEscape($did));
		Showmsg('delete_success');
	}
	
} elseif ($action == 'msg') {

	$_G['allowmessege'] == 0 && Showmsg('msg_group_right');
	if ($_G['postpertime'] || $_G['maxsendmsg']) {
		$rp = $db->get_one("SELECT COUNT(*) AS tdmsg,MAX(mdate) AS lastwrite FROM pw_msg WHERE fromuid=".pwEscape($winduid)." AND mdate>".pwEscape($tdtime));
		if ($_G['postpertime'] && $timestamp - $rp['lastwrite'] <= $_G['postpertime']) {
			Showmsg('msg_limit');
		} elseif ($_G['maxsendmsg'] && $rp['tdmsg']>=$_G['maxsendmsg']) {
			Showmsg('msg_num_limit');
		}
	}
	list(,,,$msgq)	= explode("\t",$db_qcheck);

	if (empty($_POST['step'])) {
		
		InitGP(array('touid','remid','transmit'));
		if (is_numeric($remid)) {
			$reinfo = $db->get_one('SELECT m.fromuid,m.touid,m.username,m.type,mc.title,mc.content FROM pw_msg m LEFT JOIN pw_msgc mc USING(mid) WHERE m.mid='.pwEscape($remid)." AND m.type='rebox' AND m.touid=".pwEscape($winduid));
			if ($reinfo) {
				$subject = strpos($reinfo['title'],'Re:')===false ? 'Re:'.$reinfo['title'] : $reinfo['title'];
				$atc_content = "[quote]".trim(substrs(preg_replace("/\[quote\](.+?)\[\/quote\]/is", '', $reinfo['content']),100))."[/quote]\n\n";
			}
		} elseif (is_numeric($transmit)) {
			$reinfo = $db->get_one("SELECT m.touid,m.fromuid,mc.title,mc.content FROM pw_msg m LEFT JOIN pw_msgc mc ON m.mid=mc.mid WHERE m.mid=" . pwEscape($transmit));
			if ($winduid == $reinfo['touid'] || $winduid == $reinfo['fromuid']) {
				$subject = $reinfo['title'];
				$atc_content = $reinfo['content'];
			}
		} else {
			$reinfo = $db->get_one("SELECT uid,username FROM pw_members WHERE uid=".pwEscape($touid));
		}
		require_once PrintEot('ajax');ajax_footer();

	} else {

		PostCheck(1,$db_gdcheck & 8);
		InitGP(array('msg_title','pwuser','ifsave'),'P');
		InitGP(array('atc_content'),'P',0);
		$atc_content = trim(Char_cv($atc_content));
		if (!$atc_content || !$msg_title || !$pwuser) {
			Showmsg('msg_empty');
		} elseif (strlen($msg_title)>75 || strlen($atc_content)>1500) {
			Showmsg('msg_subject_limit');
		}
		if (@include(D_P."data/bbscache/wordsfb.php")) {
			foreach ($wordsfb as $key => $value) {
				$banword = (string) stripslashes($key);
				if (strpos($msg_title,$banword)!==false) {
					Showmsg('title_wordsfb');
				} elseif (strpos($atc_content,$banword)!==false) {
					Showmsg('content_wordsfb');
				}
			}
			foreach ($replace as $key => $value) {
				$banword = (string) stripslashes($key);
				if (strpos($msg_title,$banword)!==false) {
					Showmsg('post_wordsfb');
				}
			}
		}
		$msgq && Qcheck($_POST['qanswer'],$_POST['qkey']);

		$rt = $db->get_one("SELECT uid,banpm,msggroups FROM pw_members WHERE username=".pwEscape($pwuser));
		if (!$rt) {
			$errorname = $pwuser;
			Showmsg('user_not_exists');
		}
		if ($rt['msggroups'] && strpos($rt['msggroups'],",$groupid,") !== false || strpos(",$rt[banpm],",",$windid,") !== false) {
			$errorname = $pwuser;
			Showmsg('msg_refuse');
		}
		require_once(R_P.'require/msg.php');
		$msg = array(
			'toUser'	=> $pwuser,
			'fromUid'	=> $winduid,
			'fromUser'	=> $windid,
			'subject'	=> $msg_title,
			'content'	=> stripslashes($atc_content)
		);
		if ($ifsave) {
			$db->update('INSERT INTO pw_msg SET '.pwSqlSingle(array(
				'touid'		=> $rt['uid'],
				'fromuid'	=> $winduid,
				'username'	=> $pwuser,
				'type'		=> 'sebox',
				'ifnew'		=> 0,
				'mdate'		=> $timestamp
			),false));
			$mid = $db->insert_id();
			$db->update('INSERT INTO pw_msgc SET '.pwSqlSingle(array(
				'mid'		=> $mid,
				'title'		=> $msg_title,
				'content'	=> stripslashes($atc_content)
			),false));
		}
		pwSendMsg($msg);
		Showmsg('send_success');
	}
} elseif ($action == 'usetool') {

	$tooldb = array();
	$i = $j = 0;
	$query  = $db->query("SELECT t.id,t.name,t.filename,t.descrip,u.nums FROM pw_tools t LEFT JOIN pw_usertool u ON t.id=u.toolid  AND u.uid=".pwEscape($winduid)." WHERE t.state='1' AND t.type='1' ORDER BY vieworder");
	while ($rt = $db->fetch_array($query)) {
		$rt['nums']=(int)$rt['nums'];
		$tooldb[$i][$j] = $rt;
		$j++;
		if ($j>1) {
			$i++;$j=0;
		}
	}
	require_once PrintEot('ajax');ajax_footer();

} elseif ($action == 'usertool') {
	//道具帖子类
	InitGP(array('uid'),'GP',2);
	!$uid && Showmsg('undefined_action');
	$i = $j = 0;
	$query  = $db->query("SELECT t.id,t.name,t.filename,t.descrip,u.nums FROM pw_tools t LEFT JOIN pw_usertool u ON t.id=u.toolid  AND u.uid=".pwEscape($winduid)." WHERE state='1' AND type='2' ORDER BY vieworder");
	while ($rt = $db->fetch_array($query)) {
		$rt['nums']=(int)$rt['nums'];
		$tooldb[$i][$j] = $rt;
		$j++;
		if ($j>1) {
			$i++;$j=0;
		}
	}
	require_once PrintEot('ajax');ajax_footer();

} elseif ($action == 'dig') {

	PostCheck();
	!$_G['dig'] && Showmsg("dig_right");
	$read = $db->get_one("SELECT t.author,t.subject,t.dig,f.forumset FROM pw_threads t LEFT JOIN pw_forumsextra f USING(fid) WHERE tid=".pwEscape($tid));
	!$read && Showmsg('data_error');
	$forumset = unserialize($read['forumset']);
	!$forumset['dig'] && Showmsg('forum_dig_allow');
	$rt  = $db->get_one("SELECT uid,digtid FROM pw_memberinfo WHERE uid=".pwEscape($winduid));
	Add_S($rt);
	if (strpos(",$rt[digtid],",",$tid,") === false) {
		$read['dig']++;
		$db->update("UPDATE pw_threads SET dig=dig+1 WHERE tid=".pwEscape($tid));
		if ($rt) {
			strlen($rt['digtid'])>2000 && $rt['digtid'] = '';
			$rt['digtid'] .= ($rt['digtid'] ? ',' : '').$tid;
			$db->update("UPDATE pw_memberinfo SET digtid=".pwEscape($rt['digtid'])."WHERE uid=".pwEscape($winduid));
		} else {
			$db->update("INSERT INTO pw_memberinfo SET ".pwSqlSingle(array('uid'=>$winduid,'digtid'=>$tid)));
		}
		require_once(R_P.'require/posthost.php');
		PostHost("http://push.phpwind.net/push.php?type=dig&url=".rawurlencode("$db_bbsurl/read.php?tid=$tid")."&tocharset=$db_charset&title=".rawurlencode($read['subject'])."&bbsname=".rawurlencode($db_bbsname),"");
		Showmsg('dig_success');
	} else {
		Showmsg("dig_limit");
	}
} elseif ($action == 'extend') {

	InitGP(array('type'));
	if ($type == 'pwcode') {
		$code  = array();
		$query = $db->query("SELECT * FROM pw_windcode");
		while ($rt = $db->fetch_array($query)) {
			$rt['descrip'] = str_replace("\n","|",$rt['descrip']);
			$code[] = $rt;
		}
	} else {
		@include_once(D_P.'data/bbscache/setform.php');
		InitGP(array('id'),'GP',2);
		$setform = array();
		if (isset($setformdb[$id])) {
			$setform = $setformdb[$id];
		}
	}
	require_once PrintEot('ajax');ajax_footer();

} elseif ($action == 'sharelink') {

	!$db_ifselfshare && Showmsg("sharelink_colse");

	if (empty($_POST['step'])) {

		require_once PrintEot('ajax');ajax_footer();

	} else {

		PostCheck();
		InitGP(array('linkname','linkurl','linkdescrip','linklogo'),'P');
		(!$linkname || !$linkurl) && Showmsg('sharelink_link_empty');
		!$linkdescrip && $linkdescrip = '';
		!$linklogo && $linklogo = '';
		$linkurl = strtolower($linkurl);
		strncmp($linkurl,'http://',7) != 0 && Showmsg('sharelink_link_error');
		$rs = $db->get_one("SELECT sid FROM pw_sharelinks WHERE username=".pwEscape($windid));
		$rs && Showmsg('sharelink_apply_limit');
		$pwSQL = pwSqlSingle(array(
			'name'		=> $linkname,
			'url'		=> $linkurl,
			'descrip'	=> $linkdescrip,
			'logo'		=> $linklogo,
			'ifcheck'	=> 0,
			'username'	=> $windid
		));
		$db->update("INSERT INTO pw_sharelinks SET $pwSQL");

		require_once(R_P.'require/msg.php');
		$message = array(
			'toUser'	=> $manager,
			'subject'	=> 'sharelink_apply_title',
			'content'	=> 'sharelink_apply_content',
			'other'		=> array(
				'username'		=> $windid,
				'time'			=> get_date($timestamp)
			)
		);
		pwSendMsg($message);

		Showmsg("sharelink_success");
	}
} elseif ($action == 'showface') {

	InitGP(array('page'));
	(!is_numeric($page) || $page < 1) && $page=1;
	$pre_page	= $page-1;
	$next_page	= $page+1;
	$db_perpage = 20;
	$img = @opendir("$imgdir/face");
	$imagearray	= @readdir($img);
	$imgselect	= "<span onClick=\"showimage('$imgpath','none.gif')\" class=\"fl face\"><img src='$imgpath/face/none.gif' width=\"50\" height=\"50\"></span>";
	$num = 0;
	while ($imagearray = @readdir($img)) {
		if ($imagearray != "." && $imagearray != ".." && $imagearray != "" && $imagearray != "none.gif" && eregi("\.(gif|jpg|png|bmp)$",$imagearray)) {
			++$num;
			if ($num > ($page-1)*$db_perpage && $num < $page*$db_perpage) {
				$imgselect .= "<span onClick=\"showimage('$imgpath','$imagearray')\" class=\"fl face\"><img src='$imgpath/face/$imagearray' width=\"50\" height=\"50\"></span>";
			}
		}
	}
	@closedir($img);
	require_once PrintEot('ajax');ajax_footer();

} elseif ($action == 'newrp') {

	if ($db_replysitemail && getstatus($winddb['userstatus'],6)) {
		include_once(D_P.'data/bbscache/forum_cache.php');
		$rt = $db->get_one("SELECT replyinfo FROM pw_memberinfo WHERE uid=".pwEscape($winduid));
		$replyinfo = explode(',',substr($rt['replyinfo'],1,-1));
		$replyinfo = pwImplode($replyinfo);
		$replydb = array();
		$query = $db->query("SELECT tid,fid,subject,postdate,lastpost FROM pw_threads WHERE tid IN($replyinfo) LIMIT 20");
		if ($db->num_rows($query) == 0) {
			Showmsg('newrp_error');
		}
		while ($rt = $db->fetch_array($query)) {
			$rt['subject'] = substrs($rt['subject'],55);
			$rt['fname'] = $forum[$rt['fid']]['name'];
			$rt['lastpost'] = get_date($rt['lastpost'],'Y-m-d');
			$replydb[] = $rt;
		}
		require_once PrintEot('ajax');ajax_footer();
	} else {
		Showmsg('newrp_error');
	}
} elseif ($action == 'delnewrp') {

	PostCheck();
	!$tid && Showmsg('data_error');
	$rt = $db->get_one("SELECT replyinfo FROM pw_memberinfo WHERE uid=".pwEscape($winduid));
	$rt['replyinfo'] = addslashes(str_replace(",$tid,",',',$rt['replyinfo']));
	$rt['replyinfo'] == ',' && $rt['replyinfo'] = '';
	$db->update("UPDATE pw_memberinfo SET replyinfo='$rt[replyinfo]' WHERE uid=".pwEscape($winduid));
	$db->update("UPDATE pw_threads SET ifmail='0' WHERE tid=".pwEscape($tid));
	if (getstatus($winddb['userstatus'],6) && !$rt['replyinfo']) {
		$db->update("UPDATE pw_members SET userstatus=userstatus&~(1<<5) WHERE uid=".pwEscape($winduid));
	}
	Showmsg('operate_success');

} elseif ($action == 'addfriend') {

	PostCheck();
	InitGP(array('touid'),'GP',2);
	if ($touid == $winduid) {
		Showmsg('friend_selferror');
	}
	$friend = $db->get_one("SELECT uid,username,userstatus FROM pw_members WHERE uid=".pwEscape($touid));
	if (!$friend) {
		$errorname = '';
		Showmsg('user_not_exists');
	}
	$rs = $db->get_one("SELECT uid,status FROM pw_friends WHERE uid=".pwEscape($winduid)." AND friendid=".pwEscape($friend['uid']));
	if ($rs) {
		if ($rs['status'] == '1') {
			Showmsg('friend_status_check');
		} else {
			Showmsg('friend_already_exists');
		}
	}
	$friendcheck = getstatus($friend['userstatus'],3,3);

	if (empty($_POST['step'])) {

		if ($friendcheck == 2) {
			Showmsg('friend_not_add');
		} elseif (!$friendcheck) {
			$pwSQL1 = pwSqlSingle(array(
				'uid'		=> $winduid,
				'friendid'	=> $friend['uid'],
				'joindate'	=> $timestamp,
				'status'	=> 0
			));
			$db->update("INSERT INTO pw_friends	SET $pwSQL1");
			$db->update("UPDATE pw_memberdata SET f_num=f_num+1 WHERE uid=".pwEscape($winduid));
			$pwSQL2 = pwSqlSingle(array(
				'uid'		=> $friend['uid'],
				'friendid'	=> $winduid,
				'joindate'	=> $timestamp,
				'status'	=> 0
			));
			$db->update("INSERT INTO pw_friends	SET $pwSQL2");
			$db->update("UPDATE pw_memberdata SET f_num=f_num+1 WHERE uid=".pwEscape($friend['uid']));
			$msg = array(
				'toUser'	=> $friend['username'],
				'subject'	=> 'friend_add_title_1',
				'content'	=> 'friend_add_content_1',
				'other'		=> array(
					'uid'		=> $winduid,
					'username'	=> $windid
				)
			);
			require_once R_P.'require/msg.php';
			pwSendMsg($msg);
			Showmsg('friend_update_success');
		} else {
			require_once PrintEot('ajax');ajax_footer();
		}
	} elseif ($friendcheck == 1) {

		InitGP(array('checkmsg'),'P');
		if (strlen($checkmsg) > 255) {
			$_strmaxlen = 255;
			Showmsg('string_limit');
		}
		$pwSQL = pwSqlSingle(array(
			'uid'		=> $winduid,
			'friendid'	=> $friend['uid'],
			'status'	=> 1,
			'joindate'	=> $timestamp,
			'descrip'	=> $checkmsg
		));
		$db->update("INSERT INTO pw_friends	SET $pwSQL");
		$msg = array(
			'toUser'	=> $friend['username'],
			'subject'	=> 'friend_add_title_2',
			'content'	=> 'friend_add_content_2',
			'other'		=> array(
				'uid'		=> $winduid,
				'username'	=> $windid,
				'msg'		=> stripslashes($checkmsg)
			)
		);
		require_once R_P.'require/msg.php';
		pwSendMsg($msg);
		Showmsg('friend_add_check');
	} else {
		Showmsg('undefined_action');
	}
} elseif ($action == 'deletefriend') {
	PostCheck();
	InitGP(array('fuid'),'GP',2);
	$ckuser = $db->get_value("SELECT m.username FROM pw_friends f LEFT JOIN pw_members m ON f.uid=m.uid WHERE f.uid=".pwEscape($fuid)." AND f.friendid=".pwEscape($winduid));
	if ($ckuser) {
		$db->update('DELETE FROM pw_friends WHERE uid='.pwEscape($fuid)." AND friendid=".pwEscape($winduid));
		$db->update("UPDATE pw_memberdata SET f_num=f_num-1 WHERE uid=".pwEscape($fuid));
		$ckuser2 = $db->get_value("SELECT friendid FROM pw_friends WHERE uid=".pwEscape($winduid)." AND friendid=".pwEscape($fuid));
		if ($ckfuid2) {
			$db->update('DELETE FROM pw_friends WHERE uid='.pwEscape($winduid)." AND friendid=".pwEscape($fuid));
			$db->update("UPDATE pw_memberdata SET f_num=f_num-1 WHERE uid=".pwEscape($winduid));
		}
		$msg = array(
			'toUser'	=> $ckuser,
			'subject'	=> 'friend_delete_title',
			'content'	=> 'friend_delete_content',
			'other'		=> array(
				'uid'		=> $winduid,
				'username'	=> $windid
			)
		);
		require_once R_P.'require/msg.php';
		pwSendMsg($msg);
		Showmsg('friend_delete');
	} else {
		Showmsg('undefined_action');
	}

} elseif ($action == 'showsmile') {

	InitGP(array('subjectid','page','type'));
	$u         = "http://dm.phpwind.net/misc";
	$subjectid = (int)$subjectid;
	(!is_numeric($page) || $page<1) && $page = 1;
	$s         = '300.xml';
	if ($type == 'general') {
		$s = $subjectid ? $subjectid.'_'.$page.'.xml' : '300.xml';
	} elseif ($type == 'magic') {
		$s = $subjectid ? $subjectid.'_'.$page.'.xml' : '200.xml';
	}
	$cachefile = D_P."data/bbscache/myshow_{$s}";
	if (!file_exists($cachefile) || $timestamp - pwFilemtime($cachefile) > 43200) {
		$data = '';
		if ($subjectid) {
			$url = "$u/list/$s?$timestamp";
		} else {
			$url = "$u/menu/$s?$timestamp";
		}
		require_once(R_P.'require/posthost.php');
		$data = PostHost($url);
		if ($data && strpos($data,'<?xml')!==false) {
			writeover($cachefile,$data);
		}
	}
	header("Content-Type: text/xml; charset=UTF-8");
	$data = readover($cachefile);
	echo $data;
	exit;

} elseif ($action == 'honor') {

	!$_G['allowhonor'] && Showmsg('undefined_action');

	if (empty($_POST['step'])) {

		require_once PrintEot('ajax');ajax_footer();

	} else {

		PostCheck();
		InitGP(array('content'),'P');
		$content = str_replace("\n",'',$content);
		strlen($content) > 90 && $content = substrs($content,90);
		if ($winddb['honor'] != stripslashes($content)) {
			$db->update("UPDATE pw_members SET honor=".pwEscape($content)." WHERE uid=".pwEscape($winduid));
			if ($db_modes['o']['ifopen']) {
				$db->update("INSERT INTO pw_owritedata SET"
					. pwSQLSingle(array(
						'uid'		=> $winduid,
						'touid'		=> 0,
						'postdate'	=> $timestamp,
						'isshare'	=> 0,
						'source'	=> 'signature',
						'content'	=> $content
				)));
			}
			require_once(R_P.'require/postfunc.php');
			pwAddFeed($winduid,'honor','',array('honor' => $content));
		}
		echo "success\t".stripslashes($content);ajax_footer();
	}
} elseif ($action == 'readlog') {

	$readlog = explode(',',GetCookie('readlog'));
	@krsort($readlog);
	$tids = array();
	$i = 0;
	foreach ($readlog as $key=>$value) {
		if (is_numeric($value)) {
			$tids[] = $value;
			if (++$i>9) break;
		}
	}
	Cookie('readlog',','.implode(',',$tids).',');
	$tids && $tids = pwImplode($tids);
	!$tids && Showmsg('data_error');
	include_once(D_P.'data/bbscache/forum_cache.php');
	$readb = array();
	$query = $db->query("SELECT t.tid,t.fid,t.subject,t.author,t.authorid,t.anonymous,f.f_type,f.password,f.allowvisit FROM pw_threads t LEFT JOIN pw_forums f USING(fid) WHERE t.tid IN($tids)");
	while ($rt = $db->fetch_array($query)) {
		if (empty($rt['password']) && $rt['f_type']<>'hidden' && (empty($rt['allowvisit']) || allowcheck($rt['allowvisit'],$groupid,$winddb['groups']))) {
			if ($rt['anonymous'] && !in_array($groupid,array('3','4')) && $rt['authorid']<>$winduid) {
				$rt['author']	= $db_anonymousname;
				$rt['authorid'] = 0;
			}
			$readb[] = $rt;
		}
	}
	require_once PrintEot('ajax');ajax_footer();

} elseif ($action == 'threadlog') {

	$threadlog = explode(',',GetCookie('threadlog'));
	@krsort($threadlog);
	$fids = ',';
	$i = 0;
	foreach ($threadlog as $key=>$value) {
		if (is_numeric($value)) {
			$fids .= $value.',';
			if (++$i>9) break;
		}
	}
	Cookie('threadlog',$fids);
	include_once(D_P.'data/bbscache/forum_cache.php');
	$threaddb = array();
	foreach ($forum as $key => $value) {
		if (in_array($key,$threadlog)) {
			$threaddb[$key] = $value['name'];
		}
	}
	require_once PrintEot('ajax');ajax_footer();

} elseif ($action == 'mgmode') {

	if ($groupid <> 3 && $groupid <> 4) {
		echo 'false';ajax_footer();
	}
	$rightset = $db->get_value("SELECT value FROM pw_adminset WHERE gid=".pwEscape($groupid));
	require_once(R_P.'require/pw_func.php');
//	$rightset = P_unserialize($rightset);
	if (!$rightset || !(is_array($rightset = unserialize($rightset)))) {
		$rightset = array();
	}

	if ($rightset['setforum'] || $rightset['setstyles']) {
		require_once PrintEot('ajax');
	} else {
		echo 'false';
	}
	ajax_footer();

} elseif ($action == 'setstyle') {

	if ($groupid <> 3 && $groupid <> 4) {
		Showmsg('undefined_action');
	}
	$rightset = $db->get_value("SELECT value FROM pw_adminset WHERE gid=".pwEscape($groupid));
	require_once(R_P.'require/pw_func.php');
//	$rightset = P_unserialize($rightset);
	if (!$rightset || !(is_array($rightset = unserialize($rightset)))) {
		$rightset = array();
	}
	!$rightset['setstyles'] && Showmsg('undefined_action');

	if (empty($_POST['step'])) {

		if (file_exists(D_P."data/style/$skin.php") && strpos($skin,'..')===false) {
			@include Pcv(D_P."data/style/$skin.php");
		} elseif (file_exists(D_P."data/style/$db_defaultstyle.php") && strpos($db_defaultstyle,'..')===false) {
			@include Pcv(D_P."data/style/$db_defaultstyle.php");
			$skin = $db_defaultstyle;
		} else {
			@include(D_P."data/style/wind.php");
			$skin = 'wind';
		}
		require_once PrintEot('ajax');
		ajax_footer();

	} else {

		PostCheck();
		InitGP(array('set','setskin'));
		if (!is_array($set)) {
			Showmsg('undefined_action');
		}
		foreach ($set as $key => $value) {
			if (in_array($key,array('tablewidth','mtablewidth'))) {
				if (!preg_match('/(%|px|em)/i',$value)) {
					$set[$key] .= 'px';
				}
			}
		}
		$pwSQL = pwSqlSingle(array(
			'bgcolor'		=> $set['bgcolor'],		'linkcolor'		=>$set['linkcolor'],
			'tablecolor'	=> $set['tablecolor'],	'tdcolor'		=> $set['tdcolor'],
			'tablewidth'	=> $set['tablewidth'],	'mtablewidth'	=> $set['mtablewidth'],
			'headcolor'		=> $set['headcolor'],	'headborder'	=> $set['headborder'],
			'headfontone'	=> $set['headfontone'],	'headfonttwo'	=> $set['headfonttwo'],
			'cbgcolor'		=> $set['cbgcolor'],	'cbgborder'		=> $set['cbgborder'],
			'cbgfont'		=> $set['cbgfont'],		'forumcolorone'	=> $set['forumcolorone'],
			'forumcolortwo'	=> $set['forumcolortwo']
		));
		$rs = $db->get_one("SELECT sid FROM pw_styles WHERE name=".pwEscape($setskin)." AND uid='0'");
		if ($rs) {
			$db->update("UPDATE pw_styles SET $pwSQL WHERE name=".pwEscape($setskin)." AND uid='0'");
		} else {
			$db->update("INSERT INTO pw_styles SET $pwSQL");
		}
		require_once(R_P.'admin/cache.php');
		updatecache_sy($setskin);

		Showmsg('operate_success');
	}

} elseif ($action == 'mforder') {

	PostCheck();
	if ($groupid <> 3 && $groupid <> 4) {
		Showmsg('undefined_action');
	}
	$rightset = $db->get_value("SELECT value FROM pw_adminset WHERE gid=".pwEscape($groupid));
	require_once(R_P.'require/pw_func.php');
//	$rightset = P_unserialize($rightset);
	if (!$rightset || !(is_array($rightset = unserialize($rightset)))) {
		$rightset = array();
	}

	if (!$rightset['setforum']) {
		Showmsg('undefined_action');
	}
	$vieworder = 0;
	foreach ($_POST['cate'] as $cid => $fids) {
		$fid_a = explode(',',$fids);
		foreach ($fid_a as $key => $fid) {
			$db->update("UPDATE pw_forums SET vieworder=".pwEscape($key)." WHERE fid=".pwEscape($fid));
		}
		$db->update("UPDATE pw_forums SET vieworder=".pwEscape($vieworder)." WHERE fid=".pwEscape($cid));
		++$vieworder;
	}
	require_once(R_P.'admin/cache.php');
	updatecache_f();

	Showmsg('operate_success');

} elseif ($action == 'mfsetname') {

	PostCheck();
	if ($groupid <> 3 && $groupid <> 4) {
		Showmsg('undefined_action');
	}
	$rightset = $db->get_value("SELECT value FROM pw_adminset WHERE gid=".pwEscape($groupid));
	require_once(R_P.'require/pw_func.php');
//	$rightset = P_unserialize($rightset);
	if (!$rightset || !(is_array($rightset = unserialize($rightset)))) {
		$rightset = array();
	}

	if (!$rightset['setforum']) {
		Showmsg('undefined_action');
	}
	InitGP(array('fids','desc'),'P',0);
	$ifcache = false;

	if (is_array($desc)) {
		foreach ($desc as $fid => $descrip) {
			$descrip = str_replace(array('&#60;','&#61;','<iframe'),array('<','=','&lt;iframe'),$descrip);
			strlen($descrip) > 250 && Showmsg('descrip_long');
			$db->update("UPDATE pw_forums SET descrip=".pwEscape($descrip)."WHERE fid=".pwEscape($fid));
		}
	}
	if (is_array($fids)) {
		foreach ($fids as $fid => $name) {
			$name = str_replace(array('&#60;','&#61;','<iframe'),array('<','=','&lt;iframe'),$name);
			$db->update("UPDATE pw_forums SET name=".pwEscape($name)."WHERE fid=".pwEscape($fid));
		}
		$ifcache = true;
	}
	if ($ifcache) {
		require_once(R_P.'admin/cache.php');
		updatecache_f();
	}
	Showmsg('operate_success');

} elseif ($action == 'shortcut') {

	!$fid && Showmsg('undefined_action');
	$myshortcut = explode(',',$winddb['shortcut']);
	foreach ($myshortcut as $key=>$value){
		if (!$value || !is_numeric($value)) {
			unset($myshortcut[$key]);
		}
	}
	if (in_array($fid,$myshortcut)) {
		foreach ($myshortcut as $key=>$value) {
			if (!$value || $value == $fid) {
				unset($myshortcut[$key]);
			}
		}
		$shortcut = ','.implode(',',$myshortcut).',';
		$db->update("UPDATE pw_members SET shortcut=".pwEscape($shortcut)." WHERE uid=".pwEscape($winduid));
		Showmsg("shortcutno");
	} else {
		count($myshortcut)>=6 && Showmsg('shortcut_numlimit');
		require_once(D_P.'data/bbscache/forum_cache.php');
		$forumkeys = array_keys($forum);
		!in_array($fid,$forumkeys) && Showmsg('undefined_action');
		$myshortcut[] = $fid;

		$shortcut = ','.implode(',',$myshortcut).',';
		$db->update("UPDATE pw_members SET shortcut=".pwEscape($shortcut)." WHERE uid=".pwEscape($winduid));
		Showmsg("shortcutok");
	}
} elseif ($action == 'pushto') {

	InitGP(array('fid','seltid'));
	@include_once Pcv(D_P.'data/bbscache/mode_push_config.php');
	$pushs = array();
	if ($groupid=='3' || $groupid=='4' || CkInArray($windid,$manager)) {
		$pushs = $PUSH;
	} elseif ($groupid=='5') {
		foreach ($PUSH as $key=>$value) {
			if (in_array($value['scr'],array('thread','cate'))) {
				$pushs[] = $value;
			}
		}
	}
	if (!$pushs) {
		Showmsg('no_aim_to_push');
	}
	require_once PrintEot('ajax');
	ajax_footer();

} elseif ($action == 'debate') {
	PostCheck();
	$do = GetGP('do');
	if ($do == 'vote') {
		$debate = $db->get_one("SELECT endtime,obvote,revote,judge FROM pw_debates WHERE tid=".pwEscape($tid));
		empty($debate) && Showmsg('data_error');
		if ($debate['judge'] > 0 || $debate['endtime'] < $timestamp) {
			Showmsg('debate_over');
		}
		$standpoint = $db->get_value("SELECT standpoint FROM pw_debatedata WHERE pid='0' AND tid=".pwEscape($tid)."AND authorid=".pwEscape($winduid));
		$standpoint = (int)$standpoint;
		if ($standpoint == 1) {
			Showmsg('debate_voted_Y');
		} elseif ($standpoint == 2) {
			Showmsg('debate_voted_N');
		}
		if (GetGP('q') == 'y') {
			$db->update("UPDATE pw_debates SET obvote=obvote+1");
			$debate['obvote']++;
			$standpoint = 1;
		} else {
			$db->update("UPDATE pw_debates SET revote=revote+1");
			$debate['revote']++;
			$standpoint = 2;
		}
		$db->update("INSERT INTO pw_debatedata SET".pwSqlSingle(array(
			'pid'			=> 0,
			'tid'			=> $tid,
			'authorid'		=> $winduid,
			'standpoint'	=> $standpoint,
			'postdate'		=> $timestamp,
			'vote'			=> 0,
			'voteids'		=> ''
		)));

		$tmpVotes = $debate['revote']+$debate['obvote'];
		$tmpob = round($debate['obvote']/$tmpVotes,2)*100;
		$tmpre = round($debate['revote']/$tmpVotes,2)*100;

		Showmsg('debate_success');

	} elseif ($do == 'judge') {

		if ($_POST['step']) {
			InitGP(array('judge','debater','umpirepoint'));
			strlen($umpirepoint) > 255 && Showmsg('debate_pointlen');
			$pwSQL['umpirepoint'] = $umpirepoint;
			$debate = $db->get_one("SELECT umpirepoint,debater,judge FROM pw_debates WHERE tid=".pwEscape($tid));
			if (empty($debate['judge'])) {
				$pwSQL['judge'] = ($judge==1 || $judge==3) ? $judge : 2;
			}
			if (empty($debate['debater'])) {
				$rt = $db->get_one("SELECT authorid FROM pw_members m LEFT JOIN pw_debatedata dd ON m.uid=dd.authorid WHERE m.username=".pwEscape($debater)."AND dd.tid=".pwEscape($tid));
				if ($rt) {
					$pwSQL['debater'] = $debater;
				}
			}
			$db->update("UPDATE pw_debates SET".pwSqlSingle($pwSQL));
			Showmsg('debate_judgesuccess');
		} else {
			$debate = $db->get_one("SELECT obvote,revote,obposts,reposts,umpirepoint,debater,judge FROM pw_debates WHERE tid=".pwEscape($tid));
			if (!$debate['debater']) {
				$debater = array();
				$query = $db->query("SELECT dd.authorid,dd.vote,m.username FROM pw_debatedata dd LEFT JOIN pw_members m ON dd.authorid=m.uid WHERE dd.tid=".pwEscape($tid)."ORDER BY dd.vote DESC LIMIT 10");
				while ($rt = $db->fetch_array($query)) {
					$debater[$rt['authorid']]['vote'] += $rt['vote'];
					$debater[$rt['authorid']]['username'] = $rt['username'];
				}
			}
			require_once PrintEot('ajax');ajax_footer();
		}
	} elseif ($do == 'agree') {

		$pid = (int)GetGP('pid');
		$debate = $db->get_one("SELECT endtime,judge FROM pw_debates WHERE tid=".pwEscape($tid));
		empty($debate) && Showmsg('data_error');
		if ($debate['judge'] > 0 || $debate['endtime'] < $timestamp) {
			Showmsg('debate_over');
		}
		$debate = $db->get_one("SELECT authorid,vote,voteids FROM pw_debatedata WHERE pid=".pwEscape($pid)."AND tid=".pwEscape($tid));
		empty($debate) && Showmsg('data_error');
		$debate['authorid'] == $winduid && Showmsg('debate_voteself');
		if (strpos($debate['voteids'],$winduid) !== false) {
			Showmsg('debate_voted');
		}
		$debate['voteids'] .= "$winduid,";
		$db->update("UPDATE pw_debatedata SET vote=vote+1,voteids=".pwEscape($debate['voteids'],false)."WHERE pid=".pwEscape($pid)."AND tid=".pwEscape($tid));
		$vote = $debate['vote']+1;
		Showmsg('debate_success');
	}
} elseif ($action == 'mutiatt') {

	$out = 'var att = {';
	$del = array();
	$query = $db->query("SELECT aid,name,size,attachurl,uploadtime FROM pw_attachs WHERE tid='0' AND pid='0' AND uid=" . pwEscape($winduid) . ' ORDER BY aid');
	while ($rt = $db->fetch_array($query)) {
		$rt['uploadtime'] = get_date($rt['uploadtime']);
		if (file_exists($attachpath.'/mutiupload/'.$rt['attachurl'])) {
			$out .= "'{$rt[aid]}' : ['$rt[name]', '$rt[size]', '$attachpath/mutiupload/$rt[attachurl]', '$rt[uploadtime]'],";
		} else {
			$del[] = $rt['aid'];
		}
	}
	if ($del) {
		$db->update("DELETE FROM pw_attachs WHERE aid IN(" . pwImplode($del) . ')');
	}
	$out = rtrim($out,',')."}";
	echo "ok\t$out";
	ajax_footer();

} elseif ($action == 'delmutiatt') {

	$aids  = array();
	$query = $db->query("SELECT aid,attachurl FROM pw_attachs WHERE tid='0' AND pid='0' AND uid=" . pwEscape($winduid));
	while ($rt = $db->fetch_array($query)) {
		if (file_exists($attachpath.'/mutiupload/'.$rt['attachurl'])) {
			P_unlink($attachpath.'/mutiupload/'.$rt['attachurl']);
		}
		$aids[] = $rt['aid'];
	}
	$db->update("DELETE FROM pw_attachs WHERE aid IN (" . pwImplode($aids).')');
	echo 'ok';ajax_footer();

} elseif ($action == 'msggoto') {

	InitGP(array('from','type'));
	InitGP(array('mid'), 'GP', 2);

	switch ($from) {
		case 'read':
			$sqlwhere = " AND type='rebox' AND touid=" . pwEscape($winduid) . "AND fromuid<>0";break;
		case 'readn':
			$sqlwhere = " AND type='rebox' AND touid=" . pwEscape($winduid) . "AND fromuid=0";break;
		case 'readsnd':
			$sqlwhere = " AND type='sebox' AND fromuid=" . pwEscape($winduid);break;
		case 'readscout':
			$sqlwhere = " AND type='rebox' AND fromuid=" . pwEscape($winduid);break;
		case 'readpub':
			$sqlwhere = " AND type='public' AND togroups LIKE " . pwEscape("%,$winddb[groupid],%");
			$delmsg = $db->get_value('SELECT delmsg FROM pw_memberinfo WHERE uid='.pwEscape($winduid));
			if ($delmsg) {
				$delmsg = explode(',', trim($delmsg,','));
				$sqlwhere .= " AND mid NOT IN(" . pwImplode($delmsg) . ')';
			}
			break;
		default:
			Showmsg('undefined_action');
	}
	if ($type == 'next') {
		$sql = "SELECT MAX(mid) AS mid FROM pw_msg WHERE mid<" . pwEscape($mid);
	} else {
		$sql = "SELECT MIN(mid) AS mid FROM pw_msg WHERE mid>" . pwEscape($mid);
	}
	$rt = $db->get_one($sql.$sqlwhere);
	if ($rt['mid']) {
		echo "ok\t$rt[mid]";
	} else {
		echo 'none';
	}
	ajax_footer();

} elseif ($action == 'playatt') {

	InitGP(array('aid'));

	$rt = $db->get_one("SELECT * FROM pw_attachs WHERE aid=" . pwEscape($aid));
	$a_url = geturl($rt['attachurl']);
	if (empty($a_url) || $rt['needrvrc'] > 0) {
		Showmsg('job_attach_error');
	}
	$width = 314;
	$ext = strtolower(substr(strrchr($rt['attachurl'],'.'),1));
	switch ($ext) {
		case 'mp3':
		case 'wma':	$height = 53;$type = 'wmv';break;
		case 'wmv':	$height = 256;$type = 'wmv';break;
		case 'swf': $height = 256;$type = 'flash';break;
		case 'rm' : $height = 256;$type = 'rm';break;
		default:Showmsg('undefined_action');
	}
	echo "ok\t$a_url[0]\t$width\t$height\t$type";ajax_footer();

} elseif ($action == 'getverify') {

	echo $verifyhash;ajax_footer();

} elseif ($action == 'changestate') {
	if (GetCookie('hideid') == '1') {
		Cookie('hideid','',0);
		echo "0";ajax_footer();
	} else {
		Cookie('hideid','1');
		echo "1";ajax_footer();
	}
} elseif ($action == 'changeeditor') {
	InitGP(array('editor'));
	if ($editor != getstatus($winddb['userstatus'],11)) {
		$ustatus = 'userstatus=userstatus'.($editor ? '|' : '&~') .'(1<<10)';
		$db->update("UPDATE pw_members SET $ustatus WHERE uid=".pwEscape($winduid));
	}
} elseif ($action == 'pwb_friend') {
	InitGP(array('u'),'P',2);
	if (!$u) Showmsg('undefined_action');
	if ($u != $winduid) Showmsg('undefined_action');
	$friends = getFriends($winduid,0,8,false,1);
	$str = '';
	if ($friends) {
		$friend_online = array();
		foreach ($friends as $key => $value) {
			if ($value['thisvisit']+$db_onlinetime*1.5>$timestamp) {
				$friend_online[] = array('uid'=>$value['uid'],'face'=>$value['face'],'username'=>$value['username']);
			}
		}
		if ($friend_online) {
			$str = pwJsonEncode($friend_online);
		}
	}
	echo "success\t$str";
	ajax_footer();
} elseif ($action == 'pwb_message') {
	InitGP(array('u'),'P',2);
	if (!$u) Showmsg('undefined_action');
	if ($u != $winduid) Showmsg('undefined_action');
	$msgdb = array();
	$query = $db->query("SELECT m.mid,m.fromuid,m.type,m.username as `from`,mc.title FROM pw_msg m LEFT JOIN pw_msgc mc USING(mid) WHERE m.type='rebox' AND m.touid=".pwEscape($winduid)." AND ifnew=1 ORDER BY m.mdate DESC ".pwLimit(0,20));
	while ($msginfo = $db->fetch_array($query)) {
		$msginfo['title'] = substrs($msginfo['title'],40);
		$msgdb[] = $msginfo;
	}
	
	//群发消息
	$pubmsg = getUserPublicMsgRecord($winduid);
	@extract($pubmsg);
	$checkmsg = $readmsg.','.$delmsg;
	$msg_gid = $winddb['groupid'];
	$query2  = $db->query("SELECT m.mid,m.fromuid,m.type,m.username as `from`,mc.title FROM pw_msg m LEFT JOIN pw_msgc mc USING(mid) WHERE m.type='public' AND m.togroups LIKE ".pwEscape("%,$msg_gid,%")." AND m.mdate>".pwEscape($winddb['regdate'])." ORDER BY m.mdate DESC LIMIT 20");
	while ($msginfo = $db->fetch_array($query2)) {
		if ($checkmsg && strpos(",$checkmsg,",",$msginfo[mid],") !== false) {
			continue;
		}
		$msginfo['title'] = substrs($msginfo['title'],40);
		$msgdb[] = $msginfo;
	}
	if (empty($msgdb) && $winddb['newpm'] > 0) {
		$db->update("UPDATE pw_members SET newpm='0' WHERE uid=".pwEscape($winduid));
	}
	$str = '';
	if ($msgdb) {
		$str = pwJsonEncode($msgdb);
	}
	echo "success\t$str";
	ajax_footer();
} elseif ($action  == 'readmessage') {
	
	require_once(R_P.'require/bbscode.php');
	InitGP(array('mid','type'));
	if ($type == 'personal' || $type == 'system') {
		$sqladd = $type == 'personal' ? 'm.fromuid<>0' : 'm.fromuid=0';
		$msginfo = $db->get_one("SELECT m.mid,m.fromuid AS withuid,m.touid,m.username,m.ifnew,m.mdate,mc.title,mc.content FROM pw_msg m LEFT JOIN pw_msgc mc USING(mid) WHERE m.mid=".pwEscape($mid)." AND m.type='rebox' AND m.touid=".pwEscape($winduid)."AND ".$sqladd);
		if ($msginfo) {
			$msginfo['content'] = str_replace("\n","<br>",$msginfo['content']);
			$msginfo['content'] = convert($msginfo['content'],$db_windpost);
			if (strpos($msginfo['content'],'[s:') !== false) {
				$msginfo['content'] = showface($msginfo['content']);
			}
			$msginfo['content'] = wordsfb($msginfo['content']);
			$msginfo['title']   = str_replace('&ensp;$','$', $msginfo['title']);
			$msginfo['content'] = str_replace('&ensp;$','$', $msginfo['content']);
			$msginfo['mdate']   = get_date($msginfo['mdate']);
			if ($msginfo['ifnew'] > 0) {
				$db->update("UPDATE pw_msg SET ifnew=0 WHERE mid=".pwEscape($mid));
				updateNewpm($winduid,'minus');
			}
			$str = '';
			if ($msginfo) {
				$str = pwJsonEncode($msginfo);
			}
			require_once PrintEot('ajax');ajax_footer();
		} else {
			Showmsg('msg_error');
		}
	} elseif ($type == 'public') {
		$msg_gid = $winddb['groupid'];
		$msginfo = $db->get_one("SELECT m.mid,m.fromuid AS withuid,m.touid,m.username,m.ifnew,m.mdate,mc.title,mc.content FROM pw_msg m LEFT JOIN pw_msgc mc USING(mid) WHERE m.mid=".pwEscape($mid)." AND m.type='public' AND m.togroups LIKE ".pwEscape("%,$msg_gid,%"));
		
		if ($msginfo) {
			require_once(R_P.'require/bbscode.php');
			$pubmsg = getUserPublicMsgRecord($winduid);
			@extract($pubmsg);
			$msginfo['content'] = str_replace("\n","<br>",$msginfo['content']);
			$msginfo['content'] = convert($msginfo['content'],$db_windpost);
			if (strpos($msginfo['content'],'[s:') !== false) {
				$msginfo['content'] = showface($msginfo['content']);
			}
			$msginfo['title']   = str_replace('&ensp;$','$', $msginfo['title']);
			$msginfo['content'] = str_replace('&ensp;$','$', $msginfo['content']);
			$msginfo['mdate']   = get_date($msginfo['mdate']);
			$msginfo['content'] = str_replace("\$email",$winddb['email'],$msginfo['content']);
			$msginfo['content'] = str_replace("\$windid",$windid,$msginfo['content']);
			
			if ($pubmsg) {
				if (strpos(",$readmsg,",",$msginfo[mid],") === false) {
					$readmsg .= $readmsg ? ','.$msginfo['mid'] : $msginfo['mid'];
					if (empty($readmsg)) {
						$readmsg = '';
					} else {
						$temp_msgdb  = explode(',',$readmsg);
						arsort($temp_msgdb);
						$readmsg = implode(',',$temp_msgdb);
					}				
					$db->update('UPDATE pw_memberinfo SET readmsg='.pwEscape($readmsg,false).' WHERE uid='.pwEscape($winduid));
				}
			} else {
				$readmsg = $msginfo['mid'];
				$db->update("INSERT INTO pw_memberinfo SET ".pwSqlSingle(array('uid'=>$winduid,'readmsg'=>$readmsg)));
			}
			updateNewpm($winduid,'minus');
			$str = '';
			if ($msginfo) {
				$str = pwJsonEncode($msginfo);
			}
			require_once PrintEot('ajax');ajax_footer();
		} else {
			Showmsg('msg_error');
		}
	} else {
		Showmsg('msg_error');
	}
	
} elseif ($action == 'unread') {
	
	PostCheck();
	InitGP(array('mid'));
	$rt = $db->get_one("SELECT mid FROM pw_msg WHERE mid=".pwEscape($mid)." AND type='rebox' AND touid=".pwEscape($winduid)."AND fromuid<>0");
	if ($rt) {
		$db->update('UPDATE pw_msg SET ifnew=1 WHERE mid='.pwEscape($mid));
		updateNewpm($winduid,'add');
		Showmsg('msg_unread_success');
	} else {
		Showmsg('msg_error');
	}
	
} elseif ($action == 'delmessage') {
	
	PostCheck();
	InitGP(array('delid','type'));
	
	!$delid && Showmsg('del_error');

	if ($type == 'personal' || $type == 'system') {
		$newdelid = '';
		$newdelid = $db->get_value("SELECT mid FROM pw_msg WHERE mid=".pwEscape($delid)."  AND type='rebox' AND touid=".pwEscape($winduid));
		if ($newdelid) {
			$db->update("DELETE FROM pw_msg WHERE mid=".pwEscape($newdelid));
			require_once(R_P.'require/msg.php');
			delete_msgc($newdelid);
		}
	} elseif ($type == 'public') {
		$delmsg = $db->get_value('SELECT delmsg FROM pw_memberinfo WHERE uid='.pwEscape($winduid));
		if ($delmsg) {
			$delmsg  = ','.$delmsg.','.$delid.',';
			$delmsg = substr($delmsg,1,-1);
			$temp_delmsg = explode(',',$delmsg);
			arsort($temp_delmsg);
			$delmsg = implode(',',$temp_delmsg);
			$db->update("UPDATE pw_memberinfo SET delmsg=".pwEscape($delmsg)." WHERE uid=".pwEscape($winduid));
		} else {
			$db->pw_update(
				"SELECT * FROM pw_memberinfo WHERE uid=".pwEscape($winduid),
				"UPDATE pw_memberinfo SET delmsg=".pwEscape($delid)." WHERE uid=".pwEscape($winduid),
				"INSERT INTO pw_memberinfo SET ".pwSqlSingle(array('uid'=>$winduid,'delmsg'=>$delid))
			);
		}
	} else {
		Showmsg('del_error');
	}
	
	$winddb['newpm'] = 0;
	Showmsg('del_success');
}

function getfavor($tids) {
	$tids  = explode('|',$tids);
	$tiddb = array();
	foreach ($tids as $key => $t) {
		if ($t) {
			$v = explode(',',$t);
			foreach ($v as $k => $v1) {
				$tiddb[$key][$v1] = $v1;
			}
		}
	}
	return $tiddb;
}
function makefavor($tiddb) {
	$newtids = $ex = '';
	$k = 0;
	ksort($tiddb);
	foreach ($tiddb as $key => $val) {
		$new_tids = '';
		rsort($val);
		if ($key != $k) {
			$s = $key - $k;
			for ($i = 0; $i < $s; $i++) {
				$newtids .= '|';
			}
		}
		foreach ($val as $k => $v) {
			is_numeric($v) && $new_tids .= $new_tids ? ','.$v : $v;
		}
		$newtids .= $ex.$new_tids;
		$k  = $key + 1;
		$ex = '|';
	}
	return $newtids;
}

function getNum($fid){
	if ($forum[$fid]['type'] = 'category'){
		$fidnum = 1;
	} elseif ($forum[$fid]['type'] = 'forum'){
		$fidnum = 2;
	} elseif ($forum[$fid]['type'] = 'sub'){
		$fup = $forum[$fid]['fup'];
		if ($forum[$fup]['type'] = 'forum'){
			$fidnum = 3;
		}else{
			$fidnum = 4;
		}
	}
	return $fidnum;
}
?>