1919class 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}
0 commit comments