<?
include_once("./_common.php");
include_once("$g4[path]/memo.config.php");

if (!defined("_GNUBOARD_")) exit; // 禁止单独访问此页面 

if (!$kind or !$me_id)
    alert("没有设置下载信息设置");

switch ($kind) {
  case 'send'   : $sql = " select * from $g4[memo_send_table] where me_id = $me_id "; break;
  case 'recv'   : $sql = " select * from $g4[memo_recv_table] where me_id = $me_id "; break;
  case 'spam'   : $sql = " select * from $g4[memo_spam_table] where me_id = $me_id "; break;
  case 'save'   : $sql = " select * from $g4[memo_save_table] where me_id = $me_id "; break;
  case 'notice' : $sql = " select * from $g4[memo_notice_table] where me_id = $me_id "; break;
  default     : alert("系统错误 kind值错误");
}
$result = sql_fetch($sql);

if ($member[mb_id] != $result[memo_owner])
    alert("权限错误！您不能下载他人的附件");

$file_server = $result[me_file_server];
$file_local = $result[me_file_local];

$filepath="$g4[path]/data/memo2/$file_server";

//$original="$file_local"; -- UTF-8 文件名, NaviGator设计
if (preg_match("/^utf/i", $g4[charset])) 
    $original = urlencode($file_local); 
else 
    $original = $file_local; 

if (file_exists($filepath)) {
    if(eregi("msie", $_SERVER[HTTP_USER_AGENT]) && eregi("5\.5", $_SERVER[HTTP_USER_AGENT])) {
        header("content-type: doesn/matter");
        header("content-length: ".filesize("$filepath"));
        header("content-disposition: attachment; filename=\"$original\"");
        header("content-transfer-encoding: binary");
    } else {
        header("content-type: file/unknown");
        header("content-length: ".filesize("$filepath"));
        header("content-disposition: attachment; filename=\"$original\"");
        header("content-description: php generated data");
    }
    header("pragma: no-cache");
    header("expires: 0");
    flush();

    if (is_file("$filepath")) {
        $fp = fopen("$filepath", "rb");

        // 4.00 代替
        // 提升服务器性能减少负载的话相对 print ， echo 或 while下面的方法更加有效...
        //if (!fpassthru($fp)) {
        //    fclose($fp);
        //}

        while(!feof($fp)) { 
            echo fread($fp, 100*1024); 
            flush(); 
        } 
        fclose ($fp); 
        flush();
    } else {
        alert("文件不存在或路径错误！");
    }

} else {
    alert("找不到文件");
}
?>