icstool/ics.php

181 lines
3.9 KiB
PHP
Raw Normal View History

2024-01-22 14:16:35 +01:00
<html>
2024-01-22 14:57:34 +01:00
<header>
<title>Evénements à venir</title>
</header>
2024-01-22 16:31:39 +01:00
<body style="font-family: helvetica; line-height: 24px; font-size: 16px;">
2024-01-28 18:29:40 +01:00
<?php
require 'settings.php';
if ($password && (!isset($_SERVER['PHP_AUTH_PW']) || $_SERVER['PHP_AUTH_PW'] != $password)) {
header('WWW-Authenticate: Basic realm="bbn"');
header('HTTP/1.0 401 Unauthorized');
die('<p>Access denied.</p></body></html>');
}
/*function ics2json($input)
2024-01-22 14:16:35 +01:00
{
2024-01-28 18:29:40 +01:00
$id = 0;
$root = array();
$curr = root;
input.split("\r\n").forEach(l =>
2024-01-22 14:16:35 +01:00
{
2024-01-28 18:29:40 +01:00
var key = l.split(":")[0].split(";")[0];
var val = l.split(":")[1];
if (key == "BEGIN")
2024-01-22 14:16:35 +01:00
{
2024-01-28 18:29:40 +01:00
if (curr[val])
{
val += "_" + (id++);
}
curr[val] = {
parent: curr
};
curr = curr[val];
2024-01-22 14:16:35 +01:00
}
2024-01-28 18:29:40 +01:00
else if (key == "END")
{
var parent = curr.parent;
delete curr.parent;
curr = parent;
}
else
{
curr[key] = key.startsWith("DT") ? dt(val) : val;
}
});
root.VCALENDAR.VEVENTS = [];
Object.keys(root.VCALENDAR)
.filter(k => (k == "VEVENT" || k.startsWith("VEVENT_")))
.forEach(k =>
2024-01-22 14:16:35 +01:00
{
2024-01-28 18:29:40 +01:00
root.VCALENDAR.VEVENTS.push(root.VCALENDAR[k]);
delete root.VCALENDAR[k];
});
return root.VCALENDAR;
}*/
2024-01-22 14:16:35 +01:00
2024-01-28 18:29:40 +01:00
// Get ics file
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, $user . ':' . $password);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
2024-01-22 14:16:35 +01:00
2024-01-28 18:29:40 +01:00
if (!$result)
2024-01-22 14:16:35 +01:00
{
2024-01-28 18:29:40 +01:00
die('<p>Error while getting ics file from caldav server.</p></body></html>');
}
curl_close($curl);
// port "showresult" js function:
$recent = 7;
if (isset($_GET['recent']))
{
$recent = $_GET['recent'];
}
echo '<h1>Evénements à venir</h1>';
?>
</body>
</html>
<!-- old -->
<div id="content">Récupération du calendrier...</div>
<script type="text/javascript">
2024-01-22 14:16:35 +01:00
function dt(s)
{
2024-01-22 14:47:40 +01:00
var formatted = s.substr(0,4) + "-" + s.substr(4,2) + "-" + s.substr(6,2);
if (s.length > 8)
{
formatted += "T" + s.substr(9,2) + ":" + s.substr(11,2) + ":" + s.substr(13,2);
}
return new Date(formatted);
2024-01-22 14:16:35 +01:00
}
function formatdate(d)
{
return d.toLocaleString('fr-FR', { timeZone: 'Europe/Paris', dateStyle: "full", timeStyle: "short" });
}
function showresult()
{
if (xhr.status == 200)
{
var params = new URLSearchParams(window.location.search);
var recent = parseInt(params.get("recent"));
if (isNaN(recent))
{
recent = 7;
}
2024-01-23 09:42:52 +01:00
2024-01-22 14:16:35 +01:00
var html = "<h1>Evénements à venir</h1>";
html += `<h3>${formatdate(new Date)}</h3>`;
html += `<div>En gras: modifié les ${recent} derniers jours</div>`;
var weblink = params.get("weblink");
if (weblink)
{
html += `<div><a target="_blank" href="${weblink}">Lien vers calendrier web</a></div>`;
}
2024-01-22 14:16:35 +01:00
var o = ics2json(xhr.responseText);
2024-01-23 09:42:52 +01:00
var lastmodified = new Date();
lastmodified.setDate(lastmodified.getDate() - recent);
2024-01-22 14:16:35 +01:00
var group = {};
o.VEVENTS
.filter(e => e.DTSTART >= (new Date))
.sort( (a,b) => a.DTSTART - b.DTSTART)
.forEach(e => {
var formatteddate = formatdate(e.DTSTART);
var splitdate = formatteddate.split(" ");
var year = splitdate[3];
var month = splitdate[2];
if (!group[year])
{
html += `<h2>${year}</h2>`;
group[year] = {};
}
if (!group[year][month])
{
html += `<h3>${month}</h3>`;
group[year][month] = true;
}
var line = `<li title="modifié le ${formatdate(e.DTSTAMP)}"> ${formatteddate}: ${e.SUMMARY}`;
2024-01-23 09:42:52 +01:00
if (e.DTSTAMP >= lastmodified)
2024-01-22 14:16:35 +01:00
{
line = `<b>${line}</b>`;
}
html += line;
});
2024-01-22 14:57:34 +01:00
content.innerHTML = html;
2024-01-22 14:16:35 +01:00
}
}
var password = localStorage.getItem("icspassword");
if (!password)
{
password = prompt("password:");
localStorage.setItem("icspassword", password);
}
var xhr = new XMLHttpRequest();
xhr.onload = showresult;
xhr.open("POST", "ics.php");
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send("password=" + password);
</script>