Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final website #6

Open
wants to merge 125 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
125 commits
Select commit Hold shift + click to select a range
3e0ce2c
add files
TRPB Jan 12, 2017
eb7c540
set charset/error reporting
TRPB Jan 12, 2017
3a4d697
added create query
TRPB Jan 12, 2017
c696502
changed query to UPDATE
TRPB Jan 12, 2017
966e1c5
removed <p>
TRPB Jan 12, 2017
4d50c10
added database.sql
TRPB Jan 14, 2017
3019492
add files
TRPB Jan 14, 2017
a6484c6
switched to shorthand echo
TRPB Jan 14, 2017
be4eefe
uses layout file
TRPB Jan 14, 2017
ea9041d
uses output buffering to enable a jokes template
TRPB Jan 14, 2017
839ed0f
added addjoke template/link
TRPB Jan 14, 2017
c7534f1
add addjoke.php and change form styling
TRPB Jan 14, 2017
4441410
complete addjoke.php
TRPB Jan 14, 2017
f31bdda
add delete joke files
TRPB Jan 15, 2017
283445d
use __DIR__
TRPB Jan 15, 2017
a869886
author attribution
TRPB Feb 13, 2017
45dbbb3
styling fix
TRPB Feb 13, 2017
9b9c82c
added id to sql query
TRPB Feb 13, 2017
a5df19d
author attribution
TRPB Feb 13, 2017
5725357
added sample switcher
TRPB Feb 13, 2017
cdd3c8d
database import
TRPB Feb 13, 2017
8a98c61
removed sample switcher
TRPB Feb 18, 2017
fd1d14d
added backticks
TRPB Feb 18, 2017
32ca18f
moved database connection to its own file
TRPB Feb 18, 2017
931ca7d
added totalJokes function
TRPB Feb 18, 2017
bf9ed2c
Number of jokes are shown on the joke list
TRPB Feb 18, 2017
c458ec6
moved insert query into insertJoke function
TRPB Feb 18, 2017
883868d
added backticks
TRPB Feb 19, 2017
a684d38
delete joke
TRPB Mar 7, 2017
5704908
allJokes function
TRPB Mar 11, 2017
76e78f1
added edit page & relevant functions
TRPB Feb 18, 2017
69eb5a1
added backticks
TRPB Feb 19, 2017
3a169d5
updated joke functions
TRPB Mar 7, 2017
56c03d0
removed debugging code
TRPB Mar 7, 2017
8f82e4e
fixed error-now supports dates
TRPB Mar 7, 2017
ab0cbcc
added generic functions edit/find
TRPB Mar 12, 2017
ac27c5c
added processDates function
TRPB Mar 12, 2017
b6b4248
generic functions
TRPB Mar 12, 2017
5a45145
now uses save function
TRPB Mar 12, 2017
0069f35
added name to submit button
TRPB Mar 12, 2017
f6157ac
editjoke now uses an array
TRPB Mar 12, 2017
8e5133a
replaced implode with rtrim
TRPB Mar 12, 2017
12f58db
replaced implode with rtrim
TRPB Mar 12, 2017
8ef7c5e
replaced implode with rtrim
TRPB Mar 12, 2017
847690b
jokes.php now uses class instead of functions
TRPB Jun 7, 2017
8f6eb28
updated other controllers
TRPB Jun 7, 2017
05de742
single entry point
TRPB Jun 9, 2017
bdc9a69
missing $this
TRPB Jun 9, 2017
a33b121
moved JokeController into classes/controllers - lets us do namespacce…
TRPB Jun 10, 2017
40bd808
simpler index.php
TRPB Jun 9, 2017
38a6943
entrypoint 3
TRPB Jun 10, 2017
7167a0e
entrypoint with loadTemplate function
TRPB Jun 10, 2017
f72c95a
now works with any controller
TRPB Jun 10, 2017
d26acdd
controllers/ in wrong include
TRPB Jun 10, 2017
13c3e54
uses simpler url routing for easier refactoring later on
TRPB Jun 16, 2017
6d43f23
uses routes from the start rather than using controller/action vars t…
TRPB Jun 17, 2017
78e8091
added rest of the routes
TRPB Jun 17, 2017
cda8106
amended 301 case check
TRPB Jun 17, 2017
1459f83
uses friendly urls throughout
TRPB Jun 17, 2017
489a75a
fixed 301 recirect url
TRPB Jun 17, 2017
5f75cbd
entry point is now a class
TRPB Jun 17, 2017
5fe9983
moved router to its own file
TRPB Jun 17, 2017
9f46b49
remove autoloader
TRPB Jun 17, 2017
e8375ce
implemented autoloader
TRPB Jun 17, 2017
ae2623f
implemented autoloader
TRPB Jun 17, 2017
c89fbc0
moved everything to namespaces
TRPB Jun 19, 2017
dbcab36
now uses a generic router
TRPB Jun 19, 2017
5543bdf
added interface
TRPB Jun 19, 2017
b9d4d22
added registration form
TRPB Jun 21, 2017
f1176c6
added validation
TRPB Jun 21, 2017
f626a62
fixed filter
TRPB Jun 21, 2017
6335962
checks for duplicate users
TRPB Jun 21, 2017
ad0460d
password hashing
TRPB Jun 22, 2017
a26bf94
has login check
TRPB Jun 24, 2017
69b85ef
added error page
TRPB Jun 24, 2017
a240b9a
login form
TRPB Jun 24, 2017
7916168
logout button
TRPB Jun 24, 2017
0ff68de
now tracks users when jokes are posted
TRPB Jun 29, 2017
904c6bc
users can only see edit/delete buttons for their jokes
TRPB Jun 29, 2017
ae31126
extra security checks
TRPB Jun 29, 2017
99e75ca
added author class
TRPB Jul 22, 2017
a4ab5ea
fix bug: cannot add new jokes
TRPB Jul 22, 2017
36ff7bb
joke class
TRPB Jul 23, 2017
c456455
uses joke objects
TRPB Jul 23, 2017
5132aac
now has Joke object
TRPB Jul 23, 2017
d7ce322
fixed edit joke
TRPB Jul 23, 2017
99092a1
implement caching
TRPB Jul 23, 2017
b4eacea
edit category + database
TRPB Jul 25, 2017
7c2ccd3
category class
TRPB Jul 25, 2017
7722b24
allows adding/editing categories
TRPB Jul 25, 2017
665d252
saveEdit function
TRPB Jul 25, 2017
9d1e94d
category list page
TRPB Jul 25, 2017
22d6da9
delete action
TRPB Jul 25, 2017
980d0fa
add category link
TRPB Jul 25, 2017
063c695
add joke allows selecting category
TRPB Jul 25, 2017
2496ec5
jokes are assigned to categories
TRPB Jul 30, 2017
01fd776
uses objects for delete method
TRPB Jul 30, 2017
e844a67
category list on jokes page
TRPB Aug 9, 2017
9dd2f2c
category list
TRPB Aug 9, 2017
905b9e9
can now amend categories with checkboxes
TRPB Aug 9, 2017
f1dd0e4
router permissions check
TRPB Aug 10, 2017
d1ff6f8
edit permissions page
TRPB Aug 10, 2017
677cdba
fix route
TRPB Aug 10, 2017
8940cd3
binary permissions complete
TRPB Aug 11, 2017
3d632cb
added markdown parser
TRPB Aug 15, 2017
415d59c
better column sizes
TRPB Aug 15, 2017
3d51dd8
sorting
TRPB Aug 17, 2017
6348689
added limit
TRPB Aug 17, 2017
9e0570c
added pagination
TRPB Aug 17, 2017
4c322f6
limit/offset for categories
TRPB Aug 17, 2017
7a0c931
added sample switcher
TRPB Oct 4, 2017
f655886
added registration link
TRPB Oct 4, 2017
308402e
updated template syntax
TRPB Oct 5, 2017
e45d174
updated template syntax
TRPB Oct 5, 2017
ceb7f2d
updated template syntax
TRPB Oct 5, 2017
20b424f
updated template syntax
TRPB Oct 5, 2017
d0f6f9b
added sample switcher
TRPB Oct 5, 2017
304add2
updated sample switcher: supports custom samples
TRPB Oct 5, 2017
f22210a
updated sample switcher: supports custom samples
TRPB Oct 5, 2017
85a3997
updated sample switcher: supports custom samples
TRPB Oct 5, 2017
d0f7884
updated sample switcher: supports custom samples
TRPB Oct 6, 2017
e00eaa9
updated sample switcher: supports custom samples
TRPB Oct 6, 2017
eeac7c1
Updated login.php to fix logout issue
alessio-levrero Feb 27, 2018
8fbc700
Fixed some db issues
alessio-levrero Feb 27, 2018
54cd6c6
Merge pull request #3 from alessio-levrero/patch-1
TRPB Feb 27, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions classes/Ijdb/Controllers/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
namespace Ijdb\Controllers;

class Category {
private $categoriesTable;

public function __construct(\Ninja\DatabaseTable $categoriesTable) {
$this->categoriesTable = $categoriesTable;
}

public function edit() {

if (isset($_GET['id'])) {
$category = $this->categoriesTable->findById($_GET['id']);
}

$title = 'Edit Category';

return ['template' => 'editcategory.html.php',
'title' => $title,
'variables' => [
'category' => $category ?? null
]
];
}

public function saveEdit() {
$category = $_POST['category'];

$this->categoriesTable->save($category);

header('location: /category/list');
}

public function list() {
$categories = $this->categoriesTable->findAll();

$title = 'Joke Categories';

return ['template' => 'categories.html.php',
'title' => $title,
'variables' => [
'categories' => $categories
]
];
}

public function delete() {
$this->categoriesTable->delete($_POST['id']);

header('location: /category/list');
}
}
113 changes: 113 additions & 0 deletions classes/Ijdb/Controllers/Joke.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
namespace Ijdb\Controllers;
use \Ninja\DatabaseTable;
use \Ninja\Authentication;

class Joke {
private $authorsTable;
private $jokesTable;
private $categoriesTable;
private $authentication;

public function __construct(DatabaseTable $jokesTable, DatabaseTable $authorsTable, DatabaseTable $categoriesTable, Authentication $authentication) {
$this->jokesTable = $jokesTable;
$this->authorsTable = $authorsTable;
$this->categoriesTable = $categoriesTable;
$this->authentication = $authentication;
}

public function list() {

$page = $_GET['page'] ?? 1;

$offset = ($page-1)*10;

if (isset($_GET['category'])) {
$category = $this->categoriesTable->findById($_GET['category']);
$jokes = $category->getJokes(10, $offset);
$totalJokes = $category->getNumJokes();
}
else {
$jokes = $this->jokesTable->findAll('jokedate DESC', 10, $offset);
$totalJokes = $this->jokesTable->total();
}

$title = 'Joke list';



$author = $this->authentication->getUser();

return ['template' => 'jokes.html.php',
'title' => $title,
'variables' => [
'totalJokes' => $totalJokes,
'jokes' => $jokes,
'user' => $author,
'categories' => $this->categoriesTable->findAll(),
'currentPage' => $page,
'categoryId' => $_GET['category'] ?? null
]
];
}

public function home() {
$title = 'Internet Joke Database';

return ['template' => 'home.html.php', 'title' => $title];
}

public function delete() {

$author = $this->authentication->getUser();

$joke = $this->jokesTable->findById($_POST['id']);

if ($joke->authorId != $author->id && !$author->hasPermission(\Ijdb\Entity\Author::DELETE_JOKES) ) {
return;
}


$this->jokesTable->delete($_POST['id']);

header('location: /joke/list');
}

public function saveEdit() {
$author = $this->authentication->getUser();

$joke = $_POST['joke'];
$joke['jokedate'] = new \DateTime();

$jokeEntity = $author->addJoke($joke);

$jokeEntity->clearCategories();

foreach ($_POST['category'] as $categoryId) {
$jokeEntity->addCategory($categoryId);
}

header('location: /joke/list');
}

public function edit() {
$author = $this->authentication->getUser();
$categories = $this->categoriesTable->findAll();

if (isset($_GET['id'])) {
$joke = $this->jokesTable->findById($_GET['id']);
}

$title = 'Edit joke';

return ['template' => 'editjoke.html.php',
'title' => $title,
'variables' => [
'joke' => $joke ?? null,
'user' => $author,
'categories' => $categories
]
];
}

}
46 changes: 46 additions & 0 deletions classes/Ijdb/Controllers/Login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace Ijdb\Controllers;

class Login {
private $authentication;

public function __construct(\Ninja\Authentication $authentication) {
$this->authentication = $authentication;
}

public function loginForm() {
return ['template' => 'login.html.php', 'title' => 'Log In'];
}

public function processLogin() {
if ($this->authentication->login($_POST['email'], $_POST['password'])) {
header('location: /login/success');
}
else {
return ['template' => 'login.html.php',
'title' => 'Log In',
'variables' => [
'error' => 'Invalid username/password.'
]
];
}
}

public function success() {
return ['template' => 'loginsuccess.html.php', 'title' => 'Login Successful'];
}

public function error() {
return ['template' => 'loginerror.html.php', 'title' => 'You are not logged in'];
}

public function permissionsError() {
return ['template' => 'permissionserror.html.php', 'title' => 'Access Denied'];
}

public function logout() {
unset($_SESSION);
session_destroy();
return ['template' => 'logout.html.php', 'title' => 'You have been logged out'];
}
}
121 changes: 121 additions & 0 deletions classes/Ijdb/Controllers/Register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
namespace Ijdb\Controllers;
use \Ninja\DatabaseTable;

class Register {
private $authorsTable;

public function __construct(DatabaseTable $authorsTable) {
$this->authorsTable = $authorsTable;
}

public function registrationForm() {
return ['template' => 'register.html.php',
'title' => 'Register an account'];
}


public function success() {
return ['template' => 'registersuccess.html.php',
'title' => 'Registration Successful'];
}

public function registerUser() {
$author = $_POST['author'];

//Assume the data is valid to begin with
$valid = true;
$errors = [];

//But if any of the fields have been left blank, set $valid to false
if (empty($author['name'])) {
$valid = false;
$errors[] = 'Name cannot be blank';
}

if (empty($author['email'])) {
$valid = false;
$errors[] = 'Email cannot be blank';
}
else if (filter_var($author['email'], FILTER_VALIDATE_EMAIL) == false) {
$valid = false;
$errors[] = 'Invalid email address';
}
else { //if the email is not blank and valid:
//convert the email to lowercase
$author['email'] = strtolower($author['email']);

//search for the lowercase version of `$author['email']`
if (count($this->authorsTable->find('email', $author['email'])) > 0) {
$valid = false;
$errors[] = 'That email address is already registered';
}
}


if (empty($author['password'])) {
$valid = false;
$errors[] = 'Password cannot be blank';
}

//If $valid is still true, no fields were blank and the data can be added
if ($valid == true) {
//Hash the password before saving it in the database
$author['password'] = password_hash($author['password'], PASSWORD_DEFAULT);

//When submitted, the $author variable now contains a lowercase value for email
//and a hashed password
$this->authorsTable->save($author);

header('Location: /author/success');
}
else {
//If the data is not valid, show the form again
return ['template' => 'register.html.php',
'title' => 'Register an account',
'variables' => [
'errors' => $errors,
'author' => $author
]
];
}
}

public function list() {
$authors = $this->authorsTable->findAll();

return ['template' => 'authorlist.html.php',
'title' => 'Author List',
'variables' => [
'authors' => $authors
]
];
}

public function permissions() {

$author = $this->authorsTable->findById($_GET['id']);

$reflected = new \ReflectionClass('\Ijdb\Entity\Author');
$constants = $reflected->getConstants();

return ['template' => 'permissions.html.php',
'title' => 'Edit Permissions',
'variables' => [
'author' => $author,
'permissions' => $constants
]
];
}

public function savePermissions() {
$author = [
'id' => $_GET['id'],
'permissions' => array_sum($_POST['permissions'] ?? [])
];

$this->authorsTable->save($author);

header('location: /author/list');
}
}
36 changes: 36 additions & 0 deletions classes/Ijdb/Entity/Author.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace Ijdb\Entity;

class Author {

const EDIT_JOKES = 1;
const DELETE_JOKES = 2;
const ADD_CATEGORIES = 4;
const EDIT_CATEGORIES = 8;
const REMOVE_CATEGORIES = 16;
const EDIT_USER_ACCESS = 32;

public $id;
public $name;
public $email;
public $password;
private $jokesTable;

public function __construct(\Ninja\DatabaseTable $jokeTable) {
$this->jokesTable = $jokeTable;
}

public function getJokes() {
return $this->jokesTable->find('authorId', $this->id);
}

public function addJoke($joke) {
$joke['authorId'] = $this->id;

return $this->jokesTable->save($joke);
}

public function hasPermission($permission) {
return $this->permissions & $permission;
}
}
Loading