<?php
/**
 * $Source: /home/cvs/nukescripts/modules/Submit_News/index.php,v $
 * $Revision: 1.0.0a $
 * $Author: cyril $
 * $Date: 2007/02/15 11:39:32 $
 *
 */
/************************************************************************/
/* PHP-NUKE MaXiMuS: Web Portal System                                  */
/* =============================                                        */
/*                                                                      */
/* Copyright (c) 2002 - 2007                                            */
/* http://www.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");

$module_name = "Submit_News";
get_lang($module_name);
$pagetitle = "- "._SUBMITNEWS."";

function themepreview($title, $hometext, $bodytext="", $notes="") {
	echo "<b>$title</b><br /><br />$hometext";
	if ($bodytext != "") {
	br('2');
		echo "$bodytext";
	}
	if ($notes != "") {
	br('2');
		echo "<b>"._NOTE."</b> <i>$notes</i>";
	}
}

function defaultDisplay() {
	global $AllowableHTML, $prefix, $user, $cookie, $anonymous, $currentlang, $multilingual, $db, $module_name, $wysiwyg;
	if ($wysiwyg)
    {
    include_once('FCKeditor/fckeditor.php');
    }
	incoh();
	Max_OpenTable();
	echo "<center><font class='title'><b>"._SUBMITNEWS."</b></font><br /><br />";
	echo "<font class=\"content\"><i>"._SUBMITADVICE."</i></font></center><br />";
	Max_CloseTable();
	br('1');
	Max_OpenTable();
	if (defined('USER_ALLOW')) getusrinfo($user);
	echo "<p><form action=\"modules.php?name=$module_name\" method=\"post\">"
	."<b>"._YOURNAME.":</b> ";
	if (defined('USER_ALLOW')) {
		cookiedecode($user);
		echo "<a href=\"modules.php?name=Your_Account\">$cookie[1]</a> <font class=\"content\">[ <a href=\"modules.php?name=Your_Account&amp;op=logout\">"._LOGOUT."</a> ]</font>";
	} else {
		echo "$anonymous <font class=\"content\">[ <a href=\"modules.php?name=Your_Account\">"._NEWUSER."</a> ]</font>";
	}
	echo "<br /><br />"
	."<b>"._SUBTITLE."</b> "
	."("._BEDESCRIPTIVE.")<br />"
	."<input type=\"text\" name=\"subject\" size=\"50\" maxlength=\"80\" /><br /><font class=\"content\">("._BADTITLES.")</font>"
	."<br /><br />"
	."<b>"._TOPIC.":</b> <select name=\"topic\">";
	$result = $db->sql_query("SELECT topicid, topictext FROM ".TABLE_TOPICS." ORDER BY topictext");
	echo "<option value=\"\">"._SELECTTOPIC."</option>\n";
	while ($row = $db->sql_fetchrow($result)) {
		$topicid = intval($row['topicid']);
		$topics = stripslashes(check_html($row['topictext'], "nohtml"));
		if ($topicid == $topic) {
			$sel = "selected ";
		}
		echo "<option $sel value=\"$topicid\">$topics</option>\n";
		$sel = "";
	}
	echo "</select>";
	if ($multilingual == 1) {
		echo "<br /><br /><b>"._LANGUAGE.": </b>"
		."<select name=\"alanguage\">";
		$handle=opendir('language');
		while ($file = readdir($handle)) {
			if (preg_match("/^lang\-(.+)\.php/", $file, $matches)) {
				$langFound = $matches[1];
				$languageslist .= "$langFound ";
			}
		}
		closedir($handle);
		$languageslist = explode(" ", $languageslist);
		sort($languageslist);
		for ($i=0; $i < sizeof($languageslist); $i++) {
			if($languageslist[$i]!="") {
				echo "<option value=\"$languageslist[$i]\" ";
				if($languageslist[$i]==$currentlang) echo "selected";
				echo ">".ucfirst($languageslist[$i])."</option>\n";
			}
		}
		echo "</select>";
	} else {
		echo "<input type=\"hidden\" name=\"alanguage\" value=\"$language\" />";
	}
	br('2');
	echo "<b>"._STORYTEXT.":</b><br />";
if ( $wysiwyg == 'fck' ) {
// Editor Start
$oFCKeditor = new FCKeditor(story) ;
$oFCKeditor->ToolbarSet = 'Basic';
$oFCKeditor->InstanceName = "story" ;
$oFCKeditor->Create() ;
// Editor Ende
} else {
echo  "<textarea cols=\"70\" rows=\"15\" name=\"story\"></textarea>";
}
br('3');
echo "<b>"._EXTENDEDTEXT.":</b><br />";
if ( $wysiwyg == 'fck' ) {
// Editor Start
$oFCKeditor = new FCKeditor(storyext) ;
$oFCKeditor->ToolbarSet = 'Basic';
$oFCKeditor->InstanceName = "storyext" ;
$oFCKeditor->Create() ;
// Editor Ende
} else {
echo  "<textarea cols=\"70\" rows=\"15\" name=\"storyext\"></textarea>";
}
echo "<br />"
	."<font class=\"content\">("._AREYOUSURE.")<br /><br />"
	.""._HTMLNOTALLOWED."</font>"
	."<br /><br /><input type=\"submit\" name=\"op\" value=\""._PREVIEW."\" /><br />("._SUBPREVIEW.")</font></form>";
	Max_CloseTable();
	incof();;
}

function PreviewStory($name, $address, $subject, $story, $storyext, $topic, $alanguage) {
	global $user, $cookie, $bgcolor1, $bgcolor2, $anonymous, $multilingual, $AllowableHTML, $db, $module_name, $wysiwyg;
	if ($wysiwyg)
    {
    include_once('FCKeditor/fckeditor.php');
    }
	incoh();
	$f_story = stripslashes($story);
	$f_storyext = stripslashes($storyext);
	$story2 = "$f_story<br /><br />$f_storyext";
	Max_OpenTable();
	echo "<center><font class='title'><b>"._NEWSUBPREVIEW."</b></font>";
	Max_CloseTable();
	br('1');
	Max_OpenTable();
	echo "<center><i>"._STORYLOOK."</i></center><br /><br />";
	echo "<table width=\"70%\" bgcolor=\"$bgcolor2\" cellpadding=\"0\" cellspacing=\"1\" border=\"0\"align=\"center\"><tr><td>"
	."<table width=\"100%\" bgcolor=\"$bgcolor1\" cellpadding=\"8\" cellspacing=\"1\" border=\"0\"><tr><td>";
	if ($topic=="") {
		$topicimage="AllTopics.gif";
		$warning = "<center><blink><b>"._SELECTTOPIC."</b></blink></center>";
	} else {
		$warning = "";
		$row = $db->sql_fetchrow($db->sql_query("SELECT topictext, topicimage FROM ".TABLE_TOPICS." WHERE topicid='$topic'"));
		$topicimage = stripslashes($row['topicimage']);
	}
	echo "<img src=\"images/topics/$topicimage\" border=\"0\" align=\"right\" alt=\"$row[topictext]\" title=\"$row[topictext]\" />";
	themepreview($subject, $story2);
	echo "$warning"
	."</td></tr></table></td></tr></table>"
	."<br /><br /><center><font class=\"tiny\">"._CHECKSTORY."</font></center>";
	Max_CloseTable();
	br('1');
	Max_OpenTable();
	echo "<p><form action=\"modules.php?name=$module_name\" method=\"post\">"
	."<b>"._YOURNAME.":</b> ";
	if (defined('USER_ALLOW')) {
		cookiedecode($user);
		echo "<a href=\"modules.php?name=Your_Account\">$cookie[1]</a> <font class=\"content\">[ <a href=\"modules.php?name=Your_Account&amp;op=logout\">"._LOGOUT."</a> ]</font>";
	} else {
		echo "$anonymous";
	}
	echo "<br /><br /><b>"._SUBTITLE.":</b><br />"
	."<input type=\"text\" name=\"subject\" size=\"50\" maxlength=\"80\" value=\"$subject\" />"
	."<br /><br /><b>"._TOPIC.": </b><select name=\"topic\">";
	$result2 = $db->sql_query("SELECT topicid, topictext FROM ".TABLE_TOPICS." ORDER BY topictext");
	echo "<option value=\"\">"._SELECTTOPIC."</option>\n";
	while ($row2 = $db->sql_fetchrow($result2)) {
		$topicid = intval($row2['topicid']);
		$topics = stripslashes(check_html($row2['topictext'], "nohtml"));
		if ($topicid == $topic) {
			$sel = "selected ";
		}
		echo "<option $sel value=\"$topicid\">$topics</option>\n";
		$sel = "";
	}
	echo "</select>";
	if ($multilingual == 1) {
		echo "<br /><br /><b>"._LANGUAGE.": </b>"
		."<select name=\"alanguage\">";
		$handle=opendir('language');
		while ($file = readdir($handle)) {
			if (preg_match("/^lang\-(.+)\.php/", $file, $matches)) {
				$langFound = $matches[1];
				$languageslist .= "$langFound ";
			}
		}
		closedir($handle);
		$languageslist = explode(" ", $languageslist);
		sort($languageslist);
		for ($i=0; $i < sizeof($languageslist); $i++) {
			if($languageslist[$i]!="") {
				echo "<option value=\"$languageslist[$i]\" ";
				if($languageslist[$i]==$alanguage) echo "selected";
				echo ">".ucfirst($languageslist[$i])."</option>\n";
			}
		}
		echo "</select>";
	}
	echo "<br /><br /><b>"._STORYTEXT.":</b><br />";
 if ( $wysiwyg == 'fck' ) {
// Editor Start
$oFCKeditor = new FCKeditor(story) ;
$oFCKeditor->ToolbarSet = 'Basic';
$oFCKeditor->InstanceName = "story" ;
$oFCKeditor->Value = "$f_story";
$oFCKeditor->Create() ;
// Editor Ende
} else {
echo  "<textarea cols=\"70\" rows=\"15\" name=\"story\">$f_story</textarea>";
}
echo "<br />"
	."<br /><b>"._EXTENDEDTEXT.":</b><br />";
 if ( $wysiwyg == 'fck' ) {
// Editor Start
$oFCKeditor = new FCKeditor(storyext) ;
$oFCKeditor->ToolbarSet = 'Basic';
$oFCKeditor->InstanceName = "storyext" ;
$oFCKeditor->Value = "$f_storyext";
$oFCKeditor->Create() ;
// Editor Ende
} else {
echo  "<textarea cols=\"70\" rows=\"15\" name=\"storyext\">$f_storyext</textarea>";
}
echo "<br />"
	."<font class=\"content\">("._AREYOUSURE.")</font><br /><br />"
	.""._HTMLNOTALLOWED.""
	."<br /><br />"
	."<input type=\"submit\" name=\"op\" value=\""._PREVIEW."\">&nbsp;&nbsp;"
	."<input type=\"submit\" name=\"op\" value=\""._OK."\"></form>";
	Max_CloseTable();
	incof();
}

function submitStory($name, $address, $subject, $story, $storyext, $topic, $alanguage) {
	global $user, $EditedMessage, $cookie, $anonymous, $notify, $notify_email, $notify_subject, $notify_message, $notify_from, $db;
	if (defined('USER_ALLOW')) {
		cookiedecode($user);
		$uid = $cookie[0];
		$name = $cookie[1];
	} else {
		$uid = 1;
		$name = "$anonymous";
	}
	$subject = ereg_replace("\"", "''", $subject);
	$subject = FixQuotes(filter_text($subject, "nohtml"));
	$story = stripslashes(FixQuotes($story));
	$storyext = stripslashes(FixQuotes($storyext));
	$result = $db->sql_query("INSERT INTO ".TABLE_QUEUE." VALUES (NULL, '$uid', '$name', '$subject', '$story', '$storyext', now(), '$topic', '$alanguage')");
	if(!$result) {
		echo ""._ERROR."<br />";
		exit();
	}
	if($notify) {
		$notify_message = "$notify_message\n\n\n========================================================\n$subject\n\n\n$story\n\n$storyext\n\n$name";
		mail($notify_email, $notify_subject, $notify_message, "From: $notify_from\nX-Mailer: PHP/" . phpversion());
	}
	incoh();
	Max_OpenTable();
	$waiting = $db->sql_numrows($db->sql_query("SELECT * FROM ".TABLE_QUEUE.""));
	echo "<center><font class='title'>"._SUBSENT."</font><br /><br />"
	."<font class=\"content\"><b>"._THANKSSUB."</b><br /><br />"
	.""._SUBTEXT.""
	."<br />"._WEHAVESUB." $waiting "._WAITING."";
	Max_CloseTable();
	incof();
}

switch($op) {

	case ""._PREVIEW."":
	PreviewStory($name, $address, $subject, $story, $storyext, $topic, $alanguage);
	break;

	case ""._OK."":
	SubmitStory($name, $address, $subject, $story, $storyext, $topic, $alanguage);
	break;

	default:
	defaultDisplay();
	break;

}