This repository has been archived by the owner on Mar 2, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
105 lines (86 loc) · 3.86 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
// Text header
header("Content-type: text/plain");
// Check url
if (!isset($_GET['webcal']) || !filter_var($_GET['webcal'], FILTER_VALIDATE_URL)) {
header("HTTP/1.1 422 Unprocessable Entity");
die("Malformed or no 'webcal' url query parameter given. Ensure it is a valid url.");
}
try {
// Lib
require_once("icalendar-master/zapcallib.php");
// Util functions
function startsWith($haystack, $needle)
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
function endsWith($haystack, $needle)
{
$length = strlen($needle);
return $length === 0 ||
(substr($haystack, -$length) === $needle);
}
// Get the webcal
try {
$webcal = $_GET['webcal'];
// Ensure http
if (startsWith($webcal, "webcal://")) {
$webcal = "http://" . explode("webcal://", $webcal, 2)[1];
}
if (startsWith($webcal, "https://")) {
$webcal = "http://" . explode("https://", $webcal, 2)[1];
}
// Get content
$webcal_content = @file_get_contents($webcal);
} catch (Exception $e) {
header("HTTP/1.1 500 Internal Server Error");
die("An error occurred whilst fetching the webcal from the given url.");
}
if (!$webcal_content) {
header("HTTP/1.1 500 Internal Server Error");
die("An invalid response was received when fetching the webcal from the given url.");
}
// Make it an ical object
try {
$icalobj = new ZCiCal($webcal_content);
} catch (Exception $e) {
header("HTTP/1.1 500 Internal Server Error");
die("An error occurred whilst parsing the webcal to a valid ical object.");
}
// Iterate over every event
foreach ($icalobj->tree->child as $node) {
if ($node->getName() == "VEVENT") {
// Get the start and ends
$start = $node->data["DTSTART"]->value[0];
$end = $node->data["DTEND"]->value[0];
// Check if "fake" all day
if (endsWith($start, "T000000")) {
if (endsWith($end, "T235959")) {
// Update to all day event
$start_date = explode("T", $start, 2)[0];
$end_date = explode("T", $end, 2)[0];
$node->data["DTSTART"]->parameter = ["value" => "DATE"];
$node->data["DTEND"]->parameter = ["value" => "DATE"];
$node->data["DTSTART"]->value = [$start_date];
$node->data["DTEND"]->value = [$end_date];
}
}
}
}
// Add custom event (#ad)
$root = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . '/';
$eventobj = new ZCiCalNode("VEVENT", $icalobj->curnode);
$eventobj->addNode(new ZCiCalDataNode("SUMMARY:Webcal Fixer"));
$eventobj->addNode(new ZCiCalDataNode("DESCRIPTION:This calendar is originally from (" . $_GET['webcal'] . ") and has been converted by Webcal Fixer (" . $root . ").\n\nWebcal Fixer is a tool designed to fix bad webcal or icals. It converts any 'fake' all day events to proper all day events, clearing up valuable space in your calendar view. The project converts any event going from 12am to 11:59pm across one or multiple days to an all day event across the day or days.\n\nThis project is created by Matt Cowley (https://mattcowley.co.uk/). (#ad)"));
$eventobj->addNode(new ZCiCalDataNode("DTSTART;VALUE=DATE:" . date("Ymd")));
$eventobj->addNode(new ZCiCalDataNode("DTEND;VALUE=DATE:" . date("Ymd")));
$eventobj->addNode(new ZCiCalDataNode("URL:webcal://" . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"]));
// Export new
echo $icalobj->export();
die();
} catch (Exception $e) {
header("HTTP/1.1 500 Internal Server Error");
die("An error occurred whilst processing the webcal.");
}
?>