Coded the basics for the blog

This commit is contained in:
Romain de Laage 2020-03-06 09:37:56 +01:00
parent 3f1af31bc6
commit 8ef32e183d
Signed by: rdelaage
GPG Key ID: 18BC11F1027B2AD4
11 changed files with 194 additions and 18 deletions

5
assets/lang/en.php Executable file
View File

@ -0,0 +1,5 @@
<?php
$lang = array(
'homeMenu' => 'Home menu'
);
?>

5
assets/lang/fr.php Executable file
View File

@ -0,0 +1,5 @@
<?php
$lang = array(
'homeMenu' => 'Menu principal'
);
?>

9
config/main.php Executable file
View File

@ -0,0 +1,9 @@
<?php
$config = array(
'title' => 'My blog',
'description' => 'Welcome on my wonderful blog',
'dbHost' => 'localhost',
'dbUser' => 'usr',
'dbPasswd' => 'JFZZxF+^}U:SZ<7EE=n!_(gQs',
'dbName' => 'blog'
);

View File

@ -1,27 +1,44 @@
<?php
include('config/main.php');
if(isset($_GET['lang'])){
if(!empty($_GET['lang'])){
if(file_exists('assets/lang/'.$_GET['lang'].'.php')){
include('assets/lang/'.$_GET['lang'].'.php');
}
}
}
if(isset($_GET['page'])){
if(!empty($_GET['page'])){
if(file_exists('views/'.$_GET['page'].'.php')){
include('views/'.$_GET['page'].'.php');
}else{
$title = "Page not found";
$content = "Sorry, the page you asked for doesn't exist";
}
}else{
$title = "Page empty";
$content = "Sorry, we are not able to show you a content because the page to show is empty";
header("location:?page=index");
}
}else{
$title = "Page not set";
$content = "Sorry, we are not able to show you a content because the page to show is not defined";
header("location:?page=index");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>L'étudiant auto-hébergé</title>
<title><?php echo $config["title"]; ?></title>
<meta charset="utf-8" />
</head>
<body>
<h1>L'étudiant auto-hébergé - Blog</h1>
<p>Vous pouvez trouver ici mes billets de blog sur mon quotidien en tant qu'étudiant auto-hébergé.</p>
<hr />
<article>
<h1><a href="#lien-vers-l-article">Nom de l'article</a></h1>
<p>Description</p>
</article>
<hr />
<article>
<h1><a href="#lien-vers-l-article">Nom de l'article</a></h1>
<p>Description</p>
</article>
<hr />
<article>
<h1><a href="#lien-vers-l-article">Nom de l'article</a></h1>
<p>Description</p>
</article>
<h1><?php echo $config["title"]; ?></h1>
<p><?php echo $config["description"] ?></p>
<a href="?page=index"><?php echo isset($lang["homeMenu"])?$lang["homeMenu"]:"Return to home"; ?></a>
<hr />
<?php echo isset($content)?$content:'Content not found'; ?>
</body>
</html>

41
lib/articles.php Normal file
View File

@ -0,0 +1,41 @@
<?php
function getArticles(){
require("lib/db.php");
$req = $db->prepare("SELECT * FROM articles");
if($req->execute(array())){
$list = array();
while($data = $req->fetch())
$list[] = $data;
}else
$list = NULL;
return $list;
}
function newArticle($title, $description, $author, $content){
require("lib/db.php");
$req = $db->prepare("INSERT INTO articles (title, description, author, date, content) VALUES (?, ?, ?, NOW(), ?)");
if($req->execute(array($title, $description, $author, $content))){
return true;
}
return false;
}
function getArticle($ID){
require("lib/db.php");
$req = $db->prepare("SELECT * FROM articles WHERE ID = ?");
if($req->execute(array($ID))){
if($data = $req->fetch())
return $data;
}
return false;
}

10
lib/db.php Executable file
View File

@ -0,0 +1,10 @@
<?php
include('config/main.php');
try{
$db = new PDO('mysql:host='.$config['dbHost'].';dbname='.$config['dbName'].';charset=utf8', $config['dbUser'], $config['dbPasswd']);
}catch(PDOException $e){
print "Erreur !: " . $e->getMessage() . "<br/>";
die();
}
?>

30
lib/users.php Normal file
View File

@ -0,0 +1,30 @@
<?php
function isValid($login, $password){
require('lib/db.php');
$req = $db->prepare("SELECT ID, password FROM users WHERE name = ?");
if($req->execute(array($login))){
$data = $req->fetch();
if(password_verify($password, $data['password'])){
return $data['ID'];
}else{
return NULL;
}
}else{
return NULL;
}
}
function getUserName($id){
require('lib/db.php');
$req = $db->prepare("SELECT name FROM users WHERE ID = ?");
if($req->execute(array($id))){
$data = $req->fetch();
return $data["name"];
}else{
return NULL;
}
}

2
test.php Normal file
View File

@ -0,0 +1,2 @@
<?php
include("lib/articles.php");

17
views/article.php Normal file
View File

@ -0,0 +1,17 @@
<?php
if(isset($_GET["ID"])){
if(!empty($_GET["ID"])){
include_once("lib/articles.php");
include_once("lib/users.php");
$article = getArticle($_GET["ID"]);
$authorName = getUserName($article["author"]);
$content = "<h1>".$article["title"]."</h1><article>".$article["content"]."</article><span>written by ".$authorName." on ".$article["date"]."</span>";
}else{
$content = "Article ID is empty";
}
}else{
$content = "No article ID set";
}

10
views/index.php Executable file
View File

@ -0,0 +1,10 @@
<?php
include_once("lib/articles.php");
$articles = getArticles();
$content = "";
foreach($articles as $article){
$content .= "<article><h1><a href=\"?page=article&ID=".$article["ID"]."\">".$article["title"]."</a></h1><p>".$article["description"]."</p></article><hr/>";
}
?>

30
views/write.php Normal file
View File

@ -0,0 +1,30 @@
<?php
if(isset($_POST["writeSubmit"])){
include_once("lib/users.php");
$user = isValid($_POST['login'], $_POST['password']);
if($user){
include_once("lib/articles.php");
if(newArticle($_POST["title"], $_POST["description"], $user, $_POST["content"]))
$content = "Article successfuly sent !";
else
$content = "We are unable to send your article !";
}else{
$content = "Not a valid user !";
}
}else
$content = "<form action=\"\" method=\"POST\">
<label for=\"title\">Title</label>
<input type=\"text\" name=\"title\" id=\"title\" /><br />
<label for=\"description\">Description</label>
<textarea name=\"description\" id=\"description\"></textarea><br />
<label for=\"content\">Content</label>
<textarea name=\"content\" id=\"content\"></textarea><br />
<label for=\"login\">Login</label>
<input type=\"text\" name=\"login\" id=\"login\" /><br />
<label for=\"password\">Password</label>
<input type=\"password\" name=\"password\" id=\"password\" /><br />
<input type=\"submit\" name=\"writeSubmit\" />
</form>";