<?php
//KasP DETECTED
define('IN_PHPBB', true);
$phpbb_root_path = './';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);

$userdata = session_pagestart($user_ip, PAGE_GENERAL);
init_userprefs($userdata);
if ( !$userdata['session_logged_in'] )
{
	$redirect = "index.$phpEx";
	header('Location: ' . append_sid("login.$phpEx?redirect=$redirect", true));
}

if (isset($_GET['go'])) 
{
	$id = intval($_GET['id']);
	$sql = "SELECT *
		FROM ".OTVET_TABLE."
		WHERE topic_id = $id";
	$result = $db->sql_query($sql);
		if (!$result)
	{
		message_die(GENERAL_MESSAGE, 'Не найдено');
	}
	$watt = $db->sql_fetchrow($result);
	$topic_id = $watt['topic_id'];
	$post_id = $watt['post_id'];
	$sql = "DELETE
		FROM ".OTVET_TABLE."
		WHERE topic_id = $id
		AND post_to_user_id =".$userdata['user_id'];
		if ( !($result = $db->sql_query($sql)) )
        {
		    message_die(GENERAL_MESSAGE, 'Error!'); 
        }
		
	$URL="viewtopic.$phpEx?t=$topic_id&p=$post_id#$post_id";
	header ("Location: $URL");
		
}
else
	{
	include($phpbb_root_path . 'includes/page_header.'.$phpEx);

	$template->set_filenames(array(
		'body' => 'profile_otv.tpl')
		);
	
$numb_strok = 50;
$user = $userdata['user_id'];
$sql = "SELECT * FROM ".OTVET_TABLE." WHERE post_to_user_id = $user";
if (!$result = $db->sql_query($sql)){message_die(GENERAL_ERROR, 'Could not query users', '', __LINE__, __FILE__, $sql);}
$total = $db->sql_fetchrowset($result);
$total_new = array();
$total_new_n = array();
for($i = 0; $i < count($total); $i++) {$total_id[$i] = $total[$i]['topic_id'];}
for($i = 0; $i < count($total); $i++) 
{
	if(!in_array($total_id[$i], $total_new))
	{
		$index_new = count($total_new);
		for($s=0; $s<count($total); $s++)
		{
			if ($total_id[$s] == $total_id[$i])
			{
				$total_new_n[$index_new] = $total_new_n[$index_new]+1;
			}
		}
		$total_new[$index_new] = $total_id[$i];
	}
}
for($i = 0; $i < count($total_new); $i++){$total_new_join[$i] = $total_new_n[$i] . '.' . $total_new[$i];}
if ($total_new_join != null){rsort($total_new_join);}
$watt = (count($total_new_join) < $numb_strok+1) ? count($total_new_join) : $numb_strok;
for($i = 0; $i < count($total_new); $i++)
{
     $topic_temp = explode(".",$total_new_join[$i]);
     $topic_id = $topic_temp[1];
	 
    $sql = "SELECT *
	FROM ".OTVET_TABLE."
	WHERE post_to_user_id = ".$user."
	AND topic_id = ".$topic_id; 
	
	if ( !($result = $db->sql_query($sql)) ) 
	{ 
		message_die(CRITICAL_ERROR, 'Error Getting Forums!'); 
	}
	$tota = $db->sql_fetchrow($result);
		$topic_idd[$i] = $tota['topic_id'];
		$id[$i] = $tota['id'];
		$pishet[$i] = $tota['poster_name'];
		$post_id[$i] = $tota['post_id'];
		$otv[$i] = $tota['post_to_id'];
			
		$sql = "SELECT count(*) AS total
				FROM ".OTVET_TABLE."
				WHERE post_to_user_id = ".$user."
				AND topic_id = $topic_id"; 
		$result = $db->sql_query($sql);
		if (!$result)
	{
		message_die(GENERAL_ERROR, 'Unable to get attachment information.', '', __LINE__, __FILE__, $sql);
	}
		$okle = $db->sql_fetchrow($result);
		if ( $okle['total'] > 0 ){$okle = $okle['total'];} else {$okle = 0;}
	
		$sql = "SELECT * FROM ".TOPICS_TABLE."
			WHERE topic_id = ".$topic_id;
		if ( !($result = $db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR , "Could not get");
			}
			$topic = $db->sql_fetchrowset($result);
			for($k = 0; $k < count($topic); $k++)
		{
			$topic[$k] = $topic[$k]['topic_title'];
			$topic = $topic[$k];
		}
			$template->assign_block_vars('logs', array(
			'ID' => $topic_idd[$i],
			'TT' => $okle,
			'AFTOR' => $pishet[$i],
			'HREF'=> append_sid('formsg.'.$phpEx.'?go&id='.$topic_idd[$i].'&p='.$post_id[$i]),
			'IDD' => $id[$i],
			'FORUM' => $forum_id[$i],
			'POST' => $post_id[$i],
			'TITLE' => $topic
			)
		);
	}
}
$template->pparse('body');

include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
//KasP DETECTED
?>