<?php

/**
 * [Discuz!] (C)2001-2099 Discuz! Team
 * This is NOT a freeware, use is subject to license terms
 * https://license.discuz.vip
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

if(!$_G['setting']['friendstatus']) {
	showmessage('friend_status_off');
}

require_once libfile('function/friend');

$_GET['action'] = dhtmlspecialchars(preg_replace('/[^\[A-Za-z0-9_\]]/', '', $_GET['action']));
$friendgrouplist = friend_group_list();
if($_GET['action'] == 'group') {
	$id = intval($_GET['id']);
	$groupuserinfo = table_forum_groupuser::t()->fetch_userinfo($_G['uid'], $id);
	if(empty($groupuserinfo['uid'])) {
		showmessage('group_invite_failed');
	}
	$foruminfo = table_forum_forum::t()->fetch($id);
	$grouplevel = $foruminfo['level'];
	loadcache('grouplevels');
	$grouplevel = $_G['grouplevels'][$grouplevel];
	$membermaximum = $grouplevel['specialswitch']['membermaximum'];
	if(!empty($membermaximum)) {
		$curnum = table_forum_groupuser::t()->fetch_count_by_fid($id, -1);
		if($curnum >= $membermaximum) {
			showmessage('group_member_maximum', '', ['membermaximum' => $membermaximum]);
		}
	}

	$groupname = $foruminfo['name'];
	$invitename = lang('group/misc', 'group_join', ['groupname' => $groupname]);
	if(!submitcheck('invitesubmit')) {
		$friends = friend_list($_G['uid'], 100);
		if(!empty($friends)) {
			$frienduids = array_keys($friends);
			$inviteduids = [];
			$query = table_forum_groupinvite::t()->fetch_all_inviteuid($id, $frienduids, $_G['uid']);
			foreach($query as $inviteuser) {
				$inviteduids[$inviteuser['inviteuid']] = $inviteuser['inviteuid'];
			}
			$query = table_forum_groupuser::t()->fetch_all_userinfo($frienduids, $id);
			foreach($query as $inviteuser) {
				$inviteduids[$inviteuser['uid']] = $inviteuser['uid'];
			}
		}
		$inviteduids = !empty($inviteduids) ? implode(',', $inviteduids) : '';
	} else {
		$uids = $_GET['uids'];
		if($uids) {
			if(count($uids) > 20) {
				showmessage('group_choose_friends_max');
			}
			foreach(table_common_member::t()->fetch_all($uids, false, 0) as $uid => $user) {
				table_forum_groupinvite::t()->insert(['fid' => $id, 'uid' => $_G['uid'], 'inviteuid' => $uid, 'dateline' => TIMESTAMP], true, true);
				$already = table_forum_groupinvite::t()->affected_rows();
				if($already == 1) {
					notification_add($uid, 'group', 'group_member_invite', ['groupname' => $groupname, 'fid' => $id, 'url' => 'forum.php?mod=group&action=join&fid='.$id, 'from_id' => $id, 'from_idtype' => 'invite_group'], 1);
				}
			}
			showmessage('group_invite_succeed', "forum.php?mod=group&fid=$id");
		} else {
			showmessage('group_invite_choose_member', "forum.php?mod=group&fid=$id");
		}
	}
} elseif($_GET['action'] == 'thread') {
	$inviteduids = [];
	$id = intval($_GET['id']);
	$thread = table_forum_thread::t()->fetch_thread($id);
	$at = 0;
	$maxselect = 20;
	if(empty($_GET['activity'])) {
		$at = 1;
		$maxselect = 0;
		if($_G['group']['allowat']) {
			$atnum = 0;
			foreach(table_home_notification::t()->fetch_all_by_authorid_fromid($_G['uid'], $id, 'at') as $row) {
				$atnum++;
				$inviteduids[$row['uid']] = $row['uid'];
			}
			$maxselect = $_G['group']['allowat'] - $atnum;
		} else {
			showmessage('noperm_at_user');
		}
		if($maxselect <= 0) {
			showmessage('thread_at_usernum_limit');
		}
		$invitename = lang('forum/misc', 'at_invite');
	} else {
		$invitename = lang('forum/misc', 'join_activity');
	}

	if(!submitcheck('invitesubmit')) {
		$inviteduids = !empty($inviteduids) ? implode(',', $inviteduids) : '';
	} else {
		$uids = $_GET['uids'];
		if($uids) {
			if(count($uids) > $maxselect) {
				showmessage('group_choose_friends_max');
			}
			$post = table_forum_post::t()->fetch_threadpost_by_tid_invisible($id);
			require_once libfile('function/post');
			$post['message'] = threadmessagecutstr($thread, $post['message'], 150);
			foreach(table_common_member::t()->fetch_all($uids, false, 0) as $uid => $user) {
				if($at) {
					notification_add($uid, 'at', 'at_message', ['from_id' => $id, 'from_idtype' => 'at', 'buyerid' => $_G['uid'], 'buyer' => $_G['username'], 'tid' => $id, 'subject' => $thread['subject'], 'pid' => $post['pid'], 'message' => $post['message']]);
				} else {
					notification_add($uid, 'thread', 'thread_invite', ['subject' => $thread['subject'], 'invitename' => $invitename, 'tid' => $id, 'from_id' => $id, 'from_idtype' => 'invite_thread']);
				}
			}
			showmessage(($at ? 'at_succeed' : 'group_invite_succeed'), "forum.php?mod=viewthread&tid=$id");
		} else {
			showmessage(($at ? 'at_choose_member' : 'group_invite_choose_member'), "forum.php?mod=viewthread&tid=$id");
		}
	}
} elseif($_GET['action'] == 'blog') {
	$id = intval($_GET['id']);
	$blog = table_home_blog::t()->fetch($id);

	if(!submitcheck('invitesubmit')) {
		$inviteduids = '';
	} else {
		$uids = $_GET['uids'];
		if($uids) {
			if(count($uids) > 20) {
				showmessage('group_choose_friends_max');
			}
			foreach(table_common_member::t()->fetch_all($uids, false, 0) as $uid => $user) {
				notification_add($uid, 'blog', 'blog_invite', ['subject' => $blog['subject'], 'uid' => $blog['uid'], 'blogid' => $id, 'from_id' => $id, 'from_idtype' => 'invite_blog']);
			}
			showmessage('group_invite_succeed', 'home.php?mod=space&uid='.$blog['uid']."&do=blog&id=$id");
		} else {
			showmessage('group_invite_choose_member', 'home.php?mod=space&uid='.$blog['uid']."&do=blog&id=$id");
		}
	}
} elseif($_GET['action'] == 'article') {
	$id = intval($_GET['id']);
	$article = table_portal_article_title::t()->fetch($id);

	if(!submitcheck('invitesubmit')) {
		$inviteduids = '';
	} else {
		require_once libfile('function/portal');
		$article_url = fetch_article_url($article);
		$uids = $_GET['uids'];
		if($uids) {
			if(count($uids) > 20) {
				showmessage('group_choose_friends_max');
			}
			foreach(table_common_member::t()->fetch_all($uids, false, 0) as $uid => $user) {
				notification_add($uid, 'article', 'article_invite', ['subject' => $article['title'], 'url' => $article_url, 'from_id' => $id, 'from_idtype' => 'invite_article']);
			}
			showmessage('group_invite_succeed', $article_url);
		} else {
			showmessage('group_invite_choose_member', $article_url);
		}
	}
}

include template('common/invite');
