Skip to content

Commit

Permalink
working code for v5 and v6
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroBuffon committed Sep 28, 2024
1 parent 3738a70 commit e596449
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 38 deletions.
108 changes: 70 additions & 38 deletions Pihole/Pihole.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,33 @@ class Pihole extends \App\SupportedApps implements \App\EnhancedApps

public function __construct()
{
//$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set
$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set
}

public function test()
{
$version = $this->config->version;

if ($version == 5) {
$test = parent::appTest($this->url("api.php?summaryRaw"));
echo $test->status;
}
if ($version == 6) {
$test = $this->login();
if ($test->getStatusCode() === 200 && $test->getBody()) {
$wk_resp = json_decode($test->getBody());
echo "Hello " . $wk_resp->data->username;
} else {
echo "Failed";
$test = $this->getInfo();
if ($test["valid"]){
echo "Successfully communicated with the API";
}else{
echo "Error while communicating with the API";
}
}
}
public function livestats()
{
$status = "inactive";
$version = $this->config->version;

if ($version == 5) {
$res = parent::execute($this->url("api.php?summaryRaw"));
$details = json_decode($res->getBody());

$data = [];

if ($details) {
$data["ads_blocked"] = number_format(
$details->ads_blocked_today
Expand All @@ -52,57 +48,93 @@ public function livestats()
$details->ads_percentage_today,
1
);
$data["gravity"] = number_format(
$details->domains_being_blocked,
0,
'',
'.'
);

$status = "active";
}
}

if ($version == 6) {
$auth = $this->login();

$attrs = [
"body" => "sid:" . $auth->sid,
"headers" => [
"content-type" => "application/json",
"accept" => "application/json",
],
];
$result = parent::execute($this->url("api/stats/summary"),$attrs);

$data["ads_blocked"] = $result->blocked;
$data["ads_percentage"] = $result->percent_blocked;
$results = $this->getInfo();

$data["ads_blocked"] = $results["queries"];
$data["ads_percentage"] = $results["percent"];
$data["gravity"] = $results["gravity"];

$status = "active";
}
return parent::getLiveStats($status, $data);
}

public function url($endpoint)
{
$version = $this->config->version;
if ($version == 5){
$apiKey = $this->config->apikey;
$apikey = $this->config->apikey;
$api_url = parent::normaliseurl($this->config->url) . $endpoint;

if ($apiKey) {
$api_url .= "&auth=" . $apiKey;
if ($apikey) {
$api_url .= "&auth=" . $apikey;
}
}
if ($version == 6) {
$api_url = parent::normaliseurl($this->config->url) . $endpoint;
}
return $api_url;
}
public function login()

public function getInfo()
{
$attrs = [
"body" => "password:" . $this->config->apiKey,
"body" => json_encode(['password' => $this->config->apikey]),
"cookies" => $this->jar,
"headers" => [
"content-type" => "application/json",
"accept" => "application/json",
"Content-Type" => "application/json",
"Accept" => "application/json",
],
];
return parent::execute(
$this->url("api/auth"),
$attrs,
null,
"POST"
);

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

$attrsid = [
"body" => json_encode(['sid' => $auth->session->sid]),
"cookies" => $this->jar,
"headers" => [
"Content-Type" => "application/json",
"Accept" => "application/json",
],
];

// Get queries data
$responsesummary = parent::execute($this->url("api/stats/summary"), $attrsid, null, "GET");
$datasummary = json_decode($responsesummary->getBody());

// After retrieving the data the session is closed to declutter
parent::execute($this->url("api/auth"), $attrsid, null, "DELETE");

// Extract data from the response
$valid = $auth->session->valid;
$validity = $auth->session->validity;
$message = $auth->session->message;
$queriesblocked = $datasummary->queries->blocked;
$percentblocked = round($datasummary->queries->percent_blocked, 2);
$gravity = number_format($datasummary->gravity->domains_being_blocked, 0, '', '.');

$data = [
'valid' => $valid,
'validity' => $validity,
'message' => $message,
'queries' => $queriesblocked,
'percent' => $percentblocked,
'gravity' => $gravity
];
return $data;
}
}
}
4 changes: 4 additions & 0 deletions Pihole/livestats.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
<span class="title">Percent<br /> Blocked</span>
<strong>{!! $ads_percentage !!}</strong>
</li>
<li>
<span class="title">Domains<br /> Blocked</span>
<strong>{!! $gravity !!}</strong>
</li>
</ul>

0 comments on commit e596449

Please sign in to comment.