Test Setup Failed
Push — release_2_0 ( c5cba9...f39600 )
by Stefan
11:39
created

getRequest()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 14
rs 9.8666
cc 3
nc 3
nop 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 51 and the first side effect is on line 27.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
 * AJAX backend for the user GUI
24
 *
25
 * @package UserAPI
26
 */
27
include(dirname(dirname(dirname(__FILE__))) . "/config/_config.php");
0 ignored issues
show
Coding Style introduced by
"include" is a statement not a function; no parentheses are required
Loading history...
Coding Style introduced by
File is being unconditionally included; use "require" instead
Loading history...
28
$API = new \core\UserNetAPI();
29
$validator = new web\lib\common\InputValidation();
30
$loggerInstance = new \core\common\Logging();
31
32
const LISTOFACTIONS = [
33
    'listLanguages',
34
    'listCountries',
35
    'listIdentityProviders',
36
    'listAllIdentityProviders',
37
    'listProfiles', // needs $idp set - abort if not
38
    'listDevices',
39
    'generateInstaller', // needs $device and $profile set
40
    'downloadInstaller', // needs $device and $profile set optional $generatedfor
41
    'profileAttributes', // needs $profile set
42
    'sendLogo', // needs $idp and $disco set
43
    'sendFedLogo', // needs $federation
44
    'deviceInfo', // needs $device and profile set
45
    'locateUser',
46
    'detectOS',
47
    'orderIdentityProviders',
48
    'getUserCerts',
49
];
50
51
function getRequest($varName, $filter) {
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
52
    $safeText = ["options"=>["regexp"=>"/^[\w\d-]+$/"]];
53
    switch ($filter) {
54
        case 'safe_text':
55
            $out = filter_input(INPUT_GET, $varName, FILTER_VALIDATE_REGEXP, $safeText) ?? filter_input(INPUT_POST, $varName, FILTER_VALIDATE_REGEXP, $safeText);
56
            break;
57
        case 'int':
58
            $out = filter_input(INPUT_GET, $varName, FILTER_VALIDATE_INT) ?? filter_input(INPUT_POST, $varName, FILTER_VALIDATE_INT);
59
            break;
60
        default:
61
            $out = NULL;
62
            break;
63
    }
64
    return $out;
65
}
66
67
// make sure this is a known action
68
$action = getRequest('action', 'safe_text');
69
if (!in_array($action, LISTOFACTIONS)) {
70
    throw new Exception("Unknown action used.");
71
}
72
73
$langR = getRequest('lang', 'safe_text');
74
$lang = $langR ? $validator->supportedLanguage($langR) : FALSE;
75
$deviceR = getRequest('device', 'safe_text');
76
$device = $deviceR ? $validator->Device($deviceR) : FALSE;
77
$idpR = getRequest('idp', 'int');
78
$idp = $idpR ? $validator->IdP($idpR)->identifier : FALSE;
79
$profileR = getRequest('profile', 'int');
80
$profile = $profileR ? $validator->Profile($profileR)->identifier : FALSE;
81
$federationR = getRequest('federation', 'safe_text');
82
$federation = $federationR ? $validator->Federation($federationR)->tld : FALSE;
83
$disco = getRequest('disco', 'int');
84
$width = getRequest('width', 'int') ?? 0;
85
$height = getRequest('height', 'int') ?? 0;
86
$sort = getRequest('sort', 'int') ?? 0;
87
$generatedfor = getRequest('generatedfor', 'safe_text') ?? 'user';
88
$token = getRequest('token', 'safe_text');
89
$idR = getRequest('id', 'safe_text');
90
$id = $idR ? $idR : FALSE;
91
92
switch ($action) {
93
    case 'listLanguages':
94
        $API->JSON_listLanguages();
95
        break;
96
    case 'listCountries':
97
        $API->JSON_listCountries();
98
        break;
99
    case 'listIdentityProviders':
100
        if ($federation === FALSE) {
101
           $federation = $id ? $validator->Federation($id)->tld : FALSE;
102
        }
103
        if ($federation === FALSE) { // federation is a mandatory parameter!
104
            exit;
105
        }
106
        $API->JSON_listIdentityProviders($federation);
107
        break;
108
    case 'listAllIdentityProviders':
109
        $API->JSON_listIdentityProvidersForDisco();
110
        break;
111
    case 'listProfiles': // needs $idp set - abort if not
112
        if ($idp === FALSE) {
113
           $idp = $id ? $validator->IdP($id)->identifier : FALSE;
114
        }
115
        if ($idp === FALSE) {
116
            exit;
117
        }
118
        $API->JSON_listProfiles($idp, $sort);
119
        break;
120
    case 'listDevices':
121
        if ($profile === FALSE) {
122
           $profile = $id ? $validator->Profile($id)->identifier : FALSE;
123
        }
124
        if ($profile === FALSE) {
125
            exit;
126
        }
127
        $API->JSON_listDevices($profile);
128
        break;
129
    case 'generateInstaller': // needs $device and $profile set
130
        if ($device === FALSE) {
131
            $device = $id;
132
        }
133
        if ($device === FALSE || $profile === FALSE) {
134
            exit;
135
        }
136
        $API->JSON_generateInstaller($device, $profile);
137
        break;
138
    case 'downloadInstaller': // needs $device and $profile set optional $generatedfor
139
        if ($device === FALSE) {
140
            $device = $id;
141
        }
142
        if ($device === FALSE || $profile === FALSE) {
143
            exit;
144
        }
145
        $API->downloadInstaller($device, $profile, $generatedfor);
146
        break;
147
    case 'profileAttributes': // needs $profile set
148
        if ($profile === FALSE) {
149
           $profile = $id ? $validator->Profile($id)->identifier : FALSE;
150
        }
151
        if ($profile === FALSE) {
152
            exit;
153
        }
154
        $API->JSON_profileAttributes($profile);
155
        break;
156
    case 'sendLogo': // needs $idp and $disco set
157
        if ($idp === FALSE) {
158
           $idp = $id ? $validator->IdP($id)->identifier : FALSE;
159
        }
160
        if ($idp === FALSE) {
161
            exit;
162
        }
163
        if ($disco == 1) {
164
            $width = 120;
165
            $height = 40;
166
        }
167
        $API->sendLogo($idp, "idp", $width, $height);
168
        break;
169
    case 'sendFedLogo': // needs $federation
170
        if ($federation === FALSE) {
171
            if ($idp === FALSE) {
172
            exit;
173
        }
174
            $API->sendLogo($idp, "federation_from_idp", $width, $height);
175
        } else {
176
            $API->sendLogo($federation, "federation", $width, $height);
177
        }
178
        break;        
179
    case 'deviceInfo': // needsdevice and profile set
180
        if ($device === FALSE) {
181
            $device = $id;
182
        }
183
        if ($device === FALSE || $profile === FALSE) {
184
            exit;
185
        }
186
        $API->deviceInfo($device, $profile);
187
        break;
188
    case 'locateUser':
189
        $API->JSON_locateUser();
190
        break;
191
    case 'detectOS':
192
        $API->JSON_detectOS();
193
        break;
194
    case 'orderIdentityProviders':
195
        $coordinateArray = NULL;
196
        if ($location) {
197
            $coordinateArrayRaw = explode(':', $location);
198
            $coordinateArray = ['lat' => $coordinateArrayRaw[0], 'lon' => $coordinateArrayRaw[1]];
199
        }
200
        if ($federation === FALSE) { // is this parameter mandatory? The entire API call is not mentioned in UserAPI.md documentation currently
201
            $federation = "";
202
        }
203
        $API->JSON_orderIdentityProviders($federation, $coordinateArray);
204
        break;
205
    case 'getUserCerts':
206
        $API->JSON_getUserCerts($token);
207
        break;
208
}
209
210
// $loggerInstance->debug(4, "UserAPI action: " . $action . ':' . $lang !== FALSE ? $lang : '' . ':' . $profile . ':' . $device . "\n");
211