<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* 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.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
defined('MAXIMUS') OR die('Access denied articles directly');

if (is_file('modules/News/includes/functions.php')) {
require_once('modules/News/includes/functions.php');}
$optionbox = "";
$module_name = "News";
get_lang($module_name);

	$multilingual = $db->XMFbin( $multilingual );
	$currentlang  = $db->XMFlang( $currentlang );

global $save;
if (stristr($_SERVER['REQUEST_URI'],"mainfile")) {
    redirect_url(" modules.php?name=$module_name&amp;file=article&amp;sid=$sid");
} elseif (!isset($sid) && !isset($tid)) {
    redirect_url(" index.php");
}
if (isset($sid)) $sid=$GLOBALS['db']->XMFnum($sid);
if (isset($tid)) $tid=$GLOBALS['db']->XMFnum($tid);
if ($save AND defined('USER_ALLOW')) {
    cookiedecode($user);
    $GLOBALS['db']->sql_query("UPDATE `".TABLE_USERS."` SET `umode`='$mode', `uorder`='$order', `thold`='$thold' where `uid`='$cookie[0]'");
    getusrinfo($user);
    $info = base64_encode("$userinfo[user_id]:$userinfo[username]:$userinfo[user_password]:$userinfo[storynum]:$userinfo[umode]:$userinfo[uorder]:$userinfo[thold]:$userinfo[noscore]");
      @setcookie("user","$info",time()+$cookieusrtime);
}

if ($op == "Reply") {
    redirect_url(" modules.php?name=$module_name&amp;file=comments&amp;op=Reply&amp;pid=0&amp;sid=$sid&amp;mode=$mode&amp;order=$order&amp;thold=$thold");
}

$sql = "SELECT `catid`, `aid`, `time`, `title`, `hometext`, `bodytext`, `topic`, `informant`, `notes`, `acomm`, `haspoll`, `pollID`, `score`, `ratings` FROM `".TABLE_STORIES."` WHERE `sid`='$sid'";
	    if ( !($result = $GLOBALS['db']->sql_query($sql)) )
{ query_die(CORE_ERROR, 'Could not SELECT _stories', '', __LINE__, __FILE__, $sql);}
if ($numrows = $GLOBALS['db']->sql_numrows($result) != 1) {
    redirect_url(" index.php");
    die();
}
$row       = $GLOBALS['db']->sql_fetchrow($result);
$catid     = $GLOBALS['db']->XMFnum($row['catid']);
$aid       = stripslashes($row['aid']);
$time      = stripslashes($row['time']);
$title     = stripslashes(check_html($row['title'], "nohtml"));
$hometext  = stripslashes($row['hometext']);
$bodytext  = stripslashes($row['bodytext']);
$topic     = $GLOBALS['db']->XMFnum($row['topic']);
$informant = stripslashes($row['informant']);
$notes     = stripslashes($row['notes']);
$acomm     = $GLOBALS['db']->XMFnum($row['acomm']);
$haspoll   = $GLOBALS['db']->XMFnum($row['haspoll']);
$pollID    = $GLOBALS['db']->XMFnum($row['pollID']);
$score     = $GLOBALS['db']->XMFnum($row['score']);
$ratings   = $GLOBALS['db']->XMFnum($row['ratings']);

if ($aid == "") {
    redirect_url(" modules.php?name=$module_name");
}

$GLOBALS['db']->sql_query("UPDATE `".TABLE_STORIES."` SET `counter`=counter+1 where `sid`='$sid'");
$artpage = 1;
$pagetitle = "- $title";

if (defined('ADMIN_ALLOW')) {
	$article_ret  = CACHE_AANEWS;
	$name_article = "ArticleAdmin$sid";
	$article_extension = "php";
} else {
	$article_ret  = CACHE_VANEWS;
	$name_article = "Article$sid";
	$article_extension = "html";
}

incoh( "".$name_article."", "".$article_extension."",  "".$article_ret."", '2');

$artpage  = 0;
formatTimestamp($time);
$title    = stripslashes(check_html($title, "nohtml"));
$hometext = stripslashes($hometext);
$bodytext = stripslashes($bodytext);
$notes    = stripslashes($notes);

if ($notes != "") {
    $notes = "<br /><br /><b>"._NOTE."</b> <i>$notes</i>";
} else {
    $notes = "";
}

if($bodytext == "") {
    $bodytext = "$hometext$notes";
} else {
    $bodytext = "$hometext<br /><br />$bodytext$notes";
}

if($informant == "") {
    $informant = $anonymous;
}

getTopics($topic);

if ($catid != 0) {
    $sql2 = "select `title` from `".TABLE_STORIES_CAT."` where `catid`='$catid'";
	    if ( !($result2 = $GLOBALS['db']->sql_query($sql2)) )
{ query_die(CORE_ERROR, 'Could not SELECT _stories_cat', '', __LINE__, __FILE__, $sql2);}
    $row2   = $GLOBALS['db']->sql_fetchrow($result2);
    $title1 = stripslashes(check_html($row2['title'], "nohtml"));
    $title  = "<a href=\"modules.php?name=$module_name&amp;file=categories&amp;op=newindex&amp;catid=$catid\"><font class=\"storycat\">$title1</font></a>: $title";
}

echo "<table width='100%' border='0'><tr><td valign='top' width='100%'>\n";

themearticle($aid, $informant, $datetime, $title, $bodytext, $topic, $topicname, $topicimage, $topictext);

include_once('modules/News/associates.php');

if ((($mode != "nocomments") OR ($acomm == 0)) OR ($articlecomm == 1)) {
    include_once('modules/News/comments.php');
}
echo "</td><td>&nbsp;</td><td valign=\"top\">\n";

if ($multilingual == 1) {
    $querylang = "AND (`blanguage`='$currentlang' OR `blanguage`='')";
} else {
    $querylang = "";
}

/* Determine if the article has attached a poll */
if ($haspoll == 1) {
    $url = sprintf("modules.php?name=Surveys&amp;op=results&amp;pollID=%d", $pollID);
    $boxContent  = "<form action=\"modules.php?name=Surveys\" method=\"post\">";
    $boxContent .= "<input type=\"hidden\" name=\"pollID\" value=\"".$pollID."\" />";
    $boxContent .= "<input type=\"hidden\" name=\"forwarder\" value=\"".$url."\" />";
    $sql3 = "SELECT `pollTitle`, `voters` FROM `".TABLE_POLL_DESC."` WHERE `pollID`='$pollID'";
	    if ( !($result3 = $GLOBALS['db']->sql_query($sql3)) )
{ query_die(CORE_ERROR, 'Could not SELECT _poll_desc', '', __LINE__, __FILE__, $sql3);}
    $row3 = $GLOBALS['db']->sql_fetchrow($result3);
    $pollTitle   = stripslashes(check_html($row3['pollTitle'], "nohtml"));
    $voters      = stripslashes($row3['voters']);
    $boxTitle    = _ARTICLEPOLL;
    $boxContent .= "<font class=\"content\"><b>$pollTitle</b></font><br /><br />\n";
    $boxContent .= "<table border=\"0\" width=\"100%\">";
    for($i = 1; $i <= 12; $i++) {
	$sql4 = "SELECT `pollID`, `optionText`, `optionCount`, `voteID` FROM `".TABLE_POLL_DATA."` WHERE (`pollID`='$pollID') AND (`voteID`='$i')";
	    if ( !($result4 = $GLOBALS['db']->sql_query($sql4)) )
{ query_die(CORE_ERROR, 'Could not obtain _poll_data', '', __LINE__, __FILE__, $sql4);}
	$row4    = $GLOBALS['db']->sql_fetchrow($result4);
	$numrows = $GLOBALS['db']->sql_numrows($result4);
	if($numrows != 0) {
	    $optionText = stripslashes($row4['optionText']);
	    if($optionText != "") {
		$boxContent .= "<tr><td valign=\"top\"><input type=\"radio\" name=\"voteID\" value=\"".$i."\" /></td><td width=\"100%\"><font class=\"content\">$optionText</font></td></tr>\n";
	    }
	}
    }
    $boxContent .= "</table><br /><center><font class=\"content\"><input type=\"submit\" value=\""._VOTE."\" /></font><br />";
    if (defined('USER_ALLOW')) {
        cookiedecode($user);
    }
    for($i = 0; $i < 12; $i++) {
	$sql5 = "SELECT `optionCount` FROM `".TABLE_POLL_DATA."` WHERE (`pollID`='$pollID') AND (`voteID`='$i')";
    if ( !($result5 = $GLOBALS['db']->sql_query($sql5)) )
{ query_die(CORE_ERROR, 'Could not obtain _poll_data', '', __LINE__, __FILE__, $sql5);}
	$row5 = $GLOBALS['db']->sql_fetchrow($result5);
	$optionCount = stripslashes($row5['optionCount']);
	$sum = (int)$sum+$optionCount;
    }
    $boxContent .= "<font class=\"content\">[ <a href=\"modules.php?name=Surveys&amp;op=results&amp;pollID=$pollID&amp;mode=$cookie[4]&amp;order=$cookie[5]&amp;thold=$cookie[6]\"><b>"._RESULTS."</b></a> | <a href=\"modules.php?name=Surveys\"><b>"._POLLS."</b></a> ]<br />";

    if ($pollcomm) {
	$sql6 = "SELECT * FROM `".TABLE_POLLCOMMENTS."` WHERE `pollID`='$pollID'";
    if ( !($result6 = $GLOBALS['db']->sql_query($sql6)) )
{ query_die(CORE_ERROR, 'Could not obtain _pollcomments', '', __LINE__, __FILE__, $sql6);}
	$numcom  = $GLOBALS['db']->sql_numrows($result6);
	$boxContent .= "<br />"._VOTES.": <b>$sum</b><br />"._PCOMMENTS." <b>$numcom</b>\n\n";
    } else {
        $boxContent .= "<br />"._VOTES." <b>$sum</b>\n\n";
    }
    $boxContent .= "</font></center></form>\n\n";
    themesidebox($boxTitle, $boxContent);
}

$sql7 = "SELECT `title`, `content`, `active`, `bposition` FROM `".TABLE_BLOCKS."` WHERE `blockfile`='block-Login.php' $querylang";
    if ( !($result7 = $GLOBALS['db']->sql_query($sql7)) )
{ query_die(CORE_ERROR, 'Could not obtain _blocks', '', __LINE__, __FILE__, $sql7);}
$row7 = $GLOBALS['db']->sql_fetchrow($row7);
$title    = stripslashes(check_html($row7['title'], "nohtml"));
$content  = stripslashes($row7['content']);
$active   = $GLOBALS['db']->XMFnum($row7['active']);
$position = stripslashes($row7['bposition']);
$position = substr("$position", 0,1);
if (($active == 1) AND ($position == "r") AND (!defined('USER_ALLOW'))) {
    loginbox();
}


$boxtitle = ""._RELATED."";
$boxstuff = "<font class=\"content\">";
$sql8 = "SELECT `name`, `url` FROM `".TABLE_RELATED."` WHERE `tid`='$topic'";
    if ( !($result8 = $GLOBALS['db']->sql_query($sql8)) )
{ query_die(CORE_ERROR, 'Could not obtain _related', '', __LINE__, __FILE__, $sql8);}
while ($row8  = $GLOBALS['db']->sql_fetchrow($result8)) {
	$name = stripslashes($row8['name']);
	$url  = stripslashes($row8['url']);
   $boxstuff .= "<strong><big>&middot;</big></strong>&nbsp;<a href=\"$url\" target=\"new\">$name</a><br />\n";
}

$boxstuff .= "<strong><big>&middot;</big></strong>&nbsp;<a href=\"modules.php?name=Search&amp;topic=$topic\">"._MOREABOUT." $topictext</a><br />\n";
$boxstuff .= "<strong><big>&middot;</big></strong>&nbsp;<a href=\"modules.php?name=Search&amp;author=$aid\">"._NEWSBY." $aid</a>\n";

$boxstuff .= "<br /><hr width=\"95%\" size=\"1\" /><center><font class=\"content\"><b>"._MOSTREAD." $topictext:</b><br />\n";

global $multilingual, $currentlang, $admin_file;
    if ($multilingual == 1) {
	$querylang = "AND (alanguage='$currentlang' OR alanguage='')";
    } else {
	$querylang = "";
    }
$row9 = $GLOBALS['db']->sql_fetchrow($GLOBALS['db']->sql_query("SELECT `sid`, `title` FROM `".TABLE_STORIES."` WHERE `topic`='$topic' $querylang ORDER BY `counter` DESC LIMIT 0,1"));
$topstory = $GLOBALS['db']->XMFnum($row9['sid']);
$ttitle   = stripslashes(check_html($row9['title'], "nohtml"));

$boxstuff .= "<a href=\"modules.php?name=$module_name&amp;file=article&amp;sid=$topstory\">$ttitle</a></font></center><br />\n";
themesidebox($boxtitle, $boxstuff);

if ($ratings != 0) {
    $rate = substr($score / $ratings, 0, 4);
    $r_image = round($rate);
	if ($r_image == 1) {
    		$the_image = "<br /><br /><img src=\"modules/News/images/rating/stars-1.png\" border=\"1\" alt=\"\" /></center><br />";
	} elseif ($r_image == 2) {
    		$the_image = "<br /><br /><img src=\"modules/News/images/rating/stars-2.png\" border=\"1\" alt=\"\" /></center><br />";
	} elseif ($r_image == 3) {
    		$the_image = "<br /><br /><img src=\"modules/News/images/rating/stars-3.png\" border=\"1\" alt=\"\" /></center><br />";
	} elseif ($r_image == 4) {
    		$the_image = "<br /><br /><img src=\"modules/News/images/rating/stars-4.png\" border=\"1\" alt=\"\" /></center><br />";
	} elseif ($r_image == 5) {
    		$the_image = "<br /><br /><img src=\"modules/News/images/rating/stars-5.png\" border=\"1\" alt=\"\" /></center><br />";
	}
} else {
    $rate = 0;
    $the_image = "</center><br />";
}

$ratetitle = ""._RATEARTICLE."";
$ratecontent = "<center>"._AVERAGESCORE.": <b>$rate</b><br />"._VOTES.": <b>$ratings</b>$the_image";
$ratecontent .= "<form action=\"modules.php?name=$module_name\" method=\"post\"><center>"._RATETHISARTICLE."</center><br />";
$ratecontent .= "<input type=\"hidden\" name=\"sid\" value=\"$sid\" />";
$ratecontent .= "<input type=\"hidden\" name=\"op\" value=\"rate_article\" />";
$ratecontent .= "<input type=\"radio\" name=\"score\" value=\"5\" /> <img src=\"modules/News/images/rating/stars-5.png\" border=\"0\" alt=\""._EXCELLENT."\" title=\""._EXCELLENT."\" /><br />";
$ratecontent .= "<input type=\"radio\" name=\"score\" value=\"4\" /> <img src=\"modules/News/images/rating/stars-4.png\" border=\"0\" alt=\""._VERYGOOD."\" title=\""._VERYGOOD."\" /><br />";
$ratecontent .= "<input type=\"radio\" name=\"score\" value=\"3\" /> <img src=\"modules/News/images/rating/stars-3.png\" border=\"0\" alt=\""._GOOD."\" title=\""._GOOD."\" /><br />";
$ratecontent .= "<input type=\"radio\" name=\"score\" value=\"2\" /> <img src=\"modules/News/images/rating/stars-2.png\" border=\"0\" alt=\""._REGULAR."\" title=\""._REGULAR."\" /><br />";
$ratecontent .= "<input type=\"radio\" name=\"score\" value=\"1\" /> <img src=\"modules/News/images/rating/stars-1.png\" border=\"0\" alt=\""._BAD."\" title=\""._BAD."\" /><br /><br />";
$ratecontent .= "<center><input type=\"submit\" value=\""._CASTMYVOTE."\" /></center></form>";
themesidebox($ratetitle, $ratecontent);

$optiontitle = ""._OPTIONS."";
$optionbox = "<br />";
$optionbox .= "&nbsp;<img src='modules/News/images/print.png' border='0' alt='"._PRINTER."' title='"._PRINTER."' /> <a href=\"modules.php?name=$module_name&amp;file=print&amp;sid=$sid\" target='_blank'>"._PRINTER."</a><br /><br />";
$optionbox .= "&nbsp;<img src=\"modules/News/images/pdf.png\" border=\"0\" alt=\""._PDF."\" title=\""._PDF."\" width=\"16\" height=\"16\" /><a href=\"modules.php?name=$module_name&amp;op=pdf&amp;sid=".$sid."\" target=\"_blank\"> "._PDF."</a><br /><br />";
$optionbox .= "&nbsp;<img src='modules/News/images/friend.png' border='0' alt='"._FRIEND."' title='"._FRIEND."' /> <a href=\"modules.php?name=$module_name&amp;file=friend&amp;op=FriendSend&amp;sid=$sid\">"._FRIEND."</a><br /><br /></center>\n";
if (defined('ADMIN_ALLOW')) {
    $optionbox .= "<center><b>"._ADMIN."</b><br />[ <a href=\"".$admin_file.".php?op=adminStory\">"._ADD."</a> | <a href=\"".$admin_file.".php?op=EditStory&amp;sid=$sid\" />"._EDIT."</a> | <a href=\"".$admin_file.".php?op=RemoveStory&amp;sid=$sid\" />"._DELETE."</a> ]<br /><br /></center>";
}
themesidebox($optiontitle, $optionbox);
echo "</td></tr></table>\n";

incof( "".$name_article."", "".$article_extension."",  "".$article_ret."", '2');