Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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.

# 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.
6 changes: 0 additions & 6 deletions adlister_login.php

This file was deleted.

14 changes: 14 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

// 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';
require_once 'utils/Auth.php';
require_once 'utils/Input.php';
require_once 'utils/Logger.php';

$dbc = DB::connect();
96 changes: 96 additions & 0 deletions database/ads_seeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

// Advertisements seeder

$ads = [
[
'item' => '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' => '',
],


]
16 changes: 16 additions & 0 deletions database/create_ads_table_migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

$dbc->exec('DROP TABLE IF EXISTS ads');

$query = 'CREATE TABLE ads (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
item VARCHAR(512) NOT NULL,
price INT UNSIGNED NOT NULL,
description TEXT,
location VARCHAR(256),
date_posted DATE,
user_id INT UNSIGNED,
PRIMARY KEY (id)
)';

$dbc->exec($query);
13 changes: 13 additions & 0 deletions database/create_users_table_migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php


$dbc->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);
6 changes: 6 additions & 0 deletions database/migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

require_once '../bootstrap.php';

require_once 'create_ads_table_migration.php';
require_once 'create_users_table_migration.php';
5 changes: 5 additions & 0 deletions database/seeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php


?>

15 changes: 15 additions & 0 deletions database/users_seeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

require_once '../bootstrap.php';

$user = [
'email' => $_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();

10 changes: 10 additions & 0 deletions env-template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return array(
'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' => ''
);
27 changes: 27 additions & 0 deletions models/Ad.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

require_once '../bootstrap.php';

class Ad extends BaseModel
{
protected static $table = 'ads';

protected function insert()
{
$query = 'INSERT INTO ads (item, price, description, location, date_posted)
VALUES (:item, :price, :description, :location, :date_posted)';
$stmt = self::$dbc->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()
{

}
}
62 changes: 12 additions & 50 deletions models/BaseModel.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

require 'adlister_login.php';
require_once '../bootstrap.php';

class Model {
class BaseModel {

protected static $dbc;
protected static $table;
Expand All @@ -27,10 +27,10 @@ private static function dbConnect()
if (!self::$dbc)
{
self::$dbc = new PDO(
'mysql:host='.DB_HOST.';
dbname='.DB_NAME,
DB_USER,
DB_PASS
'mysql:host='.$_ENV['DB_HOST'].';
dbname='.$_ENV['DB_NAME'],
$_ENV['DB_USER'],
$_ENV['DB_PASS']
);

// Tell PDO to throw exceptions on error
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -179,3 +140,4 @@ public function delete()

}
}

54 changes: 54 additions & 0 deletions models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

require_once '../bootstrap.php';

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['']
Loading