Skip to content

Commit

Permalink
added User to the framework core project
Browse files Browse the repository at this point in the history
  • Loading branch information
revwhyte committed Feb 4, 2021
1 parent 11ff520 commit 4e41939
Show file tree
Hide file tree
Showing 13 changed files with 415 additions and 9 deletions.
4 changes: 4 additions & 0 deletions mvc/app/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php
// load config
require_once 'config/config.php';
// load helpers
require_once 'helpers/url_helper.php';
require_once 'helpers/session_helper.php';
// add more helpers here

// autoload core libs
spl_autoload_register(function($className) {
Expand Down
178 changes: 178 additions & 0 deletions mvc/app/controllers/Users.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php
class Users extends Controller{
public function __construct() {
$this->userModel = $this->model('User');
}

public function register() {
// check for POST
if($_SERVER['REQUEST_METHOD'] == 'POST') {
// process form

// sanitize POST data
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

// init data
$data = [
'name' => trim($_POST['name']),
'email' => trim($_POST['email']),
'password' => trim($_POST['password']),
'confirm_password' => trim($_POST['confirm_password']),
'name_err' => '',
'email_err' => '',
'password_err' => '',
'confirm_password_err' => '',
];

// validate name
if(empty($data['name'])) {
$data['name_err'] = 'Please enter name';
}

// validate email
if(empty($data['email'])) {
$data['email_err'] = 'Please enter e-mail';
}
else {
// check email
if($this->userModel->findUserByEmail($data['email'])) {
$data['email_err'] = 'E-mail is already taken';
}
}

// validate password
if(empty($data['password'])) {
$data['password_err'] = 'Please enter password';
}
// For some haunted reason, this isnt working at all! '-'
// if(strlen($data['password'] <= 6)) {
// $data['password_err'] = 'Password must have at least 6 characters';
// }

// validate confirm password
if(empty($data['confirm_password'])) {
$data['confirm_password_err'] = 'Please confirm password';
} else {
if($data['password'] != $data['confirm_password']) {
$data['confirm_password_err'] = 'Passwords do not match';
}
}

// make sure errors are empty
if(empty($data['name_err']) && empty($data['email_err']) && empty($data['password_err']) && empty($data['confirm_password_err'])) {
// hash password
$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);

// register user
if($this->userModel->register($data)) {
flash('register_success', 'You are registered');
redirect('users/login');
} else {
die('something went wrong');
}
} else {
// load view with errors
$this->view('users/register', $data);
}
} else {
// init data
$data = [
'name' => '',
'email' => '',
'password' => '',
'confirm_password' => '',
'name_err' => '',
'email_err' => '',
'password_err' => '',
'confirm_password_err' => '',
];

// load view
$this->view('users/register', $data);
}
}

public function login() {
// check for POST
if($_SERVER['REQUEST_METHOD'] == 'POST') {
// sanitize POST data
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

// init data
$data = [
'email' => trim($_POST['email']),
'password' => trim($_POST['password']),
'email_err' => '',
'password_err' => '',
];

// validate email
if(empty($data['email'])) {
$data['email_err'] = 'Please enter e-mail';
}

// validate password
if(empty($data['password'])) {
$data['password_err'] = 'Please enter password';
}

// check for user/email
if($this->userModel->findUserByEmail($data['email'])) {
// user found
} else {
// user not found
$data['email_err'] = 'No user found';
}

if(empty($data['email_err']) && empty($data['password_err'])) {
// check and set logged user
$loggedInUser = $this->userModel->login($data['email'], $data['password']);

if($loggedInUser) {
// create session
// terminal($loggedInUser->id);
$this->createUserSession($loggedInUser);
} else {
$data['password_err'] = 'Password incorrect';

$this->view('users/login', $data);
}

} else {
$this->view('users/login', $data);
}

} else {
// init data
$data = [
'email' => '',
'password' => '',
'email_err' => '',
'password_err' => '',
];

// load view
$this->view('users/login', $data);
}
}

public function logout() {
unset($_SESSION['user_id']);
unset($_SESSION['user_email']);
unset($_SESSION['user_name']);

session_destroy();

redirect('users/login');
}

public function createUserSession($user) {
$_SESSION['user_id'] = $user->id;
$_SESSION['user_email'] = $user->email;
$_SESSION['user_name'] = $user->name;

redirect('posts');
}


}
34 changes: 34 additions & 0 deletions mvc/app/helpers/session_helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
session_start();

// flash message
// example: flash('register_success', 'you are now registered')
// display in view: echo flash('register_success');
function flash($name = '', $message = '', $class = 'alert alert-success') {
if(!empty($name)) {
if(!empty($message) && empty($_SESSION[$name])) {
if(!empty($_SESSION[$name])) {
unset($_SESSION[$name]);
}
if(!empty($_SESSION[$name.'_class'])) {
unset($_SESSION[$name.'_class']);
}

$_SESSION[$name] = $message;
$_SESSION[$name.'_class'] = $class;
} else if(empty($message) && !empty($_SESSION[$name])) {
$class = !empty($_SESSION[$name.'_class']) ? $_SESSION[$name.'_class'] : '';

echo "<div class='$class' id='msg-flash'>";
echo $_SESSION[$name];
echo "</div>";

unset($_SESSION[$name]);
unset($_SESSION[$name.'_class']);
}
}
}

function isLoggedIn() {
return isset($_SESSION['user_id']);
}
5 changes: 5 additions & 0 deletions mvc/app/helpers/url_helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
// simple page redirect
function redirect($page) {
header('location: ' . URLROOT . '/' . $page);
}
54 changes: 54 additions & 0 deletions mvc/app/models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
class User {
private $db;

public function __construct() {
$this->db = new Database;
}

// register user
public function register($data) {
$this->db->query('INSERT INTO users(name, email, password) VALUES (:name, :email, :password)');

// bind value
$this->db->bind(':name', $data['name']);
$this->db->bind(':email', $data['email']);
$this->db->bind(':password', $data['password']);

// execute
return $this->db->execute() ? true : false;

}

public function findUserByEmail($email) {
$this->db->query('SELECT * FROM users WHERE email = :email');
$this->db->bind(':email', $email);

$row = $this->db->single();

return $this->db->rowCount() > 0;
}

public function getUserById($id) {
$this->db->query('SELECT * FROM users WHERE id = :id');

$this->db->bind(':id', $id);

return $this->db->single();
}

public function login($email, $password) {
$this->db->query('SELECT * FROM users WHERE email = :email');
$this->db->bind(':email', $email);

$row = $this->db->single();

$hashed_password = $row->password;
if(password_verify($password, $hashed_password)) {
// match
return $row;
} else {
return false;
}
}
}
7 changes: 6 additions & 1 deletion mvc/app/views/inc/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo SITENAME; ?></title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<link rel='stylesheet' href="<?php echo URLROOT; ?>/css/style.css"/>
<link rel="icon" type="image/ico" href="<?php echo URLROOT; ?>/img/favicon.ico">
</head>
<body>
<body>
<?php require APPROOT . '/views/inc/navbar.php'; ?>
<div class="container">
51 changes: 51 additions & 0 deletions mvc/app/views/inc/navbar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<nav class="navbar navbar-expand-lg navbar-dark bg-dark mb-3">
<div class="container">
<a href="<?php echo URLROOT; ?>" class="navbar-brand">SharePosts</a>
<button
class="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarsExampleDefault"
aria-controls="navbarsExampleDefault"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="navbar-toggler-icon"></span>
</button>

<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a href="<?php echo URLROOT; ?>" class="nav-link">Home</a>
</li>
<?php if(isLoggedIn()): ?>
<li class="nav-item">
<a href="<?php echo URLROOT; ?>/posts" class="nav-link">Posts</a>
</li>
<?php endif; ?>
<li class="nav-item">
<a href="<?php echo URLROOT . '/pages/about'; ?>" class="nav-link">About</a>
</li>
</ul>

<ul class="navbar-nav ml-auto">
<?php if(isset($_SESSION['user_id'])): ?>
<li class="nav-item">
<!-- <a href="#" class="nav-link"><i class="fa fa-user-o"></i> <?php echo $_SESSION['user_name']; ?></a> -->
<a href="#" class="nav-link">Hi, <?php echo $_SESSION['user_name']; ?>! <i class="fa fa-hand-peace-o"></i></a>
</li>
<li class="nav-item">
<a href="<?php echo URLROOT . '/users/logout'; ?>" class="nav-link">Logout</a>
</li>
<?php else: ?>
<li class="nav-item">
<a href="<?php echo URLROOT . '/users/register'; ?>" class="nav-link">Register</a>
</li>
<li class="nav-item">
<a href="<?php echo URLROOT . '/users/login'; ?>" class="nav-link">Login</a>
</li>
<?php endif; ?>
</ul>
</div>
</div>
</nav>
3 changes: 2 additions & 1 deletion mvc/app/views/pages/about.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php require APPROOT . '/views/inc/header.php'; ?>

<h1><?php echo $data['title']; ?></h1>
<p>This is the MVC PHP framework. Please refer to the Docs on how to use it.</p>
<p><?php echo $data['description']; ?></p>
<p>Version: <strong><?php echo APPVERSION; ?></strong></p>

<?php require APPROOT . '/views/inc/footer.php'; ?>
11 changes: 8 additions & 3 deletions mvc/app/views/pages/index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php require APPROOT . '/views/inc/header.php'; ?>

<h1><?php echo $data['title']; ?></h1>
<p>This is the MVC PHP framework. Please refer to the Docs on how to use it.</p>
<div class="jumbotron jumbotron-fluid text-center">
<div class="container">
<h1 class="display-3">
<?php echo $data['title']; ?>
</h1>
<p class="lead"><?php echo $data['description']; ?></p>
</div>
</div>
<?php require APPROOT . '/views/inc/footer.php'; ?>
Loading

0 comments on commit 4e41939

Please sign in to comment.