-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
197 lines (167 loc) · 7.02 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* Stand alone app based on SlimPHP getting calendar from Viggo.
*/
require 'vendor/autoload.php';
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use GuzzleHttp\Client;
use Kalendersiden\ViggoAdapter;
use Kigkonsult\Icalcreator\Vcalendar;
date_default_timezone_set('Europe/Copenhagen');
$configuration = [
'settings' => [
'displayErrorDetails' => true,
],
];
// Prepare the Pimple dependency injection container.
$container = new \Slim\Container($configuration);
$container['config'] = [
'organization' => 'Vejle Idrætshøjskole',
'calendar' => 'https://vejle.viggo.dk/ExportCalendar/?ViggoId=87&UserId=298&code=17bca452d0b19b39a49d3ffdc1a77faabe5ae617',
#'host' => 'http://localhost:8080'
'host' => 'https://vih-kalendersiden.fly.dev'
];
// Add a Twig service to the container.
$container['twig'] = function($container) {
$loader = new Twig_Loader_Filesystem('templates');
return new Twig_Environment($loader, array('cache'));
};
// Create the Slim application using our container.
$app = new \Slim\App($container);
$app->get('/', function(Slim\Http\Request $request, Slim\Http\Response $response) {
// Load the template through the Twig service in the DIC.
$template = $this->get('twig')->loadTemplate('index.html');
// Render the template using a simple content variable.
return $response->write($template->render(['organization' => $this->get('config')['organization']]));
}) ->setName('frontpage');
$app->get('/calendar/{name}', function (Slim\Http\Request $request, Slim\Http\Response $response) {
$name = $request->getAttribute('name');
if ($name === 'vies') {
$config = [
Vcalendar::UNIQUE_ID => "vies.dk",
];
$url = $this->get('config')['host'] . "/calendar/proxy/vies";
$headline = 'Vejle Idrætsefterskole';
} else if ($name === 'vih') {
$config = [
Vcalendar::UNIQUE_ID => "vih.dk",
];
$url = "https://vejle.viggo.dk/ExportCalendar/?ViggoId=87&UserId=298&code=17bca452d0b19b39a49d3ffdc1a77faabe5ae617";
$headline = 'Vejle Idrætshøjskole';
} else {
return $response->withStatus(404);
}
$content = file_get_contents($url);
$vcalendar = new Vcalendar($config);
$vcalendar->parse($content);
$adapter = new ViggoAdapter($vcalendar);
$event_data = $adapter->parse();
$start_month = (int)$request->getQueryParam('start_month');
$year = (int)$request->getQueryParam('year');
$months= (int)$request->getQueryParam('months');
$pages = (int)$request->getQueryParam('pages');
$format = 'landscape';
$url = 'https://kalendersiden.dk/generate.php';
$client = new Client([
// You can set any number of default request options.
'timeout' => 2.0,
]);
$guzzle_response = $client->post($url, [
'form_params' => [
'month' => $start_month,
'year' => $year,
'months' => $months,
'pages' => $pages,
'format' => $format,
'head' => $headline,
'info' => ['showyear', 'holidays', 'weeks'],
'userdata' => $event_data
]
]);
try {
$response = $response->withHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
$response = $response->withHeader('Content-Type', 'application/pdf');
$response = $response->withHeader('Content-Disposition', 'attachment; filename="kalender.pdf"');
$response = $response->write($guzzle_response->getBody());
return $response;
} catch (HttpException $ex) {
echo $ex;
}
});
$app->get('/calendar/csv/{name}', function (Request $request, Response $response) {
$name = $request->getAttribute('name');
if ($name === 'vies') {
$config = [
Vcalendar::UNIQUE_ID => "vies.dk",
];
$url = $this->get('config')['host'] . "/calendar/proxy/vies";
$headline = 'Vejle Idrætsefterskole';
} else if ($name === 'vih') {
$config = [
Vcalendar::UNIQUE_ID => "vih.dk",
];
$url = "https://vejle.viggo.dk/ExportCalendar/?ViggoId=87&UserId=298&code=17bca452d0b19b39a49d3ffdc1a77faabe5ae617";
$headline = 'Vejle Idrætshøjskole';
} else {
return $response->withStatus(404);
}
$content = file_get_contents($url);
// $client = new GuzzleHttp\Client();
// $response = $client->request('GET', $url);
// $body = $response->getBody();
// $content = $body->getContents();
$vcalendar = new Vcalendar($config);
$vcalendar->parse($content);
$events = $vcalendar->selectComponents(date('Y'), date('m'), date('d'), date('Y') + 1, date('m'), date('d'));
foreach ($events as $year => $year_arr) {
foreach ($year_arr as $month => $month_arr) {
foreach ($month_arr as $day => $day_arr) {
foreach ($day_arr as $event) {
$begin = $event->getDtstart();
$end = $event->getDtend();
$summary = $event->getSummary();
for ($i = $begin; $begin <= $end; $i->modify('+1 day')) {
$event_text = $i->format("Y/m/d") . ', ' . $summary;
if (isset($vevents[$i->format("Ymd")])) {
if (!str_contains($vevents[$i->format("Ymd")], $summary)) {
$vevents[$i->format("Ymd")] = $vevents[$i->format("Ymd")] . ' - ' . $summary;
}
} else {
$vevents[$i->format("Ymd")] = $event_text;
}
}
$last_date = $i;
}
}
}
}
$output = implode("\n", $vevents);
try {
$response = $response->withHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
$response = $response->withHeader('Content-Type', 'application/excel');
$response = $response->withHeader('Content-Disposition', 'attachment; filename="kalender.csv"');
$response->getBody()->write($output);
return $response;
} catch (HttpException $ex) {
echo $ex;
}
});
/**
* Has become obsolete
* Check to use the direct links instead
*/
$app->get('/calendar/proxy/{name}', function (Request $request, Response $response) {
$name = $request->getAttribute('name');
$response->withHeader('Content-Type', 'text/calendar');
$response->withHeader('Content-Disposition', 'attachment');
if ($name === 'vies') {
$response->getBody()->write(file_get_contents('https://vejle.viggo.dk/ExportCalendar/?ViggoId=87&UserId=1528&code=4d4e5cc9cc0a6e52360344f0508a22de8f420194'));
} else if ($name === 'vih') {
$response->getBody()->write(file_get_contents('https://vejle.viggo.dk/ExportCalendar/?ViggoId=87&UserId=298&code=17bca452d0b19b39a49d3ffdc1a77faabe5ae617'));
} else {
return $response->withStatus(404);
}
return $response;
});
$app->run();