@@ -2,7 +2,9 @@
|
||||
require_once('UserDataSet.php');
|
||||
|
||||
/**
|
||||
* Authentication service for handling JWT-based authentication
|
||||
* Backend Authentication service for handling JWT authentication
|
||||
* https://jwt.io/introduction
|
||||
* This cost me blood, sweat and tears, mostly tears.
|
||||
*/
|
||||
class AuthService {
|
||||
private string $secretKey;
|
||||
@@ -14,7 +16,7 @@ class AuthService {
|
||||
* @throws Exception if OpenSSL extension is not loaded
|
||||
*/
|
||||
public function __construct() {
|
||||
// Load environment variables from .env file
|
||||
// Load environment variables from .env file (:D more configuration needs to be added to .env, but scope creep already huge)
|
||||
$envFile = __DIR__ . '/../.env';
|
||||
if (file_exists($envFile)) {
|
||||
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
@@ -37,14 +39,14 @@ class AuthService {
|
||||
$this->secretKey = getenv('JWT_SECRET_KEY') ?: 'your-256-bit-secret';
|
||||
$this->tokenExpiry = (int)(getenv('JWT_TOKEN_EXPIRY') ?: 3600);
|
||||
|
||||
// Verify OpenSSL extension is available
|
||||
// Verify OpenSSL extension is available. This should be on by default regardless, but just in case.
|
||||
if (!extension_loaded('openssl')) {
|
||||
throw new Exception('OpenSSL extension is required for JWT');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a JWT token for a user
|
||||
* Generates a JWT token
|
||||
* @param array $userData User information to include in token
|
||||
* @return string The generated JWT token
|
||||
*/
|
||||
@@ -52,6 +54,7 @@ class AuthService {
|
||||
$issuedAt = time();
|
||||
$expire = $issuedAt + $this->tokenExpiry;
|
||||
|
||||
// Create payload with user data
|
||||
$payload = [
|
||||
'iat' => $issuedAt,
|
||||
'exp' => $expire,
|
||||
@@ -101,7 +104,7 @@ class AuthService {
|
||||
$signature = hash_hmac('sha256', "$header.$payload", $this->secretKey, true);
|
||||
$signature = $this->base64UrlEncode($signature);
|
||||
|
||||
return "$header.$payload.$signature";
|
||||
return "$header.$payload.$signature"; //Wooooooo!!! JWT is a thing!
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -1,15 +1,11 @@
|
||||
<?php
|
||||
/**
|
||||
* Represents a facility in the EcoBuddy system
|
||||
* Represents a singular facility
|
||||
*
|
||||
* This class serves as a data model for facilities, encapsulating all
|
||||
* the properties and behaviours of a single facility. It follows the
|
||||
* Data Transfer Object (DTO) pattern that I learned about in my
|
||||
* software architecture module.
|
||||
* Data model for facilities, encapsulating all
|
||||
* properties and behaviours of a single facility.
|
||||
*
|
||||
* Each facility has location data, descriptive information, and metadata
|
||||
* about who contributed it. This class provides a clean interface for
|
||||
* accessing this data throughout the application.
|
||||
* Each facility has location data, descriptive info, and metadata.
|
||||
*/
|
||||
class FacilityData {
|
||||
/**
|
||||
@@ -77,10 +73,6 @@ class FacilityData {
|
||||
|
||||
/**
|
||||
* Gets the facility's title
|
||||
*
|
||||
* The title is the primary name or label for the facility that
|
||||
* is displayed to users in the interface.
|
||||
*
|
||||
* @return string The facility title
|
||||
*/
|
||||
public function getTitle() {
|
||||
@@ -89,10 +81,6 @@ class FacilityData {
|
||||
|
||||
/**
|
||||
* Gets the facility's category
|
||||
*
|
||||
* The category helps classify facilities by type, such as
|
||||
* recycling centre, community garden, etc.
|
||||
*
|
||||
* @return string The facility category
|
||||
*/
|
||||
public function getCategory() {
|
||||
@@ -101,10 +89,6 @@ class FacilityData {
|
||||
|
||||
/**
|
||||
* Gets the facility's current status
|
||||
*
|
||||
* The status indicates whether the facility is operational,
|
||||
* under maintenance, closed, etc.
|
||||
*
|
||||
* @return string The facility status
|
||||
*/
|
||||
public function getStatus() {
|
||||
@@ -113,10 +97,6 @@ class FacilityData {
|
||||
|
||||
/**
|
||||
* Gets the facility's description
|
||||
*
|
||||
* The description provides detailed information about the facility,
|
||||
* its purpose, services offered, etc.
|
||||
*
|
||||
* @return string The facility description
|
||||
*/
|
||||
public function getDescription() {
|
||||
@@ -125,9 +105,6 @@ class FacilityData {
|
||||
|
||||
/**
|
||||
* Gets the facility's house/building number
|
||||
*
|
||||
* This is part of the facility's address and helps locate it physically.
|
||||
*
|
||||
* @return string The house/building number
|
||||
*/
|
||||
public function getHouseNumber() {
|
||||
@@ -136,9 +113,6 @@ class FacilityData {
|
||||
|
||||
/**
|
||||
* Gets the facility's street name
|
||||
*
|
||||
* This is part of the facility's address and helps locate it physically.
|
||||
*
|
||||
* @return string The street name
|
||||
*/
|
||||
public function getStreetName() {
|
||||
@@ -147,9 +121,6 @@ class FacilityData {
|
||||
|
||||
/**
|
||||
* Gets the facility's county
|
||||
*
|
||||
* This is part of the facility's address and helps locate it physically.
|
||||
*
|
||||
* @return string The county
|
||||
*/
|
||||
public function getCounty() {
|
||||
@@ -158,9 +129,6 @@ class FacilityData {
|
||||
|
||||
/**
|
||||
* Gets the facility's town or city
|
||||
*
|
||||
* This is part of the facility's address and helps locate it physically.
|
||||
*
|
||||
* @return string The town or city
|
||||
*/
|
||||
public function getTown() {
|
||||
@@ -169,10 +137,6 @@ class FacilityData {
|
||||
|
||||
/**
|
||||
* Gets the facility's postcode
|
||||
*
|
||||
* This is part of the facility's address and helps locate it physically.
|
||||
* It's also useful for searching facilities by location.
|
||||
*
|
||||
* @return string The postcode
|
||||
*/
|
||||
public function getPostcode() {
|
||||
@@ -181,10 +145,6 @@ class FacilityData {
|
||||
|
||||
/**
|
||||
* Gets the facility's longitude coordinate
|
||||
*
|
||||
* This is used for displaying the facility on a map and
|
||||
* for calculating distances between facilities.
|
||||
*
|
||||
* @return float The longitude coordinate
|
||||
*/
|
||||
public function getLng() {
|
||||
@@ -193,10 +153,6 @@ class FacilityData {
|
||||
|
||||
/**
|
||||
* Gets the facility's latitude coordinate
|
||||
*
|
||||
* This is used for displaying the facility on a map and
|
||||
* for calculating distances between facilities.
|
||||
*
|
||||
* @return float The latitude coordinate
|
||||
*/
|
||||
public function getLat() {
|
||||
@@ -205,10 +161,6 @@ class FacilityData {
|
||||
|
||||
/**
|
||||
* Gets the username of the facility's contributor
|
||||
*
|
||||
* This tracks who added the facility to the system,
|
||||
* which is useful for auditing and attribution.
|
||||
*
|
||||
* @return string The contributor's username
|
||||
*/
|
||||
public function getContributor() {
|
||||
|
@@ -1,57 +0,0 @@
|
||||
<?php
|
||||
require_once('FacilityDataSet.php');
|
||||
class Paginator {
|
||||
protected $_pages, $_totalPages, $_rowLimit, $_pageMatrix, $_rowCount;
|
||||
|
||||
public function __construct($rowLimit, $dataset) {
|
||||
$this->_rowLimit = $rowLimit;
|
||||
$this->_totalPages = $this->calculateTotalPages($dataset['count']);
|
||||
$this->_rowCount = $dataset['count'];
|
||||
$this->_pages = $dataset['dataset'];
|
||||
$this->_pageMatrix = $this->Paginate();
|
||||
}
|
||||
public function getTotalPages() {
|
||||
return $this->_totalPages;
|
||||
}
|
||||
private function calculateTotalPages(int $count): int {
|
||||
return $count > 0 ? ceil($count / $this->_rowLimit) : 0;
|
||||
}
|
||||
|
||||
public function Paginate(): array {
|
||||
$pageMatrix = [];
|
||||
for ($i = 0; $i < $this->_totalPages; $i++) {
|
||||
$page = [];
|
||||
$start = $i * $this->_rowLimit;
|
||||
$end = min($start + $this->_rowLimit, $this->_rowCount); // Ensure within bounds
|
||||
|
||||
for ($j = $start; $j < $end; $j++) {
|
||||
$page[] = $this->_pages[$j];
|
||||
}
|
||||
|
||||
$pageMatrix[$i] = $page;
|
||||
}
|
||||
return $pageMatrix;
|
||||
}
|
||||
|
||||
public function getPageFromUri(): int {
|
||||
// Retrieve 'page' parameter and default to 0 if missing or invalid
|
||||
return filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, [
|
||||
'options' => ['default' => 0, 'min_range' => 0] // Default to 1 if invalid or missing
|
||||
]);
|
||||
}
|
||||
|
||||
public function getPage(int $pageNumber): array {
|
||||
|
||||
if ($pageNumber < 0 || $pageNumber >= $this->_totalPages) {
|
||||
return []; // Return an empty array if the page number is invalid
|
||||
}
|
||||
return $this->_pageMatrix[$pageNumber];
|
||||
}
|
||||
|
||||
public function countPageResults(int $pageNumber): int {
|
||||
if ($pageNumber < 0 || $pageNumber >= $this->_totalPages) {
|
||||
return 0; // Return 0 if the page number is invalid
|
||||
}
|
||||
return count($this->_pageMatrix[$pageNumber]);
|
||||
}
|
||||
}
|
@@ -70,16 +70,6 @@ class User {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the user's access level
|
||||
*
|
||||
* @param int $level The access level to set (admin = 1, regular user = 2)
|
||||
* @return void
|
||||
*/
|
||||
private function setAccessLevel($level) {
|
||||
$this->_accessLevel = $level;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the user's access level
|
||||
*
|
||||
@@ -128,33 +118,6 @@ class User {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs the user out
|
||||
*
|
||||
* Resets all user properties to their default values.
|
||||
* Note: This doesn't invalidate the JWT token - handled client-side
|
||||
* by removing the token from storage.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function logout() {
|
||||
// Reset user properties
|
||||
$this->_loggedIn = false;
|
||||
$this->_username = "None";
|
||||
$this->_userId = "0";
|
||||
$this->_accessLevel = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the user is currently logged in
|
||||
*
|
||||
* @return bool True if the user is logged in, false otherwise
|
||||
*/
|
||||
public function isLoggedIn(): bool
|
||||
{
|
||||
return $this->_loggedIn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Static method to check if a request is authenticated
|
||||
|
Reference in New Issue
Block a user