~eduroam/eduroam-configurator

ref: aefe68c2c8cd0ebefe1e3deec93fa305b7bd1395 eduroam-configurator/src/eduroam/cat/identityprovider.php -rw-r--r-- 9.4 KiB
aefe68c2 — Jørn Åne de Jong Fix device detection for Android 10 2 years ago
                                                                                
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
<?php declare(strict_types=1);

/*
 * This file is part of the PHP eduroam CAT client
 * A client to download data from https://cat.eduroam.org/
 *
 * Copyright: 2018-2020, Jørn Åne de Jong, Uninett AS <jorn.dejong@uninett.no>
 * SPDX-License-Identifier: BSD-3-Clause
 */

namespace eduroam\CAT;

use stdClass;

/**
 * Identity Provider
 *
 * An identity provider represents an entity in the real world that is eduroam,
 * IdP, usually an institution.  Identity providers have some attributes and
 * profiles, which can be accessed through instance methods.
 */
class IdentityProvider
{
	/**
	 * List of all identity providers by CAT base, language and IdP entity id.
	 *
	 * This variable is static to facilitate lazy-loading.
	 * The CAT API has no support to get one identity provider,
	 * so we'll have to get them all at the same time.
	 *
	 * This API call is actually meant for DiscoJuice according to the documentation,
	 * but it contains information that the other APIs don't have,
	 * such as icon and geolocation.
	 *
	 * @see https://github.com/GEANT/CAT/blob/master/tutorials/UserAPI.md
	 *
	 * @var array
	 */
	public static $allIdentityProviders = [];

	/**
	 * List of all identity providers by CAT base, country, language and IdP entity id.
	 *
	 * This variable is static to facilitate lazy-loading.
	 * The CAT API has no support to get one identity provider,
	 * so we'll have to get them all at the same time.
	 *
	 * This API call is the preferred one according to the documentation,
	 * but it is lacking in features.
	 * Lazy loading will use this if possible, and fall back to allIdentityProviders
	 * if the requested information is not available through IdPbyCountry
	 *
	 * @see https://github.com/GEANT/CAT/blob/master/tutorials/UserAPI.md
	 *
	 * @var array
	 */
	public static $identityProvidersByCountry = [];

	/**
	 * CAT instance
	 *
	 * @var CAT
	 */
	private $cat;

	/**
	 * Identity provider Entity ID in CAT API
	 *
	 * @var int
	 */
	private $id;

	/**
	 * 2-char country code
	 *
	 * @var ?string
	 */
	private $c = null;

	/**
	 * Language flag to use in requests against CAT
	 *
	 * @var string
	 */
	private $lang;

	/**
	 * Construct a lazy-loaded identity provider
	 *
	 * When any of the getters is called, the class will load the value from the
	 * static cache, or fill the static cache.
	 *
	 * @param CAT    $cat  CAT instance
	 * @param int    $id   Entity ID
	 * @param string $lang Language
	 */
	public function __construct( CAT $cat, int $id, string $lang = '' )
	{
		$this->cat = $cat;
		$this->id = $id;
		$this->lang = $lang;
	}

	/**
	 * Get all identity providers as objects as an indexed array
	 *
	 * @see https://cat.eduroam.org/doc/UserAPI/tutorial_UserAPI.pkg.html#actions.listAllIdentityProviders
	 *
	 * @param CAT    $cat  CAT instance
	 * @param string $lang Language
	 *
	 * @return IdentityProvider[]
	 */
	public static function getAllIdentityProviders( CAT $cat, string $lang = '' ): array
	{
		static::loadAllIdentityProviders( $cat, $lang );
		$idps = [];
		foreach ( static::$allIdentityProviders[$cat->getBase()][$lang] as $idpData ) {
			$idps[$idpData->entityID] = new self( $cat, (int)($idpData->entityID), $lang );
		}

		return $idps;
	}

	/**
	 * Get all identity providers from the given country as objects
	 *
	 * @see https://cat.eduroam.org/doc/UserAPI/tutorial_UserAPI.pkg.html#actions.listIdentityProviders
	 *
	 * @param CAT    $cat  CAT instance
	 * @param string $c    Country, usually two uppercase letters
	 * @param string $lang Language
	 *
	 * @return IdentityProvider[]
	 */
	public static function getIdentityProvidersByCountry( CAT $cat, string $c, string $lang = '' ): array
	{
		static::loadIdentityProvidersByCountry( $cat, $c, $lang );
		$idps = [];
		foreach ( static::$identityProvidersByCountry[$cat->getBase()][$c][$lang] as $idpData ) {
			$idps[$idpData->id] = new self( $cat, (int)$idpData->id, $lang );
			$idps[$idpData->id]->c = $c;
		}

		return $idps;
	}

	/**
	 * Get the raw data associated with this identity provider.
	 *
	 * This is the JSON data converted to a PHP object.
	 *
	 * @return stdClass
	 */
	public function getRaw(): stdClass
	{
		$this->loadAllIdentityProviders( $this->cat, $this->lang );

		return static::$allIdentityProviders[$this->cat->getBase()][$this->lang][$this->id];
	}

	/**
	 * Get the entity ID of this identity provider.
	 *
	 * @return int Entity ID
	 */
	public function getEntityID(): int
	{
		return $this->id;
	}

	/**
	 * Get the country the identity provider is located in.
	 * Country is usually represented with two uppercase letters.
	 *
	 * @return string Country code
	 */
	public function getCountry(): string
	{
		if ( isset( $this->c ) ) {
			return $this->c;
		}
		$this->c = $this->getRaw()->country;

		return $this->c;
	}

	/**
	 * Get the icon ID for this institution.
	 * This ID is usually the same as Entity ID if an icon is set,
	 * or <code>NULL</code> if no icon is set.
	 *
	 * @return null|int Icon ID
	 */
	public function getIconID()
	{
		return $this->getRaw()->icon ?? null;
	}

	/**
	 * Get the icon URL on CAT for this institution.
	 * If the institution has no icon, return NULL instead.
	 *
	 * @return null|string URL to icon, for hotlinking from CAT
	 */
	public function getIconUrl()
	{
		$icon = $this->getIconID();
		if ( null !== $icon ) {
			return $this->cat->getBase() . '?'
				. \http_build_query( [
					'action' => 'sendLogo',
					'id' => $icon,
				], '', '&', \PHP_QUERY_RFC3986 );
		}
	}

	/**
	 * Alias for #getTitle()
	 *
	 * @return string The title
	 */
	public function getDisplay(): string
	{
		return $this->getTitle();
	}

	/**
	 * Get the title of the identity provider.
	 * This is typically the friendly name of the institution.
	 *
	 * @return string The title
	 */
	public function getTitle(): string
	{
		if ( isset( $this->c )
			&& !isset( $this->getRaw()->title )
		) {
			$this->loadIdentityProvidersByCountry( $this->cat, $this->c, $this->lang );

			return $this->getRaw()->display;
		}

		return $this->getRaw()->title;
	}

	/**
	 * Get the latitude and longitude where the institution is located.
	 * This function returns a list of objects that have <code>lat</code> and <code>lon</code> properties.
	 * If no geolocation information is known, it returns an empty array.
	 *
	 * @return stdClass[] Objects with lat and lon
	 */
	public function getGeo(): array
	{
		$idp = $this->getRaw();
		foreach ( $idp->geo ?? [] as $geo ) {
			if ( \is_numeric( $geo->lat ) && \is_numeric( $geo->lon ) ) {
				$geo->lat = (float)$geo->lat;
				$geo->lon = (float)$geo->lon;
			}
		}

		return $idp->geo ?? [];
	}

	/**
	 * Get the distances from the identity provider to the provided location.
	 * This function will always return a filled array,
	 * if the institution doesn't have a location, the array will contain
	 * one infinity value.  This makes it easier to call \min() and \max().
	 *
	 * @param float $lat Latitude
	 * @param float $lon Longitude
	 *
	 * @return float[]
	 */
	public function getDistanceFrom( float $lat, float $lon ): array
	{
		$results = [];
		$geos = $this->getGeo();
		if ( !$geos ) {
			return [\INF];
		}
		foreach ( $geos as $geo ) {
			$lat2 = \deg2rad( $geo->lat );
			$lat1 = \deg2rad( $lat );
			$lon2 = \deg2rad( $geo->lon );
			$lon1 = \deg2rad( $lon );
			$results[] = \acos( \sin( $lat1 ) * \sin( $lat2 ) + \cos( $lat1 ) * \cos( $lat2 ) * \cos( $lon2 - $lon1 ) ) * 6371;
		}

		return $results;
	}

	/*
	 * #getEmail(), #getPhone() and #getWebsite() are omitted because
	 * these are not exposed via the public CAT API.
	 */

	/**
	 * Return all profiles associated with this identity provider
	 *
	 * @return Profile[]
	 */
	public function getProfiles(): array
	{
		return Profile::getProfilesByIdPEntityID( $this->cat, $this->id, $this->lang );
	}

	/**
	 * Determine if this identity provider is matched by a given search string
	 *
	 * @param string $search The search string
	 *
	 * @return bool The identity provider matches the search string
	 */
	public function hasSearchMatch( string $search ): bool
	{
		$keywords = \preg_split( '/[\s,]+/', \strtolower( \trim( $search ) ) );

		return \array_reduce( $keywords, function( bool $carry, string $item ): bool {
			return $carry && ( !$item || false !== \strpos( \strtolower( $this->getTitle() ), $item ) );
		}, true );
	}

	/**
	 * Fill lazy loaded $allIdentityProviders
	 *
	 * @see https://cat.eduroam.org/doc/UserAPI/tutorial_UserAPI.pkg.html#actions.listAllIdentityProviders
	 *
	 * @param CAT    $cat  CAT instance
	 * @param string $lang Language
	 */
	private static function loadAllIdentityProviders( CAT $cat, string $lang = '' ): void
	{
		if ( !isset( static::$allIdentityProviders[$cat->getBase()][$lang] ) ) {
			$idps = $cat->listAllIdentityProviders( $lang );
			foreach ( $idps as $idp ) {
				static::$allIdentityProviders[$cat->getBase()][$lang][$idp->id] = $idp;
			}
		}
	}

	/**
	 * Fill lazy loaded $identityProvidersByCountry
	 *
	 * @see https://cat.eduroam.org/doc/UserAPI/tutorial_UserAPI.pkg.html#actions.listIdentityProviders
	 *
	 * @param CAT    $cat  CAT instance
	 * @param string $c    Country, usually two uppercase letters
	 * @param string $lang Language
	 */
	private static function loadIdentityProvidersByCountry( CAT $cat, string $c, string $lang = '' ): void
	{
		if ( !isset( static::$identityProvidersByCountry[$cat->getBase()][$c][$lang] ) ) {
			$idps = $cat->listIdentityProviders( $c, $lang );
			foreach ( $idps as $idp ) {
				static::$identityProvidersByCountry[$cat->getBase()][$c][$lang][$idp->id] = $idp;
			}
		}
	}
}