This commit is contained in:
quenousimporte 2025-02-04 15:06:52 +01:00
parent 6f37161f4c
commit b0561ab797
1 changed files with 42 additions and 20 deletions

62
ics.php
View File

@ -23,32 +23,42 @@ if ($password && (!isset($_SERVER['PHP_AUTH_PW']) || $_SERVER['PHP_AUTH_PW'] !=
//error_reporting(E_ALL); //error_reporting(E_ALL);
//ini_set('display_errors', 1); //ini_set('display_errors', 1);
function ics2json($input) { function ics2json($input)
{
$id = 0; $id = 0;
$root = []; $root = [];
$curr = &$root; $curr = &$root;
$lines = explode("\r\n", $input); $lines = explode("\r\n", $input);
foreach ($lines as $line) { foreach ($lines as $line)
{
$parts = explode(":", $line, 2); $parts = explode(":", $line, 2);
$key = explode(";", $parts[0])[0]; $key = explode(";", $parts[0])[0];
$val = isset($parts[1]) ? $parts[1] : ''; $val = isset($parts[1]) ? $parts[1] : '';
if ($key == "BEGIN") { if ($key == "BEGIN")
if (isset($curr[$val])) { {
if (isset($curr[$val]))
{
$val .= "_" . ($id++); $val .= "_" . ($id++);
} }
$curr[$val] = ['parent' => &$curr]; $curr[$val] = ['parent' => &$curr];
$curr = &$curr[$val]; $curr = &$curr[$val];
} elseif ($key == "END") { }
elseif ($key == "END")
{
$parent = &$curr['parent']; $parent = &$curr['parent'];
unset($curr['parent']); unset($curr['parent']);
$curr = &$parent; $curr = &$parent;
} else { }
else
{
$curr[$key] = (strpos($key, "DT") === 0) ? dt($val) : $val; $curr[$key] = (strpos($key, "DT") === 0) ? dt($val) : $val;
} }
} }
$root['VCALENDAR']['VEVENTS'] = []; $root['VCALENDAR']['VEVENTS'] = [];
foreach ($root['VCALENDAR'] as $k => $v) { foreach ($root['VCALENDAR'] as $k => $v)
if ($k == "VEVENT" || strpos($k, "VEVENT_") === 0) { {
if ($k == "VEVENT" || strpos($k, "VEVENT_") === 0)
{
$root['VCALENDAR']['VEVENTS'][] = $v; $root['VCALENDAR']['VEVENTS'][] = $v;
unset($root['VCALENDAR'][$k]); unset($root['VCALENDAR'][$k]);
} }
@ -56,19 +66,23 @@ function ics2json($input) {
return $root['VCALENDAR']; return $root['VCALENDAR'];
} }
function dt($s) { function dt($s)
{
$formatted = substr($s, 0, 4) . "-" . substr($s, 4, 2) . "-" . substr($s, 6, 2); $formatted = substr($s, 0, 4) . "-" . substr($s, 4, 2) . "-" . substr($s, 6, 2);
if (strlen($s) > 8) { if (strlen($s) > 8)
{
$formatted .= "T" . substr($s, 9, 2) . ":" . substr($s, 11, 2) . ":" . substr($s, 13, 2); $formatted .= "T" . substr($s, 9, 2) . ":" . substr($s, 11, 2) . ":" . substr($s, 13, 2);
} }
return new DateTime($formatted); return new DateTime($formatted);
} }
function formatdate($d) { function formatdate($d)
{
return strftime('%A, %e %B %Y %H:%M', $d->getTimestamp()); return strftime('%A, %e %B %Y %H:%M', $d->getTimestamp());
} }
function getWeek($date) { function getWeek($date)
{
$dto = new DateTime(); $dto = new DateTime();
$dto->setISODate($date->format('o'), $date->format('W')); $dto->setISODate($date->format('o'), $date->format('W'));
return $dto->format('W'); return $dto->format('W');
@ -82,7 +96,8 @@ curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, $davuser . ':' . $davpassword); curl_setopt($curl, CURLOPT_USERPWD, $davuser . ':' . $davpassword);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl); $result = curl_exec($curl);
if (!$result) { if (!$result)
{
die('error'); die('error');
} }
curl_close($curl); curl_close($curl);
@ -99,33 +114,40 @@ $o = ics2json($result);
$lastmodified = new DateTime(); $lastmodified = new DateTime();
$lastmodified->modify("-$recent days"); $lastmodified->modify("-$recent days");
$group = []; $group = [];
usort($o['VEVENTS'], function($a, $b) { usort($o['VEVENTS'], function($a, $b)
{
return $a['DTSTART'] <=> $b['DTSTART']; return $a['DTSTART'] <=> $b['DTSTART'];
}); });
foreach ($o['VEVENTS'] as $e) { foreach ($o['VEVENTS'] as $e)
if ($e['DTSTART'] >= new DateTime() && (!$recentonly || $e['DTSTAMP'] >= $lastmodified)) { {
if ($e['DTSTART'] >= new DateTime() && (!$recentonly || $e['DTSTAMP'] >= $lastmodified))
{
$formatteddate = strftime('%A %d %B %Y %H:%M', $e['DTSTART']->getTimestamp()); $formatteddate = strftime('%A %d %B %Y %H:%M', $e['DTSTART']->getTimestamp());
$splitdate = explode(' ', $formatteddate); $splitdate = explode(' ', $formatteddate);
$year = $splitdate[3]; $year = $splitdate[3];
$month = $splitdate[2]; $month = $splitdate[2];
$week = getWeek($e['DTSTART']); $week = getWeek($e['DTSTART']);
if (!isset($group[$year])) { if (!isset($group[$year]))
{
$html .= "<h2>$year</h2>"; $html .= "<h2>$year</h2>";
$group[$year] = []; $group[$year] = [];
} }
if (!isset($group[$year][$month])) { if (!isset($group[$year][$month]))
{
$html .= "<h3>". ucfirst($month) ."</h3>"; $html .= "<h3>". ucfirst($month) ."</h3>";
$group[$year][$month] = []; $group[$year][$month] = [];
} }
if (!isset($group[$year][$month][$week])) { if (!isset($group[$year][$month][$week]))
{
$html .= "<h4>Semaine $week</h4>"; $html .= "<h4>Semaine $week</h4>";
$group[$year][$month][$week] = true; $group[$year][$month][$week] = true;
} }
$line = "<li title=\"modifié le " . strftime('%A, %e %B %Y %H:%M', $e['DTSTAMP']->getTimestamp()) . "\">"; $line = "<li title=\"modifié le " . strftime('%A, %e %B %Y %H:%M', $e['DTSTAMP']->getTimestamp()) . "\">";
$line .= ucfirst($formatteddate) . ": " . $e['SUMMARY']; $line .= ucfirst($formatteddate) . ": " . $e['SUMMARY'];
if ($e['DTSTAMP'] >= $lastmodified) { if ($e['DTSTAMP'] >= $lastmodified)
{
$line .= " 🆕"; $line .= " 🆕";
} }
$line .= '</li>'; $line .= '</li>';