152 lines
5.4 KiB
PHP
152 lines
5.4 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../vendor/autoload.php';
|
|
require_once __DIR__ . '/../db.php';
|
|
|
|
use App\Controllers\AuthController;
|
|
use App\Controllers\ProjectController;
|
|
use App\Controllers\TaskController;
|
|
|
|
// Simple routing
|
|
$requestUri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
|
|
// Serve HTML files for non-API routes
|
|
if (!str_starts_with($requestUri, '/api/')) {
|
|
switch ($requestUri) {
|
|
case '/':
|
|
case '/index.html':
|
|
readfile(__DIR__ . '/index.html');
|
|
exit;
|
|
case '/dashboard.html':
|
|
readfile(__DIR__ . '/dashboard.html');
|
|
exit;
|
|
case '/project.html':
|
|
readfile(__DIR__ . '/project.html');
|
|
exit;
|
|
default:
|
|
// Try to serve static file if it exists
|
|
$filePath = __DIR__ . $requestUri;
|
|
if (file_exists($filePath) && is_file($filePath)) {
|
|
$extension = pathinfo($filePath, PATHINFO_EXTENSION);
|
|
$mimeTypes = [
|
|
'css' => 'text/css',
|
|
'js' => 'application/javascript',
|
|
'json' => 'application/json',
|
|
'png' => 'image/png',
|
|
'jpg' => 'image/jpeg',
|
|
'jpeg' => 'image/jpeg',
|
|
'gif' => 'image/gif',
|
|
'svg' => 'image/svg+xml',
|
|
];
|
|
if (isset($mimeTypes[$extension])) {
|
|
header('Content-Type: ' . $mimeTypes[$extension]);
|
|
}
|
|
readfile($filePath);
|
|
exit;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Headers for JSON responses
|
|
header('Content-Type: application/json');
|
|
|
|
try {
|
|
switch (true) {
|
|
case $requestUri === '/api/register' && $method === 'POST':
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$authController = new AuthController();
|
|
echo json_encode($authController->register(
|
|
$data['name'],
|
|
$data['email'],
|
|
$data['password']
|
|
));
|
|
break;
|
|
|
|
case $requestUri === '/api/login' && $method === 'POST':
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$authController = new AuthController();
|
|
echo json_encode($authController->login(
|
|
$data['email'],
|
|
$data['password']
|
|
));
|
|
break;
|
|
|
|
case $requestUri === '/api/logout' && $method === 'POST':
|
|
$authController = new AuthController();
|
|
echo json_encode($authController->logout());
|
|
break;
|
|
|
|
case $requestUri === '/api/projects' && $method === 'GET':
|
|
if (!AuthController::isAuthenticated()) {
|
|
http_response_code(401);
|
|
echo json_encode(['error' => 'Unauthorized']);
|
|
break;
|
|
}
|
|
$projectController = new ProjectController();
|
|
echo json_encode($projectController->getUserProjects());
|
|
break;
|
|
|
|
case $requestUri === '/api/projects' && $method === 'POST':
|
|
if (!AuthController::isAuthenticated()) {
|
|
http_response_code(401);
|
|
echo json_encode(['error' => 'Unauthorized']);
|
|
break;
|
|
}
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$projectController = new ProjectController();
|
|
echo json_encode($projectController->createProject(
|
|
$data['name'],
|
|
$data['description']
|
|
));
|
|
break;
|
|
|
|
case preg_match('/^\/api\/projects\/(\d+)$/', $requestUri, $matches) && $method === 'GET':
|
|
if (!AuthController::isAuthenticated()) {
|
|
http_response_code(401);
|
|
echo json_encode(['error' => 'Unauthorized']);
|
|
break;
|
|
}
|
|
$projectController = new ProjectController();
|
|
echo json_encode($projectController->getProjectDetails($matches[1]));
|
|
break;
|
|
|
|
case $requestUri === '/api/tasks' && $method === 'POST':
|
|
if (!AuthController::isAuthenticated()) {
|
|
http_response_code(401);
|
|
echo json_encode(['error' => 'Unauthorized']);
|
|
break;
|
|
}
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$taskController = new TaskController();
|
|
echo json_encode($taskController->createTask(
|
|
$data['name'],
|
|
$data['description'],
|
|
$data['project_id'],
|
|
$data['status'] ?? 'created'
|
|
));
|
|
break;
|
|
|
|
case preg_match('/^\/api\/tasks\/(\d+)\/status$/', $requestUri, $matches) && $method === 'PUT':
|
|
if (!AuthController::isAuthenticated()) {
|
|
http_response_code(401);
|
|
echo json_encode(['error' => 'Unauthorized']);
|
|
break;
|
|
}
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$taskController = new TaskController();
|
|
echo json_encode($taskController->updateTaskStatus(
|
|
$matches[1],
|
|
$data['status']
|
|
));
|
|
break;
|
|
|
|
default:
|
|
http_response_code(404);
|
|
echo json_encode(['error' => 'Not Found']);
|
|
break;
|
|
}
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => $e->getMessage()]);
|
|
}
|