Skip to content

Commit 83e98dc

Browse files
committed
v1.1.2 - Quality
1 parent 12cebbe commit 83e98dc

File tree

7 files changed

+100
-23
lines changed

7 files changed

+100
-23
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# technitium-dnsserver-php-api
22

33
This API client is intended to be used with Technitiums DNS Server
4+
For the full Technitium API Documentation please visit [Technitium API Documentation](https://github.com/TechnitiumSoftware/DnsServer/blob/master/APIDOCS.md)
45

56
## Installation
67

@@ -55,6 +56,20 @@ if($api->apps->downloadAndInstall($sampleApp["name"], $sampleApp["url"])) {
5556

5657
```
5758

59+
### Send to custom endpoint
60+
61+
```php
62+
<?php
63+
64+
require_once "/vendor/autoload.php";
65+
use Technitium\DNSServer\API;
66+
67+
$api = new API();
68+
// You have to set <bool>$bypass to true to use this feature
69+
echo var_dump($api->sendCall(data: array("field" => "value"), endpoint: "admin/users/list", skip: false, bypass: true))
70+
71+
```
72+
5873
## DDNS
5974

6075
You can use the `DDNS.Helper.API.dnsserver.ente.php` class to configure records to point to the current IP address.
@@ -108,6 +123,12 @@ DDNS(new API(__DIR__ . "/configurations", ".env-custom"), file_get_contents("/my
108123

109124
## Changes
110125

126+
### v1.1.2: Quality
127+
128+
- Added more documentation to the classes
129+
- Small code changes
130+
<!-- Removed whitespaces -->
131+
111132
### v1.1.1: Fixes
112133

113134
- Small changes to the `README.md`

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "ente/technitium-dnsserver-php-api",
33
"type": "library",
44
"license": "GPL-3.0-only",
5-
"version": "1.1.1",
5+
"version": "1.1.2",
66
"authors": [
77
{
88
"name": "Ente",

src/API.dnsserver.ente.php

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
class API {
2020

2121
private $protocol;
22-
2322
private $admin;
2423
private $allowed;
2524
private $apps;
@@ -32,16 +31,13 @@ class API {
3231
private $users;
3332
private $zones;
3433
private $ddns;
35-
3634
private $log;
37-
3835
private $conf;
39-
4036
private $path;
41-
4237
private $fullPath;
43-
4438
private $env = [];
39+
const PREFIX_GET = "&token=";
40+
const PREFIX_POST = "?token=";
4541

4642
public function __construct($confPath = null, $name = null){
4743
$this->loader();
@@ -57,6 +53,12 @@ private function setProtocol(){
5753
}
5854
}
5955

56+
/**
57+
* `loadConf()` - Load the .env file and set the environment variables.
58+
* @param mixed $path The full path to the directory containing the .env file. (optional)
59+
* @param mixed $name The name of the .env file. (optional)
60+
* @return void
61+
*/
6062
private function loadConf($path = null, $name = null){
6163
$this->conf = $name ?? ".env";
6264
$this->path = $path ?? $_SERVER["DOCUMENT_ROOT"];
@@ -102,6 +104,15 @@ public function loader(){
102104
require_once __DIR__ . "/helper/Log.Helper.API.dnsserver.ente.php";
103105
}
104106

107+
/**
108+
* `sendCall()` - Send a request to the Technitium API.
109+
* @param array $data The data to send to the API
110+
* @param string $endpoint The endpoint to send the data to, e.g. "admin/users/list"
111+
* @param mixed $method The HTTP method to use. Default is "POST".
112+
* @param mixed $skip Set to `true` to skip the authentication URI append.
113+
* @param mixed $bypass Set to `true` to bypass the endpoint check allowing to access not (yet) implemented methods.
114+
* @return array Returns the response from the API or an error (["status" => "error"]) as an array.
115+
*/
105116
public function sendCall($data, $endpoint, $method = "POST", $skip = false, $bypass = false){
106117
$c = curl_init();
107118
$endpoint = $this->prepareEndpoint($endpoint, $bypass);
@@ -126,50 +137,72 @@ public function sendCall($data, $endpoint, $method = "POST", $skip = false, $byp
126137
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
127138
break;
128139
}
140+
$result = ["status" => "error", "error" => "Unknown error"];
129141
try {
130142
$response = curl_exec($c);
131143
if(!$response){
132144
Log::error_rep("Failed to send request: " . curl_error($c));
133-
return ["status" => "error", "error" => curl_error($c)];
145+
$result = ["status" => "error", "error" => curl_error($c)];
134146
}
135147
} catch (\Throwable $e){
136148
Log::error_rep("Failed to send request: " . $e->getMessage());
137-
return ["status" => "error", "error" => $e->getMessage()];
149+
$result = ["status" => "error", "error" => $e->getMessage()];
138150
}
139151
curl_close($c);
140152
if($this->checkResponse($response)){
141153
Log::error_rep("Successfully accessed endpoint: " . $endpoint);
142-
return json_decode($response, true);
154+
$result = json_decode($response, true);
143155
}
144-
return [
145-
"error" => "An error occurred",
146-
];
156+
return $result;
147157
}
148158

159+
/**
160+
* `appendAuth()` - Append the authentication token to the URI.
161+
* @param mixed $m The HTTP method to use. Default is "POST".
162+
* @param mixed $skip Set to `true` to skip the authentication URI append, allowing the use of `API::getPermanentToken()`.
163+
* @return string Returns the authentication token URI string or an empty string.
164+
*/
149165
private function appendAuth($m = "POST", $skip = false){
150166
$this->loadConf($this->path, $this->conf);
167+
$authAppend = null;
151168
if($skip){
152169
return "";
153170
}
154171
if(!empty($this->env["TOKEN"])){
155172
switch($m){
156173
case "POST":
157-
return "?token=" . @$this->env["TOKEN"];
174+
$authAppend = $this::PREFIX_POST . @$this->env["TOKEN"];
175+
break;
158176
case "GET":
159-
return "&token=" . @$this->env["TOKEN"];
177+
$authAppend = $this::PREFIX_GET . @$this->env["TOKEN"];
178+
break;
179+
default:
180+
$authAppend = $this::PREFIX_POST . @$this->env["TOKEN"];
181+
break;
160182
}
161183
} else {
162184
$this->getPermanentToken();
163185
$this->loadConf($this->path, $this->conf);
164186
switch($m){
165187
case "POST":
166-
return "?token=" . @$this->env["TOKEN"];
188+
$authAppend = $this::PREFIX_POST . @$this->env["TOKEN"];
189+
break;
167190
case "GET":
168-
return "&token=" . @$this->env["TOKEN"];
191+
$authAppend = $this::PREFIX_GET . @$this->env["TOKEN"];
192+
break;
193+
default:
194+
$authAppend = $this::PREFIX_POST . @$this->env["TOKEN"];
195+
break;
169196
}
170197
}
198+
return $authAppend;
171199
}
172200

201+
/**
202+
* `getPermanentToken()` - Get a permanent token from the Technitium API.
203+
* This function is called when the token is not found in the .env file.
204+
* @return bool Returns `true` if the token was successfully written to the .env file.
205+
*/
173206
private function getPermanentToken(){
174207
Log::error_rep("Getting permanent token... | .env: " . $this->fullPath);
175208
$response = $this->sendCall([
@@ -190,11 +223,18 @@ private function getPermanentToken(){
190223
->write(true);
191224

192225
} catch(\Throwable $e){
193-
Log::error_rep("Unable to write to .env file: " . $this->fullPath); }
226+
Log::error_rep("Unable to write to .env file: " . $this->fullPath);
227+
}
194228
return true;
195229
}
196230

197-
private function checkResponse($response){
231+
/**
232+
* `checkResponse()` - Check if the Technitium API response is valid.
233+
* If the response status contains either "error" or "invalid-token" it is considered invalid.
234+
* @param mixed $response The response returned by `API::sendCall()` function.
235+
* @return bool Returns `true` if the response is valid, otherwise `false`.
236+
*/
237+
private function checkResponse(string $response){
198238
if(is_null($response)){
199239
return false;
200240
} else {
@@ -203,6 +243,12 @@ private function checkResponse($response){
203243
}
204244
}
205245

246+
/**
247+
* `prepareEndpoint()` - Generates the API URI for the endpoint in question.
248+
* @param mixed $endpoint The endpoint to generate the URI for.
249+
* @param mixed $bypass Set to `true` to bypass the endpoint check allowing to access not (yet) implemented methods.
250+
* @return bool|string Returns the URI as string or `false` if the endpoint is not implemented.
251+
*/
206252
private function prepareEndpoint($endpoint, $bypass = false){
207253
if($bypass){
208254
return $this->protocol . "://" . $this->env["API_URL"] . "/api/" . $endpoint;
@@ -280,7 +326,7 @@ public function ddns(): DDNS {
280326
}
281327

282328
public function log(): Log {
283-
if(!$this->log) $this->log = new Log($this);
329+
if(!$this->log) $this->log = new Log(null);
284330
return $this->log;
285331
}
286332
}

src/endpoints/admin/Groups.admin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
/**
55
* Groups class
6-
*
6+
*
77
* This class is used to interact with the groups endpoint of the Technitium DNS Server API
88
*/
99
class groups {

src/endpoints/allowed/Allowed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public function __construct($api){
1111
* `list()` - Returns a list of all allowed domains.
1212
* @param string $domain The domain to list.
1313
* @param string $direction The direction to list the domains. Valid values are [`up`, `down`].
14-
* @return array|bool Returns the result array or `false` if the group was not found.
14+
* @return array|bool Returns the result array or `false` if the group was not found.
1515
*/
1616
public function list(string $domain = "", string $direction = "up"){
1717
$response = $this->API->sendCall(["domain" => $domain, "direction" => $direction], "allowed/list");

src/endpoints/apps/Config.apps.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public function __construct($api){
1111
/**
1212
* `get()` - Returns the configuration of an app.
1313
* @param string $name The name of the app.
14-
* @return array|bool Returns the result array or `false` if the group was not found.
14+
* @return array|bool Returns the result array or `false` if the group was not found.
1515
*/
1616
public function get(string $name){
1717
$response = $this->API->sendCall(["name" => $name], "apps/config/get");

src/endpoints/dhcp/Scopes.dhcp.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,21 @@ public function enable(string $name){
8181
return $response["status"] == "ok";
8282
}
8383

84+
/**
85+
* `disable()` - Disables the DHCP scope.
86+
* @param string $name The name of the scope.
87+
* @return bool Returns `true` if the request was successful, `false` otherwise.
88+
*/
8489
public function disable(string $name){
8590
$response = $this->API->sendCall(["name" => $name], "dhcp/scopes/disable");
8691
return $response["status"] == "ok";
8792
}
8893

94+
/**
95+
* `delete()` - Deletes the DHCP scope.
96+
* @param string $name The name of the scope.
97+
* @return bool Returns `true` if the request was successful, `false` otherwise.
98+
*/
8999
public function delete(string $name){
90100
$response = $this->API->sendCall(["name" => $name], "dhcp/scopes/delete");
91101
return $response["status"] == "ok";

0 commit comments

Comments
 (0)