-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Undergraduate Student
committed
Dec 15, 2024
1 parent
b8c6c64
commit 050fffe
Showing
8 changed files
with
213 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Enable PHP error reporting | ||
php_flag display_startup_errors on | ||
php_flag display_errors on | ||
php_flag html_errors on | ||
php_flag log_errors on | ||
php_value error_log /home/student/iee/2020/iee2020202/logs/PHP_errors.log | ||
|
||
# Access control for this directory | ||
<Files "users.php"> | ||
Require all granted | ||
</Files> | ||
|
||
# URL Rewriting to handle dynamic routes for users.php | ||
RewriteEngine On | ||
RewriteRule ^users$ /ADISE24_DreamTeam/lib/users.php [L] | ||
|
||
# Only rewrite if the requested URL is not an existing file or directory | ||
RewriteCond %{REQUEST_FILENAME} !-f | ||
RewriteCond %{REQUEST_FILENAME} !-d | ||
|
||
# Rewrite all requests to users.php with the original requested path as a query parameter | ||
RewriteRule ^(.*)$ /~iee2020202/ADISE24_DreamTeam/lib/users.php?path=$1 [QSA,L] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<VirtualHost *:80> | ||
ServerAdmin [email protected] | ||
DocumentRoot /home/student/iee/2020/iee2020202/public_html/ADISE24_DreamTeam | ||
ServerName users.iee.ihu.gr | ||
ServerAlias www.users.iee.ihu.gr | ||
|
||
ErrorLog ${APACHE_LOG_DIR}/error.log | ||
CustomLog ${APACHE_LOG_DIR}/access.log combined | ||
|
||
<Directory /home/student/iee/2020/iee2020202/public_html/ADISE24_DreamTeam> | ||
Options Indexes FollowSymLinks | ||
AllowOverride All | ||
Require all granted | ||
</Directory> | ||
|
||
</VirtualHost> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
require_once "lib/dbconnect.php"; | ||
require_once "lib/users.php"; // Assuming you have user-related functions | ||
|
||
$method = $_SERVER['REQUEST_METHOD']; | ||
$request = explode('/', trim($_SERVER['PATH_INFO'],'/')); | ||
$input = json_decode(file_get_contents('php://input'), true); | ||
if ($input == null) { | ||
$input = []; | ||
} | ||
|
||
if (isset($_SERVER['HTTP_X_TOKEN'])) { | ||
$input['token'] = $_SERVER['HTTP_X_TOKEN']; | ||
} else { | ||
$input['token'] = ''; | ||
} | ||
|
||
// Main routing logic | ||
switch ($r = array_shift($request)) { | ||
case 'users': | ||
// Handle 'users' path | ||
switch ($b = array_shift($request)) { | ||
case '': | ||
// Show all users | ||
handle_users($method, $input); | ||
break; | ||
default: | ||
// Show user by a specific identifier (e.g., color or ID) | ||
handle_user($method, $b, $input); | ||
break; | ||
} | ||
break; | ||
|
||
case 'status': | ||
// Handle 'status' path | ||
if (sizeof($request) == 0) { | ||
handle_status($method); | ||
} else { | ||
header("HTTP/1.1 404 Not Found"); | ||
} | ||
break; | ||
|
||
default: | ||
// Return 404 if no valid endpoint is matched | ||
header("HTTP/1.1 404 Not Found"); | ||
echo json_encode(['errormesg' => "Endpoint not found."]); | ||
exit; | ||
} | ||
|
||
// Handler for 'users' endpoint (show all users) | ||
function handle_users($method, $input) { | ||
if ($method == 'GET') { | ||
show_users(); // Assuming you have a function to show users | ||
} else { | ||
header('HTTP/1.1 405 Method Not Allowed'); | ||
} | ||
} | ||
|
||
// Handler for 'user' endpoint (show a specific user) | ||
function handle_user($method, $identifier, $input) { | ||
if ($method == 'GET') { | ||
show_user($identifier); // Assuming you have a function to show a specific user | ||
} else { | ||
header('HTTP/1.1 405 Method Not Allowed'); | ||
} | ||
} | ||
|
||
// Handler for 'status' endpoint | ||
function handle_status($method) { | ||
if ($method == 'GET') { | ||
show_status(); // Assuming you have a function to show status | ||
} else { | ||
header('HTTP/1.1 405 Method Not Allowed'); | ||
} | ||
} | ||
?> | ||
|
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,44 @@ | ||
<?php | ||
|
||
// Include the db_upass.php file to use the connection variables | ||
require_once "db_upass.php"; | ||
|
||
// no need fro you to see our hosr or db hehehe | ||
$host=$DB_HOST; | ||
$db = $DB; | ||
|
||
$user=$DB_USER; | ||
$pass=$DB_PASS; | ||
/** | ||
* Establishes a MySQL database connection using PDO. | ||
* | ||
* @return PDO | ||
*/ | ||
function getDatabaseConnection() { | ||
// Use the variables defined in db_upass.php | ||
global $DB_HOST, $DB, $DB_USER, $DB_PASS, $UNI_HOSTNAME, $UNI_SOCKET; | ||
|
||
// Define the custom port | ||
$port = 3333; | ||
|
||
// Check if we are on the university server | ||
if (gethostname() == $UNI_HOSTNAME) { | ||
// Use the socket path for the university server connection | ||
$dsn = "mysql:unix_socket=$UNI_SOCKET;dbname=$DB;charset=utf8mb4"; | ||
// print "Connected to the remote server successfully.<br>"; | ||
} else { | ||
// Standard connection string for local connection with custom port | ||
$dsn = "mysql:host=$DB_HOST;port=$port;dbname=$DB;charset=utf8mb4"; | ||
// print "Connected to the local server successfully.<br>"; | ||
} | ||
|
||
if(gethostname()== $UNI_HOSTNAME ) { | ||
$mysqli = new mysqli($host, $user, $pass, $db,null,$UNI_SOCKET); | ||
} else { | ||
$pass=null; | ||
$mysqli = new mysqli($host, $user, $pass, $db); | ||
try { | ||
// Create a new PDO instance and set error mode to exceptions | ||
$pdo = new PDO($dsn, $DB_USER, $DB_PASS); | ||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
// echo "Connected to the database successfully.<br>"; // Confirmation message | ||
return $pdo; // Return the PDO instance | ||
} catch (PDOException $e) { | ||
// Handle connection failure and show an error message | ||
die(json_encode(['success' => false, 'error' => 'Database connection failed: ' . $e->getMessage()])); | ||
} | ||
} | ||
|
||
if ($mysqli->connect_errno) { | ||
echo "Failed to connect to MySQL: (" . | ||
$mysqli->connect_errno . ") " . $mysqli->connect_error; | ||
|
||
}?> | ||
// Call the function to establish the connection | ||
getDatabaseConnection(); | ||
?> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,66 @@ | ||
<?php | ||
|
||
require_once 'dbconnect.php'; | ||
|
||
header('Content-Type: application/json'); | ||
|
||
// Function to parse the URL (path parameter from query string) | ||
function getPathSegments() { | ||
$path = isset($_GET['path']) ? $_GET['path'] : ''; | ||
$segments = explode('/', trim($path, '/')); | ||
return $segments; | ||
} | ||
|
||
// Function to get the database connection | ||
|
||
|
||
// RESTful Functions | ||
function show_users() { | ||
global $mysqli; | ||
$sql = 'select username,piece_color from players'; | ||
$st = $mysqli->prepare($sql); | ||
$st->execute(); | ||
$res = $st->get_result(); | ||
header('Content-type: application/json'); | ||
print json_encode($res->fetch_all(MYSQLI_ASSOC), JSON_PRETTY_PRINT); | ||
$pdo = getDatabaseConnection(); | ||
$sql = 'SELECT username, piece_color FROM players'; | ||
$stmt = $pdo->prepare($sql); | ||
$stmt->execute(); | ||
$res = $stmt->fetchAll(PDO::FETCH_ASSOC); | ||
echo json_encode($res, JSON_PRETTY_PRINT); | ||
} | ||
|
||
function show_user($piece_color) { | ||
$pdo = getDatabaseConnection(); | ||
$sql = 'SELECT username, piece_color FROM players WHERE piece_color = ?'; | ||
$stmt = $pdo->prepare($sql); | ||
$stmt->execute([$piece_color]); | ||
$res = $stmt->fetchAll(PDO::FETCH_ASSOC); | ||
echo json_encode($res, JSON_PRETTY_PRINT); | ||
} | ||
|
||
// Main Controller | ||
$method = $_SERVER['REQUEST_METHOD']; | ||
$segments = getPathSegments(); | ||
|
||
// Adjust paths for your directory structure | ||
$basePath = 'ADISE24_DreamTeam/lib/users'; | ||
|
||
// Check if the request matches the base path | ||
if ($segments[0] === 'users') { | ||
if ($method === 'GET') { | ||
if (count($segments) === 1) { | ||
// If no additional segment, call show_users() | ||
show_users(); | ||
} elseif (count($segments) === 2) { | ||
// If additional segment, call show_user($piece_color) | ||
$piece_color = $segments[1]; | ||
show_user($piece_color); | ||
} else { | ||
http_response_code(404); | ||
echo json_encode(["error" => "Invalid endpoint"]); | ||
} | ||
} else { | ||
http_response_code(405); | ||
echo json_encode(["error" => "Method not allowed"]); | ||
} | ||
} else { | ||
http_response_code(404); | ||
echo json_encode(["error" => "Endpoint not found"]); | ||
} | ||
?> | ||
|
||
?> |