diff --git a/add_project.php b/add_project.php index 92746d6..7975510 100644 --- a/add_project.php +++ b/add_project.php @@ -1,15 +1,22 @@ 'Project name is required']); + echo json_encode(['success' => false, 'error' => 'Project name is required']); exit; } -$stmt = $pdo->prepare("INSERT INTO projects (name) VALUES (?)"); -$stmt->execute([trim($data['name'])]); +$stmt = $pdo->prepare("INSERT INTO projects (user_id, name) VALUES (?, ?)"); +$stmt->execute([$user_id, $name]); echo json_encode(['success' => true, 'id' => $pdo->lastInsertId()]); -?> diff --git a/add_subtask.php b/add_subtask.php index f320d92..8889f99 100644 --- a/add_subtask.php +++ b/add_subtask.php @@ -1,18 +1,32 @@ 'Invalid input']); - exit; + http_response_code(400); + echo json_encode(['success' => false, 'error' => 'Invalid input']); + exit; } -$stmt = $pdo->prepare("INSERT INTO subtasks (task_id, name) VALUES (?, ?)"); -$stmt->execute([$task_id, $name]); +// Ensure task belongs to this user +$stmt = $pdo->prepare("SELECT id FROM tasks WHERE id = ? AND user_id = ? LIMIT 1"); +$stmt->execute([$task_id, $user_id]); +if (!$stmt->fetchColumn()) { + http_response_code(403); + echo json_encode(['success' => false, 'error' => 'Forbidden']); + exit; +} + +$stmt = $pdo->prepare("INSERT INTO subtasks (user_id, task_id, name) VALUES (?, ?, ?)"); +$stmt->execute([$user_id, $task_id, $name]); echo json_encode(['success' => true, 'id' => $pdo->lastInsertId()]); -?> diff --git a/add_task.php b/add_task.php index 8119ffd..57c709c 100644 --- a/add_task.php +++ b/add_task.php @@ -1,5 +1,11 @@ 'Invalid input']); + echo json_encode(['success' => false, 'error' => 'Invalid input']); exit; } -$stmt = $pdo->prepare("INSERT INTO tasks (project_id, name) VALUES (?, ?)"); -$stmt->execute([$project_id, $name]); +// Ensure project belongs to this user +$stmt = $pdo->prepare("SELECT id FROM projects WHERE id = ? AND user_id = ? LIMIT 1"); +$stmt->execute([$project_id, $user_id]); +if (!$stmt->fetchColumn()) { + http_response_code(403); + echo json_encode(['success' => false, 'error' => 'Forbidden']); + exit; +} + +$stmt = $pdo->prepare("INSERT INTO tasks (user_id, project_id, name) VALUES (?, ?, ?)"); +$stmt->execute([$user_id, $project_id, $name]); echo json_encode(['success' => true, 'id' => $pdo->lastInsertId()]); -?> diff --git a/auth.php b/auth.php new file mode 100644 index 0000000..88ff806 --- /dev/null +++ b/auth.php @@ -0,0 +1,36 @@ + false, 'error' => 'Not authenticated']); + exit; + } +} + +function require_can_manage_settings(): void { + require_login(); + if (!current_user_can_manage_settings()) { + http_response_code(403); + header('Content-Type: application/json'); + echo json_encode(['success' => false, 'error' => 'Forbidden']); + exit; + } +} diff --git a/delete_project.php b/delete_project.php index d900e92..ad37160 100644 --- a/delete_project.php +++ b/delete_project.php @@ -1,6 +1,11 @@ 0) { - $pdo->prepare("DELETE FROM projects WHERE id = ?")->execute([$id]); + $pdo->prepare("DELETE FROM projects WHERE id = ? AND user_id = ?")->execute([$id, $user_id]); } diff --git a/delete_subtask.php b/delete_subtask.php index 32649c5..8282217 100644 --- a/delete_subtask.php +++ b/delete_subtask.php @@ -1,6 +1,11 @@ 0) { - $pdo->prepare("DELETE FROM subtasks WHERE id = ?")->execute([$id]); + $pdo->prepare("DELETE FROM subtasks WHERE id = ? AND user_id = ?")->execute([$id, $user_id]); } diff --git a/delete_task.php b/delete_task.php index d7265e3..6beb327 100644 --- a/delete_task.php +++ b/delete_task.php @@ -1,6 +1,11 @@ 0) { - $pdo->prepare("DELETE FROM tasks WHERE id = ?")->execute([$id]); + $pdo->prepare("DELETE FROM tasks WHERE id = ? AND user_id = ?")->execute([$id, $user_id]); } diff --git a/get_data.php b/get_data.php index 72446e9..4955210 100644 --- a/get_data.php +++ b/get_data.php @@ -1,19 +1,26 @@ query("SELECT * FROM projects ORDER BY sort_order ASC")->fetchAll(); +header('Content-Type: application/json'); +require_login(); + +$user_id = current_user_id(); + +$stmt = $pdo->prepare("SELECT * FROM projects WHERE user_id = ? ORDER BY sort_order ASC"); +$stmt->execute([$user_id]); +$projects = $stmt->fetchAll(); foreach ($projects as &$project) { - $stmt = $pdo->prepare("SELECT * FROM tasks WHERE project_id = ? ORDER BY created_at"); - $stmt->execute([$project['id']]); + $stmt = $pdo->prepare("SELECT * FROM tasks WHERE project_id = ? AND user_id = ? ORDER BY created_at"); + $stmt->execute([$project['id'], $user_id]); $project['tasks'] = $stmt->fetchAll(); foreach ($project['tasks'] as &$task) { - $stmt = $pdo->prepare("SELECT * FROM subtasks WHERE task_id = ? ORDER BY created_at"); - $stmt->execute([$task['id']]); + $stmt = $pdo->prepare("SELECT * FROM subtasks WHERE task_id = ? AND user_id = ? ORDER BY created_at"); + $stmt->execute([$task['id'], $user_id]); $task['subtasks'] = $stmt->fetchAll(); } } -echo json_encode($projects); -?> +echo json_encode($projects); \ No newline at end of file diff --git a/index.php b/index.php index b5db406..c532392 100644 --- a/index.php +++ b/index.php @@ -343,24 +343,78 @@ - -