<?php

#############################################################################
#                              Site@School Download module
# Site@School is a website management system designed for schools.
# This is a download module which can be used in Site@School.
#
# coded by Fred Stuurman (fc.stuurman@hccnet.nl)
############################################################################
# download.php: last changed 10/10/2006  version 2.4.10
#                  by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: download.php,v 1.2 2006/10/15 19:08:54 fstuurman Exp $
############################################################################
#                        LICENSE INFORMATION
# Copyright (C) 2002 - 2006 Municipality of Amsterdam, City District
#  'de Baarsjes', Amsterdam, the Netherlands.
#
# 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
# of the License, 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
##############################################################################
define('IN_SAS', true);

$cat_id = 0;
$dwnl_id = 0 ;

if (IsSet ($_GET['dwnl_id']))
{
	$dwnl_id = intval($_GET['dwnl_id']);
}
if (IsSet ($_GET['cat_id']))
{
	$cat_id = intval($_GET['cat_id']);
}

include ("../../configuration/database.inc.php"); // including database configuration file and connecting
mysql_connect("$db_server", "$db_username", "$db_password");
mysql_select_db("$db_name");

$downloads_table = $dbprefix . "m_downloads";
$downloads_categorytable = $dbprefix . "m_downloads_cat";

$query = "SELECT config_value FROM $table_configuration WHERE config_key='serverpath'";
$result = mysql_query($query) or die_script($query, mysql_error());
$serverpath = mysql_result($result, 0);
unset ($query);
unset ($result);

$query = "SELECT config_value FROM $table_configuration WHERE config_key='media_dir'";
$result = mysql_query($query) or die_script($query, mysql_error());
$media = mysql_result($result, 0);
unset ($query);
unset ($result);

$query = "SELECT config_value FROM $table_configuration WHERE config_key='user_path'";
$result = mysql_query($query) or die_script($query, mysql_error());
$cmsdir = mysql_result($result, 0);
unset ($query);
unset ($result);

$query = "SELECT public FROM $downloads_categorytable WHERE id = '" . $cat_id . "'";
$result = mysql_query($query);
$is_public = mysql_result($result, 0, "public");

if ($is_public == 1)
{
	require ("../../core/session.php"); //build a session
}

if (($is_public == 1) AND (!IsSet ($_SESSION['userid'])))
{
	exit;
}

//update download counter and last download date
mysql_query("UPDATE $downloads_table 
									SET counter = counter + 1,
									last_download = NOW() WHERE id = '$dwnl_id'");

$query = "SELECT filename,stored_file FROM $downloads_table WHERE id = '" . $dwnl_id . "'";
$result = mysql_query($query);
$filename = @ mysql_result($result, 0, "filename");
$stored_file = @ mysql_result($result, 0, "stored_file");

$file_extension = strtolower(substr(strrchr($filename, "."), 1));

switch ($file_extension)
{
	case "pdf" :
		$ctype = "application/pdf";
		break;
	case "exe" :
		$ctype = "application/octet-stream";
		break;
	case "zip" :
		$ctype = "application/zip";
		break;
	case "doc" :
		$ctype = "application/msword";
		break;
	case "xls" :
		$ctype = "application/vnd.ms-excel";
		break;
	case "ppt" :
		$ctype = "application/vnd.ms-powerpoint";
		break;
	case "gif" :
		$ctype = "image/gif";
		break;
	case "png" :
		$ctype = "image/png";
		break;
	case "jpe" :
		$ctype = "image/jpg";
		break;
	case "jpeg" :
		$ctype = "image/jpg";
		break;
	case "jpg" :
		$ctype = "image/jpg";
		break;
	default :
		$ctype = "application/force-download";
}

//set the header type for download 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"$filename\"");

//present file output to user.
$string = file_get_contents($serverpath . $cmsdir . '/' . $media . '/downloads/' . $cat_id . '/' . $stored_file);
echo $string;
?>