AbstractProfile::prepShowtime()   A
last analyzed

Complexity

Conditions 6
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 14
rs 9.2222
c 0
b 0
f 0
cc 6
nc 2
nop 0
1
<?php
2
3
/*
4
 * *****************************************************************************
5
 * Contributions to this work were made on behalf of the GÉANT project, a 
6
 * project that has received funding from the European Union’s Framework 
7
 * Programme 7 under Grant Agreements No. 238875 (GN3) and No. 605243 (GN3plus),
8
 * Horizon 2020 research and innovation programme under Grant Agreements No. 
9
 * 691567 (GN4-1) and No. 731122 (GN4-2).
10
 * On behalf of the aforementioned projects, GEANT Association is the sole owner
11
 * of the copyright in all material which was developed by a member of the GÉANT
12
 * project. GÉANT Vereniging (Association) is registered with the Chamber of 
13
 * Commerce in Amsterdam with registration number 40535155 and operates in the 
14
 * UK as a branch of GÉANT Vereniging.
15
 * 
16
 * Registered office: Hoekenrode 3, 1102BR Amsterdam, The Netherlands. 
17
 * UK branch address: City House, 126-130 Hills Road, Cambridge CB2 1PQ, UK
18
 *
19
 * License: see the web/copyright.inc.php file in the file structure or
20
 *          <base_url>/copyright.php after deploying the software
21
 */
22
23
/**
24
 * This file contains the AbstractProfile class. It contains common methods for
25
 * both RADIUS/EAP profiles and SilverBullet profiles
26
 *
27
 * @author Stefan Winter <[email protected]>
28
 * @author Tomasz Wolniewicz <[email protected]>
29
 * @author Maja Górecka-Wolniewicz <[email protected]>
30
 *
31
 * @package Developer
32
 *
33
 */
34
35
namespace core;
36
37
use \Exception;
0 ignored issues
show
Bug introduced by
The type \Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
38
39
/**
40
 * This class represents an EAP Profile.
41
 * Profiles can inherit attributes from their IdP, if the IdP has some. Otherwise,
42
 * one can set attribute in the Profile directly. If there is a conflict between
43
 * IdP-wide and Profile-wide attributes, the more specific ones (i.e. Profile) win.
44
 * 
45
 * @author Stefan Winter <[email protected]>
46
 * @author Tomasz Wolniewicz <[email protected]>
47
 *
48
 * @license see LICENSE file in root directory
49
 *
50
 * @package Developer
51
 */
52
abstract class AbstractProfile extends EntityWithDBProperties
53
{
54
55
    const HIDDEN = -1;
56
    const AVAILABLE = 0;
57
    const UNAVAILABLE = 1;
58
    const INCOMPLETE = 2;
59
    const NOTCONFIGURED = 3;
60
    const PROFILETYPE_RADIUS = "RADIUS";
61
    const PROFILETYPE_SILVERBULLET = "SILVERBULLET";
62
    public const SERVERNAME_ADDED = 2;
63
    public const CA_ADDED = 3;
64
    public const CA_CLASH_ADDED = 4;
65
    public const SERVER_CERT_ADDED = 5;
66
    public const CA_ROOT_NO_EXT = 6;
67
68
    /**
69
     * DB identifier of the parent institution of this profile
70
     * @var integer
71
     */
72
    public $institution;
73
74
    /**
75
     * name of the parent institution of this profile in the current language
76
     * @var string
77
     */
78
    public $instName;
79
80
    /**
81
     * realm of this profile (empty string if unset)
82
     * @var string
83
     */
84
    public $realm;
85
86
    /**
87
     * This array holds the supported EAP types (in object representation). 
88
     * 
89
     * They are not synced against the DB after instantiation.
90
     * 
91
     * @var array
92
     */
93
    protected $privEaptypes;
94
95
    /**
96
     * number of profiles of the IdP this profile is attached to
97
     * 
98
     * @var integer
99
     */
100
    protected $idpNumberOfProfiles;
101
102
    /**
103
     * IdP-wide attributes of the IdP this profile is attached to
104
     * 
105
     * @var array
106
     */
107
    protected $idpAttributes;
108
109
    /**
110
     * Federation level attributes that this profile is attached to via its IdP
111
     * 
112
     * @var array
113
     */
114
    protected $fedAttributes;
115
116
    /**
117
     * This class also needs to handle frontend operations, so needs its own
118
     * access to the FRONTEND database. This member stores the corresponding 
119
     * handle.
120
     * 
121
     * @var DBConnection
122
     */
123
    protected $frontendHandle;
124
125
    /**
126
     * readiness levels for OpenRoaming column in profiles)
127
     */
128
    const OVERALL_OPENROAMING_LEVEL_NO = 4;
129
    const OVERALL_OPENROAMING_LEVEL_GOOD = 3;
130
    const OVERALL_OPENROAMING_LEVEL_NOTE = 2;
131
    const OVERALL_OPENROAMING_LEVEL_WARN = 1;
132
    const OVERALL_OPENROAMING_LEVEL_ERROR = 0;
133
    
134
/**
135
 * constants used for displaying messages
136
 */    
137
    const OPENROAMING_ALL_GOOD = 24;
138
    const OPENROAMING_NO_REALM = 17; //none
139
    const OPENROAMING_BAD_SRV = 16; //none
140
    const OPENROAMING_BAD_NAPTR = 10; // warning
141
    const OPENROAMING_SOME_BAD_CONNECTIONS = 8; //warning
142
    const OPENROAMING_NO_DNSSEC = 8; //warning
143
    const OPENROAMING_NO_NAPTR = 3; //error
144
    const OPENROAMING_BAD_NAPTR_RESOLVE = 2; //error
145
    const OPENROAMING_BAD_SRV_RESOLVE = 1; //error
146
    const OPENROAMING_BAD_CONNECTION = 0; //error
147
    
148
    
149
    const READINESS_LEVEL_NOTREADY = 0;
150
    const READINESS_LEVEL_SUFFICIENTCONFIG = 1;
151
    const READINESS_LEVEL_SHOWTIME = 2;
152
    
153
    
154
    const CERT_STATUS_NONE = -1;
155
    const CERT_STATUS_OK = 0;
156
    const CERT_STATUS_WARN = 1;
157
    const CERT_STATUS_ERROR = 2;
158
    
159
    const OVERALL_OPENROAMING_INDEX = [
160
        self::OVERALL_OPENROAMING_LEVEL_NO => 'OVERALL_OPENROAMING_LEVEL_NO',
161
        self::OVERALL_OPENROAMING_LEVEL_GOOD => 'OVERALL_OPENROAMING_LEVEL_GOOD',
162
        self::OVERALL_OPENROAMING_LEVEL_NOTE => 'OVERALL_OPENROAMING_LEVEL_NOTE',
163
        self::OVERALL_OPENROAMING_LEVEL_WARN => 'OVERALL_OPENROAMING_LEVEL_WARN',
164
        self::OVERALL_OPENROAMING_LEVEL_ERROR => 'OVERALL_OPENROAMING_LEVEL_ERROR',      
165
    ];
166
167
    const OPENROAMING_INDEX = [
168
        self::OVERALL_OPENROAMING_LEVEL_NO => 'OVERALL_OPENROAMING_LEVEL_NO',
169
    ];
170
    
171
    const CERT_STATUS_INDEX = [
172
        self::CERT_STATUS_OK => 'CERT_STATUS_OK',
173
        self::CERT_STATUS_WARN => 'CERT_STATUS_WARN',
174
        self::CERT_STATUS_ERROR => 'CERT_STATUS_ERROR',     
175
    ];
176
    
177
    /**
178
     *  generates a detailed log of which installer was downloaded
179
     * 
180
     * @param int    $idpIdentifier the IdP identifier
181
     * @param int    $profileId     the Profile identifier
182
     * @param string $deviceId      the Device identifier
183
     * @param string $area          the download area (user, silverbullet, admin)
184
     * @param string $lang          the language of the installer
185
     * @param int    $eapType       the EAP type of the installer
186
     * @return void
187
     * @throws Exception
188
     */
189
    protected function saveDownloadDetails($idpIdentifier, $profileId, $deviceId, $area, $lang, $eapType, $openRoaming)
190
    {
191
        if (\config\Master::PATHS['logdir']) {
192
            $file = fopen(\config\Master::PATHS['logdir']."/download_details.log", "a");
193
            if ($file === FALSE) {
194
                throw new Exception("Unable to open file for append: $file");
195
            }
196
            fprintf($file, "%-015s;%d;%d;%s;%s;%s;%d;%d\n", microtime(TRUE), $idpIdentifier, $profileId, $deviceId, $area, $lang, $eapType, $openRoaming);
197
            fclose($file);
198
        }
199
    }
200
201
    /**
202
     * checks if security-relevant parameters have changed
203
     * 
204
     * @param AbstractProfile $old old instantiation of a profile to compare against
205
     * @param AbstractProfile $new new instantiation of a profile 
206
     * @return array there are never any user-induced changes in SB
207
     */
208
    public static function significantChanges($old, $new)
209
    {
210
        $retval = [];
211
        // check if a CA was added
212
        $x509 = new common\X509();
213
        $baselineCA = [];
214
        $baselineCApublicKey = [];
215
        foreach ($old->getAttributes("eap:ca_file") as $oldCA) {
216
            $ca = $x509->processCertificate($oldCA['value']);
217
            $baselineCA[$ca['sha1']] = $ca['name'];
218
            $baselineCApublicKey[$ca['sha1']] = $ca['full_details']['public_key'];
219
        }
220
        // remove the new ones that are identical to the baseline
221
        foreach ($new->getAttributes("eap:ca_file") as $newCA) {
222
            $ca = $x509->processCertificate($newCA['value']);
223
            if (array_key_exists($ca['sha1'], $baselineCA) || $ca['root'] != 1) {
224
                // do nothing; we assume here that SHA1 doesn't clash
225
                continue;
226
            }
227
            // check if a CA with identical DN was added - alert NRO if so
228
            $foundSHA1 = array_search($ca['name'], $baselineCA);
229
            if ($foundSHA1 !== FALSE) {
230
                // but only if the public key does not match
231
                if ($baselineCApublicKey[$foundSHA1] === $ca['full_details']['public_key']) {
232
                    continue;
233
                }
234
                $retval[AbstractProfile::CA_CLASH_ADDED] .= "#SHA1 for CA with DN '".$ca['name']."' has SHA1 fingerprints (pre-existing) "./** @scrutinizer ignore-type */ array_search($ca['name'], $baselineCA)." and (added) ".$ca['sha1'];
235
            } else {
236
                $retval[AbstractProfile::CA_ADDED] .= "#CA with DN '"./** @scrutinizer ignore-type */ print_r($ca['name'], TRUE)."' and SHA1 fingerprint ".$ca['sha1']." was added as trust anchor";
237
            }
238
        }
239
        // check if a servername was added
240
        $baselineNames = [];
241
        foreach ($old->getAttributes("eap:server_name") as $oldName) {
242
            $baselineNames[] = $oldName['value'];
243
        }
244
        foreach ($new->getAttributes("eap:server_name") as $newName) {
245
            if (!in_array($newName['value'], $baselineNames)) {
246
                $retval[AbstractProfile::SERVERNAME_ADDED] .= "#New server name '".$newName['value']."' added";
247
            }
248
        }
249
        return $retval;
250
    }
251
252
    /**
253
     * Tests OpenRoaming aspects of the profile like DNS settings and server reachibility
254
     * 
255
     * @return array of arrays of the form [['level' => $level, 'explanation' => $explanation, 'reason' => $reason]];
256
     */
257
    public function openroamingReadinessTest() {
258
        // do OpenRoaming initial diagnostic checks
259
        // numbers correspond to RFC7585Tests::OVERALL_LEVEL
260
        $results = [];
261
        $resultLevel = $this::OVERALL_OPENROAMING_LEVEL_GOOD; // assume all is well, degrade if we have concrete findings to suggest otherwise
262
        $tag = "aaa+auth:radius.tls.tcp";
263
        // do we know the realm at all? Notice if not.
264
        if (!isset($this->getAttributes("internal:realm")[0]['value'])) {
265
            $explanation = _("The profile information does not include the realm, so no DNS checks for OpenRoaming can be executed.");
266
            $level = $this::OVERALL_OPENROAMING_LEVEL_NOTE;
267
            $reason = $this::OPENROAMING_NO_REALM;
268
            $results[] = ['level' => $level, 'explanation' => $explanation, 'reason' => $reason];
269
            $resultLevel = min([$resultLevel, $level]);
270
        } else {
271
            $dnsChecks = new \core\diag\RFC7585Tests($this->getAttributes("internal:realm")[0]['value'], $tag);
272
            $relevantNaptrRecords = $dnsChecks->relevantNAPTR();
273
            if ($relevantNaptrRecords <= 0) {
274
                $explanation = _("There is no relevant DNS NAPTR record ($tag) for this realm. OpenRoaming will not work.");
275
                $reason = $this::OPENROAMING_NO_NAPTR;
276
                $level = $this::OVERALL_OPENROAMING_LEVEL_ERROR;
277
                $results[] = ['level' => $level, 'explanation' => $explanation, 'reason' => $reason];
278
                $resultLevel = min([$resultLevel, $level]);
279
            } else {
280
                $recordCompliance = $dnsChecks->relevantNAPTRcompliance();
281
                if ($recordCompliance != \core\diag\AbstractTest::RETVAL_OK) {
282
                    $explanation = _("The DNS NAPTR record ($tag) for this realm is not syntax conform. OpenRoaming will likely not work.");
283
                    $reason = $this::OPENROAMING_BAD_NAPTR;
284
                    $level = $this::OVERALL_OPENROAMING_LEVEL_WARN;
285
                    $results[] = ['level' => $level, 'explanation' => $explanation, 'reason' => $reason];
286
                    $resultLevel = min([$resultLevel, $level]);
287
                }
288
                // check if target is the expected one, if set by NRO
289
                foreach ($this->fedAttributes as $attr) {
290
                    if ($attr['name'] === 'fed:openroaming_customtarget') {
291
                        $customText = $attr['value'];
292
                    } else {
293
                        $customText = '';
294
                    }
295
                }
296
                if ($customText !== '') {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $customText seems to be defined by a foreach iteration on line 289. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
297
                    foreach ($dnsChecks->NAPTR_records as $orpointer) {
298
                        if ($orpointer["replacement"] != $customText) {
299
                            $explanation = _("The SRV target of an OpenRoaming NAPTR record is unexpected.");
300
                            $reason = $this::OPENROAMING_BAD_SRV;
301
                            $level = $this::OVERALL_OPENROAMING_LEVEL_NOTE;
302
                            $results[] = ['level' => $level, 'explanation' => $explanation, 'reason' => $reason];
303
                            $resultLevel = min([$resultLevel, $level]);
304
                        }
305
                    }
306
                }
307
                $srvResolution = $dnsChecks->relevantNAPTRsrvResolution();
308
                $hostnameResolution = $dnsChecks->relevantNAPTRhostnameResolution();
309
310
                if ($srvResolution <= 0) {
311
                    $explanation = _("The DNS SRV target for NAPTR $tag does not resolve. OpenRoaming will not work.");
312
                    $level = $this::OVERALL_OPENROAMING_LEVEL_ERROR;
313
                    $reason = $this::OPENROAMING_BAD_NAPTR_RESOLVE;
314
                    $results[] = ['level' => $level, 'explanation' => $explanation, 'reason' => $reason];
315
                    $resultLevel = min([$resultLevel, $this::OVERALL_OPENROAMING_LEVEL_ERROR]);
316
                } elseif ($hostnameResolution <= 0) {
317
                    $explanation = _("The DNS hostnames in the SRV records do not resolve to actual host IPs. OpenRoaming will not work.");
318
                    $level = $this::OVERALL_OPENROAMING_LEVEL_ERROR;
319
                    $reason = $this::OPENROAMING_BAD_SRV_RESOLVE;
320
                    $results[] = ['level' => $level, 'explanation' => $explanation, 'reason' => $reason];
321
                    $resultLevel = min([$resultLevel, $level]);
322
                }
323
                // connect to all IPs we found and see if they are really an OpenRoaming server
324
                $allHostsOkay = TRUE;
325
                $oneHostOkay = FALSE;
326
                $testCandidates = [];
327
                foreach ($dnsChecks->NAPTR_hostname_records as $oneServer) {
328
                    $testCandidates[$oneServer['hostname']][] = ($oneServer['family'] == "IPv4" ? $oneServer['IP'] : "[".$oneServer['IP']."]").":".$oneServer['port'];
329
                }
330
                foreach ($testCandidates as $oneHost => $listOfIPs) {
331
                    $connectionTests = new \core\diag\RFC6614Tests(array_values($listOfIPs), $oneHost, "openroaming");
332
                    // for now (no OpenRoaming client certs available) only run server-side tests
333
                    foreach ($listOfIPs as $oneIP) {
334
                        $connectionResult = $connectionTests->cApathCheck($oneIP);
335
                        if ($connectionResult != \core\diag\AbstractTest::RETVAL_OK || ( isset($connectionTests->TLS_CA_checks_result['cert_oddity']) && count($connectionTests->TLS_CA_checks_result['cert_oddity']) > 0)) {
336
                            $allHostsOkay = FALSE;
337
                        } else {
338
                            $oneHostOkay = TRUE;
339
                        }
340
                    }
341
                }
342
                if (!$allHostsOkay) {
343
                    if (!$oneHostOkay) {
344
                        $explanation = _("When connecting to the discovered OpenRoaming endpoints, they all had errors. OpenRoaming will likely not work.");
345
                        $level = $this::OVERALL_OPENROAMING_LEVEL_ERROR;
346
                        $reason = $this::OPENROAMING_BAD_CONNECTION;
347
                        $results[] = ['level' => $level, 'explanation' => $explanation, 'reason' => $reason];
348
                        $resultLevel = min([$resultLevel, $level]);
349
                    } else {
350
                        $explanation = _("When connecting to the discovered OpenRoaming endpoints, only a subset of endpoints had no errors.");
351
                        $level = $this::OVERALL_OPENROAMING_LEVEL_WARN;
352
                        $reason = $this::OPENROAMING_SOME_BAD_CONNECTIONS;
353
                        $results[] = ['level' => $level, 'explanation' => $explanation, 'reason' => $reason];
354
                        $resultLevel = min([$resultLevel, $level]);
355
                    }
356
                }
357
            }
358
        }
359
        if (!$dnsChecks->allResponsesSecure) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dnsChecks does not seem to be defined for all execution paths leading up to this point.
Loading history...
360
            $explanation = _("At least one DNS response was NOT secured using DNSSEC. OpenRoaming ANPs may refuse to connect to the endpoint.");
361
            $level = $this::OVERALL_OPENROAMING_LEVEL_WARN;
362
            $reason = $this::OPENROAMING_NO_DNSSEC;
363
            $results[] = ['level' => $level, 'explanation' => $explanation, 'reason' => $reason];
364
            $resultLevel = min([$resultLevel, $level]);
365
        }
366
        if ($resultLevel == $this::OVERALL_OPENROAMING_LEVEL_GOOD) {
367
            $explanation = _("Initial diagnostics regarding the DNS part of OpenRoaming (including DNSSEC) were successful.");
368
            $level = $this::OVERALL_OPENROAMING_LEVEL_GOOD;
369
            $reason = $this::OPENROAMING_ALL_GOOD;
370
            $results = [['level' => $level, 'explanation' => $explanation, 'reason' => $reason]];
371
        }               
372
        $this->setOpenRoamingReadinessInfo($resultLevel);
373
        return $results;
374
    }
375
    
376
    /**
377
     * Takes note of the OpenRoaming participation and conformance level
378
     * 
379
     * @param int $level the readiness level, as determined by RFC7585Tests
380
     * @return void
381
     */
382
    public function setOpenRoamingReadinessInfo(int $level)
383
    {
384
            $this->databaseHandle->exec("UPDATE profile SET openroaming = ? WHERE profile_id = ?", "ii", $level, $this->identifier);
385
    }
386
387
    /**
388
     * each profile has supported EAP methods, so get this from DB, Silver Bullet has one
389
     * static EAP method.
390
     * 
391
     * @return array list of supported EAP methods
392
     */
393
    protected function fetchEAPMethods()
394
    {
395
        $eapMethod = $this->databaseHandle->exec("SELECT eap_method_id 
396
                                                        FROM supported_eap supp 
397
                                                        WHERE supp.profile_id = $this->identifier 
398
                                                        ORDER by preference");
399
        $eapTypeArray = [];
400
        // SELECTs never return a boolean, it's always a resource
401
        while ($eapQuery = (mysqli_fetch_object(/** @scrutinizer ignore-type */ $eapMethod))) {
402
            $eaptype = new common\EAP($eapQuery->eap_method_id);
403
            $eapTypeArray[] = $eaptype;
404
        }
405
        $this->loggerInstance->debug(4, "This profile supports the following EAP types:\n"./** @scrutinizer ignore-type */ print_r($eapTypeArray, true));
406
        return $eapTypeArray;
407
    }
408
409
    /**
410
     * Class constructor for existing profiles (use IdP::newProfile() to actually create one). Retrieves all attributes and 
411
     * supported EAP types from the DB and stores them in the priv_ arrays.
412
     * 
413
     * sub-classes need to set the property $realm, $name themselves!
414
     * 
415
     * @param int $profileIdRaw identifier of the profile in the DB
416
     * @param IdP $idpObject    optionally, the institution to which this Profile belongs. Saves the construction of the IdP instance. If omitted, an extra query and instantiation is executed to find out.
417
     * @throws Exception
418
     */
419
    public function __construct($profileIdRaw, $idpObject = NULL)
420
    {
421
        $this->databaseType = "INST";
422
        parent::__construct(); // we now have access to our INST database handle and logging
423
        $handle = DBConnection::handle("FRONTEND");
424
        if ($handle instanceof DBConnection) {
425
            $this->frontendHandle = $handle;
426
        } else {
427
            throw new Exception("This database type is never an array!");
428
        }
429
        $profile = $this->databaseHandle->exec("SELECT inst_id FROM profile WHERE profile_id = ?", "i", $profileIdRaw);
430
        // SELECT always yields a resource, never a boolean
431
        if ($profile->num_rows == 0) {
432
            $this->loggerInstance->debug(2, "Profile $profileIdRaw not found in database!\n");
433
            throw new Exception("Profile $profileIdRaw not found in database!");
434
        }
435
        $this->identifier = $profileIdRaw;
436
        $profileQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $profile);
437
        if (!($idpObject instanceof IdP)) {
438
            $this->institution = $profileQuery->inst_id;
439
            $idp = new IdP($this->institution);
440
        } else {
441
            $idp = $idpObject;
442
            $this->institution = $idp->identifier;
443
        }
444
445
        $this->instName = $idp->name;
446
        $this->idpNumberOfProfiles = $idp->profileCount();
447
        $this->idpAttributes = $idp->getAttributes();
448
        $fedObject = new Federation($idp->federation);
449
        $this->fedAttributes = $fedObject->getAttributes();
450
        $this->loggerInstance->debug(4, "--- END Constructing new AbstractProfile object ... ---\n");
451
    }
452
453
    /**
454
     * find a profile, given its realm
455
     * 
456
     * @param string $realm the realm for which we are trying to find a profile
457
     * @return int|false the profile identifier, if any
458
     */
459
    public static function profileFromRealm($realm)
460
    {
461
        // static, need to create our own handle
462
        $handle = DBConnection::handle("INST");
463
        $execQuery = $handle->exec("SELECT profile_id FROM profile WHERE realm LIKE '%@$realm'");
464
        // a SELECT query always yields a resource, not a boolean
465
        if ($profileIdQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $execQuery)) {
466
            return $profileIdQuery->profile_id;
467
        }
468
        return FALSE;
469
    }
470
471
    /**
472
     * Constructs the outer ID which should be used during realm tests. Obviously
473
     * can only do something useful if the realm is known to the system.
474
     * 
475
     * @return string the outer ID to use for realm check operations
476
     * @throws Exception
477
     */
478
    public function getRealmCheckOuterUsername()
479
    {
480
        $realm = $this->getAttributes("internal:realm")[0]['value'] ?? FALSE;
481
        if ($realm == FALSE) { // we can't really return anything useful here
482
            throw new Exception("Unable to construct a realmcheck username if the admin did not tell us the realm. You shouldn't have called this function in this context.");
483
        }
484
        if (count($this->getAttributes("internal:checkuser_outer")) > 0 && $this->getAttributes("internal:checkuser_value")[0]['value'] != NULL) {
485
            // we are supposed to use a specific outer username for checks, 
486
            // which is different from the outer username we put into installers
487
            return $this->getAttributes("internal:checkuser_value")[0]['value']."@".$realm;
488
        }
489
        if (count($this->getAttributes("internal:use_anon_outer")) > 0 && $this->getAttributes("internal:anon_local_value")[0]['value'] != NULL ) {
490
            // no special check username, but there is an anon outer ID for
491
            // installers - so let's use that one
492
            return $this->getAttributes("internal:anon_local_value")[0]['value']."@".$realm;
493
        }
494
        // okay, no guidance on outer IDs at all - but we need *something* to
495
        // test with for the RealmChecks. So:
496
        return "@".$realm;
497
    }
498
499
    /**
500
     * update the last_changed timestamp for this profile
501
     * 
502
     * @return void
503
     */
504
    public function updateFreshness()
505
    {
506
        $this->databaseHandle->exec("UPDATE profile SET last_change = CURRENT_TIMESTAMP WHERE profile_id = $this->identifier");
507
    }
508
509
    /**
510
     * gets the last-modified timestamp (useful for caching "dirty" check)
511
     * 
512
     * @return string the date in string form, as returned by SQL
513
     */
514
    public function getFreshness()
515
    {
516
        $execLastChange = $this->databaseHandle->exec("SELECT last_change FROM profile WHERE profile_id = $this->identifier");
517
        // SELECT always returns a resource, never a boolean
518
        if ($freshnessQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $execLastChange)) {
519
            return $freshnessQuery->last_change;
520
        }
521
    }
522
    /**
523
     * tests if the configurator needs to be regenerated
524
     * returns the configurator path or NULL if regeneration is required
525
     */
526
    /**
527
     * This function tests if the configurator needs to be regenerated 
528
     * (properties of the Profile may have changed since the last configurator 
529
     * generation).
530
     * SilverBullet will always return NULL here because all installers are new!
531
     * 
532
     * @param string $device device ID to check
533
     * @return mixed a string with the path to the configurator download, or NULL if it needs to be regenerated
534
     */
535
536
    /**
537
     * This function tests if the configurator needs to be regenerated (properties of the Profile may have changed since the last configurator generation).
538
     * 
539
     * @param string $device device ID to check
540
     * @return mixed a string with the path to the configurator download, or NULL if it needs to be regenerated
541
     */
542
    public function testCache($device, $openRoaming)
543
    {
544
        $returnValue = ['cache' => NULL, 'mime' => NULL];
545
        $lang = $this->languageInstance->getLang();
546
        $result = $this->frontendHandle->exec("SELECT download_path, mime, UNIX_TIMESTAMP(installer_time) AS tm FROM downloads WHERE profile_id = ? AND device_id = ? AND lang = ? AND openroaming = ?", "issi", $this->identifier, $device, $lang, $openRoaming);
547
        // SELECT queries always return a resource, not a boolean
548
        if ($result && $cache = mysqli_fetch_object(/** @scrutinizer ignore-type */ $result)) {
549
            $execUpdate = $this->databaseHandle->exec("SELECT UNIX_TIMESTAMP(last_change) AS last_change FROM profile WHERE profile_id = $this->identifier");
550
            // SELECT queries always return a resource, not a boolean
551
            if ($lastChange = mysqli_fetch_object(/** @scrutinizer ignore-type */ $execUpdate)->last_change) {
552
                if ($lastChange < $cache->tm) {
553
                    $this->loggerInstance->debug(4, "Installer cached:$cache->download_path\n");
554
                    $returnValue = ['cache' => $cache->download_path, 'mime' => $cache->mime];
555
                }
556
            }
557
        }
558
        return $returnValue;
559
    }
560
561
    /**
562
     * Updates database with new installer location. Actually does stuff when
563
     * caching is possible; is a noop if not
564
     * 
565
     * @param string $device         the device identifier string
566
     * @param string $path           the path where the new installer can be found
567
     * @param string $mime           the mime type of the new installer
568
     * @param int    $integerEapType the inter-representation of the EAP type that is configured in this installer
569
     * @return void
570
     */
571
    abstract public function updateCache($device, $path, $mime, $integerEapType, $openRoaming);
572
573
    /** Toggle anonymous outer ID support.
574
     *
575
     * @param boolean $shallwe TRUE to enable outer identities (needs valid $realm), FALSE to disable
576
     * @return void
577
     */
578
    abstract public function setAnonymousIDSupport($shallwe);
579
580
    /**
581
     * Log a new download for our stats
582
     * 
583
     * @param string $device the device id string
584
     * @param string $area   either admin or user
585
     * @return boolean TRUE if incrementing worked, FALSE if not
586
     */
587
    public function incrementDownloadStats($device, $area, $openRoaming)
588
    {
589
        if ($area == "admin" || $area == "user" || $area == "silverbullet") {
590
            $lang = $this->languageInstance->getLang();
591
            $this->frontendHandle->exec("INSERT INTO downloads (profile_id, device_id, lang, openroaming, downloads_$area) VALUES (? ,?, ?, ?, 1) ON DUPLICATE KEY UPDATE downloads_$area = downloads_$area + 1", "issi", $this->identifier, $device, $lang, $openRoaming);
592
            $this->frontendHandle->exec("INSERT INTO downloads_history (profile_id, device_id, downloads_$area, openroaming, stat_date) VALUES (?, ?, 1, ?, DATE_FORMAT(NOW(), '%Y-%m-01')) ON DUPLICATE KEY UPDATE downloads_$area = downloads_$area + 1", "isi", $this->identifier, $device, $openRoaming);
593
            // get eap_type from the downloads table
594
            $eapTypeQuery = $this->frontendHandle->exec("SELECT eap_type FROM downloads WHERE profile_id = ? AND device_id = ? AND lang = ?", "iss", $this->identifier, $device, $lang);
595
            // SELECT queries always return a resource, not a boolean
596
            if (!$eapTypeQuery || !$eapO = mysqli_fetch_object(/** @scrutinizer ignore-type */ $eapTypeQuery)) {
597
                $this->loggerInstance->debug(2, "Error getting EAP_type from the database\n");
598
            } else {
599
                if ($eapO->eap_type == NULL) {
600
                    $this->loggerInstance->debug(2, "EAP_type not set in the database\n");
601
                } else {
602
                    $this->saveDownloadDetails($this->institution, $this->identifier, $device, $area, $this->languageInstance->getLang(), $eapO->eap_type, $openRoaming);
603
                }
604
            }
605
            return TRUE;
606
        }
607
        return FALSE;
608
    }
609
610
    /**
611
     * Retrieve current download stats from database, either for one specific device or for all devices
612
     * 
613
     * @param string $device the device id string
614
     * @return mixed user downloads of this profile; if device is given, returns the counter as int, otherwise an array with devicename => counter
615
     */
616
    public function getUserDownloadStats($device = NULL)
617
    {
618
        $columnName = "downloads_user";
619
        if ($this instanceof \core\ProfileSilverbullet) {
620
            $columnName = "downloads_silverbullet";
621
        }
622
        $returnarray = [];
623
        $numbers = $this->frontendHandle->exec("SELECT device_id, SUM($columnName) AS downloads_user FROM downloads WHERE profile_id = ? GROUP BY device_id", "i", $this->identifier);
624
        // SELECT queries always return a resource, not a boolean
625
        while ($statsQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $numbers)) {
626
            $returnarray[$statsQuery->device_id] = $statsQuery->downloads_user;
627
        }
628
        if ($device !== NULL) {
629
            if (isset($returnarray[$device])) {
630
                return $returnarray[$device];
631
            }
632
            return 0;
633
        }
634
        // we should pretty-print the device names
635
        $finalarray = [];
636
        $devlist = \devices\Devices::listDevices($this->identifier);
637
        foreach ($returnarray as $devId => $count) {
638
            if (isset($devlist[$devId])) {
639
                $finalarray[$devlist[$devId]['display']]['current'] = $count;
640
            }
641
            
642
        }
643
644
        $monthlyList = [];
645
        $monthly = $this->frontendHandle->exec("SELECT downloads_user,device_id FROM downloads_history WHERE profile_id=? AND stat_date=DATE_FORMAT(NOW(),'%Y-%m-01')", "i",  $this->identifier);
646
        while ($statsQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $monthly)) {
647
            $monthlyList[$statsQuery->device_id] = $statsQuery->downloads_user;
648
        }
649
        foreach ($monthlyList as $devId => $count) {
650
            if (isset($devlist[$devId])) {
651
                $finalarray[$devlist[$devId]['display']]['monthly'] = $count;
652
            }
653
            
654
        }        
655
        
656
        \core\common\Entity::intoThePotatoes();
657
        ksort($finalarray, SORT_STRING|SORT_FLAG_CASE);
658
        \core\common\Entity::outOfThePotatoes();
659
        return $finalarray;
660
    }
661
662
    /**
663
     * Deletes the profile from database and uninstantiates itself.
664
     * Works fine also for Silver Bullet; the first query will simply do nothing
665
     * because there are no stored options
666
     * 
667
     * @return void
668
     */
669
    public function destroy()
670
    {
671
        $this->databaseHandle->exec("DELETE FROM profile_option WHERE profile_id = $this->identifier");
672
        $this->databaseHandle->exec("DELETE FROM supported_eap WHERE profile_id = $this->identifier");
673
        $this->databaseHandle->exec("DELETE FROM profile WHERE profile_id = $this->identifier");
674
    }
675
676
    /**
677
     * Specifies the realm of this profile.
678
     * 
679
     * Forcefully type-hinting $realm parameter to string - Scrutinizer seems to
680
     * think that it can alternatively be an array<integer,?> which looks like a
681
     * false positive. If there really is an issue, let PHP complain about it at
682
     * runtime.
683
     * 
684
     * @param string $realm the realm (potentially with the local@ part that should be used for anonymous identities)
685
     * @return void
686
     */
687
    public function setRealm(string $realm)
688
    {
689
        $this->databaseHandle->exec("UPDATE profile SET realm = ? WHERE profile_id = ?", "si", $realm, $this->identifier);
690
        $this->realm = $realm;
691
    }
692
693
    /**
694
     * register new supported EAP method for this profile
695
     *
696
     * @param \core\common\EAP $type       The EAP Type, as defined in class EAP
697
     * @param int              $preference preference of this EAP Type. If a preference value is re-used, the order of EAP types of the same preference level is undefined.
698
     * @return void
699
     */
700
    public function addSupportedEapMethod(\core\common\EAP $type, $preference)
701
    {
702
        $eapInt = $type->getIntegerRep();
703
        $this->databaseHandle->exec("INSERT INTO supported_eap (profile_id, eap_method_id, preference) VALUES (?, ?, ?)", "iii", $this->identifier, $eapInt, $preference);
704
        $this->updateFreshness();
705
    }
706
707
    /**
708
     * Produces an array of EAP methods supported by this profile, ordered by preference
709
     * 
710
     * @param int $completeOnly if set and non-zero limits the output to methods with complete information
711
     * @return array list of EAP methods, (in object representation)
712
     */
713
    public function getEapMethodsinOrderOfPreference($completeOnly = 0)
714
    {
715
        $temparray = [];
716
717
        if ($completeOnly == 0) {
718
            return $this->privEaptypes;
719
        }
720
        foreach ($this->privEaptypes as $type) {
721
            if ($this->isEapTypeDefinitionComplete($type) === true) {
722
                $temparray[] = $type;
723
            }
724
        }
725
        return($temparray);
726
    }
727
728
    /**
729
     * Performs a sanity check for a given EAP type - did the admin submit enough information to create installers for him?
730
     * 
731
     * @param common\EAP $eaptype the EAP type
732
     * @return mixed TRUE if the EAP type is complete; an array of missing attributes if it's incomplete; FALSE if it's incomplete for other reasons
733
     */
734
    public function isEapTypeDefinitionComplete($eaptype)
735
    {
736
        if ($eaptype->needsServerCACert() && $eaptype->needsServerName()) {
737
            $missing = [];
738
            // silverbullet needs a support email address configured
739
            if ($eaptype->getIntegerRep() == common\EAP::INTEGER_SILVERBULLET && count($this->getAttributes("support:email")) == 0) {
740
                return ["support:email"];
741
            }
742
            $cnOption = $this->getAttributes("eap:server_name"); // cannot be set per device or eap type
743
            $caOption = $this->getAttributes("eap:ca_file"); // cannot be set per device or eap type
744
745
            if (count($caOption) > 0 && count($cnOption) > 0) {// see if we have at least one root CA cert
746
                foreach ($caOption as $oneCa) {
747
                    $x509 = new \core\common\X509();
748
                    $caParsed = $x509->processCertificate($oneCa['value']);
749
                    if ($caParsed['root'] == 1) {
750
                        return TRUE;
751
                    }
752
                }
753
                $missing[] = "eap:ca_file";
754
            }
755
            if (count($caOption) == 0) {
756
                $missing[] = "eap:ca_file";
757
            }
758
            if (count($cnOption) == 0) {
759
                $missing[] = "eap:server_name";
760
            }
761
            if (count($missing) == 0) {
762
                return TRUE;
763
            }
764
            return $missing;
765
        }
766
        return TRUE;
767
    }
768
769
    /**
770
     * list all devices marking their availabiblity and possible redirects
771
     *
772
     * @return array of device ids display names and their status
773
     */
774
    public function listDevices()
775
    {
776
        $returnarray = [];
777
        $redirect = $this->getAttributes("device-specific:redirect"); // this might return per-device ones or the general one
778
        // if it was a general one, we are done. Find out if there is one such
779
        // which has device = NULL
780
        $generalRedirect = NULL;
781
        foreach ($redirect as $index => $oneRedirect) {
782
            if ($oneRedirect["level"] == Options::LEVEL_PROFILE) {
783
                $generalRedirect = $index;
784
            }
785
        }
786
        if ($generalRedirect !== NULL) { // could be index 0
787
            return [['id' => '0', 'redirect' => $redirect[$generalRedirect]['value']]];
788
        }
789
        $preferredEap = $this->getEapMethodsinOrderOfPreference(1);
790
        $eAPOptions = [];
791
        if (count($this->getAttributes("media:openroaming")) == 1 && $this->getAttributes("media:openroaming")[0]['value'] == 'always-preagreed') {
792
            $orAlways = 1;
793
        } else {
794
            $orAlways = 0;
795
        }
796
        
797
        foreach (\devices\Devices::listDevices($this->identifier, $orAlways) as $deviceIndex => $deviceProperties) {
798
            $factory = new DeviceFactory($deviceIndex);
799
            $dev = $factory->device;
800
            // find the attribute pertaining to the specific device
801
            $group = '';
802
            $redirectUrl = 0;
803
            $redirects = [];
804
            foreach ($redirect as $index => $oneRedirect) {
805
                if ($oneRedirect["device"] == $deviceIndex) {
806
                    $redirects[] = $oneRedirect;
807
                }
808
            }
809
            if (count($redirects) > 0) {
810
                $redirectUrl = $this->languageInstance->getLocalisedValue($redirects);
811
            }
812
            $devStatus = self::AVAILABLE;
813
            $message = 0;
814
            if (isset($deviceProperties['options']) && isset($deviceProperties['options']['message']) && $deviceProperties['options']['message']) {
815
                $message = $deviceProperties['options']['message'];
816
            }
817
            if (isset($deviceProperties['group'])) {
818
                $group = $deviceProperties['group'];
819
            }
820
            $eapCustomtext = 0;
821
            $deviceCustomtext = 0;
822
            $geteduroam = 0;
823
            if ($redirectUrl === 0) {
824
                if (isset($deviceProperties['options']) && isset($deviceProperties['options']['redirect']) && $deviceProperties['options']['redirect']) {
825
                    $devStatus = self::HIDDEN;
826
                } else {
827
                    $dev->calculatePreferredEapType($preferredEap);
828
                    $eap = $dev->selectedEap;
829
                    if (count($eap) > 0) {
830
                        if (isset($eAPOptions["eap-specific:customtext"][serialize($eap)])) {
831
                            $eapCustomtext = $eAPOptions["eap-specific:customtext"][serialize($eap)];
832
                        } else {
833
                            // fetch customtexts from method-level attributes
834
                            $eapCustomtext = 0;
835
                            $customTextAttributes = [];
836
                            $attributeList = $this->getAttributes("eap-specific:customtext"); // eap-specific attributes always have the array index 'eapmethod' set
837
                            foreach ($attributeList as $oneAttribute) {
838
                                if ($oneAttribute["eapmethod"] == $eap) {
839
                                    $customTextAttributes[] = $oneAttribute;
840
                                }
841
                            }
842
                            if (count($customTextAttributes) > 0) {
843
                                $eapCustomtext = $this->languageInstance->getLocalisedValue($customTextAttributes);
844
                            }
845
                            $eAPOptions["eap-specific:customtext"][serialize($eap)] = $eapCustomtext;
846
                        }
847
                        // fetch customtexts for device
848
                        $customTextAttributes = [];
849
                        $attributeList = $this->getAttributes("device-specific:customtext");
850
                        foreach ($attributeList as $oneAttribute) {
851
                            if ($oneAttribute["device"] == $deviceIndex) { // device-specific attributes always have the array index "device" set
852
                                $customTextAttributes[] = $oneAttribute;
853
                            }
854
                        }
855
                        $deviceCustomtext = $this->languageInstance->getLocalisedValue($customTextAttributes);
856
                    } else {
857
                        $devStatus = self::UNAVAILABLE;
858
                    }
859
                    $geteduroamOpts = $this->getAttributes("device-specific:geteduroam");
860
                    foreach ($geteduroamOpts as $dev) {
861
                        if ($dev['device'] == $deviceIndex) {
862
                            $geteduroam = $dev['value'] == 'on' ? 1 : 0;
863
                        }
864
                    }
865
                }
866
            }
867
            $returnarray[] = ['id' => $deviceIndex, 'display' => $deviceProperties['display'], 'status' => $devStatus, 'redirect' => $redirectUrl, 'eap_customtext' => $eapCustomtext, 'device_customtext' => $deviceCustomtext, 'message' => $message, 'options' => $deviceProperties['options'], 'group' => $group, 'geteduroam' => $geteduroam];
868
        }
869
        return $returnarray;
870
    }
871
872
    /**
873
     * prepare profile attributes for device modules
874
     * Gets profile attributes taking into account the most specific level on which they may be defined
875
     * as well as the chosen language.
876
     * can be called with an optional $eap argument
877
     * 
878
     * @param array $eap if specified, retrieves all attributes except those not pertaining to the given EAP type
879
     * @return array list of attributes in collapsed style (index is the attrib name, value is an array of different values)
880
     */
881
    public function getCollapsedAttributes($eap = [])
882
    {
883
        $collapsedList = [];
884
        foreach ($this->getAttributes() as $attribute) {
885
            // filter out eap-level attributes not pertaining to EAP type $eap
886
            if (count($eap) > 0 && isset($attribute['eapmethod']) && $attribute['eapmethod'] != 0 && $attribute['eapmethod'] != $eap) {
887
                continue;
888
            }
889
            // create new array indexed by attribute name
890
            
891
            if (isset($attribute['device'])) {
892
                $collapsedList[$attribute['name']][$attribute['device']][] = $attribute['value'];
893
            } else {
894
                $collapsedList[$attribute['name']][] = $attribute['value'];
895
            } 
896
            // and keep all language-variant names in a separate sub-array
897
            if ($attribute['flag'] == "ML") {
898
                $collapsedList[$attribute['name']]['langs'][$attribute['lang']] = $attribute['value'];
899
            }
900
        }
901
        // once we have the final list, populate the respective "best-match"
902
        // language to choose for the ML attributes
903
904
        foreach ($collapsedList as $attribName => $valueArray) {
905
            if (isset($valueArray['langs'])) { // we have at least one language-dependent name in this attribute
906
                // for printed names on screen:
907
                // assign to exact match language, fallback to "default" language, fallback to English, fallback to whatever comes first in the list
908
                $collapsedList[$attribName][0] = $valueArray['langs'][$this->languageInstance->getLang()] ?? $valueArray['langs']['C'] ?? $valueArray['langs']['en'] ?? array_shift($valueArray['langs']);
909
                // for names usable in filesystems (closer to good old ASCII...)
910
                // prefer English, otherwise the "default" language, otherwise the same that we got above
911
                $collapsedList[$attribName][1] = $valueArray['langs']['en'] ?? $valueArray['langs']['C'] ?? $collapsedList[$attribName][0];
912
            }
913
        }
914
        return $collapsedList;
915
    }
916
    
917
    /**
918
     * Is the profile global redirection set?
919
     * 
920
     * @return bool
921
     */
922
    public function isRedirected() {
923
        $result = $this->databaseHandle->exec("SELECT profile_id FROM profile_option WHERE profile_id = ? AND option_name='device-specific:redirect' AND device_id IS NULL", "i", $this->identifier);
924
        if ($result->num_rows == 0) {
925
            return false;
926
        }
927
        return true;
928
    }
929
    
930
    /**
931
     * Does the profile contain enough information to generate installers with
932
     * it? Silverbullet will always return TRUE; RADIUS profiles need to do some
933
     * heavy lifting here.
934
     * 
935
     * @return int one of the constants above which tell if enough info is set to enable installers
936
     */
937
    public function readinessLevel()
938
    {
939
        $result = $this->databaseHandle->exec("SELECT sufficient_config, showtime FROM profile WHERE profile_id = ?", "i", $this->identifier);
940
        // SELECT queries always return a resource, not a boolean
941
        $configQuery = mysqli_fetch_row(/** @scrutinizer ignore-type */ $result);
942
        if ($configQuery[0] == "0") {
943
            return self::READINESS_LEVEL_NOTREADY;
944
        }
945
        // at least fully configured, if not showtime!
946
        if ($configQuery[1] == "0") {
947
            return self::READINESS_LEVEL_SUFFICIENTCONFIG;
948
        }
949
        return self::READINESS_LEVEL_SHOWTIME;
950
    }
951
952
    /**
953
     * Checks all profile certificates validity periods comparing to the pre-defined
954
     * thresholds and returns the most critical status.
955
     * 
956
     * @return int - one of constants defined in this profile
957
     */
958
    public function certificateStatus()
959
    {
960
        $query = "SELECT option_value AS cert FROM profile_option  WHERE option_name='eap:ca_file' AND profile_id = ?";        
961
        $result = $this->databaseHandle->exec($query, "i", $this->identifier);
962
        $rows = $result->fetch_all();
963
        $x509 = new \core\common\X509();
964
        $profileStatus = self::CERT_STATUS_NONE;
965
        foreach ($rows as $row) {
966
            $encodedCert = $row[0];
967
            $tm = $x509->processCertificate(base64_decode($encodedCert))['full_details']['validTo_time_t']- time();
968
            if ($tm < \config\ConfAssistant::CERT_WARNINGS['expiry_critical']) {
969
                $certStatus = self::CERT_STATUS_ERROR;
970
            } elseif ($tm < \config\ConfAssistant::CERT_WARNINGS['expiry_warning']) {
971
                $certStatus = self::CERT_STATUS_WARN;
972
            } else {
973
                $certStatus = self::CERT_STATUS_OK;
974
            }
975
            $profileStatus = max($profileStatus, $certStatus);
976
        }
977
        return $profileStatus;
978
    }
979
980
    /**
981
     * Checks if the profile has enough information to have something to show to end users. This does not necessarily mean
982
     * that there's a fully configured EAP type - it is sufficient if a redirect has been set for at least one device.
983
     * 
984
     * @return boolean TRUE if enough information for showtime is set; FALSE if not
985
     */
986
    private function readyForShowtime()
987
    {
988
        $properConfig = FALSE;
989
        $attribs = $this->getCollapsedAttributes();
990
        // do we have enough to go live? Check if any of the configured EAP methods is completely configured ...
991
        if (sizeof($this->getEapMethodsinOrderOfPreference(1)) > 0) {
992
            $properConfig = TRUE;
993
        }
994
        // if not, it could still be that general redirect has been set
995
        if (!$properConfig) {
996
            if (isset($attribs['device-specific:redirect'])) {
997
                $properConfig = TRUE;
998
            }
999
            // just a per-device redirect? would be good enough... but this is not actually possible:
1000
            // per-device redirects can only be set on the "fine-tuning" page, which is only accessible
1001
            // if at least one EAP type is fully configured - which is caught above and makes readyForShowtime TRUE already
1002
        }
1003
        // do we know at least one SSID to configure, or work with wired? If not, it's not ready...
1004
        if (!isset($attribs['media:SSID']) &&
1005
                (!isset(\config\ConfAssistant::CONSORTIUM['ssid']) || count(\config\ConfAssistant::CONSORTIUM['ssid']) == 0) &&
1006
                !isset($attribs['media:wired'])) {
1007
            $properConfig = FALSE;
1008
        }
1009
        // institutions without a name are not really a corner case we should support
1010
        if (!isset($attribs['general:instname'])) {
1011
            $properConfig = FALSE;
1012
        }
1013
        return $properConfig;
1014
    }
1015
1016
    /**
1017
     * set the showtime property if prepShowTime says that there is enough info *and* the admin flagged the profile for showing
1018
     * 
1019
     * @return void
1020
     */
1021
    public function prepShowtime()
1022
    {
1023
        $properConfig = $this->readyForShowtime();
1024
        $this->databaseHandle->exec("UPDATE profile SET sufficient_config = ".($properConfig ? "TRUE" : "FALSE")." WHERE profile_id = ".$this->identifier);
1025
1026
        $attribs = $this->getCollapsedAttributes();
1027
        // if not enough info to go live, set FALSE
1028
        // even if enough info is there, admin has the ultimate say: 
1029
        //   if he doesn't want to go live, no further checks are needed, set FALSE as well
1030
        if (!$properConfig || !isset($attribs['profile:production']) || (isset($attribs['profile:production']) && $attribs['profile:production'][0] != "on")) {
1031
            $this->databaseHandle->exec("UPDATE profile SET showtime = FALSE WHERE profile_id = ?", "i", $this->identifier);
1032
            return;
1033
        }
1034
        $this->databaseHandle->exec("UPDATE profile SET showtime = TRUE WHERE profile_id = ?", "i", $this->identifier);
1035
    }
1036
1037
    /**
1038
     * internal helper - some attributes are added by the constructor "ex officio"
1039
     * without actual input from the admin. We can streamline their addition in
1040
     * this function to avoid duplication.
1041
     * 
1042
     * @param array $internalAttributes - only names and value
1043
     * @return array full attributes with all properties set
1044
     */
1045
    protected function addInternalAttributes($internalAttributes)
1046
    {
1047
        // internal attributes share many attribute properties, so condense the generation
1048
        $retArray = [];
1049
        foreach ($internalAttributes as $attName => $attValue) {
1050
            $retArray[] = ["name" => $attName,
1051
                "lang" => NULL,
1052
                "value" => $attValue,
1053
                "level" => Options::LEVEL_PROFILE,
1054
                "row_id" => 0,
1055
                "flag" => NULL,
1056
            ];
1057
        }
1058
        return $retArray;
1059
    }
1060
1061
    /**
1062
     * Retrieves profile attributes stored in the database
1063
     * 
1064
     * @return array The attributes in one array
1065
     */
1066
    protected function addDatabaseAttributes()
1067
    {
1068
        $databaseAttributes = $this->retrieveOptionsFromDatabase("SELECT DISTINCT option_name, option_lang, option_value, row_id
1069
                FROM $this->entityOptionTable
1070
                WHERE $this->entityIdColumn = ?
1071
                AND device_id IS NULL AND eap_method_id = 0
1072
                ORDER BY option_name", "Profile");
1073
        return $databaseAttributes;
1074
    }
1075
}
1076