From a86f6a2cef1400a6ba290695cdcc36e59ff58438 Mon Sep 17 00:00:00 2001 From: ryanorsinger Date: Mon, 20 Apr 2015 14:37:44 -0500 Subject: [PATCH 1/5] Add migrations and simplify requires to bootstrap --- adlister_login.php | 6 ---- bootstrap.php | 26 ++++++++++++++++ database/create_items_table_migration.php | 16 ++++++++++ database/create_users_table_migration.php | 13 ++++++++ database/migration.php | 6 ++++ models/BaseModel.php | 10 +++---- public/{ad.create.php => ads.create.php} | 3 +- public/{ad.index.php => ads.index.php} | 0 public/index.php | 36 +++++++++++++++++++++-- utils/Auth.php | 3 ++ utils/Input.php | 2 +- utils/Logger.php | 3 ++ 12 files changed, 108 insertions(+), 16 deletions(-) delete mode 100644 adlister_login.php create mode 100644 database/create_items_table_migration.php create mode 100644 database/create_users_table_migration.php rename public/{ad.create.php => ads.create.php} (83%) rename public/{ad.index.php => ads.index.php} (100%) diff --git a/adlister_login.php b/adlister_login.php deleted file mode 100644 index 973af47..0000000 --- a/adlister_login.php +++ /dev/null @@ -1,6 +0,0 @@ -setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); diff --git a/database/create_items_table_migration.php b/database/create_items_table_migration.php new file mode 100644 index 0000000..12ba7fb --- /dev/null +++ b/database/create_items_table_migration.php @@ -0,0 +1,16 @@ +exec('DROP TABLE IF EXISTS items'); + +$query = 'CREATE TABLE items ( + id INT UNSIGNED NOT NULL AUTO_INCREMENT, + price INT UNSIGNED NOT NULL, + item VARCHAR(256) NOT NULL, + description TEXT, + location VARCHAR(256), + date_posted DATE, + user_id INT UNSIGNED, + PRIMARY KEY (id) +)'; + +$dbc->exec($query); diff --git a/database/create_users_table_migration.php b/database/create_users_table_migration.php new file mode 100644 index 0000000..e3409d0 --- /dev/null +++ b/database/create_users_table_migration.php @@ -0,0 +1,13 @@ +exec('DROP TABLE IF EXISTS users'); + +$query = 'CREATE TABLE users ( + id INT UNSIGNED NOT NULL AUTO_INCREMENT, + email VARCHAR(128) NOT NULL, + password VARCHAR(256) NOT NULL, + PRIMARY KEY (id) +)'; + +$dbc->exec($query); diff --git a/database/migration.php b/database/migration.php index e69de29..aec9c96 100644 --- a/database/migration.php +++ b/database/migration.php @@ -0,0 +1,6 @@ + - - + diff --git a/public/ad.index.php b/public/ads.index.php similarity index 100% rename from public/ad.index.php rename to public/ads.index.php diff --git a/public/index.php b/public/index.php index ecdd5e0..692311e 100644 --- a/public/index.php +++ b/public/index.php @@ -1,14 +1,46 @@ - Chadlister + + -

Welcome to Chadlister!

+ + +
+

Welcome to Radlister!

+

The rad way to post free, unlimited ads!

+ +
diff --git a/utils/Auth.php b/utils/Auth.php index e69de29..814a42d 100644 --- a/utils/Auth.php +++ b/utils/Auth.php @@ -0,0 +1,3 @@ + Date: Mon, 20 Apr 2015 16:41:16 -0500 Subject: [PATCH 2/5] Update migrations --- database/create_items_table_migration.php | 2 +- public/index.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/database/create_items_table_migration.php b/database/create_items_table_migration.php index 12ba7fb..082f2fe 100644 --- a/database/create_items_table_migration.php +++ b/database/create_items_table_migration.php @@ -4,8 +4,8 @@ $query = 'CREATE TABLE items ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, + item VARCHAR(512) NOT NULL, price INT UNSIGNED NOT NULL, - item VARCHAR(256) NOT NULL, description TEXT, location VARCHAR(256), date_posted DATE, diff --git a/public/index.php b/public/index.php index 692311e..69e0d23 100644 --- a/public/index.php +++ b/public/index.php @@ -38,7 +38,7 @@
-

Welcome to Radlister!

+

Welcome to Rad-Lister!

The rad way to post free, unlimited ads!

From ba162b80044150fa107b969ef37934c5a189defb Mon Sep 17 00:00:00 2001 From: ryanorsinger Date: Mon, 20 Apr 2015 16:43:36 -0500 Subject: [PATCH 3/5] Add first draft of readme.md --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..d4dfc18 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# Ad Lister Project + +* FYI Credentials are saved in a gitignored .env.php file. * + +The Ad Lister project is a modified craigslist clone that will help test and solidify your understanding of the HTML, CSS, JavaScript, and PHP concepts you have learned so far in this course. You will be working on this project in teams of two to three. Teams of two are preferred unless there are an odd number of students, forcing one team of three. + +This project is meant to be a challenge. Working through tough challenges that require lots of coding will help take your skills and confidence to the next level. + +Here are the primary goals of the project: + +Gain experience in building a well-designed in PHP site without the use of a framework. +Learn how to use Git in a team environment. +Test your resourcefulness in solving problems. From 014d3525e3c9b7fc8095dc5656e9ec4fa0a94ca0 Mon Sep 17 00:00:00 2001 From: ryanorsinger Date: Tue, 21 Apr 2015 16:06:18 -0500 Subject: [PATCH 4/5] Add readme and migration updates --- README.md | 13 +++ bootstrap.php | 16 +--- database/ads_seeder.php | 96 +++++++++++++++++++ ...ion.php => create_ads_table_migration.php} | 4 +- database/migration.php | 2 +- database/seeder.php | 5 + database/users_seeder.php | 15 +++ env-template.php | 10 ++ models/Ad.php | 25 +++++ models/BaseModel.php | 51 ++-------- models/User.php | 14 +++ public/test.php | 13 +++ utils/Auth.php | 55 ++++++++++- utils/DB.php | 29 ++++++ 14 files changed, 285 insertions(+), 63 deletions(-) create mode 100644 database/ads_seeder.php rename database/{create_items_table_migration.php => create_ads_table_migration.php} (78%) create mode 100644 database/users_seeder.php create mode 100644 env-template.php create mode 100644 public/test.php create mode 100644 utils/DB.php diff --git a/README.md b/README.md index d4dfc18..b720ee5 100644 --- a/README.md +++ b/README.md @@ -11,3 +11,16 @@ Here are the primary goals of the project: Gain experience in building a well-designed in PHP site without the use of a framework. Learn how to use Git in a team environment. Test your resourcefulness in solving problems. + +# Getting Started +1. Clone this repo into `~/vagrant-lamp/sites/` so that it creates adlister.dev/ there. +2. Setup a host for adlister.dev +3. Create your `.env.php` file +4. Create a new database +5. Run `/database/migrations.php` in order to create tables. +6. Run `/database/seeder.php` in order to seed the tables with test data. + +# $_ENV setup +1. Open env-template.php to see the fields you should use to create your .env.php file. +2. Specify your environmental variables and credentials within your .env.php file. +3. So that you DO NOT commit passwords, double check that .env.php is added to .gitignore. diff --git a/bootstrap.php b/bootstrap.php index ffa81e1..788c7a9 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -3,6 +3,7 @@ // Load the environment variables. $_ENV = include_once '.env.php'; +require_once 'utils/DB.php'; require_once 'models/BaseModel.php'; require_once 'models/Ad.php'; require_once 'models/User.php'; @@ -10,17 +11,4 @@ require_once 'utils/Input.php'; require_once 'utils/Logger.php'; - -/** - * Get new instance of PDO Object. - * This exists outside of the BaseModel class in order to support migration/seeder access. - */ -$dbc = new PDO( - 'mysql:host='.$_ENV['DB_HOST'].'; - dbname='.$_ENV['DB_NAME'], - $_ENV['DB_USER'], - $_ENV['DB_PASS'] - ); - -// Tell PDO to throw exceptions on error -$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); +$dbc = DB::connect(); diff --git a/database/ads_seeder.php b/database/ads_seeder.php new file mode 100644 index 0000000..2571d7f --- /dev/null +++ b/database/ads_seeder.php @@ -0,0 +1,96 @@ + '50cc Hyamadahizuki Motorized Cycle', + 'price' => '$500', + 'description' => 'Tear up the road with this great road-hog!', + 'location' => 'San Antonio, TX', + 'date_posted' => '2015-01-01', + 'user_id' => '1', + ], + [ + 'item' => '', + 'price' => '', + 'description' => '', + 'location' => '', + 'date_posted' => '', + 'user_id' => '', + ], + [ + 'item' => '', + 'price' => '', + 'description' => '', + 'location' => '', + 'date_posted' => '', + 'user_id' => '', + ], + [ + 'item' => '', + 'price' => '', + 'description' => '', + 'location' => '', + 'date_posted' => '', + 'user_id' => '', + ], + [ + 'item' => '', + 'price' => '', + 'description' => '', + 'location' => '', + 'date_posted' => '', + 'user_id' => '', + ], + [ + 'item' => '', + 'price' => '', + 'description' => '', + 'location' => '', + 'date_posted' => '', + 'user_id' => '', + ], + [ + 'item' => '', + 'price' => '', + 'description' => '', + 'location' => '', + 'date_posted' => '', + 'user_id' => '', + ], + [ + 'item' => '', + 'price' => '', + 'description' => '', + 'location' => '', + 'date_posted' => '', + 'user_id' => '', + ], + [ + 'item' => '', + 'price' => '', + 'description' => '', + 'location' => '', + 'date_posted' => '', + 'user_id' => '', + ], + [ + 'item' => '', + 'price' => '', + 'description' => '', + 'location' => '', + 'date_posted' => '', + 'user_id' => '', + ], + [ + 'item' => '', + 'price' => '', + 'description' => '', + 'location' => '', + 'date_posted' => '', + 'user_id' => '', + ], + + + ] diff --git a/database/create_items_table_migration.php b/database/create_ads_table_migration.php similarity index 78% rename from database/create_items_table_migration.php rename to database/create_ads_table_migration.php index 082f2fe..a601a04 100644 --- a/database/create_items_table_migration.php +++ b/database/create_ads_table_migration.php @@ -1,8 +1,8 @@ exec('DROP TABLE IF EXISTS items'); +$dbc->exec('DROP TABLE IF EXISTS ads'); -$query = 'CREATE TABLE items ( +$query = 'CREATE TABLE ads ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, item VARCHAR(512) NOT NULL, price INT UNSIGNED NOT NULL, diff --git a/database/migration.php b/database/migration.php index aec9c96..bdeb175 100644 --- a/database/migration.php +++ b/database/migration.php @@ -2,5 +2,5 @@ require_once '../bootstrap.php'; -require_once 'create_items_table_migration.php'; +require_once 'create_ads_table_migration.php'; require_once 'create_users_table_migration.php'; diff --git a/database/seeder.php b/database/seeder.php index e69de29..1f1b2bd 100644 --- a/database/seeder.php +++ b/database/seeder.php @@ -0,0 +1,5 @@ + + diff --git a/database/users_seeder.php b/database/users_seeder.php new file mode 100644 index 0000000..72b0a04 --- /dev/null +++ b/database/users_seeder.php @@ -0,0 +1,15 @@ + $_ENV['USER_EMAIL'], + 'password' => $_ENV['USER_PASS'], + ]; + +$query = 'INSERT INTO users (email, password) VALUES (:email, :password)'; +$stmt = $dbc->prepare($query); +$stmt->bindValue(':email', $user['email'], PDO::PARAM_STR); +$stmt->bindValue(':password', $user['password'], PDO::PARAM_STR); +$stmt->execute(); + diff --git a/env-template.php b/env-template.php new file mode 100644 index 0000000..9fba0f9 --- /dev/null +++ b/env-template.php @@ -0,0 +1,10 @@ + '', + 'DB_NAME' => '', + 'DB_USER' => '', + 'DB_PASS' => '', + 'USER_EMAIL' => '', + 'USER_PASS' => '' +); diff --git a/models/Ad.php b/models/Ad.php index e69de29..f523feb 100644 --- a/models/Ad.php +++ b/models/Ad.php @@ -0,0 +1,25 @@ +prepare($query); + $stmt->bindValue(':item', $this->item, PDO::PARAM_STR); + $stmt->bindValue(':price', $this->price, PDO::PARAM_STR); + $stmt->bindValue(':description', $this->description, PDO::PARAM_STR); + $stmt->bindValue(':location', $this->location, PDO::PARAM_STR); + $stmt->exectute(); + } + + protected function update() + { + + } +} diff --git a/models/BaseModel.php b/models/BaseModel.php index 5136347..3c7e6ad 100644 --- a/models/BaseModel.php +++ b/models/BaseModel.php @@ -2,7 +2,7 @@ require_once '../bootstrap.php'; -class Model { +class BaseModel { protected static $dbc; protected static $table; @@ -73,50 +73,9 @@ public function save() } } - protected function insert() - { - - $table = static::$table; - - $query = "INSERT INTO $table (first_name, last_name, username, password) - VALUES (':first_name', ':last_name', ':username', ':password');"; - - $stmt = self::$dbc->prepare($query); - $stmt->bindValue(':first_name', $this->first_name, PDO::PARAM_STR); - $stmt->bindValue(':last_name', $this->last_name, PDO::PARAM_STR); - $stmt->bindValue(':username', $this->username, PDO::PARAM_STR); - $stmt->bindValue(':password', $this->password, PDO::PARAM_STR); - $stmt->execute(); - - // @TODO: After insert, add the id back to the attributes array so the object can properly reflect the id - - } - - protected function update() - { - $table = static::$table; - - // @TODO: Ensure that update is properly handled with the id key - $query = "UPDATE $table SET - first_name = :first_name, - last_name = :last_name, - email = :email, - username = :username, - password = :password - WHERE id = :id"; - - // @TODO: Use prepared statements to ensure data security - $stmt = self::$dbc->prepare($query); - $stmt->bindValue(':first_name', $this->first_name, PDO::PARAM_STR); - $stmt->bindValue(':last_name', $this->last_name, PDO::PARAM_STR); - $stmt->bindValue(':username', $this->username, PDO::PARAM_STR); - $stmt->bindValue(':email', $this->email, PDO::PARAM_STR); - $stmt->bindValue(':password', $this->password, PDO::PARAM_STR); - $stmt->bindValue(':id', $this->id, PDO::PARAM_INT); - $stmt->execute(); - } - + protected function insert() {} + protected function update() {} /* * Find a record based on an id @@ -155,7 +114,9 @@ public static function all() { self::dbConnect(); - $result = self::$dbc->query('SELECT * FROM users')->fetchAll(PDO::FETCH_ASSOC); + $table = static::$table; + + $result = self::$dbc->query("SELECT * FROM $table")->fetchAll(PDO::FETCH_ASSOC); $instance = null; if ($result) diff --git a/models/User.php b/models/User.php index e69de29..2c9a329 100644 --- a/models/User.php +++ b/models/User.php @@ -0,0 +1,14 @@ + diff --git a/utils/Auth.php b/utils/Auth.php index 814a42d..b18a090 100644 --- a/utils/Auth.php +++ b/utils/Auth.php @@ -1,3 +1,56 @@ setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + return $dbc; + } + + + +} From 0d9283f5e46bd68a185974bfc117a29782cdc58a Mon Sep 17 00:00:00 2001 From: ryanorsinger Date: Fri, 1 May 2015 17:43:35 -0500 Subject: [PATCH 5/5] Update AdLister --- env-template.php | 8 ++++---- models/Ad.php | 2 ++ models/BaseModel.php | 1 + models/User.php | 42 +++++++++++++++++++++++++++++++++++++++++- public/index.php | 8 ++++++++ utils/Auth.php | 4 ++++ 6 files changed, 60 insertions(+), 5 deletions(-) diff --git a/env-template.php b/env-template.php index 9fba0f9..146f0f4 100644 --- a/env-template.php +++ b/env-template.php @@ -1,10 +1,10 @@ '', - 'DB_NAME' => '', - 'DB_USER' => '', - 'DB_PASS' => '', + 'DB_HOST' => '127.0.0.1', + 'DB_NAME' => 'your-db-name', + 'DB_USER' => 'your-db-user', + 'DB_PASS' => 'your-db-password', 'USER_EMAIL' => '', 'USER_PASS' => '' ); diff --git a/models/Ad.php b/models/Ad.php index f523feb..57cfd88 100644 --- a/models/Ad.php +++ b/models/Ad.php @@ -16,6 +16,8 @@ protected function insert() $stmt->bindValue(':description', $this->description, PDO::PARAM_STR); $stmt->bindValue(':location', $this->location, PDO::PARAM_STR); $stmt->exectute(); + + } protected function update() diff --git a/models/BaseModel.php b/models/BaseModel.php index 3c7e6ad..2627e52 100644 --- a/models/BaseModel.php +++ b/models/BaseModel.php @@ -140,3 +140,4 @@ public function delete() } } + diff --git a/models/User.php b/models/User.php index 2c9a329..9d5d63d 100644 --- a/models/User.php +++ b/models/User.php @@ -6,9 +6,49 @@ class User extends BaseModel { protected static $table = 'users'; + public static function findUserByUsername($username) + { + self::dbConnect(); + $table = static::$table; + + $query = "SELECT * from $table where username = :username"; + self::$dbc->prepare($query); + $stmt = self::$dbc->bindValue(':username', $username, PDO::PARAM_STR); + $stmt->execute(); + $result = $stmt->fetch(PDO::FETCH_ASSOC); + + // The following code will set the attributes on the calling object based on the result variable's contents + + $instance = null; + if ($result) + { + $instance = new static; + $instance->attributes = $result; + } + return $instance; + + } + protected function insert() {} protected function update() - {} + { + + $hashed_pass = password_hash($this->password, PASSWORD_DEFAULT); + + $query = "UPDATE users...."; + + $stmt->bindValue(':password', $hashed_pass, PDO::PARAM_STR); + + } } + + +$userToFind = User::findUserByUsername('Bob'); +var_dump($userToFind); + +$userToFind == ['username' => 'Bob']; + +$newUser = new User(); +$newUser->username = $userToFind[''] diff --git a/public/index.php b/public/index.php index 69e0d23..98a03a9 100644 --- a/public/index.php +++ b/public/index.php @@ -41,6 +41,14 @@

Welcome to Rad-Lister!

The rad way to post free, unlimited ads!

+ + + + + + + + diff --git a/utils/Auth.php b/utils/Auth.php index b18a090..6785bad 100644 --- a/utils/Auth.php +++ b/utils/Auth.php @@ -16,6 +16,10 @@ public static function attempt($username, $password) { if($username == 'guest' && password_verify($password, self::$hash)) { $_SESSION['LOGGED_IN_USER'] = $username; + + $user = User::findUserByUsername($username); + + $_SESSION['LOGGED_IN_USER_ID'] = $user['id']; $message = "User $username logged in."; Log::info($message); return $message;