diff --git a/src/Controller/AddressBook.php b/src/Controller/AddressBook.php index 2a2e0ba..8fba3a9 100644 --- a/src/Controller/AddressBook.php +++ b/src/Controller/AddressBook.php @@ -3,12 +3,38 @@ namespace App\Controller; use App\Service\Template; -use stdClass; -class AddressBook extends stdClass +class AddressBook { - public function __construct(Template $template) + public function __construct(private readonly Template $template) { - $template->render(templateName: 'index.tpl'); } + + public function main(): void + { + try { + $this->template->render(templateName: 'index.tpl'); + } catch (\Exception $e) { + die($e->getMessage()); + } + + } + public function admin(string $command = '') + { + try { + $this->template->render(templateName: 'admin/index.tpl'); + } catch (\Exception $e) { + die($e->getMessage()); + } + } + + public function login() + { + try { + $this->template->render(templateName: 'admin/index.tpl'); + } catch (\Exception $e) { + die($e->getMessage()); + } + } + } \ No newline at end of file diff --git a/src/Entity/User.php b/src/Entity/User.php index 29dfd52..cc21b3e 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -9,9 +9,10 @@ class User private string $password, private string $first = '', private string $last = '', - private int $id = 0 + private int $id = 0, + private bool $isAdmin = false ) - { - // empty body - } + { + // empty body + } } \ No newline at end of file diff --git a/src/Repository/UserRepository.php b/src/Repository/UserRepository.php new file mode 100644 index 0000000..cd559dd --- /dev/null +++ b/src/Repository/UserRepository.php @@ -0,0 +1,255 @@ +databaseConnection->getConnection()->prepare(query: $sql); + $statement->execute(); + while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { + $domain = new Domain(name: $result['name'], panel: $result['panel'], id: $result['id']); + $domains[] = $domain; + } + return $domains; + } catch (PDOException $e) { + exit($e->getMessage()); + } + } + + + /** + * @param int $id + * + * @return bool|\App\Entity\Domain + */ + public function findByID(int $id): bool|Domain + { + $this->logger->debug(message: "findById($id)"); + + $sql = " + SELECT id, name, panel + FROM . " . DatabaseConnection::TABLE_DOMAINS . " + WHERE id = :id"; + + try { + $statement = $this->databaseConnection->getConnection()->prepare(query: $sql); + $statement->bindParam(param: ':id', var: $id); + $statement->execute(); + if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { + + return new Domain(name: $result['name'], panel: $result['panel'], id: $result['id']); + } else { + return false; + } + } catch (PDOException $e) { + exit($e->getMessage()); + } + } + + + /** + * @param String $name + * + * @return \App\Entity\Domain|bool + */ + public function findByName(string $name): Domain|bool + { + $this->logger->debug(message: "findByName($name)"); + + $sql = " + SELECT id, name, panel + FROM " . DatabaseConnection::TABLE_DOMAINS . " + WHERE name = :name"; + + try { + $statement = $this->databaseConnection->getConnection()->prepare(query: $sql); + $statement->bindParam(param: ':name', var: $name); + $statement->execute(); + if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { + return new Domain(name: $result['name'], panel: $result['panel'], id: $result['id']); + } else { + return false; + } + } catch (PDOException $e) { + exit($e->getMessage()); + } + } + + + /** + * @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_DOMAINS . " (name, panel) + VALUES (:name, :panel)"; + + try { + $name = $domain->getName(); + $panel = $domain->getPanel(); + $statement = $this->databaseConnection->getConnection()->prepare(query: $sql); + $statement->bindParam(param: ':name', var: $name); + $statement->bindParam(param: ':panel', var: $panel); + $statement->execute(); + + return $this->databaseConnection->getConnection()->lastInsertId(); + } catch (PDOException $e) { + exit($e->getMessage()); + } + } + + + /** + * @param \App\Entity\Domain $domain + * + * @return false|int + */ + public function update(Domain $domain): bool|int + { + $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 { + $name = $domain->getName(); + } + if (empty($domain->getPanel())) { + $panel = $current->getPanel(); + } else { + $panel = $domain->getPanel(); + } + + $sql = " + UPDATE " . DatabaseConnection::TABLE_DOMAINS . " SET + name = :name, + panel = :panel + WHERE id = :id"; + + try { + $statement = $this->databaseConnection->getConnection()->prepare(query: $sql); + $statement->bindParam(param: 'id', var: $id); + $statement->bindParam(param: 'name', var: $name); + $statement->bindParam(param: 'panel', var: $panel); + $statement->execute(); + + return $statement->rowCount(); + } catch (PDOException $e) { + echo $e->getMessage(); + return false; + } + } + + + /** + * @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_DOMAINS . " + WHERE id = :id"; + + try { + $statement = $this->databaseConnection->getConnection()->prepare(query: $sql); + $id = $domain->getId(); + $statement->bindParam(param: 'id', var: $id); + $statement->execute(); + + return $statement->rowCount(); + } catch (PDOException $e) { + exit($e->getMessage()); + } + } + + + /** + * @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()); + } + } +} \ No newline at end of file diff --git a/src/Service/Router.php b/src/Service/Router.php new file mode 100644 index 0000000..c9636d1 --- /dev/null +++ b/src/Service/Router.php @@ -0,0 +1,53 @@ +uri = explode(separator: '/', string: $uri); + } + + public function registerRoute(string $route, \Closure $callback): void + { + $this->routes[$route] = $callback; + } + + + private function matchRoute($url = '/users/tracer/posts/tracer', $method = 'GET') + { + $reqUrl = $url; + + $reqUrl = rtrim(string: $reqUrl, characters: "/"); + + foreach ($this->routes as $route => $closure) { + // convert urls like '/users/:uid/posts/:pid' to regular expression + // $pattern = "@^" . preg_replace('/\\\:[a-zA-Z0-9\_\-]+/', '([a-zA-Z0-9\-\_]+)', preg_quote($route['url'])) . "$@D"; + $pattern = "@^" . preg_replace('\\/users/:[a-zA-Z0-9\_\-]+/', '([a-zA-Z0-9\-\_]+)', $route) . "$@D"; + // echo $pattern."\n"; + $params = []; + // check if the current request params the expression + $match = preg_match($pattern, $reqUrl, $params); + if ($match) { + // remove the first match + array_shift($params); + // call the callback with the matched positions as params + // return call_user_func_array($route['callback'], $params); + return [$route, $params]; + } + } + return []; + } + + public function handleRouting(): void + { + $foo = $this->matchRoute(); + var_dump($foo); + } +} \ No newline at end of file