@@ -1 +0,0 @@ | |||
place for private files like dbase |
@@ -1,189 +0,0 @@ | |||
<?php | |||
namespace Blog; | |||
session_start(); | |||
use DI\Container; | |||
use Psr\Http\Message\ResponseInterface as Response; | |||
use Psr\Http\Message\ServerRequestInterface as Request; | |||
use Slim\Factory\AppFactory; | |||
use Blog\DB\User; | |||
use Blog\Model\Blogs; | |||
use Blog\View\Twig; | |||
use Blog\DB\DB; | |||
use Blog\Model\Menu; | |||
use Blog\Model\Blog; | |||
require __DIR__ . '/../vendor/autoload.php'; | |||
$container = new Container(); | |||
AppFactory::setContainer($container); | |||
$app = AppFactory::create(); | |||
$container->set('twig', function () { | |||
$twig = new Twig('../templates'); | |||
return $twig; | |||
}); | |||
$container->set( | |||
'db', | |||
function () { | |||
$db = new DB(); | |||
return $db; | |||
} | |||
); | |||
function addNavbar($twig) | |||
{ | |||
$menu = new Menu('primary'); | |||
$twig->addBlockVariable('primary', $menu->getLinks()); | |||
} | |||
function addNavbar2($twig) | |||
{ | |||
$menu = new Menu('secondary'); | |||
$twig->addBlockVariable('secondary', $menu->getLinks()); | |||
} | |||
function addFooter($response) | |||
{ | |||
$response->getBody()->write('<div>Privacy statement | Cookie Policy | Contact</div>'); | |||
$response->getBody()->write("</body></html>"); | |||
return; | |||
} | |||
$app->get('/', function (Request $request, Response $response, array $args) { | |||
$blogs = new Blogs(); | |||
$blogs = $blogs->getBlogs(); | |||
$vars = [ | |||
"content" => $blogs | |||
]; | |||
$twig = $this->get('twig'); | |||
addNavbar($twig); | |||
addNavbar2($twig); | |||
$a = $twig->render('index.html.twig', $vars); | |||
$response->getBody()->write($a); | |||
return $response; | |||
}); | |||
$app->map(['GET', 'POST'], '/blog/create', function (Request $request, Response $response, array $args) { | |||
if (isset($_SESSION['username'])) { | |||
if ($request->getMethod() == 'GET') { | |||
addNavbar($response); | |||
if (isset($_SESSION['error'])) { | |||
$response->getBody()->write($_SESSION['error']); | |||
$response->getBody()->write('<hr/>'); | |||
unset($_SESSION['error']); | |||
} | |||
// Opdracht : Toon reeds ingevoerde gegevens in geval van foutmelding | |||
$response->getBody()->write('<form action="/blog/create" method="POST">'); | |||
$response->getBody()->write('<label for="slug">slug:</label>'); | |||
$response->getBody()->write('<input type="text" name="slug"/><br/>'); | |||
$response->getBody()->write('<label for="title">Titel:</label>'); | |||
$response->getBody()->write('<input type="text" name="title"/><br/>'); | |||
$response->getBody()->write('<label for="content">Inhoud:</label>'); | |||
$response->getBody()->write('<textarea type="textarea" name="content"></textarea><br/>'); | |||
$response->getBody()->write('<input type="submit"/>'); | |||
addFooter($response); | |||
} else { | |||
$data = $request->getParsedBody(); | |||
$blog = new Blog(); | |||
$res = $blog->createBlog($data); | |||
if ($res) { | |||
return $response->withHeader('Location', '/')->withStatus(302); | |||
} else { | |||
$err = $blog->lastErrorMsg(); | |||
$_SESSION['error'] = $err; | |||
return $response->withHeader('Location', '/blog/create')->withStatus(302); | |||
} | |||
} | |||
} else { | |||
addNavbar($response); | |||
$response->getBody()->write(('Please login')); | |||
addFooter($response); | |||
} | |||
return $response; | |||
}); | |||
$app->get('/blog/{slug}', function (Request $request, Response $response, array $args) { | |||
$blogs = new Blogs(); | |||
$blogs = $blogs->getBlog($args['slug']); | |||
$vars = [ | |||
"content" => $blogs | |||
]; | |||
$twig = $this->get('twig'); | |||
addNavbar($twig); | |||
$a = $twig->render('blog.html.twig', $vars); | |||
$response->getBody()->write($a); | |||
return $response; | |||
}); | |||
$app->get('/logout', function (Request $request, Response $response, array $args) { | |||
unset($_SESSION['username']); | |||
addNavbar($response); | |||
$response->getBody()->write('Logged out'); | |||
addFooter($response); | |||
return $response->withHeader('Location', '/')->withStatus(302); | |||
}); | |||
$app->map(['GET', 'POST'], '/login', function (Request $request, Response $response, array $args) { | |||
if ($request->getMethod() == 'GET') { | |||
addNavbar($response); | |||
$response->getBody()->write('<form action="/login" method="POST">'); | |||
$response->getBody()->write('<label for="username">Username:</label>'); | |||
$response->getBody()->write('<input type="text" name="username"/><br/>'); | |||
$response->getBody()->write('<label for="password">Password:</label>'); | |||
$response->getBody()->write('<input type="password" name="password"/><br/>'); | |||
$response->getBody()->write('<input type="submit"/>'); | |||
addFooter($response); | |||
} else { | |||
$postdata = $request->getParsedBody(); | |||
$user = new User(); | |||
$logged_in = $user->checkUserPass($postdata['username'], $postdata['password']); | |||
if ($logged_in) { | |||
//if ($postdata['username'] == 'gebruiker' && $postdata['password'] == "abcd") { | |||
$_SESSION["username"] = $postdata['username']; | |||
addNavbar($response); | |||
$response->getBody()->write('Logged in'); | |||
addFooter($response); | |||
return $response->withHeader('Location', '/')->withStatus(302); | |||
} else { | |||
$response->getBody()->write('Username and/or password incorrect'); | |||
} | |||
} | |||
return $response; | |||
}); | |||
$app->post('/postcomment', function (Request $request, Response $response, array $args) { | |||
$response->getBody()->write("Postcomment"); | |||
return $response; | |||
}); | |||
$app->run(); |
@@ -1,36 +0,0 @@ | |||
<?php | |||
namespace Blog\DB; | |||
use Blog\Model\Blog as BlogModel; | |||
class Blog extends DB | |||
{ | |||
public function createBlog(BlogModel $data) | |||
{ | |||
$this->enableExceptions(false); | |||
$stmt = $this->prepare("INSERT INTO blogs (slug, title, content) VALUES (:slug, :title, :content)"); | |||
$stmt->bindValue(':slug', $data['slug'], SQLITE3_TEXT); | |||
$stmt->bindValue(':title', $data['title'], SQLITE3_TEXT); | |||
$stmt->bindValue(':content', $data['content'], SQLITE3_TEXT); | |||
$res = $stmt->execute(); | |||
return $res; | |||
} | |||
public function getBlog($slug) | |||
{ | |||
$sql = "SELECT * FROM blogs WHERE slug = :slug ;"; | |||
$stmt = $this->prepare($sql); | |||
$stmt->bindValue(':slug', $slug, SQLITE3_TEXT); | |||
$res = $stmt->execute(); | |||
return $res; | |||
} | |||
public function getBlogs() | |||
{ | |||
$sql = "SELECT * FROM blogs"; | |||
$stmt = $this->prepare($sql); | |||
$res = $stmt->execute(); | |||
return $res; | |||
} | |||
} |
@@ -1,13 +0,0 @@ | |||
<?php | |||
namespace Blog\DB; | |||
use SQLite3; | |||
class DB extends SQLite3 | |||
{ | |||
function __construct() | |||
{ | |||
$this->open('../private/test.db'); | |||
} | |||
} |
@@ -1,16 +0,0 @@ | |||
<?php | |||
namespace Blog\DB; | |||
class Menu extends DB | |||
{ | |||
// Fetch all links for a given menu type | |||
public function getMenu($type) | |||
{ | |||
$sql = "SELECT * FROM menu WHERE type = :type ;"; | |||
$stmt = $this->prepare($sql); | |||
$stmt->bindValue(":type", $type); | |||
$res = $stmt->execute(); | |||
return $res; | |||
} | |||
} |
@@ -1,22 +0,0 @@ | |||
<?php | |||
namespace Blog\DB; | |||
use Blog\DB\DB; | |||
class User extends DB | |||
{ | |||
public function checkUserPass($user, $pass) | |||
{ | |||
// TODO : SQL injection attack! | |||
$sql = "SELECT count(*) as count FROM users WHERE username = '" . $user . "' AND password = '" . $pass . "';"; | |||
$ret = $this->query($sql); | |||
$rows = $ret->fetchArray(SQLITE3_ASSOC); | |||
$rowcount = $rows['count']; | |||
if ($rowcount == 1) { | |||
return true; | |||
} else { | |||
return false; | |||
} | |||
} | |||
} |
@@ -1,93 +0,0 @@ | |||
<?php | |||
namespace Blog\Model; | |||
use Blog\DB\Blog as BlogDB; | |||
use Blog\Model\Comment; | |||
class Blog | |||
{ | |||
protected $author; | |||
protected $title; | |||
protected $subtitle; | |||
protected $date; | |||
protected $content; | |||
protected $comments; | |||
protected $id; | |||
protected $slug; | |||
public function setAuthor($value) | |||
{ | |||
$this->author = $value; | |||
} | |||
public function setDate($value) | |||
{ | |||
$this->date = $value; | |||
} | |||
public function getDate() | |||
{ | |||
return $this->date; | |||
} | |||
public function getAuthor() | |||
{ | |||
return $this->author; | |||
} | |||
public function setContent($value) | |||
{ | |||
$this->content = $value; | |||
} | |||
public function setID($value) | |||
{ | |||
$this->id = $value; | |||
} | |||
public function setSlug($value) | |||
{ | |||
$this->slug = $value; | |||
} | |||
public function setTitle($value) | |||
{ | |||
$this->title = $value; | |||
} | |||
public function getContent() | |||
{ | |||
return $this->content; | |||
} | |||
public function getID() | |||
{ | |||
return $this->id; | |||
} | |||
public function getSlug() | |||
{ | |||
return $this->slug; | |||
} | |||
public function getTitle() | |||
{ | |||
return $this->title; | |||
} | |||
public function getComments() | |||
{ | |||
$comments = new Comment(); | |||
$this->comments = $comments->getComments($this->id); | |||
return $this->comments; | |||
} | |||
public function save() | |||
{ | |||
$db = new BlogDB(); | |||
$data = ["slug" => $this->slug, "title" => $this->title, "content" => $this->content]; | |||
//$db->createBlog($data); | |||
$db->createBlog($this); | |||
} | |||
} |
@@ -1,49 +0,0 @@ | |||
<?php | |||
namespace Blog\Model; | |||
use Blog\DB\Blog as BlogDB; | |||
use Blog\Model\Blog; | |||
class Blogs | |||
{ | |||
protected $blogs = []; | |||
public function getBlog($slug) | |||
{ | |||
$db = new BlogDB(); | |||
$res = $db->getBlog($slug); | |||
$result = $res->fetchArray(SQLITE3_ASSOC); | |||
$blog = new Blog(); | |||
$blog->setSlug($result['slug']); | |||
$blog->setID($result['id']); | |||
$blog->setTitle($result['title']); | |||
$blog->setContent($result['content']); | |||
$this->blogs[] = $blog; | |||
return $this; | |||
} | |||
// TODO/ filter system | |||
// $filter is assoc array vb ['id' => 'dbase id', 'category'=>['PHP', 'Slim']] | |||
// of : ['id' => [1,2,3]] | |||
public function getBlogs() | |||
{ | |||
$db = new BlogDB(); | |||
$res = $db->getBlogs(); | |||
while ($result = $res->fetchArray(SQLITE3_ASSOC)) { | |||
$blog = new Blog(); | |||
$blog->setSlug($result['slug']); | |||
$blog->setID($result['id']); | |||
$blog->setTitle($result['title']); | |||
$blog->setContent($result['content']); | |||
$this->blogs[] = $blog; | |||
} | |||
return $this; | |||
} | |||
public function getBlogsList() | |||
{ | |||
return $this->blogs; | |||
} | |||
} |
@@ -1,18 +0,0 @@ | |||
<?php | |||
namespace Blog\Model; | |||
class Comment | |||
{ | |||
protected $user; | |||
protected $date; | |||
protected $blogID; | |||
protected $comment; | |||
public function getComments($blogID) | |||
{ | |||
// vraag de DB om alle comments voro blogID | |||
return []; | |||
} | |||
} |
@@ -1,67 +0,0 @@ | |||
<?php | |||
namespace Blog\Model; | |||
class Link | |||
{ | |||
protected $id; | |||
protected $name; | |||
protected $url; | |||
protected $type; | |||
protected $parent; | |||
public function setID($value) | |||
{ | |||
$this->id = $value; | |||
return; | |||
} | |||
public function getOD() | |||
{ | |||
return $this->id; | |||
} | |||
public function setName($value) | |||
{ | |||
$this->name = $value; | |||
return; | |||
} | |||
public function getName() | |||
{ | |||
return $this->name; | |||
} | |||
public function setUrl($value) | |||
{ | |||
$this->url = $value; | |||
return; | |||
} | |||
public function getUrl() | |||
{ | |||
return $this->url; | |||
} | |||
public function setType($value) | |||
{ | |||
$this->type = $value; | |||
return; | |||
} | |||
public function getType() | |||
{ | |||
return $this->type; | |||
} | |||
public function setParent($value) | |||
{ | |||
$this->parent = $value; | |||
return; | |||
} | |||
public function getParent() | |||
{ | |||
return $this->parent; | |||
} | |||
} |
@@ -1,37 +0,0 @@ | |||
<?php | |||
namespace Blog\Model; | |||
use \Blog\Model\Link; | |||
use \Blog\DB\Menu as DB_Menu; | |||
class Menu | |||
{ | |||
protected $links = []; | |||
public function __construct($type) | |||
{ | |||
$this->_loadMenu($type); | |||
} | |||
protected function _loadMenu($type) | |||
{ | |||
$db = new DB_Menu(); | |||
$res = $db->getMenu($type); | |||
while ($result = $res->fetchArray(SQLITE3_ASSOC)) { | |||
$link = new Link(); | |||
$link->setID($result['id']); | |||
$link->setName($result['name']); | |||
$link->setUrl($result['url']); | |||
$link->setType($result['type']); | |||
$link->setParent($result['parent']); | |||
$this->links[] = $link; | |||
} | |||
} | |||
public function getLinks() | |||
{ | |||
return $this->links; | |||
} | |||
} |
@@ -1,51 +0,0 @@ | |||
<?php | |||
namespace Blog\View; | |||
use Twig\Loader\FilesystemLoader; | |||
use Twig\Environment; | |||
class Twig | |||
{ | |||
protected $twig; | |||
protected $variables; | |||
public function __construct($tmpl_folder) | |||
{ | |||
$loader = new FilesystemLoader($tmpl_folder); | |||
$this->twig = new Environment($loader, []); | |||
$this->variables = ['navbar' => [], 'messages' => ['msgs' => []], 'footer' => []]; | |||
} | |||
protected function load($tmpl) | |||
{ | |||
return $this->twig->load($tmpl); | |||
} | |||
public function addMessage($msg) | |||
{ | |||
$current = $this->variables['messages']['msgs']; | |||
$current[] = $msg; | |||
$this->variables['messages']['msgs'] = $current; | |||
} | |||
// add block variables to the global variable bag | |||
public function addBlockVariable($block, $data) | |||
{ | |||
$current = $this->variables[$block]; | |||
if ($current) { | |||
$new = array_merge($current, $data); | |||
} else { | |||
$new = $data; | |||
} | |||
$this->variables[$block] = $new; | |||
} | |||
public function render($tmpl, $vars) | |||
{ | |||
$template = $this->load($tmpl); | |||
$variables = array_merge($this->variables, $vars); | |||
return $template->render($variables); | |||
} | |||
} |
@@ -1,11 +0,0 @@ | |||
<html> | |||
<body> | |||
<div id="navbar"> | |||
{% block primary %} {% include "primary.html.twig" %} {% endblock %} {% | |||
block secondary %} {% include "secondary.html.twig" %} {% endblock %} | |||
</div> | |||
<div id="messages">{% include "messages.html.twig" %}</div> | |||
<diV id="main"> {% block main %}{% endblock %} </diV> | |||
<div id="footer">{% block footer %}{% endblock %}</div> | |||
</body> | |||
</html> |
@@ -1,13 +0,0 @@ | |||
{% extends "base.html.twig" %} {% block main %} | |||
<ul> | |||
{% for blog in content.getBlogsList %} | |||
<h1>{{ blog.title }}</h1> | |||
{{ | |||
blog.content | |||
}} | |||
{% endfor %} | |||
</ul> | |||
{% endblock %} |
@@ -1,10 +0,0 @@ | |||
{% extends "base.html.twig" %} {% block main %} | |||
<ul> | |||
{% for blog in content.getBlogsList%} | |||
<li> | |||
<a href="/blog/{{ blog.slug }}">{{ blog.title }}</a> | |||
</li> | |||
{% endfor %} | |||
</ul> | |||
{% endblock %} |
@@ -1,5 +0,0 @@ | |||
<div> | |||
{% for msg in messages.msgs %} | |||
{{ msg.type }} - {{ msg.text }} </br> | |||
{% endfor %} | |||
</div> |
@@ -1,9 +0,0 @@ | |||
<div> | |||
<ul> | |||
{% for url in navbar %} | |||
<li> | |||
<a href="{{ url.url }}">{{ url.name }} </a> | |||
</li> | |||
{% endfor %} | |||
</ul> | |||
</div> |
@@ -1,9 +0,0 @@ | |||
<div> | |||
<ul> | |||
{% for url in primary %} | |||
<li> | |||
<a href="{{ url.url }}">{{ url.name }} </a> | |||
</li> | |||
{% endfor %} | |||
</ul> | |||
</div> |
@@ -1,9 +0,0 @@ | |||
<div> | |||
<ul> | |||
{% for url in secondary %} | |||
<li> | |||
<a href="{{ url.url }}">{{ url.name }} </a> | |||
</li> | |||
{% endfor %} | |||
</ul> | |||
</div> |