Skip to content

Commit 7c0a46d

Browse files
authored
Merge pull request #1 from HATBE/dev
Rewriting...
2 parents 4708308 + c85e2b7 commit 7c0a46d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2709
-680
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
/config/config.php
1+
config/config.php

TODO.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# TODO
2+
3+
Prio / description
4+
5+
- 100 markdown -> parsedown
6+
- 40 Search system
7+
- 20 Searchbar
8+
- 5 installer

config/config.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
<?php
2+
// datanase
23
define('DB_HOST', 'localhost');
3-
define('DB_USER', 'user');
4-
define('DB_PASSWORD', 'password');
4+
define('DB_USER', '');
5+
define('DB_PASS', '');
56
define('DB_NAME', 'blog');
67

7-
define('ROOT_PATH', '/');
8+
// page settings
9+
define('ROOT_PATH', 'https://lixer.hatbe.ch/'); // domain (must end with a "/"!)
10+
define('DEFAULT_KEYWORDS', 'This, blog, does, not, have, keywords');
11+
define('DESCRIPTION', 'This blog does not have a description');
12+
define('PAGE_TITLE', 'HATBES BLOG');
13+
define('PAGE_SLOGAN', 'Blogging and stuff.');
814

9-
define('SITE_NAME', 'COOL BLOG');
15+
define('ITEMS_PER_PAGE', 4);

dump.sql

Lines changed: 0 additions & 40 deletions
This file was deleted.

public/assets/css/style.css

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
* {
2+
border-radius: 0 !important;
3+
outline: none !important;
4+
}
15
body {
2-
background-color: rgba(var(--bs-dark-rgb)) !important;
6+
background-color: #1c1c1c !important;
7+
color: white !important;
8+
}
9+
a {
10+
text-decoration: none !important;
311
}
412
.page-link {
513
background-color: rgba(var(--bs-secondary-rgb)) !important;
@@ -23,10 +31,4 @@ body {
2331
background-color: rgba(var(--bs-secondary-rgb)) !important;
2432
color: white;
2533
border: 1px solid rgba(0,0,0,.125);
26-
}
27-
.body-container {
28-
background-color: #a9a9a9 !important;
29-
}
30-
.nounderline {
31-
text-decoration: none !important
32-
}
34+
}

public/index.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?php
2-
require_once(__DIR__ . '/../src/Core.php');
2+
require_once(__DIR__ . '/../src/classes/Core.php');
33

4-
$core = new Core();
4+
new Core();

src/Controller.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Core.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/Template.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/classes/Controller.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
abstract class Controller {
3+
protected $core;
4+
5+
protected function model($model) {
6+
$model = ucfirst(strtolower($model)) . 'Model';
7+
$path = __DIR__ . '/models/' . $model . '.php';
8+
if(file_exists($path)) {
9+
require_once($path);
10+
return new $model();
11+
}
12+
return null;
13+
}
14+
15+
protected function render($view, $data = []) {
16+
$path = __DIR__ . '/../views/' . $view . '.php';
17+
if(file_exists($path)) {
18+
require_once($path);
19+
} else {
20+
echo 'View not found';
21+
exit();
22+
}
23+
}
24+
}

src/classes/Core.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
class Core {
3+
private $url;
4+
private $controller = 'PostsController'; // this must exist! in /src/classes/controllers/{class}
5+
private $method = 'index'; // this must exist! in /src/classes/controllers/{class}->{method}
6+
private $params = [];
7+
8+
private $loggedInUser = null;
9+
10+
public function __construct() {
11+
$this->init();
12+
}
13+
14+
private function init() {
15+
session_start();
16+
$this->include();
17+
$this->getUrl();
18+
$this->getController();
19+
$this->controller = new $this->controller();
20+
$this->getMethod();
21+
$this->getParams();
22+
call_user_func_array([$this->controller, $this->method], $this->params);
23+
}
24+
25+
private function getController() {
26+
$controller = ucfirst(strtolower($this->url[0])) . 'Controller';
27+
if(file_exists(__DIR__ . '/controllers/' . $controller . '.php')) {
28+
$this->controller = $controller;
29+
array_shift($this->url);
30+
}
31+
require_once(__DIR__ . '/controllers/' . $this->controller . '.php');
32+
}
33+
34+
private function getMethod() {
35+
if(isset($this->url[0])) {
36+
$method = strtolower($this->url[0]);
37+
if(method_exists($this->controller, $method)) {
38+
$this->method = $method;
39+
array_shift($this->url);
40+
}
41+
}
42+
}
43+
44+
private function getParams() {
45+
$this->params = $this->url ? array_values($this->url) : [null];
46+
}
47+
48+
private function include() {
49+
require_once(__DIR__ . '/../../config/config.php');
50+
require_once(__DIR__ . '/Database.php');
51+
require_once(__DIR__ . '/Controller.php');
52+
require_once(__DIR__ . '/Model.php');
53+
require_once(__DIR__ . '/Linker.php');
54+
require_once(__DIR__ . '/lib/Parsedown.php');
55+
require_once(__DIR__ . '/Template.php');
56+
}
57+
58+
private function getUrl() {
59+
if(isset($_SERVER['PATH_INFO'])) {
60+
$url = rtrim($_SERVER['PATH_INFO'], '/'); // remove last slash
61+
$url = substr($url, 1); // remove first slash
62+
$url = filter_var($url, FILTER_SANITIZE_URL); // sanitize URL
63+
$url = explode('/', $url);
64+
if($url[0] == '') return;
65+
$this->url = $url;
66+
}
67+
}
68+
}

src/Database.php renamed to src/classes/Database.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
class Database {
33
private $host = DB_HOST;
44
private $user = DB_USER;
5-
private $password = DB_PASSWORD;
5+
private $password = DB_PASS;
66
private $name = DB_NAME;
77

88
private $stmt;
@@ -20,6 +20,7 @@ public function __construct() {
2020
} catch(PDOException $e) {
2121
$this->error = $e->getMessage();
2222
echo $this->error;
23+
exit();
2324
}
2425
}
2526

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

56-
public function single() {
57+
public function single() {
5758
$this->execute();
5859
return $this->stmt->fetch(PDO::FETCH_OBJ);
5960
}

src/classes/Linker.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
class Linker {
3+
public static function link($controller, $method, array $args = []) {
4+
$link = ROOT_PATH . $controller . '/' . $method;
5+
$link .= '/' . join('/', $args);
6+
return $link;
7+
}
8+
}

src/Model.php renamed to src/classes/Model.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ abstract class Model {
55
protected $db;
66

77
public function __construct() {
8-
$this->db = new Database;
8+
$this->db = new Database();
99
}
10-
}
10+
}

src/classes/Template.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
class Template {
3+
public static function load(string $name, array $data = array()) {
4+
if(file_exists(__DIR__ . '/../templates/' . $name . '.php')) {
5+
extract($data, EXTR_SKIP);
6+
require(__DIR__ . '/../templates/' . $name . '.php');
7+
} else {
8+
echo "Template \"{$name}\" not found!";
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)