init commit

This commit is contained in:
bckelley
2024-01-25 09:52:59 -06:00
commit a979c73924
33 changed files with 1870 additions and 0 deletions
+53
View File
@@ -0,0 +1,53 @@
<?php
/**
* Pages Controller Class
*
* This class handles the main pages of the application.
*/
class Pages extends Controller {
/**
* Constructor method.
*/
public function __construct(){
// Constructor logic (if any)
}
/**
* Index Method
*
* Handles the homepage of the application.
*/
public function index(){
// Check if the user is logged in
if (isLoggedIn()) {
// Redirect to the posts index page
redirect('/posts/index');
}
// Data for the view
$data = [
'title' => 'Home',
'description' => '',
];
// Load the view with the specified data
$this->view('pages/index', $data);
}
/**
* About Method
*
* Handles the about us page of the application.
*/
public function about(){
// Data for the view
$data = [
'title' => 'About Us',
'description' => '',
'version' => APP_VERSION,
];
// Load the view with the specified data
$this->view('pages/about', $data);
}
}
+222
View File
@@ -0,0 +1,222 @@
<?php
/**
* Posts Controller Class
*
* This class handles the functionality related to posts in the application.
*/
class Posts extends Controller {
private $postModel;
private $userModel;
/**
* Constructor method.
*/
public function __construct() {
// Check if the user is logged in
if (!isLoggedIn()) {
// Redirect to the login page if not logged in
redirect('/users/login');
}
// Initialize models
$this->postModel = $this->model('Post');
$this->userModel = $this->model('User');
}
/**
* Index Method
*
* Displays a list of posts.
*/
public function index() {
// Get all posts
$posts = $this->postModel->getPosts();
// Data for the view
$data = [
'posts' => $posts,
];
// Load the view with the specified data
$this->view('/posts/index', $data);
}
/**
* Show Method
*
* Displays a single post.
*
* @param int $id Post ID
*/
public function show($id) {
// Get post and user information
$post = $this->postModel->getPost($id);
$user = $this->userModel->getUserById($post->user_id);
// Data for the view
$data = [
'post' => $post,
'user' => $user,
];
// Load the view with the specified data
$this->view('/posts/show', $data);
}
/**
* Add Method
*
* Adds a new post.
*/
public function add() {
// Check if the form is submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Sanitize input data
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);
// Extract form data
$data = [
'title' => trim($_POST['title']),
'summary' => trim($_POST['summary']),
'body' => trim($_POST['body']),
'userId' => $_SESSION['userId'],
'titleErr' => '',
'summaryErr' => '',
'bodyErr' => '',
];
// Validate form data
if (empty($data['title'])) {
$data['titleErr'] = 'Please enter a title.';
}
if (empty($data['summary'])) {
$data['summaryErr'] = 'Please enter a summary.';
}
if (empty($data['body'])) {
$data['bodyErr'] = 'Please enter a body.';
}
// Check for errors
if (empty($data['titleErr']) && empty($data['summaryErr']) && empty($data['bodyErr'])) {
// Add post to the database
if ($this->postModel->addPost($data)) {
message('post_message', 'Your post has been added.', 'alert alert-success');
redirect('/posts/index');
} else {
die('Something went wrong.');
}
} else {
// Load the view with the specified data and errors
$this->view('/posts/add', $data);
}
} else {
// Display the form
$data = [
'title' => '',
'summary' => '',
'body' => '',
];
// Load the view with the specified data
$this->view('/posts/add', $data);
}
}
/**
* Edit Method
*
* Edits an existing post.
*
* @param int $id Post ID
*/
public function edit($id) {
// Check if the form is submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Sanitize input data
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);
// Extract form data
$data = [
'id' => $id,
'title' => trim($_POST['title']),
'summary' => trim($_POST['summary']),
'body' => trim($_POST['body']),
'userId' => $_SESSION['userId'],
'titleErr' => '',
'summaryErr' => '',
'bodyErr' => '',
];
// Validate form data
if (empty($data['title'])) {
$data['titleErr'] = 'Please enter a title.';
}
if (empty($data['summary'])) {
$data['summaryErr'] = 'Please enter a summary.';
}
if (empty($data['body'])) {
$data['bodyErr'] = 'Please enter a body.';
}
// Check for errors
if (empty($data['titleErr']) && empty($data['summaryErr']) && empty($data['bodyErr'])) {
// Update post in the database
if ($this->postModel->updatePost($data)) {
message('post_message', 'Your post has been updated.', 'alert alert-success');
redirect('/posts/index');
} else {
die('Something went wrong.');
}
} else {
// Load the view with the specified data and errors
$this->view('/posts/edit', $data);
}
} else {
// Display the form with existing post data
$post = $this->postModel->getPost($id);
// Check if the user is authorized to edit the post
if ($post->user_id != $_SESSION['userId']) {
redirect('/posts/index');
}
// Data for the view
$data = [
'id' => $id,
'title' => $post->title,
'summary' => $post->summary,
'body' => $post->body,
];
// Load the view with the specified data
$this->view('/posts/edit', $data);
}
}
/**
* Delete Method
*
* Deletes a post.
*
* @param int $id Post ID
*/
public function delete($id) {
// Check if the form is submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Delete post from the database
if ($this->postModel->deletePost($id)) {
message('post_message', 'Your post has been removed.');
redirect('/posts/index');
} else {
die('Something went wrong');
}
} else {
// Redirect to the posts index page if form is not submitted
redirect('/posts/index');
}
}
}
+204
View File
@@ -0,0 +1,204 @@
<?php
/**
* Users Controller Class
*
* This class handles user-related functionality such as registration, login, and logout.
*/
class Users extends Controller {
private $userModel;
/**
* Constructor method.
*/
public function __construct() {
// Initialize the user model
$this->userModel = $this->model('User');
}
/**
* Register Method
*
* Handles user registration.
*/
public function register() {
// Check if the form is submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Sanitize input data
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);
// Extract form data
$data = [
'username' => trim($_POST['username']),
'email' => trim($_POST['email']),
'password' => trim($_POST['password']),
'again' => trim($_POST['again']),
'usernameErr' => '',
'emailErr' => '',
'passErr' => '',
'againErr' => '',
];
// Validate form data
if (empty($data['email'])) {
$data['emailErr'] = 'Please provide an email.';
} else {
if ($this->userModel->findUserByEmail($data['email'])) {
$data['emailErr'] = 'Email exists.';
}
}
if (empty($data['username'])) {
$data['usernameErr'] = 'Please provide a valid username.';
}
if (empty($data['password'])) {
$data['passErr'] = 'Please provide a valid password.';
} elseif (strlen($data['password']) < 6) {
$data['passErr'] = 'Please provide a password 6 or more characters in length.';
}
if (empty($data['again'])) {
$data['againErr'] = 'Please confirm your password.';
} else {
if ($data['password'] != $data['again']) {
$data['passErr'] = 'Passwords do not match.';
}
}
// Check for errors
if (empty($data['usernameErr']) && empty($data['emailErr']) &&
empty($data['passErr']) && empty($data['againErr'])) {
// Hash the password
$data['password'] = password_hash($data['password'], PASSWORD_BCRYPT);
// Register the user
if ($this->userModel->register($data)) {
message('reg_success', 'You are now registered and can login.', 'alert alert-success');
redirect('/users/login');
} else {
die('Something went wrong.');
}
} else {
// Load the view with the specified data and errors
$this->view('users/register', $data);
}
} else {
// Display the registration form
$data = [
'username' => '',
'email' => '',
'password' => '',
'again' => '',
'usernameErr' => '',
'emailErr' => '',
'passErr' => '',
'againErr' => '',
];
// Load the view with the specified data
$this->view('users/register', $data);
}
}
/**
* Login Method
*
* Handles user login.
*/
public function login() {
// Check if the form is submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Sanitize input data
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);
// Extract form data
$data = [
'email' => trim($_POST['email']),
'password' => trim($_POST['password']),
'emailErr' => '',
'passErr' => '',
];
// Validate form data
if (empty($data['email'])) {
$data['emailErr'] = 'Please provide an email.';
}
if (empty($data['password'])) {
$data['passErr'] = 'Please provide a valid password.';
} elseif (strlen($data['password']) < 6) {
$data['passErr'] = 'Please provide a password 6 or more characters in length.';
}
// Check if the user exists
if ($this->userModel->findUserByEmail($data['email'])) {
} else {
$data['emailErr'] = 'No user found.';
}
// Check for errors
if (empty($data['emailErr']) && empty($data['passErr'])) {
// Attempt to log in the user
$loggedIn = $this->userModel->login($data['email'], $data['password']);
if ($loggedIn) {
$this->createUserSession($loggedIn);
} else {
$data['passErr'] = 'Password was incorrect.';
}
} else {
// Load the view with the specified data and errors
$this->view('users/login', $data);
}
} else {
// Display the login form
$data = [
// 'username' => '',
'email' => '',
'password' => '',
// 'usernameErr' => '',
'emailErr' => '',
'passErr' => '',
];
// Load the view with the specified data
$this->view('users/login', $data);
}
}
/**
* createUserSession Method
*
* Creates a user session upon successful login.
*
* @param object $user User object
*/
public function createUserSession($user) {
// Set session variables
$_SESSION['userId'] = $user->id;
$_SESSION['username'] = $user->name;
$_SESSION['userEmail'] = $user->email;
// Redirect to the posts index page
redirect('/posts/index');
}
/**
* Logout Method
*
* Logs out the user.
*/
public function logout() {
// Unset session variables
unset($_SESSION['userId']);
unset($_SESSION['username']);
unset($_SESSION['userEmail']);
// Redirect to the login page
redirect('/users/login');
}
}