<?php
// ------------------------------------------------------------------------- //
//                XOOPS - PHP Content Management System                      //
//                       <http://www.xoops.org/>                             //
// ------------------------------------------------------------------------- //
// Based on:								     //
// myPHPNUKE Web Portal System - http://myphpnuke.com/	  		     //
// PHP-NUKE Web Portal System - http://phpnuke.org/	  		     //
// Thatware - http://thatware.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, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
// ------------------------------------------------------------------------- //
// Author of File: Kazumi Ono (http://www.mywebaddons.com/)                  //
################################################################################
$pagetype = "post";
include("header.php");
if ( empty($HTTP_POST_VARS['forum']) ) {
	redirect_header("index.php", 2, _MD_ERRORFORUM);
	exit();
} else {	
	$sql = "SELECT forum_type, forum_name, forum_access, allow_html, allow_sig, posts_per_page, hot_threshold, topics_per_page FROM ".$xoopsDB->prefix("bb_forums")." WHERE forum_id = ".$HTTP_POST_VARS['forum']."";
	if (!$result = $xoopsDB->query($sql)) {
		error_die(_MD_CANTGETFORUM);
	}
	$forumdata = $xoopsDB->fetchArray($result);
	if ( $forumdata['forum_type'] == 1 ) {
	// To get here, we have a logged-in user. So, check whether that user is allowed to view
	// this private forum.
		$accesserror = 0;
		if ( $xoopsUser ) {
			if ( !$xoopsUser->isAdmin($xoopsModule->mid()) ) {
				if ( !check_priv_forum_auth($xoopsUser->uid(), $HTTP_POST_VARS['forum'], TRUE) ) {
					$accesserror = 1;
				}
			}
		} else {
			$accesserror = 1;
		}
		if ( $accesserror == 1 ) {
			redirect_header("viewforum.php?order=".$HTTP_POST_VARS['order']."&viewmode=".$HTTP_POST_VARS['viewmode']."&forum=".$HTTP_POST_VARS['forum']."",2,_MD_NORIGHTTOPOST);
			exit();
		}
	}
	$accesserror = 0;
	if ( $forumdata['forum_access'] == 3 ) {
		if ( $xoopsUser ) {
			if ( !$xoopsUser->isAdmin($xoopsModule->mid()) ) {
				if ( !is_moderator($HTTP_POST_VARS['forum'], $xoopsUser->uid()) ) {
					$accesserror = 1;
				}
			}
		} else {
			$accesserror = 1;
		}
		
	} elseif ( $forumdata['forum_access'] == 1 && !$xoopsUser ) {
		$accesserror = 1;
	}
	if ( $accesserror == 1 ) {
		redirect_header("viewforum.php?order=".$HTTP_POST_VARS['order']."&viewmode=".$HTTP_POST_VARS['viewmode']."&forum=".$HTTP_POST_VARS['forum']."",2,_MD_NORIGHTTOPOST);
		exit();
	}
	if ( $HTTP_POST_VARS['op'] == "preview" ) {
		include(XOOPS_ROOT_PATH."/header.php");
		OpenTable();
		include("page_header.php");
		$myts =& MyTextSanitizer::getInstance();
		$p_subject = $myts->makeTboxData4Preview($HTTP_POST_VARS['subject']);
		if ( $HTTP_POST_VARS['nosmiley'] == 1 && $HTTP_POST_VARS['nohtml'] == 1 ) {
			$p_message = $myts->makeTareaData4Preview($HTTP_POST_VARS['message'],0,0,1);
		} elseif ($HTTP_POST_VARS['nohtml'] == 1) {
			$p_message = $myts->makeTareaData4Preview($HTTP_POST_VARS['message'],0,1,1);
		} elseif ($HTTP_POST_VARS['nosmiley'] == 1) {
			$p_message = $myts->makeTareaData4Preview($HTTP_POST_VARS['message'],1,0,1);
		} else {
			$p_message = $myts->makeTareaData4Preview($HTTP_POST_VARS['message'],1,1,1);
		}
		themecenterposts($p_subject,$p_message);
		echo "<br />";
		$subject = $myts->makeTboxData4PreviewInForm($HTTP_POST_VARS['subject']);
		$message = $myts->makeTareaData4PreviewInForm($HTTP_POST_VARS['message']);
		$hidden = $myts->makeTboxData4PreviewInForm($HTTP_POST_VARS['hidden']);
		$viewmode = $myts->makeTboxData4PreviewInForm($HTTP_POST_VARS['viewmode']);
		include("include/forumform.inc.php");
		include("page_tail.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();
			}
			$editor = $xoopsUser->getVar("uname");
   			$on_date .= _MD_ON." ".formatTimestamp(time());
			//$message .= "\n\n<small>[ "._MD_EDITEDBY." ".$editor." ".$on_date." ]</small>";
		} else {
			$isreply = 0;
			if ( $xoopsUser && empty($HTTP_POST_VARS['noname']) ) {
				$uid = $xoopsUser->getVar("uid");
			} else {
				if ( $forumdata['forum_access'] == 2 ) {
					$uid = 0;
				} else {
					if ( !empty($HTTP_POST_VARS['topic_id']) ) {
						redirect_header("viewtopic.php?topic_id=".$HTTP_POST_VARS['topic_id']."&order=".$HTTP_POST_VARS['order']."&viewmode=".$HTTP_POST_VARS['viewmode']."&pid=".$HTTP_POST_VARS['pid']."&forum=".$HTTP_POST_VARS['forum']."",2,_MD_ANONNOTALLOWED);
					} else {
						redirect_header("viewforum.php?forum=".$HTTP_POST_VARS['forum']."",2,_MD_ANONNOTALLOWED);
					}
					exit();
				}
			}
			$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($GLOBALS['REMOTE_ADDR']);
			$forumpost->setUid($uid);
		}
		$forumpost->setSubject($HTTP_POST_VARS['subject']);
		$forumpost->setText($HTTP_POST_VARS['message']);
		$forumpost->setNohtml($HTTP_POST_VARS['nohtml']);
		$forumpost->setNosmiley($HTTP_POST_VARS['nosmiley']);
		$forumpost->setIcon($HTTP_POST_VARS['icon']);
		$forumpost->setAttachsig($HTTP_POST_VARS['attachsig']);
		if (!empty($HTTP_POST_VARS['notify'])) {
			$forumpost->setNotify($HTTP_POST_VARS['notify']);
		}
		$postid = $forumpost->store();

		if ( $isreply == 1 ) {
			$sql = "SELECT t.topic_notify, u.email, u.uname, u.uid FROM ".$xoopsDB->prefix("bb_topics")." t, ".$xoopsDB->prefix("users")." u WHERE t.topic_id = ".$HTTP_POST_VARS['topic_id']." AND t.topic_poster = u.uid";
   			if (!$result = $xoopsDB->query($sql)) {
				echo _MD_COULDNOTQUERY;
   			}
   			$m = $xoopsDB->fetchArray($result);
			$myuid = 0;
			if ( $xoopsUser ) {
				$myuid = $xoopsUser->uid();
			}
   			if ($m['topic_notify'] == 1 && $m['uid'] != $myuid && $m['uid'] != 0) {
      				// We have to get the mail body and subject line in the board default language!
      				$subject = _MD_REPLYPOSTED;
				
      				$message = sprintf(_MD_HELLO,$m['uname']);
				$message .= "\n\n";
				$message .= sprintf(_MD_URRECEIVING,$xoopsConfig['sitename']);
				$message .= "\n\n"._MD_CLICKBELOW;
				$message .= "\n\n";
				$message .= XOOPS_URL."/modules/newbb/viewtopic.php?forum=".$HTTP_POST_VARS['forum']."&topic_id=".$HTTP_POST_VARS['topic_id']."\n\n------------\n".$xoopsConfig['sitename']."\n".XOOPS_URL."";
				$xoopsMailer =& getMailer();
				$xoopsMailer->useMail();
				$xoopsMailer->setToEmails($m['email']);
				$xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
				$xoopsMailer->setFromName($xoopsConfig['sitename']);
				$xoopsMailer->setSubject($subject);
				$xoopsMailer->setBody($message);
				$xoopsMailer->send();
   			}
		}

		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(XOOPS_ROOT_PATH."/footer.php");
}
?>