Compare commits

..

No commits in common. "53fc1456d14cfceec6c7c0f5e5676e40face5135" and "36e376337ee61f1a8c3dfea398284bff20657c91" have entirely different histories.

2 changed files with 32 additions and 75 deletions

View File

@ -1,9 +1,6 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
use Exception;
error_reporting(error_level: E_ALL); error_reporting(error_level: E_ALL);
require dirname(path: __DIR__) . '/vendor/autoload.php'; require dirname(path: __DIR__) . '/vendor/autoload.php';
@ -22,14 +19,15 @@ header(header: "Access-Control-Max-Age: 3600");
header(header: "Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"); header(header: "Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// TODO make a log class // TODO make a log class
$oFile = fopen(filename: 'log.txt', mode: 'a'); $oFile = fopen (filename: 'log.txt', mode: 'a');
$uri = parse_url(url: $_SERVER['REQUEST_URI'], component: PHP_URL_PATH); $uri = parse_url(url: $_SERVER['REQUEST_URI'], component: PHP_URL_PATH);
fputs(stream: $oFile, data: $uri . PHP_EOL); fputs(stream: $oFile, data: $uri . PHP_EOL);
fclose(stream: $oFile); fclose(stream: $oFile);
$uri = explode(separator: '/', string: $uri); $uri = explode( separator: '/', string: $uri );
if ($uri[1] !== 'api') { if ($uri[1] !== 'api') {
header(header: "HTTP/1.1 404 Not Found"); header(header: "HTTP/1.1 404 Not Found");
exit(); exit();
@ -37,13 +35,6 @@ if ($uri[1] !== 'api') {
$requestMethod = $_SERVER["REQUEST_METHOD"]; $requestMethod = $_SERVER["REQUEST_METHOD"];
try { $controller = new RequestController(config: $config, requestMethod: $requestMethod, uri: $uri);
$controller = new RequestController(config: $config, requestMethod: $requestMethod, uri: $uri); $controller->processRequest();
$controller->processRequest();
} catch (Exception $e) {
echo json_encode(value: [
'error' => $e->getMessage()
]);
}

View File

@ -1,50 +1,34 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
error_reporting(error_level: E_ALL); error_reporting(error_level: E_ALL);
use App\Repository\ApikeyRepository;
use App\Repository\DomainRepository;
use DI\Container;
use DI\ContainerBuilder;
use UnhandledMatchError; use UnhandledMatchError;
use function DI\autowire;
/** /**
* *
*/ */
class RequestController class RequestController
{ {
//private DatabaseConnection $databaseConnection; private DatabaseConnection $databaseConnection;
private DomainRepository $domainRepository; private DomainController $domainController;
private ApikeyRepository $apikeyRepository; private PanelController $panelController;
private Container $container; private String $header;
private string $header;
private array $result; private array $result;
private string $status; private String $status;
private string $message; private String $message;
/** /**
* @param array $config * @param array $config
* @param String $requestMethod * @param String $requestMethod
* @param array $uri * @param array $uri
*
* @throws \Exception
*/ */
public function __construct(private array $config, private string $requestMethod, private array $uri) public function __construct(private array $config, private String $requestMethod, private array $uri)
{ {
$this->requestMethod = strtoupper(string: $requestMethod); $this->requestMethod = strtoupper(string: $requestMethod);
$this->databaseConnection = new DatabaseConnection(config: $this->config);
$containerBuilder = new ContainerBuilder(); $this->panelController = new PanelController(databaseConnection: $this->databaseConnection);
$containerBuilder->addDefinitions([ $this->domainController = new DomainController(databaseConnection: $this->databaseConnection, panelController: $this->panelController);
DatabaseConnection::class => autowire()->constructorParameter(parameter: 'config', value: $this->config),
]);
$this->container = $containerBuilder->build();
$this->domainRepository = $this->container->get(name: DomainRepository::class);
$this->apikeyRepository = $this->container->get(name: ApikeyRepository::class);
} }
@ -70,7 +54,7 @@ class RequestController
'PUT' => $this->handleDomainPutRequest(), 'PUT' => $this->handleDomainPutRequest(),
'DELETE' => $this->handleDomainDeleteRequest() 'DELETE' => $this->handleDomainDeleteRequest()
}; };
} catch (UnhandledMatchError) { } catch(UnhandledMatchError) {
$this->header = '400 Bad Request'; $this->header = '400 Bad Request';
$this->status = '400 Bad Request'; $this->status = '400 Bad Request';
$this->message = "unknown request method: $this->requestMethod"; $this->message = "unknown request method: $this->requestMethod";
@ -79,7 +63,7 @@ class RequestController
} }
} }
if (!empty($this->header)) { if(!empty($this->header)) {
header(header: $_SERVER['SERVER_PROTOCOL'] . ' ' . $this->header); header(header: $_SERVER['SERVER_PROTOCOL'] . ' ' . $this->header);
} }
if (!empty($this->result)) { if (!empty($this->result)) {
@ -114,8 +98,9 @@ class RequestController
return false; return false;
} else { } else {
[$prefix,] = explode(separator: '.', string: $apiKey); [$prefix,] = explode(separator: '.', string: $apiKey);
if ($apiResult = $this->apikeyRepository->findByPrefix(prefix: $prefix)) { $apiUsers = new ApiKeys(databaseConnection: $this->databaseConnection);
$storedHash = $apiResult->getApiToken(); if ($apiResult = $apiUsers->findByPrefix(prefix: $prefix)) {
$storedHash = $apiResult['api_token'];
if (!password_verify(password: $apiKey, hash: $storedHash)) { if (!password_verify(password: $apiKey, hash: $storedHash)) {
$this->header = "401 Unauthorized"; $this->header = "401 Unauthorized";
$this->status = "401 Unauthorized"; $this->status = "401 Unauthorized";
@ -125,7 +110,7 @@ class RequestController
} else { } else {
$this->header = "401 Unauthorized"; $this->header = "401 Unauthorized";
$this->status = "401 Unauthorized"; $this->status = "401 Unauthorized";
$this->message = "Invalid API key."; $this->message = "API key not found.";
return false; return false;
} }
} }
@ -138,29 +123,10 @@ class RequestController
public function handleDomainGetRequest(): void public function handleDomainGetRequest(): void
{ {
if (empty($this->uri[3])) { if (empty($this->uri[3])) {
$domains = $this->domainRepository->findAll(); $this->result = $this->domainController->findAll();
$resultDomain = [];
foreach ($domains as $singleDomain) {
$domain = [
'id' => $singleDomain->getId(),
'name' => $singleDomain->getName(),
'panel_id' => $singleDomain->getPanelId(),
'a' => $singleDomain->getA(),
'aaaa' => $singleDomain->getAaaa()
];
$resultDomain[] = $domain;
}
$this->result = $resultDomain;
} else { } else {
if ($result = $this->domainRepository->findByName(name: $this->uri[3])) { if ($result = $this->domainController->findByName(name: $this->uri[3])) {
$domain = [ $this->result = $result;
'id' => $result->getId(),
'name' => $result->getName(),
'panel_id' => $result->getPanelId(),
'a' => $result->getA(),
'aaaa' => $result->getAaaa()
];
$this->result = $domain;
} else { } else {
$this->header = "404 Not Found "; $this->header = "404 Not Found ";
$this->status = "404 Not Found "; $this->status = "404 Not Found ";
@ -189,12 +155,12 @@ class RequestController
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "At least one IP address or panel ID is required."; $this->message = "At least one IP address or panel ID is required.";
} else { } else {
if ($this->domainRepository->findByName(name: $name)) { if ($this->domainController->findByName(name: $name)) {
$this->header = "400 Bad request"; $this->header = "400 Bad request";
$this->status = "400 Bad request"; $this->status = "400 Bad request";
$this->message = "Domain: $name already exists."; $this->message = "Domain: $name already exists.";
} else { } else {
$result = $this->domainRepository->insert(name: $name, panelID: $panelID, a: $a, aaaa: $aaaa); $result = $this->domainController->insert(name: $name, panelID: $panelID, a: $a, aaaa: $aaaa);
$this->status = "201 Created"; $this->status = "201 Created";
$this->message = $result; $this->message = $result;
} }
@ -226,7 +192,7 @@ class RequestController
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "An ID is required"; $this->message = "An ID is required";
} else { } else {
if (!$this->domainRepository->findByID(id: $id)) { if (!$this->domainController->findByID(id: $id)) {
$this->status = "404 Not Found"; $this->status = "404 Not Found";
$this->message = "Domain with ID : $id doesn't exist."; $this->message = "Domain with ID : $id doesn't exist.";
} else { } else {
@ -239,7 +205,7 @@ class RequestController
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "At least one IP address is required."; $this->message = "At least one IP address is required.";
} else { } else {
$this->domainRepository->update(id: $id, name: $panelID, panelID: $name, a: $a, aaaa: $aaaa); $dcResult = $this->domainController->update(id: $id, name: $panelID, panelID: $name, a: $a, aaaa: $aaaa);
$this->header = "201 Updated"; $this->header = "201 Updated";
$this->status = "201 Updated"; $this->status = "201 Updated";
$this->message = "201 Updated"; $this->message = "201 Updated";
@ -270,12 +236,12 @@ class RequestController
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "You need to supply an ID."; $this->message = "You need to supply an ID.";
} else { } else {
if (!$this->domainRepository->findByID(id: $id)) { if (!$this->domainController->findByID(id: $id)) {
$this->header = "400 Bad Request"; $this->header = "400 Bad Request";
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "There is no domain with ID $id."; $this->message = "There is no domain with ID $id.";
} else { } else {
$this->domainRepository->delete(id: $id); $this->domainController->delete(id: $id);
$this->header = "204 No content."; $this->header = "204 No content.";
$this->status = "204 No content."; $this->status = "204 No content.";
$this->message = "The domain $id has been deleted."; $this->message = "The domain $id has been deleted.";