AEC (Ajax Event Calendar) ical export function

Put in functions.php:

// Ical Export for Event Calendar

function ical_feed(){
global $wpdb;
$blog_name = get_bloginfo('name');
$blog_url = get_bloginfo('home');

header('Content-type: text/calendar; charset=utf-8'); //set charset utf 8 since it is ümlaut-freindly
header('Content-Disposition: attachment; filename="event-calendar.ics"');

$timezone = get_option('timezone_string');
// dst = Daylight Saving Time
$is_dst = 0;
if ($timezone) {
$now = time();
$tz = new DateTimeZone($timezone);
$tr = $tz->getTransitions(); //debug: $tr = $tz->getTransitions($now, $now);
$is_dst = $tr[0]['isdst'];
}
$gmt_offset = get_option('gmt_offset')-$is_dst;
$d = preg_split('/\./',$gmt_offset);
$tzname = "GMT+".two_digits($d[0]).":".(($d[1])? two_digits($d[1]) : "00");
$tzoffsetto = "+".two_digits($d[0]).(($d[1])? two_digits($d[1]) : "00");
$tzoffsetfrom = "+".two_digits($d[0]+1).(($d[1])? two_digits($d[1]) : "00");
$daylight_tzname = "GMT+".two_digits($d[0]+1).":".(($d[1])? two_digits($d[1]) : "00");
$daylight_tzoffsetto = "+".two_digits($d[0]+1).(($d[1])? two_digits($d[1]) : "00");
$daylight_tzoffsetfrom = "+".two_digits($d[0]).(($d[1])? two_digits($d[1]) : "00");
$apostrophe = (isset($_GET['content']) ? "'" : '’');

$events = '';
$space = ' ';
$results = $wpdb->get_results('SELECT
title,
description,
IF (ALLDAY=1, CONCAT(";VALUE=DATE:",DATE_FORMAT(START,"%Y%m%d")), CONCAT(":",DATE_FORMAT(START,"%Y%m%d"),"T",TIME_FORMAT(START,"%H%i%s"))) AS start,
IF (ALLDAY=1, CONCAT(";VALUE=DATE:",DATE_FORMAT(END,"%Y%m%d")), CONCAT(":", DATE_FORMAT(END,"%Y%m%d"),"T",TIME_FORMAT(END,"%H%i%s"))) AS end,
link,
address,
contact,
contact_info,
repeat_freq,
repeat_int,
CONCAT(";UNTIL=", DATE_FORMAT(repeat_end,"%Y%m%d")) AS rend
FROM ' . $wpdb->prefix . AEC_EVENT_TABLE . ';');
if ($results === false){
$this->log($wpdb->print_error());
} else{
//print_r($results);
foreach ($results as $event) {
$start_time = ($timezone)? preg_replace('/(^:.+$)/',';TZID='.$timezone.'$1',$event->start) : $event->start;
$end_time = ($timezone)? preg_replace('/(^:.+$)/',';TZID='.$timezone.'$1',$event->end) : $event->end;
$summary = stripslashes($event->title);
$link = ($event->link)? 'URL:'.$event->link : '';
$location = $event->address;
$organizer = ($event->contact_info)? 'ORGANIZER:'.$event->contact_info : '';
$content = ($event->description)? str_replace(',', '\,', str_replace('\\', '\\\\', str_replace("\n", "\n" . $space, stripslashes(strip_tags($event->description))))) : '';
$freq = $event->repeat_int;
$interval = $event->repeat_freq;
$rend = ($event->rend)? $event->rend : ";";
$freq = ($freq == 0)? "DAILY" : (($freq == 1)? "WEEKLY" : (($freq == 2)? "MONTHLY" : (($freq == 3)? "YEARLY" : false)));
$rrule = ($interval > 0)? 'RRULE:FREQ='.$freq.';INTERVAL='.$interval.$rend : false;
$events .= <<" , "\r\n" , $content);

// strip empty lines when no location and url is set (max 3 empty lines)
$content = str_replace("\r\n\r\n" , "\r\n" , $content);
$content = str_replace("\r\n\r\n" , "\r\n" , $content);
$content = str_replace("\r\n\r\n" , "\r\n" , $content);

echo $content;

exit;
}
function two_digits($value){
return preg_replace('/(^\d{1}$)/','0$1',$value);
}

if (isset($_GET['ical']))
{
add_action('init', 'ical_feed');
}

Einen Menschen lieben, heißt einwilligen, mit ihm alt zu werden.