summaryrefslogtreecommitdiffstats
path: root/modules-available/locationinfo/page.inc.php
blob: 1a58131ffbce9a38246b8c22036476215373bcc1 (plain) (blame)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
<?php

class Page_LocationInfo extends Page
{
	private $show;

	/**
	 * Called before any page rendering happens - early hook to check parameters etc.
	 */
	protected function doPreprocess()
	{
		$this->show = Request::any('show', false, 'string');
		if ($this->show === 'panel') {
			$this->showPanel();
			exit(0);
		}
		User::load();
		if (!User::isLoggedIn()) {
			Message::addError('main.no-permission');
			Util::redirect('?do=Main'); // does not return
		}
		$action = Request::post('action');
		if ($action === 'writePanelConfig') {
			$this->writePanelConfig();
			$show = 'panels';
		} elseif ($action === 'writeLocationConfig') {
			$this->writeLocationConfig();
			$show = 'locations';
		} elseif ($action === 'deletePanel') {
			$this->deletePanel();
			$show = 'panels';
		} elseif ($action === 'updateServerSettings') {
			$this->updateServerSettings();
			$show = 'backends';
		} else {
			if (($id = Request::post('del-serverid', false, 'int')) !== false) {
				$this->deleteServer($id);
				$show = 'backends';
			} elseif (($id = Request::post('chk-serverid', false, 'int')) !== false) {
				$this->checkConnection($id);
				$show = 'backends';
			} elseif (Request::isPost()) {
				Message::addWarning('main.invalid-action', $action);
			}
		}
		if (Request::isPost() || $this->show === false) {
			if (!empty($show)) {
				//
			} elseif (User::hasPermission('panel.list')) {
				$show = 'panels';
			} elseif (User::hasPermission('location.*')) {
				$show = 'locations';
			} elseif (User::hasPermission('backend.*')) {
				$show = 'backends';
			} else {
				User::assertPermission('panel.list');
			}
			Util::redirect('?do=locationinfo&show=' . $show);
		}
	}

	/**
	 * Menu etc. has already been generated, now it's time to generate page content.
	 */
	protected function doRender()
	{
		$data = array('class-' . $this->show => 'active', 'errors' => []);
		// Do this here so we always see backend errors
		if (User::hasPermission('backend.*')) {
			$backends = $this->loadBackends();
			foreach ($backends as $backend) {
				if (!empty($backend['error'])) {
					$data['errors'][] = $backend;
				}
			}
		}
		Permission::addGlobalTags($data['perms'], null, ['backend.*', 'location.*', 'panel.list']);
		Render::addTemplate('page-tabs', $data);
		switch ($this->show) {
		case 'locations':
			$this->showLocationsTable();
			break;
		case 'backends':
			$this->showBackendsTable($backends ?? []);
			break;
		case 'edit-panel':
			$this->showPanelConfig();
			break;
		case 'panels':
			$this->showPanelsTable();
			break;
		case 'backendlog':
			$this->showBackendLog();
			break;
		default:
			Util::redirect('?do=locationinfo');
		}
	}

	/**
	 * Deletes the server from the db.
	 */
	private function deleteServer($id): void
	{
		User::assertPermission('backend.edit');
		if ($id === 0) {
			Message::addError('server-id-missing');
			return;
		}
		$res = Database::exec("DELETE FROM `locationinfo_coursebackend` WHERE serverid=:id", array('id' => $id));
		if ($res !== 1) {
			Message::addWarning('invalid-server-id', $id);
		}
	}

	private function deletePanel(): void
	{
		$id = Request::post('uuid', false, 'string');
		if ($id === false) {
			Message::addError('main.parameter-missing', 'uuid');
			return;
		}
		$this->assertPanelPermission($id, 'panel.edit');
		$res = Database::exec("DELETE FROM `locationinfo_panel` WHERE paneluuid = :id", array('id' => $id));
		if ($res !== 1) {
			Message::addWarning('invalid-panel-id', $id);
		}
		if (Module::isAvailable('runmode')) {
			RunMode::deleteMode(Page::getModule(), $id);
		}
	}

	private static function getTime(string $str): ?int
	{
		$str = explode(':', $str);
		if (count($str) !== 2)
			return null;
		if ($str[0] < 0 || $str[0] > 23 || $str[1] < 0 || $str[1] > 59)
			return null;
		return $str[0] * 60 + $str[1];
	}

	private function writeLocationConfig(): void
	{
		// Check locations
		$locationid = Request::post('locationid', false, 'int');
		if ($locationid === false) {
			Message::addError('main.parameter-missing', 'locationid');
			return;
		}
		if (Location::get($locationid) === false) {
			Message::addError('location.invalid-location-id', $locationid);
			return;
		}
		User::assertPermission('location.edit', $locationid);

		$serverid = Request::post('serverid', 0, 'int');
		if ($serverid === 0) {
			$serverid = null;
		}
		$serverlocationid = Request::post('serverlocationid', '', 'string');

		$changeServerRecursive = (Request::post('recursive', '', 'string') !== '');
		if (empty($serverlocationid) && !$changeServerRecursive) {
			$insertServerId = null;
			$ignoreServer = 1;
		} else {
			$insertServerId = $serverid;
			$ignoreServer = 0;
		}

		$NOW = time();

		Database::exec("INSERT INTO `locationinfo_locationconfig` (locationid, serverid, serverlocationid, lastcalendarupdate, lastchange)
				VALUES (:id, :insertserverid, :serverlocationid, 0, :now)
				ON DUPLICATE KEY UPDATE serverid = IF(:ignore_server AND serverid IS NULL, NULL, :serverid), serverlocationid = VALUES(serverlocationid),
					lastcalendarupdate = 0, lastchange = VALUES(lastchange)", array(
			'id' => $locationid,
			'insertserverid' => $insertServerId,
			'serverid' => $serverid,
			'serverlocationid' => $serverlocationid,
			'ignore_server' => $ignoreServer,
			'now' => $NOW,
		));

		if ($changeServerRecursive) {
			// Recursive overwriting of serverid
			$children = Location::getRecursiveFlat($locationid);
			$array = array();
			foreach ($children as $loc) {
				$array[] = $loc['locationid'];
			}
			if (!empty($array)) {
				Database::exec("UPDATE locationinfo_locationconfig
				SET serverid = :serverid, lastcalendarupdate = IF(serverid <> :serverid, 0, lastcalendarupdate), lastchange = :now
				WHERE locationid IN (:locations)", array(
					'serverid' => $serverid,
					'locations' => $array,
					'now' => $NOW,
				));
			}
		}
	}

	/**
	 * Get all location ids from the locationids parameter, which is comma separated, then split
	 * and remove any ids that don't exist. The cleaned list will be returned.
	 * Will show error and redirect to main page if parameter is missing
	 *
	 * @param bool $failIfEmpty Show error and redirect to main page if parameter is missing or list is empty
	 * @return array list of locations from parameter
	 */
	private function getLocationIdsFromRequest(): array
	{
		$locationids = Request::post('locationids', Request::REQUIRED_EMPTY, 'string');
		$locationids = explode(',', $locationids);
		$all = array_map(function ($item) { return $item['locationid']; }, Location::queryLocations());
		$locationids = array_filter($locationids, function ($item) use ($all) { return in_array($item, $all); });
		if (empty($locationids)) {
			Message::addError('main.parameter-empty', 'locationids');
			Util::redirect('?do=locationinfo');
		}
		return $locationids;
	}

	/**
	 * Updated the config in the db.
	 */
	private function writePanelConfig(): void
	{
		// UUID - existing or new
		$paneluuid = Request::post('uuid', false, 'string');
		if (($paneluuid === false || strlen($paneluuid) !== 36) && $paneluuid !== 'new') {
			Message::addError('invalid-panel-id', $paneluuid);
			Util::redirect('?do=locationinfo');
		}
		// Check panel type
		$paneltype = Request::post('ptype', false, 'string');

		if ($paneltype === 'DEFAULT') {
			$params = $this->preparePanelConfigDefault();
		} elseif ($paneltype === 'URL') {
			$params = $this->preparePanelConfigUrl();
		}  elseif ($paneltype === 'SUMMARY') {
			$params = $this->preparePanelConfigSummary();
		} else {
			Message::addError('invalid-panel-type', $paneltype);
			Util::redirect('?do=locationinfo');
		}

		// Permission
		$this->assertPanelPermission($paneluuid, 'panel.edit', $params['locationids'] ?? []);

		if ($paneluuid === 'new') {
			$paneluuid = Util::randomUuid();
			$query = "INSERT INTO `locationinfo_panel` (paneluuid, panelname, locationids, paneltype, panelconfig, lastchange)
				VALUES (:id, :name, :locationids, :type, :config, :now)";
		} else {
			$query = "UPDATE `locationinfo_panel`
				SET panelname = :name, locationids = :locationids, paneltype = :type, panelconfig = :config, lastchange = :now
				WHERE paneluuid = :id";
		}
		$params['id'] = $paneluuid;
		$params['name'] = Request::post('name', '-', 'string');
		$params['type'] = $paneltype;
		$params['now'] = time();
		$params['config'] = json_encode($params['config']);
		$params['locationids'] = implode(',', $params['locationids']);
		Database::exec($query, $params);

		Message::addSuccess('config-saved');
		Util::redirect('?do=locationinfo');
	}

	/**
	 * @return array{config: array, locationids: array}
	 */
	private function preparePanelConfigDefault(): array
	{
		// Check locations
		$locationids = self::getLocationIdsFromRequest();
		if (count($locationids) > 4) {
			$locationids = array_slice($locationids, 0, 4);
		}
		// Build struct from POST
		$conf = array(
			'language' => Request::post('language', 'en', 'string'),
			'mode' => Request::post('mode', 1, 'int'),
			'vertical' => Request::post('vertical', false, 'bool'),
			'eco' => Request::post('eco', false, 'bool'),
			'prettytime' => Request::post('prettytime', false, 'bool'),
			'roomplanner' => Request::post('roomplanner', true, 'bool'),
			'startday' => Request::post('startday', 0, 'int'),
			'scaledaysauto' => Request::post('scaledaysauto', false, 'bool'),
			'daystoshow' => Request::post('daystoshow', 7, 'int'),
			'rotation' => Request::post('rotation', 0, 'int'),
			'scale' => Request::post('scale', 50, 'int'),
			'switchtime' => Request::post('switchtime', 20, 'int'),
			'calupdate' => Request::post('calupdate', 120, 'int'),
			'roomupdate' => Request::post('roomupdate', 30, 'int'),
			'hostname' => Request::post('hostname', false, 'bool'),
		);
		if ($conf['roomupdate'] < 15) {
			$conf['roomupdate'] = 15;
		}
		if ($conf['calupdate'] < 30) {
			$conf['calupdate'] = 30;
		}

		$overrides = array();
		for ($i = 0; $i < sizeof($locationids); $i++) {
			$overrideLoc = Request::post('override'.$locationids[$i], false, 'bool');
			if ($overrideLoc) {
				$overrideArray = array(
					'mode' => Request::post('override'.$locationids[$i].'mode', 1, 'int'),
					'roomplanner' => Request::post('override'.$locationids[$i].'roomplanner', false, 'bool'),
					'vertical' => Request::post('override'.$locationids[$i].'vertical', false, 'bool'),
					'startday' => Request::post('override'.$locationids[$i].'startday', 0, 'int'),
					'scaledaysauto' => Request::post('override'.$locationids[$i].'scaledaysauto', false, 'bool'),
					'daystoshow' => Request::post('override'.$locationids[$i].'daystoshow', 7, 'int'),
					'rotation' => Request::post('override'.$locationids[$i].'rotation', 0, 'int'),
					'scale' => Request::post('override'.$locationids[$i].'scale', 50, 'int'),
					'switchtime' => Request::post('override'.$locationids[$i].'switchtime', 60, 'int'),
				);
				$overrides[$locationids[$i]] = $overrideArray;
			}
		}
		$conf['overrides'] = $overrides;

		return array('config' => $conf, 'locationids' => $locationids);
	}

	/**
	 * @return array{config: array, locationids: array}
	 */
	private function preparePanelConfigUrl(): array
	{
		$bookmarkNames = Request::post('bookmarkNames', [], 'array');
		$bookmarkUrls = Request::post('bookmarkUrls', [], 'array');
		$bookmarkString = '';
		for ($i = 0; $i < count($bookmarkNames); $i++) {
			if ($bookmarkNames[$i] == '' || $bookmarkUrls[$i] == '') continue;
			$bookmarkString .= rawurlencode($bookmarkNames[$i]);
			$bookmarkString .= ",";
			$bookmarkString .= rawurlencode($bookmarkUrls[$i]);
			$bookmarkString .= " ";
		}
		$bookmarkString = substr($bookmarkString, 0, -1);

		$conf = array(
			'url' => Request::post('url', 'https://www.bwlehrpool.de/', 'string'),
			'insecure-ssl' => Request::post('insecure-ssl', 0, 'int'),
			'reload-minutes' => max(0, Request::post('reloadminutes', 0, 'int')),
			'whitelist' => preg_replace("/[\r\n]+/m", "\n", Request::post('whitelist', '', 'string')),
			'blacklist' => preg_replace("/[\r\n]+/m", "\n", Request::post('blacklist', '', 'string')),
			'split-login' => Request::post('split-login', 0, 'bool'),
			'browser' => Request::post('browser', 'firefox', 'string'),
			'interactive' => Request::post('interactive', '0', 'bool'),
			'hw-video' => Request::post('hw-video', '0', 'bool'),
			'bookmarks' => $bookmarkString ?: '',
			'allow-tty' => Request::post('allow-tty', '', 'string'),
			'zoom-factor' => Request::post('zoom-factor', 100, 'int'),
		);
		return array('config' => $conf, 'locationids' => []);
	}

	/**
	 * @return array{config: array, locationids: array}
	 */
	private function preparePanelConfigSummary(): array
	{
		// Build json structure
		$conf = array(
			'language' => Request::post('language', 'en', 'string'),
			'eco' => Request::post('eco', false, 'bool'),
			'roomplanner' => Request::post('roomplanner', false, 'bool'),
			'panelupdate' => Request::post('panelupdate', 30, 'int')
		);
		if ($conf['panelupdate'] < 15) {
			$conf['panelupdate'] = 15;
		}
		// Check locations
		$locationids = self::getLocationIdsFromRequest();
		return array('config' => $conf, 'locationids' => $locationids);
	}

	/**
	 * Updates the server settings in the db.
	 */
	private function updateServerSettings(): void
	{
		User::assertPermission('backend.edit');
		$serverid = Request::post('id', -1, 'int');
		$servername = Request::post('name', 'unnamed', 'string');
		$servertype = Request::post('type', '', 'string');
		$backend = CourseBackend::getInstance($servertype);

		if ($backend === false) {
			Message::addError('invalid-backend-type', $servertype);
			Util::redirect('?do=locationinfo');
		}

		$tmptypeArray = $backend->getCredentialDefinitions();

		$credentialsJson = array();
		foreach ($tmptypeArray as $cred) {
			$credentialsJson[$cred->property] = Request::post('prop-' . $cred->property);
		}
		$params = array(
			'name' => $servername,
			'type' => $servertype,
			'credentials' => json_encode($credentialsJson)
		);
		if ($serverid === 0) {
			Database::exec('INSERT INTO `locationinfo_coursebackend` (servername, servertype, credentials)
					VALUES (:name, :type, :credentials)', $params);
			$this->checkConnection(Database::lastInsertId());
		} else {
			$params['id'] = $serverid;
			Database::exec('UPDATE `locationinfo_coursebackend`
					SET servername = :name, servertype = :type, credentials = :credentials
					WHERE serverid = :id', $params);
			$this->checkConnection($serverid);
		}
	}

	/**
	 * Checks if the server connection to a backend is valid.
	 *
	 * @param int $id Server id which connection should be checked.
	 */
	private function checkConnection(int $serverid = 0): void
	{
		if ($serverid === 0) {
			ErrorHandler::traceError('checkConnection called with no server id');
		}
		User::assertPermission('backend.check');

		$dbresult = Database::queryFirst("SELECT servertype, credentials
				FROM `locationinfo_coursebackend`
				WHERE serverid = :serverid", array('serverid' => $serverid));

		$serverInstance = CourseBackend::getInstance($dbresult['servertype']);
		if ($serverInstance === false) {
			LocationInfo::setServerError($serverid, 'Unknown backend type: ' . $dbresult['servertype']);
			return;
		}
		$credentialsOk = $serverInstance->setCredentials($serverid,
			(array)json_decode($dbresult['credentials'], true));

		if ($credentialsOk) {
			$serverInstance->checkConnection();
		}

		LocationInfo::setServerError($serverid, $serverInstance->getErrors());
	}

	private function loadBackends(): array
	{
		// Get a list of all the backend types.
		$servertypes = array();
		$s_list = CourseBackend::getList();
		foreach ($s_list as $s) {
			$typeInstance = CourseBackend::getInstance($s);
			$servertypes[$s] = $typeInstance->getDisplayName();
		}
		// Build list of defined backends
		$serverlist = array();
		$dbquery2 = Database::simpleQuery("SELECT * FROM `locationinfo_coursebackend` ORDER BY servername ASC");
		foreach ($dbquery2 as $row) {
			if (isset($servertypes[$row['servertype']])) {
				$row['typename'] = $servertypes[$row['servertype']];
			} else {
				$row['typename'] = '[' . $row['servertype'] . ']';
				$row['disabled'] = 'disabled';
			}

			if (!empty($row['error'])) {
				$error = json_decode($row['error'], true);
				if (isset($error['timestamp'])) {
					$time = date('Y-m-d H:i', $error['timestamp']);
				} else {
					$time = '???';
				}
				$row['error'] = $error['error'];
				$row['errtime'] = $time;
			}
			$serverlist[] = $row;
		}
		return $serverlist;
	}

	/**
	 * Show the list of backends
	 */
	private function showBackendsTable(array $serverlist): void
	{
		User::assertPermission('backend.*');
		$data = array(
			'serverlist' => $serverlist,
		);
		Permission::addGlobalTags($data['perms'], null, ['backend.edit', 'backend.check']);
		// Pass the data to the html and render it.
		Render::addTemplate('page-servers', $data);
	}

	private function showBackendLog(): void
	{
		$id = Request::get('serverid', false, 'int');
		if ($id === false) {
			Message::addError('main.parameter-missing', 'serverid');
			Util::redirect('?do=locationinfo');
		}
		$server = Database::queryFirst('SELECT servername FROM locationinfo_coursebackend
				WHERE serverid = :id', ['id' => $id]);
		if ($server === false) {
			Message::addError('invalid-server-id', $id);
			Util::redirect('?do=locationinfo');
		}
		$server['list'] = [];
		$res = Database::simpleQuery('SELECT dateline, message FROM locationinfo_backendlog
				WHERE serverid = :id ORDER BY logid DESC LIMIT 100', ['id' => $id]);
		foreach ($res as $row) {
			$row['dateline_s'] = Util::prettyTime($row['dateline']);
			$row['class'] = substr($row['message'], 0, 3) === '[F]' ? 'text-danger' : 'text-warning';
			$row['message'] = Substr($row['message'], 3);
			$server['list'][] = $row;
		}
		Render::addTemplate('page-server-log', $server);
	}

	private function showLocationsTable(): void
	{
		$allowedLocations = User::getAllowedLocations('location.edit');
		if (empty($allowedLocations)) {
			Message::addError('main.no-permission');
			return;
		}
		$locations = Location::getLocations(0, 0, false, true);

		// Get hidden state of all locations
		$dbquery = Database::simpleQuery("SELECT li.locationid, li.serverid, li.serverlocationid, loc.openingtime, li.lastcalendarupdate, cb.servertype, cb.servername
			FROM `locationinfo_locationconfig` AS li
			LEFT JOIN `locationinfo_coursebackend` AS cb USING (serverid)
			LEFT JOIN `location` AS loc USING (locationid)");

		foreach ($dbquery as $row) {
			$locid = (int)$row['locationid'];
			if (!isset($locations[$locid]) || !in_array($locid, $allowedLocations))
				continue;
			$glyph = !empty($row['openingtime']) ? 'ok' : '';
			$backend = '';
			if (!empty($row['serverid']) && !empty($row['serverlocationid'])) {
				$backend = $row['servername'] . '(' . $row['serverlocationid'] . ')';
			}
			$locations[$locid] += array(
				'openingGlyph' => $glyph,
				'strong' => $glyph === 'ok',
				'backend' => $backend,
				'lastCalendarUpdate' => Util::prettyTime($row['lastcalendarupdate']), // TODO
				'backendMissing' => !CourseBackend::exists($row['servertype']),
			);
		}

		$stack = array();
		$depth = -1;
		foreach ($locations as &$location) {
			$location['allowed'] = in_array($location['locationid'], $allowedLocations);
			while ($location['depth'] <= $depth) {
				array_pop($stack);
				$depth--;
			}
			while ($location['depth'] > $depth) {
				array_push($stack, empty($location['openingGlyph']) && ($depth === -1 || empty($stack[$depth])) ? '' : 'arrow-up');
				$depth++;
			}
			if ($depth > 0 && empty($location['openingGlyph'])) {
				$location['openingGlyph'] = $stack[$depth - 1];
			}
		}

		Render::addTemplate('page-locations', array(
			'list' => array_values($locations),
		));
	}

	private function showPanelsTable(): void
	{
		$visibleLocations = User::getAllowedLocations('panel.list');
		if (in_array(0, $visibleLocations)) {
			$visibleLocations = true;
		}
		$editLocations = User::getAllowedLocations('panel.edit');
		if (in_array(0, $editLocations)) {
			$editLocations = true;
		}
		$assignLocations = USer::getAllowedLocations('panel.assign-client');
		if (in_array(0, $assignLocations)) {
			$assignLocations = true;
		}
		if (empty($visibleLocations)) {
			Message::addError('main.no-permission');
			return;
		}
		$res = Database::simpleQuery('SELECT p.paneluuid, p.panelname, p.locationids, p.panelconfig,
			p.paneltype FROM locationinfo_panel p
			ORDER BY panelname ASC');
		$hasRunmode = Module::isAvailable('runmode');
		if ($hasRunmode) {
			$runmodes = RunMode::getForModule(Page::getModule(), true);
		}
		$panels = array();
		$locations = Location::getLocationsAssoc();
		foreach ($res as $row) {
			if ($row['paneltype'] === 'URL') {
				$url = json_decode($row['panelconfig'], true)['url'];
				$row['locations'] = $row['locationurl'] = $url;
				$row['edit_disabled'] = empty($editLocations) ? 'disabled' : '';
				$row['runmode_disabled'] = empty($assignLocations) ? 'disabled' : '';
			} else {
				$lids = explode(',', $row['locationids']);
				// Permissions
				if ($visibleLocations !== true && !empty(array_diff($lids, $visibleLocations))) {
					continue;
				}
				$row['edit_disabled'] = $editLocations !== true && !empty(array_diff($lids, $editLocations))
					? 'disabled' : '';
				$row['runmode_disabled'] = $assignLocations !== true && !empty(array_diff($lids, $assignLocations))
					? 'disabled' : '';
				// Locations
				$locs = array_map(function ($id) use ($locations) {
					return isset($locations[$id]) ? $locations[$id]['locationname'] : "<<deleted=$id>>";
				}, $lids);
				$row['locations'] = implode(', ', $locs);
			}
			$len = mb_strlen($row['panelname']);
			if ($len < 3) {
				$row['panelname'] .= str_repeat(' ', 3 - $len);
			}
			if ($hasRunmode && isset($runmodes[$row['paneluuid']])) {
				$row['assignedMachineCount'] = count($runmodes[$row['paneluuid']]);
			}
			$panels[] = $row;
		}
		Render::addTemplate('page-panels', compact('panels', 'hasRunmode'));
	}

	/**
	 * AJAX
	 */
	protected function doAjax()
	{
		User::load();
		if (!User::isLoggedIn()) {
			die('Unauthorized');
		}
		$action = Request::any('action');
		$id = Request::any('id', 0, 'int');
		if ($action === 'config-location') {
			$this->ajaxConfigLocation($id);
		} elseif ($action === 'serverSettings') {
			$this->ajaxServerSettings($id);
		}
	}

	/**
	 * Ajax the server settings.
	 *
	 * @param int $id Serverid
	 */
	private function ajaxServerSettings(int $id): void
	{
		User::assertPermission('backend.edit');
		$oldConfig = Database::queryFirst('SELECT servername, servertype, credentials
				FROM `locationinfo_coursebackend` WHERE serverid = :id', array('id' => $id));

		// Credentials stuff.
		if ($oldConfig !== false) {
			$oldCredentials = json_decode($oldConfig['credentials'], true);
		} else {
			$oldCredentials = array();
		}

		// Get a list of all the backend types.
		$serverBackends = array();
		$s_list = CourseBackend::getList();
		foreach ($s_list as $s) {
			$backendInstance = CourseBackend::getInstance($s);
			$backend = array(
				'backendtype' => $s,
				'display' => $backendInstance->getDisplayName(),
				'active' => ($oldConfig !== false && $s === $oldConfig['servertype']),
			);
			$backend['credentials'] = $backendInstance->getCredentialDefinitions();
			foreach ($backend['credentials'] as $cred) {
				/* @var BackendProperty $cred */
				if ($backend['active'] && isset($oldCredentials[$cred->property])) {
					$cred->initForRender($backendInstance->mangleProperty($cred->property, $oldCredentials[$cred->property]));
				} else {
					$cred->initForRender();
				}
				$cred->title = Dictionary::translateFile('backend-' . $s, $cred->property);
				$cred->helptext = Dictionary::translateFile('backend-' . $s, $cred->property . "_helptext");
				$cred->credentialsHtml = Render::parse('server-prop-' . $cred->template, (array)$cred);
			}
			$serverBackends[] = $backend;
		}
		echo Render::parse('ajax-config-server', array('id' => $id,
			'name' => $oldConfig['servername'],
			'currentbackend' => $oldConfig['servertype'],
			'backendList' => $serverBackends,
			'defaultBlank' => $oldConfig === false));
	}

	/**
	 * Ajax the time table
	 *
	 * @param int $id id of the location
	 */
	private function ajaxConfigLocation(int $id): void
	{
		User::assertPermission('location.edit', $id);
		$locConfig = Database::queryFirst("SELECT info.serverid, info.serverlocationid, loc.openingtime
 			FROM `locationinfo_locationconfig` AS info
 			LEFT JOIN  `location` AS loc USING (locationid)
 			WHERE locationid = :id", array('id' => $id));
		if ($locConfig !== false) {
			$openingtimes = json_decode($locConfig['openingtime'], true);
		} else {
			$locConfig = array('serverid' => null, 'serverlocationid' => '');
		}
		if (!isset($openingtimes) || !is_array($openingtimes)) {
			$openingtimes = array();
		}

		// Preset serverid from parent if none is set
		if (is_null($locConfig['serverid'])) {
			$chain = Location::getLocationRootChain($id);
			if (!empty($chain)) {
				$res = Database::simpleQuery("SELECT serverid, locationid FROM locationinfo_locationconfig
					WHERE locationid IN (:locations) AND serverid IS NOT NULL", array('locations' => $chain));
				$chain = array_flip($chain);
				$best = false;
				foreach ($res as $row) {
					if ($best === false || $chain[$row['locationid']] < $chain[$best['locationid']]) {
						$best = $row;
					}
				}
				if ($best !== false) {
					$locConfig['serverid'] = $best['serverid'];
				}
			}
		}

		// get Server / ID list
		$res = Database::simpleQuery("SELECT serverid, servername FROM locationinfo_coursebackend ORDER BY servername ASC");
		$serverList = array();
		foreach ($res as $row) {
			if ($row['serverid'] == $locConfig['serverid']) {
				$row['selected'] = 'selected';
			}
			$serverList[] = $row;
		}

		$data = array(
			'id' => $id,
			'serverlist' => $serverList,
			'serverlocationid' => $locConfig['serverlocationid'],
			'openingtimes' => $this->compressTimes($openingtimes),
		);

		echo Render::parse('ajax-config-location', $data);
	}

	/**
	 * Checks if simple mode or expert mode is active.
	 * Tries to merge/compact the opening times schedule, and
	 * will actually modify the passed array iff it can be
	 * transformed into simple opening times.
	 *
	 * @return array new optimized openingtimes
	 */
	private function compressTimes(array $array): array
	{
		if (empty($array))
			return [];
		// Decompose by day
		$DAYLIST = array_flip(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']);
		$new = [];
		foreach ($array as $row) {
			$s = Page_LocationInfo::getTime($row['openingtime']);
			$e = Page_LocationInfo::getTime($row['closingtime']);
			if ($s === null || $e === null || $e <= $s)
				continue;
			foreach ($row['days'] as $day) {
				$day = $DAYLIST[$day] ?? -1;
				if ($day === -1)
					continue;
				$this->addDay($new, $day, $s, $e);
			}
		}
		// Merge by timespan
		$merged = [];
		foreach ($new as $day => $ranges) {
			foreach ($ranges as $range) {
				$range = $range[0] . '#' . $range[1];
				if (!isset($merged[$range])) {
					$merged[$range] = [];
				}
				$merged[$range][$day] = true;
			}
		}
		// Finally transform to display struct
		$new = [];
		foreach ($merged as $span => $days) {
			$out = explode('#', $span);
			$new[] = [
				'days' => $this->buildDaysString(array_keys($days)),
				'open' => sprintf('%02d:%02d', ($out[0] / 60), ($out[0] % 60)),
				'close' => sprintf('%02d:%02d', ($out[1] / 60), ($out[1] % 60)),
			];
		}
		return $new;
	}

	/**
	 * @param array $daysArray List of days, "Monday", "Tuesday" etc. Must not contain duplicates.
	 * @return string Human-readable representation of list of days
	 */
	private function buildDaysString(array $daysArray): string
	{
		/* Dictionary::translate('monday') Dictionary::translate('tuesday') Dictionary::translate('wednesday')
		 * Dictionary::translate('thursday') Dictionary::translate('friday') Dictionary::translate('saturday')
		 * Dictionary::translate('sunday')
		 */
		$DAYLIST = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
		$output = [];
		$first = $last = -1;
		sort($daysArray);
		$daysArray[] = -1; // One trailing element to enforce a flush
		foreach ($daysArray as $day) {
			if ($first === -1) {
				$first = $last = $day;
			} elseif ($last + 1 === $day) {
				// Chain
				$last++;
			} else {
				$string = Dictionary::translate($DAYLIST[$first]);
				if ($first !== $last) {
					$string .= ($first + 1 === $last ? ",\xe2\x80\x89" : "\xe2\x80\x89-\xe2\x80\x89")
						. Dictionary::translate($DAYLIST[$last]);
				}
				$output[] = $string;
				$first = $last = $day;
			}
		}
		return implode(', ', $output);
	}

	private function addDay(&$array, $day, $s, $e)
	{
		if (!isset($array[$day])) {
			$array[$day] = array(array($s, $e));
			return;
		}
		foreach (array_keys($array[$day]) as $key) {
			$current = $array[$day][$key];
			if ($s <= $current[0] && $e >= $current[1]) {
				// Fully dominated
				unset($array[$day][$key]);
				continue; // Might partially overlap with additional ranges, keep going
			}
			if ($current[0] <= $s && $current[1] >= $s) {
				// $start lies within existing range
				if ($current[0] <= $e && $current[1] >= $e)
					return; // Fully in existing range, do nothing
				// $end seems to extend range we're checking against but $start lies within this range, update and keep going
				$s = $current[0];
				unset($array[$day][$key]);
				continue;
			}
			// Last possibility: $start is before range, $end within range
			if ($current[0] <= $e && $current[1] >= $e) {
				// $start must lie before range start, otherwise we'd have hit the case above
				$e = $current[1];
				unset($array[$day][$key]);
				//continue;
			}
		}
		$array[$day][] = array($s, $e);
	}

	/**
	 * Ajax the config of a panel.
	 */
	private function showPanelConfig(): void
	{
		$id = Request::get('uuid', false, 'string');
		if ($id === false) {
			Message::addError('main.parameter-missing', 'uuid');
			return;
		}
		$config = false;
		if ($id === 'new-default') {
			// Creating new panel
			$panel = array(
				'panelname' => '',
				'locationids' => '',
				'paneltype' => 'DEFAULT',
			);
			$id = 'new';
		} elseif ($id === 'new-summary') {
			// Creating new panel
			$panel = array(
				'panelname' => '',
				'locationids' => '',
				'paneltype' => 'SUMMARY',
			);
			$id = 'new';
		} elseif ($id === 'new-url') {
			// Creating new panel
			$panel = array(
				'panelname' => '',
				'paneltype' => 'URL',
			);
			$id = 'new';
		} else {
			// Get Config data from db
			$panel = Database::queryFirst("SELECT panelname, locationids, paneltype, panelconfig
				FROM locationinfo_panel
				WHERE paneluuid = :id", array('id' => $id));
			if ($panel === false) {
				Message::addError('invalid-panel-id', $id);
				return;
			}

			$config = json_decode($panel['panelconfig'], true);
			if (!isset($config['roomplanner'])) {
				$config['roomplanner'] = true;
			}
		}

		// Permission
		$this->assertPanelPermission($panel, 'panel.edit');

		$def = LocationInfo::defaultPanelConfig($panel['paneltype']);
		if (!is_array($config)) {
			$config = $def;
		} else {
			$config += $def;
		}

		$langs = Dictionary::getLanguages(true);
		if (isset($config['language'])) {
			foreach ($langs as &$lang) {
				if ($lang['cc'] === $config['language']) {
					$lang['selected'] = 'selected';
				}
			}
		}

		if ($panel['paneltype'] === 'DEFAULT') {
			Render::addTemplate('page-config-panel-default', array(
				'new' => $id === 'new',
				'uuid' => $id,
				'panelname' => $panel['panelname'],
				'languages' => $langs,
				'mode' => $config['mode'],
				'vertical_checked' => $config['vertical'] ? 'checked' : '',
				'eco_checked' => $config['eco'] ? 'checked' : '',
				'prettytime_checked' => $config['prettytime'] ? 'checked' : '',
				'roomplanner' => $config['roomplanner'],
				'startday' => $config['startday'],
				'scaledaysauto_checked' => $config['scaledaysauto'] ? 'checked' : '',
				'daystoshow' => $config['daystoshow'],
				'rotation' => $config['rotation'],
				'scale' => $config['scale'],
				'switchtime' => $config['switchtime'],
				'calupdate' => $config['calupdate'],
				'roomupdate' => $config['roomupdate'],
				'locations' => Location::getLocations(),
				'locationids' => $panel['locationids'],
				'overrides' => json_encode($config['overrides']),
				'hostname_checked' => $config['hostname'] ? 'checked' : '',
			));
		} elseif ($panel['paneltype'] === 'URL') {

			$bookmarksArray = [];
			if ($config['bookmarks'] !== '') {
				$bookmarksConfig = explode(' ', $config['bookmarks']);
				foreach ($bookmarksConfig AS $bookmark) {
					$bookmark = explode(',', $bookmark);
					$name = rawurldecode($bookmark[0]);
					$url = rawurldecode($bookmark[1]);
					$bookmarksArray[] = [
						'name' => $name,
						'url' => $url,
					];
				}
			}

			LocationInfo::cleanupUrlFilter($config);

			Render::addTemplate('page-config-panel-url', array(
				'new' => $id === 'new',
				'uuid' => $id,
				'panelname' => $panel['panelname'],
				'url' => $config['url'],
				'zoom-factor' => $config['zoom-factor'],
				'ssl_checked' => $config['insecure-ssl'] ? 'checked' : '',
				'reloadminutes' => (int)$config['reload-minutes'],
				'whitelist' => str_replace("\n", "\r\n", $config['whitelist']),
				'blacklist' => str_replace("\n", "\r\n", $config['blacklist']),
				'split-login_checked' => $config['split-login'] ? 'checked' : '',
				'browser' => $config['browser'],
				'interactive_checked' => $config['interactive'] ? 'checked' : '',
				'hwvideo_checked' => $config['hw-video'] ? 'checked' : '',
				'bookmarks' => $bookmarksArray,
				'allow-tty_' . $config['allow-tty'] . '_checked' => 'checked',
			));
		} else {
			Render::addTemplate('page-config-panel-summary', array(
				'new' => $id === 'new',
				'uuid' => $id,
				'panelname' => $panel['panelname'],
				'languages' => $langs,
				'panelupdate' => $config['panelupdate'],
				'roomplanner' => $config['roomplanner'],
				'locations' => Location::getLocations(),
				'locationids' => $panel['locationids'],
				'eco_checked' => $config['eco'] ? 'checked' : '',
			));
		}
	}

	private function showPanel(): void
	{
		$uuid = Request::get('uuid', false, 'string');
		if ($uuid === false) {
			http_response_code(400);
			die('Missing parameter uuid');
		}
		$type = InfoPanel::getConfig($uuid, $config);
		if ($type === null) {
			http_response_code(404);
			die('Panel with given uuid not found');
		}

		if ($type === 'URL') {
			Util::redirect($config['url']);
		}

		$data = array();
		preg_match('#^/(.*)/#', $_SERVER['PHP_SELF'], $script);
		preg_match('#^/([^?]+)/#', $_SERVER['REQUEST_URI'], $request);
		if ($script[1] !== $request[1]) {
			// Working with server-side redirects
			$data['api'] = 'api/';
		} else {
			// 1:1
			$data['api'] = 'api.php?do=locationinfo&';
		}

		if ($type === 'DEFAULT') {
			$data += array(
				'uuid' => $uuid,
				'config' => json_encode($config),
				'language' => $config['language'],
			);

			die(Render::parse('frontend-default', $data, null, $config['language']));
		}

		if ($type === 'SUMMARY') {
			$locations = LocationInfo::getLocationsOr404($uuid, false);
			$config['tree'] = Location::getRecursive($locations);
			$data += array(
				'uuid' => $uuid,
				'config' => json_encode($config),
				'language' => $config['language'],
			);

			die(Render::parse('frontend-summary', $data, null, $config['language']));
		}

		http_response_code(500);
		die('Unknown panel type ' . $type);
	}

	/**
	 * @param string|array $panelOrUuid UUID of panel, or array with keys paneltype and locationds
	 * @param int[] $additionalLocations
	 */
	private function assertPanelPermission($panelOrUuid, string $permission, array $additionalLocations = null): void
	{
		if (is_array($panelOrUuid)) {
			$panel = $panelOrUuid;
		} else {
			$panel = Database::queryFirst('SELECT paneltype, locationids FROM locationinfo_panel
					WHERE paneluuid = :uuid', ['uuid' => $panelOrUuid]);
		}
		if ($panel === false || $panel['paneltype'] === 'URL' || empty($panel['locationids'])) {
			if (empty($additionalLocations)) {
				User::assertPermission($permission, null, '?do=locationinfo');
				return;
			}
		}
		$allowed = User::getAllowedLocations($permission);
		if (in_array(0, $allowed))
			return;
		if (!empty($allowed)) {
			if (isset($panel['locationids'])) {
				$locations = explode(',', $panel['locationids']);
			} else {
				$locations = [];
			}
			if (!empty($additionalLocations)) {
				$locations = array_merge($locations, $additionalLocations);
			}
			if (empty(array_diff($locations, $allowed)))
				return;
		}
		Message::addError('main.no-permission');
		Util::redirect('?do=locationinfo');
	}

}