Passed
Push — master ( 9948f7...7383fc )
by Tomasz
17:33
created

UserNetAPI::JSON_detectOS()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * ******************************************************************************
5
 * Copyright 2011-2017 DANTE Ltd. and GÉANT on behalf of the GN3, GN3+, GN4-1 
6
 * and GN4-2 consortia
7
 *
8
 * License: see the web/copyright.php file in the file structure
9
 * ******************************************************************************
10
 */
11
12
/**
13
 * This is the collection of methods dedicated for the user GUI
14
 * @author Tomasz Wolniewicz <[email protected]>
15
 * @author Stefan Winter <[email protected]>
16
 * @package UserAPI
17
 *
18
 * Parts of this code are based on simpleSAMLPhp discojuice module.
19
 * This product includes GeoLite data created by MaxMind, available from
20
 * http://www.maxmind.com
21
 */
22
23
namespace core;
24
use \Exception;
25
26
/**
27
 * This class collect methods used for comminication via network UserAPI
28
 * The methods are generally wrappers around more general UserAPI ones
29
 */
30
31
class UserNetAPI extends UserAPI {
32
33
    /**
34
     * nothing special to be done here.
35
     */
36
    public function __construct() {
37
        parent::__construct();
38
    }
39
    
40
    /**
41
     *  wrapper JSON function
42
     * 
43
     * @param array|bool|null $data the core data to be converted to JSON
44
     * @param int $status extra status information, defaults to 1
45
     * @return string JSON encoded data
46
     */
47
    public function returnJSON($data, $status = 1) {
48
        $returnArray = [];
49
        $returnArray['status'] = $status;
50
        $returnArray['data'] = $data;
51
        $returnArray['tou'] = "Please consult Terms of Use at: " . $this->getRootURL() . "/tou.php";
52
        return(json_encode($returnArray));
53
    }
54
55
    /**
56
     * outputs the list of supported languages.
57
     */
58
    public function JSON_listLanguages() {
59
        $returnArray = [];
60
        foreach (CONFIG['LANGUAGES'] as $id => $val) {
61
            $returnArray[] = ['lang' => $id, 'display' => $val['display'], 'locale' => $val['locale']];
62
        }
63
        echo $this->returnJSON($returnArray);
64
    }
65
66
    /**
67
     * outputs the list of countries with configured IdPs
68
     *
69
     */
70
    public function JSON_listCountries() {
71
        $federations = $this->printCountryList(1);
72
        $returnArray = [];
73
        foreach ($federations as $id => $val) {
74
            $returnArray[] = ['federation' => $id, 'display' => $val];
75
        }
76
        echo $this->returnJSON($returnArray);
77
    }
78
79
    /**
80
     * outputs the list of IdPs in a given country
81
     *
82
     * @param string $country the country we are interested in
83
     */
84
    public function JSON_listIdentityProviders($country) {
85
        $idps = $this->listAllIdentityProviders(1, $country);
86
        $returnArray = [];
87 View Code Duplication
        foreach ($idps as $idp) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
            $returnArray[] = ['idp' => $idp['entityID'], 'display' => $idp['title']];
89
        }
90
        echo $this->returnJSON($returnArray);
91
    }
92
93
    /**
94
     * outputs the list of all active IdPs
95
     *
96
     * The IdP list is formatted for DiscoJuice consumption
97
     */
98
    public function JSON_listIdentityProvidersForDisco() {
99
        $idps = $this->listAllIdentityProviders(1);
100
        $returnArray = [];
101
        foreach ($idps as $idp) {
102
            $idp['idp'] = $idp['entityID'];
103
            $returnArray[] = $idp;
104
        }
105
        echo json_encode($returnArray);
106
    }
107
108
    /**
109
     * outputs the list of IdPs in a given country ordered with respect to their distance to the user's location
110
     * 
111
     * @param string $country the country in question
112
     * @param array $location the coordinates of the approximate user location
113
     *
114
     */
115
    public function JSON_orderIdentityProviders($country, $location = NULL) {
116
        $idps = $this->orderIdentityProviders($country, $location);
117
        $returnArray = [];
118 View Code Duplication
        foreach ($idps as $idp) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
            $returnArray[] = ['idp' => $idp['id'], 'display' => $idp['title']];
120
        }
121
        echo $this->returnJSON($returnArray);
122
    }
123
124
    /**
125
     * outputs a list of profiles available for a given IdP
126
     *
127
     * @param int $idpIdentifier the IdP identifier
128
     * @param int $sort should the result set be sorted? 0 = no, 1 = yes
129
     */
130
    public function JSON_listProfiles($idpIdentifier, $sort = 0) {
131
        $this->languageInstance->setTextDomain("web_user");
132
        $returnArray = [];
133
        try {
134
            $idp = new IdP($idpIdentifier);
135
        } catch (\Exception $fail) {
136
            echo $this->returnJSON($returnArray, 0);
137
            return;
138
        }
139
        $hasLogo = FALSE;
140
        $logo = $idp->getAttributes('general:logo_file');
141
        if (count($logo) > 0) {
142
            $hasLogo = 1;
143
        }
144
        $profiles = $idp->listProfiles(TRUE);
145
        if ($sort == 1) {
146
            usort($profiles, ["UserAPI", "profileSort"]);
147
        }
148
        foreach ($profiles as $profile) {
149
            $returnArray[] = ['profile' => $profile->identifier, 'display' => $profile->name, 'idp_name' => $profile->instName, 'logo' => $hasLogo];
150
        }
151
        echo $this->returnJSON($returnArray);
152
    }
153
154
    /**
155
     * outputs the list of devices available for the given profile
156
     *
157
     * @param int $profileId the Profile identifier
158
     */
159
    public function JSON_listDevices($profileId) {
160
        $this->languageInstance->setTextDomain("web_user");
161
        $returnArray = [];
162
        $profileAttributes = $this->profileAttributes($profileId);
163
        $thedevices = $profileAttributes['devices'];
164
        foreach ($thedevices as $D) {
165
            if (isset($D['options']) && isset($D['options']['hidden']) && $D['options']['hidden']) {
166
                continue;
167
            }
168
            if ($D['device'] === '0') {
169
                $disp = '';
170
            } else {
171
                $disp = $D['display'];
172
            }
173
            $returnArray[] = ['device' => $D['id'], 'display' => $disp, 'status' => $D['status'], 'redirect' => $D['redirect']];
174
        }
175
        echo $this->returnJSON($returnArray);
176
    }
177
178
    /**
179
     * outputs the link to the installers (additionally, actually generates it or takes it from cache)
180
     *
181
     * @param string $device identifier as in {@link devices.php}
182
     * @param int $prof_id profile identifier
183
     */
184
    public function JSON_generateInstaller($device, $prof_id) {
185
        $this->loggerInstance->debug(4, "JSON::generateInstaller arguments: $device,$prof_id\n");
186
        $output = $this->generateInstaller($device, $prof_id);
187
        $this->loggerInstance->debug(4, "output from GUI::generateInstaller:");
188
        $this->loggerInstance->debug(4, print_r($output, true));
189
        $this->loggerInstance->debug(4, json_encode($output));
190
//    header('Content-type: application/json; utf-8');
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
191
        echo $this->returnJSON($output);
192
    }
193
194
    /**
195
     * outputs OS guess in JSON
196
     */
197
    public function JSON_detectOS() {
198
        $returnArray = $this->detectOS();
199
        $status = is_array($returnArray) ? 1 : 0;
200
        echo $this->returnJSON($returnArray, $status);
201
    }
202
    
203
    /**
204
     * outputs user certificates pertaining to a given token in JSON
205
     * @param string $token
206
     */
207
    public function JSON_getUserCerts($token) {
208
        $returnArray = $this->getUserCerts($token);
209
        $status = is_array($returnArray) ? 1 : 0;
210
        echo $this->returnJSON($returnArray, $status);
211
    }
212
    
213
    /** outputs the user location as JSON
214
     * @throws Exception
215
     */
216
    public function JSON_locateUser() {
217
        header('Content-type: application/json; utf-8');
218
        echo json_encode($this->locateUser());
219
    }
220
221
    /**
222
     * outputs support data prepared within {@link GUI::profileAttributes()}
223
     */
224
    public function JSON_profileAttributes($prof_id) {
225
//    header('Content-type: application/json; utf-8');
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
226
        echo $this->returnJSON($this->profileAttributes($prof_id));
227
    }
228
229
    /**
230
     * outputs a logo
231
     * 
232
     * @param string|int $identifier
233
     * @param string $type "federation" or "idp"
234
     * @param int $width
235
     * @param int $height
236
     */
237 View Code Duplication
    public function sendLogo($identifier, $type, $width = 0, $height = 0) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
238
        $logo = $this->getLogo($identifier, $type, $width, $height);
239
        header("Content-type: " . $logo['filetype']);
240
        header("Cache-Control:max-age=36000, must-revalidate");
241
        header($logo['expires']);
242
        echo $logo['blob'];
243
    }
244
    
245
}