CSY2028-assignment-1/functions.php

166 lines
4.4 KiB
PHP
Raw Normal View History

2022-11-16 13:27:51 +00:00
<?php
function fetchCats() {
2022-11-20 13:20:58 +00:00
$pdo = startDB();
2022-11-16 19:17:35 +00:00
$stmt = $pdo->prepare('SELECT * FROM category');
2022-11-16 13:27:51 +00:00
$stmt->execute();
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
function adminCheck() {
if(isset($_SESSION['admin'])) {
if($_SESSION['admin'] != 'y') {
echo '<script>window.location.href = "../index.php";</script>';
}
}
else {
echo'<script>window.location.href = "../index.php";</script>';
}
}
2022-11-20 13:20:58 +00:00
2022-11-20 20:14:56 +00:00
function startDB() { // 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
function checkListing() {
if (!isset($_GET['listing_id'])) {
echo '<script>window.location.href = "index.php";</script>';
}
}
2022-11-20 21:12:58 +00:00
function checkId() {
if (!isset($_GET['user_id'])) {
echo '<script>window.location.href = "index.php";</script>';
}
}
2022-11-20 14:44:18 +00:00
function getListing() {
2022-11-20 18:51:17 +00:00
return getFirstAllMatches('auction', 'listing_id', $_GET['listing_id']);
2022-11-20 14:44:18 +00:00
}
function populateCatSelect() {
$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
function executeQuery($tableName, $colName, $constraintCol, $constraint) {
$pdo = startDB();
$stmt = $pdo->prepare('SELECT '. $colName .' FROM '.$tableName.' WHERE '. $constraintCol .' = :constraint');
$values = [
'constraint' => $constraint
];
$stmt->execute($values);
return $stmt;
}
function executeQueryWithoutConstraint($tableName, $colName) {
$pdo = startDB();
$stmt = $pdo->prepare('SELECT'.$colName.'FROM '.$tableName);
$stmt->execute();
return $stmt;
}
function getFirstMatch($tableName, $colName, $constraintCol, $constraint){
return executeQuery($tableName, $colName, $constraintCol, $constraint)->fetch();
}
function getEveryMatch($tableName, $colName, $constraintCol, $constraint){
return executeQuery($tableName, $colName, $constraintCol, $constraint)->fetchAll();
}
function executeAllQuery($tableName, $constraintCol, $constraint) {
return executeQuery($tableName, '*', $constraintCol, $constraint);
}
function getEveryAllMatches($tableName, $constraintCol, $constraint) {
return executeAllQuery($tableName, $constraintCol, $constraint)->fetchAll();
}
function getFirstAllMatches($tableName, $constraintCol, $constraint) {
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
?>