###############################################################################
# MailMembers.pl                                                              #
###############################################################################
# YaBB: Yet another Bulletin Board                                            #
# Open-Source Community Software for Webmasters                               #
# Version:        YaBB 2 RC 1                                                 #
# Released:       December 27, 2004                                           #
# Distributed by: http://www.yabbforum.com                                    #
# =========================================================================== #
# Copyright (c) 2000-2005 YaBB (www.yabbforum.com) - All Rights Reserved.     #
# Software by: The YaBB Development Team                                      #
#              with assistance from the YaBB community.                       #
# Sponsored by: Xnull Internet Media, Inc. - http://www.ximinc.com            #
#               Your source for web hosting, web design, and domains.         #
###############################################################################

$mailmembersplver = 'YaBB 2 RC 1 $Revision: 1.20 $';
if($action eq 'detailedversion') { return 1; }

if($iamguest) { &admin_fatal_error("$ml_txt{'223'}"); }

## language definition $ml_txt in Main.lng ##

LoadLanguage("Main");

require "$vardir/membergroups.txt";

$sortmode = "";
$selPos = "";
$selUser = "";

if($FORM{'sortform'} eq "position") { $selPos = qq~ selected="selected"~; }
else { $selUser = qq~ selected="selected"~; }

if ($INFO{'sort'} ne "") {$sortmode = ";sort=".$INFO{'sort'};}
elsif ($FORM{'sortform'} ne "") {$sortmode = ";sort=".$FORM{'sortform'};}

sub MLByLetter {
	if($iamguest) { &admin_fatal_error("$ml_txt{'223'}"); }
	$yymain .= qq~

	<table border="0" width="100%" cellspacing="1" cellpadding="3" class="bordercolor">
	<tr>
		<td width="100%" valign="middle" class="titlebg">
		<span style="float: left;"><img src="$imagesdir/register.gif" alt="" border="0" style="vertical-align: middle;" /><b> $admintxt{'19'}</b></span>
		<span style="float: right;">
		<b>$ml_txt{'1'}</b>
		<form action="$adminurl?action=mailing" method="post" name="selsort" style="display: inline">
		<select name="sortform" style="font-size: 9pt;" onchange="submit()">
			<option value="username"$selUser>$ml_txt{'35'}</option>
			<option value="position"$selPos>$ml_txt{'87'}</option>
		</select>
		</form>
		</span>
		</td>
	</tr>
	</table>
	<script language="JavaScript1.2" src="$ubbcjspath" type="text/javascript"></script>
	<form name="adv_membermail" action="$adminurl?action=deletemultimembers;$sortmode" method="POST" style="display: inline" onsubmit="return checkIfChecked(this); return submitproc()">
	<input type="hidden" name="button" value="1" />

	<div class="windowbg2" style="width: 100%; border: 1px #cccccc solid;">

	<div class="windowbg" style="float: left; width: 44%; height: 270px; margin: 1%; border: 1px #cccccc inset; overflow: auto;">
	<table border="0" width="99%" cellspacing="0" cellpadding="3" class="windowbg">
	~;
	%TopMembers = ();

	fopen(MEMBERLISTREAD,"$memberdir/memberlist.txt");
		@member = ();
		while(chomp($memdata=<MEMBERLISTREAD>)) {
			($membername,$mememail,$memrealname) = split (/\|/, $memdata);
			&LoadUser($membername);
			if($membername eq $username) {
				$TopMembers{$membername} = "!!!";
			}
			else {
				if($FORM{'sortform'} eq "position" || $INFO{'sort'} eq "position"){
					$pstsort = 99999999 - ${$uid.$membername}{'postcount'};
					if(${$uid.$membername}{'position'} ne "") {
						if(${$uid.$membername}{'position'} eq "Administrator") { $sortgroups = "aaa.$pstsort"; }
						elsif(${$uid.$membername}{'position'} eq "Global Moderator") { $sortgroups = "bbb.$pstsort"; }
						else { $sortgroups = "ccc.$memberunfo{$membername}.$pstsort"; }
					}
					else {
						$sortgroups = "ddd.$pstsort.$memberunfo{$membername}";
					}
					$TopMembers{$membername} = $sortgroups;
				}
				else {
					$TopMembers{$membername} = $memrealname;
				}
			}
		}
		fclose(MEMBERLISTREAD);

		my @toplist = sort { lc $TopMembers{$a} cmp lc $TopMembers{$b}} keys %TopMembers;

	$memcount = @toplist;

	$b=0;
	$numshown=0;
	$actualnum = 0;

	while(($numshown < $memcount)){
		$user = $toplist[$b];

		if ($user ne ""){
			&LoadUser($user);
		}
		if($user eq $username) {$bagcolor = "windowbg2";} else {$bagcolor = "windowbg";}
		if(${$uid.$user}{'realname'} ne "") {

			$addel = qq~<input type="checkbox" name="member$actualnum" value="$user" class="windowbg" style="border: 0;" />~;
			$actualnum++;

			my $memberinfo = "&nbsp;";
			if(!${$uid.$user}{'position'} && $showallgroups) {
				foreach $postamount (sort { $b <=> $a } keys %Post) {
		   			if (${$uid.$user}{'postcount'} > $postamount){
	   					($title, $stars, $starpic, $color, $noshow, $viewperms, $topicperms, $replyperms, $pollperms, $attachperms) = split(/\|/, $Post{$postamount});
						$title =~ s/\'/\\'/g;
						$title =~ s/\"/\\"/g;
   						$memberinfo = $title;
   						last;
	   				}
				}
			}
			elsif(${$uid.$user}{'position'} eq "Administrator") {
				($memberinfo, $dummy) = split(/\|/, $Group{"Administrator"},2);
			}
			elsif(${$uid.$user}{'position'} eq "Global Moderator") {
				($memberinfo, $dummy) = split(/\|/, $Group{"Global Moderator"},2);
			}
			elsif(${$uid.$user}{'position'} eq "Moderator") {
				($memberinfo, $dummy) = split(/\|/, $Group{"Moderator"},2);
			}
			else {
				$memberinfo = ${$uid.$user}{'position'};
			}
			if(${$uid.$user}{'addgroups'} ne "") {
				$memberinfo .= qq~, ${$uid.$user}{'addgroups'}~;
			}
			$viewmembinfo = $memberinfo;
			&ToJS($memberinfo);
			$tmp_postcount = ${$uid.$user}{'postcount'};
			$checkinfo = $memberinfo;
			$checkinfo =~ s/\, /\'\|\'/g;
			$CheckingAll .= qq~"'$checkinfo'", ~;

			$yymain .= qq~
			<tr>
			<td class="$bagcolor" align="center" valign="middle">$addel</td>
			<td class="$bagcolor" align="left" valign="middle">$link{$user}<br />$viewmembinfo<br />${$uid.$user}{'email'}</td>
			</tr>~;
		}

		$numshown++;
		$b++;
	}
	undef @toplist;

	$yymain .= qq~
	</table>
	</div>
	~;

unless ($memcount == 0)	{
	if ($FORM{'sortform'} eq ""){$FORM{'sortform'} = $INFO{'sort'};}
	if (!$FORM{'reversed'}){$FORM{'reversed'} = $INFO{'reversed'};}

	@groupinfo = ();
	$i=0; $z=0;

	($title, $dummy) = split(/\|/, $Group{"Administrator"},2);
	&ToJS($title);
	$groupinfo[$i] = $title; $i++;
	$grp_data = qq~"'$title'", ~;
	($title, $dummy) = split(/\|/, $Group{"Global Moderator"},2);
	&ToJS($title);
	$groupinfo[$i] = $title; $i++;
	$grp_data .= qq~"'$title'", ~;
	($title, $dummy) = split(/\|/, $Group{"Moderator"},2);
	&ToJS($title);
	$groupinfo[$i] = $title; $i++;
	$grp_data .= qq~"'$title'", ~;
	while($NoPost[$z]) {
		($title, $dummy) = split(/\|/, $NoPost[$z],2);
		&ToJS($title);
		$groupinfo[$i] = $title;
		$grp_data .= qq~"'$title'", ~;
		$i++; $z++;
	}
	$groupcnt = $i;
	$grp_data .= qq~""~;

	$yymain .= qq~
	<div class="windowbg2" style="float: left; width: 50%; height: 270px; margin: 1%; padding: 4px; border: 1px #cccccc solid;">

	<table border="0" width="100%" cellspacing="0" cellpadding="2" class="windowbg2">
        <tr>
           <td align="left" width="100%"><b>$amv_txt{'1'}:</b></td>
        </tr>
        <tr>
           <td align="left" width="100%"><input type="text" value="" size="40" name="emailsubject" style="width: 100%" /></td>
        </tr>
        <tr>
           <td align="left" width="100%"><b>$amv_txt{'2'}:</b></td>
        </tr>
        <tr>
           <td align="left" width="100%"><textarea cols="38" rows="11" name="emailtext" style="width:100%"></textarea></td>
        </tr>
        <tr>
		<td align="left" width="100%"><span class="small">$amv_txt{'39'}</span></td>
        </tr>
	</table>
	</div>

	<div class="windowbg2" style="float: left; width: 44%; margin: 1%; margin-top: 0; border: 0;">
	<table border="0" width="100%" cellspacing="0" cellpadding="3" class="windowbg2">
	<tr>
	<td class="windowbg2" align="left" valign="top"><b>$amv_txt{'42'}:</b></td>
	<td class="windowbg2" align="left" valign="top"><input type="checkbox" name="check_all" value="1" class="windowbg2" style="border: 0;" onclick="javascript: if (this.checked) selectCheckAll(true); else selectCheckAll(false);" /></td>
	</tr>
<script language="JavaScript1.2" type="text/javascript">
<!--

    document.write('<tr>');
    document.write('<td class="windowbg2" align="left" valign="top"><b>$amv_txt{'40'}</b><br /><span class="small">$amv_txt{'46'}</span></td>');
    document.write('<td class="windowbg2" align="left">');
    document.write('<select name="field1" size="$groupcnt" multiple="multiple" onchange="selectCheck()">');
	~;
	$i=0;
	while ($i < $groupcnt) {
$yymain .= qq~
	document.write('<option value="$i">$groupinfo[$i]</option>');
~;
	$i++;
	}
	$yymain .= qq~
    document.write('</select> ');
    document.write('</td></tr>');
//-->
</script>
	</table>
	</div>

	<div class="windowbg2" style="float: left; width: 50%; margin: 1%; margin-top: 0; margin-bottom: 0; border: 0;">
	<table border="0" width="100%" cellspacing="0" cellpadding="3" class="windowbg2">
	<tr>
	<td class="windowbg2" align="left" valign="top"><b>$amv_txt{'47'}:</b></td>
	</tr>
	</table>
	</div>

	<div class="windowbg2" style="float: left; width: 50%; height: 95px; margin: 1%; border: 1px #cccccc solid; overflow: auto;">
	~;
	if(-e ("$vardir/maillist.dat")) {
		fopen(FILE, "$vardir/maillist.dat");
		@maillist = <FILE>;
		fclose(FILE);
		$yymain .= qq~
		<table border="0" width="99%" cellspacing="0" cellpadding="3" class="windowbg2">
		~;
		foreach $curmail (@maillist) {
			chomp $curmail;
			($otime, $osubject, $otext, $osender) = split(/\|/, $curmail);
			&LoadUser($osender);
			$thetime = &timeformat($otime);

			$jsubject = $osubject;
			$jtext = $otext;
			&ToJS($jsubject);
			&ToJS($jtext);

			$yymain .= qq~
			<tr>
				<td class="windowbg2" align="left" valign="middle">
					<input type="radio" name="usemail" value="$otime" class="windowbg2" style="border: 0; vertical-align: middle;" onclick="showMail('$jsubject', '$jtext');" />
				</td>
				<td class="windowbg2" align="left" valign="top"><span class="small">$thetime<br />${$uid.$osender}{'realname'}</span></td>
				<td class="windowbg2" align="left" valign="top"><span class="small">$osubject</span></td>
				<td class="windowbg2" align="left" valign="middle"><a href="$adminurl?action=deletemail;delmail=$otime"><img src="$imagesdir/admin_rem.gif" border="0" alt="del" /></a></td>
			</tr>
			~;
		}
		$yymain .= qq~
		</table>
		~;
	}
	$yymain .= qq~
	</div>

<div style="clear: both;"></div>
</div>

<table border="0" width="100%" cellspacing="1" cellpadding="3" class="bordercolor">
    <tr>
      <td class="titlebg" align="center">
            <input type="submit" value="$amv_txt{'41'}" />
      </td>
    </tr>
</table>

</form>

<script language="JavaScript1.2" type="text/javascript">
<!--
mem_data = new Array ( $CheckingAll"" );
group_data = new Array ( $grp_data );

function selectCheckAll(tchecked) {
	for(var x = 0; x < document.adv_membermail.field1.options.length; x++) document.adv_membermail.field1.options[x].selected = tchecked;
	for(var i = 1; i <= $actualnum; i++) document.adv_membermail.elements[i].checked = tchecked;
}

function selectCheck() {
var z = 1;
var grpcnt = 0;
grp_data = new Array ();

	for(x = 0; x < document.adv_membermail.field1.options.length; x++) {
		if (document.adv_membermail.field1.options[x].selected) {
			grp_data[grpcnt] = group_data[document.adv_membermail.field1.options[x].value];
			grpcnt++;
		}
	}

	if (grpcnt < document.adv_membermail.field1.options.length) { document.adv_membermail.check_all.checked = false; }

	for (var i = 0; i < $actualnum; i++) {
		var check = 0;
		for(x = 0; x < grpcnt; x++) {
			var limit = grp_data[x];
			var value = mem_data[i].split("|");
			var j = 0;
			while(value[j]) {
				if (value[j] == limit) { check = 1; x = grpcnt; }
				j++;
			}
		}
		if (check == 1) {document.adv_membermail.elements[z].checked = true;}
		else {document.adv_membermail.elements[z].checked = false;}
		z++;
	}
}

function checkIfChecked(theForm) {
	var nonechecked = true;
	for(var i = 1; i <= $actualnum; i++) {
		if (document.adv_membermail.elements[i].checked) nonechecked = false;
	}
	if (nonechecked) { alert("$amv_txt{'48'}"); return false }
	return true
}

function showMail(thesubject, thetext) {
	thetext=thetext.replace(/\<br \\/\>/g, "\\n");
	document.adv_membermail.emailsubject.value = thesubject;
	document.adv_membermail.emailtext.value = thetext;
}
//-->
</script>
	~;
	}

	$yytitle = "$admin_txt{'6'}";
	$action_area = "mailing";
	&AdminTemplate;
	exit;
}

sub ToJS {
	$_[0] =~ s~\;~&#059~g;
	$_[0] =~ s~\!~&#33~g;
	$_[0] =~ s~\(~&#40~g;
	$_[0] =~ s~\)~&#41~g;
	$_[0] =~ s~\-~&#45~g;
	$_[0] =~ s~\.~&#46~g;
	$_[0] =~ s~\:~&#58~g;
	$_[0] =~ s~\?~&#63~g;
	$_[0] =~ s~\[~&#91~g;
	$_[0] =~ s~\\~&#92&#92~g;
	$_[0] =~ s~\]~&#93~g;
	$_[0] =~ s~\^~&#94~g;
	$_[0] =~ s~\"~&#34~g;
	$_[0] =~ s~\'~&#96~g;
}

1;