<?php

/*
 *	W-AGORA 4.0
 *	-----------
 *	$Id: getfile.php,v 1.23 2004/10/05 14:12:58 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($HTTP_GET_VARS['bn']) && !empty($HTTP_GET_VARS['att_id']) ) {
		$bn = $HTTP_GET_VARS['bn'];
		$att_id = (int) $HTTP_GET_VARS['att_id'];
	} elseif ( !empty($HTTP_POST_VARS['bn']) && !empty($HTTP_POST_VARS['att_id']) ) {
		$bn = $HTTP_POST_VARS['bn'];
		$att_id = (int) $HTTP_POST_VARS['att_id'];
	} elseif (isset($HTTP_SERVER_VARS['PATH_INFO']) && ereg('/', $HTTP_SERVER_VARS['PATH_INFO']) ) {
		$path_info =  preg_replace(":^/:", "", $HTTP_SERVER_VARS['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($HTTP_USER_AGENT,"MSIE 5") && !strstr($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);
	}
?>
