2021-03-25 09:48:24 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This software is governed by the CeCILL-B license. If a copy of this license
|
|
|
|
* is not distributed with this file, you can obtain one at
|
|
|
|
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
|
|
|
|
*
|
|
|
|
* Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ
|
|
|
|
* Authors of Framadate/OpenSondage: Framasoft (https://github.com/framasoft)
|
|
|
|
*
|
|
|
|
* =============================
|
|
|
|
*
|
|
|
|
* Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
|
|
|
|
* ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
|
|
|
|
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
|
|
|
|
*
|
|
|
|
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
|
|
|
|
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
|
|
|
|
*/
|
|
|
|
namespace Framadate\Services;
|
|
|
|
|
|
|
|
use DateTime;
|
2021-12-20 17:46:50 +01:00
|
|
|
use Framadate\Utils;
|
2021-03-25 09:48:24 +01:00
|
|
|
use Sabre\VObject;
|
|
|
|
|
|
|
|
class ICalService {
|
|
|
|
/**
|
|
|
|
* Creates an ical-File and initiates the download. If possible, the provided time is used, else an all day event is created.
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function getEvent($poll, string $start_day, string $start_time): void
|
|
|
|
{
|
2021-03-25 09:48:24 +01:00
|
|
|
if(!$this->dayIsReadable($start_day)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$ical_text = "";
|
2021-04-16 15:35:35 +02:00
|
|
|
$elements = explode("-", $start_time);
|
|
|
|
$end_time = null;
|
|
|
|
if(count($elements) === 2) {
|
|
|
|
$start_time = trim($elements[0]);
|
|
|
|
$end_time = trim($elements[1]);
|
|
|
|
}
|
2021-03-25 09:48:24 +01:00
|
|
|
$start_time = $this->reviseTimeString($start_time);
|
2021-04-16 15:35:35 +02:00
|
|
|
if($end_time !== null) {
|
|
|
|
$end_time = $this->reviseTimeString($end_time);
|
|
|
|
}
|
2021-03-25 09:48:24 +01:00
|
|
|
if($start_time !== null) {
|
2021-04-16 15:35:35 +02:00
|
|
|
if($end_time !== null) {
|
|
|
|
$ical_text = $this->getTimedEvent($poll, $start_day . " " . $start_time, $start_day . " " . $end_time);
|
|
|
|
} else {
|
|
|
|
$ical_text = $this->getTimedEvent1Hour($poll, $start_day . " " . $start_time);
|
|
|
|
}
|
2021-03-25 09:48:24 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$date = DateTime::createFromFormat('d-m-Y', $start_day);
|
|
|
|
$day = $date->format('Ymd');
|
|
|
|
$ical_text = $this->getAllDayEvent($poll, $day);
|
|
|
|
}
|
|
|
|
$this->provideFile($poll->title, $ical_text);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls getTimedEvent with one hour as a time slot, starting at $start_daytime
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function getTimedEvent1Hour($poll, string $start_daytime): string
|
|
|
|
{
|
2021-03-25 09:48:24 +01:00
|
|
|
$end_daytime = date(DATE_ATOM, strtotime('+1 hours', strtotime($start_daytime)));
|
|
|
|
return $this->getTimedEvent($poll, $start_daytime, $end_daytime);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the text for an ical event including the time
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function getTimedEvent($poll, string $start_daytime, string $end_daytime): string
|
|
|
|
{
|
2021-03-25 09:48:24 +01:00
|
|
|
$vcalendar = new VObject\Component\VCalendar([
|
|
|
|
'VEVENT' => [
|
|
|
|
'SUMMARY' => $poll->title,
|
|
|
|
'DESCRIPTION' => $this->stripMD($poll->description),
|
2021-12-20 17:46:50 +01:00
|
|
|
'DTSTART' => new DateTime($start_daytime),
|
|
|
|
'DTEND' => new DateTime($end_daytime)
|
2021-03-25 09:48:24 +01:00
|
|
|
],
|
|
|
|
'PRODID' => ICAL_PRODID
|
|
|
|
]);
|
|
|
|
return $vcalendar->serialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the text for an ical event if the time is not known
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function getAllDayEvent($poll, string $day): string
|
|
|
|
{
|
2021-03-25 09:48:24 +01:00
|
|
|
$vcalendar = new VObject\Component\VCalendar();
|
|
|
|
$vevent = $vcalendar->add('VEVENT');
|
|
|
|
$vevent->add('SUMMARY', $poll->title);
|
|
|
|
$vevent->add('DESCRIPTION', $this->stripMD($poll->description));
|
|
|
|
$dtstart = $vevent->add('DTSTART', $day);
|
|
|
|
$dtstart['VALUE'] = 'DATE';
|
|
|
|
unset($vcalendar->PRODID);
|
|
|
|
$vcalendar->add('PRODID', ICAL_PRODID);
|
|
|
|
return $vcalendar->serialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a file and initiates the download
|
2021-12-20 17:46:50 +01:00
|
|
|
* @param string $title
|
2021-03-25 09:48:24 +01:00
|
|
|
* @param string $ical_text
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function provideFile(string $title, string $ical_text): void
|
|
|
|
{
|
2021-03-25 09:48:24 +01:00
|
|
|
header('Content-Description: File Transfer');
|
|
|
|
header('Content-Disposition: attachment; filename=' . $this->stripTitle($title) . ICAL_ENDING);
|
|
|
|
header('Expires: 0');
|
|
|
|
header('Cache-Control: must-revalidate');
|
|
|
|
header('Pragma: public');
|
2021-10-18 16:50:34 +02:00
|
|
|
header("Content-Type: text/calendar");
|
2021-03-25 09:48:24 +01:00
|
|
|
echo $ical_text;
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reformats a string value into a time readable by DateTime
|
|
|
|
* @param string $time
|
|
|
|
* @return string the corrected value, null if the format is unknown
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function reviseTimeString(string $time): ?string
|
|
|
|
{
|
2021-03-25 09:48:24 +01:00
|
|
|
// 24-hour clock / international format
|
|
|
|
if (preg_match('/^\d\d(:)\d\d$/', $time)) {
|
|
|
|
return $time;
|
|
|
|
}
|
|
|
|
// 12-hour clock / using am and pm
|
2021-12-20 17:46:50 +01:00
|
|
|
|
|
|
|
if (preg_match('/^\d[0-2]?:?\d{0,2}\s?[aApP][mM]$/', $time)) {
|
2021-03-25 09:48:24 +01:00
|
|
|
return $this->formatTime($time);
|
|
|
|
}
|
|
|
|
// french format HHhMM or HHh
|
2021-12-20 17:46:50 +01:00
|
|
|
|
|
|
|
if (preg_match('/^\d\d?[hH]\d?\d?$/', $time)) {
|
2021-03-25 09:48:24 +01:00
|
|
|
return $this->formatTime(str_pad(str_ireplace("H", ":", $time), 5, "0"));
|
|
|
|
}
|
|
|
|
// Number only
|
2021-12-20 17:46:50 +01:00
|
|
|
|
|
|
|
if (preg_match('/^\d{1,4}$/', $time)) {
|
2021-03-25 09:48:24 +01:00
|
|
|
return $this->formatTime(str_pad(str_pad($time, 2, "0", STR_PAD_LEFT), 4, "0"));
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-20 17:46:50 +01:00
|
|
|
* @param string $day
|
|
|
|
* @return false|int 1 if the day string can be parsed, 0 if not and false if an error occured
|
2021-03-25 09:48:24 +01:00
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function dayIsReadable(string $day) {
|
2021-03-25 09:48:24 +01:00
|
|
|
return preg_match('/^\d{2}-\d{2}-\d{4}$/', $day);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $time
|
|
|
|
* @return string date string in format H:i (e.g. 19:00)
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function formatTime(string $time): string
|
|
|
|
{
|
2021-03-25 09:48:24 +01:00
|
|
|
return date("H:i", strtotime($time));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts MD Code to HTML, then strips HTML away
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function stripMD(string $string): string
|
|
|
|
{
|
|
|
|
return strip_tags(Utils::markdown($string));
|
2021-03-25 09:48:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Strips a string so it's usable as a file name (only digits, letters and underline allowed)
|
2021-12-20 17:46:50 +01:00
|
|
|
*
|
|
|
|
* @return null|string
|
2021-03-25 09:48:24 +01:00
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function stripTitle(string $string): ?string {
|
2021-03-25 09:48:24 +01:00
|
|
|
return preg_replace('/[^a-z0-9_]+/', '-', strtolower($string));
|
|
|
|
}
|
|
|
|
}
|