<?php
include("header.php");
include_once(XOOPS_ROOT_PATH."/class/xoopscomments.php");
if ( isset($HTTP_POST_VARS['op']) ){
	switch($HTTP_POST_VARS['op']){
	    case "preview":
		include(XOOPS_ROOT_PATH."/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_comment = $myts->makeTareaData4Preview($HTTP_POST_VARS['message'],0,0,1);
		} elseif ( $HTTP_POST_VARS['nohtml'] == 1 ) {
			$p_comment = $myts->makeTareaData4Preview($HTTP_POST_VARS['message'],0,1,1);
		} elseif ( $HTTP_POST_VARS['nosmiley'] == 1 ) {
			$p_comment = $myts->makeTareaData4Preview($HTTP_POST_VARS['message'],1,0,1);
		} else {
			$p_comment = $myts->makeTareaData4Preview($HTTP_POST_VARS['message'],1,1,1);
		}
		themecenterposts($p_subject,$p_comment);
		$icon = $HTTP_POST_VARS['icon'];
		$noname = $HTTP_POST_VARS['noname'];
		$nohtml = $HTTP_POST_VARS['nohtml'];
		$nosmiley = $HTTP_POST_VARS['nosmiley'];
		$pid = $HTTP_POST_VARS['pid'];
		$item_id = $HTTP_POST_VARS['item_id'];
		$comment_id = $HTTP_POST_VARS['comment_id'];
		$order = $HTTP_POST_VARS['order'];
		$subject = $myts->makeTboxData4PreviewInForm($HTTP_POST_VARS['subject']);
		$message = $myts->makeTareaData4PreviewInForm($HTTP_POST_VARS['message']);
		OpenTable();
		include(XOOPS_ROOT_PATH."/include/commentform.inc.php");
		CloseTable();
		break;
	    case "post":
		if ( !empty($HTTP_POST_VARS['comment_id']) ) {
			$artcomment = new XoopsComments($xoopsDB->prefix("comments"),$HTTP_POST_VARS['comment_id']);
			$accesserror = 0;
			if ( $xoopsUser ) {
				if ( !$xoopsUser->isAdmin($xoopsModule->mid()) ) {
					if ( $artcomment->getVar("user_id") != $xoopsUser->getVar("uid") ) {
						$accesserror = 1;
					}
				}
			} else {
				$accesserror = 1;
			}
			if ( $accesserror == 1 ) {
				redirect_header("article.php?storyid=".intval($HTTP_POST_VARS['item_id'])."&comment_id=".intval($HTTP_POST_VARS['comment_id'])."&order=".intval($HTTP_POST_VARS['order']),1,_NW_EDITNOTALLOWED);
				exit();
			}
		} else {
			$artcomment = new XoopsComments($xoopsDB->prefix("comments"));
			$artcomment->setVar("pid", $HTTP_POST_VARS['pid']);
			$artcomment->setVar("item_id", $HTTP_POST_VARS['item_id']);
			$artcomment->setVar("ip", $GLOBALS['REMOTE_ADDR']);
			if ( $xoopsUser ) {
				$uid = $xoopsUser->getVar("uid");
			} else {
				if ( $xoopsConfig['anonpost'] == 1 ) {
					$uid = 0;
				} else {
					redirect_header("article.php?storyid=".$HTTP_POST_VARS['itemid']."&comment_id=".$HTTP_POST_VARS['comment_id']."&order=".$HTTP_POST_VARS['order'],1,_NW_ANONNOTALLOWED);
					exit();
				}
			}
			$artcomment->setVar("user_id", $uid);
		}
		$artcomment->setVar("subject", $HTTP_POST_VARS['subject']);
		$artcomment->setVar("comment", $HTTP_POST_VARS['message']);
		$artcomment->setVar("nohtml", $HTTP_POST_VARS['nohtml']);
		$artcomment->setVar("nosmiley", $HTTP_POST_VARS['nosmiley']);
		$artcomment->setVar("icon", $HTTP_POST_VARS['icon']);
		$newtid = $artcomment->store();
		redirect_header("article.php?storyid=".intval($HTTP_POST_VARS['item_id'])."&comment_id=".$newtid."&order=".intval($HTTP_POST_VARS['order'])."#".$newtid."",2,_NW_THANKSFORPOST);
		exit();
		break;
	}
} else {
	redirect_header("index.php",2);
	exit();
}
include(XOOPS_ROOT_PATH."/footer.php");
?>