Skip to content

Rewriting... #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/config/config.php
config/config.php
8 changes: 8 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# TODO

Prio / description

- 100 markdown -> parsedown
- 40 Search system
- 20 Searchbar
- 5 installer
14 changes: 10 additions & 4 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
<?php
// datanase
define('DB_HOST', 'localhost');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
define('DB_USER', '');
define('DB_PASS', '');
define('DB_NAME', 'blog');

define('ROOT_PATH', '/');
// page settings
define('ROOT_PATH', 'https://lixer.hatbe.ch/'); // domain (must end with a "/"!)
define('DEFAULT_KEYWORDS', 'This, blog, does, not, have, keywords');
define('DESCRIPTION', 'This blog does not have a description');
define('PAGE_TITLE', 'HATBES BLOG');
define('PAGE_SLOGAN', 'Blogging and stuff.');

define('SITE_NAME', 'COOL BLOG');
define('ITEMS_PER_PAGE', 4);
40 changes: 0 additions & 40 deletions dump.sql

This file was deleted.

18 changes: 10 additions & 8 deletions public/assets/css/style.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
* {
border-radius: 0 !important;
outline: none !important;
}
body {
background-color: rgba(var(--bs-dark-rgb)) !important;
background-color: #1c1c1c !important;
color: white !important;
}
a {
text-decoration: none !important;
}
.page-link {
background-color: rgba(var(--bs-secondary-rgb)) !important;
Expand All @@ -23,10 +31,4 @@ body {
background-color: rgba(var(--bs-secondary-rgb)) !important;
color: white;
border: 1px solid rgba(0,0,0,.125);
}
.body-container {
background-color: #a9a9a9 !important;
}
.nounderline {
text-decoration: none !important
}
}
4 changes: 2 additions & 2 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
require_once(__DIR__ . '/../src/Core.php');
require_once(__DIR__ . '/../src/classes/Core.php');

$core = new Core();
new Core();
19 changes: 0 additions & 19 deletions src/Controller.php

This file was deleted.

46 changes: 0 additions & 46 deletions src/Core.php

This file was deleted.

11 changes: 0 additions & 11 deletions src/Template.php

This file was deleted.

24 changes: 24 additions & 0 deletions src/classes/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
abstract class Controller {
protected $core;

protected function model($model) {
$model = ucfirst(strtolower($model)) . 'Model';
$path = __DIR__ . '/models/' . $model . '.php';
if(file_exists($path)) {
require_once($path);
return new $model();
}
return null;
}

protected function render($view, $data = []) {
$path = __DIR__ . '/../views/' . $view . '.php';
if(file_exists($path)) {
require_once($path);
} else {
echo 'View not found';
exit();
}
}
}
68 changes: 68 additions & 0 deletions src/classes/Core.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
class Core {
private $url;
private $controller = 'PostsController'; // this must exist! in /src/classes/controllers/{class}
private $method = 'index'; // this must exist! in /src/classes/controllers/{class}->{method}
private $params = [];

private $loggedInUser = null;

public function __construct() {
$this->init();
}

private function init() {
session_start();
$this->include();
$this->getUrl();
$this->getController();
$this->controller = new $this->controller();
$this->getMethod();
$this->getParams();
call_user_func_array([$this->controller, $this->method], $this->params);
}

private function getController() {
$controller = ucfirst(strtolower($this->url[0])) . 'Controller';
if(file_exists(__DIR__ . '/controllers/' . $controller . '.php')) {
$this->controller = $controller;
array_shift($this->url);
}
require_once(__DIR__ . '/controllers/' . $this->controller . '.php');
}

private function getMethod() {
if(isset($this->url[0])) {
$method = strtolower($this->url[0]);
if(method_exists($this->controller, $method)) {
$this->method = $method;
array_shift($this->url);
}
}
}

private function getParams() {
$this->params = $this->url ? array_values($this->url) : [null];
}

private function include() {
require_once(__DIR__ . '/../../config/config.php');
require_once(__DIR__ . '/Database.php');
require_once(__DIR__ . '/Controller.php');
require_once(__DIR__ . '/Model.php');
require_once(__DIR__ . '/Linker.php');
require_once(__DIR__ . '/lib/Parsedown.php');
require_once(__DIR__ . '/Template.php');
}

private function getUrl() {
if(isset($_SERVER['PATH_INFO'])) {
$url = rtrim($_SERVER['PATH_INFO'], '/'); // remove last slash
$url = substr($url, 1); // remove first slash
$url = filter_var($url, FILTER_SANITIZE_URL); // sanitize URL
$url = explode('/', $url);
if($url[0] == '') return;
$this->url = $url;
}
}
}
5 changes: 3 additions & 2 deletions src/Database.php → src/classes/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
class Database {
private $host = DB_HOST;
private $user = DB_USER;
private $password = DB_PASSWORD;
private $password = DB_PASS;
private $name = DB_NAME;

private $stmt;
Expand All @@ -20,6 +20,7 @@ public function __construct() {
} catch(PDOException $e) {
$this->error = $e->getMessage();
echo $this->error;
exit();
}
}

Expand Down Expand Up @@ -53,7 +54,7 @@ public function resultSet() {
return $this->stmt->fetchAll(PDO::FETCH_OBJ);
}

public function single() {
public function single() {
$this->execute();
return $this->stmt->fetch(PDO::FETCH_OBJ);
}
Expand Down
8 changes: 8 additions & 0 deletions src/classes/Linker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
class Linker {
public static function link($controller, $method, array $args = []) {
$link = ROOT_PATH . $controller . '/' . $method;
$link .= '/' . join('/', $args);
return $link;
}
}
4 changes: 2 additions & 2 deletions src/Model.php → src/classes/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ abstract class Model {
protected $db;

public function __construct() {
$this->db = new Database;
$this->db = new Database();
}
}
}
11 changes: 11 additions & 0 deletions src/classes/Template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
class Template {
public static function load(string $name, array $data = array()) {
if(file_exists(__DIR__ . '/../templates/' . $name . '.php')) {
extract($data, EXTR_SKIP);
require(__DIR__ . '/../templates/' . $name . '.php');
} else {
echo "Template \"{$name}\" not found!";
}
}
}
Loading