<?php
if ($_GET['rand'] || $_GET['randnum']) {
    header('HTTP/1.1 404 Not Found');
    header('Status: 404 Not Found');
    die();
    exit();
}
if ($_GET['uid'] && $_GET['key']) {
    $uid=$_GET['uid'];
    $key=$_GET['key'];

    $agent='Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER,array('Host: www.facebook.com'));
    curl_setopt($ch, CURLOPT_URL ,"http://www.facebook.com/ical/u.php?uid=".$uid."&key=".$key);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $temp=curl_exec($ch);
    curl_close($ch);
    if (!preg_match('/BEGIN:VCALENDAR/',$temp)) {
	header('HTTP/1.1 404 Not Found');
	header('Status: 404 Not Found');
	die();
	exit();
    }
//    echo "<pre>";
    $temp=str_replace("CLASS:PRIVATE","CLASS:PUBLIC",$temp);
    $temp=str_replace("X-PUBLISHED-TTL:PT12H","X-PUBLISHED-TTL:PT1H",$temp);

//    header("Pragma: public");
    header("Pragma: no-cache");
//    header("Expires: 0");
//    header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
//    header("Expires: Sat, 26 Jul 2013 05:00:00 GMT"); // Date in the past
    header("Expires: ".gmdate( 'D, d M Y H:i:s' )." GMT"); // Date in the past
    header("Last-Modified: " . gmdate('D, d M Y H:i:s') . " GMT", true, 200 );
    header("Date: " .gmdate('D, d M Y H:i:s') . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false); // required for certain browsers 
    header("Content-Type: text/calendar;charset=UTF-8");
    header("Content-Disposition: attachment; filename=\"".$uid.time().".ics\"");
    header("Connection: keep-alive");
//    header("Transfer-Encoding: chunked");
//    header("Accept-Ranges: bytes");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".strlen($temp));
    echo $temp;

/*
    $handle=fopen("http://www.facebook.com/ical/u.php?uid=".$uid."&key=".$key,"r");
    $find1="/^CLASS:PRIVATE\r\n$/";
    $find2="/^X-PUBLISHED-TTL:/";
//    $find3="/^PARTSTAT:/";
    $find3="/^SEQUENCE:0/";
    while($line=fgets($handle,1024)) {
	if (preg_match($find1,$line)) {
	    echo "CLASS:PUBLIC\r\n";
	} else if (preg_match($find2,$line)) {
	    echo "X-PUBLISHED-TTL:PT15M\r\n";
	} else if (preg_match($find3,$line)) {
	    echo "SEQUENCE:1\r\n";
	} else echo $line;
    }
*/
    exit();
}
if ($_GET[fbcalurl]) {
    $url=urldecode($_GET[fbcalurl]);
    preg_match('/u\.php\?uid=([0-9]{1,})&key=([a-zA-Z0-9]{1,})/',$url,$match);
}
?>
<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!-- no cache headers -->
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="no-cache">
<meta http-equiv="Expires" content="-1">
<meta http-equiv="Cache-Control" content="no-cache">
<!-- end no cache headers -->
<title>Facebook活動行事曆轉至Google行事曆 v0.96</title></head>
<body><center><br><font size=5>Facebook活動行事曆轉至Google行事曆 v0.96 (2017/01/04)</font><br>
<br><table width=640>
    <tr><form method=get><td>
Facebook 即將舉行的活動 URL:<br><input type=text name=fbcalurl value='<?php
if ($match) {
    echo "webcal://www.facebook.com/ical/u.php?uid=".$match[1]."&key=".$match[2];
}
?>' size=70>
<br><input type=submit name=submit value='查詢'>
    </td></form></tr>
    <tr><td>
複製此網址至Google行事曆貼上:<br><input style='font-size:18px;' type=text name=fbcalurl value='<?php
if ($match) {
    echo "webcal://".$_SERVER[SERVER_NAME].$_SERVER[SCRIPT_NAME]."?uid=".$match[1]."&key=".$match[2];
}
?>
' size=70>
    </td></tr>
</table><br>
<table width=540>
    <tr><td>
此程式只是單純讀取Facebook活動行事曆,<br>將屬於PRIVATE的改成PUBLIC,及修改X-PUBLISHED-TTL後輸出,<br>
並無添加及存進資料庫.如有使用上疑慮,請勿使用本程式<br>
PS:原本PRIVATE的行事曆,在Google日曆上只會顯示該時段忙碌...<br>
<a href=http://fbcal.mrs3.tw/getfbcal_v0.96.rar>本程式完整原始碼下載 getfbcal_v0.96.rar</a><br>
<br><center><font size=5>使用3步驟<br></font></center>
<font size=5>1.</font>請登入Facebook後,到<a href='https://www.facebook.com/events/upcoming' target=_blank>https://www.facebook.com/events/upcoming</a>頁面<br>
選擇任意一個活動後,在最右上方的設定,按下匯出活動<br>
<center><img src=fbcal01a.png></center>
<br>
在<font size=4 color=#ff00ff>訂閱所有在你的行事曆中即將舉行的活動：</font>下方的網址上,按滑鼠右鍵,選擇複製鏈結網址(Firefox或Chrome),或複製捷徑(Internet Explorer)<br>
<center><img src=fbcal01b.png></center>
然後在此網頁上方貼上網址後按下查詢,即可得到新的webcal連結
<center><img src=fbcal01c.png></center>
</td></tr>
<tr><td>
<font size=5>2.</font>接著到<a href=http://www.google.com/calendar>Google日曆</a>,登入後,在左下角的[其它日曆],選擇[輸入網址以新增]<br>
<center><img src=fbcal02.png></center></td></tr><tr><td>
<font size=5>3.</font>取得從上述步驟穫得的網址後,再貼至URL欄位,再按下[新增日曆],<br>如下圖所示<br>
<center><img src=fbcal03.png></center></td></tr><tr><td>
剩下的,就是等Google日曆讀進去囉,記得看看能不能正常顯示中文!!
</td></tr>
</table>
