<?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.
// -----------------------------------------------------------------------


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

require_once ("class/File.class.php");
$file = new File($db, $core_config['file']);
$file->no_image = $core_config['file']['image']['thumb'][2]['no_image'];

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

$file->max_height = $core_config['file']['image']['max_height'];
$file->max_width = $core_config['file']['image']['max_width'];

if (isset($_REQUEST['module_id'], $_REQUEST['item_id']) && !empty($_REQUEST['module_id']) && !empty($_REQUEST['item_id'])) {
	$file->module_id = $_REQUEST['module_id'];
	$file->item_id   = $_REQUEST['item_id'];
}
else {
	$file->user_id   = $_SESSION['user_id'];
	$file->module_id = 0;
	$file->item_type = $core_config['file']['allocation']['type']['user'];
	$menu_section = "me";
}


if (isset($_REQUEST['image']) && $_REQUEST['image'] == "true") {
	$image = "true";
}
else {
	$image = null;
}

if (isset($_POST['frm_submit'])) {
	
	$file->setFile($_FILES['frm_file']);
	if (isset($_POST['frm_file_name'])) {
		$file->setSaveName($_POST['frm_file_name']);
	}
	if ($file->isOk()) {
		$file->uploadFile();
		if (empty($GLOBALS['am_error_log'])) {
			$file->createThumbNails();
		}
		if (isset($_REQUEST['module_id'], $_REQUEST['item_id'])) {
			header('location: index.php?amn=core_fil_view&module_id=' . $_REQUEST['module_id'] . '&item_id=' . $_REQUEST['item_id']);
			exit;
		}
		header("location: index.php?amn=core_fil_view");
		exit;
	}
}
elseif (isset($_POST['frm_update'])) {
	if (isset($_POST['frm_file_name'])) {
		$file->setSaveName($_POST['frm_file_name']);
	}
	$file->updateRecord($_REQUEST['update']);
	$selected_file = $file->selFiles($image, null, $_REQUEST['update']);
}
elseif (isset($_REQUEST['delete'])) {
	$file->deleteFile($_REQUEST['delete']);
	
	if (isset($_REQUEST['module_id'], $_REQUEST['item_id'])) {
		header('location: index.php?amn=core_fil_view&module_id=' . $_REQUEST['module_id'] . '&item_id=' . $_REQUEST['item_id']);
		exit;
	}
	
	header("location: index.php?amn=core_fil_view");
	exit;
}
elseif (isset($_REQUEST['update'])) {
	$selected_file = $file->selFiles($image, null, $_REQUEST['update']);
}
elseif (isset($_REQUEST['file'])) {
	$selected_file = $file->selFiles($image, null, $_REQUEST['file']);
}

$images = $file->selFiles("images", "t2");
$files  = $file->selFiles();
	
$body->set("files", $files);
$body->set("images", $images);
	
if (isset($selected_file)) {
	$body->set("selected_file", $selected_file);
}
	
$output_file_statistics = $file->getAllocation();

$body->set("file_statistics", $output_file_statistics);


?>