Passed
Push — master ( 13c09d...15db62 )
by Maja
08:21
created

checkConfigRADIUSDaemon()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 15
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
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 9 and the first side effect is on line 2.

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
require_once dirname(dirname(__FILE__)) . "/config/_config.php";
3
/**
4
    * check if URL responds with 200
5
    *
6
    * @param string $srv server name
7
    * @return integer or NULL
8
*/
9
function checkConfigRADIUSDaemon ($srv) {
10
    $ch = curl_init();
11
    if ($ch === FALSE) {
12
        return NULL;
13
    }
14
    $timeout = 10;
15
    curl_setopt ( $ch, CURLOPT_URL, $srv );
16
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
17
    curl_setopt ( $ch, CURLOPT_TIMEOUT, $timeout );
18
    curl_exec($ch);
19
    $http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
20
    if ($http_code == 200) {
21
        return 1;
22
    }
23
    return 0;
24
}
25
26
$dbLink = \core\DBConnection::handle("INST");
27
$allProblems = $dbLink->exec("SELECT deployment_id, inst_id, status, radius_status_1, radius_status_2, radius_instance_1, radius_instance_2 from deployment where radius_status_1=2 or radius_status_2=2");
28
if (!$allProblems) {
29
    exit;
30
}
31
$brokenDeployments = array();
32
while ($problemRow = mysqli_fetch_object(/** @scrutinizer ignore-type */ $allProblems)) {
33
    foreach (array(1, 2) as $id) {
34
        $fld_s = "radius_status_$id";
35
        $fld_i = "radius_instance_$id";
36
        if ($problemRow->$fld_s == 2) {
37
            if (!isset($brokenDeployments[$problemRow->$fld_i])) {
38
                $brokenDeployments[$problemRow->$fld_i] = array();
39
            }
40
            $brokenDeployments[$problemRow->$fld_i][$problemRow->inst_id] = $problemRow->deployment_id;
41
        }
42
    }
43
}
44
if (empty($brokenDeployments)) {
45
    exit;
46
}
47
$allServers = $dbLink->exec("SELECT server_id, mgmt_hostname from managed_sp_servers");
48
$radiusSites = array();
49
$siteStatus = array();
50
while ($siteRow = mysqli_fetch_object($allServers)) {
0 ignored issues
show
Bug introduced by
It seems like $allServers can also be of type true; however, parameter $result of mysqli_fetch_object() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
while ($siteRow = mysqli_fetch_object(/** @scrutinizer ignore-type */ $allServers)) {
Loading history...
51
    $radiusSite[$siteRow->server_id] = $siteRow->mgmt_hostname;
52
}
53
$siteStatus = array();
54
foreach (array_keys($brokenDeployments) as $server_id) {
55
    print "check $server_id " . $radiusSite[$server_id] . "\n";
56
    $siteStatus[$server_id]  = checkConfigRADIUSDaemon('http://' . $radiusSite[$server_id]);
57
    if ($siteStatus[$server_id]) {
58
        echo "\ncheck radius\n";
59
        echo \config\Diagnostics::RADIUSSPTEST['port']."\n";
60
        $statusServer = new \core\diag\RFC5997Tests($radiusSite[$server_id], \config\Diagnostics::RADIUSSPTEST['port'], \config\Diagnostics::RADIUSSPTEST['secret']);
61
        if ($statusServer->statusServerCheck() === \core\diag\AbstractTest::RETVAL_OK) {
62
            $siteStatus[$server_id] = 1;
63
        } else {
64
            $siteStatus[$server_id] = 0;
65
        }
66
    }
67
} 
68
if (!in_array(1, array_values($siteStatus))) { 
69
    exit;
70
}
71
foreach (array_keys($brokenDeployments) as $server_id) {
72
    if (isset($siteStatus[$server_id]) && $siteStatus[$server_id]) {
73
        foreach ($brokenDeployments[$server_id] as $inst_id => $deployment_id) {            
74
            $idp = new \core\IdP($inst_id);
75
            $hotspotProfiles = $idp->listDeployments();
76
            $deployment = $hotspotProfiles[0];
77
            $idx = 1;
78
            if ($deployment->radius_instance_2 == $server_id) {
79
                $idx = 2;
80
            }
81
            echo "\nfix $deployment_id of $inst_id on server $server_id index $idx\n";
82
            /** @scrutinizer ignore-call */
83
            $response = $deployment->setRADIUSconfig(($deployment->status == \core\AbstractDeployment::INACTIVE)? 1 : 0, $idx, 1);
84
            if (isset($response["res[$idx]"]) && $response["res[$idx]"] = 'OK') {
85
                echo "FIXED\n";
86
            }
87
        }
88
    }
89
}
90
91