###############################################################################
# News.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            #
###############################################################################

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

$ElevateNewsItems = 10;
$newstempname = "$templatesdir/$template.News.template";
&LoadLanguage("Display");
&LoadLanguage("FA");
&LoadLanguage("Poll");

sub ShowNews {
	$display = ($INFO{'dsp'} && $INFO{'dsp'} <= 50) ? $INFO{'dsp'} : $ElevateNewsItems;
	$catboards[0] = $currentboard;

	foreach $curboard (@catboards) {
		chomp $curboard;
		$currcat = "$INFO{'board'}";
		unless ( $currcat ) { &fatal_error("Cat not found"); }

		fopen(BOARDFILE, "$boardsdir/$curboard.txt");
		@threads = <BOARDFILE>;
		fclose(BOARDFILE);
		chomp @threads;

		for ($k = 0; $k < $ElevateNewsItems; $k++) {
			($tnum, $tsub, $tname, $temail, $tdate, $treplies, $tusername, $ticon, $tstate) = split( /\|/, $threads[$k] );
			fopen(MFILE, "$datadir/$tnum.txt") || next;
			@messages2 = <MFILE>;
			fclose(MFILE);
			$message = $messages2[0];
			chomp $message;

			if( $message ) {
				($msub, $mname, $memail, $mdate, $musername, $micon, $mattach, $mip, $message, $mns, $mlm, $mlmb, $mfn) = split(/\|/,$message);
				($views, $lastposter) = split(/\|/,$message2);
				if ($INFO{'nosort'}) {
					$mtime = $tdate;
				}
				else {
					$mtime = $mdate;
				}
				$data{$mtime} = [$views, $lastposter, 0, $curboard, $tnum, $treplies, $tusername, $tname, $msub, $mname, $memail, $mdate, $musername, $micon, $mattach, $mip, $message, $mns, $mfn];
			}
		}
 	}

######################
# print news
#
	@messages2 = sort {$b <=> $a } keys %data;
	if( @messages2 ) {
		if( @messages2 > $display ) { $#messages2 = $display - 1; }
		&LoadCensorList;		# Load Censor List
	}


	fopen(TEMPLATE, $newstempname) || &fatal_error("File: '$newstempname' not found.");
	@news_template = <TEMPLATE>;
	fclose(TEMPLATE);

	for( $k = "0"; $k < @messages2; $k++ ) {
		($views, $lastposter, $boardname, $board, $tnum, $c, $tusername, $tname, $msub, $mname, $memail, $mdate, $musername, $micon, $mattach, $mip, $message, $mns, $mfn) = @{ $data{$messages2[$k]} };
		$message =~ s~\&#124;~\|~g;
		$msub =~ s~\&#124;~\|~g;
		$mname =~ s~\&#124;~\|~g;
		&LoadUser($musername);	# If user is not in memory, s/he must be loaded.

########## START FILE ATTACHEMENT ADDITION ##########
		if($mfn && $mfn ne "") {				 		# Should we show an attachment file?
			if (-e ("$uploaddir/$mfn")) {
				$attachment = qq~<span class="small"><a href="$uploadurl/$mfn" target="_blank"><img src="$imagesdir/paperclip.gif" border="0" align="absmiddle"> $mfn</a></span>~;
				if(($mfn =~ /(jpg|gif|bmp|png|jpeg)$/i) && ($amdisplaypics == 1)) {
					$showattach = qq~<table width="80%" align="center"><tr><Td><FIELDSET><LEGEND><font color="#ffffff">Attached Images&nbsp;</font></LEGEND>
	                              <TABLE cellSpacing=3 cellPadding=0 border=0>
	                                <TBODY>
	                                    <!-- BEGIN TEMPLATE: postbit_attachment -->
	                                    <TR>
	                                      <TD><img src="$uploadurl/$mfn" alt="$mfn" border="0"></TD>
	                                    </TR>
	                                    <!-- END TEMPLATE: postbit_attachment -->
	                                </TBODY>
	                              </TABLE>
	                              </FIELDSET></td></tr></table>~;
				} else {
					$showattach = qq~<table width="80%" align="center"><tr><Td><FIELDSET><LEGEND><font color="#ffffff">Attached Files&nbsp;</font></LEGEND>
	                              <TABLE cellSpacing=3 cellPadding=0 border=0>
	                                <TBODY>
	                                    <!-- BEGIN TEMPLATE: postbit_attachment -->
	                                    <TR>
								          <TD><img src="$imagesdir/paperclip.gif" border="0" align="absmiddle"> - </TD>
	                                      <TD><a href="$uploadurl/$mfn" target="_blank">$mfn</a></TD>
	                                    </TR>
	                                    <!-- END TEMPLATE: postbit_attachment -->
	                                </TBODY>
	                              </TABLE>
	                            </FIELDSET></td></tr></table>~;
				}
			} else {
				$attachment = qq~<span class="small"><img src="$imagesdir/paperclip.gif" border="0" align="absmiddle">  $mfn $fatxt{'1'}</span>~;
				$showattach = '';
			}
		} else {
			$attachment = '';
			$showattach = '';
		}
########## END FILE ATTACHEMENT ADDITION ##########

		&Censor($message);
		&Censor($msub);

		&wrap;
		$message =~ s/\&#124;/\|/g;

		if($enable_ubbc) { if(!$yyYaBBCloaded) { require "$sourcedir/YaBBC.pl"; } &DoUBBC; }
		&wrap2;

### Check if there's a poll for this news item?
		if (-e "$datadir/$tnum.poll") { $ihas_poll = 1; } else { $ihas_poll = 0; }	### Look for a poll file for this thread.

		$newsdate = &timeformat($mdate);
		$newstitle = $msub;
		$newsauthor = qq~ $link{$musername}~;
		$newscategory = $boardname;
		$newsboardid = $board;
		$newsreplies = $c;
		$newsattachment = $attachment;
		$newscommentsurl = qq~$scripturl?num=$tnum/new~;
		$newsaddcommenturl = qq~$scripturl?action=post;num=$tnum;title=Post+reply~;
		$newsmessage = qq~$message<br><Br>$showattach~;

		for( $news_templatemain = 0; $news_templatemain < @news_template; $news_templatemain++ ) {
			$news_curline = $news_template[$news_templatemain];
			if ((-e "$datadir/$tnum.poll") && ($news_curline =~ /<yabb newspoll>/)) {
				&display_newspoll($tnum);
			} else {
				$news_curline =~ s~<yabb\s+(\w+)>~${"news$1"}~g;
				$yymain .= qq~ $news_curline~;
			}
		}
	}
	$yytitle = "News Page";
	&template;
	exit;
}
#
# End of Main news stuff..
#########################################
# Display a Polled News item...
#
sub display_newspoll{
	$pollnum = @_[0];

	&LoadCensorList;	# Load Censor List

	fopen (FILE, "$datadir/$pollnum.poll");
	$poll_question = <FILE>;
	@poll_data = <FILE>;
	fclose (FILE);
	chomp $poll_question;
	($poll_question, $poll_locked, $poll_uname, $poll_name, $poll_email, $poll_date, $guest_vote, $hide_results, $multi_vote, $poll_mod, $poll_modname, $poll_comment) = split(/\|/, $poll_question);

	$has_voted = 0;
	if (!$guest_vote && $username eq "Guest") { $has_voted = 4; }
	else {
		fopen(FILE, "$datadir/$pollnum.polled");
		@polled = <FILE>;
		fclose(FILE);
		foreach $tmpLine (@polled){
			chomp $tmpline;
			($voters_ip, $voters_name, $voters_vote, $vote_date) = split(/\|/, $tmpLine);
			   if ($username eq "Guest" && $voters_name eq "Guest" && lc $voters_ip eq lc $user_ip) { $has_voted = 1; last; }
			elsif ($username eq "Guest" && $voters_name ne "Guest" && lc $voters_ip eq lc $user_ip) { $has_voted = 2; last; }
			elsif ($username ne "Guest" && lc $username eq lc $voters_name) { $has_voted = 3; last; }
		}
	}

	my @options;
	my @votes;
	my $totalvotes = 0;
	my $maxvote = 0;
	for(my $i = 0; $i < @poll_data; $i++) {
		chomp $poll_data[$i];
		($votes[$i], $options[$i]) = split(/\|/, $poll_data[$i]);
		$totalvotes += int($votes[$i]);
		if (int($votes[$i]) >= $maxvote) { $maxvote = int($votes[$i]); }
	}

	$endedtext = "";
	if ($username ne "Guest" && ($username eq $poll_uname || $settings[7] eq "Administrator" || $settings[7] eq "Global Moderator" || exists $moderators{$username})) {
		if ($poll_locked) {
			$lockpoll = qq~<a href="$cgi;action=lockpoll;num=$pollnum" class="altlink">$img{'openpoll'}</a>~;
		} else {
			$lockpoll = qq~<a href="$cgi;action=lockpoll;num=$pollnum" class="altlink">$img{'closepoll'}</a>~;
		}
		$modifypoll = qq~$menusep<a href="$cgi;action=modify;message=$polltxt{'15'};thread=$pollnum" class="altlink">$img{'modifypoll'}</a>~;
		$deletepoll = qq~$menusep<a href="$cgi;action=modify2;thread=$pollnum;id=$polltxt{'15'};d=1" onclick="return confirm('$polltxt{'44'}')" class="altlink">$img{'deletepoll'}</a>~;
		if ($settings[7] eq "Administrator") { $displayvoters = qq~<a href="$cgi;action=showvoters;num=$pollnum">$img{'viewvotes'}</a>~;}
		if ($hide_results) {
			$endedtext = qq~<tr>
                  <td colspan="2" valign="top" align="center"><font size="2"><b>$polltxt{'53'}</b></font><br><br></td>
                </tr>~;
			$hide_results = 0;
		}
	}

	if($poll_modname ne "" && $poll_mod ne "" && $showmodify) {
		$poll_mod = &timeformat($poll_mod);
		fopen (FILE, "$memberdir/$poll_modname.dat");
		@info = <FILE>;
		fclose(FILE);
		chomp $info[1];
		$displaydate = qq~<font size="1">&#171; $polltxt{'45a'}: <a href="$cgi;action=viewprofile;username=$poll_modname">$info[1]</a> $polltxt{'46'}: $poll_mod &#187;</font>~;
	} elsif ($poll_uname ne "" && $poll_date ne "") {
		$poll_date = &timeformat($poll_date);
		if($poll_uname ne 'Guest' && -e "$memberdir/$poll_uname.dat") {
			fopen (FILE, "$memberdir/$poll_uname.dat");
			@info = <FILE>;
			fclose(FILE);
			chomp $info[1];
			$displaydate = qq~ ~;
		} elsif ($poll_name ne "") {
			$displaydate = qq~<font size="1">&#171; $polltxt{'45'}: $poll_name $polltxt{'46'}: $poll_date &#187;</font>~;
		} else {
			$displaydate = "";
		}
	} else {
		$displaydate = "";
	}

	if ($poll_locked) {
		$endedtext = qq~<tr>
                  <td colspan="2" valign="top" align="center"><font size="2"><b>$polltxt{'22'}</b></font><br><br></td>
                </tr>~;
		$poll_icon = qq~$img{'polliconclosed'}~;
		$has_voted = 5;
	} else {
		$poll_icon = qq~$img{'pollicon'}~;
	}

	# Censor the question.
	&Censor($poll_question);
	&Censor($poll_comment);
	if($ubbcpolls) {
		if(!$yyYaBBCloaded) { require "$sourcedir/YaBBC.pl"; }
		$message = $poll_question;
		&DoUBBC;
		$poll_question = $message;
	}

	$deletevote = "";
	if ($has_voted) {
		$footer = qq~$polltxt{'17'}: $totalvotes~;
		$width = qq~~;
		$deletevote = qq~<a href="$cgi;action=undovote;num=$pollnum">$img{'deletevote'}</a>~;
		if ($iamadmin) { $deletevote = $deletevote.$menusep; }
	} else {
		$footer = qq~<input type="submit" value="$polltxt{'18'}">~;
		$width = qq~ width="80%"~;
	}
	if ($iamguest) { $deletevote = ""; }
	if ($poll_locked) { $deletevote = ""; }

	$yymain .= qq~
<td valign="top"><font size="2">
  <table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="$color{'bordercolor'}" class="bordercolor" align="center">
    <tr>
      <td>
        <table cellpadding="3" cellspacing="1" border="0" width="100%">
          <tr>
            <td valign="top" align="center" bgcolor="$color{'windowbg2'}" class="windowbg2">
              <form name="poll" method="post" action="$scripturl?action=vote;num=$pollnum" style="display:inline">
                <table border="0" width="100%" cellspacing="0" cellpadding="0">
                  <tr>
                    <td colspan="2"><font size="2"><b>$polltxt{'16'}:</b> $poll_question</font><br><br></td>
                  </tr>$endedtext<tr>
                    <td colspan="2" align="center">~;

  	if ($has_voted && $hide_results && !$poll_locked) {

  		# Display Poll Hidden Message
  		 qq~<font size="2">$polltxt{'47'}</font><br><font size="1">($polltxt{'48'})</font><br>~;

  	} else {

  		$yymain .= qq~
                       <table$width border="0" cellspacing="2" cellpadding="0">~;

  		for($i = 0; $i < @options; $i++) {

  			unless ($options[$i]) {next;};

  			# Censor the options.
  			foreach (@censored) {
  				($tmpa,$tmpb) = @{$_};
  				$options[$i] =~ s~\Q$tmpa\E~$tmpb~gi;
  			}
  			$options[$i] =~ s~[\n\r]~~g;

  			if($ubbcpolls) {
  				if(!$yyYaBBCloaded) { require "$sourcedir/YaBBC.pl"; }
  				$message = $options[$i];
  				&DoUBBC;
  				$options[$i] = $message;
  			}

  			if ($has_voted) {

  				# Display Poll Results

  				$pollpercent = 0;
  				$pollbar = 0;
  				if ($totalvotes ne 0 && $maxvote ne 0) {
  					$pollpercent = int(1000 * $votes[$i] / $totalvotes);
  					$pollpercent = $pollpercent / 10;
  					$pollbar = int( 150 * $votes[$i] / $maxvote);
  				}

  				$yymain .= qq~
                        <tr>
                          <td valign="top"><font size="2"><b>$options[$i]&nbsp;&nbsp;</b></font></td>
                          <td valign="top" width="250" nowrap><font size="2">&nbsp;<img src="$imagesdir/poll_left.gif" align="absmiddle"><img src="$imagesdir/poll_middle.gif" height="12" width="$pollbar" align="absmiddle"><img src="$imagesdir/poll_right.gif" align="absmiddle"> $votes[$i] ($pollpercent%)</font></td>
                        </tr>~;

  			} else {

  				# Display Poll Options
  				if ($multi_vote) { $input = qq~<input type="checkbox" name="option$i" value="$i" class="poll">~; } else { $input = qq~<input type="radio" name="option" value="$i" class="poll">~; }

  				$yymain .= qq~
                        <tr>
                          <td valign="middle" width="25">$input</td>
                          <td valign="middle"><font size="2"><b>$options[$i]</b></font></td>
                        </tr>~;

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

  	$yymain .= qq~
                    </td>
                  </tr><tr>
                    <td colspan="2" align="center"><br><font size="2"><b>$footer</b></font></td>
                  </tr><tr>~;
  		if ($poll_comment ne "") {
  			$message = $poll_comment;
  			if($enable_ubbc) { if(!$yyYaBBCloaded) { require "$sourcedir/YaBBC.pl"; } &DoUBBC; }
  			$poll_comment = $message;
  			$yymain .= qq~
                    <td colspan="2"><br><font size="2">$poll_comment</font></td>
                  </tr><tr>~;}
  		$yymain .= qq~
                    <td valign="bottom"><br><font size="1"></font></td>
                    <td valign="bottom" align="right"><br><font size="1"></font></td>
                  </tr>
                </table>
              </form>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</td>
~;
}

#
# End of News
###########################################

1;