<?php
defined('IN_TS') or die('Access Denied.');

function download($dir,$name)
{
	$arr=explode('.', $dir);
	$ext=end($arr);  //end()返回数组的最后一个元素
	if($ext=='pdf')
	{
		$file = fopen($dir,"r"); // 打开文件
		// 输入文件标签
		Header("Content-type: application/pdf");
		Header("filename:" . $name);
		// 输出文件内容
		echo fread($file,filesize($dir));
		fclose($file);
	}else
	{	
		$file = fopen($dir,"r"); // 打开文件
		// 输入文件标签
		Header("Content-type: application/octet-stream");
		Header("Accept-Ranges: bytes");
		Header("Accept-Length: ".filesize($dir));
		Header("Content-Disposition: attachment; filename=" . $name);
		// 输出文件内容
		echo fread($file,filesize($dir));
		fclose($file);			
		
	}

	
}


    if($ts=='')
    {
        echo "gun!";
        exit;
        
    }
    if($ts=='-1')
    {
        echo "erro";
        exit;
        
    }
    $userid = aac('user')->isLogin();
    $ts=tsUrlCheck($ts);
    if(aac('user')->isPublisher()==false) {tsNotice('你可能被限制或没有验证邮箱，检查邮箱是否认证。','->点击认证<-',tsUrl('user','verify'));}
    $userme=aac('user')->getOneUser($userid);
   
    $the_file=$new['pubs']->find('editor',array(
                                                'pwd'=>$ts,
                                                
                                                ));
    if($the_file)
    {
        $file_dir = "uploadfile/editor/".$the_file['url'];
        if (!file_exists($file_dir)) { //检查文件是否存在
            echo "文件已删除";
            exit;
        }else{
            if($the_file['userid']==$userid)
            {
				download($file_dir,$the_file['title']);
                exit;
            }else
            {
                if(aac('user')->delScore($userid,"download file:" . $the_file['title'],2))
                {
                    download($file_dir,$the_file['title']);
                    aac('user')->addScore($the_file['userid'],$userme['username']."download:" . $the_file['title'],1,1);
                    exit;
                }else
                {
                    tsNotice('下载文件需要2积分，你似乎不够！');
                }
            }
          
           
            
        }
        
    }else
    {
        echo "文件已删除";
        exit;
        
    }
    
    /*
    $file_name = "187.doc";
    $file_dir = "uploadfile/editor/0/0/";
    if (!file_exists($file_dir . $file_name)) { //检查文件是否存在
        echo "文件已删除";
        exit;
    }else{
        
        $file = fopen($file_dir . $file_name,"r"); // 打开文件
        // 输入文件标签
        Header("Content-type: application/octet-stream");
        Header("Accept-Ranges: bytes");
        Header("Accept-Length: ".filesize($file_dir . $file_name));
        Header("Content-Disposition: attachment; filename=" . $file_name);
        // 输出文件内容
        echo fread($file,filesize($file_dir . $file_name));
        fclose($file);
        exit;
        
    }
   */

?>
