getServerLoad()   B
last analyzed

Complexity

Conditions 9
Paths 7

Size

Total Lines 54
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 24
c 1
b 0
f 0
nc 7
nop 0
dl 0
loc 54
rs 8.0555

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php require($_SERVER['DOCUMENT_ROOT'] . "/static/config.inc.php"); ?>
2
<?php require($_SERVER['DOCUMENT_ROOT'] . "/static/conn.php"); ?>
3
<?php require($_SERVER['DOCUMENT_ROOT'] . "/lib/profile.php"); ?>
4
<?php
5
session_start();
6
7
ini_set('display_errors', 1);
8
ini_set('display_startup_errors', 1);
9
error_reporting(E_ALL);
10
11
if(!isset($_SESSION['siteusername'])) {
12
    header("Location: ../index.php");
13
}
14
15
if(isAdmin($_SESSION['siteusername'], $conn) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
16
    die("Ur not admin");
17
} else {
18
    if(@$_POST['suspend']) {
19
        suspendUser($_POST['subject'], $conn);
20
        echo "Suces<br>";
21
    } else if(@$_POST['unsuspend']) {
22
        unsuspendUser($_POST['subject'], $conn);
23
        echo "Suces<br>";
24
    } else if(@$_POST['del']) {
25
        delPostsFromUser($_POST['subject'], $conn);
26
        echo "succes<br>";
27
    }
28
}
29
30
function _getServerLoadLinuxData()
31
{
32
    if (is_readable("/proc/stat"))
33
    {
34
        $stats = @file_get_contents("/proc/stat");
35
36
        if ($stats !== false)
37
        {
38
            // Remove double spaces to make it easier to extract values with explode()
39
            $stats = preg_replace("/[[:blank:]]+/", " ", $stats);
40
41
            // Separate lines
42
            $stats = str_replace(array("\r\n", "\n\r", "\r"), "\n", $stats);
43
            $stats = explode("\n", $stats);
44
45
            // Separate values and find line for main CPU load
46
            foreach ($stats as $statLine)
47
            {
48
                $statLineData = explode(" ", trim($statLine));
49
50
                // Found!
51
                if
52
                (
53
                    (count($statLineData) >= 5) &&
54
                    ($statLineData[0] == "cpu")
55
                )
56
                {
57
                    return array(
58
                        $statLineData[1],
59
                        $statLineData[2],
60
                        $statLineData[3],
61
                        $statLineData[4],
62
                    );
63
                }
64
            }
65
        }
66
    }
67
68
    return null;
69
}
70
71
// Returns server load in percent (just number, without percent sign)
72
function getServerLoad()
73
{
74
    $load = null;
75
76
    if (stristr(PHP_OS, "win"))
77
    {
78
        $cmd = "wmic cpu get loadpercentage /all";
79
        @exec($cmd, $output);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for exec(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

79
        /** @scrutinizer ignore-unhandled */ @exec($cmd, $output);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
80
81
        if ($output)
82
        {
83
            foreach ($output as $line)
84
            {
85
                if ($line && preg_match("/^[0-9]+\$/", $line))
86
                {
87
                    $load = $line;
88
                    break;
89
                }
90
            }
91
        }
92
    }
93
    else
94
    {
95
        if (is_readable("/proc/stat"))
96
        {
97
            // Collect 2 samples - each with 1 second period
98
            // See: https://de.wikipedia.org/wiki/Load#Der_Load_Average_auf_Unix-Systemen
99
            $statData1 = _getServerLoadLinuxData();
100
            sleep(1);
101
            $statData2 = _getServerLoadLinuxData();
102
103
            if
104
            (
105
                (!is_null($statData1)) &&
106
                (!is_null($statData2))
107
            )
108
            {
109
                // Get difference
110
                $statData2[0] -= $statData1[0];
111
                $statData2[1] -= $statData1[1];
112
                $statData2[2] -= $statData1[2];
113
                $statData2[3] -= $statData1[3];
114
115
                // Sum up the 4 values for User, Nice, System and Idle and calculate
116
                // the percentage of idle time (which is part of the 4 values!)
117
                $cpuTime = $statData2[0] + $statData2[1] + $statData2[2] + $statData2[3];
118
119
                // Invert percentage to get CPU time, not idle time
120
                $load = 100 - ($statData2[3] * 100 / $cpuTime);
121
            }
122
        }
123
    }
124
125
    return $load;
126
}
127
?>
128
<!DOCTYPE html>
129
<html>
130
<head>
131
    <link href="/static/css/required.css" rel="stylesheet">
132
    <title>Witter: What are you doing?</title>
133
</head>
134
<body id="front">
135
<div id="container">
136
    <?php require($_SERVER['DOCUMENT_ROOT'] . "/static/header.php"); ?>
137
    <div id="content">
138
        <div class="wrapper">
139
            <form method="post" enctype="multipart/form-data" id="submitform">
140
                <b>Suspend User</b>
141
                <br><input placeholder="Subject" type="text" name="subject" required="required" size="63"></b><br>
142
                <input type="submit" name="suspend" value="Suspend">
143
            </form><br>
144
145
            <form method="post" enctype="multipart/form-data" id="submitform">
146
                <b>Unsuspend User</b>
147
                <br><input placeholder="Subject" type="text" name="subject" required="required" size="63"></b><br>
148
                <input type="submit" name="unsuspend" value="Unsuspend">
149
            </form><br>
150
151
            <form method="post" enctype="multipart/form-data" id="submitform">
152
                <b>Delete Posts from User</b>
153
                <br><input placeholder="Subject" type="text" name="subject" required="required" size="63"></b><br>
154
                <input type="submit" name="del" value="Delete">
155
            </form><br>
156
            <?php require($_SERVER['DOCUMENT_ROOT'] . "/static/footer.php"); ?>
157
        </div>
158
    </div>
159
</div>
160
</body>
161
</html>