rendered paste body<?php session_start(); require_once('utility/sql.php'); require_once('utility/page_html.php'); if (isset($_GET['doThis'])) { $action = $_GET['doThis']; } if (isset($_GET['id'])) { $listid = $_GET['id']; } if (isset($_SESSION['msg'])) { echo $_SESSION['msg']; echo '<br>'; unset($_SESSION['msg']); } switch ($action) { case 'register': register(); break; case 'addboat': addboat(); break; case 'do_addboat': do_addboat(); break; case 'do_register': doRegister(); break; case 'login': login(); break; case 'doLogin': doLogin(); break; case 'logout': logout(); break; case 'listboats': case 'list': listBoats(); break; case 'showlistings': showlistings(); break; case 'editlisting': editlisting($listid); break; case 'do_editlisting': do_editlisting($listid); break; default: listBoats(); break; } function addboat() { global $db; HTML_Header(); if ($_SESSION['loggedIn'] != 1) { $_SESSION['msg'] = 'You are not logged in and cannot add a boat.'; header('Location: '.THIS_PAGE.'?doThis=listboats'); } $types = $db->select_all(array( 'id', 'name'), array( 'boat_types', )); $categories = $db->select_all(array( 'id', 'name', ), array( 'boat_categories')); $makes = $db->select_all(array( 'id', 'name', ), array( 'boat_makes')); echo '<form action="index.php?doThis=do_addboat" method="POST"><table>'; echo '<tr><td>Type:</td><td><select name="watertype">'; foreach ($types as $type) { echo '<option value="'.$type['id'].'">'.$type['name'].'</option>'; } echo '</select></td></tr>'; echo '<tr><td>Category:</td><td><select name="category">'; foreach ($categories as $category) { echo '<option value="'.$category['id'].'" '.$selected.'>'.$category['name'].'</option>'; } echo '</select></td></tr>'; echo '<tr><td>Make:</td><td><select name="make">'; foreach ($makes as $make) { echo '<option value="'.$make['id'].'" '.$selected.'>'.$make['name'].'</option>'; } echo '</select></td></tr>'; echo '<tr><td>Size (in inches):</td><td><input name="size" type="text"></td></tr>'; echo '<tr><td>Description:</td><td><input name="desc" type="textbox"></td></tr>'; echo '<tr><td>Title:</td><td><input name="title" type="text"></td></tr>'; echo '<tr><td>URL of Show Picture</td><td><input name="showpicture" type"text"></td></tr>'; echo '</table><br><input type="submit" value="submit"></form>'; HTML_Footer(); } function do_addboat() { global $db; $db->insert('boat_listings', array( 'userid' => $_SESSION['userid'], 'typeid' => $_POST['watertype'], 'title' => $_POST['title'], 'size' => $_POST['size'], 'makeid' => $_POST['make'], 'catid' => $_POST['category'], 'active' => 0, 'expire' => NULL, 'dealer' => 0, 'description' => $_POST['desc'], 'showpicture' => $_POST['showpicture'], 'picnumber' => 1, )); $_SESSION['msg'] = 'Succesfully added the Listing, awaiting approval.'; header('Location: '.THIS_PAGE.'?doThis=list'); } function showlistings() { global $db; HTML_Header(); $boats = $db->select_all(array( 'boat_listings.id', 'users.firstname', 'users.lastname', 'users.email', 'boat_listings.size', 'boat_listings.title', 'boat_listings.expire', 'boat_listings.showpicture', 'boat_listings.description', 'boat_makes.name' => 'make', 'boat_categories.name' => 'category', 'boat_types.name' => 'type', ), array( 'boat_listings', 'users' => array('users.id' => 'boat_listings.userid'), 'boat_makes' => array('boat_makes.id' => 'boat_listings.makeid'), 'boat_categories' => array('boat_categories.id' => 'boat_listings.catid'), 'boat_types' => array('boat_types.id' => 'boat_listings.typeid'), ), array( 'users.id' => $_SESSION['userid'] )); echo '<table><tr>'; echo '<td><a href="index.php?doThis=addboat">Add a Listing</a></td>'; echo '</tr></table>'; echo '<br><br><br><br>'; echo '<table>'; echo '<tr>'; echo '<th>Title</th>'; echo '<th>Description</th>'; echo '<th>Show Picture</th>'; echo '<th>Make</th>'; echo '<th>Category</th>'; echo '<th>Type</th>'; echo '<th>Size</th>'; echo '<th>Expires</th>'; echo '<th> </th>'; echo '</tr>'; foreach ($boats as $boat) { echo '<tr>'; echo '<td>'.$boat['title'].'</td>'; echo '<td>'.$boat['description'].'</td>'; echo '<td><img src="'.$boat['showpicture'].'"></td>'; echo '<td>'.$boat['make'].'</td>'; echo '<td>'.$boat['category'].'</td>'; echo '<td>'.$boat['type'].'</td>'; echo '<td>'.$boat['size'].'</td>'; echo '<td>'.$boat['expire'].'</td>'; echo '<td><a href="index.php?doThis=editlisting&id='.$boat['id'].'">Edit</a></td>'; echo '</tr>'; } echo '</table>'; HTML_Footer(); } function editlisting($listid) { global $db; HTML_Header(); $boat = $db->select(array( 'boat_listings.id', 'users.firstname', 'users.lastname', 'users.email', 'boat_listings.size', 'boat_listings.title', 'boat_listings.expire', 'boat_listings.showpicture', 'boat_listings.description', 'boat_makes.name' => 'make', 'boat_categories.name' => 'category', 'boat_types.name' => 'type', ), array( 'boat_listings', 'users' => array('users.id' => 'boat_listings.userid'), 'boat_makes' => array('boat_makes.id' => 'boat_listings.makeid'), 'boat_categories' => array('boat_categories.id' => 'boat_listings.catid'), 'boat_types' => array('boat_types.id' => 'boat_listings.typeid'), ), array( 'boat_listings.id' => $listid, )); $types = $db->select_all(array( 'id', 'name'), array( 'boat_types', )); $categories = $db->select_all(array( 'id', 'name', ), array( 'boat_categories')); $makes = $db->select_all(array( 'id', 'name', ), array( 'boat_makes')); echo '<form action="index.php?doThis=do_editlisting&id='.$listid.'" method="POST">'; echo '<table>'; echo '<tr><td>Type:</td><td><select name="watertype">'; foreach ($types as $type) { if ($type['name'] == $boat['type']) { $selected = 'selected="selected"'; } else { $selected = ''; } echo '<option value="'.$type['id'].'" '.$selected.'>'.$type['name'].'</option>'; } echo '</select></td></tr>'; echo '<tr><td>Category:</td><td><select name="category">'; foreach ($categories as $category) { if ($category['name'] == $boat['category']) { $selected = 'selected="selected"'; } else { $selected = ''; } echo '<option value="'.$category['id'].'" '.$selected.'>'.$category['name'].'</option>'; } echo '</select></td></tr>'; echo '<tr><td>Make:</td><td><select name="make">'; foreach ($makes as $make) { if ($make['name'] == $boat['make']) { $selected = 'selected="selected"'; } else { $selected = ''; } echo '<option value="'.$make['id'].'" '.$selected.'>'.$make['name'].'</option>'; } echo '</select></td></tr>'; echo '<tr><td>Size (in inches):</td><td><input name="size" type="text" value="'.$boat['size'].'"></td></tr>'; echo '<tr><td>Description:</td><td><input name="desc" type="textbox" value="'.$boat['description'].'"></td></tr>'; echo '<tr><td>Title:</td><td><input name="title" type="text" value="'.$boat['title'].'"></td></tr>'; echo '<tr><td>URL of Show Picture</td><td><input name="showpicture" type"text" value="'.$boat['showpicture'].'"></td></tr>'; echo '</table><br><input type="submit" value="submit">'; echo '</form>'; HTML_Footer(); } function do_editlisting($listid) { global $db; $db->update('boat_listings', array( 'typeid' => $_POST['watertype'], 'title' => $_POST['title'], 'size' => $_POST['size'], 'makeid' => $_POST['make'], 'catid' => $_POST['category'], 'description' => $_POST['desc'], 'showpicture' => $_POST['showpicture'], 'picnumber' => 1, ), array( 'id' => $listid, )); $_SESSION['msg'] = 'Succesfully modified the Listing, awaiting approval.'; if ($_SESSION['accesslevel'] == 'Administrator') { header('Location: '.THIS_PAGE.'?doThis=listboats'); } else { header('Location: '.THIS_PAGE.'?doThis=showlistings'); } } function listBoats() { global $db; HTML_Header(); if (empty($_POST)) { $boats = $db->select_all(array( 'boat_listings.id', 'users.firstname', 'users.lastname', 'users.email', 'boat_listings.size', 'boat_listings.title', 'boat_listings.expire', 'boat_listings.showpicture', 'boat_listings.description', 'boat_makes.name' => 'make', 'boat_categories.name' => 'category', 'boat_types.name' => 'type', ), array( 'boat_listings', 'users' => array('users.id' => 'boat_listings.userid'), 'boat_makes' => array('boat_makes.id' => 'boat_listings.makeid'), 'boat_categories' => array('boat_categories.id' => 'boat_listings.catid'), 'boat_types' => array('boat_types.id' => 'boat_listings.typeid'), ), array( 'boat_listings.active' => 1, )); } else { $boats = $db->select_all(array( 'boat_listings.id', 'users.firstname', 'users.lastname', 'users.email', 'boat_listings.size', 'boat_listings.title', 'boat_listings.expire', 'boat_listings.description', 'boat_listings.showpicture', 'boat_makes.name' => 'make', 'boat_categories.name' => 'category', 'boat_types.name' => 'type', ), array( 'boat_listings', 'users' => array('users.id' => 'boat_listings.userid'), 'boat_makes' => array('boat_makes.id' => 'boat_listings.makeid'), 'boat_categories' => array('boat_categories.id' => 'boat_listings.catid'), 'boat_types' => array('boat_types.id' => 'boat_listings.typeid'), ), array( 'boat_listings.active' => 1, 'boat_listings.typeid' => $_POST['watertype'], 'boat_listings.category' => $_POST['category'], )); } $types = $db->select_all(array( 'id', 'name'), array( 'boat_types', )); $categories = $db->select_all(array( 'id', 'name', ), array( 'boat_categories')); $makes = $db->select_all(array( 'id', 'name', ), array( 'boat_makes')); echo '<form method="POST" action="index.php?doThis=listboats">'; echo '<table border="1">'; echo '<tr><td>Water Type: <select name="watertype">'; foreach ($types as $type) { if ($_POST['watertype'] == $type['id']) { $selected = 'selected="selected"'; } else { $selected = ''; } echo '<option value="'.$type['id'].'" '.$selected.'>'.$type['name'].'</option>'; } echo '</select></td>'; echo '<td>Boat Category: <select name="category">'; foreach ($categories as $category) { if ($_POST['category'] == $category['id']) { $selected = 'selected="selected"'; } else { $selected = ''; } echo '<option value="'.$category['id'].'" '.$selected.'>'.$category['name'].'</option>'; } echo '</select></td>'; echo '<td>Size (in inches) Min = <input type="text" name="min" size="4" value="'.((isset($_POST['min']))?$_POST['min']:'').'"> || Max = <input type="text" name="max" size="4" value="'.((isset($_POST['max']))?$_POST['max']:'').'"></td>'; echo '<td>Boat Make: <select name="make">'; foreach ($makes as $make) { if ($_POST['make'] == $make['id']) { $selected = 'selected="selected"'; } else { $selected = ''; } echo '<option value="'.$make['id'].'" '.$selected.'>'.$make['name'].'</option>'; } echo '</select></td>'; echo '</tr>'; echo '</table><br>'; echo '<input type="submit" value="Search">'; echo '</form>'; echo '<br><br>'; if (empty($boats)) { echo '<h1>No Boats match your Criteria.</h1>'; } else { echo '<h1>Boat Listings Matching your Criteria</h1>'; echo '<table>'; echo '<tr>'; echo '<th>Title</th>'; echo '<th>Description</th>'; echo '<th>Picture</th>'; echo '<th>Make</th>'; echo '<th>Category</th>'; echo '<th>Type</th>'; echo '<th>Size</th>'; echo '<th>Expires</th>'; echo '<th> </th>'; echo '</tr>'; foreach ($boats as $boat) { if ($_SESSION['accesslevel'] == 'Administrator') { $editable = '<a href="index.php?doThis=editlisting&id='.$boat['id'].'">Edit</a>'; } else { $editable = ''; } echo '<tr>'; echo '<td>'.$boat['title'].'</td>'; echo '<td>'.$boat['description'].'</td>'; echo '<td><img src="'.$boat['showpicture'].'"></td>'; echo '<td>'.$boat['make'].'</td>'; echo '<td>'.$boat['category'].'</td>'; echo '<td>'.$boat['type'].'</td>'; echo '<td>'.$boat['size'].'</td>'; echo '<td>'.$boat['expire'].'</td>'; echo '<td>'.$editable.'</td>'; echo '</tr>'; } echo '</table>'; } HTML_Footer(); } function register() { HTML_Header(); echo '<h1>User Registration</h1>'; if (!isset($_GET['ans'])) { echo '<h2>User Agreement</h2>'; echo '<br'; echo 'Insert agreement here'; echo '<br>'; echo '<a href="index.php?doThis=register&ans=agree">Agree</a> '; echo ' <a href="index.php?doThis=register&ans=disagree">Disagree</a>'; } elseif ($_GET['ans'] == 'agree') { if (isset($_GET['error'])) { switch ($_GET['error']) { case 'username': echo '* Invalid Username, must be unique.'; break; case 'password': echo '* Invalid Password, must match the confirmation password.'; break; case 'name': echo '* Invalid First and/or Last Name'; break; case 'address': echo '* Invalid Address please complete all address fields accurately.'; break; case 'phone': echo '* Invalid Phone number'; break; case 'email': echo '* Invalid Email'; break; default: echo ''; break; } } echo '<form method="POST" action="index.php?doThis=do_register">'; echo 'First Name: <input type="text" name="firstname"><br>'; echo 'Last Name: <input type="text" name="lastname"><br>'; echo 'Username: <input type="text" name="username"><br>'; echo 'Password: <input type="password" name="password"><br>'; echo 'Password Confirmation: <input type="password" name="password2"><br>'; echo 'Street: <input type="text" name="street"><br>'; echo 'City: <input type="text" name="city"> State: <input type="text" name="state"><br>'; echo 'Zip Code: <input type="text" name="zipcode"><br>'; echo 'Phone Number: <input type="text" name="phone"><br>'; echo 'Email Address: <input type="text" name="email"><br>'; echo '<input type="submit" value="Submit"> <input type="reset" value="Clear">'; echo '</form>'; } else { echo '<a href="index.php?doThis=list">You need to agree inorder to join us.</a>'; } HTML_Footer(); } function doRegister() { global $db; //error handling //username if (!isset($_POST['username']) or empty($_POST['username'])) { header('Location: '.THIS_PAGE.'?doThis=register&ans=agree&error=username'); } //password if (!isset($_POST['password']) or !isset($_POST['password2']) or $_POST['password'] != $_POST['password2'] or empty($_POST['password']) or empty($_POST['password2'])) { header('Location: '.THIS_PAGE.'?doThis=register&ans=agree&error=password'); } //name if (!isset($_POST['firstname']) or !isset($_POST['lastname']) or empty($_POST['firstname']) or empty($_POST['lastname'])) { header('Location: '.THIS_PAGE.'?doThis=register&ans=agree&error=name'); } //address if (!isset($_POST['street']) or empty($_POST['street']) or !isset($_POST['city']) or empty($_POST['city']) or !isset($_POST['state']) or empty($_POST['state']) or !isset($_POST['zipcode']) or empty($_POST['zipcode'])) { header('Location: '.THIS_PAGE.'?doThis=register&ans=agree&error=address'); } //phone if (!isset($_POST['phone']) or empty($_POST['phone'])) { header('Location: '.THIS_PAGE.'?doThis=register&ans=agree&error=phone'); } if (!isset($_POST['email']) or empty($_POST['email'])) { header('Location: '.THIS_PAGE.'?doThis=register&ans=agree&error=email'); } if ($db->count('id', 'users', array('username' => $_POST['username'])) > 0) { header('Location: '.THIS_PAGE.'?doThis=register&ans=agree&error=username'); } $userid = $db->insert('users', array( 'firstname' => $_POST['firstname'], 'lastname' => $_POST['lastname'], 'address' => $_POST['street'], 'city' => $_POST['city'], 'state' => $_POST['state'], 'zipcode' => $_POST['zipcode'], 'email' => $_POST['email'], 'username' => $_POST['username'], 'password' => $_POST['password'], 'phone' => $_POST['phone'], 'user_type' => 1, )); $_SESSION['loggedIn'] = 1; $_SESSION['userid'] = $userid; $_SESSION['username'] = $_POST['username']; $_SESSION['password'] = $_POST['password']; $_SESSION['accesslevel'] = 'Member'; $_SESSION['msg'] = 'Successfully Added User'; header('Location: '.THIS_PAGE.'?doThis=list'); } function login() { global $db; HTML_Header(); echo '<form method="POST" action="index.php?doThis=doLogin">'; echo 'Username: <input name="username" type="text"><br>'; echo 'Password: <input name="password" type="password">'; echo '<input type="submit" value="login">'; echo '</form>'; HTML_Footer(); } function doLogin() { global $db; $count = $db->count('id', 'users', array('username' => $_POST['username'], 'password' => $_POST['password'])); if ($count > 0) { $_SESSION['loggedIn'] = 1; $_SESSION['userid'] = $db->select('id', 'users', array('username' => $_POST['username'])); $_SESSION['username'] = $_POST['username']; $_SESSION['password'] = $_POST['password']; $_SESSION['permissions'] = array(); if ($db->select('viewlistings', 'user_types', array('id' => $db->select('user_type', 'users', array('username' => $_POST['username'])))) > 0) { $_SESSION['permissions']['view'] = 1; } if ($db->select('editlistings', 'user_types', array('id' => $db->select('user_type', 'users', array('username' => $_POST['username'])))) > 0) { $_SESSION['permissions']['edit'] = 1; } if ($db->select('admintools', 'user_types', array('id' => $db->select('user_type', 'users', array('username' => $_POST['username'])))) > 0) { $_SESSION['permissions']['admin'] = 1; } if ($db->select('viewdealer', 'user_types', array('id' => $db->select('user_type', 'users', array('username' => $_POST['username'])))) > 0) { $_SESSION['permissions']['viewdealer'] = 1; } if ($db->select('editdealer', 'user_types', array('id' => $db->select('user_type', 'users', array('username' => $_POST['username'])))) > 0) { $_SESSION['permissions']['editdealer'] = 1; } $_SESSION['accesslevel'] = $db->select('name', 'user_types', array('id' => $db->select('user_type', 'users', array('username' => $_POST['username'])))); $_SESSION['msg'] = 'Successfully Logged In'; } else { $_SESSION['msg'] = 'Failed to Log in. Invalid Username and Password'; } header('Location: '.THIS_PAGE.'?doThis=list'); } function logout() { $_SESSION = array(); $_SESSION['msg'] = 'You have Logged Out'; header('Location: '.THIS_PAGE.'?doThis=listboats'); }?>