<?php
// $Id: post.php,v 1.2 2004/07/19 20:34:53 Farsus Exp $
// -------------------------------------------------------------------------//
//			       RUNCMS
//
//	 Relaible - Unique - Nocost &  Simplicity & ease off use
//                       < http://www.runcms.org >
// -------------------------------------------------------------------------//
// Original Author: Kazumi Ono
// Author Website : http://www.mywebaddons.com/ , http://www.myweb.ne.jp
// License Type   : GPL: See /manual/LICENSES/GPL.txt
// ------------------------------------------------------------------------- //
include_once("header.php");
include_once(XOOPS_ROOT_PATH.'/class/fileupload.php');

$upload = new fileupload();

if ( empty($HTTP_POST_VARS['forum']) )
{
	redirect_header("index.php", 2, _MD_ERRORFORUM);
	exit();
}
else if ( empty($HTTP_POST_VARS['message']) )
{
	redirect_header("javascript:history.go(-1)", 2, _MD_ERRORMESSAGE);
	exit();
}
else {
		$sql = "SELECT * FROM ".$bbTable['forums']." WHERE forum_id = ".$HTTP_POST_VARS['forum']."";
		if (!$result = $db->query($sql)) {
			redirect_header("index.php", 2, _MD_CANTGETFORUM);
			exit();
		}
	$forumdata = $db->fetch_array($result);

	$permissions = get_forum_auth($forumdata['forum_id']);
	if ( $permissions['can_view'] == 0 || ($permissions['can_post'] == 0 && $permissions['can_reply'] == 0))
	{
		redirect_header("viewforum.php?order=".$HTTP_POST_VARS['order']."&viewmode=".$HTTP_POST_VARS['viewmode']."&forum=".$HTTP_POST_VARS['forum']."",2,_MD_NORIGHTTOPOST);
		exit();
	}
	if ( !empty($HTTP_POST_VARS['contents_preview']) ) {
		include_once(XOOPS_ROOT_PATH."/header.php");
		OpenTable();
		$p_subject = $myts->makeTboxData4Preview($HTTP_POST_VARS['subject']);
		if ($xoopsUser && $xoopsUser->isAdmin($xoopsModule->mid())) {
			$myts->setType('admin');
			} else {
				$myts->setType('user');
			}
		$p_message = $myts->makeTareaData4Preview($HTTP_POST_VARS['message'], intval($HTTP_POST_VARS['allow_html']), intval($HTTP_POST_VARS['allow_smileys']), intval($HTTP_POST_VARS['allow_bbcode']));
		themecenterposts($p_subject, $p_message);
		echo "<br />";
		$subject  = $myts->makeTboxData4PreviewInForm($HTTP_POST_VARS['subject']);
		$message  = $myts->makeTboxData4PreviewInForm($HTTP_POST_VARS['message']);
		$hidden   = $myts->makeTboxData4PreviewInForm($HTTP_POST_VARS['hidden']);
		$viewmode = $myts->makeTboxData4PreviewInForm($HTTP_POST_VARS['viewmode']);
		include_once("include/forumform.inc.php");
		CloseTable();
	} else {
		include_once("class/class.forumposts.php");
		if ( !empty($HTTP_POST_VARS['post_id']) ) {
			$editerror = 0;
			$forumpost = new ForumPosts($HTTP_POST_VARS['post_id']);
			if ( $xoopsUser ) {
				if ( !$xoopsUser->isAdmin($xoopsModule->mid()) ) {
					if ( $forumpost->uid() != $xoopsUser->getVar("uid") && !is_moderator($HTTP_POST_VARS['forum'], $xoopsUser->getVar("uid")) ) {
						$editerror = 1;
					}
				}
			} else {
				$editerror = 1;
			}
			if ( $editerror == 1 ) {
				redirect_header("viewtopic.php?topic_id=".$HTTP_POST_VARS['topic_id']."&post_id=".$HTTP_POST_VARS['post_id']."&order=".$HTTP_POST_VARS['order']."&viewmode=".$HTTP_POST_VARS['viewmode']."&pid=".$HTTP_POST_VARS['pid']."&forum=".$HTTP_POST_VARS['forum']."",2,_MD_EDITNOTALLOWED);
				exit();
			}
		} else {
			$isreply = 0;
			if ( $xoopsUser && empty($HTTP_POST_VARS['noname']) )
			{
				$uid = $xoopsUser->getVar("uid");
			}
			else
			{
				$uid = 0;
			}
			$forumpost = new ForumPosts();
			$forumpost->setForum($HTTP_POST_VARS['forum']);
			if (isset($HTTP_POST_VARS['pid']) && $HTTP_POST_VARS['pid'] != "") {
				$forumpost->setParent($HTTP_POST_VARS['pid']);
			}
			if (!empty($HTTP_POST_VARS['topic_id'])) {
				$forumpost->setTopicId($HTTP_POST_VARS['topic_id']);
				$isreply = 1;
			}
			$forumpost->setIp(_REMOTE_ADDR);
			$forumpost->setUid($uid);
		}
	
		//ADD by xtremdj in order to add [sondage] to post with polls
		if(($HTTP_POST_VARS["poll_question"]!="")&&($option[0]!="")&&($option[1]!="")) $rajsubject="[Sondage] ";
		else $rajsubject="";
		//EnD
		
		$forumpost->setSubject($rajsubject.$HTTP_POST_VARS['subject']); //ADD $rajsubject by xtremdj
	
		$forumpost->setSubject($HTTP_POST_VARS['subject']);

		$message = $HTTP_POST_VARS['message'];
		if ( $xoopsUser && !empty($HTTP_POST_VARS['editpost']) && !empty($HTTP_POST_VARS['add_edited'])) {
			$editor   = $xoopsUser->getVar("uname");
			$on_date  = _MD_ON." ".formatTimestamp(time(), _MEDIUMDATESTRING);
			$message .= "\n\n[ "._EDITEDBY." ".$editor." ".$on_date." ]";
		}
		$forumpost->setText($message);

		$forumpost->setHtml($HTTP_POST_VARS['allow_html']);
		$forumpost->setSmileys($HTTP_POST_VARS['allow_smileys']);
		$forumpost->setBBcode($HTTP_POST_VARS['allow_bbcode']);
		$forumpost->setIcon($HTTP_POST_VARS['icon']);
		$forumpost->setAttachsig($HTTP_POST_VARS['attachsig']);
		// alphalogic's attachment hack <---
		if (!empty($HTTP_POST_VARS['attachment']))
		{
			$forumpost->setAttachment($HTTP_POST_VARS['attachment']);
		} else {
			$forumpost->setAttachment('');
		}
		// --->
		if (!empty($HTTP_POST_VARS['notify'])) {
			$forumpost->setNotify($HTTP_POST_VARS['notify']);
		}
		if ($xoopsUser && $xoopsUser->isAdmin($xoopsModule->mid())) {
			$forumpost->setType('admin');
			} else {
				$forumpost->setType('user');
			}
		$postid = $forumpost->store();

		if ($isreply == 1) {
			$sql = "SELECT t.topic_notify, u.email, u.uname, u.uid FROM ".$bbTable['topics']." t, ".$db->prefix("users")." u WHERE t.topic_id = ".$HTTP_POST_VARS['topic_id']." AND t.topic_poster = u.uid";
			if (!$result = $db->query($sql)) {
				echo _MD_COULDNOTQUERY;
			}
			$m = $db->fetch_array($result);
			$myuid = 0;
			if ( $xoopsUser ) {
				$myuid = $xoopsUser->uid();
			}
			if ($m['topic_notify'] == 1 && $m['uid'] != $myuid && $m['uid'] != 0) {
				$subject = _MD_REPLYPOSTED;
				$message = sprintf(_MD_HELLO,$m['uname']);
				$message .= "\n\n";
				$message .= sprintf(_MD_URRECEIVING, $meta['title']);
				$message .= "\n\n"._MD_CLICKBELOW;
				$message .= "\n\n";
				$message .= $bbPath['url']."viewtopic.php?forum=".$HTTP_POST_VARS['forum']."&topic_id=".$HTTP_POST_VARS['topic_id']."\n\n------------\n".$meta['title']."\n".XOOPS_URL."";
				$xoopsMailer =& getMailer();
				$xoopsMailer->useMail();
				$xoopsMailer->setToEmails($m['email']);
				$xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
				$xoopsMailer->setFromName($meta['title']);
				$xoopsMailer->setSubject($subject);
				$xoopsMailer->setBody($message);
				$xoopsMailer->send();
			}
		}
	//Add by xtremdj to insert polls into db
	//insert polls
		
if(($HTTP_POST_VARS["poll_question"]!="")&&($option[0]!="")&&($option[1]!=""))
	{
		$poll_question=$HTTP_POST_VARS["poll_question"];
		$poll_question =$myts->censorString($poll_question);
		$poll_question = $myts->makeTboxData4Save($poll_question);
		$start_time = time();
		$end_time = $start_time + (intval($HTTP_POST_VARS["poll_expire"]) * 86400);
	//insert poll description
	if ( $xoopsUser ) $uiduser=$xoopsUser->uid();
	else $uiduser=0;
	$requete="INSERT INTO ".$bbTable['poll_desc']." (poll_id, question, description, user_id, start_time, end_time, votes, voters, multiple, display, weight, mail_status,topic_id) VALUES (NULL, '".$poll_question."', 'j', ".$uiduser.", ".$start_time.", ".$end_time.", 0, 0, 0, 0, 0, 0,".$forumpost->topic().")";
	if (!$resultsql = $db->query($requete)) 
		{
		error_die("Error in the insertion of the polls ".$db->error());
		}
	//Select id of new poll
	$requete="SELECT MAX(poll_id) as idmaximum from ".$bbTable['poll_desc'];
	if (!$resultsql = $db->query($requete)) 
		{
		error_die("Error in the insertion of the polls ".$db->error());
		
		}
	$idrs = $db->fetch_array($resultsql);
	$idmaximum=$idrs["idmaximum"];

	//insert options

	for($i=0;$i<count($option);$i++)
		{
		if($option[$i]!="" && $bar_color[$i]!="")
			{
			$option[$i] =$myts->censorString($option[$i]);
			$option[$i] = $myts->makeTboxData4Save($option[$i]);
			
			$requete="INSERT INTO ".$bbTable['poll_option']." (option_id, poll_id, option_text, option_count, option_color) VALUES (NULL, ".$idmaximum.", '".$option[$i]."', 0, '".$bar_color[$i]."')";
			if (!$resultsql = $db->query($requete)) 
				{
				error_die("Error in the insertion of the polls ".$db->error());
				}
			}
		$cpt++;
		}
	}
		
// END HACK

//Debut modif notification par Mail
		if ($topic_id){
		    $sql = "SELECT usernotif_id FROM ".$bbTable['topics_mail']." WHERE topic_id = $topic_id";
		    $result = $db->query($sql);
		    if(!$result) {
				echo _MD_COULDNOTQUERY;
			}
			$sqltitle = "SELECT topic_title FROM ".$bbTable['topics']." WHERE topic_id = $topic_id";
			$resulttitle = $db->query($sqltitle);
			if(!$resulttitle) {
				echo _MD_COULDNOTQUERY;
			}
			$mtopic = $db->fetch_array($resulttitle);
		
			$sqlauteur = "SELECT uid FROM ".$bbTable['posts']." WHERE post_id = $postid";
			$resultauteur = $db->query($sqlauteur);
			if(!$resultauteur) {
				echo _MD_COULDNOTQUERY;
			}
			$mauteur = $db->fetch_array($resultauteur);
			$sqlpostauteur = "SELECT uname FROM ".$db->prefix("users")." WHERE uid = ".$mauteur[uid]."";
			$resultpostauteur = $db->query($sqlpostauteur);
			if(!$resultpostauteur) {
				echo _MD_COULDNOTQUERY;
			}
			$mpostauteur = $db->fetch_array($resultpostauteur);
		
			while ( $notifmail = @each ($db->fetch_array($result)) ) {
				$sqlbis = "SELECT email, uname FROM ".$db->prefix("users")." WHERE uid = ".$notifmail[1]."";
				$resultbis = $db->query($sqlbis);
				$mlist = $db->fetch_array($resultbis);
				$subject = _MD_REPLYPOSTED2;
				$message = sprintf(_MD_HELLO,$mlist['uname']);
				$message .= "\n\n";
				$message .= sprintf(_MD_URRECEIVING2,$meta['title']);
				$message .= "\n\n";
				$message .= sprintf(_MD_MAILTITRETOPIC,$mtopic['topic_title']);
				$message .= "\n\n";
				$message .= sprintf(_MD_POSTAUTEUR,$mpostauteur['uname']);
				$message .= "\n\n"._MD_CLICKBELOW;
				$message .= "\n\n";
				$message .= $bbPath['url']."viewtopic.php?forum=".$forum."&topic_id=".$topic_id."\n\n------------\n".$xoopsConfig['sitename']."\n".XOOPS_URL."";
		    	$xoopsMailer =& getMailer();
				$xoopsMailer->useMail();
				$xoopsMailer->setToEmails($mlist['email']);
				$xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
				$xoopsMailer->setFromName($meta['title']);
				$xoopsMailer->setSubject($subject);
				$xoopsMailer->setBody($message);
				$xoopsMailer->send();
			}
		}		
// Fin modif notification par mail


		if ( $HTTP_POST_VARS['viewmode'] == "flat" ) {
			redirect_header("viewtopic.php?topic_id=".$forumpost->topic()."&amp;post_id=".$postid."&amp;order=".$HTTP_POST_VARS['order']."&amp;viewmode=flat&amp;pid=".$HTTP_POST_VARS['pid']."&amp;forum=".$HTTP_POST_VARS['forum']."#".$postid."", 2, _MD_THANKSSUBMIT);
			exit();
			} else {
				$post_id = $forumpost->postid();
				redirect_header("viewtopic.php?topic_id=".$forumpost->topic()."&amp;post_id=".$postid."&amp;order=".$HTTP_POST_VARS['order']."&amp;viewmode=thread&amp;pid=".$HTTP_POST_VARS['pid']."&amp;forum=".$HTTP_POST_VARS['forum']."#".$postid."", 2, _MD_THANKSSUBMIT);
				exit();
			}

	}
include_once(XOOPS_ROOT_PATH."/footer.php");
}
?>
