diff --git a/.htaccess b/.htaccess index 40121fe..6a54ecd 100644 --- a/.htaccess +++ b/.htaccess @@ -24,7 +24,7 @@ RewriteCond %{REQUEST_FILENAME} !-f # on redirige vers le fichier handler.php avec $1 qui est la page entré (ex: blog/1/2) # le L con connait déjà, QSA va mettre toutes les requetes a la suite # (ex : blog.delta-wings.net/flag?p=5 va devenir de notre coté -> blog.delta-wings.net/assets/php/handler.php?page=flag&p=5) -RewriteRule ^(.*)$ /assets/php/handler.php?page=$1 [L,QSA] +RewriteRule ^(.*)$ /assets/php/handler.php?page=$1 [QSA,L] # indique une redirection lorque on obtient une page d'erreur 403 (interdiction d'accès (permet de "cacher" les fichiers critiques)) # on redirige vars note handler pour qu'il afficher la page 404 diff --git a/assets/css/style.css b/assets/css/style.css index b7fe476..182798e 100644 --- a/assets/css/style.css +++ b/assets/css/style.css @@ -744,3 +744,15 @@ input { height: 650px; } } + +form.login > input { + text-align: center; + display: block; + margin: auto; +} + +form.login >input[type="submit"] { + left: -46%; + position: relative; + margin-top: 10px; +} diff --git a/assets/html/404.html b/assets/html/404.html index e6f8489..561ad44 100644 --- a/assets/html/404.html +++ b/assets/html/404.html @@ -4,13 +4,14 @@ -

404

-
+

Error 404

+ + diff --git a/assets/html/includes/header.html b/assets/html/includes/header.html index a6aaf78..ea3f1d6 100644 --- a/assets/html/includes/header.html +++ b/assets/html/includes/header.html @@ -12,6 +12,9 @@
  • + diff --git a/assets/html/login.html b/assets/html/login.html new file mode 100644 index 0000000..8472de0 --- /dev/null +++ b/assets/html/login.html @@ -0,0 +1,36 @@ + + + + + + + +
    + +
    +
    +
    +
    +
    +

    LE BLOG DES APPRENANTS

    +
    +
    +

    Merci de bien vouloir vous connecter pour continuer

    +
    + +
    +
    +
    +
    +
    + + + + diff --git a/assets/html/post.html b/assets/html/post.html index e9f9185..7fdbcd1 100644 --- a/assets/html/post.html +++ b/assets/html/post.html @@ -10,7 +10,7 @@
    -
    +

    @@ -51,7 +51,7 @@

    Dans le même thème

    - +

    diff --git a/assets/html/post_edit.html b/assets/html/post_edit.html index e9f9185..8bc620b 100644 --- a/assets/html/post_edit.html +++ b/assets/html/post_edit.html @@ -11,67 +11,29 @@
    -

    +

    + +

    - - + + + +

    - +

    -
    -
    -

    Auteur

    -
    - - -
    -

    -

    -
    -
    +
    -

    Plus d'articles

    -

    Catégories

    -
    dev
    -
    digi
    -
    ops
    -
    devops
    -

    Tags

    - -
    - -
    -
    Article précédent
    - -
    - -

    Dans le même thème

    -
    diff --git a/assets/html/register.html b/assets/html/register.html new file mode 100644 index 0000000..bbd376c --- /dev/null +++ b/assets/html/register.html @@ -0,0 +1,36 @@ + + + + + + + +
    + +
    +
    +
    +
    +
    +

    LE BLOG DES APPRENANTS

    +
    +
    +

    Mercie de bien vous enregistrer chez nous

    +
    + +
    +
    +
    +
    +
    +
    + +
    + + + diff --git a/assets/html/search.html b/assets/html/search.html index f1c495b..ec0bee3 100644 --- a/assets/html/search.html +++ b/assets/html/search.html @@ -22,20 +22,24 @@
    -
    -

    - -

    - +
    diff --git a/assets/js/script.js b/assets/js/script.js index 8c67403..020ca84 100644 --- a/assets/js/script.js +++ b/assets/js/script.js @@ -22,12 +22,7 @@ var addTag = (element) => { cancelBtn.classList.add("cancelBtn"); cancelBtn.innerText = "Annuler"; - cancelBtn.addEventListener("click", function(element) { - var input = element.target.parentElement.querySelector(".add-tag"); - input.parentElement.querySelector(".addTag").innerText = "Add Tag"; - input.parentElement.removeChild(input); - element.target.parentElement.removeChild(element.target); - }); + cancelBtn.addEventListener("click", cancel); btn.removeEventListener("click", addTag); btn.addEventListener("click", addingTag); @@ -39,6 +34,16 @@ var addTag = (element) => { } +var cancel = (element) => { + var input = element.target.parentElement.querySelector(".add-tag"); + var btn = input.parentElement.querySelector(".addTag"); + btn.removeEventListener("click", addingTag); + btn.addEventListener("click", addTag); + btn.innerText = "Add Tag"; + input.parentElement.removeChild(input); + element.target.parentElement.removeChild(element.target); +} + var addingTag = (element) => { var input = document.createElement("input"); var uuid = Math.floor(Math.random() * Math.floor(-1000000)); @@ -64,10 +69,7 @@ var addingTag = (element) => { element.target.addEventListener("click", addTag); } -document.querySelector(".addTag").addEventListener("click", addTag); - - -var submit = (el) => { +var submit = () => { var major = document.querySelector('.post.text > textarea'); var title = document.querySelector("h2.title > input"); @@ -79,7 +81,7 @@ var submit = (el) => { console.log(tags); var tglst = ""; tags.forEach(element => { - tglst += "," + element.getAttribute("id") + (element.hasAttribute("data-text") ? ":" + element.getAttribute("data-text") : ""); + tglst += "," + element.getAttribute("data-text"); }); tglst = tglst.substr(1); @@ -89,4 +91,7 @@ var submit = (el) => { } -document.querySelector(".submitPost").addEventListener("click", submit); +if(document.querySelector(".addTag") != null) { + document.querySelector(".addTag").addEventListener("click", addTag); + document.querySelector(".submitPost").addEventListener("click", submit); +} diff --git a/assets/php/Controller.php b/assets/php/Controller.php index d3b970a..37349e9 100644 --- a/assets/php/Controller.php +++ b/assets/php/Controller.php @@ -34,15 +34,25 @@ class Controller { $arr = preg_split("/ /", $annot); if($arr[0] === "route") { if(preg_match($arr[1], $route)) { + $cl = $class; $instance = new $class(); $function = ($method->getName()); - return $instance->$function(); + // return $instance->$function(); } + } elseif ($arr[0] === "editor" && isset($cl) && $cl == $class) { + if(!isset($_SESSION["author"]) || (isset($_SESSION["author"]) && $_SESSION["author"]->getRole() != "ROLE_EDITOR")) header("Location: /login/?redirect=".$_SERVER["REQUEST_URI"]); + } elseif($arr[0] === "admin" && isset($cl) && $cl ==$class) { + if(!isset($_SESSION["author"]) || (isset($_SESSION["author"]) && $_SESSION["author"]->getRole() != "ROLE_ADMIN")) header("Location: /login/?redirect=".$_SERVER["REQUEST_URI"]); } } + if(isset($instance)) { + return $instance->$function(); + } } } } + header("HTTP/1.0 404 Not Found"); + return file_get_contents(DIR."/html/404.html"); } } diff --git a/assets/php/Controller/AddEditController.php b/assets/php/Controller/AddEditController.php new file mode 100644 index 0000000..ae8a92e --- /dev/null +++ b/assets/php/Controller/AddEditController.php @@ -0,0 +1,78 @@ +setTitle($_GET["title"]); + $post->setContent($_GET["content"]); + $post->setCategory($_GET["category"]); + + $tags = explode(",", $_GET["tags"]); + $tgs = array(); + foreach ($tags as $tag) { + var_dump($tag); + var_dump(Tag::getByName($tag)); + if(!(Tag::getByName($tag))) { + $tgs[] = Tag::add((new Tag())->setName($tag))->getId(); + } else { + $tgs[] = Tag::getByName($tag)->getId(); + } + } + + $post->setTags($tgs); + $post->setAuthor($_SESSION["author"]->getId()); + Post::update($post); + } + return file_get_contents(DIR."/html/post_edit.html"); + } + + /** + * @route /^\/post\/new\/*$/ + * @editor + */ + public function postAdd() { + + // var_dump($_SESSION["author"]); + // die; + + if(isset($_GET["title"]) && isset($_GET["category"]) && isset($_GET["content"]) && isset($_GET["tags"])) { + $post = new Post(); + + $post->setTitle($_GET["title"]); + $post->setContent($_GET["content"]); + $post->setCategory($_GET["category"]); + // $post->setAuthor(); + $tags = explode(",", $_GET["tags"]); + $tgs = array(); + foreach ($tags as $tag) { + var_dump($tag); + var_dump(Tag::getByName($tag)); + if(!(Tag::getByName($tag))) { + $tgs[] = Tag::add((new Tag())->setName($tag))->getId(); + } else { + $tgs[] = Tag::getByName($tag)->getId(); + } + } + // var_dump($tgs); + // die; + $post->setTags($tgs); + $post->setAuthor($_SESSION["author"]->getId()); + Post::add($post); + } + + return file_get_contents(DIR."/html/post_new.html"); + } +} diff --git a/assets/php/Controller/HomeController.php b/assets/php/Controller/HomeController.php index 2181877..dcda719 100644 --- a/assets/php/Controller/HomeController.php +++ b/assets/php/Controller/HomeController.php @@ -17,58 +17,12 @@ class HomeController extends Controller { } /** - * @route /^\/post\/new\/*$/ - */ - public function postAdd() { - - // var_dump($_SESSION["author"]); - // die; - - if(isset($_GET["title"]) && isset($_GET["category"]) && isset($_GET["content"]) && isset($_GET["tags"])) { - $post = new Post(); - - $post->setTitle($_GET["title"]); - $post->setContent($_GET["content"]); - $post->setCategory($_GET["category"]); - // $post->setAuthor(); - $tags = explode(",", $_GET["tags"]); - $tgs = array(); - foreach ($tags as $tag) { - $new_tag = explode(":", $tag); - if(count($new_tag) > 1) { - $t = new Tag(); - $t->setName($new_tag[1]); - $tgs[] = Tag::add($t)->getId(); - } else { - $tgs[] = $tag; - } - } - $post->setTags($tgs); - $post->setAuthor($_SESSION["author"]->getId()); - Post::add($post); - } - - return file_get_contents(DIR."/html/post_new.html"); - } - - /** - * @route /^\/post\/[a-z0-9]+\/$/ + * @route /^\/post\/[0-9]+\/$/ */ public function post() { return file_get_contents(DIR."/html/post.html"); } - /** - * @route /^\/post\/[a-z0-9]+\/edit\/$/ - */ - public function postEdit() { - return file_get_contents(DIR."/html/post_edit.html"); - } - - - - - /** * @route /^\/search\// */ diff --git a/assets/php/Controller/LoginController.php b/assets/php/Controller/LoginController.php new file mode 100644 index 0000000..ffdb0fb --- /dev/null +++ b/assets/php/Controller/LoginController.php @@ -0,0 +1,54 @@ +checkPassword($_POST["password"])) { + $_SESSION["author"] = $user; + if(isset($_GET["redirect"])) header("Location: " . $_GET["redirect"]); + header("Location: /"); + } + else var_dump("login incorreect"); + } + + return file_get_contents(DIR."/html/login.html"); + } + + /** + * @route /^\/logout\/$/ + */ + public function logout() { + session_destroy(); + header("Location: /"); + } + + /** + * @route /^\/register\/$/ + */ + public function register() { + if(isset($_POST["password"]) && isset($_POST["username"]) && Author::getByUsername($_POST["username"]) === null) { + $user = new Author(); + $user->setUsername($_POST["username"]); + $user->setPassword($_POST["password"]); + $user = Author::add($user); + $_SESSION["author"] = $user; + header("Location: /"); + } + return file_get_contents(DIR."/html/register.html"); + } + +} diff --git a/assets/php/Tags/Article.php b/assets/php/Tags/Article.php index 453adfe..96ffca2 100644 --- a/assets/php/Tags/Article.php +++ b/assets/php/Tags/Article.php @@ -32,7 +32,9 @@ class Article extends Tag { Functions::appendHTML($pok->parentNode, $post->getContent()); } elseif($attr == "category") { - $txt = $doc->createTextNode($post->getCategory()->getName()); + if($post->getCategory() != null) $t = $post->getCategory()->getName(); + else $t = ""; + $txt = $doc->createTextNode($t); $pok->parentNode->insertBefore($txt, $pok); } else { $col = "get" . ucfirst($attr); @@ -41,9 +43,14 @@ class Article extends Tag { } $finder = new DomXPath($doc); - $nodes = $finder->query("//*[contains(@class, 'column-cat')]"); + $nodes = $finder->query("//*[contains(@class, 'article-cat')]"); - if(count($nodes) >= 1) $nodes[0]->setAttribute("class", str_replace("column-cat", $post->getCategory()->getName() , $nodes[0]->getAttribute("class"))); + + + if(count($nodes) >= 1) { + if($post->getCategory() != null) $nodes[0]->setAttribute("class", str_replace("article-cat", $post->getCategory()->getName() , $nodes[0]->getAttribute("class"))); + else $nodes[0]->setAttribute("class", str_replace("article-cat", "", $nodes[0]->getAttribute("class"))); + } } } diff --git a/assets/php/Tags/Editor.php b/assets/php/Tags/Editor.php index 0ad4f20..4393936 100644 --- a/assets/php/Tags/Editor.php +++ b/assets/php/Tags/Editor.php @@ -5,10 +5,13 @@ namespace App\Tags; use App\DB\Category; use DateTime; use App\DB\Tag; +use App\DB\Post; class Editor extends \App\Tags\Tag { public function render() { + if($_GET["post"] == "new") $_GET["post"] = null; + else $post = Post::get($_GET["post"]); //recuperation de la balise de base (pouet) $pok = $this->getElement(); //recuperation du document (necessaire a la création de balises @@ -20,9 +23,9 @@ class Editor extends \App\Tags\Tag { case 'categories': $option = $doc->createElement("option"); $text = $doc->createTextNode("Categorie"); - $option->setAttribute("value", "0"); + $option->setAttribute("value", "1"); $option->setAttribute("disabled", "true"); - $option->setAttribute("selected", "selected"); + if(!isset($post)) $option->setAttribute("selected", "selected"); $option->appendChild($text); $pok->parentNode->insertBefore($option, $pok); foreach (Category::list() as $cat) { @@ -30,15 +33,18 @@ class Editor extends \App\Tags\Tag { $text = $doc->createTextNode($cat->getName()); $option->appendChild($text); $option->setAttribute("value", $cat->getId()); + if(isset($post) && $post->getCategory()->getId() == $cat->getId()) $option->setAttribute("selected", "selected"); $pok->parentNode->insertBefore($option, $pok); } break; case 'datetime': - $dt = new DateTime(); - $pok->parentNode->insertBefore($doc->createTextNode($dt->format('d/m/Y H:i:s')), $pok); + if(isset($post)) $txt = $post->getDateTime(); + else $txt = (new DateTime())->format('d/m/Y H:i:s'); + $pok->parentNode->insertBefore($doc->createTextNode($txt), $pok); break; case 'content': $tarea = $doc->createElement("textarea"); + if(isset($post)) $tarea->appendChild($doc->createTextNode($post->getContent())); $tarea->setAttribute("style", "width: 100%; min-height: 200px"); $pok->parentNode->insertBefore($tarea, $pok); break; @@ -46,6 +52,7 @@ class Editor extends \App\Tags\Tag { $input = $doc->createElement("input"); $input->setAttribute("style", "width: 100%"); $input->setAttribute("placeholder", "titre"); + if(isset($post)) $input->setAttribute("value", $post->getTitle()); $pok->parentNode->insertBefore($input, $pok); break; case 'tags': @@ -53,6 +60,10 @@ class Editor extends \App\Tags\Tag { $tg = $doc->createElement("input"); $tg->setAttribute("id", $el->getId()); $tg->setAttribute("type", "checkbox"); + $tg->setAttribute("data-text", $el->getName()); + if(isset($post)) { + if(in_array($el, $post->getTags())) $tg->setAttribute("checked", "checked"); + } $txt = $doc->createElement("label"); $txt->appendChild($doc->createTextNode($el->getName())); $txt->setAttribute("for", $el->getId()); diff --git a/assets/php/Tags/Loop.php b/assets/php/Tags/Loop.php index a880086..b827c3d 100644 --- a/assets/php/Tags/Loop.php +++ b/assets/php/Tags/Loop.php @@ -25,25 +25,22 @@ class Loop extends Tag { $isRecent = isset($_GET["recent"]) && $_GET["recent"] == "false" ? false : true; $category = isset($_GET["category"]) && intval($_GET["category"]) ? (int) $_GET["category"] : -1; + $category = $el->getAttribute("category") != '' ? Post::get($_GET["post"])->getCategory()->getId() : $category; $tag = isset($_GET["tag"]) && intval($_GET["tag"]) ? (int) $_GET["tag"] : -1; - if($el->getAttribute("category") != '') { - $posts = Post::listByCategory(Post::get($_GET["post"])->getCategory()->getId(), $isRecent, 6); - $postsList = array(); - foreach ($posts as $post) { - if($post->getId() != $_GET["post"]) $postsList[] = $post; - } - $posts = $postsList; - } else { - $posts = Post::list(true, 6); - - } - if($category != -1) { - $posts = Post::listByCategory($category, $isRecent, 20); + $posts = Post::listByCategory($category, $isRecent, $limit); + if(isset($_GET["post"])) { + $postsList = array(); + foreach ($posts as $post) { + if($post->getId() != $_GET["post"]) $postsList[] = $post; + } + $posts = $postsList; + } } else { - $posts = Post::list($isRecent, 10); + $posts = Post::list($isRecent, $limit); } + if($tag != -1) { $tposts = array(); foreach ($posts as $post) { @@ -68,9 +65,11 @@ class Loop extends Tag { foreach ($elements as $ele) { if($ele->getAttribute("column") == "content") { - Functions::appendHTML($ele->parentNode, $posts[$i]->getShort()); + Functions::appendHTML($ele->parentNode, substr($posts[$i]->getContent(), 0, 255)); } elseif($ele->getAttribute("column") == "category") { - $txt = $doc->createTextNode($posts[$i]->getCategory()->getName()); + // var_dump($posts[$i]->getCategory()->getName()); + if($posts[$i]->getCategory() != null) $txt = $doc->createTextNode($posts[$i]->getCategory()->getName()); + else $txt = $doc->createTextNode("no category"); $ele->parentNode->insertBefore($txt, $ele); } else { $col = 'get' . ucfirst($ele->getAttribute("column")); @@ -80,9 +79,12 @@ class Loop extends Tag { } $finder = new DomXPath($doc); - $nodes = $finder->query("//*[contains(@class, 'column-cat')]"); + $nodes = $finder->query("//*[contains(@class, 'column-category')]"); - if(count($nodes) >= 1) $nodes[0]->setAttribute("class", str_replace("column-category", $posts[$i]->getCategory()->getName() , $nodes[0]->getAttribute("class"))); + if(count($nodes) >= 1) { + if($posts[$i]->getCategory() != null) $nodes[0]->setAttribute("class", str_replace("column-category", $posts[$i]->getCategory()->getName() , $nodes[0]->getAttribute("class"))); + else $nodes[0]->setAttribute("class", str_replace("column-category", "", $nodes[0]->getAttribute("class"))); + } $nodes = $finder->query("//*[contains(@class, 'column-link')]"); diff --git a/assets/php/Tags/User.php b/assets/php/Tags/User.php new file mode 100644 index 0000000..bcbd976 --- /dev/null +++ b/assets/php/Tags/User.php @@ -0,0 +1,10 @@ +getElement(); + if(isset($_SESSION["author"])) $el->parentNode->insertBefore($this->getDoc()->createTextNode($_SESSION["author"]->getId()), $el); + } +} diff --git a/assets/php/db/Author.php b/assets/php/db/Author.php index efca6c9..b4b0279 100644 --- a/assets/php/db/Author.php +++ b/assets/php/db/Author.php @@ -14,6 +14,8 @@ class Author { private $job; + private $role = "ROLE_USER"; + public function __construct(){} public function getId() { @@ -36,6 +38,10 @@ class Author { return $this->job; } + public function getRole() { + return $this->role; + } + public function setId($id) { $this->id = $id; } @@ -45,13 +51,21 @@ class Author { } public function setPassword($password) { - $this->password = \password_hash($password, PASSWORD_DEFAULT); + $this->password = password_hash($password, PASSWORD_DEFAULT); + } + + public function setHashedPassword($password) { + $this->password = $password; } public function setJob($job) { $this->job = $job; } + public function setRole($role) { + $this->role = $role; + } + @@ -60,11 +74,13 @@ class Author { public static function fromArray($array) { + if($array == false) return null; $au = new Self(); $au->setId($array["id"]); $au->setUsername($array["username"]); - $au->setPassword($array["password"]); + $au->setHashedPassword($array["password"]); $au->setJob($array["job"]); + $au->setRole($array["role"]); return $au; } @@ -88,16 +104,33 @@ class Author { return Author::fromArray(Functions::connect()->query("SELECT * FROM users WHERE id=" . $id)->fetch()); } + public static function getByUsername(String $username) { + $query = "SELECT * FROM users WHERE username=:username"; + $prepared = Functions::connect()->prepare($query); + $prepared->bindValue(":username", $username); + $prepared->execute(); + return Author::fromArray($prepared->fetch()); + } + public static function add(Author $author) { - $query = "INSERT INTO author (id, username, password, job) - VALUES (NULL, ':username', ':password', ':job');"; + $query = "INSERT INTO users (id, username, password, job, role) + VALUES (NULL, :username, :password, :job, :role);"; + + $username = $author->getUsername(); + $password = $author->getPassword(); + $job = $author->getJob(); + $role = $author->getRole(); $pdo = Functions::connect(); $prepared = $pdo->prepare($query); - $prepared->bindParam(":username", $author->getUsername()); - $prepared->bindParam(":password", $author->getPassword()); - $prepared->bindParam(":job", $author->getjob()); + $prepared->bindParam(":username", $username); + $prepared->bindParam(":password", $password); + $prepared->bindParam(":job", $job); + $prepared->bindParam(":role", $role); $prepared->execute(); + // var_dump($prepared->errorInfo()); + // die; + return Author::list(true, 1)[0]; } public static function remove(Author $author) { diff --git a/assets/php/db/Post.php b/assets/php/db/Post.php index 537e571..5a7ebf4 100644 --- a/assets/php/db/Post.php +++ b/assets/php/db/Post.php @@ -69,7 +69,8 @@ class Post { } public function getCategory() { - return Category::get($this->category); + if($this->category != null) return Category::get($this->category); + else return null; } public function getAuthor() { @@ -87,10 +88,14 @@ class Post { public function getTags() { $temp = array(); if ($this->tags == null) return $temp; + /** @var int $tag */ foreach ($this->tags as $tag) { + // var_dump($tag); + // die; $temp[] = Tag::get($tag); } - return $temp; + // die; + return array_unique($temp, SORT_REGULAR); } @@ -129,7 +134,7 @@ class Post { * @param boolean $recent sort by most recent or not * @param integer $limit limit the number of result * - * @return array(Post) + * @return Post[] */ public static function list($recent = true, $limit = 100) { @@ -219,32 +224,42 @@ class Post { * */ public static function add(Post $post) { - $query = "INSERT INTO posts (id, title, content, categorie, author, dt) - VALUES (NULL, ':title', ':content', ':category', ':author', ':dt');"; + $query = "INSERT INTO posts (id, title, content, category, author, dt) + VALUES (NULL, :title, :content, :category, :author, :dt);"; - $title = $post->getTitle()); - $content = $post->getContent()); - $category = $post->getCategory()->getId(), PDO::PARAM_INT); - $author = $post->getAuthor()->getId(), PDO::PARAM_INT); - $dt = (new DateTime())->format("d/m/Y h:i:s")); + $title = $post->getTitle(); + $content = $post->getContent(); + $category = $post->getCategory()->getId(); + $author = $post->getAuthor()->getId(); + $dt = (new DateTime())->format("d/m/Y h:i:s"); $pdo = Functions::connect(); $prepared = $pdo->prepare($query); - $prepared->bindParam(":title", $post->getTitle()); - $prepared->bindParam(":content", $post->getContent()); - $prepared->bindParam(":category", $post->getCategory()->getId(), PDO::PARAM_INT); - $prepared->bindParam(":author", $post->getAuthor()->getId(), PDO::PARAM_INT); - $prepared->bindParam(":dt", (new DateTime())->format("d/m/Y h:i:s")); + $prepared->bindValue(":title", $title); + $prepared->bindValue(":content", $content); + $prepared->bindValue(":category", $category, PDO::PARAM_INT); + $prepared->bindValue(":author", $author, PDO::PARAM_INT); + $prepared->bindValue(":dt", $dt); - var_dump($prepared->execute(array( - ":title" => $post->getTitle(), - ":content" => $post->getContent(), - ":category" => $post->getCategory()->getId(), - ":author" => $post->getAuthor()->getId(), - ":dt" => (new DateTime())->format("d/m/Y h:i:s"), - ))); - var_dump("t"); + $prepared->execute(); + + $p = Post::list(true, 1)[0]->getId(); + + $tags = $post->getTags(); + var_dump($tags); + if(count($tags) >= 1) { + $q = "INSERT INTO post_tag (post_id, tag) VALUES ( :post , :tag )"; + $prepared = $pdo->prepare($q); + $prepared->bindValue(":post", $p); + foreach ($tags as $tg) { + $id = $tg->getId(); + $prepared->bindValue(":tag", $id); + + $prepared->execute(); + } + } + // var_dump($prepared->errorInfo()); } @@ -272,14 +287,39 @@ class Post { * */ public static function update(Post $post) { - Functions::connect()->prepare("UPDATE posts SET title=':title', content=':content', category=':category', author=':author', dt=':dt' WHERE id=:id")->execute(array( - ":title" => $post->getTitle(), - ":content" => $post->getContent(), - ":categorie" => $post->getCategorie(), - ":author" => $post->getAuthor(), - ":dt" => $post->getDt(), - ":id" => $post->getId() - )); + + $title = $post->getTitle(); + $content = $post->getContent(); + $category = $post->getCategory()->getId(); + $author = $post->getAuthor()->getId(); + $dt = $post->getDateTime(); + $id = $post->getid(); + + $pdo = Functions::connect(); + + + $prepared = $pdo->prepare("UPDATE posts SET title=:title, content=:content, category=:category, author=:author, dt=:dt WHERE id=:id"); + $prepared->bindValue(":title", $title); + $prepared->bindValue(":content", $content); + $prepared->bindValue(":category", $category, PDO::PARAM_INT); + $prepared->bindValue(":author", $author, PDO::PARAM_INT); + $prepared->bindValue(":dt", $dt); + $prepared->bindValue(":id", $id); + $prepared->execute(); + + $tags = $post->getTags(); + if(count($tags) >= 1) { + $pdo->exec("DELETE FROM post_tag WHERE post_id=" . $id); + $q = "INSERT INTO post_tag (post_id, tag) VALUES ( :post , :tag )"; + $prepared = $pdo->prepare($q); + $prepared->bindValue(":post", $id); + foreach ($tags as $tg) { + $id = $tg->getId(); + $prepared->bindValue(":tag", $id); + + $prepared->execute(); + } + } } } diff --git a/assets/php/db/Tag.php b/assets/php/db/Tag.php index 777d53b..29ae200 100644 --- a/assets/php/db/Tag.php +++ b/assets/php/db/Tag.php @@ -15,6 +15,7 @@ class Tag { public function __construct() {} public static function fromArray($array) { + if($array == false) return false; $tag = new Tag(); $tag->setId($array["id"]); $tag->setName($array["name"]); @@ -27,7 +28,7 @@ class Tag { * @param boolean $recent sort by most recent of less recent * @param int $limit * - * @return Categorie[] + * @return Tag[] */ public static function list($recent = true, $limit = 100) { $sort = $recent ? "DESC" : "ASC"; @@ -49,6 +50,23 @@ class Tag { return Tag::fromArray(Functions::connect()->query("SELECT * FROM tag WHERE id=" . $id)->fetch()); } + public static function getByName(String $name) { + $query = "SELECT * FROM tag WHERE name=:name LIMIT 1"; + $prepared = Functions::connect()->prepare($query); + $prepared->bindValue(":name", $name, PDO::PARAM_STR); + // $prepared->fetch(); + // $q = Functions::connect()->query("SELECT * FROM tag WHERE name=\"" . $name . "\""); + // if(!$q) return false; + $prepared->execute(); + $res = $prepared->fetch(PDO::FETCH_ASSOC); + // var_dump($res); + return Tag::fromArray($res); + // var_dump($name); + // var_dump($prepared->fetch()); + // var_dump($prepared->errorCode()); + // die; + } + /** * Undocumented function * @@ -62,10 +80,11 @@ class Tag { // var_dump($tag); + $name = $tag->getName(); + $pdo = Functions::connect(); $prepared = $pdo->prepare($query); - $prepared->execute(array(":name" => $tag->getName())); - + $prepared->execute(array(":name" => $name)); return Tag::list(true, 1)[0]; } @@ -108,5 +127,6 @@ class Tag { } public function setId($id) { $this->id = $id; + return $this; } } diff --git a/assets/php/handler.php b/assets/php/handler.php index b7ab599..6f8f9a6 100644 --- a/assets/php/handler.php +++ b/assets/php/handler.php @@ -6,7 +6,6 @@ use App\Tags\Tag; use App\Controller; use App\DB\Author; -session_start(); error_reporting(E_ALL); ini_set('display_errors', 'On'); @@ -14,7 +13,17 @@ ini_set('display_errors', 'On'); /** @var Composer\Autoload\ClassLoader $loader */ $loader = require "../../vendor/autoload.php"; -$_SESSION["author"] = Author::list(true, 1)[0]; +// $_SESSION["author"] = Author::list(true, 1)[0]; + +$auth = new Author(); //chargement de la class Author avant le début de session (pour que la classe dans la session rester complète) + +session_start(); + +if(isset($_SESSION["author"])) { //wip + $_SESSION["author"] = Author::get($_SESSION["author"]->getId()); +} + +// var_dump($_SESSION["author"]); define("DIR", str_replace("/php", "", __DIR__)); diff --git a/favicon.ico b/favicon.ico new file mode 100644 index 0000000..71447e4 Binary files /dev/null and b/favicon.ico differ