summaryrefslogtreecommitdiffstats
path: root/modules-available/locationinfo/inc/coursebackend/coursebackend_hisinone.inc.php
blob: 22b1d8fb9eda4f77c21a1934e311db292c00de23 (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
<?php

class CourseBackend_HisInOne extends CourseBackend
{
	private $username = '';
	private $password = '';
	private $open = true;
	private $location;
	private $verifyHostname = true;
	private $verifyCert = true;
	/**
	 * @var bool|resource
	 */
	private $curlHandle = false;


	public function setCredentialsInternal($data)
	{
		if (!$data['open']) {
			// If not using OpenCourseService, require credentials
			foreach (['username', 'password'] as $field) {
				if (empty($data[$field])) {
					$this->error = 'setCredentials: Missing field ' . $field;
					return false;
				}
			}
		}
		if (empty($data['baseUrl'])) {
			$this->error = "No url is given";
			return false;
		}

		$this->error = false;
		$this->username = $data['username'];
		if (!empty($data['role'])) {
			$this->username .= "\t" . $data['role'];
		}
		$this->password = $data['password'];
		$this->open = $data['open'] !== 'CourseService';
		$url = preg_replace('#(/+qisserver(/+services\d+(/+OpenCourseService)?)?)?\W*$#i', '', $data['baseUrl']);
		if ($this->open) {
			$this->location = $url . "/qisserver/services2/OpenCourseService";
		} else {
			$this->location = $url . "/qisserver/services2/CourseService";
		}
		$this->verifyHostname = $data['verifyHostname'];
		$this->verifyCert = $data['verifyCert'];

		return true;
	}

	public function getCredentialDefinitions()
	{
		return [
			new BackendProperty('baseUrl', 'string'),
			new BackendProperty('username', 'string'),
			new BackendProperty('role', 'string'),
			new BackendProperty('password', 'password'),
			new BackendProperty('open', ['OpenCourseService', 'CourseService'], 'OpenCourseService'),
			new BackendProperty('verifyCert', 'bool', true),
			new BackendProperty('verifyHostname', 'bool', true)
		];
	}

	public function checkConnection()
	{
		if (empty($this->location)) {
			$this->error = "Credentials are not set";
		} else {
			$this->findUnit(123456789, date('Y-m-d'), true);
		}
		return $this->error === false;
	}

	/**
	 * @param int $roomId his in one room id to get
	 * @param bool $connectionCheckOnly true will only check if no soapError is returned, return value will be empty
	 * @return array|bool if successful an array with the event ids that take place in the room
	 */
	public function findUnit($roomId, $day, $connectionCheckOnly = false)
	{
		$doc = new DOMDocument('1.0', 'utf-8');
		$doc->formatOutput = true;
		$envelope = $doc->createElementNS('http://schemas.xmlsoap.org/soap/envelope/', 'SOAP-ENV:Envelope');
		$doc->appendChild($envelope);
		if ($this->open) {
			$envelope->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ns1', 'http://www.his.de/ws/OpenCourseService');
		} else {
			$envelope->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ns1', 'http://www.his.de/ws/CourseService');
		}
		$header = $this->getHeader($doc);
		$envelope->appendChild($header);
		//Body of the request
		$body = $doc->createElement('SOAP-ENV:Body');
		$envelope->appendChild($body);
		$findUnit = $doc->createElement('ns1:findUnit');
		$body->appendChild($findUnit);
		$findUnit->appendChild($doc->createElement('ns1:individualDatesExecutionDate', $day));
		$findUnit->appendChild($doc->createElement('ns1:roomId', $roomId));

		$soap_request = $doc->saveXML();
		$response1 = $this->postToServer($soap_request, "findUnit");
		if ($this->error !== false) {
			return false;
		}
		$response2 = $this->xmlStringToArray($response1);
		if (!is_array($response2)) {
			if ($this->error === false) {
				$this->error = 'Cannot convert XML response to array';
			}
			return false;
		}
		if (!isset($response2['soapenvBody'])) {
			$this->error = 'Backend reply is missing element soapenvBody';
			return false;
		}
		if (isset($response2['soapenvBody']['soapenvFault'])) {
			$this->error = 'SOAP-Fault (' . $response2['soapenvBody']['soapenvFault']['faultcode'] . ") " . $response2['soapenvBody']['soapenvFault']['faultstring'];
			return false;
		}
		// We only need to check if the connection is working (URL ok, credentials ok, ..) so bail out early
		if ($connectionCheckOnly) {
			return array();
		}
		if ($this->open) {
			$path = '/soapenvBody/hisfindUnitResponse/hisunits';
			$subpath = '/hisunit/hisid';
		} else {
			$path = '/soapenvBody/hisfindUnitResponse/hisunitIds';
			$subpath = '/hisid';
		}
		$idSubDoc = $this->getArrayPath($response2, $path);
		if ($idSubDoc === false) {
			$this->error = 'Cannot find ' . $path;
			//@file_put_contents('/tmp/findUnit-1.' . $roomId . '.' . microtime(true), print_r($response2, true));
			return false;
		}
		if (empty($idSubDoc))
			return $idSubDoc;
		$idList = $this->getArrayPath($idSubDoc, $subpath);
		if ($idList === false) {
			$this->error = 'Cannot find ' . $subpath . ' after ' . $path;
			@file_put_contents('/tmp/bwlp-findUnit-2.' . $roomId . '.' . microtime(true), print_r($idSubDoc, true));
		}
		return $idList;
	}

	/**
	 * @param $doc DOMDocument
	 * @return DOMElement
	 */
	private function getHeader($doc)
	{
		$header = $doc->createElement('SOAP-ENV:Header');
		$security = $doc->createElementNS('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'ns2:Security');
		$mustunderstand = $doc->createAttribute('SOAP-ENV:mustUnderstand');
		$mustunderstand->value = 1;
		$security->appendChild($mustunderstand);
		$header->appendChild($security);
		$token = $doc->createElement('ns2:UsernameToken');
		$security->appendChild($token);
		$user = $doc->createElement('ns2:Username', $this->username);
		$token->appendChild($user);
		$pass = $doc->createElement('ns2:Password', $this->password);
		$type = $doc->createAttribute('Type');
		$type->value = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText';
		$pass->appendChild($type);
		$token->appendChild($pass);
		return $header;
	}

	/**
	 * @param $request string with xml SOAP request
	 * @param $action string with the name of the SOAP action
	 * @return bool|string if successful the answer xml from the SOAP server
	 */
	private function postToServer($request, $action)
	{
		$header = array(
			"Content-type: text/xml;charset=\"utf-8\"",
			"SOAPAction: \"" . $action . "\"",
			"Content-length: " . strlen($request),
		);

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

		$options = array(
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_FOLLOWLOCATION => true,
			CURLOPT_SSL_VERIFYHOST => $this->verifyHostname ? 2 : 0,
			CURLOPT_SSL_VERIFYPEER => $this->verifyCert ? 1 : 0,
			CURLOPT_URL => $this->location,
			CURLOPT_POSTFIELDS => $request,
			CURLOPT_HTTPHEADER => $header,
		);

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

		$output = curl_exec($this->curlHandle);

		if ($output === false) {
			$this->error = 'Curl error: ' . curl_error($this->curlHandle);
		} else {
			$this->error = false;
			///Operation completed successfully
		}
		return $output;
	}

	public function getCacheTime()
	{
		return 30 * 60;
	}


	public function getRefreshTime()
	{
		return 60 * 60;
	}


	public function getDisplayName()
	{
		return "HisInOne";
	}

	public function fetchSchedulesInternal($requestedRoomIds)
	{
		if (empty($requestedRoomIds)) {
			return array();
		}
		$currentWeek = $this->getCurrentWeekDates();
		$tTables = [];
		//get all eventIDs in a given room
		$eventIds = [];
		foreach ($requestedRoomIds as $roomId) {
			$ok = false;
			foreach ($currentWeek as $day) {
				$roomEventIds = $this->findUnit($roomId, $day, false);
				if ($roomEventIds === false) {
					if ($this->error !== false) {
						error_log('Cannot findUnit(' . $roomId . '): ' . $this->error);
						$this->error = false;
					}
					// TODO: Error gets swallowed
					continue;
				}
				$ok = true;
				$eventIds = array_merge($eventIds, $roomEventIds);
			}
			if ($ok) {
				$tTables[$roomId] = [];
			}
		}
		$eventIds = array_unique($eventIds);
		if (empty($eventIds)) {
			return $tTables;
		}
		$eventDetails = [];
		//get all information on each event
		foreach ($eventIds as $eventId) {
			$event = $this->readUnit(intval($eventId));
			if ($event === false) {
				error_log('Cannot readUnit(' . $eventId . '): ' . $this->error);
				$this->error = false;
				// TODO: Error gets swallowed
				continue;
			}
			$eventDetails = array_merge($eventDetails, $event);
		}
		$name = false;
		foreach ($eventDetails as $event) {
			foreach (array('/hisdefaulttext',
							'/hisshorttext',
							'/hisshortcomment') as $path) {
				$name = $this->getArrayPath($event, $path);
				if (!empty($name) && !empty($name[0]))
					break;
				$name = false;
			}
			if ($name === false) {
				$name = ['???'];
			}
			$planElements = $this->getArrayPath($event, '/hisplanelements/hisplanelement');
			if ($planElements === false) {
				$this->error = 'Cannot find ./hisplanelements/hisplanelement';
				error_log('Cannot find ./hisplanelements/hisplanelement');
				error_log(print_r($event, true));
				continue;
			}
			foreach ($planElements as $planElement) {
				if (empty($planElement['hisplannedDates']))
					continue;
				$unitPlannedDates = $this->getArrayPath($planElement,
					'/hisplannedDates/hisplannedDate/hisindividualDates/hisindividualDate');
				if ($unitPlannedDates === false) {
					$this->error = 'Cannot find ./hisplannedDates/hisplannedDate/hisindividualDates/hisindividualDate';
					error_log('Cannot find ./hisplannedDates/hisplannedDate/hisindividualDates/hisindividualDate');
					error_log(print_r($planElement, true));
					continue;
				}
				$localName = $this->getArrayPath($planElement, '/hisdefaulttext');
				if ($localName === false || empty($localName[0])) {
					$localName = $name;
				}
				foreach ($unitPlannedDates as $plannedDate) {
					$eventRoomId = $this->getArrayPath($plannedDate, '/hisroomId')[0];
					$eventDate = $this->getArrayPath($plannedDate, '/hisexecutiondate')[0];
					if (in_array($eventRoomId, $requestedRoomIds) && in_array($eventDate, $currentWeek)) {
						$startTime = $this->getArrayPath($plannedDate, '/hisstarttime')[0];
						$endTime = $this->getArrayPath($plannedDate, '/hisendtime')[0];
						$tTables[$eventRoomId][] = array(
							'title' => $localName[0],
							'start' => $eventDate . "T" . $startTime,
							'end' => $eventDate . "T" . $endTime
						);
					}
				}
			}
		}
		return $tTables;
	}


	/**
	 * @param $unit int ID of the subject in HisInOne database
	 * @return bool|array false if there was an error otherwise an array with the information about the subject
	 */
	public function readUnit($unit)
	{
		$doc = new DOMDocument('1.0', 'utf-8');
		$doc->formatOutput = true;
		$envelope = $doc->createElementNS('http://schemas.xmlsoap.org/soap/envelope/', 'SOAP-ENV:Envelope');
		$doc->appendChild($envelope);
		if ($this->open) {
			$envelope->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ns1', 'http://www.his.de/ws/OpenCourseService');
		} else {
			$envelope->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ns1', 'http://www.his.de/ws/CourseService');
		}
		$header = $this->getHeader($doc);
		$envelope->appendChild($header);
		//body of the request
		$body = $doc->createElement('SOAP-ENV:Body');
		$envelope->appendChild($body);
		$readUnit = $doc->createElement('ns1:readUnit');
		$body->appendChild($readUnit);
		$readUnit->appendChild($doc->createElement('ns1:unitId', $unit));

		$soap_request = $doc->saveXML();
		$response1 = $this->postToServer($soap_request, "readUnit");
		if ($response1 === false) {
			return false;
		}
		$response2 = $this->xmlStringToArray($response1);
		if ($response2 === false)
			return false;
		if (!isset($response2['soapenvBody'])) {
			$this->error = 'Backend reply is missing element soapenvBody';
			return false;
		}
		if (isset($response2['soapenvBody']['soapenvFault'])) {
			$this->error = 'SOAP-Fault (' . $response2['soapenvBody']['soapenvFault']['faultcode'] . ") " . $response2['soapenvBody']['soapenvFault']['faultstring'];
			return false;
		}
		return $this->getArrayPath($response2, '/soapenvBody/hisreadUnitResponse/hisunit');
	}

	/**
	 * @return array with days of the current week in datetime format
	 */
	private function getCurrentWeekDates()
	{
		$returnValue = array();
		$startDate = time();
		for ($i = 0; $i < 7; $i++) {
			$returnValue[] = date('Y-m-d', strtotime("+{$i} day 12:00", $startDate));
		}
		return $returnValue;
	}

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

}