Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d78a82c14c
|
||
|
|
d504f1d5bb
|
||
|
|
5534f10cee
|
||
|
|
c1fe1bb0e0
|
||
|
|
ee7d9623a3
|
||
|
|
ecec258a64
|
||
|
|
66040d69db
|
||
|
|
fc2de8e1ab
|
||
|
|
6cb90d5ea2
|
||
|
|
e67afc401b
|
No files matched your search
@@ -4,6 +4,8 @@ use SokoWeb\Interfaces\Database\IConnection;
|
|||||||
use SokoWeb\Interfaces\Database\IResultSet;
|
use SokoWeb\Interfaces\Database\IResultSet;
|
||||||
use SokoWeb\Interfaces\Database\IStatement;
|
use SokoWeb\Interfaces\Database\IStatement;
|
||||||
use mysqli;
|
use mysqli;
|
||||||
|
use DateTime;
|
||||||
|
use DateTimeZone;
|
||||||
|
|
||||||
class Connection implements IConnection
|
class Connection implements IConnection
|
||||||
{
|
{
|
||||||
@@ -109,5 +111,16 @@ class Connection implements IConnection
|
|||||||
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
|
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
|
||||||
$this->connection = new mysqli($this->host, $this->user, $this->password, $this->db, $this->port, $this->socket);
|
$this->connection = new mysqli($this->host, $this->user, $this->password, $this->db, $this->port, $this->socket);
|
||||||
$this->connection->set_charset('utf8mb4');
|
$this->connection->set_charset('utf8mb4');
|
||||||
|
$this->connection->query('SET time_zone = \'' . $this->getTimeZone() . '\'');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getTimeZone(): string {
|
||||||
|
$tz = new DateTimeZone(date_default_timezone_get());
|
||||||
|
$offset = $tz->getOffset(new DateTime('now', new DateTimeZone('UTC')));
|
||||||
|
|
||||||
|
$hours = intdiv($offset, 3600);
|
||||||
|
$minutes = abs(($offset % 3600) / 60);
|
||||||
|
|
||||||
|
return sprintf("%+03d:%02d", $hours, $minutes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -29,13 +29,6 @@ class Mail
|
|||||||
{
|
{
|
||||||
$this->body = file_get_contents(ROOT . '/mail/' . $template . '.html');
|
$this->body = file_get_contents(ROOT . '/mail/' . $template . '.html');
|
||||||
|
|
||||||
$baseParameters = [
|
|
||||||
'APP_NAME' => $_ENV['APP_NAME'],
|
|
||||||
'BASE_URL' => $_ENV['APP_URL'],
|
|
||||||
];
|
|
||||||
|
|
||||||
$params = array_merge($baseParameters, $params);
|
|
||||||
|
|
||||||
foreach ($params as $key => $param) {
|
foreach ($params as $key => $param) {
|
||||||
$this->body = str_replace('{{' . $key . '}}', $param, $this->body);
|
$this->body = str_replace('{{' . $key . '}}', $param, $this->body);
|
||||||
}
|
}
|
||||||
@@ -51,8 +44,10 @@ class Mail
|
|||||||
if (!empty($_ENV['MAIL_HOST'])) {
|
if (!empty($_ENV['MAIL_HOST'])) {
|
||||||
$mailer->Mailer = 'smtp';
|
$mailer->Mailer = 'smtp';
|
||||||
$mailer->Host = $_ENV['MAIL_HOST'];
|
$mailer->Host = $_ENV['MAIL_HOST'];
|
||||||
$mailer->Port = !empty($_ENV['MAIL_PORT']) ? $_ENV['MAIL_PORT'] : 25;
|
$mailer->Port = !empty($_ENV['MAIL_PORT']) ? $_ENV['MAIL_PORT'] : 587;
|
||||||
$mailer->SMTPSecure = !empty($_ENV['MAIL_SECURE']) ? $_ENV['MAIL_SECURE'] : '';
|
|
||||||
|
$secureMaping = ['none' => '', 'starttls' => PHPMailer::ENCRYPTION_STARTTLS, 'smtps' => PHPMailer::ENCRYPTION_SMTPS];
|
||||||
|
$mailer->SMTPSecure = !empty($_ENV['MAIL_SECURE']) ? $secureMaping[$_ENV['MAIL_SECURE']] : '';
|
||||||
|
|
||||||
if (!empty($_ENV['MAIL_USER'])) {
|
if (!empty($_ENV['MAIL_USER'])) {
|
||||||
$mailer->SMTPAuth = true;
|
$mailer->SMTPAuth = true;
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ use SokoWeb\Interfaces\Response\IRedirect;
|
|||||||
use SokoWeb\Interfaces\Response\IContent;
|
use SokoWeb\Interfaces\Response\IContent;
|
||||||
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
|
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
|
||||||
use SokoWeb\Interfaces\Authorization\ISecured;
|
use SokoWeb\Interfaces\Authorization\ISecured;
|
||||||
|
use SokoWeb\Interfaces\Routing\IRouteCollection;
|
||||||
use SokoWeb\Interfaces\Database\IConnection;
|
use SokoWeb\Interfaces\Database\IConnection;
|
||||||
use SokoWeb\Interfaces\Request\IRequest;
|
use SokoWeb\Interfaces\Request\IRequest;
|
||||||
use SokoWeb\Response\Redirect;
|
use SokoWeb\Response\Redirect;
|
||||||
use SokoWeb\Response\HtmlContent;
|
use SokoWeb\Response\HtmlContent;
|
||||||
use SokoWeb\Response\JsonContent;
|
use SokoWeb\Response\JsonContent;
|
||||||
use SokoWeb\Routing\RouteCollection;
|
|
||||||
|
|
||||||
class HttpResponse
|
class HttpResponse
|
||||||
{
|
{
|
||||||
@@ -19,7 +19,7 @@ class HttpResponse
|
|||||||
|
|
||||||
private IConnection $dbConnection;
|
private IConnection $dbConnection;
|
||||||
|
|
||||||
private RouteCollection $routeCollection;
|
private IRouteCollection $routeCollection;
|
||||||
|
|
||||||
private array $appConfig;
|
private array $appConfig;
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ class HttpResponse
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
IRequest $request,
|
IRequest $request,
|
||||||
IConnection $dbConnection,
|
IConnection $dbConnection,
|
||||||
RouteCollection $routeCollection,
|
IRouteCollection $routeCollection,
|
||||||
array $appConfig,
|
array $appConfig,
|
||||||
string $requestMethod,
|
string $requestMethod,
|
||||||
string $requestUrl
|
string $requestUrl
|
||||||
@@ -55,6 +55,11 @@ class HttpResponse
|
|||||||
|
|
||||||
public function render(): void
|
public function render(): void
|
||||||
{
|
{
|
||||||
|
$this->handleCors();
|
||||||
|
if ($this->method === 'options') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$match = $this->routeCollection->match($this->method, $this->parsedUrl['path']);
|
$match = $this->routeCollection->match($this->method, $this->parsedUrl['path']);
|
||||||
if ($match === null) {
|
if ($match === null) {
|
||||||
$this->render404();
|
$this->render404();
|
||||||
@@ -110,10 +115,63 @@ class HttpResponse
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function handleCors(): void
|
||||||
|
{
|
||||||
|
$origin = $this->request->header('Origin');
|
||||||
|
if (!$origin) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($this->appConfig['cors']['allow_origins'])) {
|
||||||
|
if (in_array($origin, $this->appConfig['cors']['allow_origins']) || in_array('*', $this->appConfig['cors']['allow_origins'])) {
|
||||||
|
header("Access-Control-Allow-Origin: {$origin}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($this->appConfig['cors']['allow_credentials'])) {
|
||||||
|
header('Access-Control-Allow-Credentials: true');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->method !== 'options') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($this->appConfig['cors']['allow_headers'])) {
|
||||||
|
$headers = explode(',', $this->request->header('Access-Control-Request-Headers'));
|
||||||
|
if (in_array('*', $this->appConfig['cors']['allow_headers'])) {
|
||||||
|
$allow_headers = $headers;
|
||||||
|
} else {
|
||||||
|
$allow_headers = array_intersect($this->appConfig['cors']['allow_headers'], $headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count($allow_headers) > 0) {
|
||||||
|
header('Access-Control-Allow-Headers: ' . join(', ', $allow_headers));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($this->appConfig['cors']['allow_methods'])) {
|
||||||
|
if (in_array('*', $this->appConfig['cors']['allow_methods'])) {
|
||||||
|
$allow_methods = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT'];
|
||||||
|
} else {
|
||||||
|
$allow_methods = $this->appConfig['cors']['allow_methods'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count($allow_methods) > 0) {
|
||||||
|
header('Access-Control-Allow-Methods: ' . join(', ', $allow_methods));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$max_age = $this->appConfig['cors']['max_age'] ?? 600;
|
||||||
|
header("Access-Control-Max-Age: {$max_age}");
|
||||||
|
}
|
||||||
|
|
||||||
private function redirectToLogin(): void
|
private function redirectToLogin(): void
|
||||||
{
|
{
|
||||||
$this->request->session()->set('redirect_after_login', $this->rawUrl);
|
$this->request->session()->set('redirect_after_login', $this->rawUrl);
|
||||||
$response = new Redirect($this->routeCollection->getRoute($this->appConfig['loginRouteId'])->generateLink(), IRedirect::TEMPORARY);
|
$response = new Redirect(
|
||||||
|
$this->routeCollection->getRoute($this->appConfig['loginRouteId'])
|
||||||
|
->generateLink(['redirect_after_login' => $this->rawUrl]),
|
||||||
|
IRedirect::TEMPORARY);
|
||||||
header('Location: ' . $this->getRedirectUrl($response), true, $response->getHttpCode());
|
header('Location: ' . $this->getRedirectUrl($response), true, $response->getHttpCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ class Route implements IRoute
|
|||||||
foreach ($this->pattern as $fragment) {
|
foreach ($this->pattern as $fragment) {
|
||||||
if (preg_match('/^{(\\w+)(\\?)?}$/', $fragment, $matches) === 1) {
|
if (preg_match('/^{(\\w+)(\\?)?}$/', $fragment, $matches) === 1) {
|
||||||
if (isset($parameters[$matches[1]])) {
|
if (isset($parameters[$matches[1]])) {
|
||||||
$link[] = $parameters[$matches[1]];
|
$link[] = rawurlencode($parameters[$matches[1]]);
|
||||||
unset($parameters[$matches[1]]);
|
unset($parameters[$matches[1]]);
|
||||||
} elseif (!isset($matches[2])) {//TODO: why? parameter not found but not optional
|
} elseif (!isset($matches[2])) {//TODO: why? parameter not found but not optional
|
||||||
$link[] = $fragment;
|
$link[] = $fragment;
|
||||||
@@ -53,7 +53,7 @@ class Route implements IRoute
|
|||||||
$queryParams[$key] = $value;
|
$queryParams[$key] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
$query = count($queryParams) > 0 ? '?' . http_build_query($queryParams) : '';
|
$query = count($queryParams) > 0 ? '?' . http_build_query($queryParams, encoding_type: PHP_QUERY_RFC3986) : '';
|
||||||
|
|
||||||
return '/' . implode('/', $link) . $query;
|
return '/' . implode('/', $link) . $query;
|
||||||
}
|
}
|
||||||
@@ -64,7 +64,7 @@ class Route implements IRoute
|
|||||||
|
|
||||||
foreach ($path as $i => $fragment) {
|
foreach ($path as $i => $fragment) {
|
||||||
if (preg_match('/^{(\\w+)(?:\\?)?}$/', $this->pattern[$i], $matches) === 1) {
|
if (preg_match('/^{(\\w+)(?:\\?)?}$/', $this->pattern[$i], $matches) === 1) {
|
||||||
$parameters[$matches[1]] = $fragment;
|
$parameters[$matches[1]] = rawurldecode($fragment);
|
||||||
} elseif ($fragment != $this->pattern[$i]) {
|
} elseif ($fragment != $this->pattern[$i]) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user