<?php
// setup
$private_key 'f7bd44be0519c899814196d0db677a29';  // md5() of 'iCal'
$calendar_name 'Calendar';


// check key:
$key = isset($_GET['key']) ? $_GET['key'] : '';
if ( 
$key != $private_key ) {
    
header("HTTP/1.0 404 Not Found");
    die;
}

// get the feed urls:
$feeds = array(
    
'http://hhband.com/calendar.ics',
    
'http://www.google.com/calendar/ical/usa%40holiday.calendar.google.com/public/basic.ics',
);

// retrieve and parse icals
$events = array();
$items = array();
foreach ( 
$feeds as $feed ) {
    
$curl curl_init();
    
curl_setopt($curlCURLOPT_URL$feed);
    
curl_setopt($curlCURLOPT_CONNECTTIMEOUT2);
    
curl_setopt($curlCURLOPT_RETURNTRANSFER1);
    
$items[] = curl_exec($curl);
    
curl_close($curl);
}
foreach ( 
$items as $item ) {
    
$matches = array();
    
$count preg_match_all('/(begin\:vevent)(.*?)(end\:vevent)/is'$item$matchesPREG_SET_ORDER);
    foreach ( 
$matches as $m ) {
        
$events[] = $m[0];
    }
}

// combine into new file:
$header '
BEGIN:VCALENDAR
PRODID:-//Matthew Vince//iCal Combiner 1.00//EN
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-CALNAME:' 
$calendar_name '
X-WR-TIMEZONE:America/Chicago
BEGIN:VTIMEZONE
TZID:America/Chicago
X-LIC-LOCATION:America/Chicago
BEGIN:DAYLIGHT
TZOFFSETFROM:-0600
TZOFFSETTO:-0500
TZNAME:CDT
DTSTART:19700308T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:-0500
TZOFFSETTO:-0600
TZNAME:CST
DTSTART:19701101T020000
RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU
END:STANDARD
END:VTIMEZONE
'
;
$file trim($header) . "\r\n" implode("\r\n"$events);
$file str_replace(array("\r\n""\n"), array("\n""\r\n"), $file);

// send to client
header('Content-type: text/calendar');
header('Content-Disposition: attachment; filename="cal.ics"');
echo 
$file;
?>