mirror of
https://github.com/rekryt/iplist.git
synced 2025-10-13 00:49:36 +03:00
feat: new frontend
This commit is contained in:
@@ -23,7 +23,8 @@ abstract class AbstractController implements ControllerInterface {
|
||||
* @return Response
|
||||
*/
|
||||
public function __invoke(): Response {
|
||||
return new Response(status: $this->httpStatus, headers: $this->headers, body: $this->getBody());
|
||||
$body = $this->getBody();
|
||||
return new Response(status: $this->httpStatus, headers: $this->headers, body: $body);
|
||||
}
|
||||
|
||||
abstract public function getBody(): string;
|
||||
|
@@ -2,16 +2,30 @@
|
||||
|
||||
namespace OpenCCK\App\Controller;
|
||||
|
||||
use OpenCCK\Domain\Entity\Site;
|
||||
use OpenCCK\Domain\Factory\SiteFactory;
|
||||
use Amp\Http\HttpStatus;
|
||||
use function Amp\File\isFile;
|
||||
use function Amp\File\read;
|
||||
use function finfo_close;
|
||||
use function finfo_file;
|
||||
use function finfo_open;
|
||||
|
||||
class MainController extends AbstractIPListController {
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBody(): string {
|
||||
$this->setHeaders(['content-type' => 'text/html; charset=utf-8']);
|
||||
return $this->renderTemplate('index');
|
||||
$url = parse_url($this->request->getUri());
|
||||
$path = str_replace('..', '', $url['path']);
|
||||
$path = PATH_ROOT . '/public/' . $path;
|
||||
|
||||
foreach ([$path, $path . 'index.html', $path . '.html', $path . '/index.html'] as $filePath) {
|
||||
if (isFile($filePath)) {
|
||||
return $this->output($filePath);
|
||||
}
|
||||
}
|
||||
|
||||
$this->setHttpStatus(HttpStatus::NOT_FOUND);
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -23,4 +37,27 @@ class MainController extends AbstractIPListController {
|
||||
include PATH_ROOT . '/src/App/Template/' . ucfirst($template) . 'Template.php';
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
private function output(string $path): string {
|
||||
$mimeTypes = [
|
||||
'js' => 'application/javascript',
|
||||
'json' => 'application/json',
|
||||
'css' => 'text/css',
|
||||
];
|
||||
$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
|
||||
if (isset($mimeTypes[$ext])) {
|
||||
$contentType = $mimeTypes[$ext];
|
||||
} else {
|
||||
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
$contentType = finfo_file($finfo, $path);
|
||||
finfo_close($finfo);
|
||||
}
|
||||
|
||||
$this->setHeaders(['content-type' => $contentType]);
|
||||
return read($path);
|
||||
}
|
||||
}
|
||||
|
28
src/App/Controller/ScriptsController.php
Normal file
28
src/App/Controller/ScriptsController.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace OpenCCK\App\Controller;
|
||||
|
||||
use Amp\Http\HttpStatus;
|
||||
use function Amp\File\isFile;
|
||||
use function Amp\File\read;
|
||||
use function mime_content_type;
|
||||
|
||||
class ScriptsController extends AbstractIPListController {
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBody(): string {
|
||||
$url = parse_url($this->request->getUri());
|
||||
$path = str_replace('..', '', $url['path']);
|
||||
$path = implode('/', array_slice(explode('/', $path), 2));
|
||||
$path = PATH_ROOT . '/scripts/' . $path;
|
||||
|
||||
if (isFile($path) && ($mimeType = mime_content_type($path))) {
|
||||
$this->setHeaders(['content-type' => $mimeType]);
|
||||
return read($path);
|
||||
}
|
||||
|
||||
$this->setHttpStatus(HttpStatus::NOT_FOUND);
|
||||
return '';
|
||||
}
|
||||
}
|
@@ -21,11 +21,11 @@ class TextController extends AbstractIPListController {
|
||||
$response = [];
|
||||
if (count($sites)) {
|
||||
foreach ($sites as $site) {
|
||||
$response = array_merge($response, $this->getSites()[$site]->$data);
|
||||
$response = array_merge($response, $this->getSites()[$site]->$data ?? []);
|
||||
}
|
||||
} else {
|
||||
foreach ($this->getSites() as $siteEntity) {
|
||||
$response = array_merge($response, $siteEntity->$data);
|
||||
$response = array_merge($response, $siteEntity->$data ?? []);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user