<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
// www.bcoos.net 2006-09 1.0.2 GPL
//v1.3 www.exoops.info 2006/08 GPL 
//v1.2 www.exoops.info 2006/03 GPL
// $Id: post.php,v 1.10 2003/07/07 05:36:35 half-dead Exp $
// ------------------------------------------------------------------------- //
//               E-Xoops: Content Management for the Masses                  //
//                       < http://www.e-xoops.com >                          //
// ------------------------------------------------------------------------- //
// 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($_POST['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 = ".$_POST['forum']."";
		if (!$result = $db->query($sql)) {
			redirect_header("index.php", 2, _MD_CANTGETFORUM);
			exit();
		}
	$forumdata = $db->fetchArray($result);
	if ( $forumdata['forum_type'] == 1 ) {
		$accesserror = 0;
		if ( $xoopsUser ) {
			if ( !$xoopsUser->isAdmin($xoopsModule->mid()) ) {
				if ( !check_priv_forum_auth($xoopsUser->uid(), $_POST['forum'], TRUE) ) {
					$accesserror = 1;
				}
			}
		} else {
			$accesserror = 1;
		}
		if ( $accesserror == 1 ) {
			redirect_header("viewforum.php?order=".$_POST['order']."&viewmode=".$_POST['viewmode']."&forum=".$_POST['forum']."",2,_MD_NORIGHTTOPOST);
			exit();
		}
	}
	$accesserror = 0;
	if ( $forumdata['forum_access'] == 3 ) {
		if ( $xoopsUser ) {
			if ( !$xoopsUser->isAdmin($xoopsModule->mid()) ) {
				if ( !is_moderator($_POST['forum'], $xoopsUser->uid()) ) {
					$accesserror = 1;
				}
			}
		} else {
			$accesserror = 1;
		}
	} elseif ( $forumdata['forum_access'] == 1 && !$xoopsUser ) {
		$accesserror = 1;
	}
	if ( $accesserror == 1 ) {
		redirect_header("viewforum.php?order=".$_POST['order']."&viewmode=".$_POST['viewmode']."&forum=".$_POST['forum']."",2,_MD_NORIGHTTOPOST);
		exit();
	}
	if ( !empty($_POST['contents_preview']) ) {
		include_once(XOOPS_ROOT_PATH."/header.php");
		OpenTable();
		$p_subject = $myts->makeTboxData4Preview($_POST['subject']);
		if ($xoopsUser && $xoopsUser->isAdmin($xoopsModule->mid())) {
			$myts->setType('admin');
			} else {
				$myts->setType('user');
			}
		$p_message = $myts->makeTareaData4Preview($_POST['message'], intval($_POST['allow_html']), intval($_POST['allow_smileys']), intval($_POST['allow_bbcode']));
		themecenterposts($p_subject, $p_message);
		echo "<br />";
		$subject  = $myts->makeTboxData4PreviewInForm($_POST['subject']);
		$message  = $myts->makeTboxData4PreviewInForm($_POST['message']);
		$hidden   = $myts->makeTboxData4PreviewInForm($_POST['hidden']);
		$viewmode = $myts->makeTboxData4PreviewInForm($_POST['viewmode']);
		include_once("include/forumform.inc.php");
		CloseTable();
	} else {
		include_once("class/class.forumposts.php");
		if ( !empty($_POST['post_id']) ) {
			$editerror = 0;
			$forumpost = new ForumPosts($_POST['post_id']);
			if ( $xoopsUser ) {
				if ( !$xoopsUser->isAdmin($xoopsModule->mid()) ) {
					if ( $forumpost->uid() != $xoopsUser->getVar("uid") && !is_moderator($_POST['forum'], $xoopsUser->getVar("uid")) ) {
						$editerror = 1;
					}
				}
			} else { $editerror = 1;}
			if ( $editerror == 1 ) {
				redirect_header("viewtopic.php?topic_id=".$_POST['topic_id']."&post_id=".$_POST['post_id']."&order=".$_POST['order']."&viewmode=".$_POST['viewmode']."&pid=".$_POST['pid']."&forum=".$_POST['forum']."",2,_NOTALLOWEDTOEDIT);
				exit();
			}
		} else {
			$isreply = 0;
			if ( $xoopsUser && empty($_POST['noname']) ) {
				$uid = $xoopsUser->getVar("uid");
			} else {
				if ( $forumdata['forum_access'] == 2 ) { $uid = 0;
				} else {
					if ( !empty($_POST['topic_id']) ) { redirect_header("viewtopic.php?topic_id=".$_POST['topic_id']."&order=".$_POST['order']."&viewmode=".$_POST['viewmode']."&pid=".$_POST['pid']."&forum=".$_POST['forum']."",2,_ANONYMOUSCANNOTPOST); }
					else { redirect_header("viewforum.php?forum=".$_POST['forum']."",2,_ANONYMOUSCANNOTPOST); }
					exit();
				}
			}
			$forumpost = new ForumPosts();
			$forumpost->setForum($_POST['forum']);
			if (isset($_POST['pid']) && $_POST['pid'] != "") {
				$forumpost->setParent($_POST['pid']);
			}
			if (!empty($_POST['topic_id'])) {
				$forumpost->setTopicId($_POST['topic_id']);
				$isreply = 1;
			}
			$forumpost->setIp(_REMOTE_ADDR);
			$forumpost->setUid($uid);
		}
		$forumpost->setSubject($_POST['subject']);
		$message = $_POST['message'];
		if ( $xoopsUser && !empty($_POST['editpost']) ) {
			$editor   = $xoopsUser->getVar("uname");
			$on_date  = _MD_ON." ".formatTimestamp(time(), _MEDIUMDATESTRING);
			$message .= "\n\n[ "._EDITEDBY." ".$editor." ".$on_date." ]";
		}
		$forumpost->setText($message);
		$forumpost->setHtml($_POST['allow_html']);
		$forumpost->setSmileys($_POST['allow_smileys']);
		$forumpost->setBBcode($_POST['allow_bbcode']);
		$forumpost->setIcon($_POST['icon']);
		$forumpost->setAttachsig($_POST['attachsig']);
		if (!empty($_POST['notify'])) {
			$forumpost->setNotify($_POST['notify']);
		}
		if ($xoopsUser && $xoopsUser->isAdmin($xoopsModule->mid())) { $forumpost->setType('admin'); }
		else { $forumpost->setType('user'); }
		
		if (!$xoopsUser && $bcoosConfig['anonpost'] && $bcoosConfig['captcha_comments'] && function_exists('imagecreate')) {
			include_once(XOOPS_ROOT_PATH."/class/rc4.crypt.php");
			$rc4 = new rc4_crypt();
			$rc4->key(strtoupper($verify_text));
			$verify_crc = base64_decode($verify_crc);
			$rc4->decrypt($verify_crc);
			if ( empty($verify_text) || $verify_crc != "verified" ) {
				include_once(XOOPS_ROOT_PATH."/header.php");
				OpenTable();
				$p_subject = $myts->makeTboxData4Preview($_POST['subject']);
				if ($xoopsUser && $xoopsUser->isAdmin($xoopsModule->mid())) { $myts->setType('admin'); }
				else {$myts->setType('user'); }
				$p_message = $myts->makeTareaData4Preview($_POST['message'], intval($_POST['allow_html']), intval($_POST['allow_smileys']), intval($_POST['allow_bbcode']));
				echo "<center><b>"._US_BADVERIF."</b></center>";
				themecenterposts($p_subject, $p_message);
				echo "<br />";
				$subject  = $myts->makeTboxData4PreviewInForm($_POST['subject']);
				$message  = $myts->makeTboxData4PreviewInForm($_POST['message']);
				$hidden   = $myts->makeTboxData4PreviewInForm($_POST['hidden']);
				$viewmode = $myts->makeTboxData4PreviewInForm($_POST['viewmode']);
				include_once("include/forumform.inc.php");
				CloseTable();
				include_once(XOOPS_ROOT_PATH."/footer.php");//content
				exit();
			}
		}
		
		$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 = ".$_POST['topic_id']." AND t.topic_poster = u.uid";
			if (!$result = $db->query($sql)) {
				echo _MD_COULDNOTQUERY;
			}
			$m = $db->fetchArray($result);
			$myuid = 0;
			if ( $xoopsUser ) {
				$myuid = $xoopsUser->uid();
			}
			if ($m['topic_notify'] == 1 && $m['uid'] != $myuid && $m['uid'] != 0) {
				$subject = _MD_REPLYPOSTED;
				$message = sprintf(_HELLO,$m['uname']);
				$message .= "\n\n";
				$message .= sprintf(_MD_URRECEIVING, $bcoosConfig['title']);
				$message .= "\n\n"._MD_CLICKBELOW;
				$message .= "\n\n";
				$message .= XOOPS_URL."/modules/newbb/viewtopic.php?forum=".$_POST['forum']."&topic_id=".$_POST['topic_id']."\n\n------------\n".$bcoosConfig['title']."\n".XOOPS_URL."";
				$xoopsMailer =& getMailer();
				$xoopsMailer->useMail();
				$xoopsMailer->setToEmails($m['email']);
				$xoopsMailer->setFromEmail($bcoosConfig['adminmail']);
				$xoopsMailer->setFromName($bcoosConfig['title']);
				$xoopsMailer->setSubject($subject);
				$xoopsMailer->setBody($message);
				$xoopsMailer->send();
			}
		}
		if ( $_POST['viewmode'] == "flat" ) {
			redirect_header("viewtopic.php?topic_id=".$forumpost->topic()."&amp;post_id=".$postid."&amp;order=".$_POST['order']."&amp;viewmode=flat&amp;pid=".$_POST['pid']."&amp;forum=".$_POST['forum']."#".$postid."", 2, _THANKSSUBMIT);
			exit();
			} else {
				$post_id = $forumpost->postid();
				redirect_header("viewtopic.php?topic_id=".$forumpost->topic()."&amp;post_id=".$postid."&amp;order=".$_POST['order']."&amp;viewmode=thread&amp;pid=".$_POST['pid']."&amp;forum=".$_POST['forum']."#".$postid."", 2, _THANKSSUBMIT);
				exit();
			}
	}
include_once(XOOPS_ROOT_PATH."/footer.php");
}
?>
