<?php
include_once("common/config.php");
include_once("classes/dbclass.php");
require_once('conn.php');
include_once("common/publicfun.php");

$msg_id      = intval($_GET["msg_id"]);
$category_id = intval($_GET["cid"]);
$user_id     = $_SESSION["uid"];

$Money=0;
$PreLink=strtolower($_SERVER["HTTP_REFERER"]);
$strReferer=strtolower($_SERVER["SERVER_NAME"]);

if ($user_id!=""){
   $sql="select UserName,Money from {$tablepre}scuser where ID=".$user_id;
   $myrow = $db->get_row($sql);
   $Money = $myrow["Money"];
}

//防盗链功能建议用户用IIS防盗链软件
if ($linkoff==1){
   $a=explode($strReferer,$PreLink);
   if (count($a)<1){
      header("Location:./");
      exit;
   }
}

//如果开启了登录后才能下,则未登录的就不允许下载
if ($downoff==1 && $user_id==''){
   echo "<script language='javascript'>alert('登录后才能下载资源!');";
   echo "window.location.href='./';</script>";
   exit; 
}

//$sql="SELECT *,(select category_id from {$tablepre}categorymng where msg_id='".$msg_id."') as category_id FROM {$tablepre}message where msg_id=".$msg_id;
$sql   ="SELECT * FROM {$tablepre}message where msg_id=".$msg_id;
$myrow = $db->get_row($sql);

if (!empty($myrow)){
   $msg_integral=$myrow["msg_integral"];
   $msg_poster=$myrow["msg_poster"];

   if ($html_flag==1){
      $lnk="view-".$category_id."-".$msg_id.".html";
   }else{
      $lnk="view.php?cid=".$category_id."&msg_id=".$msg_id."";
   }
   
   if ($msg_integral>0 && $user_id!=''){
      if ($Money-$msg_integral>=0){
         $str = "SELECT * FROM {$tablepre}mydown where scuser_id=".$user_id." and msg_id=".$msg_id." limit 1";
         $row = $db->get_row($str);
		 if (empty($row)){
			$db->exec("update {$tablepre}scuser set Money=Money-".$msg_integral." where ID=".$user_id);
		    $db->exec("update {$tablepre}scuser set Money=Money+".$msg_integral." where ID=".$msg_poster);
			$sql="insert into {$tablepre}mydown(msg_id,scuser_id,integral) values(".$msg_id.",".$user_id.",'".$msg_integral."')";
			$db->exec($sql);
		 }
	  }else{
         echo "<script language='javascript'>alert('您积分不足不能下载，请先充值后再下!');";
         echo "window.location.href='./';</script>";
         exit; 
	  }
   }elseif ($msg_integral==0 && $user_id!=''){
      
      $str = "SELECT * FROM {$tablepre}mydown where scuser_id=".$user_id." and msg_id=".$msg_id." limit 1";
      $row = $db->get_row($str);
	  if (empty($row)){
		 $sql="insert into {$tablepre}mydown(msg_id,scuser_id,integral) values(".$msg_id.",".$user_id.",'".$msg_integral."')";
		 $db->exec($sql);
	  }
   }


   $db->exec("update {$tablepre}categorymng set hits_num=hits_num+1,down_num=down_num+1 where msg_id=".$msg_id);

   //$DownUrl = $site_url.'/'.$myrow["msg_downurl"];
   $DownUrl = $myrow["msg_downurl"];
   $ext = end(explode('.',$DownUrl));
   $ext = strtolower($ext);
   if ($DownUrl=="" || $DownUrl=="#" || $ext=='pdf' || $ext=='odt' || $ext=='ods' || $ext=='odp'){
      //header("Location:".$_SERVER['HTTP_REFERER']);
	  header("Location:".$lnk);
   }else{
      header("Location:".$DownUrl);
   }
   exit;

}else{
   //header("Location:./");
   header("Location:".$_SERVER['HTTP_REFERER']);
}
?>