diff --git a/Controller/NotesController.php b/Controller/NotesController.php index cdb8a6c..fdb3a81 100644 --- a/Controller/NotesController.php +++ b/Controller/NotesController.php @@ -18,10 +18,19 @@ class NotesController public function showNotes() { - $this->view->setVars([ - "notes" => $this->notesModel->selectNotes() - ]); + $sortBy = $_GET['sort_by'] ?? 'updated_at'; + $sortOrder = strtoupper($_GET['sort_order'] ?? 'DESC'); + // Validate sort order to prevent SQL injection + $sortOrder = in_array($sortOrder, ['ASC', 'DESC']) ? $sortOrder : 'DESC'; + + // Validate sort column to prevent SQL injection + $validSortColumns = ['id', 'title', 'updated_at', 'created_at']; + $sortBy = in_array($sortBy, $validSortColumns) ? $sortBy : 'updated_at'; + + $this->view->setVars([ + "notes" => $this->notesModel->selectNotesForUser(2, $sortBy, $sortOrder) //$_SESSION['user_id'] + ]); } } \ No newline at end of file diff --git a/Model/NotesModel.php b/Model/NotesModel.php index 35253c8..eb37be0 100644 --- a/Model/NotesModel.php +++ b/Model/NotesModel.php @@ -5,15 +5,12 @@ use ppa\Model\ParticipantModel; class NotesModel extends Database { - public function selectNotes() + public function selectNotesForUser($userid, $sortBy = 'updated_at', $sortOrder = 'DESC') { - $sortBy = 'updated_at'; - $sortOrder = 'DESC'; - - $sql = "SELECT id, title, content, created_at, updated_at - FROM notes - WHERE user_id = 2 - ORDER BY updated_at DESC"; + $sql = "SELECT n.*, u.username AS owner_username + FROM notes n + JOIN users u ON n.user_id = u.id + ORDER BY {$sortBy} {$sortOrder}"; $pdo = $this->linkDB(); diff --git a/Views/Notes/showNotes.phtml b/Views/Notes/showNotes.phtml index 3ae9e6a..e83b915 100644 --- a/Views/Notes/showNotes.phtml +++ b/Views/Notes/showNotes.phtml @@ -6,14 +6,58 @@ setSafeMode(true); - - foreach($notes as $n) { - echo '
'; - echo '

' . $n["title"] . '

' - . '

' . $parsedown->text($n['content'] ?? '') . '

'; - echo '
'; + + function isLoggedIn() { + return isset($_SESSION['user_id']); } -?> - + + function isAdmin() { + return isLoggedIn() && isset($_SESSION['role']) && $_SESSION['role'] === 'admin'; + } + function sanitize($data, $flags = ENT_QUOTES, $encoding = 'UTF-8') { + return htmlspecialchars((string)$data, $flags, $encoding); + } + + $sortBy = $_GET['sort_by'] ?? 'updated_at'; + $sortOrder = strtoupper($_GET['sort_order'] ?? 'DESC'); // Ensure uppercase for comparison + //$notes = NotesModel::selectNotesForUser(2, $sortBy, $sortOrder); //$_SESSION['user_id'] + ?> + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ID Title Owner Content (Preview)Last Edited Actions
+ text($note['content'] ?? '')); + $previewContent = mb_substr($plainTextContent, 0, 70); + echo sanitize($previewContent) . (mb_strlen($plainTextContent) > 70 ? '...' : ''); + ?> + + Edit + +
\ No newline at end of file