@@ -0,0 +1 @@ | |||
place for private files like dbase |
@@ -1,6 +1,9 @@ | |||
<?php | |||
namespace Blog; | |||
session_start(); | |||
use DI\Container; | |||
@@ -8,23 +11,27 @@ use Psr\Http\Message\ResponseInterface as Response; | |||
use Psr\Http\Message\ServerRequestInterface as Request; | |||
use Slim\Factory\AppFactory; | |||
use Blog\DB\User; | |||
use Blog\DB\Blog; | |||
use Blog\DB\Bloglist; | |||
use Blog\DB\Blog as BlogDB; | |||
use Blog\View\Twig; | |||
use Blog\DB\DB; | |||
use Blog\Model\Menu; | |||
use Blog\View\Twig; | |||
use Blog\Model\Blog; | |||
require __DIR__ . '/../vendor/autoload.php'; | |||
$container = new Container(); | |||
AppFactory::setContainer($container); | |||
$app = AppFactory::create(); | |||
$container->set('myService', function () { | |||
$container->set('twig', function () { | |||
$twig = new Twig('../templates'); | |||
return $twig; | |||
}); | |||
@@ -38,18 +45,14 @@ $container->set( | |||
); | |||
ini_set('display_errors', 'Off'); | |||
function addNavbar() | |||
function addNavbar($twig) | |||
{ | |||
global $twig; | |||
$menu = new Menu('primary'); | |||
$err = ['type' => 'notice', 'text' => 'We did it!']; | |||
$twig->addBlockVariable('navbar', ['urls' => $urls]); | |||
$twig->addBlockVariable('messages',$err); | |||
$twig->addBlockVariable('navbar', ['urls' => $menu]); | |||
} | |||
function addFooter($response) | |||
{ | |||
$response->getBody()->write('<div>Privacy statement | Cookie Policy | Contact</div>'); | |||
@@ -57,39 +60,21 @@ function addFooter($response) | |||
return; | |||
} | |||
function init() | |||
{ | |||
$bloglist = new Bloglist; | |||
$articles = $bloglist->countBlogs(); | |||
$i = 0; | |||
$blogArray = []; | |||
while ($articles[$i]) { | |||
array_push($blogArray, $articles[$i]); | |||
$i++; | |||
} | |||
$_SESSION['blogs'] = $blogArray; | |||
} | |||
$app->get('/', function (Request $request, Response $response, array $args) { | |||
init(); | |||
addNavbar(); | |||
global $twig; | |||
//TODO: load the blog data | |||
$vars = [ | |||
"key" => "value", | |||
'key2' => ["val1", "val2", "val3"], | |||
"key3" => ["x1" => "y1", "x2" => "y2"], | |||
"loggedIn" => isset($_SESSION['username']), | |||
"content" => "Dit is de main body met info" | |||
]; | |||
$err = ["type" => "error", "text" => "something's wrong"]; | |||
$a = $twig->render('index.html.twig', $vars); | |||
$response->getBody()->write($a); | |||
$twig = $this->get('twig'); | |||
addNavbar($twig); | |||
$a = $twig->render('index.html.twig', $vars); | |||
$response->getBody()->write($a); | |||
return $response; | |||
}); | |||
@@ -97,27 +82,28 @@ $app->map(['GET', 'POST'], '/blog/create', function (Request $request, Response | |||
if (isset($_SESSION['username'])) { | |||
if ($request->getMethod() == 'GET') { | |||
addNavbar($response); | |||
if ($_SESSION['error']) { | |||
if (isset($_SESSION['error'])) { | |||
$response->getBody()->write($_SESSION['error']); | |||
$response->getBody()->write('<hr/>'); | |||
unset($_SESSION['error']); | |||
} | |||
// toon reeds ingevoerde ggevens in geval van foutmelding | |||
// 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">Content:</label>'); | |||
$response->getBody()->write('<textarea name="content" rows="4" columns="50"></textarea>'); | |||
$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); | |||
addFooter($response); | |||
} else { | |||
$data = $request->getParsedBody(); | |||
$blog = new Blog(); | |||
$res = $blog->createBlog($data); | |||
if($res) { | |||
if ($res) { | |||
return $response->withHeader('Location', '/')->withStatus(302); | |||
} else { | |||
$err = $blog->lastErrorMsg(); | |||
@@ -139,7 +125,6 @@ $app->get('/blog/{slug}', function (Request $request, Response $response, array | |||
foreach ($_SESSION['blogs'] as $art) { | |||
if ($art['slug'] == $args['slug']) { | |||
$response->getBody()->write("<h1>" . $art['title'] . "</h1>"); | |||
$response->getBody()->write('<p>' . $art['content'] . '</p>'); | |||
} | |||
} | |||
@@ -168,10 +153,11 @@ $app->map(['GET', 'POST'], '/login', function (Request $request, Response $respo | |||
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'); |
@@ -2,17 +2,18 @@ | |||
namespace Blog\DB; | |||
class Blog extends DB { | |||
public function createBlog($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; | |||
} | |||
} | |||
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; | |||
} | |||
} |
@@ -1,29 +0,0 @@ | |||
<?php | |||
namespace Blog\DB; | |||
class Bloglist extends DB { | |||
public function countBlogs() { | |||
$sql = "SELECT * FROM blogs"; | |||
$result = $this->query($sql); | |||
$row = array(); | |||
$i = 0; | |||
while($res = $result->fetchArray(SQLITE3_ASSOC)) { | |||
if(!isset($res['id'])) continue; | |||
$row[$i]['id'] = $res['id']; | |||
$row[$i]['slug'] = $res['slug']; | |||
$row[$i]['title'] = $res['title']; | |||
$row[$i]['content'] = $res['content']; | |||
$i++; | |||
} | |||
return $row; | |||
} | |||
} | |||
?> |
@@ -4,10 +4,10 @@ namespace Blog\DB; | |||
use SQLite3; | |||
class DB extends SQLite3 { | |||
function __construct() { | |||
$this->open('../private/test.db'); | |||
} | |||
class DB extends SQLite3 | |||
{ | |||
function __construct() | |||
{ | |||
$this->open('../private/test.db'); | |||
} | |||
} | |||
?> |
@@ -2,14 +2,15 @@ | |||
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; | |||
} | |||
} | |||
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; | |||
} | |||
} |
@@ -2,20 +2,21 @@ | |||
namespace Blog\DB; | |||
class User extends DB { | |||
// TODO: sql injection attack | |||
public function checkUserPass($user, $pass) { | |||
$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; | |||
} | |||
} | |||
} | |||
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; | |||
} | |||
} | |||
} |
@@ -2,39 +2,46 @@ | |||
namespace Blog\View; | |||
use \Twig\Loader\FilesystemLoader; | |||
use \Twig\Environment; | |||
class Twig { | |||
protected $twig; | |||
protected $variables; | |||
public function __construct($tmpl_folder) { | |||
$loader = new \Twig\Loader\FilesystemLoader($tmpl_folder); | |||
$this->twig = new \Twig\Environment($loader, []); | |||
$this->variables = ['navbar' => [], 'messages' => [], '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; | |||
} | |||
public function addBlockVariable($block, $data) { | |||
$current = $this->variables[$block]; | |||
$new = array_merge($current, $data); | |||
$this->variables[$block] = $new; | |||
} | |||
public function render($tmpl, $vars) { | |||
$template = $this->load($tmpl); | |||
$variables = array_merge($this->variables, $vars); | |||
return $template->render($variables); | |||
} | |||
} | |||
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]; | |||
$new = array_merge($current, $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,46 +1,58 @@ | |||
<?php | |||
<?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 getComments() { | |||
$comments = new Comment(); | |||
$this->comments = $comments->getComments($this->id); | |||
return $this->comments; | |||
} | |||
public function save() { | |||
$db = new BlogDB(); | |||
$db->createBlog($this); | |||
} | |||
} | |||
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 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); | |||
} | |||
public function getAssociative() | |||
{ | |||
} | |||
} |
@@ -1,15 +1,18 @@ | |||
<?php | |||
<?php | |||
namespace Blog\Model; | |||
class Comment { | |||
protected $user; | |||
protected $date; | |||
protected $blogID; | |||
protected $comment; | |||
public function getComments($blogID) { | |||
// Vraag de DB om alle comment voor blogID | |||
return []; | |||
} | |||
} | |||
class Comment | |||
{ | |||
protected $user; | |||
protected $date; | |||
protected $blogID; | |||
protected $comment; | |||
public function getComments($blogID) | |||
{ | |||
// vraag de DB om alle comments voro blogID | |||
return []; | |||
} | |||
} |
@@ -2,51 +2,66 @@ | |||
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 getID() { | |||
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; | |||
} | |||
} | |||
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; | |||
} | |||
} |
@@ -2,30 +2,36 @@ | |||
namespace Blog\Model; | |||
use Blog\Model\Link; | |||
use Blog\DB\Menu as DB_Menu; | |||
class Menu { | |||
use \Blog\Model\Link; | |||
use \Blog\DB\Menu as DB_Menu; | |||
protected $links = []; | |||
class Menu | |||
{ | |||
protected $links = []; | |||
public function __construct($type) { | |||
public function __construct($type) | |||
{ | |||
$this->_loadMenu($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; | |||
} | |||
} | |||
protected function _loadMenu($type) { | |||
$db = new DB_Menu(); | |||
$res = $db->getMenu($type); | |||
$results = $res->fetchArray(SQLITE3_ASSOC); | |||
foreach ($results as $result) { | |||
$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,5 +1,8 @@ | |||
{% extends "base.html.twig" %} {% block main %} | |||
{% extends "base.html.twig" %} | |||
{% block main %} | |||
{{ content }} | |||
{% endblock %} | |||
{% endblock %} |
@@ -1,5 +1,5 @@ | |||
<div> | |||
{% for msg in messages.msgs %} | |||
{{ msg.type }} - {{ msg.text }} </br> | |||
{% endfor %} | |||
{% for msg in messages.msgs %} | |||
{{ msg.type }} - {{ msg.text }} </br> | |||
{% endfor %} | |||
</div> |
@@ -1,8 +1,8 @@ | |||
<div> | |||
<ul> | |||
{% for url in navbar.urls %} | |||
{% for url in navbar.urls.getLinks %} | |||
<li> | |||
<a href="{{ url.link }}">{{ url.name }} </a> | |||
<a href="{{ url.url }}">{{ url.name }} </a> | |||
</li> | |||
{% endfor %} | |||
</ul> |