<?php

// -----------------------------------------------------------------------
// This file is part of AROUNDMe
//
// Copyright (C) 2003 - 2007 Barnraiser
// http://www.barnraiser.org/
// info@barnraiser.org
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option) any
// later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with program; see the file COPYING. If not, write to the Free
// Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
// 02110-1301, USA.
// -----------------------------------------------------------------------

$menu_section = "me";

if (!isset($_SESSION['md5'])) {
	header("Location: index.php?http_referer=" . urlencode($_SERVER['REQUEST_URI']) . "&" . strip_tags(SID));
	exit;
}


require_once ("components/core/class/File.class.php");
$file = new File($db, $core_config['file']);

if (isset($_REQUEST['user_id'])) {
	$file->user_id = $_REQUEST['user_id'];
}

if (isset($core_config['module'])) {
	$file->core_config_module = $core_config['module'];
}

$file->module_id = 0; // core
$file->no_image = $core_config['file']['image']['thumb'][1]['no_image'];

$user->file = $file;


$blocked_users = selBlockedUsers($db);

if (isset($_REQUEST['identity_id']) && is_numeric($_REQUEST['identity_id'])) {

	$query = "
		SELECT user_id
		FROM " . $db->prefix . "_user_identity
		WHERE identity_id=" . $_REQUEST['identity_id']
	;
	
	$result = $db->Execute($query);
	
	if (isset($result->fields)) {
		$user_id = $result->fields['user_id'];
		$result->Close();
	}
	else {
		// log error here
	}


	$query = "
		SELECT 1
		FROM " . $db->prefix . "_user_blocked
		WHERE user_id=" . $_SESSION['user_id'] . "
		AND user_id_blocked=" . $user_id . ""
	;

	$result = $db->Execute($query);
	$blocked = (isset($result->fields['1']) & !empty($result->fields['1']));
	$result->Close();


	if (isset($_POST['frm_submit']) && !$blocked) {
		require_once('components/core/class/Kses/kses.php');
		
		$_POST['frm_block'] = kses_parse($_POST['frm_block'], 'min');
		
		$query = "
			INSERT INTO " . $db->prefix . "_user_blocked
			VALUES(" . $_SESSION['user_id'] . ",
				   " . $user_id . ",
				   '" . date('Y-m-d h:i:s', time()) . "',
				   '" . $_POST['frm_block'] . "',
				   " . $_REQUEST['identity_id'] . ")"
		;
		
		$result = $db->Execute($query);
		
		if (!isset($result->fields)) {
			// error-log here
		}

 		header("location: index.php?amn=core_blocked");
 		exit;
	}
	
 	$blocked_users = selBlockedUsers($db);
	
 	$output_identity = $user->selUserIdentity(null, $_REQUEST['identity_id']);
	$body->set("identity", $output_identity);

}
elseif (isset($_REQUEST['unblock_user_id']) && is_numeric($_REQUEST['unblock_user_id'])) {

	$query = "
		DELETE FROM " . $db->prefix . "_user_blocked
		WHERE user_id=" . $_SESSION['user_id'] . " AND 
		user_id_blocked=" . $_REQUEST['unblock_user_id']
	;

	$result = $db->Execute($query);
	if (!isset($result->fields)) {
		// log error here
	}

	header("location: index.php?amn=core_blocked");
	exit;
}

if (!empty($blocked_users)) {
	$body->set("blocked", $blocked_users);
}

function selBlockedUsers($db) {
// 	$query = "
// 		SELECT b.user_id, b.user_id_blocked, b.block_datetime, b.block_description, 
// 		i.identity_nic, i.identity_id
// 		FROM " . $db->prefix . "_user_blocked b
// 		INNER JOIN " . $db->prefix . "_user_identity i
// 		ON (i.user_id=b.user_id_blocked AND i.identity_type=2)
// 		WHERE b.user_id=" . $_SESSION['user_id'] . "
// 		ORDER BY b.block_datetime DESC"
// 	;
	$query = "
		SELECT b.user_id, b.user_id_blocked, b.identity_id, 
		b.block_datetime, b.block_description, i.identity_nic
		FROM " . $db->prefix . "_user_blocked b
		INNER JOIN " . $db->prefix . "_user_identity i
		ON i.identity_id=b.identity_id
		WHERE b.user_id=" . $_SESSION['user_id'] . "
		ORDER BY b.block_datetime DESC"
	;
	
	$result = $db->Execute($query);
	$temp = array();
	
	if (isset($result->fields)) {
		
		while(!$result->EOF) {
			array_push($temp, $result->fields);
			$result->moveNext();
		}
	}
	else {
		// log error here;
	}
	return $temp;
}

?>