<?php

( include_once './mainfile.php' ) OR exit( '!! INCLUDE ERROR. ACCESS DENIED !!' );
error_reporting( _E_VALUE );

/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | myPHPNuke Version 188_8 RC2                                          |
// +----------------------------------------------------------------------+
// | License: GNU/GPL - http://www.gnu.org/copyleft/gpl.html              |
// +----------------------------------------------------------------------+
// | Support: http://myphpnuke.com/                                       |
// +----------------------------------------------------------------------+
// | Author: myPHPNuke Dev Team                                           |
// +----------------------------------------------------------------------+
// | Copyright: © 2001-2004 myPHPNuke Dev Team                            |
// +----------------------------------------------------------------------+
// $Id: index.php,v 1.15.2.7 2004/08/17 03:05:29 kirilt Exp $




/************************************************************************/
/* PHP-NUKE: Web Portal System                                          */
/* ===========================                                          */
/*                                                                      */
/* Copyright (c) 2001 by Francisco Burzi (fburzi@ncc.org.ve)            */
/* http://phpnuke.org                                                   */
/*                                                                      */
/* Modified by Kenneth Lee (kenneth@nexgear.com)                        */
/* [12-May-2001] Modified index.php to show next pages of stories.      */
/*               visitor able to control going to the next page,        */
/*               going back to the previous page, and returning to the  */
/*               top page when the visitor has reach the last page.     */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/

//* 2001-04-25 HTML code cleanup to meet XHTML spec, and added more \n formatting for easier to read output.
//* by: Larry Mingus (http://www.makeitsimple.com/)  and Bob McCambridge (http://www.islandncs.com/)

if ($mpn_set_timezone==1) {
   if ($mpn_host_use_safemode==1) {
           putenv("PHP_TZ=$mpn_timezone");
   } else {
           putenv("TZ=$mpn_timezone");
   }
}
$index = 1;
automatednews();

require './header.php';

function theindex($aftersid) {
    global $storyhome, $httpref, $httprefmax, $topicname, $topicimage, $topictext, $datetime, $user, $cookie;
    global $banners, $nobanner, $mpnTables, $phpEx, $sid, $index;

    $sidcount=0;    // for counting if we have reach the last stories
    $topsid=0;      // for storing the top sid
    $firstsid=0;    // for storing the sid of the first story on the page



/*
        if ($banners && !$nobanner) {
      include("banners.$phpEx");
    }
*/

    if (isset($cookie[3])) {
        $storynum = $cookie[3];
    } else {
        $storynum = $storyhome;
    }

    // get the top sid
    $result = mysql_query("SELECT sid FROM ".$mpnTables['stories']." ORDER BY sid DESC limit 1");
    if(!$result) {
        echo mysql_errno(). ": ".mysql_error(). "<br />"; exit();
    }
    while (list($s_sid) = mysql_fetch_row($result)) {
        $topsid=$s_sid;
    }

    if ($aftersid==0) {
        $result = mysql_query("SELECT sid, catid, aid, title, time, hometext, bodytext, comments, counter, topic, informant, notes FROM ".$mpnTables['stories']." WHERE (ihome='0' OR catid='0') ORDER BY sid DESC limit $storynum");
    } else {
        $result = mysql_query("SELECT sid, catid, aid, title, time, hometext, bodytext, comments, counter, topic, informant, notes FROM ".$mpnTables['stories']." WHERE ((ihome='0' OR catid='0') AND sid<$aftersid) ORDER BY sid DESC limit $storynum");
    }
    if(!$result) {
        echo mysql_errno(). ": ".mysql_error(). "<br />"; exit();
    }
    while (list($s_sid, $catid, $aid, $title, $time, $hometext, $bodytext, $comments, $counter, $topic, $informant, $notes) = mysql_fetch_row($result)) {

        if ($catid > 0) {
            list($cattitle) = mysql_fetch_row(mysql_query("select title from ".$mpnTables['stories_cat']." where catid='$catid'"));
        }
        if ($firstsid==0) { $firstsid=$s_sid; }
        $sidcount=$sidcount+1;
        $printP = "<a href=\"print.$phpEx?sid=$s_sid\"><img src=\"images/print.gif\" border=\"0\" alt=\"".translate("Printer Friendly Page")."\" width=\"15\" height=\"11\" /></a>&nbsp;";
        $sendF = "<a href=\"friend.$phpEx?op=FriendSend&amp;sid=$s_sid\"><img src=\"images/friend.gif\" border=\"0\" alt=\"".translate("Send this Story to a Friend")."\" width=\"15\" height=\"11\" /></a>";
        getTopics($s_sid);

        // Kiril: New Sanitizer

        $sanity_ary = compact( 'title', 'time', 'hometext', 'bodytext', 'notes' );
        $_SERVER['SAN'] -> slashIT( $sanity_ary );
        extract( $sanity_ary );

        // Adjust Formatting

        if ( $_SERVER['F_LIB'] -> checkPostDate( $time ) )
        {
            $slice_ary = array_slice( $sanity_ary, 2, 3 );
            $_SERVER['SAN'] -> sanitizeLegacyContent( $slice_ary );
        }
        else
        {
            $slice_ary = array_slice( $sanity_ary, 2, 3 );
            $_SERVER['SAN'] -> adjustFormat( $slice_ary, 3, 10 );
        }

        extract( $slice_ary );
        unset( $slice_ary, $sanity_ary );

        // End New Sanitizer

        $datetime = formatTimestamp($time);

        //$datetime = strftime( translate( 'emaildatestring' ) , strtotime( $time ) );

        $introcount = strlen($hometext);
        $fullcount = strlen($bodytext);
        $totalcount = $introcount + $fullcount;
        $morelink = "";

        if ($fullcount > 1) {
            $morelink .= "<a href=\"article.$phpEx?sid=$s_sid";
            if (isset($cookie[4])) { $morelink .= "&amp;mode=$cookie[4]"; } else { $morelink .= "&amp;mode=thread"; }
            if (isset($cookie[5])) { $morelink .= "&amp;order=$cookie[5]"; } else { $morelink .= "&amp;order=0"; }
            $morelink .= "\"><b>".translate("Read More...")."</b></a> | $totalcount ".translate("bytes more")." | "; }
            $count = $comments;
            $morelink .= "<a href=\"article.$phpEx?sid=$s_sid";
            if (isset($cookie[4])) { $morelink .= "&amp;mode=$cookie[4]"; } else { $morelink .= "&amp;mode=thread"; }
            if (isset($cookie[5])) { $morelink .= "&amp;order=$cookie[5]"; } else { $morelink .= "&amp;order=0"; }
            if (isset($cookie[6])) { $morelink .= "&amp;thold=$cookie[6]"; } else { $morelink .= "&amp;thold=0"; }
            $morelink2 = "<a href=\"article.$phpEx?sid=$s_sid";
            if (isset($cookie[4])) { $morelink2 .= "&amp;mode=$cookie[4]"; } else { $morelink2 .= "&amp;mode=thread"; }
            if (isset($cookie[5])) { $morelink2 .= "&amp;order=$cookie[5]"; } else { $morelink2 .= "&amp;order=0"; }
            if (isset($cookie[6])) { $morelink2 .= "&amp;thold=$cookie[6]"; } else { $morelink2 .= "&amp;thold=0"; }
            if(($count==0)) {
                if ($catid > 0) {
                    $morelink .= "\">".translate("comments?")."</a> | $printP $sendF | <a href=categories.$phpEx?op=newindex&amp;catid=$catid>$cattitle</a>";
                } else {
                    $morelink .= "\">".translate("comments?")."</a> | $printP $sendF";
                }
        } else {
            if (($fullcount<1)) {
                if(($count==1)) {
                    if ($catid > 0) {
                        $morelink .= "\"><b>".translate("Read More...")."</b></a> | $morelink2\">$count ".translate("comment")."</a> | $printP $sendF | <a href=categories.$phpEx?op=newindex&amp;catid=$catid>$cattitle</a>";
                    } else {
                        $morelink .= "\"><b>".translate("Read More...")."</b></a> | $morelink2\">$count ".translate("comment")."</a> | $printP $sendF ";
                    }
                } else {
                    if ($catid > 0) {
                        $morelink .= "\"><b>".translate("Read More...")."</b></a> | $morelink2\">$count ".translate("comments")."</a> | $printP $sendF | <a href=categories.$phpEx?op=newindex&amp;catid=$catid>$cattitle</a>";
                    } else {
                        $morelink .= "\"><b>".translate("Read More...")."</b></a> | $morelink2\">$count ".translate("comments")."</a> | $printP $sendF";
                    }
                }
            } else {
                if(($count==1)) {
                    if ($catid > 0) {
                        $morelink .= "\">$count ".translate("comment")."</a> | $printP $sendF | <a href=categories.$phpEx?op=newindex&amp;catid=$catid>$cattitle</a>";
                    } else {
                        $morelink .= "\">$count ".translate("comment")."</a> | $printP $sendF ";
                    }
                } else {
                    if ($catid > 0) {
                        $morelink .= "\">$count ".translate("comments")."</a> | $printP $sendF | <a href=categories.$phpEx?op=newindex&amp;catid=$catid>$cattitle</a>";
                    } else {
                        $morelink .= "\">$count ".translate("comments")."</a> | $printP $sendF ";
                    }
                }
            }
        }
        $sid = $s_sid;
        if ($catid != 0) {
            $resultm = mysql_query("select title from ".$mpnTables['stories_cat']." where catid='$catid'");
            list($title1) = mysql_fetch_row($resultm);
            $title = "<a href=\"categories.$phpEx?op=newindex&amp;catid=$catid\">$title1</a>: $title";
        }
        themeindex($aid, $informant, $datetime, $title, $counter, $topic, $hometext, $notes, $morelink, $topicname, $topicimage, $topictext);
    }

    // display the previous, next page links
    echo "<table border=\"0\" width=\"100%\">";
    echo "<tr><td align=\"left\">";
    // check whether to display previous page link
    if (($firstsid!=$topsid) AND ($aftersid!=0)) {
        echo "<b><a href=\"index.$phpEx?op=show&aftersid=".($aftersid+$storynum)."\">".translate("< Previous Page")."</a></b>";
    }
    echo "</td><td align=\"right\">";
    // check whether to display next page or return to top link
    if (($sidcount==$storynum) AND ($sid!=1)) {
        echo "<b><a href=\"index.$phpEx?op=show&aftersid=$sid\">".translate("Next Page >")."</a></b>";
    } else {
        echo "<b><a href=\"index.$phpEx\">".translate("<< Return to Top")."</a></b>";
    }
    echo "</td></tr></table>";

    mysql_free_result($result);
    if ($httpref==1) {

       $referer = getenv("HTTP_REFERER");
       if ($referer=="" OR eregi("^unknown", $referer) # case insensitive can't be bad
         OR substr( $referer,0,strlen( _MPN_ROOT_URI )) == _MPN_ROOT_URI # or use eregi()
         OR eregi("^bookmark", $referer)) { # "bookmark" is not more interesting than "unknown"
    }
    else {
      $result = mysql_query("select * from ".$mpnTables['referer']);
      $numrows = mysql_num_rows($result);
      if (($numrows + 1) >= $httprefmax) { # GREATER or EQUAL !
        mysql_query("delete from ".$mpnTables['referer']);
      }
      mysql_query("insert into ".$mpnTables['referer']." values (NULL, '$referer')");
    }
  }
    @( include_once __ROOT__ . 'footer.php' );
}


function whatsnew() {
    global $mpn_whatsnew_entries;
    global $anonymous, $mpn_anonymous_id;
    global $bgcolor1, $bgcolor2, $bgcolor3;
    global $index,$mpnTables, $phpEx;

    $index = 0;

    $boxstuff = "";
    $boxTitle = translate("What's new");

    $query="SELECT topic_id, topic_title, topic_time, topic_views, t.forum_id FROM ".$mpnTables['forumtopics']." t, ".$mpnTables['forums']." f where (f.forum_id=t.forum_id) and (forum_type <> '1') ORDER BY topic_time desc LIMIT " . $mpn_whatsnew_entries;

    $result=mysql_query($query);
    if (mysql_num_rows($result) > 0) {
        $boxstuff .= "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\" bgcolor=\"$bgcolor2\">\n<tr><td>\n";
        $boxstuff .= "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"8\" bgcolor=\"#ffffff\">\n";
        $boxstuff .= "<tr bgcolor=\"$bgcolor1\"><td colspan=\"3\" align=\"center\"><b>" .translate("Forum Topics")."</b></td></tr>";
        $boxstuff .="<tr>\n<td><b>" .translate("Topic")."</b></td>\n<td align=\"center\"><b>" .translate("Views")."</b></td><td align=\"right\"><b>" .translate("Date/Time")."</b></td>\n</tr>\n";
        while ($array=mysql_fetch_array($result)) {
            $boxstuff .= "<tr>\n<td><a href=\"viewtopic.$phpEx?topic=" . $array['topic_id'] . "&amp;forum=" . $array['forum_id'] . "\">";
            $boxstuff .= StripSlashes($array['topic_title']);
            $boxstuff .= "</a></td>\n<td align=\"center\">".$array['topic_views']."</td>\n<td align=\"right\">";
            $boxstuff .= strftime(translate("forumdatestring2"),strtotime($array['topic_time'])). "</td>\n</tr>\n";
        }
        $boxstuff .= "</table>\n\n</td></tr>\n";
        $boxstuff .= "</table>\n";
        $boxstuff .= "<br />\n";
    }

    $query="SELECT sid, title, time FROM ".$mpnTables['stories']." ORDER BY time desc LIMIT " . $mpn_whatsnew_entries;
    $result=mysql_query($query);
    if (mysql_num_rows($result) > 0) {
        $boxstuff .= "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\" bgcolor=\"$bgcolor2\">\n<tr><td>\n";
        $boxstuff .= "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"8\" bgcolor=\"ffffff\">\n";
        $boxstuff .= "<tr bgcolor=\"$bgcolor1\"><td colspan=\"2\" align=\"center\"><b>" .translate("Articles")."</b></td></tr>\n";
        while ($array=mysql_fetch_array($result)) {
            $title = stripslashes( $array["title"] );
            $boxstuff .= "<tr><td align=\"left\"><a href=\"article.$phpEx?sid=".$array["sid"]."&amp;mode=&amp;order=0&amp;thold=0\">$title</a></td>\n";
            $boxstuff .= "<td align=\"right\">". strftime(translate("forumdatestring2"),strtotime($array["time"])) . "</td></tr>\n";
        }
        $boxstuff .= "</table>\n</td></tr>\n";
        $boxstuff .= "</table>\n";
        $boxstuff .= "<br />";
    }

    $query="SELECT g.guest_id, g.guest_title, g.guest_poster, g.guest_time, g.guest_views, u.uname FROM ".$mpnTables['guestbook']." g, ".$mpnTables['users']." u WHERE u.uid = g.guest_poster && u.uid > 1 ORDER BY g.guest_time desc LIMIT " . $mpn_whatsnew_entries;
    $result=mysql_query($query);
    if (mysql_num_rows($result) > 0) {
        $boxstuff .= "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\" bgcolor=\"$bgcolor2\"><tr><td>\n";
        $boxstuff .= "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"8\" bgcolor=\"ffffff\">\n";
        $boxstuff .= "<tr bgcolor=\"$bgcolor1\"><td colspan=\"3\" align=\"center\"><b>" .translate("Guestbook")."</b></td></tr>\n";
        while ($array=mysql_fetch_array($result)) {
            $boxstuff .= "<tr>\n<td align=\"left\"><a href=\"guestbook.$phpEx\">". StripSlashes($array["guest_title"]) . "</a></td>\n";

            if ($array["guest_poster"] == $mpn_anonymous_id)
            {
                $boxstuff .= "<td align=\"center\">". $anonymous . "</td>\n";
            }
            else
            {
                $boxstuff .= "<td align=\"center\"><a href=\"user.$phpEx?op=userinfo&amp;uname=". $array["uname"] . "\">".$array["uname"]."</a></td>\n";
            }
            $boxstuff .= "<td align=\"right\">". strftime(translate("forumdatestring2"),strtotime($array["guest_time"])) . "</td>\n</tr>\n";

        }
        $boxstuff .= "</table>\n</td></tr>\n";
        $boxstuff .= "</table>\n";
        $boxstuff .= "<br />\n";

    }

    themecenterposts($boxTitle, $boxstuff);

    @( include_once __ROOT__ . 'footer.php' );
}

if (!isset($op))
{
    $op = null;
}

switch ($op) {

    case "show":
        theindex($aftersid);
        break;

    case "whatsnew":
        whatsnew();
        break;

    default:
        theindex(0);
        break;

}

?>
