-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added User to the framework core project
- Loading branch information
Showing
13 changed files
with
415 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; ?> |
Oops, something went wrong.