mirror of
https://github.com/pi-hole/web.git
synced 2026-04-23 18:29:43 +01:00
Calling FTL API directly from PHP
- Using constants for default values - Remove unnecessary variable - Replace `piholeStatusAPI()` with a call to FTL API - Transfer `api.php?status` code from `api.php` to `api_FTL.php` - Change `piholeStatus()` to use FTL info (no need to use `pihole`). Signed-off-by: RD WebDesign <github@rdwebdesign.com.br>
This commit is contained in:
@@ -6,88 +6,93 @@
|
||||
* This file is copyright under the latest version of the EUPL.
|
||||
* Please see LICENSE file for your rights under this license. */
|
||||
|
||||
$piholeFTLConfFile = "/etc/pihole/pihole-FTL.conf";
|
||||
const DEFAULT_FTLCONFFILE = "/etc/pihole/pihole-FTL.conf";
|
||||
const DEFAULT_FTL_IP = "127.0.0.1";
|
||||
const DEFAULT_FTL_PORT = 4711;
|
||||
|
||||
function piholeFTLConfig($force=false)
|
||||
{
|
||||
static $piholeFTLConfig;
|
||||
global $piholeFTLConfFile;
|
||||
function piholeFTLConfig($piholeFTLConfFile = DEFAULT_FTLCONFFILE, $force = false) {
|
||||
static $piholeFTLConfig;
|
||||
|
||||
if(isset($piholeFTLConfig) && !$force)
|
||||
{
|
||||
return $piholeFTLConfig;
|
||||
}
|
||||
if (isset($piholeFTLConfig) && !$force) {
|
||||
return $piholeFTLConfig;
|
||||
}
|
||||
|
||||
if(is_readable($piholeFTLConfFile))
|
||||
{
|
||||
$piholeFTLConfig = parse_ini_file($piholeFTLConfFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
$piholeFTLConfig = array();
|
||||
}
|
||||
if (is_readable($piholeFTLConfFile)) {
|
||||
$piholeFTLConfig = parse_ini_file($piholeFTLConfFile);
|
||||
} else {
|
||||
$piholeFTLConfig = array();
|
||||
}
|
||||
|
||||
return $piholeFTLConfig;
|
||||
return $piholeFTLConfig;
|
||||
}
|
||||
|
||||
function connectFTL($address, $port=4711)
|
||||
{
|
||||
if($address == "127.0.0.1")
|
||||
{
|
||||
$config = piholeFTLConfig();
|
||||
// Read port
|
||||
$portfileName = isset($config['PORTFILE']) ? $config['PORTFILE'] : '';
|
||||
if ($portfileName != '')
|
||||
{
|
||||
$portfileContents = file_get_contents($portfileName);
|
||||
if(is_numeric($portfileContents))
|
||||
$port = intval($portfileContents);
|
||||
}
|
||||
}
|
||||
function connectFTL($address, $port) {
|
||||
if ($address == DEFAULT_FTL_IP) {
|
||||
$config = piholeFTLConfig();
|
||||
// Read port
|
||||
$portfileName = isset($config['PORTFILE']) ? $config['PORTFILE'] : '';
|
||||
if ($portfileName != '') {
|
||||
$portfileContents = file_get_contents($portfileName);
|
||||
if (is_numeric($portfileContents)) {
|
||||
$port = intval($portfileContents);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Open Internet socket connection
|
||||
$socket = @fsockopen($address, $port, $errno, $errstr, 1.0);
|
||||
// Open Internet socket connection
|
||||
$socket = @fsockopen($address, $port, $errno, $errstr, 1.0);
|
||||
|
||||
return $socket;
|
||||
return $socket;
|
||||
}
|
||||
|
||||
function sendRequestFTL($requestin)
|
||||
{
|
||||
global $socket;
|
||||
|
||||
$request = ">".$requestin;
|
||||
fwrite($socket, $request) or die('{"error":"Could not send data to server"}');
|
||||
function sendRequestFTL($requestin, $socket) {
|
||||
$request = ">".$requestin;
|
||||
fwrite($socket, $request) or die('{"error":"Could not send data to server"}');
|
||||
}
|
||||
|
||||
function getResponseFTL()
|
||||
{
|
||||
global $socket;
|
||||
function getResponseFTL($socket) {
|
||||
$response = [];
|
||||
|
||||
$response = [];
|
||||
$errCount = 0;
|
||||
while (true) {
|
||||
$out = fgets($socket);
|
||||
if ($out == "") {
|
||||
$errCount++;
|
||||
}
|
||||
|
||||
$errCount = 0;
|
||||
while(true)
|
||||
{
|
||||
$out = fgets($socket);
|
||||
if ($out == "") $errCount++;
|
||||
if ($errCount > 100) {
|
||||
// Tried 100 times, but never got proper reply, fail to prevent busy loop
|
||||
die('{"error":"Tried 100 times to connect to FTL server, but never got proper reply. Please check Port and logs!"}');
|
||||
}
|
||||
if(strrpos($out,"---EOM---") !== false)
|
||||
break;
|
||||
if ($errCount > 100) {
|
||||
// Tried 100 times, but never got proper reply, fail to prevent busy loop
|
||||
die('{"error":"Tried 100 times to connect to FTL server, but never got proper reply. Please check Port and logs!"}');
|
||||
}
|
||||
|
||||
$out = rtrim($out);
|
||||
if(strlen($out) > 0)
|
||||
$response[] = $out;
|
||||
}
|
||||
if (strrpos($out,"---EOM---") !== false) {
|
||||
break;
|
||||
}
|
||||
|
||||
return $response;
|
||||
$out = rtrim($out);
|
||||
if (strlen($out) > 0) {
|
||||
$response[] = $out;
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
function disconnectFTL()
|
||||
{
|
||||
global $socket;
|
||||
fclose($socket);
|
||||
function disconnectFTL($socket) {
|
||||
fclose($socket);
|
||||
}
|
||||
|
||||
function callFTLAPI($request, $FTL_IP = DEFAULT_FTL_IP, $port = DEFAULT_FTL_PORT) {
|
||||
$socket = connectFTL($FTL_IP, $port);
|
||||
|
||||
if (!is_resource($socket)) {
|
||||
$data = array("FTLnotrunning" => true);
|
||||
} else {
|
||||
sendRequestFTL($request, $socket);
|
||||
$data = getResponseFTL($socket);
|
||||
}
|
||||
disconnectFTL($socket);
|
||||
|
||||
return $data;
|
||||
}
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user