<?php 
// ======================================================================== //
//                                                                          //
//   KooBI- Webportalsystem                                                 //
//   SCRiPTMAFiA 2005 - THE DiRTY HANDS ON YOUR SCRiPTS                     //
//                                                                          //
//   ====================================================================   //
//                                                                          //   
//   dream4 | webservices - Am Michelsgrund 7 - 69469 Weinheim - Germany    //    
//   tel. +49(0) 6201 - 980442 / fax. +49(0) 6201 - 64809                   //
//                                                                          //
// ======================================================================== //
ob_start();
$sql = new dbc("SELECT * FROM " . PREFIX . "_downloads WHERE id = '".escs($_REQUEST['fileid'])."'");
$row = $sql->fetchrow();


$sql_first = new dbc("SELECT id FROM " . PREFIX . "_downloadcat_restricted WHERE catid = '" .  $row->catid . "' limit 1");
$num_first = $sql_first->numrows();

if ($num_first > 0){
$dl_error = 1;

$sql_restricted = new dbc("SELECT * FROM " . PREFIX . "_downloadcat_restricted WHERE catid = '" .  $row->catid . "' AND uid = '" . UID . "' order by id desc limit 1");
$row_restricted = $sql_restricted->fetchrow();

	if ( ($row_restricted->time_till >= time()) && ($row_restricted->time_till != "") ){
		$dl_error = 0;
	}


	if($dl_error == 1){
		$time_ex = ($row_restricted->time_till != "") ? date("d.m.Y",$row_restricted->time_till) :  date("d.m.Y",time());
		$msg = str_replace("__DATUM__", $time_ex, $lang['error_dlperiod_expired']);
		echo "<pre>$msg</pre>";
		exit;
	}
}
//=====================================================================
// <<-- WENN BENUTZER AUF DIE DATEI ZUGREIFT (WIE AUCH IMMER,
//      UND DIESER KEINE RECHTE DAFÜR HAT, ENDET DER VORGANG HIER  -->>
//=====================================================================
$sql2 = new dbc("SELECT group_id FROM ".PREFIX."_downloadcat WHERE catid = '$row->catid' ");
$item = $sql2->fetchrow();
$g_array = explode(",", $item->group_id);
$group_ids = array();

// =====================================================================================
// miscrechte
// =====================================================================================
if (@is_numeric(UID)) {
	$queryfirst = "SELECT group_id_misc FROM " . PREFIX . "_user WHERE uid = " . UID;
	$result = new dbc($queryfirst);
	$user = $result->fetchrow();
	
	// wenn misc nicht leer ist...
		if($user->group_id_misc != ""){
			$group_ids_pre = UGROUP . ";" . $user->group_id_misc;
			$group_ids     = @explode(";", $group_ids_pre);
			
			// wennn misc leer ist und user eingeloggt ist
			} else {
			$group_ids[] = UGROUP;
		}
	// wenn user nicht eingeloggt ist
	} else {
	$group_ids[] = 2;
 }
// =====================================================================================



if (!@array_intersect($group_ids, $g_array)) {
		echo '<pre>' . $lang['popup_noperms'] . '</pre>';
		exit();
	}
	$tmpl->assign('lang', $lang);
	$tmpl->assign('theme', $THEME);
	$tmpl->assign('area', $area);
	$tmpl->assign('row', $row);
	$tmpl->assign('redir', redir());
	//=====================================================================
	// <<-- PASSWORTBERÜCKSICHTIGUNG  -->>
	//=====================================================================
	$passok = 1;
	if($row->password!=""){
		if(!($HTTP_SESSION_VARS['dlpass'.$row->id])){
			$HTTP_SESSION_VARS['dlpass'.$row->id] = $_REQUEST['apass'];
		}
		if( ($row->password!="") && ($row->password != $HTTP_SESSION_VARS['dlpass'.$row->id]) ){
			$HTTP_SESSION_VARS['dlpass'.$row->id] = "";
			$passok = -1;
			$valcontent = $tmpl->fetch('download/login.tpl');
		}
	} 
	
	$today = date("dmY", time());
	if($row->today != $today) {
		$sql = new dbc("UPDATE " . PREFIX . "_downloads set today='$today', hits_today='0' WHERE id = '$row->id'");
	}
	
	$sql = new dbc("SELECT hits_today,hits_perday FROM " . PREFIX . "_downloads WHERE id = '$row->id'");
	$row_today = $sql->fetchrow();
	if($row_today->hits_perday > 0){
		if($row_today->hits_today == $row_today->hits_perday){
			$passok = -1;
			$valcontent = $tmpl->fetch('download/download_limit.tpl');
		}
	}
	//=====================================================================
	// <<-- WENN PASSWORT GESETZT WURDE, LOGINFORM ANZEIGEN -->>
	//=====================================================================
	if($passok == 1){
		ob_end_flush();
		ob_end_clean();
		$tmpl->assign('content', $tmpl->fetch('download/downloadfile.tpl'));
		if($_REQUEST['action']=="dl"){ 
			$sql = new dbc("UPDATE " . PREFIX . "_downloads set hits=hits+1 WHERE id = '$row->id'");
			$sql = new dbc("UPDATE " . PREFIX . "_downloads set hits_today=hits_today+1 WHERE id = '$row->id'");
			
			
			if($row->dlurl_local != ""){
				header("Cache-control: private");
				header("Content-type: application/octet-stream"); 
				header("Content-disposition:attachment; filename=$row->dlurl_local");
				header("Content-Length: " . filesize("uploads/files/" . $row->dlurl_local));
				readfile("uploads/files/" . $row->dlurl_local);
				exit();
				
			} else {
				header("Location:$row->dlurl"); 
			}
			
		}
	}else{
		$tmpl->assign('content',$valcontent);
	}
	$sname .= SEPARATOR . $lang['download_thefile'] . $row->title;
?>