19 Commits
Author SHA1 Message Date
bence d504f1d5bb encode and decode parameters in routes
soko-web/pipeline/head This commit looks good
2024-11-08 12:21:54 +01:00
bence 5534f10cee use RFC3986 for query parameter encoding 2024-11-08 12:21:13 +01:00
bence c1fe1bb0e0 do not encode query parameters 2024-11-08 12:20:30 +01:00
bence ee7d9623a3 set redirect_after_login in query parameter as well
soko-web/pipeline/head This commit looks good
2024-10-24 22:15:22 +02:00
bence ecec258a64 allow starttls and smtps for mailing 2024-10-19 22:44:51 +02:00
bence 66040d69db use IRouteCollection for HttpResponse
soko-web/pipeline/head This commit looks good
2024-10-18 18:18:13 +02:00
bence fc2de8e1ab do not use $_ENV vars for mail template 2024-10-18 18:05:53 +02:00
bence 6cb90d5ea2 Merge pull request 'implement cors' (#30) from implement-cors into master
soko-web/pipeline/head This commit looks good
Reviewed-on: #30
2024-08-02 02:07:28 +02:00
bence e67afc401b implement cors
soko-web/pipeline/pr-master This commit looks good
2024-08-02 01:42:30 +02:00
bence e59d627080 Merge pull request 'feature/update-to-php81' (#29) from feature/update-to-php81 into master
soko-web/pipeline/head This commit looks good
Reviewed-on: #29
2023-09-27 22:11:05 +02:00
bence 3acca19d49 use new interface of TestCase
soko-web/pipeline/pr-master This commit looks good
2023-09-27 22:05:56 +02:00
bence 2226b88a88 adapt signature of DatabaseSessionHandler::gc to the parent class
soko-web/pipeline/pr-master There was a failure building this commit
2023-09-27 21:46:32 +02:00
bence 8e08b09ae8 generate composer.lock
soko-web/pipeline/pr-master There was a failure building this commit
2023-09-27 21:41:17 +02:00
bence a3bce1f2aa update composer packages 2023-09-27 21:38:59 +02:00
bence a84d3a3976 update to php 8.1 2023-09-27 21:35:44 +02:00
bence 7210b24aa3 Merge pull request 'log erros that were already caught' (#28) from feature/error-log into master
soko-web/pipeline/head This commit looks good
Reviewed-on: #28
2023-09-27 00:26:55 +02:00
bence 2d48f20aed log erros that were already caught
soko-web/pipeline/pr-master This commit looks good
2023-09-27 00:04:26 +02:00
bence ebe1fa2aa6 Merge pull request 'lazy create mysql connecion' (#27) from feature/lazy-create-mysql-connection into master
soko-web/pipeline/head This commit looks good
Reviewed-on: #27
2023-09-16 23:56:41 +02:00
bence bccee89c13 lazy create mysql connecion
soko-web/pipeline/pr-master This commit looks good
2023-09-16 13:18:42 +02:00
9 changed files with 489 additions and 618 deletions

No files matched your search

+1 -1
View File
@@ -1,4 +1,4 @@
FROM php:7.4.7-cli-buster
FROM php:8.1-cli-bookworm
RUN apt-get update && apt-get install -y unzip
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
+3 -3
View File
@@ -5,12 +5,12 @@
"license": "GNU GPL 3.0",
"require": {
"vlucas/phpdotenv": "^5.5",
"symfony/console": "^5.4",
"symfony/console": "^6.3",
"phpmailer/phpmailer": "^6.8",
"cocur/slugify": "^4.3"
"cocur/slugify": "^4.5"
},
"require-dev": {
"phpunit/phpunit": "^9.6",
"phpunit/phpunit": "^10.3",
"phpstan/phpstan": "^1.10"
},
"autoload": {
Generated
+366 -568
View File
File diff suppressed because it is too large. Load diff
+44 -25
View File
@@ -7,48 +7,52 @@ use mysqli;
class Connection implements IConnection
{
private mysqli $connection;
private string $host;
private string $user;
private string $password;
private string $db;
private int $port;
private string $socket;
private ?mysqli $connection = null;
public function __construct(string $host, string $user, string $password, string $db, int $port = -1, string $socket = null)
{
if ($port < 0) {
$port = (int) ini_get('mysqli.default_port');
}
if ($socket === null) {
$socket = (string) ini_get('mysqli.default_socket');
}
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$this->connection = new mysqli($host, $user, $password, $db, $port, $socket);
$this->connection->set_charset('utf8mb4');
$this->host = $host;
$this->user = $user;
$this->password = $password;
$this->db = $db;
$this->port = $port < 0 ? (int) ini_get('mysqli.default_port') : $port;
$this->socket = $socket === null ? (string) ini_get('mysqli.default_socket') : $socket;
}
public function __destruct()
{
if ($this->connection === null) {
return;
}
$this->connection->close();
}
public function startTransaction(): void
{
$this->connection->autocommit(false);
$this->getConnection()->autocommit(false);
}
public function commit(): void
{
$this->connection->commit();
$this->connection->autocommit(true);
$this->getConnection()->commit();
$this->getConnection()->autocommit(true);
}
public function rollback(): void
{
$this->connection->rollback();
$this->connection->autocommit(true);
$this->getConnection()->rollback();
$this->getConnection()->autocommit(true);
}
public function query(string $query): ?IResultSet
{
$result = $this->connection->query($query);
$result = $this->getConnection()->query($query);
if ($result !== true) {
return new ResultSet($result);
@@ -59,36 +63,51 @@ class Connection implements IConnection
public function multiQuery(string $query): array
{
$this->connection->multi_query($query);
$this->getConnection()->multi_query($query);
$ret = [];
do {
if ($result = $this->connection->store_result()) {
if ($result = $this->getConnection()->store_result()) {
$ret[] = new ResultSet($result);
} else {
$ret[] = null;
}
$this->connection->more_results();
} while ($this->connection->next_result());
$this->getConnection()->more_results();
} while ($this->getConnection()->next_result());
return $ret;
}
public function prepare(string $query): IStatement
{
$stmt = $this->connection->prepare($query);
$stmt = $this->getConnection()->prepare($query);
return new Statement($stmt);
}
public function lastId(): int
{
return $this->connection->insert_id;
return $this->getConnection()->insert_id;
}
public function getAffectedRows(): int
{
return $this->connection->affected_rows;
return $this->getConnection()->affected_rows;
}
private function getConnection(): mysqli
{
if ($this->connection === null) {
$this->createConnection();
}
return $this->connection;
}
private function createConnection(): void
{
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->set_charset('utf8mb4');
}
}
+4 -9
View File
@@ -29,13 +29,6 @@ class Mail
{
$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) {
$this->body = str_replace('{{' . $key . '}}', $param, $this->body);
}
@@ -51,8 +44,10 @@ class Mail
if (!empty($_ENV['MAIL_HOST'])) {
$mailer->Mailer = 'smtp';
$mailer->Host = $_ENV['MAIL_HOST'];
$mailer->Port = !empty($_ENV['MAIL_PORT']) ? $_ENV['MAIL_PORT'] : 25;
$mailer->SMTPSecure = !empty($_ENV['MAIL_SECURE']) ? $_ENV['MAIL_SECURE'] : '';
$mailer->Port = !empty($_ENV['MAIL_PORT']) ? $_ENV['MAIL_PORT'] : 587;
$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'])) {
$mailer->SMTPAuth = true;
+63 -4
View File
@@ -6,12 +6,12 @@ use SokoWeb\Interfaces\Response\IRedirect;
use SokoWeb\Interfaces\Response\IContent;
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
use SokoWeb\Interfaces\Authorization\ISecured;
use SokoWeb\Interfaces\Routing\IRouteCollection;
use SokoWeb\Interfaces\Database\IConnection;
use SokoWeb\Interfaces\Request\IRequest;
use SokoWeb\Response\Redirect;
use SokoWeb\Response\HtmlContent;
use SokoWeb\Response\JsonContent;
use SokoWeb\Routing\RouteCollection;
class HttpResponse
{
@@ -19,7 +19,7 @@ class HttpResponse
private IConnection $dbConnection;
private RouteCollection $routeCollection;
private IRouteCollection $routeCollection;
private array $appConfig;
@@ -32,7 +32,7 @@ class HttpResponse
public function __construct(
IRequest $request,
IConnection $dbConnection,
RouteCollection $routeCollection,
IRouteCollection $routeCollection,
array $appConfig,
string $requestMethod,
string $requestUrl
@@ -55,6 +55,11 @@ class HttpResponse
public function render(): void
{
$this->handleCors();
if ($this->method === 'options') {
return;
}
$match = $this->routeCollection->match($this->method, $this->parsedUrl['path']);
if ($match === null) {
$this->render404();
@@ -94,6 +99,7 @@ class HttpResponse
$response = call_user_func([$controller, $handler[1]]);
} catch (Exception $exception) {
$this->dbConnection->rollback();
error_log($exception);
$this->render500($exception);
return;
}
@@ -109,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
{
$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());
}
+3 -3
View File
@@ -34,7 +34,7 @@ class Route implements IRoute
foreach ($this->pattern as $fragment) {
if (preg_match('/^{(\\w+)(\\?)?}$/', $fragment, $matches) === 1) {
if (isset($parameters[$matches[1]])) {
$link[] = $parameters[$matches[1]];
$link[] = rawurlencode($parameters[$matches[1]]);
unset($parameters[$matches[1]]);
} elseif (!isset($matches[2])) {//TODO: why? parameter not found but not optional
$link[] = $fragment;
@@ -53,7 +53,7 @@ class Route implements IRoute
$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;
}
@@ -64,7 +64,7 @@ class Route implements IRoute
foreach ($path as $i => $fragment) {
if (preg_match('/^{(\\w+)(?:\\?)?}$/', $this->pattern[$i], $matches) === 1) {
$parameters[$matches[1]] = $fragment;
$parameters[$matches[1]] = rawurldecode($fragment);
} elseif ($fragment != $this->pattern[$i]) {
return null;
}
+2 -2
View File
@@ -86,12 +86,12 @@ class DatabaseSessionHandler implements ISessionHandler
return true;
}
public function gc($maxlifetime): bool
public function gc($maxlifetime): int|false
{
// empty on purpose
// old sessions are deleted by MaintainDatabaseCommand
return true;
return 1;
}
public function create_sid(): string
+3 -3
View File
@@ -17,7 +17,7 @@ final class GoogleOAuthTest extends TestCase
$redirectUrl = 'http://example.com/oauth';
$requestMock = $this->getMockBuilder(IRequest::class)
->setMethods(['setUrl', 'setMethod', 'setQuery', 'setHeaders', 'send'])
->onlyMethods(['setUrl', 'setMethod', 'setQuery', 'setHeaders', 'send'])
->getMock();
$googleOAuth = new GoogleOAuth($requestMock);
@@ -48,10 +48,10 @@ final class GoogleOAuthTest extends TestCase
$redirectUrl = 'http://example.com/oauth';
$requestMock = $this->getMockBuilder(IRequest::class)
->setMethods(['setUrl', 'setMethod', 'setQuery', 'setHeaders', 'send'])
->onlyMethods(['setUrl', 'setMethod', 'setQuery', 'setHeaders', 'send'])
->getMock();
$responseMock = $this->getMockBuilder(IResponse::class)
->setMethods(['getBody', 'getHeaders'])
->onlyMethods(['getBody', 'getHeaders'])
->getMock();
$googleOAuth = new GoogleOAuth($requestMock);