Test Setup Failed
Push — master ( 5548b8...63dc2d )
by Stefan
16:44
created

UserNetAPI::jsonListProfiles()   B

Complexity

Conditions 7
Paths 33

Size

Total Lines 31
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 31
rs 8.6026
c 0
b 0
f 0
cc 7
nc 33
nop 2
1
<?php
2
/*
3
 * *****************************************************************************
4
 * Contributions to this work were made on behalf of the GÉANT project, a 
5
 * project that has received funding from the European Union’s Framework 
6
 * Programme 7 under Grant Agreements No. 238875 (GN3) and No. 605243 (GN3plus),
7
 * Horizon 2020 research and innovation programme under Grant Agreements No. 
8
 * 691567 (GN4-1) and No. 731122 (GN4-2).
9
 * On behalf of the aforementioned projects, GEANT Association is the sole owner
10
 * of the copyright in all material which was developed by a member of the GÉANT
11
 * project. GÉANT Vereniging (Association) is registered with the Chamber of 
12
 * Commerce in Amsterdam with registration number 40535155 and operates in the 
13
 * UK as a branch of GÉANT Vereniging.
14
 * 
15
 * Registered office: Hoekenrode 3, 1102BR Amsterdam, The Netherlands. 
16
 * UK branch address: City House, 126-130 Hills Road, Cambridge CB2 1PQ, UK
17
 *
18
 * License: see the web/copyright.inc.php file in the file structure or
19
 *          <base_url>/copyright.php after deploying the software
20
 */
21
22
/**
23
 * This is the collection of methods dedicated for the user GUI
24
 * @author Tomasz Wolniewicz <[email protected]>
25
 * @author Stefan Winter <[email protected]>
26
 * @package UserAPI
27
 *
28
 * Parts of this code are based on simpleSAMLPhp discojuice module.
29
 * This product includes GeoLite data created by MaxMind, available from
30
 * http://www.maxmind.com
31
 */
32
33
namespace core;
34
use \Exception;
35
36
/**
37
 * This class collect methods used for comminication via network UserAPI
38
 * The methods are generally wrappers around more general UserAPI ones
39
 */
40
41
class UserNetAPI extends UserAPI {
42
43
    /**
44
     * nothing special to be done here.
45
     */
46
    public function __construct() {
47
        parent::__construct();
48
    }
49
    
50
    /**
51
     *  wrapper JSON function
52
     * 
53
     * @param array|bool|null $data the core data to be converted to JSON
54
     * @param int $status extra status information, defaults to 1
55
     * @return string JSON encoded data
56
     */
57
    public function returnJSON($data, $status = 1, $otherData = []) {
58
        $validator = new \web\lib\common\InputValidation();
59
        $host = $validator->hostname($_SERVER['SERVER_NAME']);
60
        if ($host === FALSE) {
61
            throw new \Exception("We don't know our own hostname?!? Giving up.");
62
        }
63
        $returnArray = [];
64
        $returnArray['status'] = $status;
65
        $returnArray['data'] = $data;
66
        $returnArray['tou'] = "Please consult Terms of Use at: //" . $host . \core\CAT::getRootUrlPath() . "/tou.php";
67
        if (!empty($otherData)) {
68
            $returnArray['otherdata'] = $otherData;
69
        }
70
        return(json_encode($returnArray));
71
    }
72
73
    /**
74
     * outputs the list of supported languages.
75
     */
76
    public function jsonListLanguages() {
77
        $returnArray = [];
78
        foreach (CONFIG['LANGUAGES'] as $id => $val) {
79
            $returnArray[] = ['lang' => $id, 'display' => $val['display'], 'locale' => $val['locale']];
80
        }
81
        echo $this->returnJSON($returnArray);
82
    }
83
84
    /**
85
     * outputs the list of countries with configured IdPs
86
     *
87
     */
88
    public function jsonListCountries() {
89
        $federations = $this->printCountryList(1);
90
        $returnArray = [];
91
        foreach ($federations as $id => $val) {
92
            $returnArray[] = ['federation' => $id, 'display' => $val];
93
        }
94
        echo $this->returnJSON($returnArray);
95
    }
96
97
    /**
98
     * outputs the list of IdPs in a given country
99
     *
100
     * @param string $country the country we are interested in
101
     */
102
    public function jsonListIdentityProviders($country) {
103
        $idps = $this->listAllIdentityProviders(1, $country);
104
        $returnArray = [];
105
        foreach ($idps as $idp) {
106
            $returnArray[] = ['idp' => $idp['entityID'], 'id' => $idp['entityID'], 'display' => $idp['title']];
107
        }
108
        echo $this->returnJSON($returnArray);
109
    }
110
111
    /**
112
     * outputs the list of all active IdPs
113
     *
114
     * The IdP list is formatted for DiscoJuice consumption
115
     */
116
    public function jsonListIdentityProvidersForDisco() {
117
        $idps = $this->listAllIdentityProviders(1);
118
        $returnArray = [];
119
        foreach ($idps as $idp) {
120
            $idp['idp'] = $idp['entityID'];
121
            $idp['id'] = $idp['entityID'];
122
            $returnArray[] = $idp;
123
        }
124
        echo json_encode($returnArray);
125
    }
126
127
    /**
128
     * outputs the list of IdPs in a given country ordered with respect to their distance to the user's location
129
     * 
130
     * @param string $country the country in question
131
     * @param array $location the coordinates of the approximate user location
132
     *
133
     */
134
    public function jsonOrderIdentityProviders($country, $location) {
135
        $idps = $this->orderIdentityProviders($country, $location);
136
        $returnArray = [];
137
        foreach ($idps as $idp) {
138
            $returnArray[] = ['idp' => $idp['id'], 'display' => $idp['title']];
139
        }
140
        echo $this->returnJSON($returnArray);
141
    }
142
143
    /**
144
     * outputs a list of profiles available for a given IdP
145
     *
146
     * @param int $idpIdentifier the IdP identifier
147
     * @param int $sort          should the result set be sorted? 0 = no, 1 = yes
148
     */
149
    public function jsonListProfiles($idpIdentifier, $sort = 0) {
150
        $returnArray = [];
151
        try {
152
            $idp = new IdP($idpIdentifier);
153
        } catch (\Exception $fail) {
154
            echo $this->returnJSON($returnArray, 0);
155
            return;
156
        }
157
        $hasLogo = 0;
158
        $logo = $idp->getAttributes('general:logo_file');
159
        if (count($logo) > 0) {
160
            $hasLogo = 1;
161
        }
162
        $fed = new Federation($idp->federation);
163
        $fedUrl = $idp->languageInstance->getLocalisedValue($fed->getAttributes('fed:url'));
164
        $fedName = $idp->languageInstance->getLocalisedValue($fed->getAttributes('fed:realname'));
165
        $otherData = [];
166
        if (!empty($fedUrl)) {
167
            $otherData['fedurl'] = $fedUrl;
168
        }
169
        if (!empty($fedName)) {
170
            $otherData['fedname'] = $fedName;
171
        }
172
        $profiles = $idp->listProfiles(TRUE);
173
        if ($sort == 1) {
174
            usort($profiles, ["UserAPI", "profileSort"]);
175
        }
176
        foreach ($profiles as $profile) {
177
            $returnArray[] = ['profile' => $profile->identifier, 'id'=>$profile->identifier, 'display' => $profile->name, 'idp_name' => $profile->instName, 'logo' => $hasLogo];
178
        }
179
        echo $this->returnJSON($returnArray, 1, $otherData);
180
    }
181
182
    /**
183
     * outputs the list of devices available for the given profile
184
     *
185
     * @param int $profileId the Profile identifier
186
     */
187
    public function jsonListDevices($profileId) {
188
        $returnArray = [];
189
        $profileAttributes = $this->profileAttributes($profileId);
190
        $thedevices = $profileAttributes['devices'];
191
        foreach ($thedevices as $D) {
192
            if (\core\common\Entity::getAttributeValue($D, 'options', 'hidden') === 1) {
193
                continue;
194
            }
195
            if ($D['device'] === '0') {
196
                $disp = '';
197
            } else {
198
                $disp = $D['display'];
199
            }
200
            $returnArray[] = ['device' => $D['id'], 'display' => $disp, 'status' => $D['status'], 'redirect' => $D['redirect']];
201
        }
202
        echo $this->returnJSON($returnArray);
203
    }
204
205
    /**
206
     * outputs the link to the installers (additionally, actually generates it or takes it from cache)
207
     *
208
     * @param string $device  identifier as in {@link devices.php}
209
     * @param int    $prof_id profile identifier
210
     */
211
    public function jsonGenerateInstaller($device, $prof_id) {
212
        $this->loggerInstance->debug(4, "JSON::generateInstaller arguments: $device,$prof_id\n");
213
        $output = $this->generateInstaller($device, $prof_id);
214
        $this->loggerInstance->debug(4, "output from GUI::generateInstaller:");
215
        $this->loggerInstance->debug(4, print_r($output, true));
216
        $this->loggerInstance->debug(4, json_encode($output));
217
//    header('Content-type: application/json; utf-8');
218
        echo $this->returnJSON($output);
219
    }
220
221
    /**
222
     * outputs OS guess in JSON
223
     */
224
    public function jsonDetectOS() {
225
        $returnArray = $this->detectOS();
226
        $status = is_array($returnArray) ? 1 : 0;
1 ignored issue
show
introduced by
The condition is_array($returnArray) is always true.
Loading history...
227
        echo $this->returnJSON($returnArray, $status);
228
    }
229
    
230
    /**
231
     * outputs user certificates pertaining to a given token in JSON
232
     * @param string $token
233
     */
234
    public function jsonGetUserCerts($token) {
235
        $returnArrayE = $this->getUserCerts($token);
236
        $returnArray = [];
237
        $status = is_array($returnArrayE) ? 1 : 0;
1 ignored issue
show
introduced by
The condition is_array($returnArrayE) is always true.
Loading history...
238
        if ($status === 1) {
239
            foreach ($returnArrayE as $element) {
240
                $returnArray[] = $element->getBasicInfo();
241
            }
242
        }
243
        echo $this->returnJSON($returnArray, $status);
244
    }
245
    
246
    /** outputs the user location as JSON
247
     * @throws Exception
248
     */
249
    public function jsonLocateUser() {
250
        header('Content-type: application/json; utf-8');
251
        echo json_encode($this->locateDevice());
252
    }
253
254
    /**
255
     * outputs support data prepared within {@link GUI::profileAttributes()}
256
     */
257
    public function jsonProfileAttributes($prof_id) {
258
//    header('Content-type: application/json; utf-8');
259
        echo $this->returnJSON($this->profileAttributes($prof_id));
260
    }
261
262
    /**
263
     * outputs a logo
264
     * 
265
     * @param int|string $identifier
266
     * @param string $type "federation" or "idp"
267
     * @param int $width
268
     * @param int $height
269
     */
270
    public function sendLogo($identifier, $type, $width = 0, $height = 0) {
271
        $logo = $this->getLogo($identifier, $type, $width, $height);
272
        $blob = $logo === NULL ? file_get_contents(ROOT . '/web/resources/images/empty.png') : $logo['blob'];
273
        header("Content-type: " . $logo['filetype']);
274
        header("Cache-Control:max-age=36000, must-revalidate");
275
        header($logo['expires']);
276
        echo $blob;
277
    }
278
    
279
}
280