Heimdall/app/SupportedApps/Pihole.php

73 lines
1.9 KiB
PHP
Raw Normal View History

2018-02-07 15:43:29 +00:00
<?php namespace App\SupportedApps;
2018-02-08 23:45:37 +00:00
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
class Pihole implements Contracts\Applications, Contracts\Livestats {
2018-02-07 15:43:29 +00:00
public function defaultColour()
{
return '#352222';
2018-02-07 15:43:29 +00:00
}
public function icon()
{
return 'supportedapps/pihole.png';
}
2018-02-08 23:45:37 +00:00
public function configDetails()
{
return 'pihole';
}
public function testConfig()
{
$res = $this->buildRequest();
switch($res->getStatusCode()) {
case 200:
echo 'Successfully connected to the API';
break;
case 401:
echo 'Failed: Invalid credentials';
break;
case 404:
echo 'Failed: Please make sure your URL is correct and that there is a trailing slash';
break;
default:
echo 'Something went wrong... Code: '.$res->getStatusCode();
break;
}
}
public function executeConfig()
{
2018-03-05 19:38:23 +00:00
$html = '';
$active = 'active';
2018-02-08 23:45:37 +00:00
$res = $this->buildRequest();
$data = json_decode($res->getBody());
2018-03-05 19:38:23 +00:00
$html = '
2018-02-08 23:45:37 +00:00
<ul class="livestats">
<li><span class="title">Domains<br />Blocked</span><strong>'.$data->domains_being_blocked.'</strong></li>
<li><span class="title">Blocked<br />Today</span><strong>'.$data->ads_blocked_today.'</span></strong></li>
</ul>
';
2018-03-05 19:38:23 +00:00
return json_encode(['status' => $active, 'html' => $html]);
2018-02-08 23:45:37 +00:00
}
public function buildRequest()
{
$config = $this->config;
$url = $config->url;
2018-02-14 20:39:07 +00:00
$url = rtrim($url, '/');
$api_url = $url.'/api.php';
2018-02-08 23:45:37 +00:00
//die( $api_url.' --- ');
2018-03-05 19:38:23 +00:00
$client = new Client(['http_errors' => false, 'timeout' => 15, 'connect_timeout' => 15]);
2018-02-08 23:45:37 +00:00
$res = $client->request('GET', $api_url);
return $res;
}
2018-02-07 15:43:29 +00:00
}