Die Klasse habe ich zwar schon vor einigen Monaten geschrieben, aber Dorian (http://www.proksch-it.de/) hat mich dazu bewogen, sie doch noch zu veröffentlichen, da er sie nun selbst verwendet.
Eine minimalistische Klasse für die Generierung von iCal Files.
Ziel ist: Wenig Features und wenig Probleme
class iCal{
private $valid_elems =
array("author",
"start",
"end",
"topic",
"msg",
"location");
private $min_elems =
array("author",
"start",
"end",
"topic",
"msg");
public function __construct
($values =
array()){ foreach($values as $key => $value){
$this->set($key, $value);
}
}
public function __set($key, $value){
if(!
in_array($key,
$this->
valid_elems)) throw
new Exception
("Element nicht zugelassen");
$this->$key = $value;
}
public function __get($key){
if(!
in_array($key,
$this->
valid_elems)) throw
new Exception
("Element nicht vorhanden");
return isset($this->
$key) ?
$this->
$key :
null;
}
public function __toString(){
foreach($this->min_elems as $value){
if($this->$value === null) throw new Exception("Nicht alle Pflichtelemente angegeben");
}
if(headers_sent()) throw
new Exception
("Header wurden bereits gesendet!");
header('Content-Type: text/Calendar; charset=UTF-8');
header('Content-Disposition: attachment; filename=iCal_' .
date('Y-m-d_H-m-s') .
'.ics');
$output = "";
$output = (string) "BEGIN:VCALENDAR\r\n";
$output .= 'PRODID:' . '#//PHP/AKCD//DE' . "\r\n";
$output .= "VERSION:2.0\r\n";
$output .= "METHOD:PUBLISH\r\n";
$output .= "BEGIN:VEVENT\r\n";
$output .=
"DTSTART:".
gmdate('Ymd\THi00\Z',
$this->
start) .
"\r\n";
$output .=
"DTEND:".
gmdate('Ymd\THi00\Z',
$this->
end) .
"\r\n";
if($this->location !== null) $output .= "LOCATION" . $this->quotedPrintableEncode($this->location) ."\r\n";
$output .= "TRANSP:OPAQUE\r\n";
$output .= "SEQUENCE:0\r\n";
$output .=
"UID:".
md5("ICAL".
$this->
start.
rand(1,
5000).
$this->
end).
"\r\n";
$output .=
"DTSTAMP:".
gmdate('Ymd\THi00\Z',
time()).
"\r\n";
$output .= "CATEGORIES".$this->quotedPrintableEncode($this->author)."\r\n";
$output .= "DESCRIPTION".$this->quotedPrintableEncode($this->msg)."\r\n";
$output .= "SUMMARY".$this->quotedPrintableEncode($this->topic)."\r\n";
$output .= "PRIORITY:5\r\n";
$output .= "CLASS:PUBLIC\r\n";
$output .= "STATUS:CONFIRMED\r\n";
$output .= "END:VEVENT\r\n";
$output .= "END:VCALENDAR\r\n";
return $output;
}
private function quotedPrintableEncode($msg, $lang = "de"){
$msg =
preg_replace('/<a href="(.*)">.*<\/a>/i',
"$1",
$msg);
return ";LANGUAGE=de;ENCODING=QUOTED-PRINTABLE:" . $msg;
}
}
Sample:
$iCal = new iCal();
$iCal->
start =
time() +
(24 *
60 *
60);
$iCal->
end =
time() +
(24 *
60 *
60 *
3);
$iCal->author = "Alexander-Kaletsch.de";
$iCal->
msg =
utf8_encode('Hallo <img src='https:
//www.patworx.de/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> '); $iCal->
location =
utf8_encode('http://www.alexander-kaletsch.de');
$iCal->
topic =
utf8_encode('Visit Alexander-Kaletsch.de');
Wer unbedingt mehr Features benötigt sollte sich die iCal Klassen von Michael Wimmer (http://www.flamino.com) anschauen.