Compare commits

..

No commits in common. "master" and "v0.10.1" have entirely different histories.

17 changed files with 637 additions and 746 deletions

View File

@ -1,4 +1,4 @@
FROM php:8.1-cli-bookworm
FROM php:7.4.7-cli-buster
RUN apt-get update && apt-get install -y unzip
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

View File

@ -5,12 +5,11 @@
"license": "GNU GPL 3.0",
"require": {
"vlucas/phpdotenv": "^5.5",
"symfony/console": "^6.3",
"phpmailer/phpmailer": "^6.8",
"cocur/slugify": "^4.5"
"symfony/console": "^5.4",
"phpmailer/phpmailer": "^6.8"
},
"require-dev": {
"phpunit/phpunit": "^10.3",
"phpunit/phpunit": "^9.6",
"phpstan/phpstan": "^1.10"
},
"autoload": {

1002
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -4,57 +4,51 @@ use SokoWeb\Interfaces\Database\IConnection;
use SokoWeb\Interfaces\Database\IResultSet;
use SokoWeb\Interfaces\Database\IStatement;
use mysqli;
use DateTime;
use DateTimeZone;
class Connection implements IConnection
{
private string $host;
private string $user;
private string $password;
private string $db;
private int $port;
private string $socket;
private ?mysqli $connection = null;
private mysqli $connection;
public function __construct(string $host, string $user, string $password, string $db, int $port = -1, string $socket = null)
{
$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;
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');
}
public function __destruct()
{
if ($this->connection === null) {
return;
}
$this->connection->close();
}
public function startTransaction(): void
{
$this->getConnection()->autocommit(false);
$this->connection->autocommit(false);
}
public function commit(): void
{
$this->getConnection()->commit();
$this->getConnection()->autocommit(true);
$this->connection->commit();
$this->connection->autocommit(true);
}
public function rollback(): void
{
$this->getConnection()->rollback();
$this->getConnection()->autocommit(true);
$this->connection->rollback();
$this->connection->autocommit(true);
}
public function query(string $query): ?IResultSet
{
$result = $this->getConnection()->query($query);
$result = $this->connection->query($query);
if ($result !== true) {
return new ResultSet($result);
@ -65,62 +59,36 @@ class Connection implements IConnection
public function multiQuery(string $query): array
{
$this->getConnection()->multi_query($query);
$this->connection->multi_query($query);
$ret = [];
do {
if ($result = $this->getConnection()->store_result()) {
if ($result = $this->connection->store_result()) {
$ret[] = new ResultSet($result);
} else {
$ret[] = null;
}
$this->getConnection()->more_results();
} while ($this->getConnection()->next_result());
$this->connection->more_results();
} while ($this->connection->next_result());
return $ret;
}
public function prepare(string $query): IStatement
{
$stmt = $this->getConnection()->prepare($query);
$stmt = $this->connection->prepare($query);
return new Statement($stmt);
}
public function lastId(): int
{
return $this->getConnection()->insert_id;
return $this->connection->insert_id;
}
public function getAffectedRows(): int
{
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');
$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);
return $this->connection->affected_rows;
}
}

View File

@ -97,28 +97,28 @@ class Select
return $this;
}
public function where($column, ?string $relation = null, $value = null): Select
public function where($column, string $relation = null, $value = null): Select
{
$this->addWhereCondition('AND', $column, $relation, $value);
return $this;
}
public function orWhere($column, ?string $relation = null, $value = null): Select
public function orWhere($column, string $relation = null, $value = null): Select
{
$this->addWhereCondition('OR', $column, $relation, $value);
return $this;
}
public function having($column, ?string $relation = null, $value = null): Select
public function having($column, string $relation = null, $value = null): Select
{
$this->addHavingCondition('AND', $column, $relation, $value);
return $this;
}
public function orHaving($column, ?string $relation = null, $value = null): Select
public function orHaving($column, string $relation = null, $value = null): Select
{
$this->addHavingCondition('OR', $column, $relation, $value);
@ -211,12 +211,12 @@ class Select
$this->joins[] = [$type, $table, $column1, $relation, $column2];
}
private function addWhereCondition(string $logic, $column, ?string $relation, $value): void
private function addWhereCondition(string $logic, $column, string $relation, $value): void
{
$this->conditions[self::CONDITION_WHERE][] = [$logic, $column, $relation, $value];
}
private function addHavingCondition(string $logic, $column, ?string $relation, $value): void
private function addHavingCondition(string $logic, $column, string $relation, $value): void
{
$this->conditions[self::CONDITION_HAVING][] = [$logic, $column, $relation, $value];
}

View File

@ -7,15 +7,13 @@ class Request implements IRequest
{
private string $url;
private ?string $method = null;
private int $method;
private string $query = '';
private ?string $body = null;
private array $headers = [];
public function __construct(string $url = '', ?string $method = null)
public function __construct(string $url = '', int $method = self::HTTP_GET)
{
$this->url = $url;
$this->method = $method;
@ -26,7 +24,7 @@ class Request implements IRequest
$this->url = $url;
}
public function setMethod(string $method): void
public function setMethod(int $method): void
{
$this->method = $method;
}
@ -40,11 +38,6 @@ class Request implements IRequest
}
}
public function setBody(string $body): void
{
$this->body = $body;
}
public function setHeaders(array $headers): void
{
$this->headers = array_merge($this->headers, $headers);
@ -54,20 +47,13 @@ class Request implements IRequest
{
$ch = curl_init();
$url = $this->url . '?' . $this->query;
if ($this->method === self::HTTP_POST) {
$url = $this->url;
if ($this->body !== null) {
if ($this->method === null) {
$this->method = self::HTTP_POST;
}
if ($this->method === self::HTTP_POST) {
curl_setopt($ch, CURLOPT_POST, 1);
} else {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->query);
} else {
$url = $this->url . '?' . $this->query;
}
curl_setopt($ch, CURLOPT_URL, $url);

View File

@ -2,24 +2,16 @@
interface IRequest
{
const HTTP_GET = 'GET';
const HTTP_GET = 0;
const HTTP_POST = 'POST';
const HTTP_PUT = 'PUT';
const HTTP_PATCH = 'PATCH';
const HTTP_DELETE = 'DELETE';
const HTTP_POST = 1;
public function setUrl(string $url): void;
public function setMethod(string $method): void;
public function setMethod(int $method): void;
public function setQuery($query): void;
public function setBody(string $body): void;
public function setHeaders(array $headers): void;
public function send(): IResponse;

View File

@ -12,12 +12,8 @@ interface IPersistentDataManager
public function selectFromDbById($id, string $type, bool $useRelations = false, array $withRelations = []);
public function selectFromDbBySlug(string $slug, string $type, bool $useRelations = false, array $withRelations = []);
public function loadRelationsFromDb(Model $model, bool $recursive = false, array $withRelations = []): void;
public function loadMultiRelationsFromDb(array $models, string $relation, bool $useRelations = false, array $withRelations = []): void;
public function saveToDb(Model $model): void;
public function deleteFromDb(Model $model): void;

View File

@ -29,6 +29,13 @@ 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);
}
@ -44,10 +51,8 @@ class Mail
if (!empty($_ENV['MAIL_HOST'])) {
$mailer->Mailer = 'smtp';
$mailer->Host = $_ENV['MAIL_HOST'];
$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']] : '';
$mailer->Port = !empty($_ENV['MAIL_PORT']) ? $_ENV['MAIL_PORT'] : 25;
$mailer->SMTPSecure = !empty($_ENV['MAIL_SECURE']) ? $_ENV['MAIL_SECURE'] : '';
if (!empty($_ENV['MAIL_USER'])) {
$mailer->SMTPAuth = true;

View File

@ -8,8 +8,6 @@ abstract class Model
protected static array $relations = [];
protected static array $multiRelations = [];
protected $id = null;
private array $snapshot = [];
@ -29,11 +27,6 @@ abstract class Model
return static::$relations;
}
public static function getMultiRelations(): array
{
return static::$multiRelations;
}
public function setId($id): void
{
$this->id = $id;
@ -48,7 +41,7 @@ abstract class Model
{
$array = [];
foreach (static::getFields() as $key) {
foreach (self::getFields() as $key) {
$method = 'get' . str_replace('_', '', ucwords($key, '_'));
if (method_exists($this, $method)) {

View File

@ -1,32 +0,0 @@
<?php namespace SokoWeb\PersistentData\Model;
use Cocur\Slugify\Slugify;
abstract class ModelWithSlug extends Model
{
protected static string $slugSource;
protected ?string $slug = null;
public static function getFields(): array
{
return array_merge(['id', 'slug'], static::$fields);
}
public function setSlug(?string $slug): void
{
$this->slug = $slug;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function generateSlug(): string
{
$slugSourceGetMethod = 'get' . str_replace('_', '', ucwords(static::$slugSource, '_'));
$this->slug = Slugify::create()->slugify($this->$slugSourceGetMethod());
return $this->slug;
}
}

View File

@ -8,12 +8,9 @@ use SokoWeb\Interfaces\Database\IAuditLogger;
use SokoWeb\Interfaces\Database\IResultSet;
use SokoWeb\Interfaces\PersistentData\IPersistentDataManager;
use SokoWeb\PersistentData\Model\Model;
use SokoWeb\PersistentData\Model\ModelWithSlug;
class PersistentDataManager implements IPersistentDataManager
{
const SLUG_MAX_LENGTH = 255;
private IConnection $dbConnection;
private ?IAuditLogger $auditLogger;
@ -61,14 +58,6 @@ class PersistentDataManager implements IPersistentDataManager
return $this->selectFromDb($select, $type, $useRelations, $withRelations);
}
public function selectFromDbBySlug(string $slug, string $type, bool $useRelations = false, array $withRelations = [])
{
$select = new Select($this->dbConnection);
$select->where('slug', '=', $slug);
return $this->selectFromDb($select, $type, $useRelations, $withRelations);
}
public function loadRelationsFromDb(Model $model, bool $recursive = false, array $withRelations = []): void
{
$relations = $model::getRelations();
@ -92,40 +81,6 @@ class PersistentDataManager implements IPersistentDataManager
}
}
public function loadMultiRelationsFromDb(array $models, string $relation, bool $useRelations = false, array $withRelations = []): void
{
if (count($models) === 0) {
return;
}
$parentModelType = get_class($models[0]);
$relationModelsSetter = 'set' . str_replace('_', '', ucwords($relation, '_'));
[$relationModelType, $reverseRelation] = call_user_func([$parentModelType, 'getMultiRelations'])[$relation];
$reverseRelationIdGetter = 'get' . str_replace('_', '', ucwords($reverseRelation, '_')) . 'Id';
$parentModelsById = [];
foreach ($models as $model) {
$parentModelsById[$model->getId()] = $model;
}
$select = new Select($this->dbConnection);
$select->where($reverseRelation . '_id', 'IN', array_keys($parentModelsById));
$relationsByParentModelId = [];
foreach ($this->selectMultipleFromDb($select, $relationModelType, $useRelations, $withRelations) as $relationModel) {
$reverseRelationId = $relationModel->$reverseRelationIdGetter();
if (!isset($relationsByParentModelId[$reverseRelationId])) {
$relationsByParentModelId[$reverseRelationId] = [];
}
$relationsByParentModelId[$reverseRelationId][] = $relationModel;
}
foreach ($parentModelsById as $parentModelId => $parentModel) {
$relationModels = $relationsByParentModelId[$parentModelId] ?? [];
$parentModel->$relationModelsSetter($relationModels);
}
}
public function saveToDb(Model $model): void
{
$this->syncRelations($model);
@ -148,21 +103,12 @@ class PersistentDataManager implements IPersistentDataManager
}
if (count($modified) > 0) {
if ($model instanceof ModelWithSlug && isset($modified['slug'])) {
$diff['slug']['new'] = $modified['slug'] = $this->generateUniqueSlug($model, $modified['slug']);
}
$modify->setId($id);
$modify->setDiff($diff);
$modify->fill($modified);
$modify->save();
}
} else {
if ($model instanceof ModelWithSlug) {
$slug = $model->generateSlug();
$modified['slug'] = $this->generateUniqueSlug($model, $slug);
}
$modify->fill($modified);
$modify->save();
@ -183,29 +129,6 @@ class PersistentDataManager implements IPersistentDataManager
$model->resetSnapshot();
}
private function generateUniqueSlug(ModelWithSlug $model, string $notUniqueSlug): string
{
$numbered = 1;
do {
if ($numbered > 1) {
$slug = substr($notUniqueSlug, 0, static::SLUG_MAX_LENGTH - (strlen((string)$numbered) + 1));
$slug = $notUniqueSlug . '_' . (string)$numbered;
} else {
$slug = substr($notUniqueSlug, 0, static::SLUG_MAX_LENGTH);
}
$select = new Select($this->dbConnection, $model::getTable());
$select->where('slug', '=', $slug);
$numbered++;
} while ($select->count() != 0);
$model->setSlug($slug);
return $slug;
}
private function createSelect(Select $select, string $type, bool $useRelations = false, array $withRelations = []): Select
{
$table = call_user_func([$type, 'getTable']);
@ -312,10 +235,7 @@ class PersistentDataManager implements IPersistentDataManager
$method = 'set' . str_replace('_', '', ucwords($relation, '_'));
$model->$method($relationModel);
} else {
while (substr($key, 0, strlen($relation . '__')) === $relation . '__') {
next($data);
$key = key($data);
}
next($data);
}
next($relations);

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 IRouteCollection $routeCollection;
private RouteCollection $routeCollection;
private array $appConfig;
@ -32,7 +32,7 @@ class HttpResponse
public function __construct(
IRequest $request,
IConnection $dbConnection,
IRouteCollection $routeCollection,
RouteCollection $routeCollection,
array $appConfig,
string $requestMethod,
string $requestUrl
@ -55,11 +55,6 @@ 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();
@ -99,7 +94,6 @@ class HttpResponse
$response = call_user_func([$controller, $handler[1]]);
} catch (Exception $exception) {
$this->dbConnection->rollback();
error_log($exception);
$this->render500($exception);
return;
}
@ -115,63 +109,10 @@ 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(['redirect_after_login' => $this->rawUrl]),
IRedirect::TEMPORARY);
$response = new Redirect($this->routeCollection->getRoute($this->appConfig['loginRouteId'])->generateLink(), IRedirect::TEMPORARY);
header('Location: ' . $this->getRedirectUrl($response), true, $response->getHttpCode());
}

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[] = rawurlencode($parameters[$matches[1]]);
$link[] = $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, encoding_type: PHP_QUERY_RFC3986) : '';
$query = count($queryParams) > 0 ? '?' . http_build_query($queryParams) : '';
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]] = rawurldecode($fragment);
$parameters[$matches[1]] = $fragment;
} elseif ($fragment != $this->pattern[$i]) {
return null;
}

View File

@ -86,12 +86,12 @@ class DatabaseSessionHandler implements ISessionHandler
return true;
}
public function gc($maxlifetime): int|false
public function gc($maxlifetime): bool
{
// empty on purpose
// old sessions are deleted by MaintainDatabaseCommand
return 1;
return true;
}
public function create_sid(): string

View File

@ -97,12 +97,7 @@ class Linker
fwrite($outputFileHandle, $extra[0]);
while (($line = fgets($inputFileHandle)) !== false) {
if (preg_match('/^\s*@include\((.*)\)\s*$/', $line, $matches) === 1) {
$include = file_get_contents(ROOT . '/views/' . $matches[1] . '.php');
fwrite($outputFileHandle, $include);
} else {
fwrite($outputFileHandle, $line);
}
fwrite($outputFileHandle, $line);
}
fwrite($outputFileHandle, $extra[1]);
@ -139,7 +134,7 @@ class Linker
{
$output = [];
if (preg_match('/^http(s)?/', $asset) !== 1) {
if (preg_match('/^[\w\/\.]+$/', $asset) === 1) {
if (
empty($_ENV['DEV']) &&
filesize(ROOT . '/public/static/' . $asset) < self::INLINE_ASSET_LIMIT

View File

@ -17,7 +17,7 @@ final class GoogleOAuthTest extends TestCase
$redirectUrl = 'http://example.com/oauth';
$requestMock = $this->getMockBuilder(IRequest::class)
->onlyMethods(['setUrl', 'setMethod', 'setQuery', 'setHeaders', 'send'])
->setMethods(['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)
->onlyMethods(['setUrl', 'setMethod', 'setQuery', 'setHeaders', 'send'])
->setMethods(['setUrl', 'setMethod', 'setQuery', 'setHeaders', 'send'])
->getMock();
$responseMock = $this->getMockBuilder(IResponse::class)
->onlyMethods(['getBody', 'getHeaders'])
->setMethods(['getBody', 'getHeaders'])
->getMock();
$googleOAuth = new GoogleOAuth($requestMock);