<?
$no = (int)$no;
include_once("./_common.php");
	function download_header($filename, $mode = "write", $filepath = "") {
		if(strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
			$filename = rawurlencode($filename);
			$filename = preg_replace('/\./', '%2e', $filename, substr_count($filename, '.') - 1);
		}
		
		header("Cache-Control: "); 
		header("Pragma: "); 
		header("Content-Type: application/octet-stream"); 
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
		if ($mode == "write") {
			$file_size = 0;
		}
		else {
//			$fp = fopen($filepath, 'rb');
			header("Content-Length: " .(string)(filesize($filepath))); 
		}
		
		
		header('Content-Disposition: attachment; filename="'.$filename.'"'); 
		header("Content-Transfer-Encoding: binary\n"); 
		flush();

	}
	

@include_once("$board_skin_path/download.head.skin.php");

// ÄíÅ°¿¡ ÀúÀåµÈ ID°ª°ú ³Ñ¾î¿Â ID°ªÀ» ºñ±³ÇÏ¿© °°Áö ¾ÊÀ» °æ¿ì ¿À·ù ¹ß»ý
// ´Ù¸¥°÷¿¡¼­ ¸µÅ© °Å´Â°ÍÀ» ¹æÁöÇÏ±â À§ÇÑ ÄÚµå
if (!get_session("ss_view_{$bo_table}_{$wr_id}"))
    alert("Àß¸øµÈ Á¢±ÙÀÔ´Ï´Ù.");

$sql = " select bf_source, bf_file from $g4[board_file_table] where bo_table = '$bo_table' and wr_id = '$wr_id' and bf_no = '$no' ";
$file = sql_fetch($sql);
if (!$file[bf_file])
    alert_close("ÆÄÀÏ Á¤º¸°¡ Á¸ÀçÇÏÁö ¾Ê½À´Ï´Ù.");

if ($member[mb_level] < $board[bo_download_level]) {
    $alert_msg = "´Ù¿î·Îµå ±ÇÇÑÀÌ ¾ø½À´Ï´Ù.";
    if ($member[mb_id])
        alert($alert_msg);
    else
        alert($alert_msg . "\\n\\nÈ¸¿øÀÌ½Ã¶ó¸é ·Î±×ÀÎ ÈÄ ÀÌ¿ëÇØ º¸½Ê½Ã¿À.", "./login.php?wr_id=$wr_id&$qstr&url=".urlencode("$g4[bbs_path]/board.php?bo_table=$bo_table&wr_id=$wr_id"));
}

$filepath = "$g4[path]/data/file/$bo_table/$file[bf_file]";
$filepath = addslashes($filepath);
if (!is_file($filepath) || !file_exists($filepath))
    alert("ÆÄÀÏÀÌ Á¸ÀçÇÏÁö ¾Ê½À´Ï´Ù.");

// »ç¿ëÀÚ ÄÚµå ½ÇÇà
@include_once("$board_skin_path/download.skin.php");

// ÀÌ¹Ì ´Ù¿î·Îµå ¹ÞÀº ÆÄÀÏÀÎÁö¸¦ °Ë»çÇÑ ÈÄ °Ô½Ã¹°´ç ÇÑ¹ø¸¸ Æ÷ÀÎÆ®¸¦ Â÷°¨ÇÏµµ·Ï ¼öÁ¤
$ss_name = "ss_down_{$bo_table}_{$wr_id}";
if (!get_session($ss_name))
{
    // ÀÚ½ÅÀÇ ±ÛÀÌ¶ó¸é Åë°ú
    // °ü¸®ÀÚÀÎ °æ¿ì Åë°ú
    if (($write[mb_id] && $write[mb_id] == $member[mb_id]) || $is_admin)
        ;
    else if ($board[bo_download_level] > 1) // È¸¿øÀÌ»ó ´Ù¿î·Îµå°¡ °¡´ÉÇÏ´Ù¸é
    {
        // ´Ù¿î·Îµå Æ÷ÀÎÆ®°¡ À½¼öÀÌ°í È¸¿øÀÇ Æ÷ÀÎÆ®°¡ 0 ÀÌ°Å³ª ÀÛ´Ù¸é
        if ($member[mb_point] + $board[bo_download_point] < 0)
            alert("º¸À¯ÇÏ½Å Æ÷ÀÎÆ®(".number_format($member[mb_point]).")°¡ ¾ø°Å³ª ¸ðÀÚ¶ó¼­ ´Ù¿î·Îµå(".number_format($board[bo_download_point]).")°¡ ºÒ°¡ÇÕ´Ï´Ù.\\n\\nÆ÷ÀÎÆ®¸¦ Àû¸³ÇÏ½Å ÈÄ ´Ù½Ã ´Ù¿î·Îµå ÇØ ÁÖ½Ê½Ã¿À.");

        // °Ô½Ã¹°´ç ÇÑ¹ø¸¸ Â÷°¨ÇÏµµ·Ï ¼öÁ¤
        insert_point($member[mb_id], $board[bo_download_point], "$board[bo_subject] $wr_id ÆÄÀÏ ´Ù¿î·Îµå", $bo_table, $wr_id, "´Ù¿î·Îµå");
    }

    // ´Ù¿î·Îµå Ä«¿îÆ® Áõ°¡
    $sql = " update $g4[board_file_table] set bf_download = bf_download + 1 where bo_table = '$bo_table' and wr_id = '$wr_id' and bf_no = '$no' ";
    sql_query($sql);

    set_session($ss_name, TRUE);
}

$g4[title] = "$group[gr_subject] > $board[bo_subject] > " . conv_subject($write[wr_subject], 255) . " > ´Ù¿î·Îµå";

if (preg_match("/^utf/i", $g4[charset]))
    $original = urlencode($file[bf_source]);
else
    $original = $file[bf_source];

@include_once("$board_skin_path/download.tail.skin.php");
download_header($original, "", $filepath);
/*
if(preg_match("/msie/i", $_SERVER[HTTP_USER_AGENT]) && preg_match("/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();
*/
$fp = fopen("$filepath", "rb");

// 4.00 ´ëÃ¼
// ¼­¹öºÎÇÏ¸¦ ÁÙÀÌ·Á¸é print ³ª echo ¶Ç´Â while ¹®À» ÀÌ¿ëÇÑ ¹æ¹ýº¸´Ù´Â ÀÌ¹æ¹ýÀÌ...
//if (!fpassthru($fp)) {
//    fclose($fp);
//}

$download_rate = 10;

while(!feof($fp)) {
    //echo fread($fp, 100*1024);
    /*
    echo fread($fp, 100*1024);
    flush();
    */

    print fread($fp, round($download_rate * 1024));
    flush();
    usleep(1000);
}
fclose ($fp);
flush();
?>
