|
| 1 | +<?php |
| 2 | +namespace Technitium\DNSServer\API; |
| 3 | +use Technitium\DNSServer\API\admin; |
| 4 | +use Technitium\DNSServer\API\allowed; |
| 5 | +use Technitium\DNSServer\API\apps; |
| 6 | +use Technitium\DNSServer\API\blocked; |
| 7 | +use Technitium\DNSServer\API\cache; |
| 8 | +use Technitium\DNSServer\API\dashboard; |
| 9 | +use Technitium\DNSServer\API\dhcp; |
| 10 | +use Technitium\DNSServer\API\dnsClient; |
| 11 | +use Technitium\DNSServer\API\settings; |
| 12 | +use Technitium\DNSServer\API\users; |
| 13 | +use Technitium\DNSServer\API\zones; |
| 14 | +use Technitium\DNSServer\API\Helper\DDNS; |
| 15 | +use Technitium\DNSServer\API\Helper\Log; |
| 16 | +use \Dotenv\Dotenv; |
| 17 | + |
| 18 | +class API { |
| 19 | + |
| 20 | + private $protocol; |
| 21 | + |
| 22 | + private $admin; |
| 23 | + private $allowed; |
| 24 | + private $apps; |
| 25 | + private $blocked; |
| 26 | + private $cache; |
| 27 | + private $dashboard; |
| 28 | + private $dhcp; |
| 29 | + private $dnsClient; |
| 30 | + private $settings; |
| 31 | + private $users; |
| 32 | + private $zones; |
| 33 | + private $ddns; |
| 34 | + |
| 35 | + private $log; |
| 36 | + |
| 37 | + public function __construct($confPath = null){ |
| 38 | + $this->loader(); |
| 39 | + $this->loadConf($confPath = null); |
| 40 | + $this->setProtocol(); |
| 41 | + } |
| 42 | + |
| 43 | + public function setProtocol(){ |
| 44 | + if($_ENV["USE_HTTPS"] == "true"){ |
| 45 | + $this->protocol = "https"; |
| 46 | + } else { |
| 47 | + $this->protocol = "http"; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public function loadConf($path = null){ |
| 52 | + if($path != null){ |
| 53 | + $env = \Dotenv\Dotenv::createImmutable($path); |
| 54 | + $env->safeLoad(); |
| 55 | + } else { |
| 56 | + $env = \Dotenv\Dotenv::createImmutable(dirname(__DIR__, 1)); |
| 57 | + $env->safeLoad(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public function loader(){ |
| 62 | + require_once __DIR__ . "/endpoints/admin/Admin.php"; |
| 63 | + require_once __DIR__ . "/endpoints/allowed/Allowed.php"; |
| 64 | + require_once __DIR__ . "/endpoints/apps/Apps.php"; |
| 65 | + require_once __DIR__ . "/endpoints/blocked/Blocked.php"; |
| 66 | + require_once __DIR__ . "/endpoints/cache/Cache.php"; |
| 67 | + require_once __DIR__ . "/endpoints/dashboard/Dashboard.php"; |
| 68 | + require_once __DIR__ . "/endpoints/dhcp/DHCP.php"; |
| 69 | + require_once __DIR__ . "/endpoints/dnsClient/DnsClient.php"; |
| 70 | + require_once __DIR__ . "/endpoints/settings/Settings.php"; |
| 71 | + require_once __DIR__ . "/endpoints/users/Users.php"; |
| 72 | + require_once __DIR__ . "/endpoints/zones/Zones.php"; |
| 73 | + |
| 74 | + require_once __DIR__ . "/helper/DDNS.Helper.API.dnsserver.ente.php"; |
| 75 | + require_once __DIR__ . "/helper/Log.Helper.API.dnsserver.ente.php"; |
| 76 | + } |
| 77 | + |
| 78 | + public function sendCall($data, $endpoint, $method = "POST"){ |
| 79 | + $c = curl_init(); |
| 80 | + $endpoint = $this->prepareEndpoint($endpoint); |
| 81 | + if($_ENV["USE_POST"]){ |
| 82 | + $method = "POST"; |
| 83 | + } |
| 84 | + switch($method){ |
| 85 | + case "POST": |
| 86 | + curl_setopt($c, CURLOPT_URL, $endpoint . $this->appendAuth()); |
| 87 | + curl_setopt($c, CURLOPT_RETURNTRANSFER, true); |
| 88 | + curl_setopt($c, CURLOPT_POST, true); |
| 89 | + curl_setopt($c, CURLOPT_POSTFIELDS, $data); |
| 90 | + break; |
| 91 | + case "GET": |
| 92 | + $data = http_build_query($data); |
| 93 | + curl_setopt($c, CURLOPT_URL, $endpoint . "?" . $data . $this->appendAuth()); |
| 94 | + curl_setopt($c, CURLOPT_RETURNTRANSFER, true); |
| 95 | + break; |
| 96 | + } |
| 97 | + |
| 98 | + $response = curl_exec($c); |
| 99 | + if(!$response){ |
| 100 | + return curl_error($c); |
| 101 | + } |
| 102 | + curl_close($c); |
| 103 | + if($this->checkResponse($response)){ |
| 104 | + return json_decode($response, true); |
| 105 | + } |
| 106 | + return [ |
| 107 | + "error" => "An error occurred", |
| 108 | + ]; |
| 109 | + } |
| 110 | + |
| 111 | + public function appendAuth($m = "POST"){ |
| 112 | + if($_ENV["TOKEN"] != ""){ |
| 113 | + switch($m){ |
| 114 | + case "POST": |
| 115 | + return "?token=" . $_ENV["TOKEN"] . "&user=" . $_ENV["USERNAME"] . "&password=" . $_ENV["PASSWORD"]; |
| 116 | + case "GET": |
| 117 | + return "&token=" . $_ENV["TOKEN"] . "&user=" . $_ENV["USERNAME"] . "&password=" . $_ENV["PASSWORD"]; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public function checkResponse($response){ |
| 123 | + if(is_null($response)){ |
| 124 | + return false; |
| 125 | + } else { |
| 126 | + $re = json_decode($response, true)["status"]; |
| 127 | + return $re != null || $re !== "error" || $re !== "invalid-token"; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + public function prepareEndpoint($endpoint){ |
| 132 | + $endpoints = json_decode(file_get_contents(__DIR__ . "/helper/endpoints.json")); |
| 133 | + |
| 134 | + if(in_array($endpoint, $endpoints)){ |
| 135 | + return $this->protocol . "://" . $_ENV["API_URL"] . "/api/" . $endpoint; |
| 136 | + } else { |
| 137 | + return false; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | + public function admin(): admin { |
| 143 | + if(!$this->admin) $this->admin = new admin($this); |
| 144 | + return $this->admin; |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + public function allowed(): allowed { |
| 149 | + if(!$this->allowed) $this->allowed = new allowed($this); |
| 150 | + return $this->allowed; |
| 151 | + } |
| 152 | + |
| 153 | + public function apps(): apps { |
| 154 | + if(!$this->apps) $this->apps = new apps($this); |
| 155 | + return $this->apps; |
| 156 | + } |
| 157 | + |
| 158 | + public function blocked(): blocked { |
| 159 | + if(!$this->blocked) $this->blocked = new blocked($this); |
| 160 | + return $this->blocked; |
| 161 | + } |
| 162 | + |
| 163 | + public function cache(): cache { |
| 164 | + if(!$this->cache) $this->cache = new cache($this); |
| 165 | + return $this->cache; |
| 166 | + } |
| 167 | + |
| 168 | + |
| 169 | + public function dashboard(): dashboard { |
| 170 | + if(!$this->dashboard) $this->dashboard = new dashboard($this); |
| 171 | + return $this->dashboard; |
| 172 | + } |
| 173 | + |
| 174 | + public function dhcp(): dhcp { |
| 175 | + if(!$this->dhcp) $this->dhcp = new dhcp($this); |
| 176 | + return $this->dhcp; |
| 177 | + } |
| 178 | + |
| 179 | + public function dnsClient(): dnsClient { |
| 180 | + if(!$this->dnsClient) $this->dnsClient = new dnsClient($this); |
| 181 | + return $this->dnsClient; |
| 182 | + } |
| 183 | + |
| 184 | + public function settings(): settings { |
| 185 | + if(!$this->settings) $this->settings = new settings($this); |
| 186 | + return $this->settings; |
| 187 | + } |
| 188 | + |
| 189 | + public function users(): users { |
| 190 | + if(!$this->users) $this->users = new users($this); |
| 191 | + return $this->users; |
| 192 | + } |
| 193 | + |
| 194 | + public function zones(): zones { |
| 195 | + if(!$this->zones) $this->zones = new zones($this); |
| 196 | + return $this->zones; |
| 197 | + } |
| 198 | + |
| 199 | + public function ddns(): DDNS { |
| 200 | + if(!$this->ddns) $this->ddns = new DDNS($this); |
| 201 | + return $this->ddns; |
| 202 | + } |
| 203 | + |
| 204 | + public function log(): Log { |
| 205 | + if(!$this->log) $this->log = new Log($this); |
| 206 | + return $this->log; |
| 207 | + } |
| 208 | +} |
0 commit comments