<?php
/**
 * $Source: /home/cvs/nukescripts/modules/News/includes/functions.php,v $
 * $Revision: 1.1.6-2 $
 * $Author: cyril $
 * $Date: 2006/08/01 11:50:01 $
 * Fix ligne 161 Piermin  - Bug 135
 * Fix ligne 133 Frenatus - Bug 121
 */
/************************************************************************/
/* 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 Header("Location: index.php");

// comments allow
$articlecomm  = 1;
$commentlimit = 4096;

global $admin_file;
if(empty($admin_file)) { $admin_file= "admin"; }
$ThemeSel = get_theme();
if (is_file("themes/".$ThemeSel."/news.php")) { include_once("themes/".$ThemeSel."/news.php"); }

$tipath = "images/topics/";

$reasons = array("As Is",
"Offtopic",
"Flamebait",
"Troll",
"Redundant",
"Insighful",
"Interesting",
"Informative",
"Funny",
"Overrated",
"Underrated");


$badreasons = 4;

$CensorList = array("fuck",
"cunt",
"fucker",
"fucking",
"pussy",
"cock",
"c0ck",
"cum",
"twat",
"clit",
"bitch",
"fuk",
"fuk",
"motherfucker");

function ne_get_configs(){
    global $prefix;
    $sql = "SELECT `config_name`, `config_value` FROM `".$prefix."_nsnne_config` ";
    if ( !($configresult = $GLOBALS['db']->sql_query($sql)) )
{ query_die(MODULE_ERROR, 'Could not obtain nsnne_config WebSite', '', __LINE__, __FILE__, $sql);}
    while ($raw = $GLOBALS['db']->sql_fetchrow($configresult)) {
    $config_name  = stripslashes($raw['config_name']);
    $config_value = stripslashes($raw['config_value']);
        $config[$config_name] = $config_value;
    }
    return $config;
}


function getTopics($xtopic) {
	global $tipath, $topicname, $topicimage, $topictext;
	$xtopic = $GLOBALS['db']->XMFnum($xtopic);
	$sqlt   = "SELECT `topicid`, `topicname`, `topicimage`, `topictext` FROM `".TABLE_TOPICS."` WHERE `topicid`='$xtopic'";
	if ( !($resultt = $GLOBALS['db']->sql_query($sqlt)) )
{ query_die(MODULE_ERROR, 'Could not obtain topics WebSite', '', __LINE__, __FILE__, $sqlt);}
	$rowt       = $GLOBALS['db']->sql_fetchrow($resultt);
	$topicid    = $GLOBALS['db']->XMFnum($rowt['topicid']);
	$topicname  = stripslashes($rowt['topicname']);
	$topicimage = stripslashes($rowt['topicimage']);
	$topictext  = stripslashes(check_html($rowt['topictext'], "nohtml"));
	$GLOBALS['db']->sql_freeresult($resultt);
}


function automated_news() {
	global $prefix, $multilingual, $currentlang, $db;
	$multilingual = $db->XMFbin( $multilingual );
	$currentlang  = $db->XMFlang( $currentlang );
	if ($multilingual == 1) {
		$querylang = "WHERE (`alanguage`='$currentlang' OR `alanguage`='')"; /* the OR is needed to display stories who are posted to ALL languages */
	} else {
		$querylang = "";
	}
	$today = getdate();
	$day = $db->XMFnum($today['mday']);
	if ($day < 10) {
		$day = "0$day";
	}
	$month = $db->XMFnum($today['mon']);
	if ($month < 10) {
		$month = "0$month";
	}
	$year = $db->XMFnum($today['year']);
	$hour = $db->XMFnum($today['hours']);
	$min  = $db->XMFnum($today['minutes']);
	$sec  = "00";
	$result = $db->sql_query("SELECT `anid`, `time` FROM `".TABLE_AUTONEWS."` $querylang");
	while ($row = $db->sql_fetchrow($result)) {
		$anid = $db->XMFnum($row['anid']);
		$time = stripslashes($row['time']);
		ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $time, $date);
		if (($date[1] <= $year) AND ($date[2] <= $month) AND ($date[3] <= $day)) {
			if (($date[4] < $hour) AND ($date[5] >= $min) OR ($date[4] <= $hour) AND ($date[5] <= $min)) {
				$result2 = $db->sql_query("SELECT * FROM `".TABLE_AUTONEWS."` WHERE `anid`='$anid'");
				while ($row2 = $db->sql_fetchrow($result2)) {
					$title       = stripslashes(FixQuotes(check_html($row2['title'], "nohtml")));
					$hometext    = stripslashes(FixQuotes($row2['hometext']));
					$bodytext    = stripslashes(FixQuotes($row2['bodytext']));
					$notes       = stripslashes(FixQuotes($row2['notes']));
					$catid2      = $db->XMFnum($row2['catid']);
					$aid2        = stripslashes($row2['aid']);
					$time2       = stripslashes($row2['time']);
					$topic2      = $db->XMFnum($row2['topic']);
					$informant2  = stripslashes($row2['informant']);
					$ihome2      = $db->XMFnum($row2['ihome']);
					$alanguage2  = stripslashes($row2['alanguage']);
					$acomm2      = $db->XMFnum($row2['acomm']);
					$associated2 = stripslashes($row2['associated']);
					$num = $db->sql_numrows($db->sql_query("SELECT `sid` FROM `".TABLE_STORIES."` WHERE `title`='$title'"));
					if ($num == 0) {
						$db->sql_query("DELETE FROM `".TABLE_AUTONEWS."` WHERE `anid`='$anid'");
						$db->sql_query("INSERT INTO `".TABLE_STORIES."` VALUES (NULL, '$catid2', '$aid2', '$title', '$time2', '$hometext', '$bodytext', '0', '0', '$topic2', '$informant2', '$notes', '$ihome2', '$alanguage2', '$acomm2', '0', '0', '0', '0', '$associated2')");
					}
				}
			}
		}
	}
}

function loginbox() {
	global $user, $sitekey, $gfx_chk;

	mt_srand ((double)microtime()*1000000);
	$maxran = 1000000;
	$random_num = mt_rand(0, $maxran);
	$datekey = date("F j");
	$rcode = hexdec(md5($_SERVER['HTTP_USER_AGENT'] . $sitekey . $random_num . $datekey));
	$code = substr($rcode, 2, 6);

	if (!defined('USER_ALLOW')) {
		$title = _LOGIN;
		$boxstuff = "<form action='modules.php?name=Your_Account' method='post'>";
		$boxstuff .= "<center><font class='content'>"._NICKNAME."<br />";
		$boxstuff .= "<input type='text' name='username' size='8' maxlength='25' /><br />";
		$boxstuff .= ""._PASSWORD."<br />";
		$boxstuff .= "<input type='password' name='user_password' size='8' maxlength='20' /><br />";
		if (extension_loaded("gd") AND ($gfx_chk == 2 OR $gfx_chk == 4 OR $gfx_chk == 5 OR $gfx_chk == 7)) {
			$boxstuff .= ""._SECURITYCODE.": <img src='?gfx=gfx&amp;random_num=$random_num' border='1' alt='"._SECURITYCODE."' title='"._SECURITYCODE."' /><br />\n";
			$boxstuff .= ""._TYPESECCODE."<br /><input type='text' name='gfx_check' size='7' maxlenght='6' />\n";
			$boxstuff .= "<input type='hidden' name='random_num' value=\"$random_num\" /><br />\n";
		} else {
			$boxstuff .= "<input type='hidden' name='random_num' value=\"$random_num\" />";
			$boxstuff .= "<input type='hidden' name='gfx_check' value=\"$code\" />";
		}
		$boxstuff .= "<input type='hidden' name='op' value='login' />";
		$boxstuff .= "<input type='submit' value=\""._LOGIN."\" /></font></center></form>";
		$boxstuff .= "<center><font class='content'>"._ASREGISTERED."</font></center>";
		themesidebox($title, $boxstuff);
	}
}

function get_author($aid) {
    if (empty($aid)) { return NULL; }
    static $users;
    if (isset($users[$aid]) AND is_array($users[$aid])) {
        $row = $users[$aid];
    } else {
        $sql = "SELECT `url`, `email` FROM `".TABLE_AUTHORS."` WHERE `aid`='$aid' ";
        if ( !($result = $GLOBALS['db']->sql_query($sql)) )
{ query_die(MODULE_ERROR, 'Could not SELECT get_authors', '', __LINE__, __FILE__, $sql);}
        $row = $GLOBALS['db']->sql_fetchrow($result);
        $users[$aid] = $row;
        $GLOBALS['db']->sql_freeresult($result);
    }
    $aidurl  = stripslashes($row['url']);
    $aidmail = stripslashes($row['email']);
    if (isset($aidurl) && $aidurl != 'http://') {
        $aid = "<a href=\"".$aidurl."\">$aid</a>";
    } elseif (isset($aidmail)) {
        $aid = "<a href=\"mailto:".$aidmail."\">$aid</a>";
    } else {
        $aid = $aid;
    }
    return $aid;
}