Skip to content

Instantly share code, notes, and snippets.

@doitian
Last active August 29, 2015 14:07
Show Gist options
  • Save doitian/effabaa71ed33162db60 to your computer and use it in GitHub Desktop.
Save doitian/effabaa71ed33162db60 to your computer and use it in GitHub Desktop.
Tower 日历加上时间信息
<?php
header('Content-Type: text/calendar; charset=utf-8');
$url = 'https://tower.im/calendars/ical/' . htmlspecialchars($_GET['id']) . '.ics';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$output = curl_exec($ch);
$lastkey = NULL;
$event = NULL;
foreach(preg_split('/((\r?\n)|(\r\n?))/u', $output) as $line) {
if ($line === 'BEGIN:VEVENT') {
$event = array ();
} elseif ($line === 'END:VEVENT') {
if (preg_match_all('/(\d?\d):(\d\d)(?: *)?([ap]m)?/u', mb_strtolower($event['DESCRIPTION']), $matches)) {
$start = (int) ($matches[1][0] . $matches[2][0]);
if ($matches[3][0] === 'pm' || (empty($matches[3][0]) && $matches[3][1] === 'pm')) {
$start += 1200;
}
if (empty($matches[0][1])) {
$end = $start + 100;
if ($end > 2359) {
$end = 2359;
}
} else {
$end = (int) ($matches[1][1] . $matches[2][1]);
if ($matches[3][1] === 'pm') {
$end += 1200;
}
}
$dtstart = $event['DTSTART'];
$event['DTSTART'] = $dtstart . 'T' . sprintf('%04d', $start) . '00';
$event['DTEND'] = $dtstart . 'T' . sprintf('%04d', $end) . '00';
$event['DTSTART;TZID=Asia/Shanghai'] = $event['DTSTART'];
$event['DTEND;TZID=Asia/Shanghai'] = $event['DTEND'];
} else {
$event['DTSTART;VALUE=DATE'] = $event['DTSTART'];
$event['DTEND;VALUE=DATE'] = $event['DTEND'];
}
unset($event['DTSTART']);
unset($event['DTEND']);
echo 'BEGIN:VEVENT' . "\n";
foreach($event as $key => $value) {
echo $key . ':' . $value . "\n";
}
echo 'END:VEVENT' . "\n";
$event = NULL;
} elseif (!is_null($event)) {
if ($line[0] === ' ') {
$event[$lastkey] .= substr($line, 1);
} else {
list($key, $value) = explode(':', $line, 2);
$event[$key] = $value;
$lastkey = $key;
}
} elseif ($line === 'CALSCALE:GREGORIAN') {
echo $line . "\n";
echo "X-WR-TIMEZONE:Asia/Shanghai\n";
echo "BEGIN:VTIMEZONE\n";
echo "TZID:Asia/Shanghai\n";
echo "X-LIC-LOCATION:Asia/Shanghai\n";
echo "BEGIN:STANDARD\n";
echo "TZOFFSETFROM:+0800\n";
echo "TZOFFSETTO:+0800\n";
echo "TZNAME:CST\n";
echo "DTSTART:19700101T000000\n";
echo "END:STANDARD\n";
echo "END:VTIMEZONE\n";
} else {
echo $line . "\n";
}
}
curl_close($ch);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment