<?php
//=================================================================
// KOOBI R7 - CMS - PORTALSOFTWARE(c) DREAM4(r) 
// HTTP://WWW.DREAM4.DE
//=================================================================
// ACHTUNG!
// DIESER PROGRAMMCODE IST URHERRECHTLICH GESCHUETZT
// DIE URHEBERRECHTE LIEGEN BEI DEN JEWEILIGEN AUTOREN.
// AENDERUNGEN IN DIESER DATEI ERFOLGEN AUF EIGENE GEFAHR!
// ES IST NICHT GESTATTET, DEN COPYRIGHTHINWEIS ZU AENDERN ODER ZU
// ENTFERNEN. NUR WENN SIE EINE COPYRIGHTHINWEIS-ENTFERNUNG BESTELLT
// HABEN, DUERFEN SIE DIESEN ENTFERNEN.
//================================================================
// BEI FRAGEN WENDEN SIE SICH BITTE AN: KONTAKT@DREAM4.DE
//=================================================================
if(!defined("BASE_DIR")) exit;
if(!get_active('forums'))
{
	notActive();
}

if ((ISLOGGED != 1) && (UGROUP != 2))
{ 
	koobi_message('Forums_PostErrorNew', 'Forums_ErrorNewPostings',  HOME_PATH . '/index.php?p=showforums&amp;area=' . AREA);
}

//=================================================================
//=================================================================
$closed = $GLOBALS['db']->RowQuery("
	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 = " . $_REQUEST["toid"] . " AND f.id = t.forum_id");
		
//=================================================================
// ist user moderator
//=================================================================
$is_moderator = false;
$result = $GLOBALS['db']->Query("SELECT user_id FROM " . PREFIX . "_f_mods WHERE forum_id = '" . $closed->id . "'");
while ($user = $result->fetchrow())
{
	if ($user->user_id == UID) $is_moderator = true;
}

//=================================================================
// Forum geschlossen
//=================================================================

if ( ($closed->fstatus == FORUM_STATUS_CLOSED)  && (UGROUP != 1) && !$is_moderator)
{
	koobi_message('Forums_Title', 'Forums_MsgisClosed', $_SERVER['HTTP_REFERER']);
}

//=================================================================
// Thema geschlossen
//=================================================================
if ( ($closed->tstatus == FORUM_STATUS_CLOSED) && (UGROUP != 1) && !$is_moderator)
{
	koobi_message('Forums_Title','Forums_TopicClosed', $_SERVER['HTTP_REFERER']);
}

//=================================================================
// Gibt es das Thema und das Posting ueberhaupt schon?
//=================================================================
if (!topicExists($_REQUEST['toid']))
{
	koobi_message('Forums_Title','Forums_ErrorNoPermission',  HOME_PATH . '/index.php?p=showforums&amp;area=' . AREA);
}

//=================================================================
// Zugriffsrechte
//=================================================================
$permissions = getForumPermissionsByUser($closed->id, UID);
$cat_query = $GLOBALS['db']->Query("SELECT group_id FROM " . PREFIX . "_f_forum WHERE id = '" . $closed->id . "'");
while ($category = $cat_query->fetchrow_assoc())
{
	// miscrechte
	$group_ids = miscIds();
	$groups = explode(",", $category['group_id']);
	if (array_intersect($group_ids, $groups)) 
	{
		$permissions[FORUM_PERMISSION_CAN_SEE] = 1;
	}
	
	if (@$permissions[FORUM_PERMISSION_CAN_SEE] == 0)
	{
		koobi_message('Forums_Title', 'Forums_PermissionDenied',  HOME_PATH . '/index.php?p=showforums&amp;area=' . AREA);
	}	
}

//=================================================================
// 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)
	{
		koobi_message('Global_NoPermission', 'Global_NoPermission_t', HOME_PATH . '/index.php?p=showtopic&amp;toid=' . $_REQUEST['toid']);
	}
} else {
	// kann auf andere themen antworten
	if (@$permissions[FORUM_PERMISSION_CAN_REPLY_OTHER_TOPIC] == 0) 
	{
		koobi_message('Global_NoPermission', 'Global_NoPermission_t', HOME_PATH . '/index.php?p=showtopic&amp;toid=' . $_REQUEST['toid']);
	}
}

//=================================================================
// Ueberpruefung der Rechte und des Datums
//=================================================================
if (isset($_REQUEST['action'] ) && $_REQUEST['action'] == 'edit')
{
	$information = $GLOBALS['db']->RowQuery("
	SELECT 
		u.Id,
		UNIX_TIMESTAMP(p.datum) AS datum,
		UNIX_TIMESTAMP(NOW()) AS today
	FROM
		" . PREFIX . "_f_post AS p,
		" . PREFIX . "_benutzer AS u
	WHERE
		p.id = '" . $_REQUEST["pid"] . "' AND
		u.Id = p.uid
	");

	
	$curr_unix_stamp = $information->today;
	$post_unix_stamp = $information->datum;
	$information->uid = $information->Id;
	//=================================================================
	// 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( (!isset($permissions[FORUM_PERMISSION_CAN_EDIT_OTHER_POST]) || $permissions[FORUM_PERMISSION_CAN_EDIT_OTHER_POST] == 0) && (UGROUP != 1) ) 
	{
		//=================================================================
		// wenn nicht der beitragverfasser und der benutzer ist kein admin
		//=================================================================
		if (!isset($information->uid) || $information->uid == UID && $permissions[FORUM_PERMISSION_CAN_EDIT_OWN_POST] == 0) 
		{
			koobi_message('Global_NoPermission', 'Global_NoPermission_t', HOME_PATH . '/index.php?p=showtopic&amp;toid=' . $_REQUEST['toid']);
		}
		
		//=================================================================
		// wenn nicht der beitragverfasser und der benutzer ist kein admin
		//=================================================================
		if (!isset($information->uid) || $information->uid != UID && UGROUP != 1 && !$is_moderator)
		{
			koobi_message('Global_NoPermission', 'Global_NoPermission_t', HOME_PATH . '/index.php?p=showtopic&amp;toid=' . $_REQUEST['toid']);
		}
		
		//=================================================================
		// wenn die zeit fuer die editierung abgelaufen ist und
		// der benutzer ist kein admin
		//=================================================================
		if ($time_diff >= MAX_EDIT_PERIOD && UGROUP != 1 && !$is_moderator) 
		{
			koobi_message('Forums_EditPost', 'Forums_ErrorPostEditEnd', HOME_PATH . '/index.php?p=showtopic&amp;toid=' . $_REQUEST['toid']);
		}
	}
}

if (SMILIES == 1) $GLOBALS['tmpl']->assign('smilie', 1);

if ($_REQUEST['pid'] != '')
{
	$h_attachments_only_show='';
	//=================================================================
	// wir moechten nicht, dass der user bei einem zitat die 
	// Anlagen des posters angehaengt bekommt
	//=================================================================
	$attachment = ($_REQUEST['action']=="quote") ? '' : "p.attachment,";
	
	$message = $GLOBALS['db']->RowQuery("
	SELECT 
		u.Benutzername as uname, 
		p.message,
		p.id,
		p.title,
		$attachment 
		t.title AS topic,
		t.posticon,
		t.uid,
		t.id AS toid,
		t.forum_id 
	FROM 
		" . PREFIX . "_f_post AS p, 
		" . PREFIX . "_benutzer AS u,
		" . PREFIX . "_f_topic AS t
	WHERE 
		p.id = '" . $_REQUEST["pid"] . "' AND 
		p.uid = u.Id AND
		t.id = p.topic_id");
	
	if ($_REQUEST['action'] == 'quote')
	{
		$message->message = "[QUOTE][B]" . $GLOBALS['config_vars']['Forums_PostQuote_prefix'] . " " . $message->uname . "[/B]\n" . $message->message . "[/QUOTE]\r\n\r\n";
	} elseif ($_REQUEST['action'] == 'edit') {
		// $message = $message->message;
	}
	
	
	$attach = explode(";", @$message->attachment);
	if(@$message->attachment)
	{
		foreach($attach as $attachment)
		{
			$row_a = $GLOBALS['db']->RowQuery("SELECT * FROM " . PREFIX ."_f_attachment WHERE id='$attachment'");
			if(is_object($row_a)) 
			{
				$attach_img = attachmentImg($row_a->orig_name);
				$h_attachments_only_show[] = '<div id="div_'.$row_a->id.'" style=""><input type="hidden" name="attach_hidden[]" id="att_'.$row_a->id.'" value="'.$row_a->id.'" />' . $attach_img . ' <a href="index.php?p=forum&amp;action=getfile&amp;id='.$row_a->id.'&amp;f_id='.$message->forum_id.'&amp;t_id='.$message->toid.'">'.$row_a->orig_name.'</a>&nbsp;<a href="?p=misc&amp;do=delattach&amp;id='.$row_a->id.'&amp;file='.$row_a->filename.'" target="attachment_frame" onclick="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 class="absmiddle" src="theme/'.$GLOBALS['theme'].'/images/forums/delete_small.png" alt="" border="0" hspace="2" /></a></div>';
			}
		}
		
		$GLOBALS['tmpl']->assign('h_attachments_only_show', $h_attachments_only_show);
	}
	
	
	$GLOBALS['tmpl']->assign('attachments_hidden', @$message->attachment);
	$GLOBALS['tmpl']->assign('message', $message);
	$GLOBALS['tmpl']->assign('f_id', $closed->id);
}

//=================================================================
// hat user thema abonniert?
//=================================================================
$row =  $GLOBALS['db']->RowQuery("SELECT title,notification FROM " . PREFIX . "_f_topic WHERE id = '$_REQUEST[toid]'");
$notifactions = @explode(";", $row->notification);

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

$navigation = getNavigation($_REQUEST['toid'], 'topic') 
	. $GLOBALS['config_vars']['Forums_Splitter'] . '<a class="forum_links_navi" href="index.php?p=showtopic&amp;toid=' . $_REQUEST['toid'] . '&amp;fid=' . $closed->id . '&amp;page=1&amp;t=' . seotitle(koobispecialchars($row->title)) . '">' 
	. koobispecialchars($row->title) 
	. '</a>'
	. $GLOBALS['config_vars']['Forums_Splitter']
	. ((isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") ? $GLOBALS['config_vars']['Forums_f_edit'] : $GLOBALS['config_vars']['Forums_Header_postreply_form']);

$GLOBALS['tmpl']->assign('navigation', $navigation);
$GLOBALS['tmpl']->assign('treeview', @explode($GLOBALS['config_vars']['Forums_Splitter'], $navigation));

$GLOBALS['tmpl']->assign('top_title', $GLOBALS['config_vars']['Forums_Header_postreply_form']);

$stitle = $GLOBALS["config_vars"]["Forums_Title"];
$sname = str_replace($GLOBALS['config_vars']['Forums_Splitter'], " - ", strip_tags($navigation));


if(isset($_REQUEST['preview']) && $_REQUEST['preview']==1)
{
	$GLOBALS['tmpl']->assign("subject", $_REQUEST['subject']);
	$GLOBALS['tmpl']->assign("text", $_REQUEST['text']);
}

$items = array();
if((!is_mod($_REQUEST['toid'])) && (UGROUP != 1)) $extra = " AND opened='1'";
$sql = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_f_post WHERE topic_id = '$_REQUEST[toid]'   ORDER BY id DESC limit 10");
while($row = $sql->fetchrow())
{
	$row->user = fetchusername($row->uid);
	$row->message = ($row->use_bbcode == 1) ? kcodes($row->message) : nl2br($row->message);
	if ( ($row->use_smilies == 1) && (SMILIES==1) ) $row->message = dosmilies($row->message);
	$row->message = (!badwordreplace($row->message) || badwordreplace($row->message)=='') ? $row->message : badwordreplace($row->message) ;
	array_push($items,$row);
}

$_REQUEST['fid'] = $closed->id;
$GLOBALS['tmpl']->assign('aid', $closed->uid);
$GLOBALS['tmpl']->assign('items', $items);
$GLOBALS['tmpl']->assign('permissions', $permissions);
$GLOBALS['tmpl']->assign('maxlength_post', MAXLENGTH_POST);
$GLOBALS['tmpl']->assign('bbcodes', BBCODESITE);
$GLOBALS['tmpl']->assign('navigation', $navigation);
$GLOBALS['tmpl']->assign('max_post_length', MAXLENGTH_POST);
$GLOBALS['tmpl']->assign('listfonts',  fontdropdown());
$GLOBALS['tmpl']->assign('sizedropdown',  sizedropdown());
$GLOBALS['tmpl']->assign('colordropdown',  colordropdown());
$GLOBALS['tmpl']->assign('posticons', @getPosticons($message->posticon));
$GLOBALS['tmpl']->assign('listemos', listsmilies(0,1,1));
$GLOBALS['tmpl']->assign('topic_id', $_REQUEST['toid']);
$GLOBALS['tmpl']->assign('threadform', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forums/threadform.tpl'));
$GLOBALS['tmpl']->assign('action', 'index.php?p=addpost');
$GLOBALS['tmpl']->assign('pagetitle_html', $sname);
$GLOBALS['tmpl']->assign('SearchPop', popSearch());

if (isset($_REQUEST['action']) && $_REQUEST['action'] == "edit")
{
	$GLOBALS['tmpl']->assign('top_title', $GLOBALS['config_vars']['Forums_f_edit']);
	if ($message->uid == UID || UGROUP == 1 || $is_moderator)
	{
		$GLOBALS['tmpl']->assign("topic", $message->topic);
		$GLOBALS['tmpl']->assign("topicform", $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forums/topicform.tpl'));
	}
}

$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forums/addtopic.tpl')); 
?>