place for private files like dbase |
<?php | <?php | ||||
namespace Blog; | namespace Blog; | ||||
session_start(); | session_start(); | ||||
use DI\Container; | use DI\Container; | ||||
use Psr\Http\Message\ServerRequestInterface as Request; | use Psr\Http\Message\ServerRequestInterface as Request; | ||||
use Slim\Factory\AppFactory; | use Slim\Factory\AppFactory; | ||||
use Blog\DB\User; | 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\DB\DB; | ||||
use Blog\Model\Menu; | use Blog\Model\Menu; | ||||
use Blog\View\Twig; | |||||
use Blog\Model\Blog; | |||||
require __DIR__ . '/../vendor/autoload.php'; | require __DIR__ . '/../vendor/autoload.php'; | ||||
$container = new Container(); | $container = new Container(); | ||||
AppFactory::setContainer($container); | AppFactory::setContainer($container); | ||||
$app = AppFactory::create(); | $app = AppFactory::create(); | ||||
$container->set('myService', function () { | |||||
$container->set('twig', function () { | |||||
$twig = new Twig('../templates'); | $twig = new Twig('../templates'); | ||||
return $twig; | return $twig; | ||||
}); | }); | ||||
); | ); | ||||
ini_set('display_errors', 'Off'); | |||||
function addNavbar() | |||||
function addNavbar($twig) | |||||
{ | { | ||||
global $twig; | |||||
$menu = new Menu('primary'); | $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) | function addFooter($response) | ||||
{ | { | ||||
$response->getBody()->write('<div>Privacy statement | Cookie Policy | Contact</div>'); | $response->getBody()->write('<div>Privacy statement | Cookie Policy | Contact</div>'); | ||||
return; | 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) { | $app->get('/', function (Request $request, Response $response, array $args) { | ||||
init(); | |||||
addNavbar(); | |||||
global $twig; | |||||
//TODO: load the blog data | |||||
$vars = [ | $vars = [ | ||||
"key" => "value", | |||||
'key2' => ["val1", "val2", "val3"], | |||||
"key3" => ["x1" => "y1", "x2" => "y2"], | |||||
"loggedIn" => isset($_SESSION['username']), | |||||
"content" => "Dit is de main body met info" | "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; | return $response; | ||||
}); | }); | ||||
if (isset($_SESSION['username'])) { | if (isset($_SESSION['username'])) { | ||||
if ($request->getMethod() == 'GET') { | if ($request->getMethod() == 'GET') { | ||||
addNavbar($response); | addNavbar($response); | ||||
if ($_SESSION['error']) { | |||||
if (isset($_SESSION['error'])) { | |||||
$response->getBody()->write($_SESSION['error']); | $response->getBody()->write($_SESSION['error']); | ||||
$response->getBody()->write('<hr/>'); | $response->getBody()->write('<hr/>'); | ||||
unset($_SESSION['error']); | 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('<form action="/blog/create" method="POST">'); | ||||
$response->getBody()->write('<label for="slug">slug:</label>'); | $response->getBody()->write('<label for="slug">slug:</label>'); | ||||
$response->getBody()->write('<input type="text" name="slug"/><br/>'); | $response->getBody()->write('<input type="text" name="slug"/><br/>'); | ||||
$response->getBody()->write('<label for="title">Titel:</label>'); | $response->getBody()->write('<label for="title">Titel:</label>'); | ||||
$response->getBody()->write('<input type="text" name="title"/><br/>'); | $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"/>'); | $response->getBody()->write('<input type="submit"/>'); | ||||
addFooter($response); | |||||
addFooter($response); | |||||
} else { | } else { | ||||
$data = $request->getParsedBody(); | $data = $request->getParsedBody(); | ||||
$blog = new Blog(); | $blog = new Blog(); | ||||
$res = $blog->createBlog($data); | $res = $blog->createBlog($data); | ||||
if($res) { | |||||
if ($res) { | |||||
return $response->withHeader('Location', '/')->withStatus(302); | return $response->withHeader('Location', '/')->withStatus(302); | ||||
} else { | } else { | ||||
$err = $blog->lastErrorMsg(); | $err = $blog->lastErrorMsg(); | ||||
foreach ($_SESSION['blogs'] as $art) { | foreach ($_SESSION['blogs'] as $art) { | ||||
if ($art['slug'] == $args['slug']) { | if ($art['slug'] == $args['slug']) { | ||||
$response->getBody()->write("<h1>" . $art['title'] . "</h1>"); | $response->getBody()->write("<h1>" . $art['title'] . "</h1>"); | ||||
$response->getBody()->write('<p>' . $art['content'] . '</p>'); | |||||
} | } | ||||
} | } | ||||
addFooter($response); | addFooter($response); | ||||
} else { | } else { | ||||
$postdata = $request->getParsedBody(); | $postdata = $request->getParsedBody(); | ||||
$user = new User(); | $user = new User(); | ||||
$logged_in = $user->checkUserPass($postdata['username'], $postdata['password']); | $logged_in = $user->checkUserPass($postdata['username'], $postdata['password']); | ||||
if ($logged_in) { | if ($logged_in) { | ||||
//if ($postdata['username'] == 'gebruiker' && $postdata['password'] == "abcd") { | |||||
$_SESSION["username"] = $postdata['username']; | $_SESSION["username"] = $postdata['username']; | ||||
addNavbar($response); | addNavbar($response); | ||||
$response->getBody()->write('Logged in'); | $response->getBody()->write('Logged in'); |
namespace Blog\DB; | 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; | |||||
} | |||||
} |
<?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; | |||||
} | |||||
} | |||||
?> |
use SQLite3; | use SQLite3; | ||||
class DB extends SQLite3 { | |||||
function __construct() { | |||||
$this->open('../private/test.db'); | |||||
} | |||||
class DB extends SQLite3 | |||||
{ | |||||
function __construct() | |||||
{ | |||||
$this->open('../private/test.db'); | |||||
} | |||||
} | } | ||||
?> |
namespace Blog\DB; | 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; | |||||
} | |||||
} |
namespace Blog\DB; | 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; | |||||
} | |||||
} | |||||
} |
namespace Blog\View; | 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); | |||||
} | |||||
} |
<?php | |||||
<?php | |||||
namespace Blog\Model; | namespace Blog\Model; | ||||
use Blog\DB\Blog as BlogDB; | use Blog\DB\Blog as BlogDB; | ||||
use Blog\Model\Comment; | 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() | |||||
{ | |||||
} | |||||
} |
<?php | |||||
<?php | |||||
namespace Blog\Model; | 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 []; | |||||
} | |||||
} |
namespace Blog\Model; | 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; | |||||
} | |||||
} |
namespace Blog\Model; | 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; | |||||
} | |||||
} |
{% extends "base.html.twig" %} {% block main %} | |||||
{% extends "base.html.twig" %} | |||||
{% block main %} | |||||
{{ content }} | {{ content }} | ||||
{% endblock %} | |||||
{% endblock %} |
<div> | <div> | ||||
{% for msg in messages.msgs %} | |||||
{{ msg.type }} - {{ msg.text }} </br> | |||||
{% endfor %} | |||||
{% for msg in messages.msgs %} | |||||
{{ msg.type }} - {{ msg.text }} </br> | |||||
{% endfor %} | |||||
</div> | </div> |
<div> | <div> | ||||
<ul> | <ul> | ||||
{% for url in navbar.urls %} | |||||
{% for url in navbar.urls.getLinks %} | |||||
<li> | <li> | ||||
<a href="{{ url.link }}">{{ url.name }} </a> | |||||
<a href="{{ url.url }}">{{ url.name }} </a> | |||||
</li> | </li> | ||||
{% endfor %} | {% endfor %} | ||||
</ul> | </ul> |