Skip to content

Commit 264e61a

Browse files
authored
Update Pihole.php
1 parent 7258d7d commit 264e61a

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

Pihole/Pihole.php

+30-7
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ public function test()
2929
if ($test["valid"]) {
3030
echo "Successfully communicated with the API";
3131
} else {
32-
echo "Error while communicating with the API";
32+
echo "Error while communicating with the API: " . $test["message"];
3333
}
3434
}
3535
}
3636
public function livestats()
3737
{
38+
$data = [];
39+
$status = "inactive";
3840
$version = $this->config->version;
3941

4042
if ($version == 5) {
@@ -56,10 +58,12 @@ public function livestats()
5658
if ($version == 6) {
5759
$results = $this->getInfo();
5860

59-
$data["ads_blocked"] = $results["queries"];
60-
$data["ads_percentage"] = $results["percent"];
61+
if ($results["valid"]) {
62+
$data["ads_blocked"] = $results["queries"];
63+
$data["ads_percentage"] = $results["percent"];
6164

62-
$status = "active";
65+
$status = "active";
66+
}
6367
}
6468
return parent::getLiveStats($status, $data);
6569
}
@@ -106,10 +110,23 @@ public function getInfo()
106110
];
107111
}
108112

109-
// Create session and retreave data
113+
// Create session and retrieve data
110114
$response = parent::execute($this->url("api/auth"), $attrs, null, "POST");
111115
$auth = json_decode($response->getBody());
112116

117+
if (!$auth->session->valid) {
118+
119+
$data = [
120+
'valid' => false,
121+
'validity' => -1,
122+
'message' => $auth->session->message,
123+
'queries' => 0,
124+
'percent' => 0
125+
];
126+
127+
return $data;
128+
}
129+
113130
if ($ignoreTls) {
114131
$attrsid = [
115132
"body" => json_encode(['sid' => $auth->session->sid]),
@@ -142,8 +159,14 @@ public function getInfo()
142159
$valid = $auth->session->valid;
143160
$validity = $auth->session->validity;
144161
$message = $auth->session->message;
145-
$queriesblocked = $datasummary->queries->blocked;
146-
$percentblocked = round($datasummary->queries->percent_blocked, 2);
162+
163+
if (!$auth->session->valid) {
164+
$queriesblocked = 0;
165+
$percentblocked = 0;
166+
} else {
167+
$queriesblocked = $datasummary->queries->blocked;
168+
$percentblocked = round($datasummary->queries->percent_blocked, 2);
169+
}
147170

148171
$data = [
149172
'valid' => $valid,

0 commit comments

Comments
 (0)