<?php
// $Id: post.php,v 1.1 2004/07/11 10:28:23 Farsus Exp $
// -------------------------------------------------------------------------//
//			                 RUNCMS                                         //
//                                                                          //
//	 reliable - 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");

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 ".$db->prefix("bb_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);
	if ( $forumdata['forum_type'] == 1 ) {
		$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 ( !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 {
				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(_REMOTE_ADDR);
			$forumpost->setUid($uid);
		}
		$forumpost->setSubject($HTTP_POST_VARS['subject']);

		$message = $HTTP_POST_VARS['message'];
		if ( $xoopsUser && !empty($HTTP_POST_VARS['editpost']) ) {
			$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']);
		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 ".$db->prefix("bb_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 .= XOOPS_URL."/modules/newbb/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();
			}
		}

		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");
}
?>
