Commit 2b46bf21 authored by Matthew Sellman's avatar Matthew Sellman

full files

parents
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CourseworkBlog</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<?php
//Lab Code
error_reporting( E_ALL );
ini_set( "display_errors", 1 );
include_once "views/admin/admin-navigation.php";
$dbInfo = "mysql:host=igor.gold.ac.uk;dbname=msell010_CourseworkBlog";
$dbUser = "msell010";
$dbPwd = "lemons";
try {
//try to create a database connection with a PDO object
$db = new PDO( $dbInfo, $dbUser, $dbPwd );
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
echo "<h1>We’re connected</h1>";
}catch ( Exception $e ) {
echo "<h1>Connection failed!</h1><p>$e</p>";
}
$navigation = isset( $_GET['page'] );
if ( $navigation ) {
//prepare to load corresponding controller
$contrl = $_GET['page'];
} else {
//or prepare to load default controller
$contrl = "entries";
}
include_once "controllers/$contrl.php";
?>
\ No newline at end of file
<?
// Lab code
include_once "models/Blog_Entry_Table.class.php";
$entryTable = new Blog_Entry_Table( $db );
$entryClicked = isset( $_GET['Unique_ID '] );
if ($entryClicked ) {
$entryId = $_GET['Unique_ID '];
$entryData = $entryTable->getEntry( $entryId );
// print_r($entryData);
include_once "views/entry-html.php";
} else {
$entries = $entryTable->getallentries();
include_once "views/list-entries-html.php";
}
?>
\ No newline at end of file
<?php
//lab code
include_once "models/Table.class.php";
include_once "models/Blog_Entry_Table.class.php";
$entryTable = new Blog_Entry_Table( $db );
//was editor form submitted?
$editorSubmitted = isset( $_POST['action'] );
if ( $editorSubmitted ) {
$buttonClicked = $_POST['action'];
$id = $_POST['Unique_ID'];
$save = ($buttonClicked === 'Save');
$insertNewEntry = ( $save and $id === '0' );
$updateEntry = ( $save and $insertNewEntry === false );
$deleteEntry = ($buttonClicked === 'Delete');
$title = $_POST['Entry_Title'];
$entry = $_POST['Entry_Text'];
if ( $insertNewEntry ) {
$savedEntryId = $entryTable->saveEntry( $title, $entry );
} else if ( $updateEntry ){
$entryTable->updateEntry( $id, $title, $entry );
$savedEntryId = $id;
} else if ( $deleteEntry ) {
$entryTable->deleteEntry( $id );
}
}
$entryRequested = isset( $_GET['Unique_ID'] );
$entrySaved = isset( $savedEntryId );
if ( $entryRequested ) {
$id = $_GET['Unique_ID'];
$entryData = $entryTable->getEntry( $id );
$entryData->entry_id = $id;
$entryData->message = "";
} else if ( $entrySaved ) {
$entryData = $entryTable->getEntry( $savedEntryId );
$entryData->message = "Entry was saved";
} else {
$entryData = new StdClass();
$entryData->Unique_ID = 0;
$entryData->Entry_Title = "";
$entryData->Entry_Text = "";
$entryData->message = "";
}
include_once "views/admin/editor-html.php";
?>
\ No newline at end of file
<?
//lab code
include_once "models/Table.class.php";
include_once "models/Blog_Entry_Table.class.php";
$entryTable = new Blog_Entry_Table( $db );
$allEntries = $entryTable->getAllEntries();
include_once "views/admin/entries-html.php";
?>
\ No newline at end of file
<?
include_once "models/Admin_Table.class.php";
$loginFormSubmitted = isset( $_POST['log-in'] );
if( $loginFormSubmitted ) {
$email = $_POST['email'];
$password = $_POST['password'];
$adminTable = new Admin_Table( $db );
try {
$adminTable->checkCredentials( $email, $password );
$admin->login();
} catch ( Exception $e ) {
echo $e->getMessage();
}
}
$loggingOut = isset ( $_POST['logout'] );
if ( $loggingOut ){
$admin->logout();
}
if (!$admin->isLoggedIn() ) {
include_once "views/admin/login-form-html.php";
}
\ No newline at end of file
<?php
include_once "models/Table.class.php";
include_once "models/Admin_Table.class.php";
$createNewAdmin = isset( $_POST['new-admin'] );
if( $createNewAdmin ) {
$newEmail = $_POST['Email'];
$newPassword = $_POST['Password'];
$adminTable = new Admin_Table($db);
try {
$adminTable->create( $newEmail, $newPassword );
$adminFormMessage = "New user created";
} catch ( Exception $e ) {
$adminFormMessage = $e->getMessage();
}
}
include_once "views/admin/new-admin-form-html.php";
\ No newline at end of file
DROP TABLE IF EXISTS Blog_Entry, Admin;
CREATE TABLE Blog_Entry(
Unique_ID INT AUTO_INCREMENT,
Entry_Title VARCHAR(140),
Entry_Text TEXT,
Date_Created TIMESTAMP,
PRIMARY KEY(Unique_ID)
);
CREATE TABLE Admin(
Admin_ID INT AUTO_INCREMENT,
Email TEXT,
Password VARCHAR(32),
PRIMARY KEY (Admin_ID)
);
\ No newline at end of file
/* Code used from lab - css to make the form look nicer */
form#editor{
width: 300px;
margin:0px;
padding:0px;
}
form#editor label, form#editor input[type='text']{
display:block;
}
form#editor #editor-buttons{
border:none;
text-align:right;
}
form#editor textarea, form#editor input[type='text']{
width:90%;
margin-bottom:2em;
}
form#editor textarea{
height:10em;
}
body {
margin: 0 auto;
padding: 0em 0em 1em;
max-width: 75.5em;
font-family: 'comic sans', sans-serif;
font-size: 1.2em;
line-height: 1.2em;
text-align:center;
background-color: royalblue;
border-style: solid;
border-width: 0.3em;
}
#header {
position: relative;
top: 0;
left: 0;
right:0;
height: 5em;
width: 100%;
background-color: white;
}
#header2 {
position: relative;
top: 0;
left: 0;
right:0;
height: 2em;
width: 100%;
background-color: white;
}
<?php
error_reporting( E_ALL );
ini_set( "display_errors", 1 );
include_once "views/admin/admin-navigation.php";
//include_once "../../../coursework_blog_config.php";
$dbInfo = "mysql:host=igor.gold.ac.uk;dbname=msell010_CourseworkBlog";
$dbUser = "msell010";
$dbPwd = "lemons";
$db = new PDO( $dbInfo, $dbUser, $dbPwd);
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$title = "Coursework Blog";
$css="css/blog.css";
$embeddedStyle = "";
include_once "views/header.php";
include_once "controllers/blog.php";
include_once "views/footer.php";
?>
\ No newline at end of file
<?
class Admin_Table extends Table {
public function create ( $email, $password ) {
$this->checkEmail( $email );
$sql = "INSERT INTO Admin ( Email, Password )
VALUES( ?, SHA1(?) )";
$data= array( $email, $password );
$this->makeStatement( $sql, $data );
}
private function checkEmail ($email) {
$sql = "SELECT Email FROM Admin WHERE email = ?";
$data = array( $email );
$this->makeStatement( $sql, $data );
$statement = $this->makeStatement( $sql, $data );
if ( $statement->rowCount() === 1 ) {
$e = new Exception("Error: '$email' already used!");
throw $e;
}
}
public function checkCredentials ( $email, $password ){
$sql = "SELECT Email FROM Admin
WHERE Email = ? AND Password = SHA1(?)";
$data = array($email, $password);
$statement = $this->makeStatement( $sql, $data );
if ( $statement->rowCount() === 1 ) {
$out = true;
} else {
$loginProblem = new Exception( "login failed!" );
throw $loginProblem;
}
return $out;
}
}
\ No newline at end of file
<?php
class Admin_User {
public function __construct(){
session_start();
}
public function isLoggedIn(){
$sessionIsSet = isset( $_SESSION['logged_in'] );
if ( $sessionIsSet ) {
$out = $_SESSION['logged_in'];
} else {
$out = false;
}
return $out;
}
public function login () {
$_SESSION['logged_in'] = true;
}
public function logout () {
$_SESSION['logged_in'] = false;
}
}
\ No newline at end of file
<?php
//lab code
class Blog_Entry_Table {
private $db;
public function __construct ( $db ) {
$this->db = $db;
}
public function saveEntry ( $title, $entry ) {
$entrySQL = "INSERT INTO Blog_Entry ( Entry_Title, Entry_Text )
VALUES ( ?, ?)";
$entryStatement = $this->db->prepare( $entrySQL );
$formData = array( $title, $entry );
try{
$entryStatement->execute( $formData );
} catch (Exception $e){
$msg = "<p>You tried to run this sql: $entrySQL<p>
<p>Exception: $e</p>";
trigger_error($msg);
}
}
public function getAllEntries () {
$entrySQL = "SELECT Unique_ID, Entry_Title, SUBSTRING(Entry_Text, 1, 150) AS intro FROM Blog_Entry";
$statement = $this->db->prepare( $entrySQL );
try{
$statement->execute();
} catch (Exception $e){
$msg = "<p>You tried to run this sql: $entrySQL<p>
<p>Exception: $e</p>";
trigger_error($msg);
}
return $statement;
}
}
?>
\ No newline at end of file
<?php
public function saveEntry ( $title, $entry ) {
$entrySQL = "INSERT INTO blog_entry ( title, entry_text )
VALUES ( ?, ?)";
$entryStatement = $this->db->prepare( $entrySQL );
$formData = array( $title, $entry );
try{
$entryStatement->execute( $formData );
} catch (Exception $e){
$msg = "<p>You tried to run this sql: $entrySQL<p>
<p>Exception: $e</p>";
trigger_error($msg);
}
}
?>
\ No newline at end of file
<?php
class Table {
protected $db;
public function __construct ( $db ) {
$this->db = $db;
}
public function makeStatement( $sql, $data = NULL) {
$statement = $this->db->prepare( $sql );
try{
$statement->execute( $data );
} catch (Exception $e) {
$exceptionMessage = "<p>You tried to run this sql: $sql <p>
<p>Exception: $e</p>";
trigger_error($exceptionMessage);
}
return $statement;
}
}
\ No newline at end of file
<?php
class Uploader {
private $filename;
private $fileData;
private $destination;
public function __construct( $key ) {
$this->filename = $_FILES[$key]['name'];
$this->fileData = $_FILES[$key]['tmp_name'];
}
public function saveIn( $folder ) {
$this->destination = $folder;
}
public function save(){
$folderIsWritAble = is_writable( $this->destination );
if( $folderIsWritAble ){
$name = "$this->destination/$this->filename";
$success = move_uploaded_file( $this->fileData, $name );
} else {
trigger_error("cannot write to $this->destination");
$success = false;
}
return $success;
}
}
\ No newline at end of file
File added
<?php
//code from lab
$out = "
<nav id='admin-navigation'>
<a href='admin.php?page=entries'>All Entries</a>
<a href='admin.php?page=editor'>Editor</a>
<a href='index.php'>Index</a>
<a href='admin.php?page=users'>Create admin user</a>
<a href='admin.php?page=upload'>Upload an image</a>
</nav>";
echo $out;
?>
\ No newline at end of file
<?php
//Lab Code
$out = "
<form method='post' action='admin.php?page=editor' id='editor'>
<input type='hidden' name='Unique_ID' value='$entryData->Unique_ID'/>
<fieldset>
<legend>New Entry Submission</legend>
<label>Entry_Title</label>
<input type='text' name='Entry_Title' value='$entryData->Entry_Title' required maxlength='150' />
<label>Entry_Text</label>
<textarea name='Entry_Text'>'$entryData->Entry_Text</textarea>
<fieldset id='editor-buttons'>
<input type='submit' name='action' value='Delete' />
<input type='submit' name='action' value='Save' />
<p id='editor-message'>$entryData->message</p>
</fieldset>
</fieldset>
</form>
";
echo $out;
?>
\ No newline at end of file
<?php
//lab code
if ( isset( $allEntries ) === false ) {
trigger_error('views/admin/entries-html.php needs $allEntries');
}
$entriesAsHTML = "<ul>";
while ( $entry = $allEntries->fetchObject() ) {
$href = "admin.php?page=editor&amp;id=$entry->Unique_ID";
$entriesAsHTML .= "<li><a href='$href'>$entry->Entry_Title</a></li>";
}
$entriesAsHTML .= "</ul>";
echo $entriesAsHTML;
?>
\ No newline at end of file
<?
$out = " <form method='post' action='admin.php'>
<p>Login to access admin area</p>
<label>e-mail</label><input type='Email' name='email' required />
<label>password</label>
<input type='password' name='Password' required />
<input type='submit' value='login' name='log-in' />
</form>";
echo $out;
\ No newline at end of file
<?php
$out = "
<form method='post' action='admin.php'>
<label>logged in as administrator</label>
<input type='submit' value='log out' name='logout' />
</form>";
echo $out;
\ No newline at end of file
<?php
if( isset($adminFormMessage) === false ) {
$adminFormMessage = "";
}
$out = "<form method='post' action='admin.php?page=users'>
<fieldset>
<legend>Create new admin user</legend>
<label>e-mail</label>
<input type='email' name='Email' required/>
<label>password</label>
<input type='password' name='Password' required/>
<input type='submit' value='create user' name='new-admin'/>
</fieldset>
<p id='admin-form-message'>$adminFormMessage</p>
</form>";
echo $out;
<?php
//lab code
//check if required data is available
$entryDataFound = isset( $entryData );
if ( $entryDataFound === false ) {
trigger_error('views/entry-html.php needs an $entryData object');
}
//properties available in $entry: entry_id, title, entry_text, date_created
echo "<article>
<h1>$entryData->Entry_Title</h1>
<div class='date'>$entryData->Date_Created</div>
$entryData->Entry_Text
</article>";
\ No newline at end of file
<?php
$out="
</body>
</html>";
echo $out;
?>
\ No newline at end of file
<?php
//code from lab
$out ="<!DOCTYPE html>
<html>
<head>
<title>$title</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<link rel='stylesheet' type='text/css' href='$css'>
<link rel='stylesheet' type='text/css' href='$embeddedStyle'>
</head>";
echo $out;
?>
\ No newline at end of file
<?php
//Lab Code
$entriesFound = isset( $entries );
if ( $entriesFound === false ) {
trigger_error( 'views/list-entries-html.php needs $entries' );
}
$entriesHTML = "<ul id='blog-entries'>";
while ( $entry = $entries->fetchObject() ) {
$href = "index.php?page=blog&amp;id=$entry->Unique_ID";
//create an <li> for each of the entries
$entriesHTML .= "<li>
<h2>$entry->Entry_Title</h2>
<div>$entry->intro
<p><a href='$href'>Read more</a></p>
</div>
</li>";
}
$entriesHTML .= "</ul>";
echo $entriesHTML;
?>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment