Skip to content

Commit b4a0b2e

Browse files
committed
v1.2: Adding support to export functions
1 parent 2646445 commit b4a0b2e

8 files changed

Lines changed: 105 additions & 17 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Then: `require_once "/path/to/vendor/autoload.php";` & `use Technitium\DNSServer
2929
- `USE_POST`: Specify if you want to access the API via POST (`true`) instead of GET (`false`) in default.
3030
- `USE_HTTPS`: Enable (`true`) HTTPS for the API connection. If your server does not support HTTPS, the API will simply return `false` to all requests.
3131

32+
When using `$api->admin()->logs()->export("2024-12-24")` you may need to adjust the `memory_limit` in your `php.ini` file or via `ini_set()`.
33+
3234
## General Usage
3335

3436
```php
@@ -125,6 +127,13 @@ DDNS(new API(__DIR__ . "/configurations", ".env-custom"), file_get_contents("/my
125127

126128
## Changes
127129

130+
### v1.2: Adding support for export functions
131+
132+
- Added support for export functions within API classes. When using export functions note that downloading log files may take quite a while. You may also need to adjust the `memory_limit`.
133+
134+
<!-- Added function to support downloading files -->
135+
<!-- Fixed incorrect endpoint URL for "Log" endpoints -->
136+
128137
### v1.1.4: Type safety
129138

130139
- Added type safety to the `DDNS.Helper.API.dnsserver.ente.php` class

src/API.dnsserver.ente.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,55 @@ public function loader(): void{
107107
require_once __DIR__ . "/helper/Log.Helper.API.dnsserver.ente.php";
108108
}
109109

110+
public function downloadFile($endpoint, $bypass, $data): string {
111+
$c = curl_init();
112+
$endpoint = $this->prepareEndpoint($endpoint, $bypass);
113+
$method = "POST";
114+
115+
curl_setopt($c, CURLOPT_URL, $endpoint . $this->appendAuth($method));
116+
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
117+
curl_setopt($c, CURLOPT_HEADER, true);
118+
119+
if ($method === "POST") {
120+
curl_setopt($c, CURLOPT_POST, true);
121+
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
122+
}
123+
124+
$response = curl_exec($c);
125+
126+
if ($response === false) {
127+
$error = curl_error($c);
128+
curl_close($c);
129+
throw new \Exception("cURL error: $error");
130+
}
131+
132+
$headerSize = curl_getinfo($c, CURLINFO_HEADER_SIZE);
133+
$headers = substr($response, 0, $headerSize);
134+
$body = substr($response, $headerSize);
135+
136+
curl_close($c);
137+
138+
if (strpos($headers, 'Content-Disposition: attachment') !== false) {
139+
if (preg_match('/filename="(.+?)"/', $headers, $matches)) {
140+
$fileName = $matches[1];
141+
} else {
142+
$fileName = bin2hex(random_bytes(16)) . ".txt";
143+
}
144+
145+
$path = __DIR__ . "/helper/data/downloads/" . $fileName;
146+
if (file_put_contents($path, $body) === false) {
147+
throw new \Exception("Failed to save the downloaded file.");
148+
}
149+
Log::error_rep("Downloaded file: " . $path . " from endpoint: " . $endpoint);
150+
return $path;
151+
} else {
152+
Log::error_rep("Unexpected response format: " . $body);
153+
throw new \Exception("API did not return a downloadable file.");
154+
}
155+
}
156+
157+
158+
110159
/**
111160
* `sendCall()` - Send a request to the Technitium API.
112161
* @param array $data The data to send to the API

src/endpoints/admin/Logs.admin.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ public function query($data): array{
4747
* `download()` - Downloads a log file.
4848
* @param string $fileName The name of the file to download.
4949
* @param int $limit The number of mb to limit the download to.
50-
* @return array|bool Returns the result array or `false` if the group was not found.
50+
* @return string|bool Returns the file path or `false` if the file was not found.
5151
*/
52-
public function download(string $fileName, int $limit = 0): array|bool{
53-
$response = $this->API->sendCall(["fileName" => $fileName, "limit" => $limit], "admin/logs/download");
54-
if($response["status"] == "ok"){
55-
return $response["response"];
52+
public function download(string $fileName, int $limit = 0): string|bool{
53+
$response = $this->API->downloadFile("logs/download", 0, ["fileName" => $fileName, "limit" => $limit]);
54+
if(!empty($response)){
55+
return $response;
5656
} else {
5757
return false;
5858
}

src/endpoints/allowed/Allowed.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,17 @@ public function import(array $zones): bool{
6464

6565
}
6666

67-
public function export(){
68-
return $this->API->sendCall([], "allowed/export");
67+
/**
68+
* `export()` - Exports all allowed zones.
69+
* @return string|bool Either the file path where the allowed zones were exported to or `false` if the export failed.
70+
*/
71+
public function export(): string|bool{
72+
$result = $this->API->downloadFile("allowed/export", false, []);
73+
74+
if(!empty($result)){
75+
return $result;
76+
} else {
77+
return false;
78+
}
6979
}
7080
}

src/endpoints/blocked/Blocked.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,18 @@ public function delete(string $domain): bool{
4444

4545
}
4646

47-
48-
public function export($data){
49-
return $this->sendCall($data, "blocked/export");
47+
/**
48+
* `export()` - Exports all blocked zones.
49+
* @return string|bool Either the file path where the blocked zones were exported to or `false` if the export failed.
50+
*/
51+
public function export(): string|bool{
52+
$result = $this->API->downloadFile("blocked/export", false, []);
53+
54+
if(!empty($result)){
55+
return $result;
56+
} else {
57+
return false;
58+
}
5059
}
5160

5261
/**

src/endpoints/zones/Zones.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,19 @@ public function enable(string $zone){
124124
return $response["status"] == "ok";
125125
}
126126

127-
public function export($data){
128-
return $this->sendCall($data, "zones/export");
127+
/**
128+
* `export()` - Export a zone.
129+
* @param string $zone The name of the zone to export.
130+
* @return srring|bool Returns the file path of the exported zone or `false` otherwise.
131+
*/
132+
public function export(string $zone): string|bool{
133+
$result = $this->API->downloadFile("zones/export", false, ["zone" => $zone]);
134+
135+
if(!empty($result)){
136+
return $result;
137+
} else {
138+
return false;
139+
}
129140
}
130141

131142
/**

src/helper/data/downloads/.gitkeep

Whitespace-only changes.

src/helper/endpoints.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@
136136
"admin/permissions/get",
137137
"admin/permissions/set",
138138

139-
"admin/logs/list",
140-
"admin/logs/download",
141-
"admin/logs/delete",
142-
"admin/logs/deleteAll",
143-
"admin/logs/query"
139+
"logs/list",
140+
"logs/download",
141+
"logs/delete",
142+
"logs/deleteAll",
143+
"logs/query"
144144
]

0 commit comments

Comments
 (0)