<?php
// ======================================================================== //
//   KOOBI - CMS                                                            //
//   SCRiPTMAFiA 2005 - THE DiRTY HANDS ON YOUR SCRiPTS                     //
//                                                                          //
//   DREAM4 - AM MICHELSGRUND 7 - 69469 WEINHEIM - GERMANY                  //    
//   TEL. +49(0) 6201 - 980442 / FAX. +49(0) 6201 - 64809                   //
// ======================================================================== //
if ( ($islogged == 0) && (UGROUP != 2) ){ 
	$EOUT = msg("f_posterror", "f_error_login", str_replace("__URL__", "index.php?p=showforums", $lang['redirect']), "index.php?p=showforums");
	$NOOUT = 1;
}

$q_closed = "
	SELECT
		f.id,
		f.status AS fstatus, 
		t.status AS tstatus,
		t.uid
	FROM
		" . PREFIX . "_f_forum AS f, 
		" . PREFIX . "_f_topic AS t
	WHERE
		t.id = " . $_GET["toid"] . " AND f.id = t.forum_id";

$r_closed = new dbc($q_closed);
$closed = $r_closed->fetchrow();

// ist user moderator
$is_moderator = false;

$query = "
	SELECT
		user_id
	FROM
		" . PREFIX . "_f_mods
	WHERE
		forum_id = " . $closed->id;

$result = new dbc($query);

while ($user = $result->fetchrow()) {
	if ($user->user_id == UID) $is_moderator = true;
}

// =========================================================
// <<-- forum geschlossen -->>
// =========================================================

if ( ($closed->fstatus == FORUM_STATUS_CLOSED)  && (UGROUP != 1) && !$is_moderator) {
	$EOUT = msg("f_forum", "f_msg_forumclosed", str_replace("__URL__", "index.php?p=showforums", $lang['redirect']), "index.php?p=showforums");
	$NOOUT = 1;
}

// =========================================================
// <<-- topic geschlossen -->>
// =========================================================
if ( ($closed->tstatus == FORUM_STATUS_CLOSED) && (UGROUP != 1) && !$is_moderator) {
	$EOUT = msg("f_forum", "f_msg_topicclosed", str_replace("__URL__", $_SERVER['HTTP_REFERER'], $lang['redirect']), $_SERVER['HTTP_REFERER']);
	$NOOUT = 1;
}

// =========================================================
// <<-- gibt es den topic und das posting ueberhaupt schon -->>
// =========================================================
if (!topicExists($_GET['toid'])) {
	$EOUT = msg("f_forum", "f_err_permited", str_replace("__URL__", "index.php?p=showforums", $lang['redirect']), "index.php?p=showforums");
	$NOOUT = 1;
}


// =========================================================
// <<-- zugriffsrechte -->>
// =========================================================
$permissions = getForumPermissionsByUser($closed->id, UID);
$cat_query = new dbc("SELECT group_id FROM " . PREFIX . "_f_forum WHERE id = '" . $closed->id . "'");
while ($category = $cat_query->fetchrow_assoc()) {
	
	// miscrechte
	include ( BASEDIR . "/system/misc_ids.php");
	
	$groups = explode(",", $category['group_id']);
	if (array_intersect($group_ids, $groups)) {
		$permissions[FORUM_PERMISSION_CAN_SEE] = 1;
	}
	
	if ($permissions[FORUM_PERMISSION_CAN_SEE] == 0){
		$EOUT = msg("f_forum", "f_noid", str_replace("__URL__", "index.php?p=showforums", $lang['redirect']), "index.php?p=showforums");
		$NOOUT = 1;
	}	
}

// =========================================================

// =========================================================
// <<-- wenn benutzer der verfasser des themas ist -->>
// =========================================================
if ($closed->uid == UID) {
	// kann auf eigene themen antworten
	if ($permissions[FORUM_PERMISSION_CAN_REPLY_OWN_TOPIC] == 0) {
		$EOUT = msg("nopermission", "nopermission_t", str_replace("__URL__", "index.php?p=showtopic&amp;toid=" . $_GET["toid"], $lang['redirect']), "index.php?p=showtopic&amp;toid=" . $_GET["toid"]);
		$NOOUT = 1;
	}
} else {
	// kann auf andere themen antworten
	if ($permissions[FORUM_PERMISSION_CAN_REPLY_OTHER_TOPIC] == 0) {
		$EOUT = msg("nopermission", "nopermission_t", str_replace("__URL__", "index.php?p=showtopic&amp;toid=" . $_GET["toid"], $lang['redirect']), "index.php?p=showtopic&amp;toid=" . $_GET["toid"]);
		$NOOUT = 1;
	}
}


// ueberpruefung der rechte und des datums
if ($_GET['action'] == "edit") {
	$q_information = "
	SELECT 
		u.uid,
		UNIX_TIMESTAMP(p.datum) AS datum,
		UNIX_TIMESTAMP(NOW()) AS today
	FROM
		" . PREFIX . "_f_post AS p,
		" . PREFIX . "_user AS u
	WHERE
		p.id = " . $_GET["pid"] . " AND
		u.uid = p.uid
	";
	
	$r_information = new dbc($q_information);
	$information = $r_information->fetchrow();
	
	$curr_unix_stamp = $information->today;
	$post_unix_stamp = $information->datum;
	
	// zeitdifferenz in stunden
	// zu einem integer casten
	$time_diff = (int) (($curr_unix_stamp - $post_unix_stamp) / 60 / 60);
	
	// wenn user andere beitraege nicht bearbeiten kann und kein admin ist ...
	if( ($permissions[FORUM_PERMISSION_CAN_EDIT_OTHER_POST] == 0) && (UGROUP != 1) ) {
		
		// wenn nicht der beitragverfasser und der benutzer ist kein admin
		if ($information->uid == UID && $permissions[FORUM_PERMISSION_CAN_EDIT_OWN_POST] == 0) {
			$EOUT = msg("nopermission", "nopermission_t", str_replace("__URL__", "index.php?p=showtopic&amp;toid=" . $_GET["toid"], $lang['redirect']), "index.php?p=showtopic&amp;toid=" . $_GET["toid"]);
			$NOOUT = 1;
		}
		
		// wenn nicht der beitragverfasser und der benutzer ist kein admin
		if ($information->uid != UID && UGROUP != 1 && !$is_moderator) {
			$EOUT = msg("nopermission", "nopermission_t", str_replace("__URL__", "index.php?p=showtopic&amp;toid=" . $_GET["toid"], $lang['redirect']), "index.php?p=showtopic&amp;toid=" . $_GET["toid"]);
			$NOOUT = 1;
		}
		
		// wenn die zeit fuer die editierung abgelaufen ist und
		// der benutzer ist kein admin
		if ($time_diff >= MAX_EDIT_PERIOD && UGROUP != 1 && !$is_moderator) {
			$EOUT = msg("f_msg_post_edit", "f_err_post_edit", str_replace("__URL__", "index.php?p=showtopic&amp;toid=" . $_GET["toid"], $lang['redirect']), "index.php?p=showtopic&amp;toid=" . $_GET["toid"]);
			$NOOUT = 1;
		}
	}
}

if (SMILIES == 1) $tmpl->assign("smilie", 1);

if ($_GET['pid'] != "") {

	// wir moechten nicht, dass der user bei einem zitat die anhaenge des posters angehaengt bekommt...
	$attachment = ($_REQUEST['action']=="quote") ? '' : "p.attachment,";
	
	$q_message = "
	SELECT 
		u.uname, 
		p.message,
		p.title,
		$attachment 
		t.title AS topic,
		t.posticon,
		t.uid
	FROM 
		" . PREFIX . "_f_post AS p, 
		" . PREFIX . "_user AS u,
		" . PREFIX . "_f_topic AS t
	WHERE 
		p.id = " . $_GET["pid"] . " AND 
		p.uid = u.uid AND
		t.id = p.topic_id";
	
	$r_message = new dbc($q_message);
	$message = $r_message->fetchrow();
	
	if ($_GET["action"] == "quote") {
		$message->message = "[QUOTE][B]" . $lang["f_quote_prefix"] . " " . $message->uname . "[/B]\n ". kspecialchars($message->message) . "[/QUOTE]\n\n";
	} elseif ($_GET["action"] == "edit") {
		// $message = $message->message;
	}
	
	
	$attach = explode(";", $message->attachment);
	if($message->attachment){
		foreach($attach as $attachment){
			$sql = new dbc("SELECT * FROM " . PREFIX ."_f_attachment WHERE id='$attachment'");
			$row_a = $sql->fetchrow();
			
			$h_attachments_only_show[] = '
			<div id="div_'.$row_a->id.'" style="display:">
			<input type="hidden" name="attach_hidden[]" id="att_'.$row_a->id.'" value="'.$row_a->id.'" />
			&bull; '.$row_a->orig_name.'
			<a title="'.$lang['del_attachment_alt'].'" href="javascript:;" 
			onclick="if(confirm(\''.$lang['confirm_del_attachment'].'\'))
			{
				document.getElementById(\'att_' . $row_a->id . '\').value=\'\';
				document.getElementById(\'div_' . $row_a->id . '\').style.display=\'none\';
				document.getElementById(\'hidden_count\').value = document.getElementById(\'hidden_count\').value - 1;
			}
			;"><img src="templates/'.$THEME.'/img/forum/del_attach.gif" alt="'.$lang['del_attachment_alt'].'" border="0" hspace="2" /></a>
			</div>';
		}
		
		$tmpl->assign("h_attachments_only_show", $h_attachments_only_show);
	}
	$tmpl->assign("attachments_hidden", $message->attachment);
	$tmpl->assign("message", $message);
	$tmpl->assign("f_id", $closed->id);
}

// hat user thema abonniert?
$sql = new dbc("SELECT notification FROM " . PREFIX . "_f_topic WHERE id = '$_GET[toid]'");
$row = $sql->fetchrow();
$notifactions = @explode(";", $row->notification);

if(@in_array(UID, $notifactions)) {
	$tmpl->assign('notification', 1);
}

$navigation = getNavigation($_GET["toid"], "topic");

$stitle = $lang["forumtitle"];
$sname = simpletitle(str_replace($lang['f_navi_separator'], " - ", strip_tags($navigation)));


if($_REQUEST['preview']==1){
	$tmpl->assign("subject", $_REQUEST['subject']);
	$tmpl->assign("text", $_REQUEST['text']);
}
#echo "<h1>HIER</h1>";
$items = array();
include( BASEDIR . "/system/addpost_last.php");
include( BASEDIR . "/inc/functions_post_array.php");

$tmpl->assign("aid", $closed->uid);
$tmpl->assign("items", $items);
$tmpl->assign("permissions", $permissions);
$tmpl->assign("maxlength_post", MAXLENGTH_POST);
$tmpl->assign("bbcodes", BBCODESITE);
$tmpl->assign("navigation", $navigation);
$tmpl->assign("max_post_length", MAXLENGTH_POST);
$tmpl->assign("listfonts",  fontdropdown());
$tmpl->assign("sizedropdown",  sizedropdown());
$tmpl->assign("colordropdown",  colordropdown());
$tmpl->assign("lang", $lang);
$tmpl->assign("posticons", getPosticons($message->posticon));
$tmpl->assign("listemos", listsmilies());
$tmpl->assign("topic_id", $_GET["toid"]);
$tmpl->assign("threadform", $tmpl->fetch("forums/threadform.tpl"));
$tmpl->assign("action", "index.php?p=addpost");

if ($_GET['action'] == "edit") {
	if ($message->uid == UID || UGROUP == 1 || $is_moderator) {
		$tmpl->assign("topic", $message->topic);
		$tmpl->assign("topicform", $tmpl->fetch("forums/topicform.tpl"));
	}
}

$tmpl->assign("content", parsetrue("container/".container("forum"), $stitle, $tmpl->fetch("forums/addtopic.tpl")));
?>