<?php

/*
	[SupeSite/X-Space] (C)2001-2006 Comsenz Inc.
	¤U¸ü³B²z

	$RCSfile: batch.download.php,v $
	$Revision: 1.17 $
	$Date: 2006/10/27 01:24:52 $
*/

include_once('./include/main.inc.php');

$aid = empty($_GET['aid'])?0:intval($_GET['aid']);
if(empty($aid)) exit('Access Denied');

include_once(S_ROOT.'./include/common.inc.php');

getcookie(1);
$query = $_SGLOBAL['db']->query('SELECT a.*, i.*, it.typename, it.viewperm, it.replyperm, it.getattachperm FROM '.tname('attachments').' a LEFT JOIN '.tname('spaceitems').' i ON i.itemid=a.itemid LEFT JOIN '.tname('itemtypes').' it ON it.typeid=i.itemtypeid WHERE a.aid=\''.$aid.'\'');
if($item = $_SGLOBAL['db']->fetch_array($query)) {
	if($_SGLOBAL['supe_uid'] != $item['uid']) {
		if($item['folder'] != 1) {
			messagebox('error', 'not_found');
		}
		if(!checkfriend($item['getattachperm'], $item['uid'])) {
			messagebox('error', 'not_found');
		}
	}
} else {
	messagebox('error', 'not_found');
}

$_SGLOBAL['db']->query('UPDATE '.tname('attachments').' SET downloads=downloads+1 WHERE aid=\''.$aid.'\'');

$filename = A_DIR.'/'.$item['filepath'];
if(is_readable($filename)) {
	if(!empty($item['isimage'])) {
		echo '<img src="'.A_URL.'/'.$item['filepath'].'" />';
	} else {
		header('Expires: '.gmdate('D, d M Y H:i:s', $_SGLOBAL['timestamp'] + 31536000).' GMT');
		header('Content-Encoding: none');
		header('Content-Length: '.$item['size']);
	
		header('Content-Disposition: attachment; filename='.$item['filename']);
		header('Content-Type: application/octet-stream');
		readfile($filename);
	}
} else {
	messagebox('error', 'not_found');
}

?>