diff --git a/blog/.DS_Store b/blog/.DS_Store new file mode 100755 index 0000000..35e269e Binary files /dev/null and b/blog/.DS_Store differ diff --git a/blog/._.DS_Store b/blog/._.DS_Store new file mode 100755 index 0000000..28c42fb Binary files /dev/null and b/blog/._.DS_Store differ diff --git a/blog/autoload.php b/blog/autoload.php new file mode 100755 index 0000000..aa87f1d --- /dev/null +++ b/blog/autoload.php @@ -0,0 +1,35 @@ + diff --git a/blog/public/index.php b/blog/public/index.php new file mode 100755 index 0000000..4ce5f9b --- /dev/null +++ b/blog/public/index.php @@ -0,0 +1,71 @@ + [ + 'controller' => 'loginController', + 'method' => 'login' + ], + '/logout' => [ + 'controller' => 'loginController', + 'method' => 'logout' + ], + '/homepages' => [ + 'controller' => 'loginController', + 'method' => 'homepages' // show Methode anwenden + ], + '/index' => [ + 'controller' => 'postsController', + 'method' => 'index' + ], + '/index_c' => [ + 'controller' => 'postsController', + 'method' => 'index_c' + ], + '/dashboard' => [ + 'controller' => 'loginController', + 'method' => 'dashboard' + ], + '/post' => [ + 'controller' => 'postsController', + 'method' => 'comment' //'show' //'comments' + ], + '/post_c' => [ + 'controller' => 'postsController', + 'method' => 'show' //'show' //'comments' + ], + '/posts-admin' => [ + 'controller' => 'postsAdminController', + 'method' => 'index' + ], + '/posts-admin_c' => [ + 'controller' => 'postsAdminController', + 'method' => 'post_c' //'method' => 'index_c' + ], + '/posts-edit' => [ + 'controller' => 'postsAdminController', + 'method' => 'edit' + ], + '/posts-edit_c' => [ + 'controller' => 'postsAdminController', + 'method' => 'edit_c' + ], + '/impressum' => [ + 'controller' => 'loginController', + 'method' => 'impressum' // index Methode anwenden + ], + ]; + //var_dump($routes[$pathInfo]); die(); + if (isset($routes[$pathInfo])) { + $route = $routes[$pathInfo]; + $controller = $container->make($route['controller']); + $method = $route['method']; + //echo "method: ".$method." - "; + //var_dump($controller); die(); + $controller->$method(); + } + + ?> diff --git a/blog/src/Blog/Post.php b/blog/src/Blog/Post.php new file mode 100755 index 0000000..74d7c3e --- /dev/null +++ b/blog/src/Blog/Post.php @@ -0,0 +1,22 @@ +user = new App\User\User(); + // durch use User\User; kann einfach geschrieben werden +// $this->user = new User(); + // durch use User\User as SomeUser kann eine neuer user aus der class User/user gebildet werden + $this->user = new SomeUser(); + } + } +?> diff --git a/blog/src/Blog/Postinterface.php b/blog/src/Blog/Postinterface.php new file mode 100755 index 0000000..49f3ed4 --- /dev/null +++ b/blog/src/Blog/Postinterface.php @@ -0,0 +1,10 @@ + diff --git a/blog/src/Core/AbstractController.php b/blog/src/Core/AbstractController.php new file mode 100755 index 0000000..672b3fb --- /dev/null +++ b/blog/src/Core/AbstractController.php @@ -0,0 +1,14 @@ + diff --git a/blog/src/Core/AbstractModel.php b/blog/src/Core/AbstractModel.php new file mode 100755 index 0000000..9f7ab54 --- /dev/null +++ b/blog/src/Core/AbstractModel.php @@ -0,0 +1,26 @@ +$offset); + } + + public function offsetGet ($offset) { + return $this->$offset; + } + + public function offsetSet ($offset, $value) { + $this->$offset = $value; + } + + public function offsetUnset ($offset) { + unset ($this->$offset); + } + } + + ?> diff --git a/blog/src/Core/AbstractRepository.php b/blog/src/Core/AbstractRepository.php new file mode 100755 index 0000000..c5044ea --- /dev/null +++ b/blog/src/Core/AbstractRepository.php @@ -0,0 +1,34 @@ +pdo = $pdo; + } + + abstract public function getTableName(); + abstract public function getModelName(); + + function all(){ + $table = $this->getTableName(); + $model = $this->getModelName(); + $stmt = $this->pdo->query("SELECT * FROM `$table`"); + $posts = $stmt->fetchAll(PDO::FETCH_CLASS, $model); + return $posts; + } + + function find($id){ + $table = $this->getTableName(); + $model = $this->getModelName(); + $stmt = $this->pdo->prepare("SELECT * FROM `$table` WHERE id = :id"); + $stmt->execute(['id' => $id]); + $stmt->setFetchMode(PDO::FETCH_CLASS, $model); + $post = $stmt->fetch(PDO::FETCH_CLASS); + return $post; + } +} + ?> diff --git a/blog/src/Core/Container.php b/blog/src/Core/Container.php new file mode 100755 index 0000000..8ea5b04 --- /dev/null +++ b/blog/src/Core/Container.php @@ -0,0 +1,89 @@ +receipts = [ + 'postsAdminController' => function() { + return new PostsAdminController( + $this->make("postsRepository"), + $this->make("commentsRepository"), + $this->make("loginService") + ); + }, + 'loginService' => function() { + return new LoginService( + $this->make("usersRepository") + ); + }, + 'loginController' => function() { + return new LoginController( + $this->make("loginService") + ); + }, + 'postsController' => function() { + return new PostsController( + $this->make('postsRepository'), + $this->make('commentsRepository') + ); + }, + 'postsRepository' => function() { + return new PostsRepository( + $this->make("pdo") + ); + }, + 'usersRepository' => function() { + return new UsersRepository( + $this->make("pdo") + ); + }, + 'commentsRepository' => function() { + return new CommentsRepository( + $this->make("pdo") + ); + }, + 'pdo' => function() { + $servername = "mysqle8e6.netcup.net"; + $port = "3306"; + $username = "k46054_hbc"; + $password = "cXZm/E97dKvZy6Cg*"; + $dbname="k46054_hbc"; + + $pdo = new PDO("mysql:host=$servername;dbname=$dbname;port=$port", $username, $password); + + $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); + return $pdo; + } + ]; + } + + public function make($name) + { + if (!empty($this->instances[$name])) + { + return $this->instances[$name]; + } + + if (isset($this->receipts[$name])) { + $this->instances[$name] = $this->receipts[$name](); + } + + return $this->instances[$name]; + } +} + ?> diff --git a/blog/src/Post/CommentModel.php b/blog/src/Post/CommentModel.php new file mode 100755 index 0000000..79ab19c --- /dev/null +++ b/blog/src/Post/CommentModel.php @@ -0,0 +1,16 @@ +getTableName(); + $stmt = $this->pdo->prepare( + "INSERT INTO `$table` (`content`, `post_id`) VALUES (:content, :postId)" + ); + $stmt->execute([ + 'content' => $content, + 'postId' => $postId + ]); + } + + public function update($postId, $content, $id, $org) { + echo "here"; + $table = $this->getTableName(); + $model = $this->getModelName(); + $stmt = $this->pdo->prepare( + "UPDATE `{$table}` SET `content` = :content, `post_id` = :postId, `original` = :original + WHERE `id` = :id"); + $stmt->execute([ + 'content' => $content, + 'postId' => $postId, + 'id' => $id, + 'original' => $org + ]); + } + + public function allByPost($id) { + $table = $this->getTableName(); + $model = $this->getModelName(); + + $stmt = $this->pdo->prepare("SELECT * FROM `$table` WHERE post_id = :id"); + $stmt->execute(['id' => $id]); + + $comments = $stmt->fetchAll(PDO::FETCH_CLASS, $model); + return $comments; + } + + public function allCommentsByID($id) { + $table = $this->getTableName(); + $model = $this->getModelName(); + + $stmt = $this->pdo->prepare("SELECT * FROM `$table` WHERE post_id = :id"); + $stmt->execute(['id' => $id]); + + $comments = $stmt->fetchAll(PDO::FETCH_CLASS, $model); + return $comments; + } + +} + ?> diff --git a/blog/src/Post/PostModel.php b/blog/src/Post/PostModel.php new file mode 100755 index 0000000..a8185bb --- /dev/null +++ b/blog/src/Post/PostModel.php @@ -0,0 +1,17 @@ + diff --git a/blog/src/Post/PostsAdminController.php b/blog/src/Post/PostsAdminController.php new file mode 100755 index 0000000..990c8d2 --- /dev/null +++ b/blog/src/Post/PostsAdminController.php @@ -0,0 +1,90 @@ +postsRepository = $postsRepository; + $this->commentsRepository = $commentsRepository; + $this->loginService = $loginService; + } + + public function index() { + $this->loginService->check(); + $all = $this->postsRepository->all(); + $this->render("post/admin/index", [ + 'all' => $all + ]); + } + + public function index_c() { + $this->loginService->check(); + $all = $this->postsRepository->all(); +// $comments = $this->commentsRepository->allCommentsByID($id); //allByPost($id); + $this->render("post/admin/index_c", [ + 'all' => $all, +// 'comments' => $comments + ]); + } + + public function getComment($id) { + $post = $this->postsRepository->find($id); + $comments = $this->commentsRepository->allByPost($id); + return $comments; + } + + public function edit_c() { + $id = getAndSetSession('id', 'id', '1'); + $this->loginService->check(); + $savedSuccess = false; + + if (!empty($_POST['id']) AND !empty($_POST['content'])) { + $content = $_POST['content']; + $postId = $_POST['post_id']; + $p_id = $_POST['id']; + $org = $_POST['original']; + $this->commentsRepository->update($postId, $content, $p_id, $org); + $savedSuccess = true; + } + + $post = $this->postsRepository->find($id); + $comments = $this->commentsRepository->allCommentsByID($id); + $this->render("post/admin/edit_c", [ + 'comments' => $comments, + 'post' => $post, + 'savedSuccess' => $savedSuccess + ]); + } + + public function edit() { + $this->loginService->check(); + $id = $_GET['id']; + $entry = $this->postsRepository->find($id); + $savedSuccess = false; + if (!empty($_POST['title']) AND !empty($_POST['content'])) { + $entry->title = $_POST['title']; + $entry->content = $_POST['content']; + $this->postsRepository->update($entry); + $savedSuccess = true; + } + $this->render("post/admin/edit", [ + 'entry' => $entry, + 'savedSuccess' => $savedSuccess + ]); + } + +} + ?> diff --git a/blog/src/Post/PostsController.php b/blog/src/Post/PostsController.php new file mode 100755 index 0000000..a77b689 --- /dev/null +++ b/blog/src/Post/PostsController.php @@ -0,0 +1,83 @@ +postsRepository = $postsRepository; + $this->commentsRepository = $commentsRepository; + } + + public function index() + { + $posts = $this->postsRepository->all(); + $this->render("post/index", [ + 'posts' => $posts + ]); + } + + public function index_c() + { + $post = $this->postsRepository->all(); //find($id); + $comments = $this->commentsRepository->allByPost($id); + $this->render("post/show", [ + 'post' => $post, + 'comments' => $comments + ]); + } + + + + public function comment() + { + $id = $_GET['id']; + if (isset($_POST['content'])) { + $content = $_POST['content']; + //$this->commentsRepository->insertForPost($id, $content); + } + $post = $this->postsRepository->find($id); + $comments = $this->commentsRepository->allByPost($id); + //$this->render("post/show", [ + $this->render("post/comment", [ + 'post' => $post, + 'comments' => $comments + ]); + } + + public function show() + { + $id = $_GET['id']; + echo "ID= ".$id; + //die(); + if (isset($_POST['content'])) { + $content = $_POST['content']; + var_dump($content); + //$this->commentsRepository->insertForPost($id, $content); + } + $post = $this->postsRepository->find($id); + $comments = $this->commentsRepository->allByPost($id); + //die(); + //$this->render("post/show", [ + $this->render("post/show", [ + 'post' => $post, + 'comments' => $comments + ]); + } + + public function getComment($id) + { + $post = $this->postsRepository->find($id); + $comments = $this->commentsRepository->allByPost($id); + return $comments; + } + +} + + ?> diff --git a/blog/src/Post/PostsRepository.php b/blog/src/Post/PostsRepository.php new file mode 100755 index 0000000..81c76c0 --- /dev/null +++ b/blog/src/Post/PostsRepository.php @@ -0,0 +1,32 @@ +getTableName(); + + $stmt = $this->pdo->prepare("UPDATE `{$table}` SET `content` = :content, `title` = :title WHERE `id` = :id"); + $stmt->execute([ + 'content' => $model->content, + 'title' => $model->title, + 'id' => $model->id + ]); + } +} + +?> diff --git a/blog/src/User/LoginController.php b/blog/src/User/LoginController.php new file mode 100755 index 0000000..4341a1e --- /dev/null +++ b/blog/src/User/LoginController.php @@ -0,0 +1,50 @@ +loginService = $loginService; + } + + public function dashboard(){ + $this->loginService->check(); + $this->render("user/dashboard", []); + } + + public function impressum(){ + $this->render("user/impressum", []); + } + + public function homepages(){ + $this->render("user/homepages", []); + } + + public function logout(){ + $this->loginService->logout(); + header("Location: login"); + } + + public function login(){ + $error = false; + if (!empty($_POST['username']) AND !empty($_POST['password'])) { + $username = $_POST['username']; + $password = $_POST['password']; + + if ($this->loginService->attempt($username, $password)) { + header("Location: dashboard"); + return; + } else { + $error = true; + } + } + $this->render("user/login", [ + 'error' => $error + ]); + } + +} + ?> diff --git a/blog/src/User/LoginService.php b/blog/src/User/LoginService.php new file mode 100755 index 0000000..49e3905 --- /dev/null +++ b/blog/src/User/LoginService.php @@ -0,0 +1,45 @@ +usersRepository = $usersRepository; + } + + public function check() { + if (isset($_SESSION['login'])) { + return true; + } else { + header("Location: login"); + die(); + } + } + + public function attempt($username, $password) { + $user = $this->usersRepository->findByUsername($username); + if (empty($user)) { + return false; + } + if (password_verify($password, $user->password)) { + $_SESSION['login'] = $user->username; + $_SESSION['rechte'] = $user->rechte; + + session_regenerate_id(true); + //var_dump($_SESSION); die(); + return true; + } else { + return false; + } + } + + public function logout() + { + unset($_SESSION['login']); + unset($_SESSION['rechte']); + session_regenerate_id(true); + } +} + ?> diff --git a/blog/src/User/UserModel.php b/blog/src/User/UserModel.php new file mode 100755 index 0000000..2f0a0dd --- /dev/null +++ b/blog/src/User/UserModel.php @@ -0,0 +1,13 @@ + diff --git a/blog/src/User/UsersRepository.php b/blog/src/User/UsersRepository.php new file mode 100755 index 0000000..7339620 --- /dev/null +++ b/blog/src/User/UsersRepository.php @@ -0,0 +1,33 @@ +getTableName(); + $model = $this->getModelName(); + $stmt = $this->pdo->prepare("SELECT * FROM `$table` WHERE username = :username"); + $stmt->execute(['username' => $username]); + $stmt->setFetchMode(PDO::FETCH_CLASS, $model); + $user = $stmt->fetch(PDO::FETCH_CLASS); + + return $user; + } +} + ?> diff --git a/blog/views/layout/css/navi.css b/blog/views/layout/css/navi.css new file mode 100755 index 0000000..d064c05 --- /dev/null +++ b/blog/views/layout/css/navi.css @@ -0,0 +1,52 @@ + diff --git a/blog/views/layout/footer.php b/blog/views/layout/footer.php new file mode 100755 index 0000000..d53f102 --- /dev/null +++ b/blog/views/layout/footer.php @@ -0,0 +1,10 @@ + + + +
+ + +