<?php
header("Content-Type: text/calendar; charset=UTF-8");
mb_internal_encoding("UTF-8");
require_once('XML/RSS.php');
$user = strip_tags($_GET['user']);
$lang = strip_tags($_GET['lang']);
function MyTAB2iCal($user,$lang)
{
$uri = "http://www.tokyoartbeat.com/list/user/".$user.".".$lang.".rdf";
$rss =& new XML_RSS("$uri");
$rss->parse();
echo "BEGIN:VCALENDAR\n";
echo "PRODID:-//MyTAB2iCal 1.0//EN\n";
echo "VERSION:2.0\n";
echo "CALSCALE:GREGORIAN\n";
echo "METHOD:PUBLISH\n";
echo "X-WR-CALNAME:".$user."'s MyTAB\n";
echo "X-WR-TIMEZONE:Asia/Tokyo\n";
echo "BEGIN:VTIMEZONE\n";
echo "TZID:Asia/Tokyo\n";
echo "X-LIC-LOCATION:Asia/Tokyo\n";
echo "BEGIN:STANDARD\n";
echo "TZOFFSETFROM:+0900\n";
echo "TZOFFSETTO:+0900\n";
echo "TZNAME:JST\n";
echo "DTSTART:19700101T000000\n";
echo "END:STANDARD\n";
echo "END:VTIMEZONE\n";
foreach ($rss->getitems() as $item ) {
preg_match('/<a.*tokyoartbeat.com\/venue.*>(.*)<\/a>/', $item['description'], $matches);
$location = mb_convert_encoding($matches[1], mb_internal_encoding(), 'auto');
$summary = mb_convert_encoding($item['title'], mb_internal_encoding(), 'auto');
$description = strip_tags(mb_convert_encoding($item['description'], mb_internal_encoding(), 'auto'));
$description = preg_replace('/\s\s+/', ' ', trim($description));
$description = preg_replace('/\n/', "\\n", $description);
if ($lang == 'ja') {
$link = $item['link'];
preg_match_all('/(\d{2,4})年(\d{1,2})月(\d{1,2})日/', $description, $matches);
}else {
$link = $item['link'].'.en';
preg_match_all('/(\d{2,4})-(\d{1,2})-(\d{1,2})/', $description, $matches);
}
$dtstart = $matches[1][0].$matches[2][0].$matches[3][0];
$dtend = $matches[1][1].$matches[2][1].$matches[3][1];
$tomorrow = mktime(0, 0, 0, date("m",strtotime($dtstart)) , date("d",strtotime($dtstart))+1, date("Y",strtotime($dtstart)));
$tomorrow = date("Ymd", $tomorrow);
$dtstamp = gmdate("Ymd", time()+9*3600)."T".gmdate("His", time()+9*3600);
echo "BEGIN:VEVENT\n";
if (isset($matches[1][1])) {
echo "DTSTART;VALUE=DATE:".$dtstart."\n";
echo "DTEND;VALUE=DATE:".$dtend."\n";
} else {
echo "DTSTART;VALUE=DATE:".$dtstart."\n";
echo "DTEND;VALUE=DATE:".$tomorrow."\n";
}
echo "DTSTAMP:".$dtstamp."\n";
echo "LOCATION:".$location."\n";
echo "SUMMARY:".$summary."\n";
echo "DESCRIPTION:".$description."\n";
echo "URL;VALUE=URI:".$link."\n";
echo "END:VEVENT\n";
}
echo "END:VCALENDAR\n";
}
MyTAB2iCal($user,$lang);
?>