<?php

/*
 *	W-AGORA 4.0
 *	-----------
 *	$Id: getfile.php,v 1.24 2005/06/03 14:54:08 mdruilhe Exp $
 *	Usage:		display an attachment
 *	Author:		Marc Druilhe <mdruilhe@w-agora.com>
 */
 
	$bn_action="getfile";
	$bn_action_mode="R";
	$no_cookie=1;

	if (!empty($_GET['bn']) && !empty($_GET['att_id']) ) {
		$bn = $_GET['bn'];
		$att_id = (int) $_GET['att_id'];
	} elseif ( !empty($_POST['bn']) && !empty($_POST['att_id']) ) {
		$bn = $_POST['bn'];
		$att_id = (int) $_POST['att_id'];
	} elseif (isset($_SERVER['PATH_INFO']) && ereg('/', $_SERVER['PATH_INFO']) ) {
		$path_info =  preg_replace(":^/:", "", $_SERVER['PATH_INFO']);
		list ($bn, $att_id, $filename) = split("/", $path_info);
		$att_id = (int) $att_id;	
	} else {
		die("Invalid argument");
	}

	include "init.inc";

	if (empty ($att_id)) {
		display_header ("error", $header_footer);
		MsgForm (ERROR_SPECIFY_KEY, "list.$ext");
		display_footer ($header_footer);
	}

	$att = $db->getAttachments ($bn_db, 0, $att_id);
	if (!is_array($att) ) {
		display_header ("error", $header_footer);
		MsgForm (sprintf(ERROR_FILE_NOT_FOUND, $att_id), "list.$ext");
		display_footer ($header_footer);
	}

	$att_name = $att[0]["att_name"];
	$att_type = $att[0]["att_type"];
	$att_size = $att[0]["att_size"];
	$inline   = (int) $att[0]["inline"];
	$key      = (int) $att[0]["note_id"];

	/* Execute before_access program */
	if (!empty($before_access)) include $before_access;
	
	if ($att_name && file_exists ("$bn_dir_notes/$att_id.$att_name")) {

		// Increments downloads count (silently)
		if (!$bn_no_stats && !$inline) { 
			$db->setHaltOnError ("no");
			$db->query("UPDATE $bn_db SET downloads=downloads+1 WHERE cle=$key");
		}

		// close database connection
		$db->closeDB();

		// output file to the browser
		header ("Content-Type: $att_type; name=\"$att_name\"");
		header ("Content-length: $att_size");
		if (empty($path_info)) {
			if (strstr($_SERVER['HTTP_USER_AGENT'],"MSIE 5") && !strstr($_SERVER['HTTP_USER_AGENT'],"Opera")) {
				header("Content-Disposition: filename=\"$att_name\"");
			} else {
				header ("Content-Disposition: attachment; filename=\"$att_name\"");
			}
		}
		readFile ("$bn_dir_notes/$att_id.$att_name");
		exit;
	} else {
		display_header ("error", $header_footer);
		MsgForm (sprintf(ERROR_FILE_NOT_FOUND, "$bn_dir_notes/$att_id.$att_name"), "list.$ext", "");
		display_footer ($header_footer);
	}
?>
