CSY2028-assignment-1/functions.php

163 lines
5.0 KiB
PHP
Raw Permalink Normal View History

2022-11-16 13:27:51 +00:00
<?php
2022-11-20 21:33:42 +00:00
function fetchCats() { //get all categories
2022-11-20 18:51:17 +00:00
$cats = executeQueryWithoutConstraint('category','*')->fetchAll();
2022-11-16 13:27:51 +00:00
return $cats;
2022-11-16 19:00:51 +00:00
}
2022-11-19 15:38:26 +00:00
2022-11-20 21:33:42 +00:00
function adminCheck() { //check to see if user is logged in as admin
2022-11-19 15:38:26 +00:00
if(isset($_SESSION['admin'])) {
if($_SESSION['admin'] != 'y') {
2022-11-20 21:33:42 +00:00
echo '<script>window.location.href = "../index.php";</script>'; //redirect
2022-11-19 15:38:26 +00:00
}
}
else {
2022-11-20 21:33:42 +00:00
echo'<script>window.location.href = "../index.php";</script>'; //redirect
2022-11-19 15:38:26 +00:00
}
}
2022-11-20 13:20:58 +00:00
2022-11-20 21:33:42 +00:00
function startDB() { //Create a db connection
// Code for connecting to the database from https://www.sitepoint.com/re-introducing-pdo-the-right-way-to-access-databases-in-php/
2022-11-20 13:20:58 +00:00
$server = 'mysql';
$username = 'student';
$password = 'student';
$schema = 'assignment1';
$pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password);
return $pdo;
}
2022-11-20 14:44:18 +00:00
2022-11-20 21:33:42 +00:00
function checkListing() { //check if the get variables contains listing_id
2022-11-20 14:44:18 +00:00
if (!isset($_GET['listing_id'])) {
echo '<script>window.location.href = "index.php";</script>';
}
}
2022-11-20 21:33:42 +00:00
function checkId() { //check if the get variables contains user_id
2022-11-20 21:12:58 +00:00
if (!isset($_GET['user_id'])) {
echo '<script>window.location.href = "index.php";</script>';
}
}
2022-11-20 21:33:42 +00:00
function getListing() { //get listing that matches listing_id stored in the get variables
2022-11-20 18:51:17 +00:00
return getFirstAllMatches('auction', 'listing_id', $_GET['listing_id']);
2022-11-20 14:44:18 +00:00
}
2022-11-20 21:33:42 +00:00
function populateCatSelect() { //Populate a select input with all categories
2022-11-20 14:44:18 +00:00
$cats = fetchCats();
$output = '';
foreach ($cats as &$cat) {
$output .= '<option value="'. $cat['category_id'] .'">'. $cat['name'] .'</option>';
}
return $output;
}
2022-11-20 18:51:17 +00:00
2022-11-20 21:33:42 +00:00
function executeQuery($tableName, $colName, $constraintCol, $constraint) { //execute a SELECT query that takes one constraint and one column name
2022-11-20 18:51:17 +00:00
$pdo = startDB();
$stmt = $pdo->prepare('SELECT '. $colName .' FROM '.$tableName.' WHERE '. $constraintCol .' = :constraint');
$values = [
'constraint' => $constraint
];
$stmt->execute($values);
return $stmt;
}
2022-11-20 21:33:42 +00:00
function executeQueryWithoutConstraint($tableName, $colName) { //execute a SELECT query with no constraint and one column name
2022-11-20 18:51:17 +00:00
$pdo = startDB();
$stmt = $pdo->prepare('SELECT'.$colName.'FROM '.$tableName);
$stmt->execute();
return $stmt;
}
2022-11-20 21:33:42 +00:00
function getFirstMatch($tableName, $colName, $constraintCol, $constraint){ //return the first match of an executeQuery
2022-11-20 18:51:17 +00:00
return executeQuery($tableName, $colName, $constraintCol, $constraint)->fetch();
}
2022-11-20 21:33:42 +00:00
function getEveryMatch($tableName, $colName, $constraintCol, $constraint){ //return every match of an executeQuery
2022-11-20 18:51:17 +00:00
return executeQuery($tableName, $colName, $constraintCol, $constraint)->fetchAll();
}
2022-11-20 21:33:42 +00:00
function executeAllQuery($tableName, $constraintCol, $constraint) { //execute a SELECT query with one constraint and all columns
2022-11-20 18:51:17 +00:00
return executeQuery($tableName, '*', $constraintCol, $constraint);
}
2022-11-20 21:33:42 +00:00
function getEveryAllMatches($tableName, $constraintCol, $constraint) { //return every match of an executeALlQuery
2022-11-20 18:51:17 +00:00
return executeAllQuery($tableName, $constraintCol, $constraint)->fetchAll();
}
2022-11-20 21:33:42 +00:00
function getFirstAllMatches($tableName, $constraintCol, $constraint) { //return the first match of an executeAllQuery
2022-11-20 18:51:17 +00:00
return executeAllQuery($tableName, $constraintCol, $constraint)->fetch();
}
2022-11-20 20:14:56 +00:00
function imageUpload($name) { //Code for uploading an image. Modified from https://www.w3schools.com/php/php_file_upload.asp
2022-11-20 19:58:30 +00:00
$imgDir = 'public/images/auctions/';
$file = $imgDir . $name;
$okFlag = true;
$fileType = strtolower($_FILES['auctionImg']['type']);
//check if file is actually an image
if(isset($_POST['submit'])) {
$sizeCheck = getimagesize($_FILES['auctionImg']['tmp_name']);
if (!$sizeCheck) {
$okFlag = false;
echo 'not an image';
}
}
//check if file exists
if(file_exists($file)) {
$okFlag = false;
echo 'already exists';
}
2022-11-20 18:51:17 +00:00
2022-11-20 19:58:30 +00:00
if($_FILES['auctionImg']['size'] > 10000000) {
$okFlag = false;
echo 'too big';
}
2022-11-20 18:51:17 +00:00
2022-11-20 19:58:30 +00:00
//check filetypes
$types = array('image/jpg','image/png','image/jpeg','image/gif');
if(!in_array($fileType, $types)) {
$okFlag = false;
echo 'wrong type';
}
2022-11-20 18:51:17 +00:00
2022-11-20 19:58:30 +00:00
if($okFlag) {
if (move_uploaded_file($_FILES['auctionImg']['tmp_name'], '../../'.$file)) {
return true;
}
else {
echo '<p>There was an error uploading your image</p>';
return false;
}
}
else {
echo '<p>There was an error uploading your image</p>';
return false;
}
}
2022-11-20 21:12:58 +00:00
function addUser($adminFlag) {
$pdo = startDB();
$stmt = $pdo->prepare('INSERT INTO users (first_name, last_name, email, password, admin)
VALUES (:first_name, :last_name, :email, :password, :admin)');
if ($adminFlag) {
$values = [
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'email' => $_POST['email'],
'admin' => 'y',
'password' => password_hash($_POST['password'], PASSWORD_DEFAULT)
];
}
else {
$values = [
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'email' => $_POST['email'],
'admin' => 'n',
'password' => password_hash($_POST['password'], PASSWORD_DEFAULT)
];
}
$stmt->execute($values);
}
2022-11-16 19:00:51 +00:00
?>