#!/usr/bin/perl

#############################################################
# based on Ikonboard v2.1 by Ikonboard.com
#
# расчищал и исправлял Svarga
# http://forum.ru-board.com/
#############################################################

use CGI::Carp "fatalsToBrowser";          # Output errors to browser
use CGI qw(:standard);                    # Saves loads of work
$CGI::POST_MAX=1024 * 150;                # limit post data
$CGI::DISABLE_UPLOADS = 1;                # Disable uploads

# checking, if the client requests the script
if(param('client') && 1 == param('client')){

eval {
($0 =~ m,(.*)/[^/]+,)   and unshift (@INC, "$1");
($0 =~ m,(.*)\\[^\\]+,) and unshift (@INC, "$1");
require "cl_ikon.lib";       # Require ikonboard ()
require "data/progs.cgi";    # Require prog names
require "data/boardinfo.cgi";# Require board info
require "data/styles.cgi";   # Require styles info
};
if ($@) {
	print header('text/html; charset=windows-1251');
	print "{$ibtxt{'0026'} $@\n$ibtxt{'0027'}}";
	exit;
	}

$|++;                                     # Unbuffer the output

#################--- Begin the program ---###################

$thisprog = "cl_forums.cgi";


$query = new CGI;

$cookiepath     = $query->url(-absolute=>1);
$cookiepath     =~ s/$thisprog//sg;
$bypass         = $query -> param('bypass');
$inforum        = $query -> param('forum');
$intopic        = $query -> param('topic');
$action         = $query -> param('action');
$inshow         = $query -> param('show');
$jumpto         = $query -> param('jumpto');

#client
$client		= $query -> param('client');

die "Hack attempt!" unless $inforum =~ m!\A\d{1,3}\Z!;
die "Hack attempt!" if $intopic && $intopic !~ m!\A\d{1,7}\Z!;


$inmembername   = cookie("amembernamecookie");
$inpassword     = cookie("apasswordcookie");

$bypass         = &stripMETA("$bypass");
$inforum        = &stripMETA("$inforum");
$intopic        = &stripMETA("$intopic");
$action         = &stripMETA("$action");
$inshow         = &stripMETA("$inshow");
$jumpto         = &stripMETA("$jumpto");


if ((!$inmembername) or ($inmembername eq "$ibtxt{'0043'}")) {
    $inmembername = "$ibtxt{'0043'}";
    }
    else {
        &getmemberstime("$inmembername");
        &getlastvisit;
        $forumlastvisit = $lastvisitinfo{$inforum};
        $currenttime = time;
        &setlastvisit("$inforum,$currenttime");
        }


if ($action eq "accessrequired") {
    $inmembername  = $query -> param('membername');
    $inpassword    = $query -> param('password');
    &getmember("$inmembername");
    if ($userregistered ne "no" && $allowedentry{$inforum} eq "yes" && $inpassword eq $password) {
        $allowcookiename = "forumsallowed" . "$inforum";
        print "Set-Cookie: $allowcookiename=yes\;";
        print "\n";
        $allowed = "yes";
        }
        else {
            $allowed = "no";
            }
        }
        else {
            $tempaccess = "forumsallowed". "$inforum";
            $testentry = cookie("$tempaccess");
            if ($testentry eq "yes") { $allowed = "yes"; }
            else { $allowed = "no"; }
            }




if ($action eq "resetposts") {
    $currenttime = time;
    $currenttime = ($currenttime+10);
    $mv=1; &setlastvisit("$inforum,$currenttime");
    print redirect(-location=>"$boardurl/$thisprog?forum=$inforum",
                   -cookie=>[$tempvisitcookie, $permvisitcookie]);
    exit;
    }

print header(-cookie=>[$tempvisitcookie, $permvisitcookie]);

&forumjump;

if ($jumpto) {
    print redirect(-location=>"$jumpto");
    exit;
    }


        &getforum("$inforum");

        &moderator;

        &title;

    $filetoopen = "$ikondir" . "forum$inforum/list.cgi";
    $filetoopen = &stripMETA($filetoopen);
    if (-e $filetoopen) {
        open FILE, "$filetoopen" or &error("$ibtxt{'0618'}&$ibtxt{'0619'}");
        flock FILE, 2;
        @topics = <FILE>;
        close FILE;
        }

    # Limit the total topics to a span

    $numberofitems = @topics;


        # Print the header

	$output .= "$forumname|$uservisitdata|$numberofitems\n\n";

    # Check if it's a private forum, and is the member cleared?

    if (("$privateforum" eq "yes") && ("$allowed" ne "yes"))  { &accessneeded; }

    if (($inmembername) && ($privateforum ne "yes")) { &whosonline("$inmembername|$ibtxt{'0617'} <a href=\"$forumsprog?forum=$inforum\"><b>$forumname</b></a>|$ibtxt{'1602'}"); }

    # Open up the forum threads list


    $maxtopics = 15;
    $numberofpages = $numberofitems / $maxtopics;

    if ($numberofitems > $maxtopics) { #if
        $showmore = "yes";
        if ($inshow eq "" || $inshow < 0) { $inshow = 0; }
        if ($inshow > 0) { $startarray = $inshow; }
            else { $startarray = 0; }
            $endarray = $inshow + $maxtopics - 1;
            if ($endarray < ($numberofitems - 1)) { $more = "yes"; }
            elsif (($endarray > ($maxtopics - 1)) && ($more ne "yes")) { $endarray = $numberofitems - 1; }
            } #
            else {
                $showmore = "no";
                $startarray = 0;
                $toptopicpages = $topicpages;
                $endarray = @topics - 1;
                }


        # Grab the threads to show

        foreach $topic (@topics[$startarray ... $endarray]) { # start topic foreach
                chomp $topic;
                ($topicid, $topictitle, $topicdescription, $threadstate, $threadposts, $threadviews, $startedby, $startedpostdate, $lastposter, $lastpostdate) = split(/\|/,$topic);




        # Span time, yet again. Who needs sub-routes?

        $numberofitems = $threadposts + 1;
        $numberofpages = $numberofitems / $maxthreads;
if(!$client){
        if ($numberofitems > $maxthreads) { #if

            if ($maxthreads < $numberofitems) { #2
                ($integer,$decimal) = split(/\./,$numberofpages);
                    if ($decimal > 0) { $numberofpages = $integer + 1; }
                        $pagestart = 0;
                        $counter = 0;
                        while ($numberofpages > $counter) { #3
                            $counter++;
                            $threadpages .= qq~<a href="$threadprog?forum=$inforum&topic=$topicid&start=$pagestart"> $counter </a>~;
                            $pagestart = $pagestart + $maxthreads;
                            } #e3
                        } #e2
                    $pagestoshow = qq~<font face="$font" color=$forumfontcolor size=$dfontsize1> &nbsp;[ $ibtxt{'0605'}&nbsp;$threadpages ]~;
                    } #1
}

                        if ($lastpostdate ne "") {
                        $lastpostdate = $lastpostdate + ($timedifferencevalue*3600) + ($timezone*3600);
                        $longdate = &longdate("$lastpostdate");
                        $shorttime = &shorttime("$lastpostdate");
                        $lastpostdate =$client ? "$longdate" : qq~<font size="$dfontsize1" face="$font" color="$fontcolormisc">$longdate - <font size="$dfontsize1" face="$font" color="$fontcolormisc">$shorttime~;
                        }
                        else {
                                $lastpostdate = '';
                                $lastpoststamp = '';
                                }
                        $startedpostdate = $startedpostdate + ($timedifferencevalue*3600) + ($timezone*3600);
                        $startedlongdate = &shortdate("$startedpostdate");
                        $startedshorttime = &shorttime("$startedpostdate");
                        $startedpostdate = $startedlongdate;

                        $topictitle ="$inforum\|$topicid\|$topictitle";

                        $startedbyfilename = $startedby;
                        $startedbyfilename =~ s/ /\_/isg;

                        $lastposterfilename = $lastposter;
                        $lastposterfilename =~ s/ /\_/isg;

                        if ($topicdescription) { $topicdescription = $client ? $topicdescription :qq~&nbsp;&nbsp;&raquo;$topicdescription~; }

                        $output .= "$topictitle|$topicdescription|$threadposts|$startedby|$lastpostdate|$lastposter\n";
                        $pagestoshow = undef;
                        $threadpages = undef;

                } # end topic foreach

&output($output);



#### Access needed form

sub accessneeded {
        $output .= '{login needed}';
        &output($output);
        }
}
# if the script is requested without parameter client=1, open a non-client
# version of the script:
else{
	require 'forums.cgi' or die "Content-Type: text/html; charset=windows-1251\n\nБеда какая-то с forums.cgi: $!\n:(";
	}

1;