diff options
author | zeldakatze <coffee@zeldakatze.de> | 2025-08-17 23:21:48 +0200 |
---|---|---|
committer | zeldakatze <coffee@zeldakatze.de> | 2025-08-17 23:21:48 +0200 |
commit | 50a67a31aaa18ec96019379a489ec65d2d641162 (patch) | |
tree | be5b005a5343660a609d1693451cdc41ff650ade /icalExport.php | |
parent | e828b9c7dd11410d995aa88dcc2b22210f099cf4 (diff) | |
download | infoCalendar-50a67a31aaa18ec96019379a489ec65d2d641162.tar.gz infoCalendar-50a67a31aaa18ec96019379a489ec65d2d641162.zip |
add ical exports, fix event listing
Diffstat (limited to 'icalExport.php')
-rw-r--r-- | icalExport.php | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/icalExport.php b/icalExport.php new file mode 100644 index 0000000..1e90c19 --- /dev/null +++ b/icalExport.php @@ -0,0 +1,140 @@ +<?php + +define("ICAL_DATE_FORMAT", "Ymd\THis"); + +class infocalendar_CalendarView { + public $id; + public $name; + public $password; + + public function __construct($id, $name, $password) { + $this->id = $id; + $this->name = $name; + $this->password = $password; + } +} + +/** creates a calendarView object for the entry with $name. Returns + * null if not found */ +function infocalendar_getViewByName($name) { + global $wpdb; + $result = $wpdb->get_results( + $wpdb->prepare("SELECT * FROM {$wpdb->prefix}infocalendar_calendar + WHERE name = %s", $name), + 'ARRAY_A'); + + /* if nothing has been found, return */ + if(count($result) != 1) + return 0; + + return new infocalendar_CalendarView($result[0]['id'], $result[0]['name'], + $result[0]['password']); +} + +function infocalendar_sendAuthHeaderAndExit() { + header('WWW-Authenticate: Basic realm="My Realm"'); + header('HTTP/1.0 401 Unauthorized'); + echo 'Invalid credentials'; + exit; +} + +function infocalendar_beAuthorizedOrBust($view) { + if($view->password != null) { + if (!isset($_SERVER['PHP_AUTH_USER'])) { + infocalendar_sendAuthHeaderAndExit(); + } else if(password_verify($_SERVER['PHP_AUTH_PW'], $view->password)) { + return; + } else { + infocalendar_sendAuthHeaderAndExit(); + die("Should not be here!"); + } + } +} + +function infocalendar_printICalTextFromView($view) { + global $wpdb; + + /* print the calendar */ +?> +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//zeldakatze.de//InfoCalendar 1.1//DD +<?php + + /* print singular events */ + $result = $wpdb->get_results($wpdb->prepare("SELECT + event.id, title, location, startDate, endDate, creator, creationDate, + repetitionOption, repeatEndTime, + postID, uid, repeatingEvent, icalImported + FROM {$wpdb->prefix}infocalendar_event AS event + + LEFT JOIN {$wpdb->prefix}infocalendar_eventCalendarMap AS eventCalendarMap + ON event.id = eventCalendarMap.eventID + + LEFT JOIN {$wpdb->prefix}infocalendar_calendar AS calendar + ON calendar.id = eventCalendarMap.calendarID + + LEFT JOIN {$wpdb->prefix}infocalendar_repeatingEvent AS repeatingEvent + ON repeatingEvent.id = event.id + + WHERE + calendar.name = %s + ", + $view->name), 'ARRAY_A'); + foreach($result as $row) { + print("BEGIN:VEVENT\n"); + print("SUMMARY:".$row['title']."\n"); + print("UID:".sha1($row['id'].$row['title'].$row['startDate'])."\n"); + print("DTSTART:". + date_format(date_create($row['startDate']), ICAL_DATE_FORMAT)."\n"); + print("DTEND:". + date_format(date_create($row['endDate']), ICAL_DATE_FORMAT)."\n"); + print("DTSTAMP:". + date_format(date_create($row['creationDate']), ICAL_DATE_FORMAT) + ."\n"); + print("LOCATION:".$row['location']."\n"); + if($row['postID'] != 0) { + print("URL:".get_permalink($row['postID'])."\n"); + } + + /* print the repeat information */ + if($row['repeatingEvent']) { + $frequency = 'DAILY'; + print("RRULE:FREQ=".$frequency. + ";INTERVAL=".$row['repetitionOption']. + ";UNTIL=". + date_format(date_create($row['repeatEndTime']),ICAL_DATE_FORMAT). + "\n"); + } + + print("END:VEVENT\n"); + } + + /* close the calendar */ + ?> +END:VCALENDAR + <?php +} + +function doICalExport(WP_REST_Request $request) { + global $wpdb; + + /* get the view information */ + // TODO sanitization!? + $calendarView = infocalendar_getViewByName($request['slug']); + if($calendarView == null) + die("unknown view"); + + /* check authentication */ + infocalendar_beAuthorizedOrBust($calendarView); + + /* print the plaintext header */ + header("Content-type: text/plain"); + + infocalendar_printICalTextFromView($calendarView); + + /* */ + +} + +?> |