Im Format UTF-8 speichern! Ggf. im Browser - Darstellung Textcodierung auf Unicode (UTF-8) stellen
<?php
/*
* Event-Kalender - Events exportieren - kalender2srvics.php (utf-8)
* Copyright: - https://werner-zenk.de
* Modify: RaHa
* Kalender in Server Datei domain.tld/ics/ speichern
* mit Smartphone als Kalenderabo abonnieren: domain.tld/ics/myICS.ics
* Alarm: 1 Tag und 1 Stunde vor dem Event
* getestet mit iPhone, iPad, Mac OS X
* Aufruf unter Plesk - Geplante Aufgaben (stündlich oder anders): /var/www/vhosts/domain.tld/kalender/kalender2srvics.php
* oder per shell-Script: php /var/www/vhosts/domain.tld/kalender/kalender2srvics.php
*
* Im Feld Beschreibung steht in einer Zeile Ort: Ortsbezeichnung
*/
session_start();
/*
* Events als ICS exportieren
*/
$abfragename="";
if (isset($_REQUEST['pass'])) { $abfragename=$_REQUEST['pass']; }
if (isset($argv[1])) { $abfragename=$argv[1]; }
mb_internal_encoding("UTF-8");
// Verbindungsdaten zur Datenbank
require_once("dbinfo.inc.php");
// Verbindung zur Datenbank aufbauen
$db_link = mysqli_connect ($DB_HOST, $DB_BENUTZER, $DB_PASSWORT, $DB_NAME);
mysqli_query($db_link, "SET NAMES 'utf8'");
$sql = "SELECT * FROM ".$TABLE_PREFIX."_kalender ORDER BY gruppe";
$db_erg = mysqli_query( $db_link, $sql );
if ( ! $db_erg ) { die('Ungültige Abfrage: ' . mysqli_error()); }
$strs=chr(13).chr(10);
//$strc=chr(10);
$strc='\\n';
$eol = "\n";
$cal_timestamp = time();
$cal_ics_content = "BEGIN:VCALENDAR" . $eol . "METHOD:PUBLISH" . $eol . "VERSION:2.0" . $eol. "CALSCALE:GREGORIAN". $eol . "PROID:checkip.net" . $eol;
while ($event = mysqli_fetch_array($db_erg))
{
$prioritaet=intval($event["prioritaet"]);
if ($prioritaet < 19) {
sscanf($event["start"], "%4s-%2s-%2s %2s:%2s", $dbJahr, $dbMonat, $dbTag, $dbStunde, $dbMinute);
sscanf($event["ende"], "%4s-%2s-%2s %2s:%2s", $dbJahr2, $dbMonat2, $dbTag2, $dbStunde2, $dbMinute2);
$cal_start = $dbJahr . $dbMonat . $dbTag . 'T' . $dbStunde . $dbMinute . '00';
$cal_end = $dbJahr2 . $dbMonat2 . $dbTag2 . 'T' . $dbStunde2 . $dbMinute2 . '00';
$cal_current_time = date("Ymd") . 'T' . date("His");
$cal_title = $event["event"];
$cal_location = $event["adresse"];
$cal_description = strip_tags($event["beschreibung"]);
$cal_description = preg_replace('/\[.*?\](.*)\[\/.*?\]/isU', '$1', $cal_description);
$cal_description = str_replace($strs, $strc, $cal_description);
$cal_url = '';
$alarm = "ALARM";
$cal_alarm_datecreate = date_create($dbJahr.'-'.$dbMonat.'-'.$dbTag.' '.$dbStunde.':'.$dbMinute.':00');
$cal_alarm_timestamp = date_timestamp_get($cal_alarm_datecreate);
if ($cal_alarm_timestamp<$cal_timestamp){ $alarm = "NOALARM"; }
if (strpos($cal_description, "NOALARM")){ $alarm = "NOALARM"; }
if ($prioritaet == 3) {if ($event["name"] !== $abfragename ){$alarm = "NOALARM";} }
if ($prioritaet > 15) { $alarm = "NOALARM"; }
if (isset($team[$prioritaet]) AND $team[$prioritaet]!=""){
$cal_description .= $strc.'Prio/Team: ' .$team[$prioritaet];
}
$cal_ics_content .= 'BEGIN:VEVENT'.$eol.
'UID:' .$cal_start.$event["id"].$eol.
'DTSTART:'.$cal_start.$eol.
'DTEND:'.$cal_end.$eol.
'LOCATION:'.$cal_location.$eol.
'DTSTAMP:'.$cal_current_time.$eol.
'SUMMARY:'.$cal_title.$eol.
'URL;VALUE=URI:'.$cal_url.$eol.
'DESCRIPTION:'.$cal_description.$eol;
if ($alarm=="ALARM"){
$cal_ics_content .= 'BEGIN:VALARM'.$eol.
'TRIGGER:-PT1H'.$eol.
'ATTACH;VALUE=URI:Basso'.$eol.
'ACTION:AUDIO'.$eol.
'DESCRIPTION:'.$cal_title.$eol.
'END:VALARM'.$eol.
'BEGIN:VALARM'.$eol.
'TRIGGER:-P1D'.$eol.
'ATTACH;VALUE=URI:Basso'.$eol.
'ACTION:AUDIO'.$eol.
'DESCRIPTION:'.$cal_title.$eol.
'END:VALARM'.$eol;
}
$cal_ics_content .= 'END:VEVENT'.$eol;
}
}
$cal_ics_content .= 'END:VCALENDAR';
$strOutput=$cal_ics_content;
//$strOutput=mb_convert_encoding($cal_ics_content, 'UTF-8', 'UTF-8');
// Öffnen des ics-file
$filename = "Event-Kalender";
if ($abfragename!=="" ) { $filename = $abfragename; }
$StrFileName="/var/www/vhosts/checkip.net/event-kalender.checkip.net/ics/".$filename.".ics";
$IntFileOpenHandle = fopen($StrFileName, "wb");
// Schreiben in das ics-file
//fwrite($IntFileOpenHandle, pack("CCC",0xef,0xbb,0xbf));
fwrite($IntFileOpenHandle, $strOutput);
// Schließen des ics-file
fclose($IntFileOpenHandle);
// ics-file
?>