Compare commits

..

No commits in common. "b60d9481ab654476882d29984e1e645537279cee" and "e9fd8e153d77e14b84880e03f7e90130ff34f822" have entirely different histories.

12 changed files with 176 additions and 278 deletions

View File

@ -1,3 +1,2 @@
As I was not allowed to use any framework, respectively no foreign code, most of the timme was spent, well creating some kind of framework myself. :-)
# addressbook
The address book itself was than done in a few hours.

View File

@ -1,11 +1,4 @@
<?php
/*
* Copyright (c) 2022. Micha Espey <tracer@24unix.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace App\Entity;
@ -20,44 +13,4 @@ class AddressBookEntry
{
// empty body
}
public function getUserid(): int
{
return $this->userid;
}
public function setUserid(int $userid): void
{
$this->userid = $userid;
}
public function getFirst(): string
{
return $this->first;
}
public function setFirst(string $first): void
{
$this->first = $first;
}
public function getLast(): string
{
return $this->last;
}
public function setLast(string $last): void
{
$this->last = $last;
}
public function getNick(): string
{
return $this->nick;
}
public function setNick(string $nick): void
{
$this->nick = $nick;
}
}

View File

@ -1,11 +1,4 @@
<?php
/*
* Copyright (c) 2022. Micha Espey <tracer@24unix.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace App\Entity;
@ -24,51 +17,81 @@ class Route
// empty body
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @param string $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
/**
* @return string
*/
public function getRoute(): string
{
return $this->route;
}
/**
* @param string $route
*/
public function setRoute(string $route): void
{
$this->route = $route;
}
/**
* @return string
*/
public function getRegEx(): string
{
return $this->regEx;
}
/**
* @param string $regEx
*/
public function setRegEx(string $regEx): void
{
$this->regEx = $regEx;
}
/**
* @return array
*/
public function getParameters(): array
{
return $this->parameters;
}
/**
* @param array $parameters
*/
public function setParameters(array $parameters): void
{
$this->parameters = $parameters;
}
/**
* @return Closure
*/
public function getCallback(): Closure
{
return $this->callback;
}
/**
* @param Closure $callback
*/
public function setCallback(Closure $callback): void
{
$this->callback = $callback;

View File

@ -1,99 +1,18 @@
<?php
/*
* Copyright (c) 2022. Micha Espey <tracer@24unix.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace App\Entity;
use App\Enums\UserAuth;
class User
{
public function __construct(
private string $nick = '',
private string $password = '',
private string $first = '',
private string $last = '',
private int $id = 0,
private bool $isAdmin = false,
private UserAuth $userAuth = UserAuth::AUTH_ANONYMOUS
private string $nick,
private string $password,
private string $first = '',
private string $last = '',
private int $id = 0,
private bool $isAdmin = false
)
{
// empty body
}
public function getNick(): string
{
return $this->nick;
}
public function setNick(string $nick): void
{
$this->nick = $nick;
}
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): void
{
$this->password = $password;
}
public function getFirst(): string
{
return $this->first;
}
public function setFirst(string $first): void
{
$this->first = $first;
}
public function getLast(): string
{
return $this->last;
}
public function setLast(string $last): void
{
$this->last = $last;
}
public function getId(): int
{
return $this->id;
}
public function setId(int $id): void
{
$this->id = $id;
}
public function isAdmin(): bool
{
return $this->isAdmin;
}
public function setIsAdmin(bool $isAdmin): void
{
$this->isAdmin = $isAdmin;
}
public function getAuth()
{
return UserAuth::AUTH_ANONYMOUS;
}
public function setAuth(UserAuth $userAuth)
{
$this->userAuth = $userAuth;
}
}

View File

@ -1,17 +0,0 @@
<?php
/*
* Copyright (c) 2022. Micha Espey <tracer@24unix.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace App\Enums;
enum UserAuth
{
case AUTH_ANONYMOUS;
case AUTH_USER;
case AUTH_ADMIN;
}

View File

@ -1,25 +1,21 @@
<?php
/*
* Copyright (c) 2022. Micha Espey <tracer@24unix.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace App\Repository;
use App\Service\Config;
use App\Service\DatabaseConnection;
use App\Entity\User;
use PDO;
use PDOException;
/**
* Handles CRUD od User class.
*
*/
class UserRepository
class DomainRepository
{
public function __construct(private readonly DatabaseConnection $databaseConnection)
public function __construct(
private readonly DatabaseConnection $databaseConnection,
private readonly Config $configController)
{
// empty body
}
@ -28,35 +24,40 @@ class UserRepository
/**
* @return array
*/
public function findAll(string $orderBy = 'nick'): array
public function findAll(): array
{
$users = [];
$sql = "
SELECT id, nick, first, last, is_admin
FROM " . DatabaseConnection::TABLE_USERS . "
ORDER BY :order";
ORDER BY name";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':order', var: $order);
$statement->execute();
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
$user = new User(nick: $result['nick'], first: $result['first'], last: $result['last'], id: $result['id']);
$users[] = $user;
$domain = new Domain(name: $result['name'], panel: $result['panel'], id: $result['id']);
$domains[] = $domain;
}
return $users;
return $domains;
} catch (PDOException $e) {
exit($e->getMessage());
}
}
public function findByID(int $id): ?User
/**
* @param int $id
*
* @return bool|\App\Entity\Domain
*/
public function findByID(int $id): bool|Domain
{
$this->logger->debug(message: "findById($id)");
$sql = "
SELECT id, nick, first, last, is_admin
FROM " . DatabaseConnection::TABLE_USERS . "
SELECT id, name, panel
FROM . " . DatabaseConnection::TABLE_DOMAINS . "
WHERE id = :id";
try {
@ -64,41 +65,85 @@ class UserRepository
$statement->bindParam(param: ':id', var: $id);
$statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new User(nick: $result['nick'], first: $result['first'], last: $result['last'], id: $result['id']);
return new Domain(name: $result['name'], panel: $result['panel'], id: $result['id']);
} else {
return null;
return false;
}
} catch (PDOException $e) {
exit($e->getMessage());
}
}
public function findByNick(string $nick): ?User
/**
* @param String $name
*
* @return \App\Entity\Domain|bool
*/
public function findByName(string $name): Domain|bool
{
$this->logger->debug(message: "findByName($name)");
$sql = "
SELECT id, nick, first, last, is_admin
FROM " . DatabaseConnection::TABLE_USERS . "
WHERE nick = :nick";
SELECT id, name, panel
FROM " . DatabaseConnection::TABLE_DOMAINS . "
WHERE name = :name";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':nick', var: $nick);
$statement->bindParam(param: ':name', var: $name);
$statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new User(nick: $result['nick'], first: $result['first'], last: $result['last'], id: $result['id']);
return new Domain(name: $result['name'], panel: $result['panel'], id: $result['id']);
} else {
return null;
return false;
}
} catch (PDOException $e) {
exit($e->getMessage());
}
}
public function insert(User $user): bool|string
/**
* @param string $host
*
* @return \App\Entity\Domain|bool
*/
public function findByHost(string $host): Domain|bool
{
/*
$this->logger->debug(message: "findByHost($host)");
$host = strtolower(string: trim(string: $host));
$count = substr_count(haystack: $host, needle: '.');
if ($count == 2) {
if (strlen(string: explode(separator: '.', string: $host)[1]) > 3) {
$host = explode(separator: '.', string: $host, limit: 2)[1];
}
} elseif ($count > 2) {
$host = $this->findByHost(host: explode(separator: '.', string: $host, limit: 2)[1]);
}
if ($domain = $this->findByName(name: $host)) {
return $domain;
} else {
return false;
}
}
/**
* @param \App\Entity\Domain $domain
*
* @return string|false
*/
public function insert(Domain $domain): bool|string
{
$domainName = $domain->getName();
$this->logger->info(message: "insert($domainName)");
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_USERS . " (name, panel)
INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel)
VALUES (:name, :panel)";
try {
@ -113,17 +158,22 @@ class UserRepository
} catch (PDOException $e) {
exit($e->getMessage());
}
*/
return false;
}
public function update(User $user): bool|int
/**
* @param \App\Entity\Domain $domain
*
* @return false|int
*/
public function update(Domain $domain): bool|int
{
$id = $user->getId();
$domainName = $domain->getName();
$this->logger->debug(message: "update($domainName)");
$id = $domain->getId();
$current = $this->findByID(id: $id);
/*
if (empty($domain->getName())) {
$name = $current->getName();
} else {
@ -136,7 +186,7 @@ class UserRepository
}
$sql = "
UPDATE " . DatabaseConnection::TABLE_USER . " SET
UPDATE " . DatabaseConnection::TABLE_DOMAINS . " SET
name = :name,
panel = :panel
WHERE id = :id";
@ -153,20 +203,26 @@ class UserRepository
echo $e->getMessage();
return false;
}
*/
return false;
}
public function delete(User $user): int
/**
* @param \App\Entity\Domain $domain
*
* @return int
*/
public function delete(Domain $domain): int
{
$domainName = $domain->getName();
$this->logger->debug(message: "delete($domainName)");
$sql = "
DELETE FROM " . DatabaseConnection::TABLE_USERS . "
DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . "
WHERE id = :id";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$id = $user->getId();
$id = $domain->getId();
$statement->bindParam(param: 'id', var: $id);
$statement->execute();
@ -176,4 +232,24 @@ class UserRepository
}
}
/**
* @param String $field
*
* @return int
*/
public function getLongestEntry(string $field): int
{
$sql = "
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_DOMAINS;
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
$result = $statement->fetch();
return $result['length'];
} catch (PDOException $e) {
exit($e->getMessage());
}
}
}

View File

@ -1,11 +1,4 @@
<?php
/*
* Copyright (c) 2022. Micha Espey <tracer@24unix.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace App\Service;
@ -18,21 +11,23 @@ class Config
{
private array $config;
/**
* @throws Exception
*/
public function __construct()
{
// Check for either config.json.local or config.json.
$configFile = dirname(path: __DIR__, levels: 2) . "/config.json.local";
if (!file_exists(filename: $configFile)) {
$configFile = dirname(path: __DIR__, levels: 2) . "/config.json";
}
if (!file_exists(filename: $configFile)) {
die('Missing config file');
throw new Exception(message: 'Missing config file');
}
$configJSON = file_get_contents(filename: $configFile);
if (json_decode(json: $configJSON) === null) {
die('Config file is not valid JSON.');
throw new Exception(message: 'Config file is not valid JSON.');
}
$this->config = json_decode(json: $configJSON, associative: true);

View File

@ -1,35 +1,27 @@
<?php
/*
* Copyright (c) 2022. Micha Espey <tracer@24unix.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace App\Service;
use PDO;
/**
* Take care of the PDO object.
*
*/
class DatabaseConnection
{
private PDO $dbConnection;
// Currently no prefixes are used, but could be easily added to config.json.
const TABLE_PREFIX = '';
const TABLE_USERS = self::TABLE_PREFIX . "users";
const TABLE_ADDRESSES = self::TABLE_PREFIX . "addresses";
public function __construct(private readonly Config $config)
public function __construct(private readonly Config $configController)
{
$dbHost = $this->config->getConfig(configKey: 'dbHost');
$dbPort = $this->config->getConfig(configKey: 'dbPort');
$dbDatabase = $this->config->getConfig(configKey: 'dbDatabase');
$dbUser = $this->config->getConfig(configKey: 'dbUser');
$dbPassword = $this->config->getConfig(configKey: 'dbPassword');
$dbHost = $this->configController->getConfig(configKey: 'dbHost');
$dbPort = $this->configController->getConfig(configKey: 'dbPort');
$dbDatabase = $this->configController->getConfig(configKey: 'dbDatabase');
$dbUser = $this->configController->getConfig(configKey: 'dbUser');
$dbPassword = $this->configController->getConfig(configKey: 'dbPassword');
$this->dbConnection = new PDO(
dsn: "mysql:host=$dbHost;port=$dbPort;charset=utf8mb4;dbname=$dbDatabase",

View File

@ -1,11 +1,4 @@
<?php
/*
* Copyright (c) 2022. Micha Espey <tracer@24unix.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace App\Service;
@ -22,14 +15,8 @@ class Router
{
private array $routes;
public function __construct(private readonly Template $template)
{
// empty body
}
/*
* This method takes a route like /admin/users/{user} and creates a regex to match on call
* More complex routes as /posts/{thread}/show/{page} are supported as well.
*/
function addRoute(string $name, string $route, Closure $callback): void
{
@ -39,7 +26,6 @@ class Router
// create regex for route:
$regex = preg_replace(pattern: '/(?<={).+?(?=})/', replacement: '(.*?)', subject: $route);
// code below is ugly, better match including the braces
$regex = str_replace(search: '{', replace: '', subject: $regex);
$regex = str_replace(search: '}', replace: '', subject: $regex);
@ -51,7 +37,8 @@ class Router
}
/*
* Check if there is a known route and executes the callback.
* Check is there is a known route and executed the callback.
* Currently no 404 handling.
*/
public function handleRouting(): void
{
@ -70,7 +57,7 @@ class Router
return;
}
}
$this->template->render(templateName: 'status/404.html.php');
// Throw a 404 result later …
die("Invalid route: $requestUri");
}
}

View File

@ -1,37 +1,18 @@
<?php
/*
* Copyright (c) 2022 Micha Espey <tracer@24unix.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Service;
/*
* As I'm not allowed to use 3rd party code like Twig or Smarty I ended up
* using PHP as a templating engine.
*/
use Exception;
class Template
{
/*
* Just store the information about the template base dir.
*/
public function __construct(private readonly string $templateDir)
{
// empty body
}
/*
* Add variables to template and throw it out
*/
public function render(string $templateName, array $vars = []): void
public function render(string $templateName): void
{
foreach ($vars as $name => $value) {
$$name = $value;
}
include $this->templateDir . $templateName;
exit(0);
}
}

View File

@ -1,5 +0,0 @@
<?php include dirname(path: __DIR__) . '/_header.html.php'; ?>
<h2>403 Permission denied</h2>
<?php include dirname(path: __DIR__) . '/_footer.html.php' ?>

View File

@ -1,5 +0,0 @@
<?php include dirname(path: __DIR__) . '/_header.html.php'; ?>
<h2>404 Page not found</h2>
<?php include dirname(path: __DIR__) . '/_footer.html.php' ?>