summaryrefslogtreecommitdiffstats
path: root/modules-available/locationinfo/inc/icalcoursebackend.inc.php
blob: 838d18b7ad51e03274c7d30979b37c9e48567b78 (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
<?php

abstract class ICalCourseBackend extends CourseBackend
{

	/** @var string */
	private $location;
	/** @var string */
	private $authMethod;
	/** @var string */
	private $user;
	/** @var string */
	private $pass;
	/** @var bool */
	private $verifyHostname;
	/** @var bool */
	private $verifyCert;
	/** @var bool|resource */
	private $curlHandle = false;

	protected function init(
		string $location, bool $verifyCert, bool $verifyHostname,
		string $authMethod = 'NONE', string $user = '', string $pass = '')
	{
		$this->verifyCert = $verifyCert;
		$this->verifyHostname = $verifyHostname;
		if (strpos($location, '%ID%') === false) {
			$location .= '%ID%';
		}
		$this->location = $location;
		$this->authMethod = $authMethod;
		$this->user = $user;
		$this->pass = $pass;
	}

	/**
	 * @param string $roomId room id
	 * @param callable $errorFunc
	 * @return ICalEvent[]|null all events for this room in the range -7 days to +7 days, or NULL on error
	 */
	protected function downloadIcal(string $roomId): ?array
	{
		if (!$this->isOK())
			return null;

		try {
			$ical = new ICalParser(['filterDaysBefore' => 7, 'filterDaysAfter' => 7]);
		} catch (Exception $e) {
			$this->addError('Error instantiating ICalParser: ' . $e->getMessage(), true);
			return null;
		}

		if ($this->curlHandle === false) {
			$this->curlHandle = curl_init();
		}

		$options = [
			CURLOPT_WRITEFUNCTION => function ($ch, $data) use ($ical) {
				$ical->feedData($data);
				return strlen($data);
			},
			CURLOPT_FOLLOWLOCATION => true,
			CURLOPT_SSL_VERIFYHOST => $this->verifyHostname ? 2 : 0,
			CURLOPT_SSL_VERIFYPEER => $this->verifyCert ? 1 : 0,
			CURLOPT_URL => str_replace('%ID%', $roomId, $this->location),
			CURLOPT_TIMEOUT => 60,
			CURLOPT_CONNECTTIMEOUT => 4,
		];
		if ($this->authMethod !== 'NONE' && defined('CURLAUTH_' . $this->authMethod)) {
			$options[CURLOPT_HTTPAUTH] = constant('CURLAUTH_' . $this->authMethod);
			$options[CURLOPT_USERPWD] = $this->user;
			if (!empty($this->pass)) {
				$options[CURLOPT_USERPWD] .= ':' . $this->pass;
			}
		}

		curl_setopt_array($this->curlHandle, $options);

		$curlRet = curl_exec($this->curlHandle);
		if (!$curlRet) {
			$this->addError('Curl error: ' . curl_error($this->curlHandle) . ' for ' . $roomId, false);
		}
		$ical->finish();
		if (!$ical->isValid()) {
			if ($curlRet) {
				$this->addError("Did not find a VCALENDAR in returned data for $roomId", false);
			}
			return null;
		}
		return $ical->events();
	}

	public function fetchSchedulesInternal(array $requestedRoomIds): array
	{
		if (empty($requestedRoomIds) || !$this->isOK()) {
			return array();
		}
		$tTables = [];
		foreach ($requestedRoomIds as $roomId) {
			$data = $this->downloadIcal($roomId);
			if ($data === null) {
				$this->addError("Downloading ical for $roomId failed", false);
				continue;
			}
			foreach ($data as $event) {
				$tTables[$roomId][] = array(
					'title' => $this->toTitle($event),
					'start' => $event->dtstart,
					'end' => $event->dtend,
					'cancelled' => false, // ??? How
				);
			}
		}
		return $tTables;
	}

	/**
	 * Get a usable title from either SUMMARY or DESCRIPTION
	 */
	protected function toTitle(ICalEvent $event): string
	{
		$title = $event->summary;
		if (empty($title)) {
			$title = $event->description;
		}
		if (empty($title)) {
			$title = 'Unknown';
		}
		return (string)preg_replace([',(\s*<br\s*/?>\s*|\r|\n|\\\r|\\\n)+,', '/\\\\([,;:])/'], ["\n", '$1'], $title);
	}

	protected function isOK(): bool
	{
		if (empty($this->location)) {
			$this->addError("Credentials are not set", true);
			return false;
		}
		return true;
	}

	public function __destruct()
	{
		if ($this->curlHandle !== false) {
			curl_close($this->curlHandle);
		}
	}

}