From 3e0ce2c4642f6b1f2a31a36e58f363656bdeed96 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 12 Jan 2017 18:18:19 +0000 Subject: [PATCH 001/124] add files --- public/index.php | 10 ++++++++++ templates/output.html.php | 12 ++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 public/index.php create mode 100644 templates/output.html.php diff --git a/public/index.php b/public/index.php new file mode 100644 index 0000000..122b779 --- /dev/null +++ b/public/index.php @@ -0,0 +1,10 @@ + + + + + Script Output + + +

+ +

+ + \ No newline at end of file From eb7c54047632dc4ed23b2b8c5694e5405e0373f8 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 12 Jan 2017 18:19:06 +0000 Subject: [PATCH 002/124] set charset/error reporting --- public/index.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/public/index.php b/public/index.php index 122b779..c1fbe88 100644 --- a/public/index.php +++ b/public/index.php @@ -1,6 +1,7 @@ setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $output = 'Database connection established.'; } catch (PDOException $e) { From 3a4d6979e63e214523763d2ade2ecfcdd1c5b8df Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 12 Jan 2017 19:11:43 +0000 Subject: [PATCH 003/124] added create query --- public/index.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/public/index.php b/public/index.php index c1fbe88..8ec279d 100644 --- a/public/index.php +++ b/public/index.php @@ -1,11 +1,22 @@ setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $output = 'Database connection established.'; + + $sql = 'CREATE TABLE joke ( + id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, + joketext TEXT, + jokedate DATE NOT NULL + ) DEFAULT CHARACTER SET utf8 ENGINE=InnoDB'; + + $pdo->exec($sql); + + $output = 'Joke table successfully created.'; } catch (PDOException $e) { - $output = 'Unable to connect to the database server: ' . $e; + $output = 'Database error: ' . $e->getMessage(); } + include '../templates/output.html.php'; \ No newline at end of file From c696502bf26db24733436a96686e255497a0e5c0 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 12 Jan 2017 19:12:52 +0000 Subject: [PATCH 004/124] changed query to UPDATE --- public/index.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/public/index.php b/public/index.php index 8ec279d..880c0fb 100644 --- a/public/index.php +++ b/public/index.php @@ -4,15 +4,13 @@ $pdo = new PDO('mysql:host=localhost;dbname=ijdb_sample;charset=utf8', 'ijdb_sample', 'mypassword'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $sql = 'CREATE TABLE joke ( - id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, - joketext TEXT, - jokedate DATE NOT NULL - ) DEFAULT CHARACTER SET utf8 ENGINE=InnoDB'; - $pdo->exec($sql); + $sql = 'UPDATE joke SET jokedate="2012-04-01" + WHERE joketext LIKE "%programmer%"'; - $output = 'Joke table successfully created.'; + $affectedRows = $pdo->exec($sql); + + $output = 'Updated ' . $affectedRows .' rows.'; } catch (PDOException $e) { $output = 'Database error: ' . $e->getMessage(); From 966e1c5ca7e394a0458daeee0de453f194e7c88d Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 12 Jan 2017 19:17:45 +0000 Subject: [PATCH 005/124] removed

--- templates/output.html.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/templates/output.html.php b/templates/output.html.php index e8f266f..9f0cd8c 100644 --- a/templates/output.html.php +++ b/templates/output.html.php @@ -5,8 +5,6 @@ Script Output -

-

\ No newline at end of file From 4d50c10ab12bd9713fdacc64e590974cb10f9304 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 14 Jan 2017 16:37:02 +0000 Subject: [PATCH 006/124] added database.sql --- database.sql | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 database.sql diff --git a/database.sql b/database.sql new file mode 100644 index 0000000..b17df07 --- /dev/null +++ b/database.sql @@ -0,0 +1,52 @@ +-- MySQL dump 10.16 Distrib 10.1.20-MariaDB, for Linux (x86_64) +-- +-- Host: 127.0.0.1 Database: 127.0.0.1 +-- ------------------------------------------------------ +-- Server version 5.7.16-0ubuntu0.16.04.1 + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8 */; +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +-- +-- Table structure for table `joke` +-- + +DROP TABLE IF EXISTS `joke`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `joke` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `joketext` text, + `jokedate` date DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `joke` +-- + +LOCK TABLES `joke` WRITE; +/*!40000 ALTER TABLE `joke` DISABLE KEYS */; +INSERT INTO `joke` VALUES (1,'A programmer was found dead in the shower. The instructions read: lather, rinse, repeat.','2012-01-05'),(2,'!false - it\'s funny because it\'s true','2017-01-08'); +/*!40000 ALTER TABLE `joke` ENABLE KEYS */; +UNLOCK TABLES; +/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + +-- Dump completed on 2017-01-14 16:36:07 From 30194923391a2e219c3898cbb2943b9636426c68 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 14 Jan 2017 16:42:30 +0000 Subject: [PATCH 007/124] add files --- public/index.php | 15 ++++++++------- templates/jokes.html.php | 22 ++++++++++++++++++++++ templates/output.html.php | 10 ---------- 3 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 templates/jokes.html.php delete mode 100644 templates/output.html.php diff --git a/public/index.php b/public/index.php index 880c0fb..e19f5a4 100644 --- a/public/index.php +++ b/public/index.php @@ -5,16 +5,17 @@ $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $sql = 'UPDATE joke SET jokedate="2012-04-01" - WHERE joketext LIKE "%programmer%"'; + $sql = 'SELECT joketext FROM joke'; + $result = $pdo->query($sql); - $affectedRows = $pdo->exec($sql); + while ($row = $result->fetch()) { + $jokes[] = $row['joketext']; + } - $output = 'Updated ' . $affectedRows .' rows.'; } catch (PDOException $e) { - $output = 'Database error: ' . $e->getMessage(); + $output = 'Unable to connect to the database server: ' . $e->getMessage() . ' in ' . + $e->getFile() . ':' . $e->getLine(); } - -include '../templates/output.html.php'; \ No newline at end of file +include '../templates/jokes.html.php'; \ No newline at end of file diff --git a/templates/jokes.html.php b/templates/jokes.html.php new file mode 100644 index 0000000..8d9174f --- /dev/null +++ b/templates/jokes.html.php @@ -0,0 +1,22 @@ + + + + + List of jokes + + + +

+ +

+ + +
+

+ +

+
+ + + + \ No newline at end of file diff --git a/templates/output.html.php b/templates/output.html.php deleted file mode 100644 index 9f0cd8c..0000000 --- a/templates/output.html.php +++ /dev/null @@ -1,10 +0,0 @@ - - - - - Script Output - - - - - \ No newline at end of file From a6484c643cb2f07bfb7e83c0cc220af820a4a56b Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 14 Jan 2017 16:43:12 +0000 Subject: [PATCH 008/124] switched to shorthand echo --- templates/jokes.html.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/jokes.html.php b/templates/jokes.html.php index 8d9174f..8761ac0 100644 --- a/templates/jokes.html.php +++ b/templates/jokes.html.php @@ -7,13 +7,13 @@

- +

- +

From be4eefe3ff111db664176fc4126d1e510fd7b9fc Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 14 Jan 2017 17:07:49 +0000 Subject: [PATCH 009/124] uses layout file --- public/{index.php => jokes.php} | 18 ++++++++++++++++-- templates/jokes.html.php | 22 ---------------------- templates/layout.html.php | 23 +++++++++++++++++++++++ 3 files changed, 39 insertions(+), 24 deletions(-) rename public/{index.php => jokes.php} (53%) delete mode 100644 templates/jokes.html.php create mode 100644 templates/layout.html.php diff --git a/public/index.php b/public/jokes.php similarity index 53% rename from public/index.php rename to public/jokes.php index e19f5a4..dec5b69 100644 --- a/public/index.php +++ b/public/jokes.php @@ -12,10 +12,24 @@ $jokes[] = $row['joketext']; } + $title = 'Joke list'; + + $output = ''; + + foreach ($jokes as $joke) { + $output .= '
'; + $output .= '

'; + $output .= $joke; + $output .= '

'; + $output .= '
'; + } + } catch (PDOException $e) { - $output = 'Unable to connect to the database server: ' . $e->getMessage() . ' in ' . + $title = 'An error has occurred'; + + $output = 'Database error: ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine(); } -include '../templates/jokes.html.php'; \ No newline at end of file +include '../templates/layout.html.php'; \ No newline at end of file diff --git a/templates/jokes.html.php b/templates/jokes.html.php deleted file mode 100644 index 8761ac0..0000000 --- a/templates/jokes.html.php +++ /dev/null @@ -1,22 +0,0 @@ - - - - - List of jokes - - - -

- -

- - -
-

- -

-
- - - - \ No newline at end of file diff --git a/templates/layout.html.php b/templates/layout.html.php new file mode 100644 index 0000000..654abf5 --- /dev/null +++ b/templates/layout.html.php @@ -0,0 +1,23 @@ + + + + + <?=$title?> + + + + +
+ +
+ +
+ © IJDB 2017 +
+ + \ No newline at end of file From ea9041d16cbb6c749c86a99296c43873e8dd73de Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 14 Jan 2017 17:14:55 +0000 Subject: [PATCH 010/124] uses output buffering to enable a jokes template --- public/jokes.php | 13 ++++--------- templates/jokes.html.php | 7 +++++++ 2 files changed, 11 insertions(+), 9 deletions(-) create mode 100644 templates/jokes.html.php diff --git a/public/jokes.php b/public/jokes.php index dec5b69..9e693e9 100644 --- a/public/jokes.php +++ b/public/jokes.php @@ -4,7 +4,6 @@ $pdo = new PDO('mysql:host=localhost;dbname=ijdb_sample;charset=utf8', 'ijdb_sample', 'mypassword'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $sql = 'SELECT joketext FROM joke'; $result = $pdo->query($sql); @@ -14,15 +13,11 @@ $title = 'Joke list'; - $output = ''; + ob_start(); - foreach ($jokes as $joke) { - $output .= '
'; - $output .= '

'; - $output .= $joke; - $output .= '

'; - $output .= '
'; - } + include '../templates/jokes.html.php'; + + $output = ob_get_clean(); } catch (PDOException $e) { diff --git a/templates/jokes.html.php b/templates/jokes.html.php new file mode 100644 index 0000000..3a1dfa9 --- /dev/null +++ b/templates/jokes.html.php @@ -0,0 +1,7 @@ + +
+

+ +

+
+ \ No newline at end of file From 839ed0f274cd5a5fe262b9e501b75141eb677589 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 14 Jan 2017 17:23:18 +0000 Subject: [PATCH 011/124] added addjoke template/link --- public/index.php | 11 +++++++++++ templates/addjoke.html.php | 6 ++++++ templates/home.html.php | 3 +++ templates/layout.html.php | 2 ++ 4 files changed, 22 insertions(+) create mode 100644 public/index.php create mode 100644 templates/addjoke.html.php create mode 100644 templates/home.html.php diff --git a/public/index.php b/public/index.php new file mode 100644 index 0000000..74e8445 --- /dev/null +++ b/public/index.php @@ -0,0 +1,11 @@ + + + + + \ No newline at end of file diff --git a/templates/home.html.php b/templates/home.html.php new file mode 100644 index 0000000..9e17461 --- /dev/null +++ b/templates/home.html.php @@ -0,0 +1,3 @@ +

Internet Joke Database

+ +

Welcome to the Internet Joke Database

\ No newline at end of file diff --git a/templates/layout.html.php b/templates/layout.html.php index 654abf5..815d7c2 100644 --- a/templates/layout.html.php +++ b/templates/layout.html.php @@ -3,12 +3,14 @@ <?=$title?> + From c7534f128a6c9d3ffc4f240b115f5dd43a1deb3b Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 14 Jan 2017 17:28:17 +0000 Subject: [PATCH 012/124] add addjoke.php and change form styling --- public/addjoke.php | 25 +++++++++++++++++++++++++ public/form.css | 5 +++++ 2 files changed, 30 insertions(+) create mode 100644 public/addjoke.php create mode 100644 public/form.css diff --git a/public/addjoke.php b/public/addjoke.php new file mode 100644 index 0000000..6726062 --- /dev/null +++ b/public/addjoke.php @@ -0,0 +1,25 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + } + catch (PDOException $e) { + $title = 'An error has occurred'; + + $output = 'DAtabase error: ' . $e->getMessage() . ' in ' . + $e->getFile() . ':' . $e->getLine(); + } + +} +else { + $title = 'Add a new joke'; + + ob_start(); + + include '../templates/addjoke.html.php'; + + $output = ob_get_clean(); +} +include '../templates/layout.html.php'; \ No newline at end of file diff --git a/public/form.css b/public/form.css new file mode 100644 index 0000000..7c39712 --- /dev/null +++ b/public/form.css @@ -0,0 +1,5 @@ +body {font-size: 1.2em; font-family: arial, helvetica, sans-serif} +input, label, select, textarea {float: left; width: 15em; margin-bottom: 1em; padding: 0.5em;} +label {clear: left; padding: 0; margin: 0;} +input[type="submit"] {margin-left: 15em; width: auto; padding: 0.5em 1em; clear: both; font-size: 1em;} +form {overflow: auto; clear: both; display: block;} \ No newline at end of file From 4441410128cdb2499d3bc084c05dcd123fc659e0 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 14 Jan 2017 17:33:31 +0000 Subject: [PATCH 013/124] complete addjoke.php --- public/addjoke.php | 14 +++++++++++++- templates/addjoke.html.php | 3 +-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/public/addjoke.php b/public/addjoke.php index 6726062..2fe3fc4 100644 --- a/public/addjoke.php +++ b/public/addjoke.php @@ -1,9 +1,21 @@ setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $sql = 'INSERT INTO joke SET + joketext = :joketext, + jokedate = CURDATE()'; + + $stmt = $pdo->prepare($sql); + + $stmt->bindValue(':joketext', $_POST['joketext']); + + $stmt->execute(); + + header('location: jokes.php'); + } catch (PDOException $e) { $title = 'An error has occurred'; diff --git a/templates/addjoke.html.php b/templates/addjoke.html.php index 10cba6e..b1d4935 100644 --- a/templates/addjoke.html.php +++ b/templates/addjoke.html.php @@ -1,6 +1,5 @@
- +
\ No newline at end of file From f31bddaf3cca814b1daf1512ed43613e3612fbeb Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sun, 15 Jan 2017 12:54:07 +0000 Subject: [PATCH 014/124] add delete joke files --- public/deletejoke.php | 24 ++++++++++++++++++++++++ public/form.css | 7 ++++++- public/jokes.php | 8 +++----- templates/jokes.html.php | 6 +++++- 4 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 public/deletejoke.php diff --git a/public/deletejoke.php b/public/deletejoke.php new file mode 100644 index 0000000..8efa63a --- /dev/null +++ b/public/deletejoke.php @@ -0,0 +1,24 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + + $sql = 'DELETE FROM joke WHERE id = :id'; + + $stmt = $pdo->prepare($sql); + + $stmt->bindValue(':id', $_POST['id']); + $stmt->execute(); + + header('location: jokes.php'); +} +catch (PDOException $e) { + $title = 'An error has occurred'; + + $output = 'Unable to connect to the database server: ' . $e->getMessage() . ' in ' . + $e->getFile() . ':' . $e->getLine(); +} + +include '../templates/layout.html.php'; \ No newline at end of file diff --git a/public/form.css b/public/form.css index 7c39712..aedf78b 100644 --- a/public/form.css +++ b/public/form.css @@ -2,4 +2,9 @@ body {font-size: 1.2em; font-family: arial, helvetica, sans-serif} input, label, select, textarea {float: left; width: 15em; margin-bottom: 1em; padding: 0.5em;} label {clear: left; padding: 0; margin: 0;} input[type="submit"] {margin-left: 15em; width: auto; padding: 0.5em 1em; clear: both; font-size: 1em;} -form {overflow: auto; clear: both; display: block;} \ No newline at end of file +form {overflow: auto; clear: both; display: block;} + +blockquote {overflow: auto; border-bottom: 1px solid #ddd} +blockquote p {float: left;} +blockquote form { float: right; display: block; clear: none;} + diff --git a/public/jokes.php b/public/jokes.php index 9e693e9..d1d8bf9 100644 --- a/public/jokes.php +++ b/public/jokes.php @@ -4,12 +4,10 @@ $pdo = new PDO('mysql:host=localhost;dbname=ijdb_sample;charset=utf8', 'ijdb_sample', 'mypassword'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $sql = 'SELECT joketext FROM joke'; - $result = $pdo->query($sql); + $sql = 'SELECT id, joketext FROM joke'; + + $jokes = $pdo->query($sql); - while ($row = $result->fetch()) { - $jokes[] = $row['joketext']; - } $title = 'Joke list'; diff --git a/templates/jokes.html.php b/templates/jokes.html.php index 3a1dfa9..0c938d2 100644 --- a/templates/jokes.html.php +++ b/templates/jokes.html.php @@ -1,7 +1,11 @@

- + +

+ + +

\ No newline at end of file From 283445d08f1cc01104d26ac2a4bc4788bc3ba06c Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sun, 15 Jan 2017 14:16:22 +0000 Subject: [PATCH 015/124] use __DIR__ --- public/addjoke.php | 4 ++-- public/deletejoke.php | 2 +- public/index.php | 4 ++-- public/jokes.php | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/public/addjoke.php b/public/addjoke.php index 2fe3fc4..73cd9aa 100644 --- a/public/addjoke.php +++ b/public/addjoke.php @@ -30,8 +30,8 @@ ob_start(); - include '../templates/addjoke.html.php'; + include __DIR__ . '/../templates/addjoke.html.php'; $output = ob_get_clean(); } -include '../templates/layout.html.php'; \ No newline at end of file +include __DIR__ . '/../templates/layout.html.php'; \ No newline at end of file diff --git a/public/deletejoke.php b/public/deletejoke.php index 8efa63a..bc4e477 100644 --- a/public/deletejoke.php +++ b/public/deletejoke.php @@ -21,4 +21,4 @@ $e->getFile() . ':' . $e->getLine(); } -include '../templates/layout.html.php'; \ No newline at end of file +include __DIR__ . '/../templates/layout.html.php'; \ No newline at end of file diff --git a/public/index.php b/public/index.php index 74e8445..d775371 100644 --- a/public/index.php +++ b/public/index.php @@ -4,8 +4,8 @@ ob_start(); -include '../templates/home.html.php'; +include __DIR__ . '/../templates/home.html.php'; $output = ob_get_clean(); -include '../templates/layout.html.php'; \ No newline at end of file +include __DIR__ . '/../templates/layout.html.php'; \ No newline at end of file diff --git a/public/jokes.php b/public/jokes.php index d1d8bf9..847f1b1 100644 --- a/public/jokes.php +++ b/public/jokes.php @@ -13,7 +13,7 @@ ob_start(); - include '../templates/jokes.html.php'; + include __DIR__ . '/../templates/jokes.html.php'; $output = ob_get_clean(); @@ -25,4 +25,4 @@ $e->getFile() . ':' . $e->getLine(); } -include '../templates/layout.html.php'; \ No newline at end of file +include __DIR__ . '/../templates/layout.html.php'; \ No newline at end of file From a8698860b888e6ee7f97e68b1b4f809a67c26e35 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Mon, 13 Feb 2017 18:24:29 +0000 Subject: [PATCH 016/124] author attribution --- database.sql | 36 +++++++++++++++++++++++++++++++----- public/jokes.css | 27 +++++++++++++++++++++++++++ public/jokes.php | 4 +++- templates/jokes.html.php | 7 +++++++ templates/layout.html.php | 5 ++++- 5 files changed, 72 insertions(+), 7 deletions(-) create mode 100644 public/jokes.css diff --git a/database.sql b/database.sql index b17df07..7403953 100644 --- a/database.sql +++ b/database.sql @@ -1,4 +1,4 @@ --- MySQL dump 10.16 Distrib 10.1.20-MariaDB, for Linux (x86_64) +-- MySQL dump 10.16 Distrib 10.1.21-MariaDB, for Linux (x86_64) -- -- Host: 127.0.0.1 Database: 127.0.0.1 -- ------------------------------------------------------ @@ -15,6 +15,31 @@ /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; +-- +-- Table structure for table `author` +-- + +DROP TABLE IF EXISTS `author`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `author` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(255) DEFAULT NULL, + `email` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `author` +-- + +LOCK TABLES `author` WRITE; +/*!40000 ALTER TABLE `author` DISABLE KEYS */; +INSERT INTO `author` VALUES (1,'Kevin Yank','thatguy@kevinyank.com'),(2,'Tom Butler','tom@r.je'); +/*!40000 ALTER TABLE `author` ENABLE KEYS */; +UNLOCK TABLES; + -- -- Table structure for table `joke` -- @@ -25,9 +50,10 @@ DROP TABLE IF EXISTS `joke`; CREATE TABLE `joke` ( `id` int(11) NOT NULL AUTO_INCREMENT, `joketext` text, - `jokedate` date DEFAULT NULL, + `jokedate` date NOT NULL, + `authorid` int(11) DEFAULT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1; +) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -36,7 +62,7 @@ CREATE TABLE `joke` ( LOCK TABLES `joke` WRITE; /*!40000 ALTER TABLE `joke` DISABLE KEYS */; -INSERT INTO `joke` VALUES (1,'A programmer was found dead in the shower. The instructions read: lather, rinse, repeat.','2012-01-05'),(2,'!false - it\'s funny because it\'s true','2017-01-08'); +INSERT INTO `joke` VALUES (1,'How many programmers does it take to screw in a lightbulb? None, it\'s a hardware problem.','2017-04-01',1),(2,'Why did the programmer quit his job? He didn\'t get arrays','2017-04-01',1),(3,'Why was the empty array stuck outside? It didn\'t have any keys','2017-04-01',2); /*!40000 ALTER TABLE `joke` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; @@ -49,4 +75,4 @@ UNLOCK TABLES; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2017-01-14 16:36:07 +-- Dump completed on 2017-02-13 18:17:21 diff --git a/public/jokes.css b/public/jokes.css new file mode 100644 index 0000000..b613ccb --- /dev/null +++ b/public/jokes.css @@ -0,0 +1,27 @@ + * {margin: 0; padding: 0; } +body {font-family: Arial, Helvetica, Sans-serif; } +header {display: block; background-color: #3cbc8d; padding: 20px; border-bottom: 5px solid #757575;} +h1 {font-weight: normal; padding: 1.5em; color: white; text-shadow: 1px 1px 0px #444; width: 1000px; margin: auto;} + +nav {background-color: #443A5C;} + +nav a {color: white; text-decoration: none;} +nav a:hover {color: #ddd;} +nav ul {display: table; width: 1000px; margin: auto;} +nav li {display: table-cell; text-align: center; padding: 1em;} + +main {overflow: auto; min-height: 30vh; background-color: #f7f7f7; width: 1000px; margin: auto; box-shadow: -1000px 0 0 #f7f7f7, 1000px 0 0 #f7f7f7; padding-top: 20px;} + +footer {padding: 1em; background-color: #FFC9A7 ; font-size: 0.8em;} + +p {margin-bottom: 1em;} + +body {font-size: 1.2em; font-family: arial, helvetica, sans-serif} +input, label, select, textarea {float: left; width: 15em; margin-bottom: 1em; padding: 0.5em;} +label {clear: left; padding: 0; margin: 0;} +input[type="submit"] {margin-left: 15em; width: auto; padding: 0.5em 1em; clear: both; font-size: 1em;} +form {overflow: auto; clear: both; display: block;} + +blockquote {display: table; margin-bottom: 1em; border-bottom: 1px solid #ccc; padding: 0.5em; } +blockquote p {display: table-cell; width: 90%; vertical-align: top;} +blockquote form { display: table-cell; width: 10%;} \ No newline at end of file diff --git a/public/jokes.php b/public/jokes.php index 847f1b1..f5fe868 100644 --- a/public/jokes.php +++ b/public/jokes.php @@ -4,7 +4,9 @@ $pdo = new PDO('mysql:host=localhost;dbname=ijdb_sample;charset=utf8', 'ijdb_sample', 'mypassword'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $sql = 'SELECT id, joketext FROM joke'; + $sql = 'SELECT joke.id, joketext, name, email + FROM joke INNER JOIN author + ON authorid = author.id'; $jokes = $pdo->query($sql); diff --git a/templates/jokes.html.php b/templates/jokes.html.php index 0c938d2..e1abcfc 100644 --- a/templates/jokes.html.php +++ b/templates/jokes.html.php @@ -2,6 +2,13 @@

+ + (by ) +

diff --git a/templates/layout.html.php b/templates/layout.html.php index 815d7c2..8118094 100644 --- a/templates/layout.html.php +++ b/templates/layout.html.php @@ -2,11 +2,14 @@ + <?=$title?> -
From 0069f35e245947d63f005fc4e25a8c65acf657fc Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sun, 12 Mar 2017 14:31:56 +0000 Subject: [PATCH 040/124] added name to submit button --- templates/editjoke.html.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/editjoke.html.php b/templates/editjoke.html.php index c2d0884..5a0fd91 100644 --- a/templates/editjoke.html.php +++ b/templates/editjoke.html.php @@ -2,5 +2,5 @@ - + \ No newline at end of file From f6157ac93de521ca575198fd0a00b083c21158e6 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sun, 12 Mar 2017 14:38:17 +0000 Subject: [PATCH 041/124] editjoke now uses an array --- public/editjoke.php | 11 ++++++----- templates/editjoke.html.php | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/public/editjoke.php b/public/editjoke.php index 9084505..7d1a8b1 100644 --- a/public/editjoke.php +++ b/public/editjoke.php @@ -3,12 +3,13 @@ include __DIR__ . '/../includes/DatabaseFunctions.php'; try { - if (isset($_POST['joketext'])) { + if (isset($_POST['joke'])) { - save($pdo, 'joke', 'id', ['id' => $_POST['jokeid'], - 'joketext' => $_POST['joketext'], - 'jokedate' => new DateTime(), - 'authorId' => 1]); + $joke = $_POST['joke']; + $joke['jokedate'] = new DateTime(); + $joke['authorId'] = 1; + + save($pdo, 'joke', 'id', $joke); header('location: jokes.php'); diff --git a/templates/editjoke.html.php b/templates/editjoke.html.php index 5a0fd91..6731068 100644 --- a/templates/editjoke.html.php +++ b/templates/editjoke.html.php @@ -1,6 +1,6 @@
- + - +
\ No newline at end of file From 8e5133ac63afa61ce267f441a2055136523d0613 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sun, 12 Mar 2017 15:01:37 +0000 Subject: [PATCH 042/124] replaced implode with rtrim --- includes/DatabaseFunctions.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/includes/DatabaseFunctions.php b/includes/DatabaseFunctions.php index f7ab33f..3d56d59 100644 --- a/includes/DatabaseFunctions.php +++ b/includes/DatabaseFunctions.php @@ -60,13 +60,10 @@ function update($pdo, $table, $primaryKey, $fields) { $fieldArray = []; foreach ($fields as $key => $value) { - //Add, for example, `id = :id` to the end of the array - $fieldArray[] = '`' . $key . '` = :' . $key; + $query .= '`' . $key . '` = :' . $key . ','; } - - $query .= implode(', ', $fieldArray); - + $query = rtrim($query, ','); $query .= ' WHERE `' . $primaryKey . '` = :primaryKey'; From 12f58db2286e97fe3cc4ae31486697ae45caf8c8 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sun, 12 Mar 2017 15:07:05 +0000 Subject: [PATCH 043/124] replaced implode with rtrim --- includes/DatabaseFunctions.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/includes/DatabaseFunctions.php b/includes/DatabaseFunctions.php index 3d56d59..bd0d36a 100644 --- a/includes/DatabaseFunctions.php +++ b/includes/DatabaseFunctions.php @@ -56,9 +56,6 @@ function update($pdo, $table, $primaryKey, $fields) { $query = ' UPDATE `' . $table .'` SET '; - //Start off with an empty array - $fieldArray = []; - foreach ($fields as $key => $value) { $query .= '`' . $key . '` = :' . $key . ','; } From 8ef7c5e1fa22b538b1db08f95b2d763d51996b31 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sun, 12 Mar 2017 15:25:55 +0000 Subject: [PATCH 044/124] replaced implode with rtrim --- includes/DatabaseFunctions.php | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/includes/DatabaseFunctions.php b/includes/DatabaseFunctions.php index bd0d36a..13a7e06 100644 --- a/includes/DatabaseFunctions.php +++ b/includes/DatabaseFunctions.php @@ -30,23 +30,27 @@ function findById($pdo, $table, $primaryKey, $value) { function insert($pdo, $table, $fields) { - - $keys = []; + $query = 'INSERT INTO `' . $table . '` ('; foreach ($fields as $key => $value) { - $keys[] = '`' . $key . '`'; + $query .= '`' . $key . '`,'; } - $query = 'INSERT INTO `' . $table .'` (' . implode(', ', $keys) . ') '; - $query .= 'VALUES ('; + $query = rtrim($query, ','); + $query .= ') VALUES ('; - $fieldKeys = array_keys($fields); - $query .= ':' . implode(', :', $fieldKeys) . ')'; + foreach ($fields as $key => $value) { + $query .= ':' . $key . ','; + } + + $query = rtrim($query, ','); + + $query .= ')'; $fields = processDates($fields); - + query($pdo, $query, $fields); } @@ -74,6 +78,7 @@ function update($pdo, $table, $primaryKey, $fields) { + function delete($pdo, $table, $primaryKey, $id ) { $parameters = [':id' => $id]; From 847690b452787db6282a3870d4caf88403e90ee0 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Wed, 7 Jun 2017 16:58:26 +0100 Subject: [PATCH 045/124] jokes.php now uses class instead of functions --- classes/DatabaseTable.php | 119 +++++++++++++++++++++++++++++++++ includes/DatabaseFunctions.php | 116 -------------------------------- public/jokes.php | 12 ++-- 3 files changed, 126 insertions(+), 121 deletions(-) create mode 100644 classes/DatabaseTable.php delete mode 100644 includes/DatabaseFunctions.php diff --git a/classes/DatabaseTable.php b/classes/DatabaseTable.php new file mode 100644 index 0000000..59c5429 --- /dev/null +++ b/classes/DatabaseTable.php @@ -0,0 +1,119 @@ +pdo = $pdo; + $this->table = $table; + $this->primaryKey = $primaryKey; + } + + private function query($sql, $parameters = []) { + $query = $this->pdo->prepare($sql); + $query->execute($parameters); + return $query; + } + + public function total() { + $query = $this->query('SELECT COUNT(*) FROM `' . $this->table . '`'); + $row = $query->fetch(); + return $row[0]; + } + + public function findById($value) { + $query = 'SELECT * FROM `' . $this->table . '` WHERE `' . $this->primaryKey . '` = :value'; + + $parameters = [ + 'value' => $value + ]; + + $query = $this->query($query, $parameters); + + return $query->fetch(); + } + + + private function insert($fields) { + $query = 'INSERT INTO `' . $this->table . '` ('; + + foreach ($fields as $key => $value) { + $query .= '`' . $key . '`,'; + } + + $query = rtrim($query, ','); + + $query .= ') VALUES ('; + + + foreach ($fields as $key => $value) { + $query .= ':' . $key . ','; + } + + $query = rtrim($query, ','); + + $query .= ')'; + + $fields = $this->processDates($fields); + + $this->query($query, $fields); + } + + + private function update($fields) { + $query = ' UPDATE `' . $this->table .'` SET '; + + foreach ($fields as $key => $value) { + $query .= '`' . $key . '` = :' . $key . ','; + } + + $query = rtrim($query, ','); + + $query .= ' WHERE `' . $this->primaryKey . '` = :primaryKey'; + + //Set the :primaryKey variable + $fields['primaryKey'] = $fields['id']; + + $fields = $this->processDates($fields); + + $this->query($query, $fields); + } + + + public function delete($id ) { + $parameters = [':id' => $id]; + + $this->query('DELETE FROM `' . $this->table . '` WHERE `' . $this->primaryKey . '` = :id', $parameters); + } + + + public function findAll() { + $result = $this->query('SELECT * FROM ' . $this->table); + + return $result->fetchAll(); + } + + private function processDates($fields) { + foreach ($fields as $key => $value) { + if ($value instanceof DateTime) { + $fields[$key] = $value->format('Y-m-d'); + } + } + + return $fields; + } + + + public function save($record) { + try { + if ($record[$this->primaryKey] == '') { + $record[$this->primaryKey] = null; + } + $this->insert($record); + } + catch (PDOException $e) { + $this->update( $record); + } + } +} \ No newline at end of file diff --git a/includes/DatabaseFunctions.php b/includes/DatabaseFunctions.php deleted file mode 100644 index 13a7e06..0000000 --- a/includes/DatabaseFunctions.php +++ /dev/null @@ -1,116 +0,0 @@ -prepare($sql); - $query->execute($parameters); - return $query; -} - - -function total($pdo, $table) { - $query = query($pdo, 'SELECT COUNT(*) FROM `' . $table . '`'); - $row = $query->fetch(); - return $row[0]; -} - - - - -function findById($pdo, $table, $primaryKey, $value) { - $query = 'SELECT * FROM `' . $table . '` WHERE `' . $primaryKey . '` = :value'; - - $parameters = [ - 'value' => $value - ]; - - $query = query($pdo, $query, $parameters); - - return $query->fetch(); -} - - -function insert($pdo, $table, $fields) { - $query = 'INSERT INTO `' . $table . '` ('; - - foreach ($fields as $key => $value) { - $query .= '`' . $key . '`,'; - } - - $query = rtrim($query, ','); - - $query .= ') VALUES ('; - - - foreach ($fields as $key => $value) { - $query .= ':' . $key . ','; - } - - $query = rtrim($query, ','); - - $query .= ')'; - - $fields = processDates($fields); - - query($pdo, $query, $fields); -} - - -function update($pdo, $table, $primaryKey, $fields) { - - $query = ' UPDATE `' . $table .'` SET '; - - - foreach ($fields as $key => $value) { - $query .= '`' . $key . '` = :' . $key . ','; - } - - $query = rtrim($query, ','); - - $query .= ' WHERE `' . $primaryKey . '` = :primaryKey'; - - //Set the :primaryKey variable - $fields['primaryKey'] = $fields['id']; - - $fields = processDates($fields); - - query($pdo, $query, $fields); -} - - - - -function delete($pdo, $table, $primaryKey, $id ) { - $parameters = [':id' => $id]; - - query($pdo, 'DELETE FROM `' . $table . '` WHERE `' . $primaryKey . '` = :id', $parameters); -} - - -function findAll($pdo, $table) { - $result = query($pdo, 'SELECT * FROM `' . $table . '`'); - - return $result->fetchAll(); -} - -function processDates($fields) { - foreach ($fields as $key => $value) { - if ($value instanceof DateTime) { - $fields[$key] = $value->format('Y-m-d'); - } - } - - return $fields; -} - - -function save($pdo, $table, $primaryKey, $record) { - try { - if ($record[$primaryKey] == '') { - $record[$primaryKey] = null; - } - insert($pdo, $table, $record); - } - catch (PDOException $e) { - update($pdo, $table, $primaryKey, $record); - } -} \ No newline at end of file diff --git a/public/jokes.php b/public/jokes.php index d4cf141..5f050d8 100644 --- a/public/jokes.php +++ b/public/jokes.php @@ -2,14 +2,16 @@ try { include __DIR__ . '/../includes/DatabaseConnection.php'; - include __DIR__ . '/../includes/DatabaseFunctions.php'; + include __DIR__ . '/../classes/DatabaseTable.php'; - - $result = findAll($pdo, 'joke'); + $jokesTable = new DatabaseTable($pdo, 'joke', 'id'); + $authorsTable = new DatabaseTable($pdo, 'author', 'id'); + + $result = $jokesTable->findAll(); $jokes = []; foreach ($result as $joke) { - $author = findById($pdo, 'author', 'id', $joke['authorId']); + $author = $authorsTable->findById($joke['authorId']); $jokes[] = [ 'id' => $joke['id'], @@ -24,7 +26,7 @@ $title = 'Joke list'; - $totalJokes = total($pdo, 'joke'); + $totalJokes = $jokesTable->total(); ob_start(); From 8f6eb28c35ab84d86c14a39b70f5a9b8d575c830 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Wed, 7 Jun 2017 17:07:36 +0100 Subject: [PATCH 046/124] updated other controllers --- public/deletejoke.php | 6 ++++-- public/editjoke.php | 12 +++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/public/deletejoke.php b/public/deletejoke.php index 1590083..f07dda8 100644 --- a/public/deletejoke.php +++ b/public/deletejoke.php @@ -1,9 +1,11 @@ delete($_POST['id']); header('location: jokes.php'); } diff --git a/public/editjoke.php b/public/editjoke.php index 7d1a8b1..9a4bfaa 100644 --- a/public/editjoke.php +++ b/public/editjoke.php @@ -1,15 +1,17 @@ save($joke); header('location: jokes.php'); @@ -17,7 +19,7 @@ else { if (isset($_GET['id'])) { - $joke = findById($pdo, 'joke', 'id', $_GET['id']); + $joke = $jokesTable->findById($_GET['id']); } $title = 'Edit joke'; From 05de74212a304619a5905deb53c8f46da8155739 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Fri, 9 Jun 2017 16:12:03 +0100 Subject: [PATCH 047/124] single entry point --- controllers/JokeController.php | 91 ++++++++++++++++++++++++++++++++++ public/deletejoke.php | 19 ------- public/editjoke.php | 41 --------------- public/index.php | 37 ++++++++++++-- public/jokes.php | 45 ----------------- 5 files changed, 124 insertions(+), 109 deletions(-) create mode 100644 controllers/JokeController.php delete mode 100644 public/deletejoke.php delete mode 100644 public/editjoke.php delete mode 100644 public/jokes.php diff --git a/controllers/JokeController.php b/controllers/JokeController.php new file mode 100644 index 0000000..cb534d1 --- /dev/null +++ b/controllers/JokeController.php @@ -0,0 +1,91 @@ +jokesTable = $jokesTable; + $this->authorsTable = $authorsTable; + } + + public function list() { + $result = $this->jokesTable->findAll(); + + $jokes = []; + foreach ($result as $joke) { + $author = $this->authorsTable->findById($joke['authorId']); + + $jokes[] = [ + 'id' => $joke['id'], + 'joketext' => $joke['joketext'], + 'jokedate' => $joke['jokedate'], + 'name' => $author['name'], + 'email' => $author['email'] + ]; + + } + + + $title = 'Joke list'; + + $totalJokes = $this->jokesTable->total(); + + ob_start(); + + include __DIR__ . '/../templates/jokes.html.php'; + + $output = ob_get_clean(); + + return ['output' => $output, 'title' => $title]; + } + + public function home() { + $title = 'Internet Joke Database'; + + ob_start(); + + include __DIR__ . '/../templates/home.html.php'; + + $output = ob_get_clean(); + + return ['output' => $output, 'title' => $title]; + } + + public function delete() { + $this->jokesTable->delete($_POST['id']); + + header('location: jokes.php'); + } + + + public function edit() { + if (isset($_POST['joke'])) { + + $joke = $_POST['joke']; + $joke['jokedate'] = new DateTime(); + $joke['authorId'] = 1; + + $jokesTable->save($joke); + + header('location: jokes.php'); + + } + else { + + if (isset($_GET['id'])) { + $joke = $jokesTable->findById($_GET['id']); + } + + $title = 'Edit joke'; + + ob_start(); + + include __DIR__ . '/../templates/editjoke.html.php'; + + $output = ob_get_clean(); + + return ['output' => $output, 'title' => $title]; + } + } +} \ No newline at end of file diff --git a/public/deletejoke.php b/public/deletejoke.php deleted file mode 100644 index f07dda8..0000000 --- a/public/deletejoke.php +++ /dev/null @@ -1,19 +0,0 @@ -delete($_POST['id']); - - header('location: jokes.php'); -} -catch (PDOException $e) { - $title = 'An error has occurred'; - - $output = 'Unable to connect to the database server: ' . $e->getMessage() . ' in ' . - $e->getFile() . ':' . $e->getLine(); -} - -include __DIR__ . '/../templates/layout.html.php'; \ No newline at end of file diff --git a/public/editjoke.php b/public/editjoke.php deleted file mode 100644 index 9a4bfaa..0000000 --- a/public/editjoke.php +++ /dev/null @@ -1,41 +0,0 @@ -save($joke); - - header('location: jokes.php'); - - } - else { - - if (isset($_GET['id'])) { - $joke = $jokesTable->findById($_GET['id']); - } - - $title = 'Edit joke'; - - ob_start(); - - include __DIR__ . '/../templates/editjoke.html.php'; - - $output = ob_get_clean(); - } -} -catch (PDOException $e) { - $title = 'An error has occurred'; - - $output = 'Database error: ' . $e->getMessage() . ' in ' . - $e->getFile() . ':' . $e->getLine(); -} - -include __DIR__ . '/../templates/layout.html.php'; \ No newline at end of file diff --git a/public/index.php b/public/index.php index d775371..9e052de 100644 --- a/public/index.php +++ b/public/index.php @@ -1,11 +1,40 @@ edit(); + } + + else if (isset($_GET['delete'])) { + $page = $jokeController->delete(); + } + + else if (isset($_GET['list'])) { + $page = $jokeController->list(); + } + + else { + $page = $jokeController->home(); + } + + $title = $page['title']; + $output = $page['output']; + +} +catch (PDOException $e) { + $title = 'An error has occurred'; + + $output = 'Database error: ' . $e->getMessage() . ' in ' . + $e->getFile() . ':' . $e->getLine(); +} include __DIR__ . '/../templates/layout.html.php'; \ No newline at end of file diff --git a/public/jokes.php b/public/jokes.php deleted file mode 100644 index 5f050d8..0000000 --- a/public/jokes.php +++ /dev/null @@ -1,45 +0,0 @@ -findAll(); - - $jokes = []; - foreach ($result as $joke) { - $author = $authorsTable->findById($joke['authorId']); - - $jokes[] = [ - 'id' => $joke['id'], - 'joketext' => $joke['joketext'], - 'jokedate' => $joke['jokedate'], - 'name' => $author['name'], - 'email' => $author['email'] - ]; - - } - - - $title = 'Joke list'; - - $totalJokes = $jokesTable->total(); - - ob_start(); - - include __DIR__ . '/../templates/jokes.html.php'; - - $output = ob_get_clean(); - -} -catch (PDOException $e) { - $title = 'An error has occurred'; - - $output = 'Database error: ' . $e->getMessage() . ' in ' . - $e->getFile() . ':' . $e->getLine(); -} - -include __DIR__ . '/../templates/layout.html.php'; \ No newline at end of file From bdc9a692fca0b8e787906af05301377005c8f577 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Fri, 9 Jun 2017 16:26:29 +0100 Subject: [PATCH 048/124] missing $this --- controllers/JokeController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/controllers/JokeController.php b/controllers/JokeController.php index cb534d1..aaabcb0 100644 --- a/controllers/JokeController.php +++ b/controllers/JokeController.php @@ -66,7 +66,7 @@ public function edit() { $joke['jokedate'] = new DateTime(); $joke['authorId'] = 1; - $jokesTable->save($joke); + $this->jokesTable->save($joke); header('location: jokes.php'); @@ -74,7 +74,7 @@ public function edit() { else { if (isset($_GET['id'])) { - $joke = $jokesTable->findById($_GET['id']); + $joke = $this->jokesTable->findById($_GET['id']); } $title = 'Edit joke'; From a33b121aa93c9887be2c7f8fe410a6b94247fee0 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 10 Jun 2017 18:44:24 +0100 Subject: [PATCH 049/124] moved JokeController into classes/controllers - lets us do namespacces more easily later --- {controllers => classes/controllers}/JokeController.php | 6 +++--- public/index.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) rename {controllers => classes/controllers}/JokeController.php (89%) diff --git a/controllers/JokeController.php b/classes/controllers/JokeController.php similarity index 89% rename from controllers/JokeController.php rename to classes/controllers/JokeController.php index aaabcb0..83d04d1 100644 --- a/controllers/JokeController.php +++ b/classes/controllers/JokeController.php @@ -33,7 +33,7 @@ public function list() { ob_start(); - include __DIR__ . '/../templates/jokes.html.php'; + include __DIR__ . '/../../templates/jokes.html.php'; $output = ob_get_clean(); @@ -45,7 +45,7 @@ public function home() { ob_start(); - include __DIR__ . '/../templates/home.html.php'; + include __DIR__ . '/../../templates/home.html.php'; $output = ob_get_clean(); @@ -81,7 +81,7 @@ public function edit() { ob_start(); - include __DIR__ . '/../templates/editjoke.html.php'; + include __DIR__ . '/../../templates/editjoke.html.php'; $output = ob_get_clean(); diff --git a/public/index.php b/public/index.php index 9e052de..f406c53 100644 --- a/public/index.php +++ b/public/index.php @@ -2,7 +2,7 @@ try { include __DIR__ . '/../includes/DatabaseConnection.php'; include __DIR__ . '/../classes/DatabaseTable.php'; - include __DIR__ . '/../controllers/JokeController.php'; + include __DIR__ . '/../classes/controllers/JokeController.php'; $jokesTable = new DatabaseTable($pdo, 'joke', 'id'); $authorsTable = new DatabaseTable($pdo, 'author', 'id'); From 40bd8088cd3b38019b7f7ae87e0d404e67c324ce Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Fri, 9 Jun 2017 16:26:08 +0100 Subject: [PATCH 050/124] simpler index.php --- classes/controllers/JokeController.php | 4 ++-- public/index.php | 16 ++-------------- templates/jokes.html.php | 4 ++-- templates/layout.html.php | 4 ++-- 4 files changed, 8 insertions(+), 20 deletions(-) diff --git a/classes/controllers/JokeController.php b/classes/controllers/JokeController.php index 83d04d1..55e4bf6 100644 --- a/classes/controllers/JokeController.php +++ b/classes/controllers/JokeController.php @@ -55,7 +55,7 @@ public function home() { public function delete() { $this->jokesTable->delete($_POST['id']); - header('location: jokes.php'); + header('location: index.php?action=list'); } @@ -68,7 +68,7 @@ public function edit() { $this->jokesTable->save($joke); - header('location: jokes.php'); + header('location: index.php?action=list'); } else { diff --git a/public/index.php b/public/index.php index f406c53..795e029 100644 --- a/public/index.php +++ b/public/index.php @@ -10,21 +10,9 @@ $jokeController = new JokeController($jokesTable, $authorsTable); - if (isset($_GET['edit'])) { - $page = $jokeController->edit(); - } + $action = $_GET['action'] ?? 'home'; - else if (isset($_GET['delete'])) { - $page = $jokeController->delete(); - } - - else if (isset($_GET['list'])) { - $page = $jokeController->list(); - } - - else { - $page = $jokeController->home(); - } + $page = $jokeController->$action(); $title = $page['title']; $output = $page['output']; diff --git a/templates/jokes.html.php b/templates/jokes.html.php index 0d4be22..aaabb3a 100644 --- a/templates/jokes.html.php +++ b/templates/jokes.html.php @@ -15,8 +15,8 @@ echo $date->format('jS F Y'); ?>) - Edit -
+ Edit +
diff --git a/templates/layout.html.php b/templates/layout.html.php index d02faa5..fa460d3 100644 --- a/templates/layout.html.php +++ b/templates/layout.html.php @@ -12,8 +12,8 @@ From 38a694388899b47fd1c2ef99cd7adb207941fa45 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 10 Jun 2017 18:56:26 +0100 Subject: [PATCH 051/124] entrypoint 3 --- classes/controllers/JokeController.php | 31 +++++++++++++------------- public/index.php | 13 ++++++++++- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/classes/controllers/JokeController.php b/classes/controllers/JokeController.php index 55e4bf6..7fe775a 100644 --- a/classes/controllers/JokeController.php +++ b/classes/controllers/JokeController.php @@ -33,23 +33,23 @@ public function list() { ob_start(); - include __DIR__ . '/../../templates/jokes.html.php'; + include __DIR__ . '/../../templates/'; $output = ob_get_clean(); - return ['output' => $output, 'title' => $title]; + return ['template' => 'jokes.html.php', + 'title' => $title, + 'variables' => [ + 'totalJokes' => $totalJokes, + 'jokes' => $jokes + ] + ]; } public function home() { $title = 'Internet Joke Database'; - ob_start(); - - include __DIR__ . '/../../templates/home.html.php'; - - $output = ob_get_clean(); - - return ['output' => $output, 'title' => $title]; + return ['template' => 'home.html.php', 'title' => $title]; } public function delete() { @@ -79,13 +79,12 @@ public function edit() { $title = 'Edit joke'; - ob_start(); - - include __DIR__ . '/../../templates/editjoke.html.php'; - - $output = ob_get_clean(); - - return ['output' => $output, 'title' => $title]; + return ['template' => 'editjoke.html.php', + 'title' => $title, + 'variables' => [ + 'joke' => $joke ?? null + ] + ]; } } } \ No newline at end of file diff --git a/public/index.php b/public/index.php index 795e029..caeb578 100644 --- a/public/index.php +++ b/public/index.php @@ -15,7 +15,18 @@ $page = $jokeController->$action(); $title = $page['title']; - $output = $page['output']; + + + if (isset($page['variables'])) { + extract($page['variables']); + } + + ob_start(); + + include __DIR__ . '/../templates/' . $page['template']; + + $output = ob_get_clean(); + } catch (PDOException $e) { From 7167a0e52ba763034e7db2c3333a961eb124797b Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 10 Jun 2017 18:57:41 +0100 Subject: [PATCH 052/124] entrypoint with loadTemplate function --- public/index.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/public/index.php b/public/index.php index caeb578..6c0d8e5 100644 --- a/public/index.php +++ b/public/index.php @@ -1,4 +1,14 @@ Date: Sat, 10 Jun 2017 19:15:39 +0100 Subject: [PATCH 053/124] now works with any controller --- public/index.php | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/public/index.php b/public/index.php index 6c0d8e5..5483a08 100644 --- a/public/index.php +++ b/public/index.php @@ -8,24 +8,41 @@ function loadTemplate($templateFileName, $variables = []) { return ob_get_clean(); } - try { include __DIR__ . '/../includes/DatabaseConnection.php'; - include __DIR__ . '/../classes/DatabaseTable.php'; - include __DIR__ . '/../classes/controllers/JokeController.php'; + include __DIR__ . '/../classes/controllers/DatabaseTable.php'; $jokesTable = new DatabaseTable($pdo, 'joke', 'id'); $authorsTable = new DatabaseTable($pdo, 'author', 'id'); - $jokeController = new JokeController($jokesTable, $authorsTable); - $action = $_GET['action'] ?? 'home'; - $page = $jokeController->$action(); + $controllerName = $_GET['controller'] ?? 'joke'; + + if ($action == strtolower($action) && $controllerName == strtolower($controllerName)) { + + $className = ucfirst($controllerName) . 'Controller'; + + include __DIR__ . '/../controllers/' . $className . '.php'; + + if ($controllerName === 'joke') { + $arguments = [$jokesTable, $authorsTable]; + } + else if ($controllerName === 'register') { + $arguments = [$authorsTable]; + } + + $controller = new $className(...$arguments); + $page = $controller->$action(); + } + else { + http_response_code(301); + header('location: index.php?controller=' . strtolower($controllerName) . '&action=' . strtolower($action)); + } + $title = $page['title']; - if (isset($page['variables'])) { $output = loadTemplate($page['template'], $page['variables']); @@ -33,8 +50,7 @@ function loadTemplate($templateFileName, $variables = []) { else { $output = loadTemplate($page['template']); } - - + } catch (PDOException $e) { $title = 'An error has occurred'; From d26acdd2764407c8bd4f842b9a609b53834c1812 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 10 Jun 2017 19:16:24 +0100 Subject: [PATCH 054/124] controllers/ in wrong include --- public/index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/public/index.php b/public/index.php index 5483a08..12ffeb9 100644 --- a/public/index.php +++ b/public/index.php @@ -10,7 +10,7 @@ function loadTemplate($templateFileName, $variables = []) { try { include __DIR__ . '/../includes/DatabaseConnection.php'; - include __DIR__ . '/../classes/controllers/DatabaseTable.php'; + include __DIR__ . '/../classes/DatabaseTable.php'; $jokesTable = new DatabaseTable($pdo, 'joke', 'id'); $authorsTable = new DatabaseTable($pdo, 'author', 'id'); @@ -24,7 +24,7 @@ function loadTemplate($templateFileName, $variables = []) { $className = ucfirst($controllerName) . 'Controller'; - include __DIR__ . '/../controllers/' . $className . '.php'; + include __DIR__ . '/../classes/controllers/' . $className . '.php'; if ($controllerName === 'joke') { $arguments = [$jokesTable, $authorsTable]; From 13c3e540c891e8ab74eadfb5f06aacfbf78a077b Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Fri, 16 Jun 2017 12:00:56 +0100 Subject: [PATCH 055/124] uses simpler url routing for easier refactoring later on --- public/index.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/public/index.php b/public/index.php index 12ffeb9..bbd412f 100644 --- a/public/index.php +++ b/public/index.php @@ -27,13 +27,12 @@ function loadTemplate($templateFileName, $variables = []) { include __DIR__ . '/../classes/controllers/' . $className . '.php'; if ($controllerName === 'joke') { - $arguments = [$jokesTable, $authorsTable]; + $controller = new JokeController($jokesTable, $authorsTable); } else if ($controllerName === 'register') { - $arguments = [$authorsTable]; + $controller = new RegisterController($authorsTable); } - - $controller = new $className(...$arguments); + $page = $controller->$action(); } else { From 6d43f231d86b3c39287c35350d8d8470997d6a2b Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 17 Jun 2017 14:51:53 +0100 Subject: [PATCH 056/124] uses routes from the start rather than using controller/action vars then combing them --- public/index.php | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/public/index.php b/public/index.php index bbd412f..ae1aaf4 100644 --- a/public/index.php +++ b/public/index.php @@ -22,18 +22,23 @@ function loadTemplate($templateFileName, $variables = []) { if ($action == strtolower($action) && $controllerName == strtolower($controllerName)) { - $className = ucfirst($controllerName) . 'Controller'; - - include __DIR__ . '/../classes/controllers/' . $className . '.php'; - - if ($controllerName === 'joke') { - $controller = new JokeController($jokesTable, $authorsTable); - } - else if ($controllerName === 'register') { - $controller = new RegisterController($authorsTable); - } - - $page = $controller->$action(); + $route = $_GET['route'] ?? 'joke/home'; //if no route variable is set, use 'joke/home' + + if ($route === 'joke/list') { + include __DIR__ . '/../classes/controllers/JokeController.php'; + $controller = new JokeController($jokesTable, $authorsTable); + $page = $controller->list(); + } + else if ($route === 'joke/home') { + include __DIR__ . '/../classes/controllers/JokeController.php'; + $controller = new JokeController($jokesTable, $authorsTable); + $page = $controller->home(); + } + else if ($route === 'register') { + include __DIR__ . '/../classes/controllers/RegisterController.php'; + $controller = new RegisterController($authorsTable); + $page = $controller->showForm(); + } } else { http_response_code(301); From 78e8091e2920d62e8e2d1a053340282d2d37b1ac Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 17 Jun 2017 14:54:35 +0100 Subject: [PATCH 057/124] added rest of the routes --- public/index.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/public/index.php b/public/index.php index ae1aaf4..48c0b9c 100644 --- a/public/index.php +++ b/public/index.php @@ -34,6 +34,16 @@ function loadTemplate($templateFileName, $variables = []) { $controller = new JokeController($jokesTable, $authorsTable); $page = $controller->home(); } + else if ($route === 'joke/edit') { + include __DIR__ . '/../classes/controllers/JokeController.php'; + $controller = new JokeController($jokesTable, $authorsTable); + $page = $controller->edit(); + } + else if ($route === 'joke/delete') { + include __DIR__ . '/../classes/controllers/JokeController.php'; + $controller = new JokeController($jokesTable, $authorsTable); + $page = $controller->delete(); + } else if ($route === 'register') { include __DIR__ . '/../classes/controllers/RegisterController.php'; $controller = new RegisterController($authorsTable); From cda81063c8a659189601c9f2d4b44ac6cb24c9f6 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 17 Jun 2017 15:59:57 +0100 Subject: [PATCH 058/124] amended 301 case check --- classes/controllers/JokeController.php | 4 ++-- public/index.php | 10 +++------- templates/jokes.html.php | 4 ++-- templates/layout.html.php | 4 ++-- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/classes/controllers/JokeController.php b/classes/controllers/JokeController.php index 7fe775a..c3d369c 100644 --- a/classes/controllers/JokeController.php +++ b/classes/controllers/JokeController.php @@ -55,7 +55,7 @@ public function home() { public function delete() { $this->jokesTable->delete($_POST['id']); - header('location: index.php?action=list'); + header('location: index.php?route=joke/list'); } @@ -68,7 +68,7 @@ public function edit() { $this->jokesTable->save($joke); - header('location: index.php?action=list'); + header('location: index.php?route=joke/list'); } else { diff --git a/public/index.php b/public/index.php index 48c0b9c..99a47ea 100644 --- a/public/index.php +++ b/public/index.php @@ -16,13 +16,9 @@ function loadTemplate($templateFileName, $variables = []) { $authorsTable = new DatabaseTable($pdo, 'author', 'id'); - $action = $_GET['action'] ?? 'home'; + $route = $_GET['route'] ?? 'joke/home'; //if no route variable is set, use 'joke/home' - $controllerName = $_GET['controller'] ?? 'joke'; - - if ($action == strtolower($action) && $controllerName == strtolower($controllerName)) { - - $route = $_GET['route'] ?? 'joke/home'; //if no route variable is set, use 'joke/home' + if ($route == strtolower($route)) { if ($route === 'joke/list') { include __DIR__ . '/../classes/controllers/JokeController.php'; @@ -52,7 +48,7 @@ function loadTemplate($templateFileName, $variables = []) { } else { http_response_code(301); - header('location: index.php?controller=' . strtolower($controllerName) . '&action=' . strtolower($action)); + header('location: index.php?route=' . strtolower($route)); } diff --git a/templates/jokes.html.php b/templates/jokes.html.php index aaabb3a..692b0db 100644 --- a/templates/jokes.html.php +++ b/templates/jokes.html.php @@ -15,8 +15,8 @@ echo $date->format('jS F Y'); ?>) - Edit -
+ Edit +
diff --git a/templates/layout.html.php b/templates/layout.html.php index fa460d3..57a11cf 100644 --- a/templates/layout.html.php +++ b/templates/layout.html.php @@ -12,8 +12,8 @@ From 1459f8333a780c2695671cde5430f5e7c51ad71c Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 17 Jun 2017 16:21:48 +0100 Subject: [PATCH 059/124] uses friendly urls throughout --- classes/controllers/JokeController.php | 4 ++-- public/index.php | 5 ++--- templates/jokes.html.php | 4 ++-- templates/layout.html.php | 8 ++++---- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/classes/controllers/JokeController.php b/classes/controllers/JokeController.php index c3d369c..52899b7 100644 --- a/classes/controllers/JokeController.php +++ b/classes/controllers/JokeController.php @@ -55,7 +55,7 @@ public function home() { public function delete() { $this->jokesTable->delete($_POST['id']); - header('location: index.php?route=joke/list'); + header('location: /joke/list'); } @@ -68,7 +68,7 @@ public function edit() { $this->jokesTable->save($joke); - header('location: index.php?route=joke/list'); + header('location: /joke/list'); } else { diff --git a/public/index.php b/public/index.php index 99a47ea..c12ea7a 100644 --- a/public/index.php +++ b/public/index.php @@ -16,16 +16,15 @@ function loadTemplate($templateFileName, $variables = []) { $authorsTable = new DatabaseTable($pdo, 'author', 'id'); - $route = $_GET['route'] ?? 'joke/home'; //if no route variable is set, use 'joke/home' + $route = ltrim(strtok($_SERVER['REQUEST_URI'], '?'), '/'); if ($route == strtolower($route)) { - if ($route === 'joke/list') { include __DIR__ . '/../classes/controllers/JokeController.php'; $controller = new JokeController($jokesTable, $authorsTable); $page = $controller->list(); } - else if ($route === 'joke/home') { + else if ($route === '') { include __DIR__ . '/../classes/controllers/JokeController.php'; $controller = new JokeController($jokesTable, $authorsTable); $page = $controller->home(); diff --git a/templates/jokes.html.php b/templates/jokes.html.php index 692b0db..942b636 100644 --- a/templates/jokes.html.php +++ b/templates/jokes.html.php @@ -15,8 +15,8 @@ echo $date->format('jS F Y'); ?>) - Edit -
+ Edit +
diff --git a/templates/layout.html.php b/templates/layout.html.php index 57a11cf..6f23acc 100644 --- a/templates/layout.html.php +++ b/templates/layout.html.php @@ -2,7 +2,7 @@ - + <?=$title?> @@ -11,9 +11,9 @@

Internet Joke Database

From 489a75a6f5dfd2e93f4b35e6ba6b43bb4e5669ea Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 17 Jun 2017 17:10:47 +0100 Subject: [PATCH 060/124] fixed 301 recirect url --- public/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/index.php b/public/index.php index c12ea7a..7dc9b13 100644 --- a/public/index.php +++ b/public/index.php @@ -47,7 +47,7 @@ function loadTemplate($templateFileName, $variables = []) { } else { http_response_code(301); - header('location: index.php?route=' . strtolower($route)); + header('location: ' . strtolower($route)); } From 5f75cbdeb2a348513a3f5488a19b864b83dbad02 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 17 Jun 2017 17:32:16 +0100 Subject: [PATCH 061/124] entry point is now a class --- classes/EntryPoint.php | 77 ++++++++++++++++++++++++++++++++++++++++++ public/index.php | 66 ++++-------------------------------- 2 files changed, 83 insertions(+), 60 deletions(-) create mode 100644 classes/EntryPoint.php diff --git a/classes/EntryPoint.php b/classes/EntryPoint.php new file mode 100644 index 0000000..f643a8f --- /dev/null +++ b/classes/EntryPoint.php @@ -0,0 +1,77 @@ +route = $route; + $this->checkUrl(); + } + + private function checkUrl() { + if ($this->route !== strtolower($this->route)) { + http_response_code(301); + header('location: ' . strtolower($this->route)); + } + } + + private function loadTemplate($templateFileName, $variables = []) { + extract($variables); + + ob_start(); + include __DIR__ . '/../templates/' . $templateFileName; + + return ob_get_clean(); + } + + private function callAction() { + include __DIR__ . '/../classes/DatabaseTable.php'; + include __DIR__ . '/../includes/DatabaseConnection.php'; + + $jokesTable = new DatabaseTable($pdo, 'joke', 'id'); + $authorsTable = new DatabaseTable($pdo, 'author', 'id'); + + if ($this->route === 'joke/list') { + include __DIR__ . '/../classes/controllers/JokeController.php'; + $controller = new JokeController($jokesTable, $authorsTable); + $page = $controller->list(); + } + else if ($this->route === '') { + include __DIR__ . '/../classes/controllers/JokeController.php'; + $controller = new JokeController($jokesTable, $authorsTable); + $page = $controller->home(); + } + else if ($this->route === 'joke/edit') { + include __DIR__ . '/../classes/controllers/JokeController.php'; + $controller = new JokeController($jokesTable, $authorsTable); + $page = $controller->edit(); + } + else if ($this->route === 'joke/delete') { + include __DIR__ . '/../classes/controllers/JokeController.php'; + $controller = new JokeController($jokesTable, $authorsTable); + $page = $controller->delete(); + } + else if ($this->route === 'register') { + include __DIR__ . '/../classes/controllers/RegisterController.php'; + $controller = new RegisterController($authorsTable); + $page = $controller->showForm(); + } + + return $page; + } + + public function run() { + + $page = $this->callAction(); + + $title = $page['title']; + + if (isset($page['variables'])) { + $output = $this->loadTemplate($page['template'], $page['variables']); + } + else { + $output = $this->loadTemplate($page['template']); + } + + include __DIR__ . '/../templates/layout.html.php'; + } +} \ No newline at end of file diff --git a/public/index.php b/public/index.php index 7dc9b13..7b0cc31 100644 --- a/public/index.php +++ b/public/index.php @@ -1,71 +1,17 @@ list(); - } - else if ($route === '') { - include __DIR__ . '/../classes/controllers/JokeController.php'; - $controller = new JokeController($jokesTable, $authorsTable); - $page = $controller->home(); - } - else if ($route === 'joke/edit') { - include __DIR__ . '/../classes/controllers/JokeController.php'; - $controller = new JokeController($jokesTable, $authorsTable); - $page = $controller->edit(); - } - else if ($route === 'joke/delete') { - include __DIR__ . '/../classes/controllers/JokeController.php'; - $controller = new JokeController($jokesTable, $authorsTable); - $page = $controller->delete(); - } - else if ($route === 'register') { - include __DIR__ . '/../classes/controllers/RegisterController.php'; - $controller = new RegisterController($authorsTable); - $page = $controller->showForm(); - } - } - else { - http_response_code(301); - header('location: ' . strtolower($route)); - } - - - $title = $page['title']; - - if (isset($page['variables'])) { - $output = loadTemplate($page['template'], $page['variables']); - } - else { - $output = loadTemplate($page['template']); - } - + $entryPoint = new EntryPoint($route); + $entryPoint->run(); } catch (PDOException $e) { $title = 'An error has occurred'; $output = 'Database error: ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine(); -} -include __DIR__ . '/../templates/layout.html.php'; \ No newline at end of file + include __DIR__ . '/../templates/layout.html.php'; +} From 5fe9983284681983ed2b05b14ec4b66a4171c233 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 17 Jun 2017 18:16:51 +0100 Subject: [PATCH 062/124] moved router to its own file --- classes/EntryPoint.php | 42 ++++-------------------------------------- classes/IjdbRoutes.php | 38 ++++++++++++++++++++++++++++++++++++++ includes/autoload.php | 7 +++++++ public/index.php | 5 +++-- 4 files changed, 52 insertions(+), 40 deletions(-) create mode 100644 classes/IjdbRoutes.php create mode 100644 includes/autoload.php diff --git a/classes/EntryPoint.php b/classes/EntryPoint.php index f643a8f..1442707 100644 --- a/classes/EntryPoint.php +++ b/classes/EntryPoint.php @@ -1,9 +1,11 @@ route = $route; + $this->routes = $routes; $this->checkUrl(); } @@ -23,45 +25,9 @@ private function loadTemplate($templateFileName, $variables = []) { return ob_get_clean(); } - private function callAction() { - include __DIR__ . '/../classes/DatabaseTable.php'; - include __DIR__ . '/../includes/DatabaseConnection.php'; - - $jokesTable = new DatabaseTable($pdo, 'joke', 'id'); - $authorsTable = new DatabaseTable($pdo, 'author', 'id'); - - if ($this->route === 'joke/list') { - include __DIR__ . '/../classes/controllers/JokeController.php'; - $controller = new JokeController($jokesTable, $authorsTable); - $page = $controller->list(); - } - else if ($this->route === '') { - include __DIR__ . '/../classes/controllers/JokeController.php'; - $controller = new JokeController($jokesTable, $authorsTable); - $page = $controller->home(); - } - else if ($this->route === 'joke/edit') { - include __DIR__ . '/../classes/controllers/JokeController.php'; - $controller = new JokeController($jokesTable, $authorsTable); - $page = $controller->edit(); - } - else if ($this->route === 'joke/delete') { - include __DIR__ . '/../classes/controllers/JokeController.php'; - $controller = new JokeController($jokesTable, $authorsTable); - $page = $controller->delete(); - } - else if ($this->route === 'register') { - include __DIR__ . '/../classes/controllers/RegisterController.php'; - $controller = new RegisterController($authorsTable); - $page = $controller->showForm(); - } - - return $page; - } - public function run() { - $page = $this->callAction(); + $page = $this->routes->callAction($this->route); $title = $page['title']; diff --git a/classes/IjdbRoutes.php b/classes/IjdbRoutes.php new file mode 100644 index 0000000..bfef696 --- /dev/null +++ b/classes/IjdbRoutes.php @@ -0,0 +1,38 @@ +list(); + } + else if ($route === '') { + include __DIR__ . '/../classes/controllers/JokeController.php'; + $controller = new JokeController($jokesTable, $authorsTable); + $page = $controller->home(); + } + else if ($route === 'joke/edit') { + include __DIR__ . '/../classes/controllers/JokeController.php'; + $controller = new JokeController($jokesTable, $authorsTable); + $page = $controller->edit(); + } + else if ($route === 'joke/delete') { + include __DIR__ . '/../classes/controllers/JokeController.php'; + $controller = new JokeController($jokesTable, $authorsTable); + $page = $controller->delete(); + } + else if ($route === 'register') { + include __DIR__ . '/../classes/controllers/RegisterController.php'; + $controller = new RegisterController($authorsTable); + $page = $controller->showForm(); + } + + return $page; + } +} \ No newline at end of file diff --git a/includes/autoload.php b/includes/autoload.php new file mode 100644 index 0000000..5725489 --- /dev/null +++ b/includes/autoload.php @@ -0,0 +1,7 @@ +run(); } catch (PDOException $e) { From 9f46b49eb854835b2f998629ffa33f4e2f9b5eb0 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 17 Jun 2017 18:17:18 +0100 Subject: [PATCH 063/124] remove autoloader --- includes/autoload.php | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 includes/autoload.php diff --git a/includes/autoload.php b/includes/autoload.php deleted file mode 100644 index 5725489..0000000 --- a/includes/autoload.php +++ /dev/null @@ -1,7 +0,0 @@ - Date: Sat, 17 Jun 2017 18:18:05 +0100 Subject: [PATCH 064/124] implemented autoloader --- includes/autoload.php | 7 +++++++ public/index.php | 1 - 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 includes/autoload.php diff --git a/includes/autoload.php b/includes/autoload.php new file mode 100644 index 0000000..5725489 --- /dev/null +++ b/includes/autoload.php @@ -0,0 +1,7 @@ + Date: Sat, 17 Jun 2017 18:27:48 +0100 Subject: [PATCH 065/124] implemented autoloader --- classes/IjdbRoutes.php | 1 - 1 file changed, 1 deletion(-) diff --git a/classes/IjdbRoutes.php b/classes/IjdbRoutes.php index bfef696..06bb8b8 100644 --- a/classes/IjdbRoutes.php +++ b/classes/IjdbRoutes.php @@ -1,7 +1,6 @@ Date: Mon, 19 Jun 2017 14:59:22 +0100 Subject: [PATCH 066/124] moved everything to namespaces --- .../Controllers/Joke.php} | 4 +- classes/Ijdb/IjdbRoutes.php | 34 +++++++++++++++++ classes/IjdbRoutes.php | 37 ------------------- classes/{ => Ninja}/DatabaseTable.php | 4 +- classes/{ => Ninja}/EntryPoint.php | 6 ++- includes/autoload.php | 5 ++- public/index.php | 2 +- 7 files changed, 49 insertions(+), 43 deletions(-) rename classes/{controllers/JokeController.php => Ijdb/Controllers/Joke.php} (96%) create mode 100644 classes/Ijdb/IjdbRoutes.php delete mode 100644 classes/IjdbRoutes.php rename classes/{ => Ninja}/DatabaseTable.php (96%) rename classes/{ => Ninja}/EntryPoint.php (85%) diff --git a/classes/controllers/JokeController.php b/classes/Ijdb/Controllers/Joke.php similarity index 96% rename from classes/controllers/JokeController.php rename to classes/Ijdb/Controllers/Joke.php index 52899b7..24d73bf 100644 --- a/classes/controllers/JokeController.php +++ b/classes/Ijdb/Controllers/Joke.php @@ -1,6 +1,8 @@ list(); + } + else if ($route === '') { + $controller = new \Ijdb\Controllers\Joke($jokesTable, $authorsTable); + $page = $controller->home(); + } + else if ($route === 'joke/edit') { + $controller = new \Ijdb\Controllers\Joke($jokesTable, $authorsTable); + $page = $controller->edit(); + } + else if ($route === 'joke/delete') { + $controller = new \Ijdb\Controllers\Joke($jokesTable, $authorsTable); + $page = $controller->delete(); + } + else if ($route === 'register') { + $controller = new \Ijdb\Controllers\Register($authorsTable); + $page = $controller->showForm(); + } + + return $page; + } +} \ No newline at end of file diff --git a/classes/IjdbRoutes.php b/classes/IjdbRoutes.php deleted file mode 100644 index 06bb8b8..0000000 --- a/classes/IjdbRoutes.php +++ /dev/null @@ -1,37 +0,0 @@ -list(); - } - else if ($route === '') { - include __DIR__ . '/../classes/controllers/JokeController.php'; - $controller = new JokeController($jokesTable, $authorsTable); - $page = $controller->home(); - } - else if ($route === 'joke/edit') { - include __DIR__ . '/../classes/controllers/JokeController.php'; - $controller = new JokeController($jokesTable, $authorsTable); - $page = $controller->edit(); - } - else if ($route === 'joke/delete') { - include __DIR__ . '/../classes/controllers/JokeController.php'; - $controller = new JokeController($jokesTable, $authorsTable); - $page = $controller->delete(); - } - else if ($route === 'register') { - include __DIR__ . '/../classes/controllers/RegisterController.php'; - $controller = new RegisterController($authorsTable); - $page = $controller->showForm(); - } - - return $page; - } -} \ No newline at end of file diff --git a/classes/DatabaseTable.php b/classes/Ninja/DatabaseTable.php similarity index 96% rename from classes/DatabaseTable.php rename to classes/Ninja/DatabaseTable.php index 59c5429..96108b3 100644 --- a/classes/DatabaseTable.php +++ b/classes/Ninja/DatabaseTable.php @@ -1,10 +1,12 @@ pdo = $pdo; $this->table = $table; $this->primaryKey = $primaryKey; diff --git a/classes/EntryPoint.php b/classes/Ninja/EntryPoint.php similarity index 85% rename from classes/EntryPoint.php rename to classes/Ninja/EntryPoint.php index 1442707..4046f18 100644 --- a/classes/EntryPoint.php +++ b/classes/Ninja/EntryPoint.php @@ -1,4 +1,6 @@ loadTemplate($page['template']); } - include __DIR__ . '/../templates/layout.html.php'; + include __DIR__ . '/../../templates/layout.html.php'; } } \ No newline at end of file diff --git a/includes/autoload.php b/includes/autoload.php index 5725489..23a2652 100644 --- a/includes/autoload.php +++ b/includes/autoload.php @@ -1,6 +1,9 @@ run(); } catch (PDOException $e) { From dbcab36456429799173f8c2cd49bc1a6dbdb3df2 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Mon, 19 Jun 2017 15:49:42 +0100 Subject: [PATCH 067/124] now uses a generic router --- classes/Ijdb/Controllers/Joke.php | 42 +++++++++++------------ classes/Ijdb/IjdbRoutes.php | 57 +++++++++++++++++++------------ classes/Ninja/DatabaseTable.php | 4 +-- classes/Ninja/EntryPoint.php | 11 ++++-- public/index.php | 2 +- 5 files changed, 66 insertions(+), 50 deletions(-) diff --git a/classes/Ijdb/Controllers/Joke.php b/classes/Ijdb/Controllers/Joke.php index 24d73bf..e2a296c 100644 --- a/classes/Ijdb/Controllers/Joke.php +++ b/classes/Ijdb/Controllers/Joke.php @@ -60,33 +60,29 @@ public function delete() { header('location: /joke/list'); } + public function saveEdit() { + $joke = $_POST['joke']; + $joke['jokedate'] = new \DateTime(); + $joke['authorId'] = 1; - public function edit() { - if (isset($_POST['joke'])) { - - $joke = $_POST['joke']; - $joke['jokedate'] = new DateTime(); - $joke['authorId'] = 1; - - $this->jokesTable->save($joke); - - header('location: /joke/list'); + $this->jokesTable->save($joke); + + header('location: /joke/list'); + } + public function edit() { + if (isset($_GET['id'])) { + $joke = $this->jokesTable->findById($_GET['id']); } - else { - if (isset($_GET['id'])) { - $joke = $this->jokesTable->findById($_GET['id']); - } + $title = 'Edit joke'; - $title = 'Edit joke'; - - return ['template' => 'editjoke.html.php', - 'title' => $title, - 'variables' => [ - 'joke' => $joke ?? null - ] - ]; - } + return ['template' => 'editjoke.html.php', + 'title' => $title, + 'variables' => [ + 'joke' => $joke ?? null + ] + ]; } + } \ No newline at end of file diff --git a/classes/Ijdb/IjdbRoutes.php b/classes/Ijdb/IjdbRoutes.php index db076b8..bf4057a 100644 --- a/classes/Ijdb/IjdbRoutes.php +++ b/classes/Ijdb/IjdbRoutes.php @@ -2,33 +2,46 @@ namespace Ijdb; class IjdbRoutes { - public function callAction($route) { + public function getRoutes() { include __DIR__ . '/../../includes/DatabaseConnection.php'; $jokesTable = new \Ninja\DatabaseTable($pdo, 'joke', 'id'); $authorsTable = new \Ninja\DatabaseTable($pdo, 'author', 'id'); - if ($route === 'joke/list') { - $controller = new \Ijdb\Controllers\Joke($jokesTable, $authorsTable); - $page = $controller->list(); - } - else if ($route === '') { - $controller = new \Ijdb\Controllers\Joke($jokesTable, $authorsTable); - $page = $controller->home(); - } - else if ($route === 'joke/edit') { - $controller = new \Ijdb\Controllers\Joke($jokesTable, $authorsTable); - $page = $controller->edit(); - } - else if ($route === 'joke/delete') { - $controller = new \Ijdb\Controllers\Joke($jokesTable, $authorsTable); - $page = $controller->delete(); - } - else if ($route === 'register') { - $controller = new \Ijdb\Controllers\Register($authorsTable); - $page = $controller->showForm(); - } + $jokeController = new \Ijdb\Controllers\Joke($jokesTable, $authorsTable); - return $page; + $routes = [ + 'joke/edit' => [ + 'POST' => [ + 'controller' => $jokeController, + 'action' => 'saveEdit' + ], + 'GET' => [ + 'controller' => $jokeController, + 'action' => 'edit' + ] + + ], + 'joke/delete' => [ + 'POST' => [ + 'controller' => $jokeController, + 'action' => 'delete' + ] + ], + 'joke/list' => [ + 'GET' => [ + 'controller' => $jokeController, + 'action' => 'list' + ] + ], + '' => [ + 'GET' => [ + 'controller' => $jokeController, + 'action' => 'home' + ] + ] + ]; + + return $routes; } } \ No newline at end of file diff --git a/classes/Ninja/DatabaseTable.php b/classes/Ninja/DatabaseTable.php index 96108b3..5fa7a02 100644 --- a/classes/Ninja/DatabaseTable.php +++ b/classes/Ninja/DatabaseTable.php @@ -98,7 +98,7 @@ public function findAll() { private function processDates($fields) { foreach ($fields as $key => $value) { - if ($value instanceof DateTime) { + if ($value instanceof \DateTime) { $fields[$key] = $value->format('Y-m-d'); } } @@ -114,7 +114,7 @@ public function save($record) { } $this->insert($record); } - catch (PDOException $e) { + catch (\PDOException $e) { $this->update( $record); } } diff --git a/classes/Ninja/EntryPoint.php b/classes/Ninja/EntryPoint.php index 4046f18..64399e3 100644 --- a/classes/Ninja/EntryPoint.php +++ b/classes/Ninja/EntryPoint.php @@ -3,11 +3,13 @@ class EntryPoint { private $route; + private $method; private $routes; - public function __construct($route, $routes) { + public function __construct($route, $method, $routes) { $this->route = $route; $this->routes = $routes; + $this->method = $method; $this->checkUrl(); } @@ -29,7 +31,12 @@ private function loadTemplate($templateFileName, $variables = []) { public function run() { - $page = $this->routes->callAction($this->route); + $routes = $this->routes->getRoutes(); + + $controller = $routes[$this->route][$this->method]['controller']; + $action = $routes[$this->route][$this->method]['action']; + + $page = $controller->$action(); $title = $page['title']; diff --git a/public/index.php b/public/index.php index 8b86bf7..41e307a 100644 --- a/public/index.php +++ b/public/index.php @@ -4,7 +4,7 @@ $route = ltrim(strtok($_SERVER['REQUEST_URI'], '?'), '/'); - $entryPoint = new \Ninja\EntryPoint($route, new \Ijdb\IjdbRoutes()); + $entryPoint = new \Ninja\EntryPoint($route, $_SERVER['REQUEST_METHOD'], new \Ijdb\IjdbRoutes()); $entryPoint->run(); } catch (PDOException $e) { From 5543bdf73abccd1193f22a40c5e5a308db5c7cfe Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Mon, 19 Jun 2017 16:45:32 +0100 Subject: [PATCH 068/124] added interface --- classes/Ijdb/IjdbRoutes.php | 2 +- classes/Ninja/EntryPoint.php | 2 +- classes/Ninja/Routes.php | 6 ++++++ 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 classes/Ninja/Routes.php diff --git a/classes/Ijdb/IjdbRoutes.php b/classes/Ijdb/IjdbRoutes.php index bf4057a..ea99986 100644 --- a/classes/Ijdb/IjdbRoutes.php +++ b/classes/Ijdb/IjdbRoutes.php @@ -1,7 +1,7 @@ route = $route; $this->routes = $routes; $this->method = $method; diff --git a/classes/Ninja/Routes.php b/classes/Ninja/Routes.php new file mode 100644 index 0000000..3fc3b37 --- /dev/null +++ b/classes/Ninja/Routes.php @@ -0,0 +1,6 @@ + Date: Wed, 21 Jun 2017 14:59:03 +0100 Subject: [PATCH 069/124] added registration form --- classes/Ijdb/Controllers/Register.php | 30 +++++++++++++++++++++++++++ classes/Ijdb/IjdbRoutes.php | 17 +++++++++++++++ database.sql | 3 ++- templates/register.html.php | 12 +++++++++++ templates/registersuccess.html.php | 3 +++ 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 classes/Ijdb/Controllers/Register.php create mode 100644 templates/register.html.php create mode 100644 templates/registersuccess.html.php diff --git a/classes/Ijdb/Controllers/Register.php b/classes/Ijdb/Controllers/Register.php new file mode 100644 index 0000000..a010205 --- /dev/null +++ b/classes/Ijdb/Controllers/Register.php @@ -0,0 +1,30 @@ +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']; + + $this->authorsTable->save($author); + + header('Location: /author/success'); + } +} \ No newline at end of file diff --git a/classes/Ijdb/IjdbRoutes.php b/classes/Ijdb/IjdbRoutes.php index ea99986..7e41507 100644 --- a/classes/Ijdb/IjdbRoutes.php +++ b/classes/Ijdb/IjdbRoutes.php @@ -9,8 +9,25 @@ public function getRoutes() { $authorsTable = new \Ninja\DatabaseTable($pdo, 'author', 'id'); $jokeController = new \Ijdb\Controllers\Joke($jokesTable, $authorsTable); + $authorController = new \Ijdb\Controllers\Register($authorsTable); $routes = [ + 'author/register' => [ + 'GET' => [ + 'controller' => $authorController, + 'action' => 'registrationForm' + ], + 'POST' => [ + 'controller' => $authorController, + 'action' => 'registerUser' + ] + ], + 'author/success' => [ + 'GET' => [ + 'controller' => $authorController, + 'action' => 'success' + ] + ], 'joke/edit' => [ 'POST' => [ 'controller' => $jokeController, diff --git a/database.sql b/database.sql index 3c09a0c..d273fc3 100644 --- a/database.sql +++ b/database.sql @@ -26,6 +26,7 @@ CREATE TABLE `author` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `email` varchar(255) DEFAULT NULL, + `password` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; @@ -36,7 +37,7 @@ CREATE TABLE `author` ( LOCK TABLES `author` WRITE; /*!40000 ALTER TABLE `author` DISABLE KEYS */; -INSERT INTO `author` VALUES (1,'Kevin Yank','thatguy@kevinyank.com'),(2,'Tom Butler','tom@r.je'); +INSERT INTO `author` VALUES (1,'Kevin Yank','thatguy@kevinyank.com', ''),(2,'Tom Butler','tom@r.je', ''); /*!40000 ALTER TABLE `author` ENABLE KEYS */; UNLOCK TABLES; diff --git a/templates/register.html.php b/templates/register.html.php new file mode 100644 index 0000000..7e012c2 --- /dev/null +++ b/templates/register.html.php @@ -0,0 +1,12 @@ +
+ + + + + + + + + + +
\ No newline at end of file diff --git a/templates/registersuccess.html.php b/templates/registersuccess.html.php new file mode 100644 index 0000000..659fc33 --- /dev/null +++ b/templates/registersuccess.html.php @@ -0,0 +1,3 @@ +

Registration Successful

+ +

You are now registered on the Internet Joke Database

\ No newline at end of file From f1176c646f729609fc5f9034e9eb160a7a4cb4e9 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Wed, 21 Jun 2017 15:54:47 +0100 Subject: [PATCH 070/124] added validation --- classes/Ijdb/Controllers/Register.php | 41 +++++++++++++++++++++++++-- public/jokes.css | 5 +++- templates/register.html.php | 25 ++++++++++++++-- 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/classes/Ijdb/Controllers/Register.php b/classes/Ijdb/Controllers/Register.php index a010205..9ea642f 100644 --- a/classes/Ijdb/Controllers/Register.php +++ b/classes/Ijdb/Controllers/Register.php @@ -17,14 +17,49 @@ public function registrationForm() { public function success() { return ['template' => 'registersuccess.html.php', - 'title' => 'Registration Successful']; + 'title' => 'Registration Successful']; } public function registerUser() { $author = $_POST['author']; - $this->authorsTable->save($author); + //Assume the data is valid to begin with + $valid = true; + $errors = []; - header('Location: /author/success'); + //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'; + } + + if (empty($author['password'])) { + $valid = false; + $errors[] = 'Password cannot be blank'; + } + + //If $valid is still true, no fields were blank and the datacan be added + if ($valid == true) { + $this->authorsTable->save($author); + + $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 + ] + ]; + } } } \ No newline at end of file diff --git a/public/jokes.css b/public/jokes.css index b613ccb..1e056ad 100644 --- a/public/jokes.css +++ b/public/jokes.css @@ -24,4 +24,7 @@ form {overflow: auto; clear: both; display: block;} blockquote {display: table; margin-bottom: 1em; border-bottom: 1px solid #ccc; padding: 0.5em; } blockquote p {display: table-cell; width: 90%; vertical-align: top;} -blockquote form { display: table-cell; width: 10%;} \ No newline at end of file +blockquote form { display: table-cell; width: 10%;} + +.errors {padding: 1em; border: 1px solid red; background-color: lightyellow; color: red; margin-bottom: 1em; overflow: auto;} +.errors ul {margin-left: 1em;} \ No newline at end of file diff --git a/templates/register.html.php b/templates/register.html.php index 7e012c2..63f24aa 100644 --- a/templates/register.html.php +++ b/templates/register.html.php @@ -1,12 +1,31 @@ + +
+

Your account could not be created, please check the following:

+
    + +
  • + +
+
+
- + - + - +
\ No newline at end of file From f626a6219b6821bc18dba79e34bd7d591b47598f Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Wed, 21 Jun 2017 16:20:11 +0100 Subject: [PATCH 071/124] fixed filter --- classes/Ijdb/Controllers/Register.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/classes/Ijdb/Controllers/Register.php b/classes/Ijdb/Controllers/Register.php index 9ea642f..b2b6105 100644 --- a/classes/Ijdb/Controllers/Register.php +++ b/classes/Ijdb/Controllers/Register.php @@ -37,6 +37,10 @@ public function registerUser() { $valid = false; $errors[] = 'Email cannot be blank'; } + else if (filter_var($author['email'], FILTER_VALIDATE_EMAIL) == false) { + $valid = false; + $errors[] = 'Invalid email address'; + } if (empty($author['password'])) { $valid = false; From 63359622b6c0cb5b3c9da77cf9b9f5287b588ef9 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Wed, 21 Jun 2017 17:21:11 +0100 Subject: [PATCH 072/124] checks for duplicate users --- classes/Ijdb/Controllers/Register.php | 15 +++++++++++++-- classes/Ninja/DatabaseTable.php | 11 +++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/classes/Ijdb/Controllers/Register.php b/classes/Ijdb/Controllers/Register.php index b2b6105..543efb2 100644 --- a/classes/Ijdb/Controllers/Register.php +++ b/classes/Ijdb/Controllers/Register.php @@ -41,16 +41,27 @@ public function registerUser() { $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 datacan be added + //If $valid is still true, no fields were blank and the data can be added if ($valid == true) { - $this->authorsTable->save($author); + //When submitted, the $author variable now contains a lowercase value for email $this->authorsTable->save($author); header('Location: /author/success'); diff --git a/classes/Ninja/DatabaseTable.php b/classes/Ninja/DatabaseTable.php index 5fa7a02..7282778 100644 --- a/classes/Ninja/DatabaseTable.php +++ b/classes/Ninja/DatabaseTable.php @@ -36,6 +36,17 @@ public function findById($value) { return $query->fetch(); } + public function find($column, $value) { + $query = 'SELECT * FROM ' . $this->table . ' WHERE ' . $column . ' = :value'; + + $parameters = [ + 'value' => $value + ]; + + $query = $this->query($query, $parameters); + + return $query->fetchAll(); + } private function insert($fields) { $query = 'INSERT INTO `' . $this->table . '` ('; From ad0460d6b549593cda54e234ae269b3eb5ec418c Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 22 Jun 2017 16:53:35 +0100 Subject: [PATCH 073/124] password hashing --- classes/Ijdb/Controllers/Register.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/classes/Ijdb/Controllers/Register.php b/classes/Ijdb/Controllers/Register.php index 543efb2..275add7 100644 --- a/classes/Ijdb/Controllers/Register.php +++ b/classes/Ijdb/Controllers/Register.php @@ -60,8 +60,11 @@ public function registerUser() { //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'); From a26bf9468226ee6c95f0fd628eea6805f8af9126 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 24 Jun 2017 15:49:09 +0100 Subject: [PATCH 074/124] has login check --- classes/Ijdb/IjdbRoutes.php | 26 +++++++++++++++------ classes/Ninja/Authentication.php | 39 ++++++++++++++++++++++++++++++++ classes/Ninja/EntryPoint.php | 32 +++++++++++++++++--------- classes/Ninja/Routes.php | 3 ++- 4 files changed, 81 insertions(+), 19 deletions(-) create mode 100644 classes/Ninja/Authentication.php diff --git a/classes/Ijdb/IjdbRoutes.php b/classes/Ijdb/IjdbRoutes.php index 7e41507..619c675 100644 --- a/classes/Ijdb/IjdbRoutes.php +++ b/classes/Ijdb/IjdbRoutes.php @@ -2,14 +2,19 @@ namespace Ijdb; class IjdbRoutes implements \Ninja\Routes { - public function getRoutes() { + private $authorsTable; + private $jokesTable; + + public function __construct() { include __DIR__ . '/../../includes/DatabaseConnection.php'; - $jokesTable = new \Ninja\DatabaseTable($pdo, 'joke', 'id'); - $authorsTable = new \Ninja\DatabaseTable($pdo, 'author', 'id'); + $this->jokesTable = new \Ninja\DatabaseTable($pdo, 'joke', 'id'); + $this->authorsTable = new \Ninja\DatabaseTable($pdo, 'author', 'id'); + } - $jokeController = new \Ijdb\Controllers\Joke($jokesTable, $authorsTable); - $authorController = new \Ijdb\Controllers\Register($authorsTable); + public function getRoutes(): array { + $jokeController = new \Ijdb\Controllers\Joke($this->jokesTable, $this->authorsTable); + $authorController = new \Ijdb\Controllers\Register($this->authorsTable); $routes = [ 'author/register' => [ @@ -36,14 +41,16 @@ public function getRoutes() { 'GET' => [ 'controller' => $jokeController, 'action' => 'edit' - ] + ], + 'login' => true ], 'joke/delete' => [ 'POST' => [ 'controller' => $jokeController, 'action' => 'delete' - ] + ], + 'login' => true ], 'joke/list' => [ 'GET' => [ @@ -61,4 +68,9 @@ public function getRoutes() { return $routes; } + + public function getAuthentication(): \Ninja\Authentication { + return new \Ninja\Authentication($this->authorsTable, 'email', 'password'); + } + } \ No newline at end of file diff --git a/classes/Ninja/Authentication.php b/classes/Ninja/Authentication.php new file mode 100644 index 0000000..84dc11a --- /dev/null +++ b/classes/Ninja/Authentication.php @@ -0,0 +1,39 @@ +users = $users; + $this->usernameColumn = $usernameColumn; + $this->passwordColumn = $passwordColumn; + } + + public function login($username, $password) { + $user = $this->users->find($this->usernameColumn, strtolower($username)); + + if (!empty($user) && password_verify($_SESSION['password'], $user[$this->passwordColumn])) { + $_SESSION['username'] = $username; + $_SESSION['password'] = $user['password']; + return true; + } + else { + return false; + } + } + + public function isLoggedIn() { + $user = $this->users->find($this->usernameColumn, strtolower($_SESSION['username'])); + + if (!empty($author) && $author[$this->passwordColumn] === $_SESSION['password']) { + return true; + } + else { + return false; + } + } +} \ No newline at end of file diff --git a/classes/Ninja/EntryPoint.php b/classes/Ninja/EntryPoint.php index 8f9deec..af29260 100644 --- a/classes/Ninja/EntryPoint.php +++ b/classes/Ninja/EntryPoint.php @@ -31,22 +31,32 @@ private function loadTemplate($templateFileName, $variables = []) { public function run() { - $routes = $this->routes->getRoutes(); + $routes = $this->routes->getRoutes(); + + + if (isset($routes[$this->route]['login']) && isset($routes[$this->route]['login'])) { + $authentication = $this->routes->getAuthentication(); + if (!$authentication->isLoggedIn()) { + header('location: /login/error'); + } + } + else { + $controller = $routes[$this->route][$this->method]['controller']; + $action = $routes[$this->route][$this->method]['action']; + $page = $controller->$action(); - $controller = $routes[$this->route][$this->method]['controller']; - $action = $routes[$this->route][$this->method]['action']; + $title = $page['title']; - $page = $controller->$action(); + if (isset($page['variables'])) { + $output = $this->loadTemplate($page['template'], $page['variables']); + } + else { + $output = $this->loadTemplate($page['template']); + } - $title = $page['title']; + include __DIR__ . '/../../templates/layout.html.php'; - if (isset($page['variables'])) { - $output = $this->loadTemplate($page['template'], $page['variables']); - } - else { - $output = $this->loadTemplate($page['template']); } - include __DIR__ . '/../../templates/layout.html.php'; } } \ No newline at end of file diff --git a/classes/Ninja/Routes.php b/classes/Ninja/Routes.php index 3fc3b37..c6aec0e 100644 --- a/classes/Ninja/Routes.php +++ b/classes/Ninja/Routes.php @@ -2,5 +2,6 @@ namespace Ninja; interface Routes { - public function getRoutes(); + public function getRoutes(): array; + public function getAuthentication(): \Ninja\Authentication; } \ No newline at end of file From 69b85efd1641b74c1cc20af14186e44eb59ec295 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 24 Jun 2017 16:19:24 +0100 Subject: [PATCH 075/124] added error page --- classes/Ijdb/Controllers/Login.php | 9 +++++++++ classes/Ijdb/IjdbRoutes.php | 11 ++++++++++- templates/loginerror.html.php | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 classes/Ijdb/Controllers/Login.php create mode 100644 templates/loginerror.html.php diff --git a/classes/Ijdb/Controllers/Login.php b/classes/Ijdb/Controllers/Login.php new file mode 100644 index 0000000..a876d17 --- /dev/null +++ b/classes/Ijdb/Controllers/Login.php @@ -0,0 +1,9 @@ + 'loginerror.html.php', 'title' => 'You are not logged in']; + } +} diff --git a/classes/Ijdb/IjdbRoutes.php b/classes/Ijdb/IjdbRoutes.php index 619c675..cce88c9 100644 --- a/classes/Ijdb/IjdbRoutes.php +++ b/classes/Ijdb/IjdbRoutes.php @@ -4,17 +4,20 @@ class IjdbRoutes implements \Ninja\Routes { private $authorsTable; private $jokesTable; + private $authentication; public function __construct() { include __DIR__ . '/../../includes/DatabaseConnection.php'; $this->jokesTable = new \Ninja\DatabaseTable($pdo, 'joke', 'id'); $this->authorsTable = new \Ninja\DatabaseTable($pdo, 'author', 'id'); + $this->authentication = new \Ninja\Authentication($this->authorsTable, 'email', 'password'); } public function getRoutes(): array { $jokeController = new \Ijdb\Controllers\Joke($this->jokesTable, $this->authorsTable); $authorController = new \Ijdb\Controllers\Register($this->authorsTable); + $loginController = new \Ijdb\Controllers\Login(); $routes = [ 'author/register' => [ @@ -58,6 +61,12 @@ public function getRoutes(): array { 'action' => 'list' ] ], + 'login/error' => [ + 'GET' => [ + 'controller' => $loginController, + 'action' => 'error' + ] + ], '' => [ 'GET' => [ 'controller' => $jokeController, @@ -70,7 +79,7 @@ public function getRoutes(): array { } public function getAuthentication(): \Ninja\Authentication { - return new \Ninja\Authentication($this->authorsTable, 'email', 'password'); + return $this->authentication; } } \ No newline at end of file diff --git a/templates/loginerror.html.php b/templates/loginerror.html.php new file mode 100644 index 0000000..17c62c8 --- /dev/null +++ b/templates/loginerror.html.php @@ -0,0 +1,3 @@ +

You are not logged in

+ +

You must be logged in to view this page. Click here to log in.

\ No newline at end of file From a240b9af846328659944fe8da97a5952db8595fc Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 24 Jun 2017 18:41:38 +0100 Subject: [PATCH 076/124] login form --- classes/Ijdb/Controllers/Login.php | 27 ++++++++++++++ classes/Ijdb/IjdbRoutes.php | 18 ++++++++- classes/Ninja/Authentication.php | 59 +++++++++++++++--------------- classes/Ninja/EntryPoint.php | 10 ++--- templates/login.html.php | 14 +++++++ templates/loginsuccess.html.php | 3 ++ 6 files changed, 95 insertions(+), 36 deletions(-) create mode 100644 templates/login.html.php create mode 100644 templates/loginsuccess.html.php diff --git a/classes/Ijdb/Controllers/Login.php b/classes/Ijdb/Controllers/Login.php index a876d17..62c803e 100644 --- a/classes/Ijdb/Controllers/Login.php +++ b/classes/Ijdb/Controllers/Login.php @@ -2,6 +2,33 @@ 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']; diff --git a/classes/Ijdb/IjdbRoutes.php b/classes/Ijdb/IjdbRoutes.php index cce88c9..0bfdb0d 100644 --- a/classes/Ijdb/IjdbRoutes.php +++ b/classes/Ijdb/IjdbRoutes.php @@ -17,7 +17,7 @@ public function __construct() { public function getRoutes(): array { $jokeController = new \Ijdb\Controllers\Joke($this->jokesTable, $this->authorsTable); $authorController = new \Ijdb\Controllers\Register($this->authorsTable); - $loginController = new \Ijdb\Controllers\Login(); + $loginController = new \Ijdb\Controllers\Login($this->authentication); $routes = [ 'author/register' => [ @@ -67,6 +67,22 @@ public function getRoutes(): array { 'action' => 'error' ] ], + 'login/success' => [ + 'GET' => [ + 'controller' => $loginController, + 'action' => 'success' + ] + ], + 'login' => [ + 'GET' => [ + 'controller' => $loginController, + 'action' => 'loginForm' + ], + 'POST' => [ + 'controller' => $loginController, + 'action' => 'processLogin' + ] + ], '' => [ 'GET' => [ 'controller' => $jokeController, diff --git a/classes/Ninja/Authentication.php b/classes/Ninja/Authentication.php index 84dc11a..7d9d15a 100644 --- a/classes/Ninja/Authentication.php +++ b/classes/Ninja/Authentication.php @@ -2,38 +2,39 @@ namespace Ninja; class Authentication { - private $users; - private $usernameColumn; - private $passwordColumn; + private $users; + private $usernameColumn; + private $passwordColumn; - public function __construct(DatabaseTable $users, $usernameColumn, $passwordColumn) { - session_start(); - $this->users = $users; - $this->usernameColumn = $usernameColumn; - $this->passwordColumn = $passwordColumn; - } + public function __construct(DatabaseTable $users, $usernameColumn, $passwordColumn) { + session_start(); + $this->users = $users; + $this->usernameColumn = $usernameColumn; + $this->passwordColumn = $passwordColumn; + } - public function login($username, $password) { - $user = $this->users->find($this->usernameColumn, strtolower($username)); + public function login($username, $password) { + $user = $this->users->find($this->usernameColumn, strtolower($username)); - if (!empty($user) && password_verify($_SESSION['password'], $user[$this->passwordColumn])) { - $_SESSION['username'] = $username; - $_SESSION['password'] = $user['password']; - return true; - } - else { - return false; - } - } + if (!empty($user) && password_verify($password, $user[0][$this->passwordColumn])) { + session_regenerate_id(); + $_SESSION['username'] = $username; + $_SESSION['password'] = $user[0][$this->passwordColumn]; + return true; + } + else { + return false; + } + } - public function isLoggedIn() { - $user = $this->users->find($this->usernameColumn, strtolower($_SESSION['username'])); + public function isLoggedIn() { + $user = $this->users->find($this->usernameColumn, strtolower($_SESSION['username'])); - if (!empty($author) && $author[$this->passwordColumn] === $_SESSION['password']) { - return true; - } - else { - return false; - } - } + if (!empty($user) && $user[0][$this->passwordColumn] === $_SESSION['password']) { + return true; + } + else { + return false; + } + } } \ No newline at end of file diff --git a/classes/Ninja/EntryPoint.php b/classes/Ninja/EntryPoint.php index af29260..66836c2 100644 --- a/classes/Ninja/EntryPoint.php +++ b/classes/Ninja/EntryPoint.php @@ -33,12 +33,10 @@ public function run() { $routes = $this->routes->getRoutes(); - - if (isset($routes[$this->route]['login']) && isset($routes[$this->route]['login'])) { - $authentication = $this->routes->getAuthentication(); - if (!$authentication->isLoggedIn()) { - header('location: /login/error'); - } + $authentication = $this->routes->getAuthentication(); + + if (isset($routes[$this->route]['login']) && isset($routes[$this->route]['login']) && !$authentication->isLoggedIn()) { + header('location: /login/error'); } else { $controller = $routes[$this->route][$this->method]['controller']; diff --git a/templates/login.html.php b/templates/login.html.php new file mode 100644 index 0000000..1327016 --- /dev/null +++ b/templates/login.html.php @@ -0,0 +1,14 @@ +' . $error . ''; +} +?> +
+ + + + + + + +
\ No newline at end of file diff --git a/templates/loginsuccess.html.php b/templates/loginsuccess.html.php new file mode 100644 index 0000000..87a11b7 --- /dev/null +++ b/templates/loginsuccess.html.php @@ -0,0 +1,3 @@ +

Login Successful

+ +

You are now logged in.

\ No newline at end of file From 7916168749bbe2d890c7ec3ccb754aae6d347327 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 24 Jun 2017 19:10:53 +0100 Subject: [PATCH 077/124] logout button --- classes/Ijdb/Controllers/Login.php | 5 +++ classes/Ijdb/IjdbRoutes.php | 6 ++++ classes/Ninja/Authentication.php | 5 +++ classes/Ninja/EntryPoint.php | 5 ++- templates/layout.html.php | 51 ++++++++++++++++-------------- templates/logout.html.php | 3 ++ 6 files changed, 51 insertions(+), 24 deletions(-) create mode 100644 templates/logout.html.php diff --git a/classes/Ijdb/Controllers/Login.php b/classes/Ijdb/Controllers/Login.php index 62c803e..ea26148 100644 --- a/classes/Ijdb/Controllers/Login.php +++ b/classes/Ijdb/Controllers/Login.php @@ -33,4 +33,9 @@ public function success() { public function error() { return ['template' => 'loginerror.html.php', 'title' => 'You are not logged in']; } + + public function logout() { + unset($_SESSION); + return ['template' => 'logout.html.php', 'title' => 'You have been logged out']; + } } diff --git a/classes/Ijdb/IjdbRoutes.php b/classes/Ijdb/IjdbRoutes.php index 0bfdb0d..554f5d2 100644 --- a/classes/Ijdb/IjdbRoutes.php +++ b/classes/Ijdb/IjdbRoutes.php @@ -73,6 +73,12 @@ public function getRoutes(): array { 'action' => 'success' ] ], + 'logout' => [ + 'GET' => [ + 'controller' => $loginController, + 'action' => 'logout' + ] + ], 'login' => [ 'GET' => [ 'controller' => $loginController, diff --git a/classes/Ninja/Authentication.php b/classes/Ninja/Authentication.php index 7d9d15a..f94f9ea 100644 --- a/classes/Ninja/Authentication.php +++ b/classes/Ninja/Authentication.php @@ -28,6 +28,11 @@ public function login($username, $password) { } public function isLoggedIn() { + + if (empty($_SESSION['username'])) { + return false; + } + $user = $this->users->find($this->usernameColumn, strtolower($_SESSION['username'])); if (!empty($user) && $user[0][$this->passwordColumn] === $_SESSION['password']) { diff --git a/classes/Ninja/EntryPoint.php b/classes/Ninja/EntryPoint.php index 66836c2..b682ece 100644 --- a/classes/Ninja/EntryPoint.php +++ b/classes/Ninja/EntryPoint.php @@ -52,7 +52,10 @@ public function run() { $output = $this->loadTemplate($page['template']); } - include __DIR__ . '/../../templates/layout.html.php'; + echo $this->loadTemplate('layout.html.php', ['loggedIn' => $authentication->isLoggedIn(), + 'output' => $output, + 'title' => $title + ]); } diff --git a/templates/layout.html.php b/templates/layout.html.php index 6f23acc..ecea358 100644 --- a/templates/layout.html.php +++ b/templates/layout.html.php @@ -1,28 +1,33 @@ - - - - <?=$title?> - - - + + + + <?=$title?> + + + -
- -
+
+ +
-
- © IJDB 2017 -
- +
+ © IJDB 2017 +
+ \ No newline at end of file diff --git a/templates/logout.html.php b/templates/logout.html.php new file mode 100644 index 0000000..acdf7d3 --- /dev/null +++ b/templates/logout.html.php @@ -0,0 +1,3 @@ +

Logged out

+ +

You have been logged out

\ No newline at end of file From 0ff68de1d7317b56993811033e411b80d3655955 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 29 Jun 2017 21:33:28 +0100 Subject: [PATCH 078/124] now tracks users when jokes are posted --- classes/Ijdb/Controllers/Joke.php | 8 ++++++-- classes/Ijdb/IjdbRoutes.php | 2 +- classes/Ninja/Authentication.php | 10 +++++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/classes/Ijdb/Controllers/Joke.php b/classes/Ijdb/Controllers/Joke.php index e2a296c..238626c 100644 --- a/classes/Ijdb/Controllers/Joke.php +++ b/classes/Ijdb/Controllers/Joke.php @@ -1,14 +1,16 @@ jokesTable = $jokesTable; $this->authorsTable = $authorsTable; + $this->authentication = $authentication; } public function list() { @@ -61,9 +63,11 @@ public function delete() { } public function saveEdit() { + $author = $this->authentication->getUser(); + $joke = $_POST['joke']; $joke['jokedate'] = new \DateTime(); - $joke['authorId'] = 1; + $joke['authorId'] = $author['id']; $this->jokesTable->save($joke); diff --git a/classes/Ijdb/IjdbRoutes.php b/classes/Ijdb/IjdbRoutes.php index 554f5d2..1c9c900 100644 --- a/classes/Ijdb/IjdbRoutes.php +++ b/classes/Ijdb/IjdbRoutes.php @@ -15,7 +15,7 @@ public function __construct() { } public function getRoutes(): array { - $jokeController = new \Ijdb\Controllers\Joke($this->jokesTable, $this->authorsTable); + $jokeController = new \Ijdb\Controllers\Joke($this->jokesTable, $this->authorsTable, $this->authentication); $authorController = new \Ijdb\Controllers\Register($this->authorsTable); $loginController = new \Ijdb\Controllers\Login($this->authentication); diff --git a/classes/Ninja/Authentication.php b/classes/Ninja/Authentication.php index f94f9ea..4035ad5 100644 --- a/classes/Ninja/Authentication.php +++ b/classes/Ninja/Authentication.php @@ -28,7 +28,6 @@ public function login($username, $password) { } public function isLoggedIn() { - if (empty($_SESSION['username'])) { return false; } @@ -42,4 +41,13 @@ public function isLoggedIn() { return false; } } + + public function getUser() { + if ($this->isLoggedIn()) { + return $this->users->find($this->usernameColumn, strtolower($_SESSION['username']))[0]; + } + else { + return false; + } + } } \ No newline at end of file From 904c6bc9c2fd657e1fd6f9cd7c8e1adfe2b1c423 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 29 Jun 2017 22:16:16 +0100 Subject: [PATCH 079/124] users can only see edit/delete buttons for their jokes --- classes/Ijdb/Controllers/Joke.php | 17 +++++++++-------- templates/jokes.html.php | 3 +++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/classes/Ijdb/Controllers/Joke.php b/classes/Ijdb/Controllers/Joke.php index 238626c..ddee3e1 100644 --- a/classes/Ijdb/Controllers/Joke.php +++ b/classes/Ijdb/Controllers/Joke.php @@ -25,7 +25,8 @@ public function list() { 'joketext' => $joke['joketext'], 'jokedate' => $joke['jokedate'], 'name' => $author['name'], - 'email' => $author['email'] + 'email' => $author['email'], + 'authorId' => $author['id'] ]; } @@ -35,17 +36,14 @@ public function list() { $totalJokes = $this->jokesTable->total(); - ob_start(); - - include __DIR__ . '/../../templates/'; - - $output = ob_get_clean(); + $author = $this->authentication->getUser(); return ['template' => 'jokes.html.php', 'title' => $title, 'variables' => [ 'totalJokes' => $totalJokes, - 'jokes' => $jokes + 'jokes' => $jokes, + 'userId' => $author['id'] ?? null ] ]; } @@ -75,6 +73,8 @@ public function saveEdit() { } public function edit() { + $author = $this->authentication->getUser(); + if (isset($_GET['id'])) { $joke = $this->jokesTable->findById($_GET['id']); } @@ -84,7 +84,8 @@ public function edit() { return ['template' => 'editjoke.html.php', 'title' => $title, 'variables' => [ - 'joke' => $joke ?? null + 'joke' => $joke ?? null, + 'userId' => $author['id'] ?? null ] ]; } diff --git a/templates/jokes.html.php b/templates/jokes.html.php index 942b636..966b282 100644 --- a/templates/jokes.html.php +++ b/templates/jokes.html.php @@ -15,11 +15,14 @@ echo $date->format('jS F Y'); ?>) + + Edit
+

\ No newline at end of file From ae311265f593d483f18433bfd69579e962ba94fe Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 29 Jun 2017 22:34:19 +0100 Subject: [PATCH 080/124] extra security checks --- classes/Ijdb/Controllers/Joke.php | 19 +++++++++++++++++++ templates/editjoke.html.php | 8 +++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/classes/Ijdb/Controllers/Joke.php b/classes/Ijdb/Controllers/Joke.php index ddee3e1..bf1c7d5 100644 --- a/classes/Ijdb/Controllers/Joke.php +++ b/classes/Ijdb/Controllers/Joke.php @@ -55,6 +55,16 @@ public function home() { } public function delete() { + + $author = $this->authentication->getUser(); + + $joke = $this->jokesTable->findById($_POST['id']); + + if ($joke['authorId'] != $author['id']) { + return; + } + + $this->jokesTable->delete($_POST['id']); header('location: /joke/list'); @@ -63,6 +73,15 @@ public function delete() { public function saveEdit() { $author = $this->authentication->getUser(); + + if (isset($_GET['id'])) { + $joke = $this->jokesTable->findById($_GET['id']); + + if ($joke['authorId'] != $author['id']) { + return; + } + } + $joke = $_POST['joke']; $joke['jokedate'] = new \DateTime(); $joke['authorId'] = $author['id']; diff --git a/templates/editjoke.html.php b/templates/editjoke.html.php index 6731068..53abf36 100644 --- a/templates/editjoke.html.php +++ b/templates/editjoke.html.php @@ -1,6 +1,12 @@ +
-
\ No newline at end of file + + + +

You may only edit jokes that you posted.

+ + \ No newline at end of file From 99e75ca0936284f2fc282990c2d7656cdacc3a0e Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 22 Jul 2017 18:39:51 +0100 Subject: [PATCH 081/124] added author class --- classes/Ijdb/Controllers/Joke.php | 16 +++++++--------- classes/Ijdb/Entity/Author.php | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 classes/Ijdb/Entity/Author.php diff --git a/classes/Ijdb/Controllers/Joke.php b/classes/Ijdb/Controllers/Joke.php index bf1c7d5..9d464eb 100644 --- a/classes/Ijdb/Controllers/Joke.php +++ b/classes/Ijdb/Controllers/Joke.php @@ -73,21 +73,19 @@ public function delete() { public function saveEdit() { $author = $this->authentication->getUser(); + $authorObject = new \Ijdb\Entity\Author($this->jokesTable); - if (isset($_GET['id'])) { - $joke = $this->jokesTable->findById($_GET['id']); + $authorObject->id = $author['id']; + $authorObject->name = $author['name']; + $authorObject->email = $author['email']; + $authorObject->password = $author['password']; - if ($joke['authorId'] != $author['id']) { - return; - } - } $joke = $_POST['joke']; $joke['jokedate'] = new \DateTime(); - $joke['authorId'] = $author['id']; - $this->jokesTable->save($joke); - + $authorObject->addJoke($joke); + header('location: /joke/list'); } diff --git a/classes/Ijdb/Entity/Author.php b/classes/Ijdb/Entity/Author.php new file mode 100644 index 0000000..183a892 --- /dev/null +++ b/classes/Ijdb/Entity/Author.php @@ -0,0 +1,25 @@ +jokesTable = $jokeTable; + } + + public function getJokes() { + return $this->jokesTable->find('authorId', $this->id); + } + + public function addJoke($joke) { + + $joke['authorId'] = $this->id; + + $this->jokesTable->save($joke); + } +} \ No newline at end of file From a4ab5ea2c683bb655182c5114caefd8eb2f47bd7 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sat, 22 Jul 2017 18:57:26 +0100 Subject: [PATCH 082/124] fix bug: cannot add new jokes --- templates/editjoke.html.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/editjoke.html.php b/templates/editjoke.html.php index 53abf36..df6f95d 100644 --- a/templates/editjoke.html.php +++ b/templates/editjoke.html.php @@ -1,4 +1,4 @@ - +
From 36ff7bb4122d4c851cc438442c135c57bc9adefc Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sun, 23 Jul 2017 13:30:47 +0100 Subject: [PATCH 083/124] joke class --- classes/Ijdb/Controllers/Joke.php | 12 ++---------- classes/Ijdb/IjdbRoutes.php | 2 +- classes/Ninja/Authentication.php | 6 +++--- classes/Ninja/DatabaseTable.php | 12 ++++++++---- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/classes/Ijdb/Controllers/Joke.php b/classes/Ijdb/Controllers/Joke.php index 9d464eb..4bd1843 100644 --- a/classes/Ijdb/Controllers/Joke.php +++ b/classes/Ijdb/Controllers/Joke.php @@ -73,18 +73,10 @@ public function delete() { public function saveEdit() { $author = $this->authentication->getUser(); - $authorObject = new \Ijdb\Entity\Author($this->jokesTable); - - $authorObject->id = $author['id']; - $authorObject->name = $author['name']; - $authorObject->email = $author['email']; - $authorObject->password = $author['password']; - - $joke = $_POST['joke']; $joke['jokedate'] = new \DateTime(); - $authorObject->addJoke($joke); + $author->addJoke($joke); header('location: /joke/list'); } @@ -102,7 +94,7 @@ public function edit() { 'title' => $title, 'variables' => [ 'joke' => $joke ?? null, - 'userId' => $author['id'] ?? null + 'userId' => $author->id ?? null ] ]; } diff --git a/classes/Ijdb/IjdbRoutes.php b/classes/Ijdb/IjdbRoutes.php index 1c9c900..74110b6 100644 --- a/classes/Ijdb/IjdbRoutes.php +++ b/classes/Ijdb/IjdbRoutes.php @@ -10,7 +10,7 @@ public function __construct() { include __DIR__ . '/../../includes/DatabaseConnection.php'; $this->jokesTable = new \Ninja\DatabaseTable($pdo, 'joke', 'id'); - $this->authorsTable = new \Ninja\DatabaseTable($pdo, 'author', 'id'); + $this->authorsTable = new \Ninja\DatabaseTable($pdo, 'author', 'id', '\Ijdb\Entity\Author', [$this->jokesTable]); $this->authentication = new \Ninja\Authentication($this->authorsTable, 'email', 'password'); } diff --git a/classes/Ninja/Authentication.php b/classes/Ninja/Authentication.php index 4035ad5..5ee838c 100644 --- a/classes/Ninja/Authentication.php +++ b/classes/Ninja/Authentication.php @@ -16,10 +16,10 @@ public function __construct(DatabaseTable $users, $usernameColumn, $passwordColu public function login($username, $password) { $user = $this->users->find($this->usernameColumn, strtolower($username)); - if (!empty($user) && password_verify($password, $user[0][$this->passwordColumn])) { + if (!empty($user) && password_verify($password, $user[0]->{$this->passwordColumn})) { session_regenerate_id(); $_SESSION['username'] = $username; - $_SESSION['password'] = $user[0][$this->passwordColumn]; + $_SESSION['password'] = $user[0]->{$this->passwordColumn}; return true; } else { @@ -34,7 +34,7 @@ public function isLoggedIn() { $user = $this->users->find($this->usernameColumn, strtolower($_SESSION['username'])); - if (!empty($user) && $user[0][$this->passwordColumn] === $_SESSION['password']) { + if (!empty($user) && $user[0]->{$this->passwordColumn} === $_SESSION['password']) { return true; } else { diff --git a/classes/Ninja/DatabaseTable.php b/classes/Ninja/DatabaseTable.php index 7282778..d7c702c 100644 --- a/classes/Ninja/DatabaseTable.php +++ b/classes/Ninja/DatabaseTable.php @@ -5,11 +5,15 @@ class DatabaseTable { private $pdo; private $table; private $primaryKey; + private $className; + private $constructorArgs; - public function __construct(\PDO $pdo, string $table, string $primaryKey) { + public function __construct(\PDO $pdo, string $table, string $primaryKey, string $className = '\stdClass', array $constructorArgs = []) { $this->pdo = $pdo; $this->table = $table; $this->primaryKey = $primaryKey; + $this->className = $className; + $this->constructorArgs = $constructorArgs; } private function query($sql, $parameters = []) { @@ -33,7 +37,7 @@ public function findById($value) { $query = $this->query($query, $parameters); - return $query->fetch(); + return $query->fetchObject($this->className, $this->constructorArgs); } public function find($column, $value) { @@ -45,7 +49,7 @@ public function find($column, $value) { $query = $this->query($query, $parameters); - return $query->fetchAll(); + return $query->fetchAll(\PDO::FETCH_CLASS, $this->className, $this->constructorArgs); } private function insert($fields) { @@ -104,7 +108,7 @@ public function delete($id ) { public function findAll() { $result = $this->query('SELECT * FROM ' . $this->table); - return $result->fetchAll(); + return $result->fetchAll(\PDO::FETCH_CLASS, $this->className, $this->constructorArgs); } private function processDates($fields) { From c456455605e2c6c81bd62965e8b5ce89e71c25e0 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sun, 23 Jul 2017 14:01:43 +0100 Subject: [PATCH 084/124] uses joke objects --- classes/Ijdb/Controllers/Joke.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/classes/Ijdb/Controllers/Joke.php b/classes/Ijdb/Controllers/Joke.php index 4bd1843..d113290 100644 --- a/classes/Ijdb/Controllers/Joke.php +++ b/classes/Ijdb/Controllers/Joke.php @@ -18,15 +18,15 @@ public function list() { $jokes = []; foreach ($result as $joke) { - $author = $this->authorsTable->findById($joke['authorId']); + $author = $this->authorsTable->findById($joke->authorId); $jokes[] = [ - 'id' => $joke['id'], - 'joketext' => $joke['joketext'], - 'jokedate' => $joke['jokedate'], - 'name' => $author['name'], - 'email' => $author['email'], - 'authorId' => $author['id'] + 'id' => $joke->id, + 'joketext' => $joke->joketext, + 'jokedate' => $joke->jokedate, + 'name' => $author->name, + 'email' => $author->email, + 'authorId' => $author->id ]; } @@ -43,7 +43,7 @@ public function list() { 'variables' => [ 'totalJokes' => $totalJokes, 'jokes' => $jokes, - 'userId' => $author['id'] ?? null + 'userId' => $author->id ?? null ] ]; } From 5132aacbf0c4c36dace86f8008f2a28626ad189d Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sun, 23 Jul 2017 14:29:46 +0100 Subject: [PATCH 085/124] now has Joke object --- classes/Ijdb/Controllers/Joke.php | 18 +----------------- classes/Ijdb/Entity/Joke.php | 18 ++++++++++++++++++ classes/Ijdb/IjdbRoutes.php | 4 ++-- templates/jokes.html.php | 14 +++++++------- 4 files changed, 28 insertions(+), 26 deletions(-) create mode 100644 classes/Ijdb/Entity/Joke.php diff --git a/classes/Ijdb/Controllers/Joke.php b/classes/Ijdb/Controllers/Joke.php index d113290..d2470ca 100644 --- a/classes/Ijdb/Controllers/Joke.php +++ b/classes/Ijdb/Controllers/Joke.php @@ -14,23 +14,7 @@ public function __construct(DatabaseTable $jokesTable, DatabaseTable $authorsTab } public function list() { - $result = $this->jokesTable->findAll(); - - $jokes = []; - foreach ($result as $joke) { - $author = $this->authorsTable->findById($joke->authorId); - - $jokes[] = [ - 'id' => $joke->id, - 'joketext' => $joke->joketext, - 'jokedate' => $joke->jokedate, - 'name' => $author->name, - 'email' => $author->email, - 'authorId' => $author->id - ]; - - } - + $jokes = $this->jokesTable->findAll(); $title = 'Joke list'; diff --git a/classes/Ijdb/Entity/Joke.php b/classes/Ijdb/Entity/Joke.php new file mode 100644 index 0000000..b3b6527 --- /dev/null +++ b/classes/Ijdb/Entity/Joke.php @@ -0,0 +1,18 @@ +authorsTable = $authorsTable; + } + + public function getAuthor() { + return $this->authorsTable->findById($this->authorId); + } +} \ No newline at end of file diff --git a/classes/Ijdb/IjdbRoutes.php b/classes/Ijdb/IjdbRoutes.php index 74110b6..a926b42 100644 --- a/classes/Ijdb/IjdbRoutes.php +++ b/classes/Ijdb/IjdbRoutes.php @@ -9,8 +9,8 @@ class IjdbRoutes implements \Ninja\Routes { public function __construct() { include __DIR__ . '/../../includes/DatabaseConnection.php'; - $this->jokesTable = new \Ninja\DatabaseTable($pdo, 'joke', 'id'); - $this->authorsTable = new \Ninja\DatabaseTable($pdo, 'author', 'id', '\Ijdb\Entity\Author', [$this->jokesTable]); + $this->jokesTable = new \Ninja\DatabaseTable($pdo, 'joke', 'id', '\Ijdb\Entity\Joke', [&$this->authorsTable]); + $this->authorsTable = new \Ninja\DatabaseTable($pdo, 'author', 'id', '\Ijdb\Entity\Author', [&$this->jokesTable]); $this->authentication = new \Ninja\Authentication($this->authorsTable, 'email', 'password'); } diff --git a/templates/jokes.html.php b/templates/jokes.html.php index 966b282..1bea531 100644 --- a/templates/jokes.html.php +++ b/templates/jokes.html.php @@ -4,22 +4,22 @@

- + joketext, ENT_QUOTES, 'UTF-8')?> - (by getAuthor()->email, ENT_QUOTES, 'UTF-8'); ?>"> - getAuthor()->name, ENT_QUOTES, 'UTF-8'); ?> on jokedate); echo $date->format('jS F Y'); ?>) - - Edit +authorId) { ?> + Edit - +

From d7ce322c440a6c38b737211acee3ba6f3af77e03 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sun, 23 Jul 2017 16:58:46 +0100 Subject: [PATCH 086/124] fixed edit joke --- templates/editjoke.html.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/templates/editjoke.html.php b/templates/editjoke.html.php index df6f95d..b2a24a6 100644 --- a/templates/editjoke.html.php +++ b/templates/editjoke.html.php @@ -1,8 +1,8 @@ - +id) || $userId == $joke->authorId) { ?>
- + - +
From 99092a1bd540b27aa15b0d694755f6ff9a5e5086 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sun, 23 Jul 2017 17:29:38 +0100 Subject: [PATCH 087/124] implement caching --- classes/Ijdb/Entity/Joke.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/classes/Ijdb/Entity/Joke.php b/classes/Ijdb/Entity/Joke.php index b3b6527..67877ca 100644 --- a/classes/Ijdb/Entity/Joke.php +++ b/classes/Ijdb/Entity/Joke.php @@ -7,12 +7,17 @@ class Joke { public $jokedate; public $joketext; private $authorsTable; + private $author; public function __construct(\Ninja\DatabaseTable $authorsTable) { $this->authorsTable = $authorsTable; } public function getAuthor() { - return $this->authorsTable->findById($this->authorId); + if (empty($this->author)) { + $this->author = $this->authorsTable->findById($this->authorId); + } + + return $this->author; } } \ No newline at end of file From b4eaceab1e1d5ccb2413682dbae7f9e797406817 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Tue, 25 Jul 2017 14:53:39 +0100 Subject: [PATCH 088/124] edit category + database --- database.sql | 33 ++++++++++++++++++++++++++++----- templates/editcategory.html.php | 6 ++++++ 2 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 templates/editcategory.html.php diff --git a/database.sql b/database.sql index d273fc3..ca00108 100644 --- a/database.sql +++ b/database.sql @@ -1,8 +1,8 @@ --- MySQL dump 10.16 Distrib 10.1.21-MariaDB, for Linux (x86_64) +-- MySQL dump 10.13 Distrib 5.7.18, for Linux (x86_64) -- --- Host: 127.0.0.1 Database: 127.0.0.1 +-- Host: 192.168.10.10 Database: ijdb_sample -- ------------------------------------------------------ --- Server version 5.7.16-0ubuntu0.16.04.1 +-- Server version 5.7.16-0ubuntu0.16.04.1 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; @@ -37,10 +37,33 @@ CREATE TABLE `author` ( LOCK TABLES `author` WRITE; /*!40000 ALTER TABLE `author` DISABLE KEYS */; -INSERT INTO `author` VALUES (1,'Kevin Yank','thatguy@kevinyank.com', ''),(2,'Tom Butler','tom@r.je', ''); +INSERT INTO `author` VALUES (1,'Kevin Yank','thatguy@kevinyank.com',''),(2,'Tom Butler','tom@r.je',''); /*!40000 ALTER TABLE `author` ENABLE KEYS */; UNLOCK TABLES; +-- +-- Table structure for table `category` +-- + +DROP TABLE IF EXISTS `category`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `category` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `category` +-- + +LOCK TABLES `category` WRITE; +/*!40000 ALTER TABLE `category` DISABLE KEYS */; +/*!40000 ALTER TABLE `category` ENABLE KEYS */; +UNLOCK TABLES; + -- -- Table structure for table `joke` -- @@ -76,4 +99,4 @@ UNLOCK TABLES; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2017-02-13 18:17:21 +-- Dump completed on 2017-07-25 14:52:05 diff --git a/templates/editcategory.html.php b/templates/editcategory.html.php new file mode 100644 index 0000000..b88cb3f --- /dev/null +++ b/templates/editcategory.html.php @@ -0,0 +1,6 @@ +
+ + + + +
From 7c2ccd3dd3ff53636604a9d152c535767cd1ac7d Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Tue, 25 Jul 2017 14:56:30 +0100 Subject: [PATCH 089/124] category class --- classes/Ijdb/Controllers/Category.php | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 classes/Ijdb/Controllers/Category.php diff --git a/classes/Ijdb/Controllers/Category.php b/classes/Ijdb/Controllers/Category.php new file mode 100644 index 0000000..2f27ac8 --- /dev/null +++ b/classes/Ijdb/Controllers/Category.php @@ -0,0 +1,10 @@ +categoriesTable = $categoriesTable; + } +} \ No newline at end of file From 7722b24e0036192388d56551568363fbd8524dad Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Tue, 25 Jul 2017 15:00:54 +0100 Subject: [PATCH 090/124] allows adding/editing categories --- classes/Ijdb/Controllers/Category.php | 16 ++++++++++++++++ classes/Ijdb/IjdbRoutes.php | 15 ++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/classes/Ijdb/Controllers/Category.php b/classes/Ijdb/Controllers/Category.php index 2f27ac8..e9066d0 100644 --- a/classes/Ijdb/Controllers/Category.php +++ b/classes/Ijdb/Controllers/Category.php @@ -7,4 +7,20 @@ class Category { 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 + ] + ]; + } } \ No newline at end of file diff --git a/classes/Ijdb/IjdbRoutes.php b/classes/Ijdb/IjdbRoutes.php index a926b42..e93941c 100644 --- a/classes/Ijdb/IjdbRoutes.php +++ b/classes/Ijdb/IjdbRoutes.php @@ -5,12 +5,14 @@ class IjdbRoutes implements \Ninja\Routes { private $authorsTable; private $jokesTable; private $authentication; + private $categoriesTable; public function __construct() { include __DIR__ . '/../../includes/DatabaseConnection.php'; $this->jokesTable = new \Ninja\DatabaseTable($pdo, 'joke', 'id', '\Ijdb\Entity\Joke', [&$this->authorsTable]); $this->authorsTable = new \Ninja\DatabaseTable($pdo, 'author', 'id', '\Ijdb\Entity\Author', [&$this->jokesTable]); + $this->categoriesTable = new \Ninja\DatabaseTable($pdo, 'category', 'id'); $this->authentication = new \Ninja\Authentication($this->authorsTable, 'email', 'password'); } @@ -18,6 +20,7 @@ public function getRoutes(): array { $jokeController = new \Ijdb\Controllers\Joke($this->jokesTable, $this->authorsTable, $this->authentication); $authorController = new \Ijdb\Controllers\Register($this->authorsTable); $loginController = new \Ijdb\Controllers\Login($this->authentication); + $categoryController = new \Ijdb\Controllers\Category($this->categoriesTable); $routes = [ 'author/register' => [ @@ -46,7 +49,6 @@ public function getRoutes(): array { 'action' => 'edit' ], 'login' => true - ], 'joke/delete' => [ 'POST' => [ @@ -89,6 +91,17 @@ public function getRoutes(): array { 'action' => 'processLogin' ] ], + 'category/edit' => [ + 'POST' => [ + 'controller' => $categoryController, + 'action' => 'saveEdit' + ], + 'GET' => [ + 'controller' => $categoryController, + 'action' => 'edit' + ], + 'login' => true + ], '' => [ 'GET' => [ 'controller' => $jokeController, From 665d2521845c96f27bc9c672dcc3c55006ad54fc Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Tue, 25 Jul 2017 15:02:10 +0100 Subject: [PATCH 091/124] saveEdit function --- classes/Ijdb/Controllers/Category.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/classes/Ijdb/Controllers/Category.php b/classes/Ijdb/Controllers/Category.php index e9066d0..1ab2e9d 100644 --- a/classes/Ijdb/Controllers/Category.php +++ b/classes/Ijdb/Controllers/Category.php @@ -23,4 +23,12 @@ public function edit() { ] ]; } + + public function saveEdit() { + $category = $_POST['category']; + + $this->categoriesTable->save($category); + + header('location: /category/list'); + } } \ No newline at end of file From 9d1e94dcaf87093b6438c18f58bc4f51f937cc4f Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Tue, 25 Jul 2017 15:10:58 +0100 Subject: [PATCH 092/124] category list page --- classes/Ijdb/Controllers/Category.php | 13 +++++++++++++ classes/Ijdb/IjdbRoutes.php | 7 +++++++ templates/categories.html.php | 17 +++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 templates/categories.html.php diff --git a/classes/Ijdb/Controllers/Category.php b/classes/Ijdb/Controllers/Category.php index 1ab2e9d..2e96da1 100644 --- a/classes/Ijdb/Controllers/Category.php +++ b/classes/Ijdb/Controllers/Category.php @@ -31,4 +31,17 @@ public function saveEdit() { header('location: /category/list'); } + + public function list() { + $categories = $this->categoriesTable->findAll(); + + $title = 'Joke Categories'; + + return ['template' => 'categories.html.php', + 'title' => $title, + 'variables' => [ + 'categories' => $categories + ] + ]; + } } \ No newline at end of file diff --git a/classes/Ijdb/IjdbRoutes.php b/classes/Ijdb/IjdbRoutes.php index e93941c..f5ba6be 100644 --- a/classes/Ijdb/IjdbRoutes.php +++ b/classes/Ijdb/IjdbRoutes.php @@ -102,6 +102,13 @@ public function getRoutes(): array { ], 'login' => true ], + 'category/list' => [ + 'GET' => [ + 'controller' => $categoryController, + 'action' => 'list' + ], + 'login' => true + ], '' => [ 'GET' => [ 'controller' => $jokeController, diff --git a/templates/categories.html.php b/templates/categories.html.php new file mode 100644 index 0000000..d2be140 --- /dev/null +++ b/templates/categories.html.php @@ -0,0 +1,17 @@ + +

Categories

+ + +
+

+ name, ENT_QUOTES, 'UTF-8')?> + + Edit +

+ + +
+

+
+ + From 22d6da9531bee2d95af9f604ed4f29b4cffd128b Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Tue, 25 Jul 2017 15:24:44 +0100 Subject: [PATCH 093/124] delete action --- classes/Ijdb/Controllers/Category.php | 6 ++++++ classes/Ijdb/IjdbRoutes.php | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/classes/Ijdb/Controllers/Category.php b/classes/Ijdb/Controllers/Category.php index 2e96da1..072d11e 100644 --- a/classes/Ijdb/Controllers/Category.php +++ b/classes/Ijdb/Controllers/Category.php @@ -44,4 +44,10 @@ public function list() { ] ]; } + + public function delete() { + $this->categoriesTable->delete($_POST['id']); + + header('location: /category/list'); + } } \ No newline at end of file diff --git a/classes/Ijdb/IjdbRoutes.php b/classes/Ijdb/IjdbRoutes.php index f5ba6be..6d39251 100644 --- a/classes/Ijdb/IjdbRoutes.php +++ b/classes/Ijdb/IjdbRoutes.php @@ -102,6 +102,13 @@ public function getRoutes(): array { ], 'login' => true ], + 'category/delete' => [ + 'POST' => [ + 'controller' => $categoryController, + 'action' => 'delete' + ], + 'login' => true + ], 'category/list' => [ 'GET' => [ 'controller' => $categoryController, From 980d0fa4b89c40bfa171ce78e1d64cce7ff764a0 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Tue, 25 Jul 2017 15:54:26 +0100 Subject: [PATCH 094/124] add category link --- templates/categories.html.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/templates/categories.html.php b/templates/categories.html.php index d2be140..338dfd2 100644 --- a/templates/categories.html.php +++ b/templates/categories.html.php @@ -1,6 +1,9 @@

Categories

+Add a new category + +

From 063c6955e139278f7a06d2191c668b10ffa0b851 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Tue, 25 Jul 2017 16:52:36 +0100 Subject: [PATCH 095/124] add joke allows selecting category --- classes/Ijdb/Controllers/Joke.php | 9 +++++++-- classes/Ijdb/IjdbRoutes.php | 2 +- public/jokes.css | 6 +++++- templates/editjoke.html.php | 8 ++++++++ 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/classes/Ijdb/Controllers/Joke.php b/classes/Ijdb/Controllers/Joke.php index d2470ca..7647475 100644 --- a/classes/Ijdb/Controllers/Joke.php +++ b/classes/Ijdb/Controllers/Joke.php @@ -6,10 +6,13 @@ class Joke { private $authorsTable; private $jokesTable; + private $categoriesTable; + private $authentication; - public function __construct(DatabaseTable $jokesTable, DatabaseTable $authorsTable, Authentication $authentication) { + public function __construct(DatabaseTable $jokesTable, DatabaseTable $authorsTable, DatabaseTable $categoriesTable, Authentication $authentication) { $this->jokesTable = $jokesTable; $this->authorsTable = $authorsTable; + $this->categoriesTable = $categoriesTable; $this->authentication = $authentication; } @@ -67,6 +70,7 @@ public function saveEdit() { public function edit() { $author = $this->authentication->getUser(); + $categories = $this->categoriesTable->findAll(); if (isset($_GET['id'])) { $joke = $this->jokesTable->findById($_GET['id']); @@ -78,7 +82,8 @@ public function edit() { 'title' => $title, 'variables' => [ 'joke' => $joke ?? null, - 'userId' => $author->id ?? null + 'userId' => $author->id ?? null, + 'categories' => $categories ] ]; } diff --git a/classes/Ijdb/IjdbRoutes.php b/classes/Ijdb/IjdbRoutes.php index 6d39251..80b8087 100644 --- a/classes/Ijdb/IjdbRoutes.php +++ b/classes/Ijdb/IjdbRoutes.php @@ -17,7 +17,7 @@ public function __construct() { } public function getRoutes(): array { - $jokeController = new \Ijdb\Controllers\Joke($this->jokesTable, $this->authorsTable, $this->authentication); + $jokeController = new \Ijdb\Controllers\Joke($this->jokesTable, $this->authorsTable, $this->categoriesTable, $this->authentication); $authorController = new \Ijdb\Controllers\Register($this->authorsTable); $loginController = new \Ijdb\Controllers\Login($this->authentication); $categoryController = new \Ijdb\Controllers\Category($this->categoriesTable); diff --git a/public/jokes.css b/public/jokes.css index 1e056ad..4a5743a 100644 --- a/public/jokes.css +++ b/public/jokes.css @@ -27,4 +27,8 @@ blockquote p {display: table-cell; width: 90%; vertical-align: top;} blockquote form { display: table-cell; width: 10%;} .errors {padding: 1em; border: 1px solid red; background-color: lightyellow; color: red; margin-bottom: 1em; overflow: auto;} -.errors ul {margin-left: 1em;} \ No newline at end of file +.errors ul {margin-left: 1em;} + +form p {clear: both;} +input[type="checkbox"] {float: left; clear: left; width: auto; margin-right: 10px;} +input[type="checkbox"] + label {clear: right;} \ No newline at end of file diff --git a/templates/editjoke.html.php b/templates/editjoke.html.php index b2a24a6..7a9a1fe 100644 --- a/templates/editjoke.html.php +++ b/templates/editjoke.html.php @@ -3,6 +3,14 @@ + +

Select categories for this joke:

+ + + + + + From 2496ec5426733be3a70a0fd45ef956a9020ff102 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sun, 30 Jul 2017 17:15:45 +0100 Subject: [PATCH 096/124] jokes are assigned to categories --- classes/Ijdb/Controllers/Joke.php | 6 +++++- classes/Ijdb/Entity/Author.php | 3 +-- classes/Ijdb/Entity/Joke.php | 10 ++++++++- classes/Ijdb/IjdbRoutes.php | 6 ++++-- classes/Ninja/DatabaseTable.php | 20 ++++++++++++++--- database.sql | 36 +++++++++++++++++++++++++------ 6 files changed, 66 insertions(+), 15 deletions(-) diff --git a/classes/Ijdb/Controllers/Joke.php b/classes/Ijdb/Controllers/Joke.php index 7647475..842c80c 100644 --- a/classes/Ijdb/Controllers/Joke.php +++ b/classes/Ijdb/Controllers/Joke.php @@ -63,7 +63,11 @@ public function saveEdit() { $joke = $_POST['joke']; $joke['jokedate'] = new \DateTime(); - $author->addJoke($joke); + $jokeEntity = $author->addJoke($joke); + + foreach ($_POST['category'] as $categoryId) { + $jokeEntity->addCategory($categoryId); + } header('location: /joke/list'); } diff --git a/classes/Ijdb/Entity/Author.php b/classes/Ijdb/Entity/Author.php index 183a892..4d4e307 100644 --- a/classes/Ijdb/Entity/Author.php +++ b/classes/Ijdb/Entity/Author.php @@ -17,9 +17,8 @@ public function getJokes() { } public function addJoke($joke) { - $joke['authorId'] = $this->id; - $this->jokesTable->save($joke); + return $this->jokesTable->save($joke); } } \ No newline at end of file diff --git a/classes/Ijdb/Entity/Joke.php b/classes/Ijdb/Entity/Joke.php index 67877ca..daf475f 100644 --- a/classes/Ijdb/Entity/Joke.php +++ b/classes/Ijdb/Entity/Joke.php @@ -8,9 +8,11 @@ class Joke { public $joketext; private $authorsTable; private $author; + private $jokeCategoriesTable; - public function __construct(\Ninja\DatabaseTable $authorsTable) { + public function __construct(\Ninja\DatabaseTable $authorsTable, \Ninja\DatabaseTable $jokeCategoriesTable) { $this->authorsTable = $authorsTable; + $this->jokeCategoriesTable = $jokeCategoriesTable; } public function getAuthor() { @@ -20,4 +22,10 @@ public function getAuthor() { return $this->author; } + + public function addCategory($categoryId) { + $jokeCat = ['jokeId' => $this->id, 'categoryId' => $categoryId]; + + $this->jokeCategoriesTable->save($jokeCat); + } } \ No newline at end of file diff --git a/classes/Ijdb/IjdbRoutes.php b/classes/Ijdb/IjdbRoutes.php index 80b8087..778db18 100644 --- a/classes/Ijdb/IjdbRoutes.php +++ b/classes/Ijdb/IjdbRoutes.php @@ -4,15 +4,17 @@ class IjdbRoutes implements \Ninja\Routes { private $authorsTable; private $jokesTable; - private $authentication; private $categoriesTable; + private $jokeCategoriesTable; + private $authentication; public function __construct() { include __DIR__ . '/../../includes/DatabaseConnection.php'; - $this->jokesTable = new \Ninja\DatabaseTable($pdo, 'joke', 'id', '\Ijdb\Entity\Joke', [&$this->authorsTable]); + $this->jokesTable = new \Ninja\DatabaseTable($pdo, 'joke', 'id', '\Ijdb\Entity\Joke', [&$this->authorsTable, &$this->jokeCategoriesTable]); $this->authorsTable = new \Ninja\DatabaseTable($pdo, 'author', 'id', '\Ijdb\Entity\Author', [&$this->jokesTable]); $this->categoriesTable = new \Ninja\DatabaseTable($pdo, 'category', 'id'); + $this->jokeCategoriesTable = new \Ninja\DatabaseTable($pdo, 'joke_category', 'categoryId'); $this->authentication = new \Ninja\Authentication($this->authorsTable, 'email', 'password'); } diff --git a/classes/Ninja/DatabaseTable.php b/classes/Ninja/DatabaseTable.php index d7c702c..13b0abd 100644 --- a/classes/Ninja/DatabaseTable.php +++ b/classes/Ninja/DatabaseTable.php @@ -75,6 +75,8 @@ private function insert($fields) { $fields = $this->processDates($fields); $this->query($query, $fields); + + return $this->pdo->lastInsertId(); } @@ -90,7 +92,7 @@ private function update($fields) { $query .= ' WHERE `' . $this->primaryKey . '` = :primaryKey'; //Set the :primaryKey variable - $fields['primaryKey'] = $fields['id']; + $fields['primaryKey'] = $fields[$this->primaryKey]; $fields = $this->processDates($fields); @@ -123,14 +125,26 @@ private function processDates($fields) { public function save($record) { + $entity = new $this->className(...$this->constructorArgs); + try { if ($record[$this->primaryKey] == '') { $record[$this->primaryKey] = null; } - $this->insert($record); + $insertId = $this->insert($record); + + $entity->{$this->primaryKey} = $insertId; } catch (\PDOException $e) { - $this->update( $record); + $this->update($record); } + + foreach ($record as $key => $value) { + if (!empty($value)) { + $entity->$key = $value; + } + } + + return $entity; } } \ No newline at end of file diff --git a/database.sql b/database.sql index ca00108..ce4eead 100644 --- a/database.sql +++ b/database.sql @@ -1,6 +1,6 @@ --- MySQL dump 10.13 Distrib 5.7.18, for Linux (x86_64) +-- MySQL dump 10.16 Distrib 10.1.25-MariaDB, for Linux (x86_64) -- --- Host: 192.168.10.10 Database: ijdb_sample +-- Host: 127.0.0.1 Database: ijdb_sample -- ------------------------------------------------------ -- Server version 5.7.16-0ubuntu0.16.04.1 @@ -28,7 +28,7 @@ CREATE TABLE `author` ( `email` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; +) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -52,7 +52,7 @@ CREATE TABLE `category` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=latin1; +) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -61,6 +61,7 @@ CREATE TABLE `category` ( LOCK TABLES `category` WRITE; /*!40000 ALTER TABLE `category` DISABLE KEYS */; +INSERT INTO `category` VALUES (1,'Programming Jokes'),(2,'One Liners'); /*!40000 ALTER TABLE `category` ENABLE KEYS */; UNLOCK TABLES; @@ -77,7 +78,7 @@ CREATE TABLE `joke` ( `jokedate` date NOT NULL, `authorId` int(11) DEFAULT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; +) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -89,6 +90,29 @@ LOCK TABLES `joke` WRITE; INSERT INTO `joke` VALUES (1,'How many programmers does it take to screw in a lightbulb? None, it\'s a hardware problem.','2017-04-01',1),(2,'Why did the programmer quit his job? He didn\'t get arrays','2017-04-01',1),(3,'Why was the empty array stuck outside? It didn\'t have any keys','2017-04-01',2); /*!40000 ALTER TABLE `joke` ENABLE KEYS */; UNLOCK TABLES; + +-- +-- Table structure for table `joke_category` +-- + +DROP TABLE IF EXISTS `joke_category`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `joke_category` ( + `jokeId` int(11) NOT NULL, + `categoryId` int(11) NOT NULL, + PRIMARY KEY (`jokeId`,`categoryId`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `joke_category` +-- + +LOCK TABLES `joke_category` WRITE; +/*!40000 ALTER TABLE `joke_category` DISABLE KEYS */; +/*!40000 ALTER TABLE `joke_category` ENABLE KEYS */; +UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; @@ -99,4 +123,4 @@ UNLOCK TABLES; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2017-07-25 14:52:05 +-- Dump completed on 2017-07-30 17:02:03 From 01fd776931667a8b02067143e031b1a16671e0ee Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Sun, 30 Jul 2017 17:26:02 +0100 Subject: [PATCH 097/124] uses objects for delete method --- classes/Ijdb/Controllers/Joke.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/Ijdb/Controllers/Joke.php b/classes/Ijdb/Controllers/Joke.php index 842c80c..ebea3bb 100644 --- a/classes/Ijdb/Controllers/Joke.php +++ b/classes/Ijdb/Controllers/Joke.php @@ -47,7 +47,7 @@ public function delete() { $joke = $this->jokesTable->findById($_POST['id']); - if ($joke['authorId'] != $author['id']) { + if ($joke->authorId != $author->id) { return; } From e844a6722360083707bdc1f24d329dd45cef172d Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Wed, 9 Aug 2017 18:03:44 +0100 Subject: [PATCH 098/124] category list on jokes page --- classes/Ijdb/Controllers/Joke.php | 3 ++- database.sql | 9 +++++---- public/jokes.css | 8 +++++++- templates/jokes.html.php | 14 +++++++++++++- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/classes/Ijdb/Controllers/Joke.php b/classes/Ijdb/Controllers/Joke.php index ebea3bb..5ea125b 100644 --- a/classes/Ijdb/Controllers/Joke.php +++ b/classes/Ijdb/Controllers/Joke.php @@ -30,7 +30,8 @@ public function list() { 'variables' => [ 'totalJokes' => $totalJokes, 'jokes' => $jokes, - 'userId' => $author->id ?? null + 'userId' => $author->id ?? null, + 'categories' => $this->categoriesTable->findAll() ] ]; } diff --git a/database.sql b/database.sql index ce4eead..16b4903 100644 --- a/database.sql +++ b/database.sql @@ -28,7 +28,7 @@ CREATE TABLE `author` ( `email` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; +) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -78,7 +78,7 @@ CREATE TABLE `joke` ( `jokedate` date NOT NULL, `authorId` int(11) DEFAULT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8; +) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -87,7 +87,7 @@ CREATE TABLE `joke` ( LOCK TABLES `joke` WRITE; /*!40000 ALTER TABLE `joke` DISABLE KEYS */; -INSERT INTO `joke` VALUES (1,'How many programmers does it take to screw in a lightbulb? None, it\'s a hardware problem.','2017-04-01',1),(2,'Why did the programmer quit his job? He didn\'t get arrays','2017-04-01',1),(3,'Why was the empty array stuck outside? It didn\'t have any keys','2017-04-01',2); +INSERT INTO `joke` VALUES (1,'How many programmers does it take to screw in a lightbulb? None, it\'s a hardware problem.','2017-04-01',1),(2,'Why did the programmer quit his job? He didn\'t get arrays','2017-04-01',1),(3,'Why was the empty array stuck outside? It didn\'t have any keys','2017-04-01',2),(20,'How do functions break up? They stop calling each other','2017-08-09',2),(21,'How do you tell HTML from HTML5? Try it out in Internet Explorer. Did it work? No? It\'s HTML5','2017-08-09',2),(22,'You don\'t need any training to be a litter picker, you pick it up as you go','2017-08-09',2); /*!40000 ALTER TABLE `joke` ENABLE KEYS */; UNLOCK TABLES; @@ -111,6 +111,7 @@ CREATE TABLE `joke_category` ( LOCK TABLES `joke_category` WRITE; /*!40000 ALTER TABLE `joke_category` DISABLE KEYS */; +INSERT INTO `joke_category` VALUES (17,1),(17,2),(18,2),(19,1),(19,2),(20,1),(20,2),(21,1),(22,2); /*!40000 ALTER TABLE `joke_category` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; @@ -123,4 +124,4 @@ UNLOCK TABLES; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2017-07-30 17:02:03 +-- Dump completed on 2017-08-09 18:02:32 diff --git a/public/jokes.css b/public/jokes.css index 4a5743a..62e0c62 100644 --- a/public/jokes.css +++ b/public/jokes.css @@ -31,4 +31,10 @@ blockquote form { display: table-cell; width: 10%;} form p {clear: both;} input[type="checkbox"] {float: left; clear: left; width: auto; margin-right: 10px;} -input[type="checkbox"] + label {clear: right;} \ No newline at end of file +input[type="checkbox"] + label {clear: right;} + +.jokelist {display: table;} +.categories {display: table-cell; width: 20%; background-color: #333; padding: 1em; list-style-type: none;} +.categories a {color: white; text-decoration: none;} +.categories li {margin-bottom: 1em;} +.jokelist .jokes {display: table-cell; padding: 1em;} diff --git a/templates/jokes.html.php b/templates/jokes.html.php index 1bea531..fea135d 100644 --- a/templates/jokes.html.php +++ b/templates/jokes.html.php @@ -1,6 +1,16 @@ +
+ + + +

jokes have been submitted to the Internet Joke Database.

+

@@ -25,4 +35,6 @@

- \ No newline at end of file + + +
From 9dd2f2c7487fe8afa4273bbf47fef02352c71201 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Wed, 9 Aug 2017 19:36:11 +0100 Subject: [PATCH 099/124] category list --- classes/Ijdb/Controllers/Joke.php | 11 ++++++++++- classes/Ijdb/Entity/Category.php | 31 +++++++++++++++++++++++++++++++ classes/Ijdb/IjdbRoutes.php | 2 +- 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 classes/Ijdb/Entity/Category.php diff --git a/classes/Ijdb/Controllers/Joke.php b/classes/Ijdb/Controllers/Joke.php index 5ea125b..6fa0f9f 100644 --- a/classes/Ijdb/Controllers/Joke.php +++ b/classes/Ijdb/Controllers/Joke.php @@ -17,7 +17,16 @@ public function __construct(DatabaseTable $jokesTable, DatabaseTable $authorsTab } public function list() { - $jokes = $this->jokesTable->findAll(); + + if (isset($_GET['category'])) + { + $category = $this->categoriesTable->findById($_GET['category']); + $jokes = $category->getJokes(); + } + else + { + $jokes = $this->jokesTable->findAll(); + } $title = 'Joke list'; diff --git a/classes/Ijdb/Entity/Category.php b/classes/Ijdb/Entity/Category.php new file mode 100644 index 0000000..3ee7fb0 --- /dev/null +++ b/classes/Ijdb/Entity/Category.php @@ -0,0 +1,31 @@ +jokesTable = $jokesTable; + $this->jokeCategoriesTable = $jokeCategoriesTable; + } + + public function getJokes() { + $jokeCategories = $this->jokeCategoriesTable->find('categoryId', $this->id); + + $jokes = []; + + foreach ($jokeCategories as $jokeCategory) { + $joke = $this->jokesTable->findById($jokeCategory->jokeId); + if ($joke) { + $jokes[] = $joke; + } + } + + return $jokes; + } +} \ No newline at end of file diff --git a/classes/Ijdb/IjdbRoutes.php b/classes/Ijdb/IjdbRoutes.php index 778db18..d323bee 100644 --- a/classes/Ijdb/IjdbRoutes.php +++ b/classes/Ijdb/IjdbRoutes.php @@ -13,7 +13,7 @@ public function __construct() { $this->jokesTable = new \Ninja\DatabaseTable($pdo, 'joke', 'id', '\Ijdb\Entity\Joke', [&$this->authorsTable, &$this->jokeCategoriesTable]); $this->authorsTable = new \Ninja\DatabaseTable($pdo, 'author', 'id', '\Ijdb\Entity\Author', [&$this->jokesTable]); - $this->categoriesTable = new \Ninja\DatabaseTable($pdo, 'category', 'id'); + $this->categoriesTable = new \Ninja\DatabaseTable($pdo, 'category', 'id', '\Ijdb\Entity\Category', [&$this->jokesTable, &$this->jokeCategoriesTable]); $this->jokeCategoriesTable = new \Ninja\DatabaseTable($pdo, 'joke_category', 'categoryId'); $this->authentication = new \Ninja\Authentication($this->authorsTable, 'email', 'password'); } From 905b9e9d7079816dc3f4db6198215e156d25acb6 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Wed, 9 Aug 2017 20:23:40 +0100 Subject: [PATCH 100/124] can now amend categories with checkboxes --- classes/Ijdb/Controllers/Joke.php | 2 ++ classes/Ijdb/Entity/Joke.php | 14 ++++++++++++++ classes/Ninja/DatabaseTable.php | 9 +++++++++ templates/editjoke.html.php | 7 ++++++- 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/classes/Ijdb/Controllers/Joke.php b/classes/Ijdb/Controllers/Joke.php index 6fa0f9f..77d8579 100644 --- a/classes/Ijdb/Controllers/Joke.php +++ b/classes/Ijdb/Controllers/Joke.php @@ -75,6 +75,8 @@ public function saveEdit() { $jokeEntity = $author->addJoke($joke); + $jokeEntity->clearCategories(); + foreach ($_POST['category'] as $categoryId) { $jokeEntity->addCategory($categoryId); } diff --git a/classes/Ijdb/Entity/Joke.php b/classes/Ijdb/Entity/Joke.php index daf475f..2ddc8e5 100644 --- a/classes/Ijdb/Entity/Joke.php +++ b/classes/Ijdb/Entity/Joke.php @@ -28,4 +28,18 @@ public function addCategory($categoryId) { $this->jokeCategoriesTable->save($jokeCat); } + + public function hasCategory($categoryId) { + $jokeCategories = $this->jokeCategoriesTable->find('jokeId', $this->id); + + foreach ($jokeCategories as $jokeCategory) { + if ($jokeCategory->categoryId == $categoryId) { + return true; + } + } + } + + public function clearCategories() { + $this->jokeCategoriesTable->deleteWhere('jokeId', $this->id); + } } \ No newline at end of file diff --git a/classes/Ninja/DatabaseTable.php b/classes/Ninja/DatabaseTable.php index 13b0abd..f302fee 100644 --- a/classes/Ninja/DatabaseTable.php +++ b/classes/Ninja/DatabaseTable.php @@ -106,6 +106,15 @@ public function delete($id ) { $this->query('DELETE FROM `' . $this->table . '` WHERE `' . $this->primaryKey . '` = :id', $parameters); } + public function deleteWhere($column, $value) { + $query = 'DELETE FROM ' . $this->table . ' WHERE ' . $column . ' = :value'; + + $parameters = [ + 'value' => $value + ]; + + $query = $this->query($query, $parameters); + } public function findAll() { $result = $this->query('SELECT * FROM ' . $this->table); diff --git a/templates/editjoke.html.php b/templates/editjoke.html.php index 7a9a1fe..8e8486b 100644 --- a/templates/editjoke.html.php +++ b/templates/editjoke.html.php @@ -6,9 +6,14 @@

Select categories for this joke:

- + hasCategory($category->id)): ?> + + + + + From f1dd0e4e1879f7097fefd1588522172ea71b73a6 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 10 Aug 2017 17:05:52 +0100 Subject: [PATCH 101/124] router permissions check --- classes/Ijdb/Controllers/Login.php | 4 ++++ classes/Ijdb/Entity/Author.php | 12 ++++++++++++ classes/Ijdb/IjdbRoutes.php | 25 ++++++++++++++++++++++--- classes/Ninja/EntryPoint.php | 5 ++++- classes/Ninja/Routes.php | 1 + templates/permissionserror.html.php | 2 ++ 6 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 templates/permissionserror.html.php diff --git a/classes/Ijdb/Controllers/Login.php b/classes/Ijdb/Controllers/Login.php index ea26148..cfe28f1 100644 --- a/classes/Ijdb/Controllers/Login.php +++ b/classes/Ijdb/Controllers/Login.php @@ -34,6 +34,10 @@ 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); return ['template' => 'logout.html.php', 'title' => 'You have been logged out']; diff --git a/classes/Ijdb/Entity/Author.php b/classes/Ijdb/Entity/Author.php index 4d4e307..38b80bf 100644 --- a/classes/Ijdb/Entity/Author.php +++ b/classes/Ijdb/Entity/Author.php @@ -2,6 +2,14 @@ namespace Ijdb\Entity; class Author { + + const EDIT_JOKES = 1; + const DELETE_JOKES = 2; + const ADD_CATEGORIES = 3; + const EDIT_CATEGORIES = 4; + const REMOVE_CATEGORIES = 5; + const EDIT_USER_ACCESS = 6; + public $id; public $name; public $email; @@ -21,4 +29,8 @@ public function addJoke($joke) { return $this->jokesTable->save($joke); } + + public function hasPermission($permission) { + + } } \ No newline at end of file diff --git a/classes/Ijdb/IjdbRoutes.php b/classes/Ijdb/IjdbRoutes.php index d323bee..24bda84 100644 --- a/classes/Ijdb/IjdbRoutes.php +++ b/classes/Ijdb/IjdbRoutes.php @@ -71,6 +71,12 @@ public function getRoutes(): array { 'action' => 'error' ] ], + 'login/permissionserror' => [ + 'GET' => [ + 'controller' => $loginController, + 'action' => 'permissionsError' + ] + ], 'login/success' => [ 'GET' => [ 'controller' => $loginController, @@ -102,21 +108,24 @@ public function getRoutes(): array { 'controller' => $categoryController, 'action' => 'edit' ], - 'login' => true + 'login' => true, + 'permissions' => \Ijdb\Entity\Author::EDIT_CATEGORIES ], 'category/delete' => [ 'POST' => [ 'controller' => $categoryController, 'action' => 'delete' ], - 'login' => true + 'login' => true, + 'permissions' => \Ijdb\Entity\Author::REMOVE_CATEGORIES ], 'category/list' => [ 'GET' => [ 'controller' => $categoryController, 'action' => 'list' ], - 'login' => true + 'login' => true, + 'permissions' => \Ijdb\Entity\Author::EDIT_CATEGORIES ], '' => [ 'GET' => [ @@ -133,4 +142,14 @@ public function getAuthentication(): \Ninja\Authentication { return $this->authentication; } + public function checkPermission($permission): bool { + $user = $this->authentication->getUser(); + + if ($user && $user->hasPermission($permission)) { + return true; + } else { + return false; + } + } + } \ No newline at end of file diff --git a/classes/Ninja/EntryPoint.php b/classes/Ninja/EntryPoint.php index b682ece..3f6da7b 100644 --- a/classes/Ninja/EntryPoint.php +++ b/classes/Ninja/EntryPoint.php @@ -35,9 +35,12 @@ public function run() { $authentication = $this->routes->getAuthentication(); - if (isset($routes[$this->route]['login']) && isset($routes[$this->route]['login']) && !$authentication->isLoggedIn()) { + if (isset($routes[$this->route]['login']) && !$authentication->isLoggedIn()) { header('location: /login/error'); } + else if (isset($routes[$this->route]['permissions']) && !$this->routes->checkPermission($routes[$this->route]['permissions'])) { + header('location: /login/permissionserror'); + } else { $controller = $routes[$this->route][$this->method]['controller']; $action = $routes[$this->route][$this->method]['action']; diff --git a/classes/Ninja/Routes.php b/classes/Ninja/Routes.php index c6aec0e..c309fdf 100644 --- a/classes/Ninja/Routes.php +++ b/classes/Ninja/Routes.php @@ -4,4 +4,5 @@ interface Routes { public function getRoutes(): array; public function getAuthentication(): \Ninja\Authentication; + public function checkPermission($permission): bool; } \ No newline at end of file diff --git a/templates/permissionserror.html.php b/templates/permissionserror.html.php new file mode 100644 index 0000000..6e00dbf --- /dev/null +++ b/templates/permissionserror.html.php @@ -0,0 +1,2 @@ +

Access Denied

+

You do not have permission to view this page.

\ No newline at end of file From d1ff6f80c9d6d28b7b0bfcc7bf542acc6623cb75 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 10 Aug 2017 17:48:28 +0100 Subject: [PATCH 102/124] edit permissions page --- classes/Ijdb/Controllers/Register.php | 27 +++++++++++++++++++++++++++ classes/Ijdb/IjdbRoutes.php | 18 ++++++++++++++++++ templates/authorlist.html.php | 21 +++++++++++++++++++++ templates/permissions.html.php | 13 +++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 templates/authorlist.html.php create mode 100644 templates/permissions.html.php diff --git a/classes/Ijdb/Controllers/Register.php b/classes/Ijdb/Controllers/Register.php index 275add7..f36f970 100644 --- a/classes/Ijdb/Controllers/Register.php +++ b/classes/Ijdb/Controllers/Register.php @@ -80,4 +80,31 @@ public function registerUser() { ]; } } + + 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 + ] + ]; + } } \ No newline at end of file diff --git a/classes/Ijdb/IjdbRoutes.php b/classes/Ijdb/IjdbRoutes.php index 24bda84..2a010b5 100644 --- a/classes/Ijdb/IjdbRoutes.php +++ b/classes/Ijdb/IjdbRoutes.php @@ -41,6 +41,24 @@ public function getRoutes(): array { 'action' => 'success' ] ], + 'author/permissions' => [ + 'GET' => [ + 'controller' => $authorController, + 'action' => 'permissions' + ], + 'POST' => [ + 'controller' => $jokeController, + 'action' => 'savePermissions' + ], + 'login' => true + ], + 'author/list' => [ + 'GET' => [ + 'controller' => $authorController, + 'action' => 'list' + ], + 'login' => true + ], 'joke/edit' => [ 'POST' => [ 'controller' => $jokeController, diff --git a/templates/authorlist.html.php b/templates/authorlist.html.php new file mode 100644 index 0000000..7e72e8b --- /dev/null +++ b/templates/authorlist.html.php @@ -0,0 +1,21 @@ +

User List

+ + + + + + + + + + + + + + + + + +
NameEmailEdit
name;?>email;?>Edit Permissions
+ + diff --git a/templates/permissions.html.php b/templates/permissions.html.php new file mode 100644 index 0000000..96d91fa --- /dev/null +++ b/templates/permissions.html.php @@ -0,0 +1,13 @@ +

Edit name?>'s Permissions

+ +
+ + $value): ?> +
+ hasPermission($value)) echo 'checked'; ?> /> +
+ + + +
\ No newline at end of file From 677cdba9d95ad8d0cd3306e0797ce2032b7d7401 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 10 Aug 2017 19:25:36 +0100 Subject: [PATCH 103/124] fix route --- classes/Ijdb/IjdbRoutes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/Ijdb/IjdbRoutes.php b/classes/Ijdb/IjdbRoutes.php index 2a010b5..3c7de7e 100644 --- a/classes/Ijdb/IjdbRoutes.php +++ b/classes/Ijdb/IjdbRoutes.php @@ -47,7 +47,7 @@ public function getRoutes(): array { 'action' => 'permissions' ], 'POST' => [ - 'controller' => $jokeController, + 'controller' => $authorController, 'action' => 'savePermissions' ], 'login' => true From 8940cd3dc43099372ca9d04ebfb4b26c698dc57c Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Fri, 11 Aug 2017 16:10:00 +0100 Subject: [PATCH 104/124] binary permissions complete --- classes/Ijdb/Controllers/Joke.php | 6 +++--- classes/Ijdb/Controllers/Register.php | 11 +++++++++++ classes/Ijdb/Entity/Author.php | 10 +++++----- classes/Ijdb/IjdbRoutes.php | 6 ++++-- templates/editjoke.html.php | 2 +- templates/jokes.html.php | 8 ++++++-- 6 files changed, 30 insertions(+), 13 deletions(-) diff --git a/classes/Ijdb/Controllers/Joke.php b/classes/Ijdb/Controllers/Joke.php index 77d8579..6c7090d 100644 --- a/classes/Ijdb/Controllers/Joke.php +++ b/classes/Ijdb/Controllers/Joke.php @@ -39,7 +39,7 @@ public function list() { 'variables' => [ 'totalJokes' => $totalJokes, 'jokes' => $jokes, - 'userId' => $author->id ?? null, + 'user' => $author, 'categories' => $this->categoriesTable->findAll() ] ]; @@ -57,7 +57,7 @@ public function delete() { $joke = $this->jokesTable->findById($_POST['id']); - if ($joke->authorId != $author->id) { + if ($joke->authorId != $author->id && !$author->hasPermission(\Ijdb\Entity\Author::DELETE_JOKES) ) { return; } @@ -98,7 +98,7 @@ public function edit() { 'title' => $title, 'variables' => [ 'joke' => $joke ?? null, - 'userId' => $author->id ?? null, + 'user' => $author, 'categories' => $categories ] ]; diff --git a/classes/Ijdb/Controllers/Register.php b/classes/Ijdb/Controllers/Register.php index f36f970..e665002 100644 --- a/classes/Ijdb/Controllers/Register.php +++ b/classes/Ijdb/Controllers/Register.php @@ -107,4 +107,15 @@ public function permissions() { ] ]; } + + public function savePermissions() { + $author = [ + 'id' => $_GET['id'], + 'permissions' => array_sum($_POST['permissions'] ?? []) + ]; + + $this->authorsTable->save($author); + + header('location: /author/list'); + } } \ No newline at end of file diff --git a/classes/Ijdb/Entity/Author.php b/classes/Ijdb/Entity/Author.php index 38b80bf..2d82f69 100644 --- a/classes/Ijdb/Entity/Author.php +++ b/classes/Ijdb/Entity/Author.php @@ -5,10 +5,10 @@ class Author { const EDIT_JOKES = 1; const DELETE_JOKES = 2; - const ADD_CATEGORIES = 3; - const EDIT_CATEGORIES = 4; - const REMOVE_CATEGORIES = 5; - const EDIT_USER_ACCESS = 6; + const ADD_CATEGORIES = 4; + const EDIT_CATEGORIES = 8; + const REMOVE_CATEGORIES = 16; + const EDIT_USER_ACCESS = 32; public $id; public $name; @@ -31,6 +31,6 @@ public function addJoke($joke) { } public function hasPermission($permission) { - + return $this->permissions & $permission; } } \ No newline at end of file diff --git a/classes/Ijdb/IjdbRoutes.php b/classes/Ijdb/IjdbRoutes.php index 3c7de7e..c3a106e 100644 --- a/classes/Ijdb/IjdbRoutes.php +++ b/classes/Ijdb/IjdbRoutes.php @@ -50,14 +50,16 @@ public function getRoutes(): array { 'controller' => $authorController, 'action' => 'savePermissions' ], - 'login' => true + 'login' => true, + 'permissions' => \Ijdb\Entity\Author::EDIT_USER_ACCESS ], 'author/list' => [ 'GET' => [ 'controller' => $authorController, 'action' => 'list' ], - 'login' => true + 'login' => true, + 'permissions' => \Ijdb\Entity\Author::EDIT_USER_ACCESS ], 'joke/edit' => [ 'POST' => [ diff --git a/templates/editjoke.html.php b/templates/editjoke.html.php index 8e8486b..e854adc 100644 --- a/templates/editjoke.html.php +++ b/templates/editjoke.html.php @@ -1,4 +1,4 @@ -id) || $userId == $joke->authorId) { ?> +id) || $user->id == $joke->authorId || $user->hasPermission(\Ijdb\Entity\Author::EDIT_JOKES)) { ?>
diff --git a/templates/jokes.html.php b/templates/jokes.html.php index fea135d..245b4c7 100644 --- a/templates/jokes.html.php +++ b/templates/jokes.html.php @@ -26,13 +26,17 @@ echo $date->format('jS F Y'); ?>) -authorId) { ?> + + id == $joke->authorId || $user->hasPermission(\Ijdb\Entity\Author::EDIT_JOKES)): ?> Edit + + id == $joke->authorId || $user->hasPermission(\Ijdb\Entity\Author::DELETE_JOKES)): ?>
- + +

From 3d632cbb8365a281219590c258e84c4299d1540f Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Tue, 15 Aug 2017 12:02:16 +0100 Subject: [PATCH 105/124] added markdown parser --- classes/Ninja/Markdown.php | 40 ++++++++++++++++++++++++++++++++++++++ templates/jokes.html.php | 4 +--- 2 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 classes/Ninja/Markdown.php diff --git a/classes/Ninja/Markdown.php b/classes/Ninja/Markdown.php new file mode 100644 index 0000000..9df4ad6 --- /dev/null +++ b/classes/Ninja/Markdown.php @@ -0,0 +1,40 @@ +string = $markDown; + } + + public function toHtml() { + //convert $this->string to HTML + $text = htmlspecialchars($this->string, ENT_QUOTES, 'UTF-8'); + + // strong (bold) + $text = preg_replace('/__(.+?)__/s', '$1', $text); + $text = preg_replace('/\*\*(.+?)\*\*/s', '$1', $text); + + // emphasis (italic) + $text = preg_replace('/_([^_]+)_/', '$1', $text); + $text = preg_replace('/\*([^\*]+)\*/', '$1', $text); + + // Convert Windows (\r\n) to Unix (\n) + $text = str_replace("\r\n", "\n", $text); + // Convert Macintosh (\r) to Unix (\n) + $text = str_replace("\r", "\n", $text); + + // Paragraphs + $text = '

' . str_replace("\n\n", '

', $text) . '

'; + // Line breaks + $text = str_replace("\n", '
', $text); + + // [linked text](link URL) + $text = preg_replace( + '/\[([^\]]+)]\(([-a-z0-9._~:\/?#@!$&\'()*+,;=%]+)\)/i', + '$1', $text); + + return $text; + } +} \ No newline at end of file diff --git a/templates/jokes.html.php b/templates/jokes.html.php index 245b4c7..6f2847b 100644 --- a/templates/jokes.html.php +++ b/templates/jokes.html.php @@ -13,8 +13,7 @@
-

- joketext, ENT_QUOTES, 'UTF-8')?> + joketext))->toHtml()?> (by @@ -37,7 +36,6 @@ -

From 415d59cffbb7b178c1b99650957fa828f7ba9709 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Tue, 15 Aug 2017 12:09:27 +0100 Subject: [PATCH 106/124] better column sizes --- public/jokes.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/jokes.css b/public/jokes.css index 62e0c62..d62bb89 100644 --- a/public/jokes.css +++ b/public/jokes.css @@ -23,7 +23,7 @@ input[type="submit"] {margin-left: 15em; width: auto; padding: 0.5em 1em; clear: form {overflow: auto; clear: both; display: block;} blockquote {display: table; margin-bottom: 1em; border-bottom: 1px solid #ccc; padding: 0.5em; } -blockquote p {display: table-cell; width: 90%; vertical-align: top;} +blockquote p {display: table-cell; width: 70%; vertical-align: top;} blockquote form { display: table-cell; width: 10%;} .errors {padding: 1em; border: 1px solid red; background-color: lightyellow; color: red; margin-bottom: 1em; overflow: auto;} From 3d51dd80172eaec26669e6150720caf3372f37b2 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 17 Aug 2017 14:59:15 +0100 Subject: [PATCH 107/124] sorting --- classes/Ijdb/Controllers/Joke.php | 2 +- classes/Ijdb/Entity/Category.php | 13 +++++++++++++ classes/Ninja/DatabaseTable.php | 18 ++++++++++++++---- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/classes/Ijdb/Controllers/Joke.php b/classes/Ijdb/Controllers/Joke.php index 6c7090d..0fd3f68 100644 --- a/classes/Ijdb/Controllers/Joke.php +++ b/classes/Ijdb/Controllers/Joke.php @@ -25,7 +25,7 @@ public function list() { } else { - $jokes = $this->jokesTable->findAll(); + $jokes = $this->jokesTable->findAll('jokedate DESC'); } $title = 'Joke list'; diff --git a/classes/Ijdb/Entity/Category.php b/classes/Ijdb/Entity/Category.php index 3ee7fb0..b030813 100644 --- a/classes/Ijdb/Entity/Category.php +++ b/classes/Ijdb/Entity/Category.php @@ -26,6 +26,19 @@ public function getJokes() { } } + usort($jokes, [$this, 'sortJokes']); + return $jokes; } + + private function sortJokes($a, $b) { + $aDate = new \DateTime($a->jokedate); + $bDate = new \DateTime($b->jokedate); + + if ($aDate->getTimestamp() == $bDate->getTimestamp()) { + return 0; + } + + return $aDate->getTimestamp() < $bDate->getTimestamp() ? -1 : 1; + } } \ No newline at end of file diff --git a/classes/Ninja/DatabaseTable.php b/classes/Ninja/DatabaseTable.php index f302fee..41b3fc5 100644 --- a/classes/Ninja/DatabaseTable.php +++ b/classes/Ninja/DatabaseTable.php @@ -40,13 +40,17 @@ public function findById($value) { return $query->fetchObject($this->className, $this->constructorArgs); } - public function find($column, $value) { + public function find($column, $value, $orderBy = null) { $query = 'SELECT * FROM ' . $this->table . ' WHERE ' . $column . ' = :value'; $parameters = [ 'value' => $value ]; + if ($orderBy != null) { + $query .= ' ORDER BY ' . $orderBy; + } + $query = $this->query($query, $parameters); return $query->fetchAll(\PDO::FETCH_CLASS, $this->className, $this->constructorArgs); @@ -100,7 +104,7 @@ private function update($fields) { } - public function delete($id ) { + public function delete($id) { $parameters = [':id' => $id]; $this->query('DELETE FROM `' . $this->table . '` WHERE `' . $this->primaryKey . '` = :id', $parameters); @@ -116,8 +120,14 @@ public function deleteWhere($column, $value) { $query = $this->query($query, $parameters); } - public function findAll() { - $result = $this->query('SELECT * FROM ' . $this->table); + public function findAll($orderBy = null) { + $query = 'SELECT * FROM ' . $this->table; + + if ($orderBy != null) { + $query .= ' ORDER BY ' . $orderBy; + } + + $result = $this->query($query); return $result->fetchAll(\PDO::FETCH_CLASS, $this->className, $this->constructorArgs); } From 6348689b4a01a0309e5c9fd778cf8bc7c62d6aec Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 17 Aug 2017 15:12:28 +0100 Subject: [PATCH 108/124] added limit --- classes/Ijdb/Controllers/Joke.php | 8 +++----- classes/Ijdb/Entity/Category.php | 2 +- classes/Ninja/DatabaseTable.php | 12 ++++++++++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/classes/Ijdb/Controllers/Joke.php b/classes/Ijdb/Controllers/Joke.php index 0fd3f68..b8f8762 100644 --- a/classes/Ijdb/Controllers/Joke.php +++ b/classes/Ijdb/Controllers/Joke.php @@ -18,14 +18,12 @@ public function __construct(DatabaseTable $jokesTable, DatabaseTable $authorsTab public function list() { - if (isset($_GET['category'])) - { + if (isset($_GET['category'])) { $category = $this->categoriesTable->findById($_GET['category']); $jokes = $category->getJokes(); } - else - { - $jokes = $this->jokesTable->findAll('jokedate DESC'); + else { + $jokes = $this->jokesTable->findAll('jokedate DESC', 10); } $title = 'Joke list'; diff --git a/classes/Ijdb/Entity/Category.php b/classes/Ijdb/Entity/Category.php index b030813..120ffaf 100644 --- a/classes/Ijdb/Entity/Category.php +++ b/classes/Ijdb/Entity/Category.php @@ -15,7 +15,7 @@ public function __construct(DatabaseTable $jokesTable, DatabaseTable $jokeCatego } public function getJokes() { - $jokeCategories = $this->jokeCategoriesTable->find('categoryId', $this->id); + $jokeCategories = $this->jokeCategoriesTable->find('categoryId', $this->id, null, 10); $jokes = []; diff --git a/classes/Ninja/DatabaseTable.php b/classes/Ninja/DatabaseTable.php index 41b3fc5..1a19667 100644 --- a/classes/Ninja/DatabaseTable.php +++ b/classes/Ninja/DatabaseTable.php @@ -40,7 +40,7 @@ public function findById($value) { return $query->fetchObject($this->className, $this->constructorArgs); } - public function find($column, $value, $orderBy = null) { + public function find($column, $value, $orderBy = null, $limit = null) { $query = 'SELECT * FROM ' . $this->table . ' WHERE ' . $column . ' = :value'; $parameters = [ @@ -51,6 +51,10 @@ public function find($column, $value, $orderBy = null) { $query .= ' ORDER BY ' . $orderBy; } + if ($limit != null) { + $query .= ' LIMIT ' . $limit; + } + $query = $this->query($query, $parameters); return $query->fetchAll(\PDO::FETCH_CLASS, $this->className, $this->constructorArgs); @@ -120,13 +124,17 @@ public function deleteWhere($column, $value) { $query = $this->query($query, $parameters); } - public function findAll($orderBy = null) { + public function findAll($orderBy = null, $limit = null) { $query = 'SELECT * FROM ' . $this->table; if ($orderBy != null) { $query .= ' ORDER BY ' . $orderBy; } + if ($limit != null) { + $query .= ' LIMIT ' . $limit; + } + $result = $this->query($query); return $result->fetchAll(\PDO::FETCH_CLASS, $this->className, $this->constructorArgs); From 9e0570cdfadacdc8ab89bb96535b61d372f70cb2 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 17 Aug 2017 15:46:55 +0100 Subject: [PATCH 109/124] added pagination --- classes/Ijdb/Controllers/Joke.php | 9 +++++++-- classes/Ninja/DatabaseTable.php | 12 ++++++++++-- public/jokes.css | 3 +++ templates/jokes.html.php | 17 +++++++++++++++++ 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/classes/Ijdb/Controllers/Joke.php b/classes/Ijdb/Controllers/Joke.php index b8f8762..d3909f5 100644 --- a/classes/Ijdb/Controllers/Joke.php +++ b/classes/Ijdb/Controllers/Joke.php @@ -18,12 +18,16 @@ public function __construct(DatabaseTable $jokesTable, DatabaseTable $authorsTab public function list() { + $page = $_GET['page'] ?? 1; + + $offset = ($page-1)*10; + if (isset($_GET['category'])) { $category = $this->categoriesTable->findById($_GET['category']); $jokes = $category->getJokes(); } else { - $jokes = $this->jokesTable->findAll('jokedate DESC', 10); + $jokes = $this->jokesTable->findAll('jokedate DESC', 10, $offset); } $title = 'Joke list'; @@ -38,7 +42,8 @@ public function list() { 'totalJokes' => $totalJokes, 'jokes' => $jokes, 'user' => $author, - 'categories' => $this->categoriesTable->findAll() + 'categories' => $this->categoriesTable->findAll(), + 'currentPage' => $page ] ]; } diff --git a/classes/Ninja/DatabaseTable.php b/classes/Ninja/DatabaseTable.php index 1a19667..f4ce75c 100644 --- a/classes/Ninja/DatabaseTable.php +++ b/classes/Ninja/DatabaseTable.php @@ -40,7 +40,7 @@ public function findById($value) { return $query->fetchObject($this->className, $this->constructorArgs); } - public function find($column, $value, $orderBy = null, $limit = null) { + public function find($column, $value, $orderBy = null, $limit = null, $offset = null) { $query = 'SELECT * FROM ' . $this->table . ' WHERE ' . $column . ' = :value'; $parameters = [ @@ -55,6 +55,10 @@ public function find($column, $value, $orderBy = null, $limit = null) { $query .= ' LIMIT ' . $limit; } + if ($offset != null) { + $query .= ' OFFSET ' . $offset; + } + $query = $this->query($query, $parameters); return $query->fetchAll(\PDO::FETCH_CLASS, $this->className, $this->constructorArgs); @@ -124,7 +128,7 @@ public function deleteWhere($column, $value) { $query = $this->query($query, $parameters); } - public function findAll($orderBy = null, $limit = null) { + public function findAll($orderBy = null, $limit = null, $offset = null) { $query = 'SELECT * FROM ' . $this->table; if ($orderBy != null) { @@ -135,6 +139,10 @@ public function findAll($orderBy = null, $limit = null) { $query .= ' LIMIT ' . $limit; } + if ($offset != null) { + $query .= ' OFFSET ' . $offset; + } + $result = $this->query($query); return $result->fetchAll(\PDO::FETCH_CLASS, $this->className, $this->constructorArgs); diff --git a/public/jokes.css b/public/jokes.css index d62bb89..c0b9b0b 100644 --- a/public/jokes.css +++ b/public/jokes.css @@ -38,3 +38,6 @@ input[type="checkbox"] + label {clear: right;} .categories a {color: white; text-decoration: none;} .categories li {margin-bottom: 1em;} .jokelist .jokes {display: table-cell; padding: 1em;} + +.currentpage:before { content: "["; } +.currentpage:after { content: "]"; } \ No newline at end of file diff --git a/templates/jokes.html.php b/templates/jokes.html.php index 6f2847b..b26b7a1 100644 --- a/templates/jokes.html.php +++ b/templates/jokes.html.php @@ -39,4 +39,21 @@
+ + +Select page: + + + + + + + + From 4c322f6f3fb9fa9fb1f4e59b6670864298d5dfe5 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 17 Aug 2017 16:50:07 +0100 Subject: [PATCH 110/124] limit/offset for categories --- classes/Ijdb/Controllers/Joke.php | 9 ++++++--- classes/Ijdb/Entity/Category.php | 8 ++++++-- classes/Ninja/DatabaseTable.php | 12 ++++++++++-- templates/jokes.html.php | 4 ++-- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/classes/Ijdb/Controllers/Joke.php b/classes/Ijdb/Controllers/Joke.php index d3909f5..edc8068 100644 --- a/classes/Ijdb/Controllers/Joke.php +++ b/classes/Ijdb/Controllers/Joke.php @@ -24,15 +24,17 @@ public function list() { if (isset($_GET['category'])) { $category = $this->categoriesTable->findById($_GET['category']); - $jokes = $category->getJokes(); + $jokes = $category->getJokes(10, $offset); + $totalJokes = $category->getNumJokes(); } else { $jokes = $this->jokesTable->findAll('jokedate DESC', 10, $offset); + $totalJokes = $this->jokesTable->total(); } $title = 'Joke list'; - $totalJokes = $this->jokesTable->total(); + $author = $this->authentication->getUser(); @@ -43,7 +45,8 @@ public function list() { 'jokes' => $jokes, 'user' => $author, 'categories' => $this->categoriesTable->findAll(), - 'currentPage' => $page + 'currentPage' => $page, + 'categoryId' => $_GET['category'] ?? null ] ]; } diff --git a/classes/Ijdb/Entity/Category.php b/classes/Ijdb/Entity/Category.php index 120ffaf..9e1f3c5 100644 --- a/classes/Ijdb/Entity/Category.php +++ b/classes/Ijdb/Entity/Category.php @@ -14,8 +14,8 @@ public function __construct(DatabaseTable $jokesTable, DatabaseTable $jokeCatego $this->jokeCategoriesTable = $jokeCategoriesTable; } - public function getJokes() { - $jokeCategories = $this->jokeCategoriesTable->find('categoryId', $this->id, null, 10); + public function getJokes($limit = null, $offset = null) { + $jokeCategories = $this->jokeCategoriesTable->find('categoryId', $this->id, null, $limit, $offset); $jokes = []; @@ -31,6 +31,10 @@ public function getJokes() { return $jokes; } + public function getNumJokes() { + return $this->jokeCategoriesTable->total('categoryId', $this->id); + } + private function sortJokes($a, $b) { $aDate = new \DateTime($a->jokedate); $bDate = new \DateTime($b->jokedate); diff --git a/classes/Ninja/DatabaseTable.php b/classes/Ninja/DatabaseTable.php index f4ce75c..747a143 100644 --- a/classes/Ninja/DatabaseTable.php +++ b/classes/Ninja/DatabaseTable.php @@ -22,8 +22,16 @@ private function query($sql, $parameters = []) { return $query; } - public function total() { - $query = $this->query('SELECT COUNT(*) FROM `' . $this->table . '`'); + public function total($field = null, $value = null) { + $sql = 'SELECT COUNT(*) FROM `' . $this->table . '`'; + $parameters = []; + + if (!empty($field)) { + $sql .= ' WHERE `' . $field . '` = :value'; + $parameters = ['value' => $value]; + } + + $query = $this->query($sql, $parameters); $row = $query->fetch(); return $row[0]; } diff --git a/templates/jokes.html.php b/templates/jokes.html.php index b26b7a1..428f337 100644 --- a/templates/jokes.html.php +++ b/templates/jokes.html.php @@ -50,9 +50,9 @@ for ($i = 1; $i <= $numPages; $i++): if ($i == $currentPage): ?> - + - + From 7a0c93155210e1827d0d10d54ce3e47c6baa3ceb Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Wed, 4 Oct 2017 14:10:40 +0100 Subject: [PATCH 111/124] added sample switcher --- public/samples/index.php | 126 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 public/samples/index.php diff --git a/public/samples/index.php b/public/samples/index.php new file mode 100644 index 0000000..5eeb442 --- /dev/null +++ b/public/samples/index.php @@ -0,0 +1,126 @@ + +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + +} +catch (PDOException $e) { + echo '

Could not connect to database. Did you delete the `homestead` user or change it\'s password?

'; + echo '

' . $e . '

'; + die; +} + + +try { + //sample user might not exist so the query may throw an exception, that's fine, ignore it. + try { + //Drop the user, there's a chance the password has been changed + $pdo->query('DROP USER \'ijdb_sample\'@\'localhost\''); + } + catch (PDOException $e) {} + + //Create the user for the sample code to use + $pdo->query('CREATE USER \'ijdb_sample\'@\'localhost\' IDENTIFIED BY \'mypassword\''); + + //Drop the database, only one sample should be used at once. + + $pdo->query('DROP DATABASE IF EXISTS ijdb_sample'); + + + $pdo->query('CREATE DATABASE ijdb_sample'); + $pdo->query('GRANT ALL PRIVILEGES ON ijdb_sample.* To \'ijdb_sample\'@\'localhost\''); + $pdo->query('FLUSH PRIVILEGES'); + $pdo->query('USE ijdb_sample'); + + if (file_exists('../../database.sql')) { + $pdo->exec(file_get_contents('../../database.sql')); + } + +} +catch (PDOException $e) { + echo 'Could not create sample database/user'; + echo $e->getMessage(); +} + + + + +if (isset($_GET['branch'])) { + exec('git checkout ' . $_GET['branch'], $n); +} +exec('git status', $output); + +$branchName = str_replace('On branch ', '', $output[0]); + +?> + + + <?= $branchName; ?> - PHP Nove to Ninja sample code + + + + + + + + +

PHP Novice to Ninja sample code

+ +

Click on a file to view in your browser

+ +
    + isDot()) continue; + if ($file->getFileName() == 'samples') continue; + + $code = file_get_contents('../' . $file->getFileName()); + echo '
  • ' . $file->getFileName() . ''; + + echo highlight_string($code, true); + echo '
  • '; + } + + ?> +
+ + +

View a different sample

+ + + + \ No newline at end of file From f655886ab7cf317a0f917dee6952b36381029db1 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Wed, 4 Oct 2017 14:20:53 +0100 Subject: [PATCH 112/124] added registration link --- templates/loginerror.html.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/loginerror.html.php b/templates/loginerror.html.php index 17c62c8..131d964 100644 --- a/templates/loginerror.html.php +++ b/templates/loginerror.html.php @@ -1,3 +1,3 @@

You are not logged in

-

You must be logged in to view this page. Click here to log in.

\ No newline at end of file +

You must be logged in to view this page. Click here to log in or Click here to register an account

\ No newline at end of file From 308402e6a343c13f5e69ff0cddedbb86f63132c8 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 5 Oct 2017 18:27:27 +0100 Subject: [PATCH 113/124] updated template syntax --- templates/editjoke.html.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/templates/editjoke.html.php b/templates/editjoke.html.php index e854adc..b24b563 100644 --- a/templates/editjoke.html.php +++ b/templates/editjoke.html.php @@ -1,4 +1,4 @@ -id) || $user->id == $joke->authorId || $user->hasPermission(\Ijdb\Entity\Author::EDIT_JOKES)) { ?> +id) || $user->id == $joke->authorId || $user->hasPermission(\Ijdb\Entity\Author::EDIT_JOKES)): ?>
@@ -18,8 +18,8 @@
- +

You may only edit jokes that you posted.

- \ No newline at end of file + \ No newline at end of file From e45d174bfec7e18070a92a2ff539146e67887c57 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 5 Oct 2017 18:27:27 +0100 Subject: [PATCH 114/124] updated template syntax --- templates/login.html.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/templates/login.html.php b/templates/login.html.php index 1327016..ce449d9 100644 --- a/templates/login.html.php +++ b/templates/login.html.php @@ -1,8 +1,6 @@ -' . $error . ''; -} -?> + +
+
From ceb7f2d3673c602da8f11b983dfb58d99bfe1e55 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 5 Oct 2017 18:27:27 +0100 Subject: [PATCH 115/124] updated template syntax --- templates/layout.html.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/templates/layout.html.php b/templates/layout.html.php index ecea358..743fe0d 100644 --- a/templates/layout.html.php +++ b/templates/layout.html.php @@ -14,11 +14,11 @@
  • Home
  • Jokes List
  • Add a new Joke
  • - +
  • Log out
  • - +
  • Log in
  • - + From 20b424f785d5f155cea1c64db312d7cee1621c6f Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 5 Oct 2017 18:27:27 +0100 Subject: [PATCH 116/124] updated template syntax --- templates/register.html.php | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/templates/register.html.php b/templates/register.html.php index 63f24aa..4b78037 100644 --- a/templates/register.html.php +++ b/templates/register.html.php @@ -1,22 +1,13 @@ - +

    Your account could not be created, please check the following:

      - -
    • - + +
    • +
    - + From d0f6f9b21a7460a06bd5ab1a3d98b85953027cd9 Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 5 Oct 2017 19:41:47 +0100 Subject: [PATCH 117/124] added sample switcher --- public/samples/index.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/public/samples/index.php b/public/samples/index.php index 5eeb442..a155bdc 100644 --- a/public/samples/index.php +++ b/public/samples/index.php @@ -110,12 +110,14 @@ - \ No newline at end of file + From 304add2144881eff11678a3f11af41d4ccac58ba Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 5 Oct 2017 20:21:21 +0100 Subject: [PATCH 118/124] updated sample switcher: supports custom samples --- public/samples/index.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/public/samples/index.php b/public/samples/index.php index a155bdc..aeaaae4 100644 --- a/public/samples/index.php +++ b/public/samples/index.php @@ -45,14 +45,23 @@ } - +exec('git status', $output); +$branchName = str_replace('On branch ', '', $output[0]); if (isset($_GET['branch'])) { - exec('git checkout ' . $_GET['branch'], $n); + exec('git status', $status); + $status = implode("\n", $status); + if (strpos($stauts, 'nothing to commit') !== false) { + $parts = explode('-Modified', $branchName); + $newBranchName = $parts[0] . '-Modified-' . date('Y-m-d H:i:s'); + + exec('git checkout -b "' . $newBranchName . '"'); + exec('git add .'); + exec('git commit -m "user modified sample"'); + } + exec('git checkout "' . $_GET['branch'] . '"', $n); } -exec('git status', $output); -$branchName = str_replace('On branch ', '', $output[0]); ?> From f22210ac52dabffd0daaa2d267e04f66a21c921f Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 5 Oct 2017 20:22:22 +0100 Subject: [PATCH 119/124] updated sample switcher: supports custom samples --- public/samples/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/samples/index.php b/public/samples/index.php index aeaaae4..e98fc83 100644 --- a/public/samples/index.php +++ b/public/samples/index.php @@ -51,7 +51,7 @@ if (isset($_GET['branch'])) { exec('git status', $status); $status = implode("\n", $status); - if (strpos($stauts, 'nothing to commit') !== false) { + if (strpos($status, 'nothing to commit') !== false) { $parts = explode('-Modified', $branchName); $newBranchName = $parts[0] . '-Modified-' . date('Y-m-d H:i:s'); From 85a399790843f2eebcc02594b194f7c9b62178dd Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Thu, 5 Oct 2017 20:51:23 +0100 Subject: [PATCH 120/124] updated sample switcher: supports custom samples --- public/samples/index.php | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/public/samples/index.php b/public/samples/index.php index e98fc83..c90b231 100644 --- a/public/samples/index.php +++ b/public/samples/index.php @@ -1,6 +1,5 @@ setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); @@ -51,15 +50,30 @@ if (isset($_GET['branch'])) { exec('git status', $status); $status = implode("\n", $status); - if (strpos($status, 'nothing to commit') !== false) { - $parts = explode('-Modified', $branchName); - $newBranchName = $parts[0] . '-Modified-' . date('Y-m-d H:i:s'); + if (strpos($status, 'nothing to commit') == false) { + + + + $parts = explode('_Modified', $branchName); + $newBranchName = $parts[0] . '_Modified-' . date('Y-m-d-H.i.s'); + - exec('git checkout -b "' . $newBranchName . '"'); - exec('git add .'); - exec('git commit -m "user modified sample"'); + exec('git checkout -b ' . $newBranchName . ' 2>&1', $z); + + exec('git add -A 2>&1', $x); + exec('git commit -m "user modified sample" 2>&1', $y); + + var_dump($z); + var_dump($y); + var_dump($x); } exec('git checkout "' . $_GET['branch'] . '"', $n); + $branchName = $_GET['branch']; +} + +if (!isset($branchName)) { + exec('git status', $output); + $branchName = str_replace('On branch ', '', $output[0]); } @@ -120,14 +134,16 @@ ' . $branch . ''; } From d0f788405cbddec92cb7cff7ae7dfadd7a221f6e Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Fri, 6 Oct 2017 12:43:52 +0100 Subject: [PATCH 121/124] updated sample switcher: supports custom samples --- public/samples/index.php | 1 + 1 file changed, 1 insertion(+) diff --git a/public/samples/index.php b/public/samples/index.php index c90b231..520fb00 100644 --- a/public/samples/index.php +++ b/public/samples/index.php @@ -141,6 +141,7 @@ $branch = trim($branch, " \t*"); $branch = str_replace('origin/', '', $branch); + $branch = str_replace('remotes/', '', $branch); $class =$branch == $branchName ? 'current' : ''; From e00eaa9b6778016d2d59adab668220b65a829e6b Mon Sep 17 00:00:00 2001 From: Tom Butler Date: Fri, 6 Oct 2017 12:57:43 +0100 Subject: [PATCH 122/124] updated sample switcher: supports custom samples --- public/samples/index.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/public/samples/index.php b/public/samples/index.php index 520fb00..a3dc2d4 100644 --- a/public/samples/index.php +++ b/public/samples/index.php @@ -136,6 +136,8 @@ exec('git branch -a', $branches); + $branchList = []; + foreach ($branches as $branch) { @@ -143,11 +145,19 @@ $branch = str_replace('origin/', '', $branch); $branch = str_replace('remotes/', '', $branch); + $branchList[$branch] = $branch; + + } + + foreach ($branchList as $branch) { $class =$branch == $branchName ? 'current' : ''; if ($branch == 'master') continue; echo '
  • ' . $branch . '
  • '; } + + + ?> From eeac7c16a66b478e76f226ca119a0718d9c9047b Mon Sep 17 00:00:00 2001 From: alessio-levrero <36887511+alessio-levrero@users.noreply.github.com> Date: Tue, 27 Feb 2018 13:44:09 +0100 Subject: [PATCH 123/124] Updated login.php to fix logout issue Added "session_destroy()" after "unset($_SESSION);" to actually kill the session - without it can give logout issues. --- classes/Ijdb/Controllers/Login.php | 1 + 1 file changed, 1 insertion(+) diff --git a/classes/Ijdb/Controllers/Login.php b/classes/Ijdb/Controllers/Login.php index cfe28f1..6a336cb 100644 --- a/classes/Ijdb/Controllers/Login.php +++ b/classes/Ijdb/Controllers/Login.php @@ -40,6 +40,7 @@ public function permissionsError() { public function logout() { unset($_SESSION); + session_destroy(); return ['template' => 'logout.html.php', 'title' => 'You have been logged out']; } } From 8fbc700b7f455fa8bff0b8e1f53d3ae5d6aa4119 Mon Sep 17 00:00:00 2001 From: alessio-levrero <36887511+alessio-levrero@users.noreply.github.com> Date: Tue, 27 Feb 2018 13:50:02 +0100 Subject: [PATCH 124/124] Fixed some db issues - Added the missing 'permissions' column in 'author' table - Fixed the jokes id and the category associations --- database.sql | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/database.sql b/database.sql index 16b4903..9a3701a 100644 --- a/database.sql +++ b/database.sql @@ -27,6 +27,7 @@ CREATE TABLE `author` ( `name` varchar(255) DEFAULT NULL, `email` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, + `permissions` int(64) NOT NULL DEFAULT 0, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; @@ -87,7 +88,7 @@ CREATE TABLE `joke` ( LOCK TABLES `joke` WRITE; /*!40000 ALTER TABLE `joke` DISABLE KEYS */; -INSERT INTO `joke` VALUES (1,'How many programmers does it take to screw in a lightbulb? None, it\'s a hardware problem.','2017-04-01',1),(2,'Why did the programmer quit his job? He didn\'t get arrays','2017-04-01',1),(3,'Why was the empty array stuck outside? It didn\'t have any keys','2017-04-01',2),(20,'How do functions break up? They stop calling each other','2017-08-09',2),(21,'How do you tell HTML from HTML5? Try it out in Internet Explorer. Did it work? No? It\'s HTML5','2017-08-09',2),(22,'You don\'t need any training to be a litter picker, you pick it up as you go','2017-08-09',2); +INSERT INTO `joke` VALUES (1,'How many programmers does it take to screw in a lightbulb? None, it\'s a hardware problem.','2017-04-01',1),(2,'Why did the programmer quit his job? He didn\'t get arrays','2017-04-01',1),(3,'Why was the empty array stuck outside? It didn\'t have any keys','2017-04-01',2),(4,'How do functions break up? They stop calling each other','2017-08-09',2),(5,'How do you tell HTML from HTML5? Try it out in Internet Explorer. Did it work? No? It\'s HTML5','2017-08-09',2),(6,'You don\'t need any training to be a litter picker, you pick it up as you go','2017-08-09',2); /*!40000 ALTER TABLE `joke` ENABLE KEYS */; UNLOCK TABLES; @@ -111,7 +112,7 @@ CREATE TABLE `joke_category` ( LOCK TABLES `joke_category` WRITE; /*!40000 ALTER TABLE `joke_category` DISABLE KEYS */; -INSERT INTO `joke_category` VALUES (17,1),(17,2),(18,2),(19,1),(19,2),(20,1),(20,2),(21,1),(22,2); +INSERT INTO `joke_category` VALUES (1,1),(1,2),(2,1),(3,1),(4,2),(5,1),(6,2); /*!40000 ALTER TABLE `joke_category` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;