Skip to content

Commit

Permalink
v1.1.1 - Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ente committed Oct 30, 2024
1 parent 68d77d6 commit 12cebbe
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 24 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ This API client is intended to be used with Technitiums DNS Server

## Installation

Via Git: `git clone https://github.com/ente/technitium-dnsserver-php-api.git` or `composer require ente/technitium-dnsserver-php-api`
### Via Git

Run `git clone https://github.com/ente/technitium-dnsserver-php-api.git` where ever you want the library to be located.
Then `require_once "/path/to/API.dnsserver.ente.php";` & `use Technitium\DNSServer\API\API;` in your PHP file.

### Via Composer

Run `composer require ente/technitium-dnsserver-php-api`
Then: `require_once "/path/to/vendor/autoload.php";` & `use Technitium\DNSServer\API\API;`
Then: `require_once "/path/to/API.dnsserver.ente.php";` & `use Technitium\DNSServer\API\API;`

## Configuration

Expand Down Expand Up @@ -103,6 +108,13 @@ DDNS(new API(__DIR__ . "/configurations", ".env-custom"), file_get_contents("/my

## Changes

### v1.1.1: Fixes

- Small changes to the `README.md`
- Added code documentation within the classes
- Implemented the `<bool>$bypass` parameter to the sendCall function to bypass the `prepareEndpoint` function therefore allowing to send to custom endpoints
- Adjusted class functions scope

### v1.1: Fixes

- `TOKEN` can now be empty
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ente/technitium-dnsserver-php-api",
"type": "library",
"license": "GPL-3.0-only",
"version": "1.1",
"version": "1.1.1",
"authors": [
{
"name": "Ente",
Expand Down
24 changes: 16 additions & 8 deletions src/API.dnsserver.ente.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ public function __construct($confPath = null, $name = null){
$this->setProtocol();
}

public function setProtocol(){
private function setProtocol(){
if($this->env["USE_HTTPS"] == "true"){
$this->protocol = "https";
} else {
$this->protocol = "http";
}
}

public function loadConf($path = null, $name = null){
private function loadConf($path = null, $name = null){
$this->conf = $name ?? ".env";
$this->path = $path ?? $_SERVER["DOCUMENT_ROOT"];
$this->fullPath = $this->path . "/" . $this->conf;
Expand Down Expand Up @@ -102,9 +102,9 @@ public function loader(){
require_once __DIR__ . "/helper/Log.Helper.API.dnsserver.ente.php";
}

public function sendCall($data, $endpoint, $method = "POST", $skip = false){
public function sendCall($data, $endpoint, $method = "POST", $skip = false, $bypass = false){
$c = curl_init();
$endpoint = $this->prepareEndpoint($endpoint);
$endpoint = $this->prepareEndpoint($endpoint, $bypass);
if($this->env["USE_POST"]){
$method = "POST";
}
Expand All @@ -120,6 +120,11 @@ public function sendCall($data, $endpoint, $method = "POST", $skip = false){
curl_setopt($c, CURLOPT_URL, $endpoint . "?" . $data . $this->appendAuth($method, $skip));
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
break;
default:
$data = http_build_query($data);
curl_setopt($c, CURLOPT_URL, $endpoint . "?" . $data . $this->appendAuth($method, $skip));
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
break;
}
try {
$response = curl_exec($c);
Expand All @@ -141,7 +146,7 @@ public function sendCall($data, $endpoint, $method = "POST", $skip = false){
];
}

public function appendAuth($m = "POST", $skip = false){
private function appendAuth($m = "POST", $skip = false){
$this->loadConf($this->path, $this->conf);
if($skip){
return "";
Expand All @@ -165,7 +170,7 @@ public function appendAuth($m = "POST", $skip = false){
}
}

public function getPermanentToken(){
private function getPermanentToken(){
Log::error_rep("Getting permanent token... | .env: " . $this->fullPath);
$response = $this->sendCall([
"user" => $this->env["USERNAME"],
Expand All @@ -189,7 +194,7 @@ public function getPermanentToken(){
return true;
}

public function checkResponse($response){
private function checkResponse($response){
if(is_null($response)){
return false;
} else {
Expand All @@ -198,7 +203,10 @@ public function checkResponse($response){
}
}

public function prepareEndpoint($endpoint){
private function prepareEndpoint($endpoint, $bypass = false){
if($bypass){
return $this->protocol . "://" . $this->env["API_URL"] . "/api/" . $endpoint;
}
$endpoints = json_decode(file_get_contents(__DIR__ . "/helper/endpoints.json"));

if(in_array($endpoint, $endpoints)){
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct($api){
$this->eloader();
}

public function eloader(){
private function eloader(){
require_once __DIR__ . "/Groups.admin.php";
require_once __DIR__ . "/Logs.admin.php";
require_once __DIR__ . "/Permissions.admin.php";
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/apps/Apps.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function __construct($api){
$this->eloader();
}

public function eloader(){
private function eloader(){
require_once __DIR__ . "/Config.apps.php";
}

Expand Down
3 changes: 2 additions & 1 deletion src/endpoints/dashboard/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ class dashboard extends API {

public function __construct($api){
$this->API = $api;
$this->eloader();
}

public function eloader(){
private function eloader(){
require_once __DIR__ . "/Stats.dashboard.php";
}

Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/dhcp/DHCP.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct($api){
$this->eloader();
}

public function eloader(){
private function eloader(){
require_once __DIR__ . "/Leases.dhcp.php";
require_once __DIR__ . "/Scopes.dhcp.php";
}
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/users/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct($api){
$this->eloader();
}

public function eloader(){
private function eloader(){
try {
require_once __DIR__ . "/Profile.users.php";
require_once __DIR__ . "/Session.users.php";
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/zones/DNSSEC.zones.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function __construct($api){
$this->eloader();
}

public function eloader(){
private function eloader(){
require_once __DIR__ . "/Properties.dnssec.zones.php";
}

Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/zones/Zones.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct($api){
$this->eloader();
}

public function eloader(){
private function eloader(){
require_once __DIR__ . "/Catalogs.zones.php";
require_once __DIR__ . "/DNSSEC.zones.php";
require_once __DIR__ . "/Permissions.zones.php";
Expand Down
30 changes: 30 additions & 0 deletions src/helper/DDNS.Helper.API.dnsserver.ente.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@ public function __construct($api, $configFile = null){
}
}

/**
* `getPublicIP()` function is used to get the public IP address of the server.
* @param mixed $uri The URI to get the public IP address from. Currently, it is set to "https://ipecho.net/plain".
* @return string The public IP address of the server.
*/
public function getPublicIP($uri = "https://ipecho.net/plain") {
return trim(file_get_contents($uri));
}

/**
* `updateRecords()` function is used to update the records in the DNS server.
* @param string $configFile The path to the configuration file.
* @return bool Returns true if the records are updated successfully.
*/
public function updateRecords(string $configFile) {
$config = json_decode(file_get_contents($configFile));
$records = $config->records;
Expand All @@ -29,6 +39,12 @@ public function updateRecords(string $configFile) {
return true;
}

/**
* `processRecord()` function is used to process the record.
* @param mixed $record The record to be processed.
* @param mixed $domain The domain the record belongs to.
* @return void Either adds or updates the record.
*/
private function processRecord($record, $domain) {
try {
$allrecords = $this->API->zones()->records()->get($record)["records"];
Expand Down Expand Up @@ -59,6 +75,13 @@ private function processRecord($record, $domain) {
}
}

/**
* `updateRecord()` function is used to update the record.
* @param mixed $record The record to be updated.
* @param mixed $domain The domain the record belongs to.
* @param mixed $newIP The new IP address to be used.
* @return void Updates the record.
*/
private function updateRecord($record, $domain, $newIP) {
if (!$this->API->zones()->records()->update([
"domain" => $record,
Expand All @@ -72,6 +95,13 @@ private function updateRecord($record, $domain, $newIP) {
}
}

/**
* `addRecord()` function is used to add the record.
* @param mixed $record The record to be added.
* @param mixed $domain The domain the record belongs to.
* @param mixed $ipAddress The IP address to be used.
* @return void Adds the record.
*/
private function addRecord($record, $domain, $ipAddress) {
if (!$this->API->zones()->records()->add([
"domain" => $record,
Expand Down
37 changes: 31 additions & 6 deletions src/helper/Log.Helper.API.dnsserver.ente.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,36 @@
namespace Technitium\DNSSERVER\API\Helper;

class Log extends \Exception {

/**
* Log constructor.
* @param string $message The exception message
* @param int $code The exception code
* @param \Exception|null $previous A reference to the previous exception
*/
public function __construct($message, $code = 0, \Exception $previous = null){
$this->error_rep($message);
$trace = $this->getTraceAsString() ?? "N/A";
$this->error_rep(message: $message, method: $trace);
parent::__construct($message, $code, $previous);
}

/**
* @return string
*/
public function __toString(): string {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}

public static function error_rep($message, $method = NULL){
/**
* `error_rep()` function is used to log the error message to the error log file.
* @param string $message The error message to be logged.
* @param string $method Name of either the method name that called the function or the request method.
*/
public static function error_rep($message, $method = null){
$error_file = self::logrotate(); // file on your fs, e.g. /var/www/html/error.log
$version = @file_get_contents($_SERVER["DOCUMENT_ROOT"] . "/VERSION") ?? "N/A"; //optional value
if($method == NULL){
$method = $_SERVER["REQUEST_METHOD"];
if($method == null){
$method = @$_SERVER["REQUEST_METHOD"];
}
$addr = @$_SERVER["SERVER_ADDR"] ?? "N/A";
$rhost = @$_SERVER["REMOTE_HOST"] ?? "N/A";
Expand All @@ -24,7 +40,12 @@ public static function error_rep($message, $method = NULL){

}

public static function getSpecificLogFilePath($date = null){
/**
* `getSpecificLogFilePath()` function is used to get the specific log file path.
* @param string|null $date The date to get the log file path for. Format: "Y-m-d".
* @return string The specific log file path.
*/
private static function getSpecificLogFilePath($date = null){
if($date == null){
$date = date("Y-m-d");
return __DIR__ . "/data/logs/log-{$date}.log";
Expand All @@ -38,7 +59,11 @@ public static function getSpecificLogFilePath($date = null){
}
}

public static function logrotate(){
/**
* `logrotate()` function is used to rotate the log file.
* @return string The path to the log file.
*/
private static function logrotate(){
$logpath = __DIR__ . "/data/logs/";
$date = date("Y-m-d");
$lastrotate = @file_get_contents(__DIR__ . "/data/logs/logrotate-cache.txt");
Expand Down

0 comments on commit 12cebbe

Please sign in to comment.