Files
Solis/internal/server/handlers.go
T
lucioandClaude Opus 4.6 737a48ac30 Fix security vulnerabilities: XSS, cookie hardening, rate limiter bypass, HSTS, and open redirect
- Fix XSS in showToast() by replacing innerHTML with safe DOM construction via textContent
- Add Secure flag to session and CSRF cookies when base_url uses HTTPS
- Prevent rate limiter bypass by only trusting X-Forwarded-For/X-Real-IP from private/loopback IPs
- Add Strict-Transport-Security header when running behind HTTPS
- Validate short link URLs to prevent open redirects (must be http, https, or relative path)
- Remove runtime config.toml file write from slug length update handler
- Pin Docker base image to alpine:3.20 instead of floating latest tag

Co-Authored-By: Claude Opus 4.6 <[email protected]>
2026-05-21 20:57:47 -07:00

1479 lines
41 KiB
Go

package server
import (
"database/sql"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"Solis/internal/db"
"Solis/internal/slug"
"github.com/google/uuid"
"golang.org/x/crypto/bcrypt"
)
// Global View Data context
type PageData struct {
CSRFToken string
ActiveNav string
Toast string
ToastType string
Error string
Items []db.Item
Item *db.Item
Locations []db.Location
Tags []db.Tag
Links []db.Link
EditingLocation *db.Location
EditingTag *db.Tag
EditingLink *db.Link
EditingItem *db.Item
FormMode bool
ViewMode bool
CustomFieldsRaw string
CustomFieldsMap map[string]string
SearchQuery string
SelectedStatus string
SelectedLocation int64
SelectedTag int64
CatalogJSON string
SlugLength int
ShowRemoved bool
IsAuthenticated bool
CurrentLocation *db.Location
Breadcrumbs []db.Location
ChildLocations []db.Location
}
// ==========================================
// 1. PUBLIC SHORT URL REDIRECT HANDLER
// ==========================================
func (s *Server) handleShortRedirect(w http.ResponseWriter, r *http.Request) {
slugVal := r.PathValue("slug")
if slugVal == "" {
http.NotFound(w, r)
return
}
slugVal = strings.ToLower(slugVal)
// Fetch redirect target by slug
link, err := s.db.GetLinkBySlug(slugVal)
if err != nil {
if err == sql.ErrNoRows {
http.NotFound(w, r)
return
}
http.Error(w, "Database error", http.StatusInternalServerError)
return
}
// Increment analytics click counter
_ = s.db.IncrementLinkClicks(link.Slug)
// Perform redirect
http.Redirect(w, r, link.URL, http.StatusMovedPermanently)
}
// ==========================================
// 2. AUTHENTICATION & LOGIN HANDLERS
// ==========================================
func (s *Server) handleLoginPage(w http.ResponseWriter, r *http.Request) {
// If already authenticated, redirect to Admin dashboard
if cookie, err := r.Cookie("session"); err == nil && s.sessions.Valid(cookie.Value) {
http.Redirect(w, r, "/admin", http.StatusSeeOther)
return
}
data := PageData{
CSRFToken: s.csrfToken(w, r),
}
s.render(w, r, "login.html", data)
}
func (s *Server) handleLogin(w http.ResponseWriter, r *http.Request) {
ip := clientIP(r)
if !s.limiter.Allow(ip) {
data := PageData{
CSRFToken: s.csrfToken(w, r),
Error: "Too many login attempts. Please wait.",
}
w.WriteHeader(http.StatusTooManyRequests)
s.render(w, r, "login.html", data)
return
}
// Validate CSRF
if !verifyCSRF(r) {
http.Error(w, "Invalid CSRF token", http.StatusForbidden)
return
}
username := r.FormValue("username")
password := r.FormValue("password")
// Verify Admin Credentials using Bcrypt
if username == s.cfg.Admin.Username && bcrypt.CompareHashAndPassword([]byte(s.cfg.Admin.PasswordHash), []byte(password)) == nil {
token, err := s.sessions.Create()
if err != nil {
http.Error(w, "Session creation failed", http.StatusInternalServerError)
return
}
http.SetCookie(w, &http.Cookie{
Name: "session",
Value: token,
Path: "/",
HttpOnly: true,
Secure: s.isHTTPS(),
SameSite: http.SameSiteLaxMode,
MaxAge: int(time.Duration(s.cfg.Admin.SessionHours) * time.Hour / time.Second),
})
http.Redirect(w, r, "/admin", http.StatusSeeOther)
return
}
data := PageData{
CSRFToken: s.csrfToken(w, r),
Error: "Invalid username or password.",
}
s.render(w, r, "login.html", data)
}
func (s *Server) handleLogout(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("session")
if err == nil {
s.sessions.Delete(cookie.Value)
}
// Expire session cookie
http.SetCookie(w, &http.Cookie{
Name: "session",
Value: "",
Path: "/",
HttpOnly: true,
MaxAge: -1,
})
http.Redirect(w, r, "/admin/login", http.StatusSeeOther)
}
// ==========================================
// 3. ASSET INVENTORY ITEMS CRUD HANDLERS
// ==========================================
func (s *Server) handleAdminItems(w http.ResponseWriter, r *http.Request) {
csrf := s.csrfToken(w, r)
showRemoved := r.URL.Query().Get("show_removed") == "1"
locations, _ := s.db.ListLocations(showRemoved)
tags, _ := s.db.ListTags(showRemoved)
// 1. DETAIL VIEW MODE (Upgrade / Redirect to clean URL format)
if viewIDStr := r.URL.Query().Get("view"); viewIDStr != "" {
id, err := strconv.ParseInt(viewIDStr, 10, 64)
if err == nil {
item, err := s.db.GetItem(id)
if err == nil {
var locSlug string
if item.LocationID.Valid {
loc, err := s.db.GetLocation(item.LocationID.Int64)
if err == nil {
locSlug = loc.Slug
}
}
if locSlug == "" {
locSlug = "default-room"
}
var itemSlug string
link, err := s.db.GetLinkByItemID(item.ID)
if err == nil {
itemSlug = link.Slug
} else {
itemSlug, _ = slug.GenerateItemSlug(item.ID, s.cfg.Slugs.Length, s.db.Conn)
if itemSlug != "" {
newURL := fmt.Sprintf("/%s/%s", locSlug, itemSlug)
_, _ = s.db.CreateLink(itemSlug, newURL, "admin", sql.NullInt64{Int64: item.ID, Valid: true})
}
}
if itemSlug != "" {
http.Redirect(w, r, fmt.Sprintf("/%s/%s?%s", locSlug, itemSlug, r.URL.RawQuery), http.StatusMovedPermanently)
return
}
}
}
}
// 2. LIST ALL ITEMS MODE (With filter / search criteria)
searchQuery := r.URL.Query().Get("search")
statusQuery := r.URL.Query().Get("status")
locationQuery := r.URL.Query().Get("location")
tagQuery := r.URL.Query().Get("tag")
allItems, err := s.db.ListItems(showRemoved)
if err != nil {
http.Error(w, "Failed to load assets", http.StatusInternalServerError)
return
}
// Apply filtering in memory (simple and extremely fast on SQLite dataset)
var filteredItems []db.Item
var locFilterID int64
if locationQuery != "" {
locFilterID, _ = strconv.ParseInt(locationQuery, 10, 64)
}
var tagFilterID int64
if tagQuery != "" {
tagFilterID, _ = strconv.ParseInt(tagQuery, 10, 64)
}
for _, item := range allItems {
matchesSearch := searchQuery == "" ||
strings.Contains(strings.ToLower(item.Name), strings.ToLower(searchQuery)) ||
strings.Contains(strings.ToLower(item.ModelNumber), strings.ToLower(searchQuery)) ||
strings.Contains(strings.ToLower(item.SerialNumber), strings.ToLower(searchQuery))
matchesStatus := statusQuery == "" || item.Status == statusQuery
matchesLocation := locationQuery == "" || (item.LocationID.Valid && item.LocationID.Int64 == locFilterID)
matchesTag := true
if tagFilterID > 0 {
matchesTag = false
for _, t := range item.Tags {
if t.ID == tagFilterID {
matchesTag = true
break
}
}
}
if matchesSearch && matchesStatus && matchesLocation && matchesTag {
filteredItems = append(filteredItems, item)
}
}
data := PageData{
CSRFToken: csrf,
ActiveNav: "items",
Items: filteredItems,
Locations: locations,
Tags: tags,
SearchQuery: searchQuery,
SelectedStatus: statusQuery,
SelectedLocation: locFilterID,
SelectedTag: tagFilterID,
ShowRemoved: showRemoved,
Toast: r.URL.Query().Get("toast"),
ToastType: r.URL.Query().Get("toast_type"),
}
s.render(w, r, "items.html", data)
}
func (s *Server) handleItemNew(w http.ResponseWriter, r *http.Request) {
locations, _ := s.db.ListLocations(false)
tags, _ := s.db.ListTags(false)
catalogJSON, err := json.Marshal(s.catalog)
if err != nil {
catalogJSON = []byte("[]")
}
data := PageData{
CSRFToken: s.csrfToken(w, r),
ActiveNav: "items",
Locations: locations,
Tags: tags,
FormMode: true,
CatalogJSON: string(catalogJSON),
}
s.render(w, r, "items.html", data)
}
func (s *Server) handleItemCreate(w http.ResponseWriter, r *http.Request) {
// Limit file uploads to prevent resource exhaustion attacks
r.Body = http.MaxBytesReader(w, r.Body, s.cfg.Upload.MaxSize*1024*1024)
if err := r.ParseMultipartForm(s.cfg.Upload.MaxSize * 1024 * 1024); err != nil {
http.Error(w, "Upload exceeds maximum limit", http.StatusBadRequest)
return
}
if r.FormValue("wizard_flow") == "true" {
category := r.FormValue("category")
itemVal := r.FormValue("item")
brand := r.FormValue("brand")
// 1. Determine the last used location ID
lastLocID, err := s.db.GetLastUsedLocationID()
if err != nil {
lastLocID = 1 // Default Room
}
locID := sql.NullInt64{Int64: lastLocID, Valid: true}
// 2. Format a beautiful name
var name string
if brand != "" && brand != "Other" && itemVal != "" && itemVal != "Other" {
name = brand + " " + itemVal
} else if itemVal != "" && itemVal != "Other" {
name = itemVal
} else if category != "" && category != "Other" {
// Find category name from ID
catName := category
for _, cat := range s.catalog {
if cat.ID == category {
catName = cat.Name
break
}
}
name = catName + " Asset"
} else {
name = "New Asset"
}
// 3. Resolve the room tag automatically
var tagIDs []int64
tagIDs = s.resolveRoomTag(lastLocID, tagIDs)
// 4. Construct category description
var desc string
catLabel := category
for _, cat := range s.catalog {
if cat.ID == category {
catLabel = cat.Name
break
}
}
desc = fmt.Sprintf("Automatically cataloged via Wizard: %s > %s > %s", catLabel, itemVal, brand)
item := &db.Item{
Name: name,
Description: desc,
Quantity: 1,
Status: "In Storage",
LocationID: locID,
}
// 5. Create the item record
createdItem, err := s.db.CreateItem(item, tagIDs)
if err != nil {
log.Printf("Error creating wizard item: %v", err)
http.Error(w, "Failed to create item", http.StatusInternalServerError)
return
}
// 6. Automatically generate and sync short link target as /{location_slug}/{item_slug}
s.syncItemShortlink(createdItem.ID, lastLocID)
// 7. Redirect straight to the Edit page so the user can modify any further details
http.Redirect(w, r, fmt.Sprintf("/admin/items/edit/%d?toast=Asset+created+successfully!+You+can+refine+its+details+below.&toast_type=success", createdItem.ID), http.StatusSeeOther)
return
}
name := r.FormValue("name")
qty, _ := strconv.Atoi(r.FormValue("quantity"))
desc := r.FormValue("description")
model := r.FormValue("model_number")
serial := r.FormValue("serial_number")
status := r.FormValue("status")
supplier := r.FormValue("supplier")
var locID sql.NullInt64
if locVal := r.FormValue("location_id"); locVal != "" {
id, _ := strconv.ParseInt(locVal, 10, 64)
locID = sql.NullInt64{Int64: id, Valid: true}
} else {
locID = sql.NullInt64{Int64: 1, Valid: true}
}
var price sql.NullFloat64
if priceVal := r.FormValue("purchase_price"); priceVal != "" {
p, _ := strconv.ParseFloat(priceVal, 64)
price = sql.NullFloat64{Float64: p, Valid: true}
}
var date sql.NullString
if dateVal := r.FormValue("purchase_date"); dateVal != "" {
date = sql.NullString{String: dateVal, Valid: true}
}
var warranty sql.NullInt64
if warrVal := r.FormValue("warranty_months"); warrVal != "" {
w, _ := strconv.ParseInt(warrVal, 10, 64)
warranty = sql.NullInt64{Int64: w, Valid: true}
}
// Parse tags checkboxes
var tagIDs []int64
for _, idStr := range r.MultipartForm.Value["tag_ids"] {
id, _ := strconv.ParseInt(idStr, 10, 64)
tagIDs = append(tagIDs, id)
}
// Resolve the room tag, prepend it, and deduplicate
tagIDs = s.resolveRoomTag(locID.Int64, tagIDs)
// Handle secure file uploads (photo and receipt)
imagePath, _ := s.saveUploadedFile(r, "image", true)
receiptPath, _ := s.saveUploadedFile(r, "receipt", false)
// Format custom fields input to SQLite JSON string
customFieldsJSON := parseCustomFields(r.FormValue("custom_fields"))
item := &db.Item{
Name: name,
Description: desc,
Quantity: qty,
ModelNumber: model,
SerialNumber: serial,
Status: status,
LocationID: locID,
PurchasePrice: price,
PurchaseDate: date,
WarrantyMonths: warranty,
Supplier: supplier,
CustomFields: customFieldsJSON,
ImagePath: imagePath,
ReceiptPath: receiptPath,
}
// Insert item into database
createdItem, err := s.db.CreateItem(item, tagIDs)
if err != nil {
log.Printf("Error creating item: %v", err)
http.Error(w, "Failed to create item", http.StatusInternalServerError)
return
}
// AUTOMATICALLY GENERATE & SYNC A SHORT LINK
s.syncItemShortlink(createdItem.ID, locID.Int64)
http.Redirect(w, r, "/admin?toast=Asset+created+successfully&toast_type=success", http.StatusSeeOther)
}
func (s *Server) handleItemEdit(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
item, err := s.db.GetItem(id)
if err != nil {
http.NotFound(w, r)
return
}
locations, _ := s.db.ListLocations(false)
tags, _ := s.db.ListTags(false)
// Translate custom JSON string back to raw lines for edit comfort
customFieldsRaw := formatCustomFields(item.CustomFields)
data := PageData{
CSRFToken: s.csrfToken(w, r),
ActiveNav: "items",
Locations: locations,
Tags: tags,
EditingItem: item,
CustomFieldsRaw: customFieldsRaw,
FormMode: true,
}
s.render(w, r, "items.html", data)
}
func (s *Server) handleItemSave(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
item, err := s.db.GetItem(id)
if err != nil {
http.NotFound(w, r)
return
}
r.Body = http.MaxBytesReader(w, r.Body, s.cfg.Upload.MaxSize*1024*1024)
if err := r.ParseMultipartForm(s.cfg.Upload.MaxSize * 1024 * 1024); err != nil {
http.Error(w, "Upload size exceeded", http.StatusBadRequest)
return
}
name := r.FormValue("name")
qty, _ := strconv.Atoi(r.FormValue("quantity"))
desc := r.FormValue("description")
model := r.FormValue("model_number")
serial := r.FormValue("serial_number")
status := r.FormValue("status")
supplier := r.FormValue("supplier")
var locID sql.NullInt64
if locVal := r.FormValue("location_id"); locVal != "" {
lid, _ := strconv.ParseInt(locVal, 10, 64)
locID = sql.NullInt64{Int64: lid, Valid: true}
} else {
locID = sql.NullInt64{Int64: 1, Valid: true}
}
var price sql.NullFloat64
if priceVal := r.FormValue("purchase_price"); priceVal != "" {
p, _ := strconv.ParseFloat(priceVal, 64)
price = sql.NullFloat64{Float64: p, Valid: true}
}
var date sql.NullString
if dateVal := r.FormValue("purchase_date"); dateVal != "" {
date = sql.NullString{String: dateVal, Valid: true}
}
var warranty sql.NullInt64
if warrVal := r.FormValue("warranty_months"); warrVal != "" {
w, _ := strconv.ParseInt(warrVal, 10, 64)
warranty = sql.NullInt64{Int64: w, Valid: true}
}
// Parse tags checkboxes
var tagIDs []int64
for _, idStr := range r.MultipartForm.Value["tag_ids"] {
tid, _ := strconv.ParseInt(idStr, 10, 64)
tagIDs = append(tagIDs, tid)
}
// Resolve the room tag, prepend it, and deduplicate
tagIDs = s.resolveRoomTag(locID.Int64, tagIDs)
// Securely upload new files and purge old replaced files
newImage, hasNewImg := s.saveUploadedFile(r, "image", true)
if hasNewImg && item.ImagePath != "" {
_ = os.Remove(filepath.Join(s.cfg.Upload.Dir, item.ImagePath))
}
if hasNewImg {
item.ImagePath = newImage
}
newReceipt, hasNewRec := s.saveUploadedFile(r, "receipt", false)
if hasNewRec && item.ReceiptPath != "" {
_ = os.Remove(filepath.Join(s.cfg.Upload.Dir, item.ReceiptPath))
}
if hasNewRec {
item.ReceiptPath = newReceipt
}
customFieldsJSON := parseCustomFields(r.FormValue("custom_fields"))
// Update record
item.Name = name
item.Description = desc
item.Quantity = qty
item.ModelNumber = model
item.SerialNumber = serial
item.Status = status
item.LocationID = locID
item.PurchasePrice = price
item.PurchaseDate = date
item.WarrantyMonths = warranty
item.Supplier = supplier
item.CustomFields = customFieldsJSON
err = s.db.UpdateItem(item, tagIDs)
if err != nil {
http.Error(w, "Failed to update item", http.StatusInternalServerError)
return
}
s.syncItemShortlink(item.ID, item.LocationID.Int64)
var locSlug string
if item.LocationID.Valid {
loc, err := s.db.GetLocation(item.LocationID.Int64)
if err == nil {
locSlug = loc.Slug
}
}
if locSlug == "" {
locSlug = "default-room"
}
var itemSlug string
link, err := s.db.GetLinkByItemID(item.ID)
if err == nil {
itemSlug = link.Slug
} else {
itemSlug = fmt.Sprintf("%d", item.ID)
}
http.Redirect(w, r, fmt.Sprintf("/%s/%s?toast=Asset+updated+successfully&toast_type=success", locSlug, itemSlug), http.StatusSeeOther)
}
func (s *Server) handleItemRemove(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
_ = s.db.SoftRemoveItem(id)
http.Redirect(w, r, "/admin?toast=Asset+moved+to+soon-to-be-deleted+queue&toast_type=success", http.StatusSeeOther)
}
func (s *Server) handleItemArchive(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
_ = s.db.ArchiveItem(id)
http.Redirect(w, r, "/admin?toast=Asset+archived+successfully&toast_type=success", http.StatusSeeOther)
}
func (s *Server) handleItemRestore(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
_ = s.db.RestoreItem(id)
http.Redirect(w, r, "/admin?show_removed=1&toast=Asset+restored+successfully&toast_type=success", http.StatusSeeOther)
}
func (s *Server) handleItemPermanentDelete(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
// Load item details first to delete uploaded file media from disk
item, err := s.db.GetItem(id)
if err == nil {
if item.ImagePath != "" {
_ = os.Remove(filepath.Join(s.cfg.Upload.Dir, item.ImagePath))
}
if item.ReceiptPath != "" {
_ = os.Remove(filepath.Join(s.cfg.Upload.Dir, item.ReceiptPath))
}
}
_ = s.db.DeleteItem(id)
http.Redirect(w, r, "/admin?show_removed=1&toast=Asset+deleted+forever+successfully&toast_type=success", http.StatusSeeOther)
}
// ==========================================
// 4. LOCATIONS MANAGEMENT HANDLERS
// ==========================================
func (s *Server) handleAdminLocations(w http.ResponseWriter, r *http.Request) {
csrf := s.csrfToken(w, r)
showRemoved := r.URL.Query().Get("show_removed") == "1"
locations, _ := s.db.ListLocations(showRemoved)
var editLoc *db.Location
if editIDStr := r.URL.Query().Get("edit"); editIDStr != "" {
id, err := strconv.ParseInt(editIDStr, 10, 64)
if err == nil {
editLoc, _ = s.db.GetLocation(id)
}
}
data := PageData{
CSRFToken: csrf,
ActiveNav: "locations",
Locations: locations,
EditingLocation: editLoc,
ShowRemoved: showRemoved,
Toast: r.URL.Query().Get("toast"),
ToastType: r.URL.Query().Get("toast_type"),
}
s.render(w, r, "locations.html", data)
}
func (s *Server) handleLocationCreate(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
desc := r.FormValue("description")
var parentID sql.NullInt64
if parentVal := r.FormValue("parent_id"); parentVal != "" {
id, _ := strconv.ParseInt(parentVal, 10, 64)
parentID = sql.NullInt64{Int64: id, Valid: true}
}
_, err := s.db.CreateLocation(name, desc, parentID, "")
if err != nil {
http.Error(w, "Failed to create location", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/admin/locations?toast=Location+created+successfully&toast_type=success", http.StatusSeeOther)
}
func (s *Server) handleLocationEdit(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
name := r.FormValue("name")
desc := r.FormValue("description")
var parentID sql.NullInt64
if parentVal := r.FormValue("parent_id"); parentVal != "" {
pid, _ := strconv.ParseInt(parentVal, 10, 64)
parentID = sql.NullInt64{Int64: pid, Valid: true}
}
_ = s.db.UpdateLocation(id, name, desc, parentID, "")
http.Redirect(w, r, "/admin/locations?toast=Location+updated+successfully&toast_type=success", http.StatusSeeOther)
}
func (s *Server) handleLocationRemove(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
_ = s.db.SoftRemoveLocation(id)
http.Redirect(w, r, "/admin/locations?toast=Location+moved+to+soon-to-be-deleted+queue&toast_type=success", http.StatusSeeOther)
}
func (s *Server) handleLocationArchive(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
_ = s.db.ArchiveLocation(id)
http.Redirect(w, r, "/admin/locations?toast=Location+archived+successfully&toast_type=success", http.StatusSeeOther)
}
func (s *Server) handleLocationRestore(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
_ = s.db.RestoreLocation(id)
http.Redirect(w, r, "/admin/locations?show_removed=1&toast=Location+restored+successfully&toast_type=success", http.StatusSeeOther)
}
func (s *Server) handleLocationPermanentDelete(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
_ = s.db.DeleteLocation(id)
http.Redirect(w, r, "/admin/locations?show_removed=1&toast=Location+deleted+forever+successfully&toast_type=success", http.StatusSeeOther)
}
// ==========================================
// 5. TAGS MANAGEMENT HANDLERS
// ==========================================
func (s *Server) handleAdminTags(w http.ResponseWriter, r *http.Request) {
csrf := s.csrfToken(w, r)
showRemoved := r.URL.Query().Get("show_removed") == "1"
tags, _ := s.db.ListTags(showRemoved)
var editTag *db.Tag
if editIDStr := r.URL.Query().Get("edit"); editIDStr != "" {
id, err := strconv.ParseInt(editIDStr, 10, 64)
if err == nil {
editTag, _ = s.db.GetTag(id)
}
}
data := PageData{
CSRFToken: csrf,
ActiveNav: "tags",
Tags: tags,
EditingTag: editTag,
ShowRemoved: showRemoved,
Toast: r.URL.Query().Get("toast"),
ToastType: r.URL.Query().Get("toast_type"),
}
s.render(w, r, "tags.html", data)
}
func (s *Server) handleTagCreate(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
color := r.FormValue("color")
desc := r.FormValue("description")
_, err := s.db.CreateTag(name, color, desc)
if err != nil {
http.Error(w, "Failed to create tag", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/admin/tags?toast=Tag+created+successfully&toast_type=success", http.StatusSeeOther)
}
func (s *Server) handleTagEdit(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
name := r.FormValue("name")
color := r.FormValue("color")
desc := r.FormValue("description")
_ = s.db.UpdateTag(id, name, color, desc)
http.Redirect(w, r, "/admin/tags?toast=Tag+updated+successfully&toast_type=success", http.StatusSeeOther)
}
func (s *Server) handleTagRemove(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
_ = s.db.SoftRemoveTag(id)
http.Redirect(w, r, "/admin/tags?toast=Tag+moved+to+soon-to-be-deleted+queue&toast_type=success", http.StatusSeeOther)
}
func (s *Server) handleTagArchive(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
_ = s.db.ArchiveTag(id)
http.Redirect(w, r, "/admin/tags?toast=Tag+archived+successfully&toast_type=success", http.StatusSeeOther)
}
func (s *Server) handleTagRestore(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
_ = s.db.RestoreTag(id)
http.Redirect(w, r, "/admin/tags?show_removed=1&toast=Tag+restored+successfully&toast_type=success", http.StatusSeeOther)
}
func (s *Server) handleTagPermanentDelete(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
_ = s.db.DeleteTag(id)
http.Redirect(w, r, "/admin/tags?show_removed=1&toast=Tag+deleted+forever+successfully&toast_type=success", http.StatusSeeOther)
}
// ==========================================
// 6. SHORTENER / LINK REDIRECT HANDLERS
// ==========================================
func (s *Server) handleAdminShortener(w http.ResponseWriter, r *http.Request) {
csrf := s.csrfToken(w, r)
showRemoved := r.URL.Query().Get("show_removed") == "1"
links, _ := s.db.ListLinks(showRemoved)
var editLink *db.Link
if editIDStr := r.URL.Query().Get("edit"); editIDStr != "" {
id, err := strconv.ParseInt(editIDStr, 10, 64)
if err == nil {
row := s.db.Conn.QueryRow("SELECT id, slug, url, item_id, created_by, clicks, lifecycle_state, created_at FROM links WHERE id = ?", id)
var l db.Link
var itemID sql.NullInt64
var ts string
if row.Scan(&l.ID, &l.Slug, &l.URL, &itemID, &l.CreatedBy, &l.Clicks, &l.LifecycleState, &ts) == nil {
l.ItemID = itemID
l.CreatedAt, _ = time.Parse("2006-01-02 15:04:05", ts)
editLink = &l
}
}
}
data := PageData{
CSRFToken: csrf,
ActiveNav: "shortener",
Links: links,
EditingLink: editLink,
SlugLength: s.cfg.Slugs.Length,
ShowRemoved: showRemoved,
Toast: r.URL.Query().Get("toast"),
ToastType: r.URL.Query().Get("toast_type"),
}
s.render(w, r, "shortener.html", data)
}
func (s *Server) handleLinkCreate(w http.ResponseWriter, r *http.Request) {
slugVal := strings.TrimSpace(r.FormValue("slug"))
urlVal := strings.TrimSpace(r.FormValue("url"))
if urlVal == "" {
http.Redirect(w, r, "/admin/shortener?toast=Destination+URL+is+required&toast_type=error", http.StatusSeeOther)
return
}
if !isValidRedirectURL(urlVal) {
http.Redirect(w, r, "/admin/shortener?toast=URL+must+use+http,+https,+or+be+a+relative+path&toast_type=error", http.StatusSeeOther)
return
}
// Auto generate slug if left blank
if slugVal == "" {
for i := 0; i < 10; i++ {
temp := slug.Generate(s.cfg.Slugs.Length)
exists, _ := s.db.SlugExists(temp)
if !exists {
slugVal = temp
break
}
}
}
// Validate slug syntax using glimmer validator
if err := slug.Validate(slugVal); err != nil {
http.Redirect(w, r, fmt.Sprintf("/admin/shortener?toast=%s&toast_type=error", err.Error()), http.StatusSeeOther)
return
}
// Validate duplicate slug
exists, _ := s.db.SlugExists(slugVal)
if exists {
http.Redirect(w, r, "/admin/shortener?toast=Short+slug+already+exists&toast_type=error", http.StatusSeeOther)
return
}
_, err := s.db.CreateLink(slugVal, urlVal, "admin", sql.NullInt64{Valid: false})
if err != nil {
http.Error(w, "Failed to create short link", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/admin/shortener?toast=Custom+shortlink+added+successfully&toast_type=success", http.StatusSeeOther)
}
func (s *Server) handleLinkSave(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
slugVal := strings.TrimSpace(r.FormValue("slug"))
urlVal := strings.TrimSpace(r.FormValue("url"))
// Validate slug
if err := slug.Validate(slugVal); err != nil {
http.Redirect(w, r, fmt.Sprintf("/admin/shortener?toast=%s&toast_type=error", err.Error()), http.StatusSeeOther)
return
}
if !isValidRedirectURL(urlVal) {
http.Redirect(w, r, "/admin/shortener?toast=URL+must+use+http,+https,+or+be+a+relative+path&toast_type=error", http.StatusSeeOther)
return
}
// Check if this slug is taken by ANOTHER link ID
var existingID int64
err = s.db.Conn.QueryRow("SELECT id FROM links WHERE slug = ? AND id != ?", slugVal, id).Scan(&existingID)
if err == nil {
http.Redirect(w, r, "/admin/shortener?toast=Short+slug+already+taken&toast_type=error", http.StatusSeeOther)
return
}
_ = s.db.UpdateLink(id, slugVal, urlVal)
http.Redirect(w, r, "/admin/shortener?toast=Shortlink+updated+successfully&toast_type=success", http.StatusSeeOther)
}
func (s *Server) handleLinkRemove(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
_ = s.db.SoftRemoveLink(id)
http.Redirect(w, r, "/admin/shortener?toast=Shortlink+moved+to+soon-to-be-deleted+queue&toast_type=success", http.StatusSeeOther)
}
func (s *Server) handleLinkArchive(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
_ = s.db.ArchiveLink(id)
http.Redirect(w, r, "/admin/shortener?toast=Shortlink+archived+successfully&toast_type=success", http.StatusSeeOther)
}
func (s *Server) handleLinkRestore(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
_ = s.db.RestoreLink(id)
http.Redirect(w, r, "/admin/shortener?show_removed=1&toast=Shortlink+restored+successfully&toast_type=success", http.StatusSeeOther)
}
func (s *Server) handleLinkPermanentDelete(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
_ = s.db.DeleteLink(id)
http.Redirect(w, r, "/admin/shortener?show_removed=1&toast=Shortlink+deleted+forever+successfully&toast_type=success", http.StatusSeeOther)
}
func (s *Server) handleSlugLengthUpdate(w http.ResponseWriter, r *http.Request) {
lengthStr := strings.TrimSpace(r.FormValue("slug_length"))
length, err := strconv.Atoi(lengthStr)
if err != nil || length < 3 || length > 16 {
http.Redirect(w, r, "/admin/shortener?toast=Slug+length+must+be+between+3+and+16&toast_type=error", http.StatusSeeOther)
return
}
s.cfg.Slugs.Length = length
http.Redirect(w, r, "/admin/shortener?toast=Slug+length+updated+to+"+strconv.Itoa(length)+"+characters&toast_type=success", http.StatusSeeOther)
}
// ==========================================
// 7. LABELS & PRINT ENGINE HANDLER
// ==========================================
func (s *Server) handlePrintLabels(w http.ResponseWriter, r *http.Request) {
csrf := s.csrfToken(w, r)
items, err := s.db.ListItems(false)
if err != nil {
http.Error(w, "Failed to load labels", http.StatusInternalServerError)
return
}
data := PageData{
CSRFToken: csrf,
ActiveNav: "print",
Items: items,
}
s.render(w, r, "print_labels.html", data)
}
// ==========================================
// 8. SECURITY PRIVATE MEDIA UPLOAD HELPERS
// ==========================================
func (s *Server) saveUploadedFile(r *http.Request, fieldName string, isImage bool) (string, bool) {
file, header, err := r.FormFile(fieldName)
if err != nil {
return "", false // No file uploaded or empty
}
defer file.Close()
// Sniff MIME type using standard 512-byte detection
buffer := make([]byte, 512)
n, _ := file.Read(buffer)
mimeType := http.DetectContentType(buffer[:n])
file.Seek(0, io.SeekStart)
// Validate content types
if isImage {
switch mimeType {
case "image/jpeg", "image/png", "image/webp":
// Allow
default:
log.Printf("Rejected image upload. Invalid MIME type: %s", mimeType)
return "", false
}
} else {
switch mimeType {
case "image/jpeg", "image/png", "image/webp", "application/pdf":
// Allow
default:
log.Printf("Rejected receipt document. Invalid MIME type: %s", mimeType)
return "", false
}
}
// Generate safe, randomized filename (UUID) keeping original extension
ext := strings.ToLower(filepath.Ext(header.Filename))
if ext == "" {
// Fallback clean extensions
if mimeType == "application/pdf" {
ext = ".pdf"
} else if mimeType == "image/png" {
ext = ".png"
} else {
ext = ".jpg"
}
}
uniqueFilename := fmt.Sprintf("%s%s", uuid.New().String(), ext)
outPath := filepath.Join(s.cfg.Upload.Dir, uniqueFilename)
outFile, err := os.Create(outPath)
if err != nil {
log.Printf("Error creating upload output file: %v", err)
return "", false
}
defer outFile.Close()
_, err = io.Copy(outFile, file)
if err != nil {
log.Printf("Error writing file contents: %v", err)
return "", false
}
return uniqueFilename, true
}
// Custom Fields Textarea parser: translates key: value lines to SQLite JSON
func parseCustomFields(raw string) string {
lines := strings.Split(raw, "\n")
m := make(map[string]string)
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
parts := strings.SplitN(line, ":", 2)
if len(parts) == 2 {
key := strings.TrimSpace(parts[0])
val := strings.TrimSpace(parts[1])
if key != "" && val != "" {
m[key] = val
}
}
}
b, _ := json.Marshal(m)
return string(b)
}
// Translates SQLite JSON string back to Key: Value lines for easy text form editing
func formatCustomFields(jsonStr string) string {
if jsonStr == "" || jsonStr == "{}" {
return ""
}
var m map[string]string
if err := json.Unmarshal([]byte(jsonStr), &m); err != nil {
return ""
}
var sb strings.Builder
for k, v := range m {
sb.WriteString(fmt.Sprintf("%s: %s\n", k, v))
}
return sb.String()
}
func isValidRedirectURL(u string) bool {
return strings.HasPrefix(u, "/") || strings.HasPrefix(u, "http://") || strings.HasPrefix(u, "https://")
}
func (s *Server) resolveRoomTag(locationID int64, tagIDs []int64) []int64 {
return tagIDs
}
// render automatically injects IsAuthenticated based on the session token
func (s *Server) render(w http.ResponseWriter, r *http.Request, name string, data PageData) {
cookie, err := r.Cookie("session")
data.IsAuthenticated = (err == nil && s.sessions.Valid(cookie.Value))
if err := renderTemplate(w, name, data); err != nil {
log.Printf("Template execution error for %s: %v", name, err)
http.Error(w, "Template execution error", http.StatusInternalServerError)
}
}
// syncItemShortlink automatically synchronizes the shortlink URL target as /{location_slug}/{item_slug}
func (s *Server) syncItemShortlink(itemID int64, locID int64) {
var locSlug string
loc, err := s.db.GetLocation(locID)
if err == nil {
locSlug = loc.Slug
} else {
locSlug = "default-room"
}
link, err := s.db.GetLinkByItemID(itemID)
if err != nil {
if err == sql.ErrNoRows {
// Generate clean, collision-resistant slug
slugVal, err := slug.GenerateItemSlug(itemID, s.cfg.Slugs.Length, s.db.Conn)
if err == nil && slugVal != "" {
newURL := fmt.Sprintf("/%s/%s", locSlug, slugVal)
_, _ = s.db.CreateLink(slugVal, newURL, "admin", sql.NullInt64{Int64: itemID, Valid: true})
}
}
return
}
newURL := fmt.Sprintf("/%s/%s", locSlug, link.Slug)
_ = s.db.UpdateLink(link.ID, link.Slug, newURL)
}
// isDescendantOf recursively checks if a location is a child/descendant of another to prevent cyclic nesting loops
func (s *Server) isDescendantOf(parentID, childID int64) bool {
if parentID == childID {
return true
}
child, err := s.db.GetLocation(childID)
if err != nil || !child.ParentID.Valid {
return false
}
return s.isDescendantOf(parentID, child.ParentID.Int64)
}
func (s *Server) handlePublicFallback(w http.ResponseWriter, r *http.Request) {
path := r.PathValue("path")
path = strings.Trim(path, "/")
if path == "" {
http.Redirect(w, r, "/admin", http.StatusSeeOther)
return
}
parts := strings.Split(path, "/")
if len(parts) == 1 {
r.SetPathValue("location_slug", parts[0])
s.handleFolderView(w, r)
return
} else if len(parts) == 2 {
r.SetPathValue("location_slug", parts[0])
r.SetPathValue("item_slug", parts[1])
s.handlePublicItemView(w, r)
return
}
http.NotFound(w, r)
}
func (s *Server) handleFolderView(w http.ResponseWriter, r *http.Request) {
slugVal := r.PathValue("location_slug")
if slugVal == "" {
http.NotFound(w, r)
return
}
slugVal = strings.ToLower(slugVal)
loc, err := s.db.GetLocationBySlug(slugVal)
if err != nil {
if err == sql.ErrNoRows {
http.NotFound(w, r)
return
}
http.Error(w, "Database error", http.StatusInternalServerError)
return
}
cookie, err := r.Cookie("session")
isAuth := (err == nil && s.sessions.Valid(cookie.Value))
var childLocs []db.Location
var items []db.Item
var breadcrumbs []db.Location
var showRemoved bool
var allLocations []db.Location
if isAuth {
showRemoved = r.URL.Query().Get("show_removed") == "1"
childLocs, err = s.db.ListChildLocations(loc.ID, showRemoved)
if err != nil {
childLocs = []db.Location{}
}
items, err = s.db.ListItemsByLocation(loc.ID, showRemoved)
if err != nil {
items = []db.Item{}
}
// Build breadcrumbs recursively to the root (parentID = null)
current := loc
for {
breadcrumbs = append([]db.Location{*current}, breadcrumbs...)
if !current.ParentID.Valid {
break
}
parent, err := s.db.GetLocation(current.ParentID.Int64)
if err != nil {
break
}
current = parent
}
allLocations, _ = s.db.ListLocations(false)
} else {
// Guest user: return empty lists, hide folder structures and breadcrumbs
childLocs = []db.Location{}
items = []db.Item{}
breadcrumbs = []db.Location{}
allLocations = []db.Location{}
showRemoved = false
}
data := PageData{
CSRFToken: s.csrfToken(w, r),
ActiveNav: "locations",
CurrentLocation: loc,
ChildLocations: childLocs,
Items: items,
Breadcrumbs: breadcrumbs,
Locations: allLocations,
ShowRemoved: showRemoved,
}
s.render(w, r, "location_folder.html", data)
}
func (s *Server) handlePublicItemView(w http.ResponseWriter, r *http.Request) {
locationSlug := r.PathValue("location_slug")
itemSlug := r.PathValue("item_slug")
link, err := s.db.GetLinkBySlug(itemSlug)
if err != nil {
if err == sql.ErrNoRows {
http.NotFound(w, r)
return
}
http.Error(w, "Database error", http.StatusInternalServerError)
return
}
if !link.ItemID.Valid {
http.NotFound(w, r)
return
}
item, err := s.db.GetItem(link.ItemID.Int64)
if err != nil {
http.NotFound(w, r)
return
}
var correctLocationSlug string
if item.LocationID.Valid {
loc, err := s.db.GetLocation(item.LocationID.Int64)
if err == nil {
correctLocationSlug = loc.Slug
}
}
if correctLocationSlug == "" {
correctLocationSlug = "default-room"
}
if correctLocationSlug != locationSlug {
http.Redirect(w, r, fmt.Sprintf("/%s/%s", correctLocationSlug, itemSlug), http.StatusMovedPermanently)
return
}
// Resolve the parent location details if available for breadcrumb fallback
var currentLoc *db.Location
if item.LocationID.Valid {
currentLoc, _ = s.db.GetLocation(item.LocationID.Int64)
}
var cfMap map[string]string
_ = json.Unmarshal([]byte(item.CustomFields), &cfMap)
locations, _ := s.db.ListLocations(false)
tags, _ := s.db.ListTags(false)
data := PageData{
CSRFToken: s.csrfToken(w, r),
ActiveNav: "items",
Item: item,
ViewMode: true,
CustomFieldsMap: cfMap,
Locations: locations,
Tags: tags,
CurrentLocation: currentLoc,
}
s.render(w, r, "items.html", data)
}
func (s *Server) handleItemMove(w http.ResponseWriter, r *http.Request) {
itemIDStr := r.FormValue("item_id")
locIDStr := r.FormValue("location_id")
itemID, err1 := strconv.ParseInt(itemIDStr, 10, 64)
locID, err2 := strconv.ParseInt(locIDStr, 10, 64)
if err1 != nil || err2 != nil {
http.Error(w, "Invalid item_id or location_id", http.StatusBadRequest)
return
}
err := s.db.MoveItemLocation(itemID, locID)
if err != nil {
http.Error(w, "Failed to move item: "+err.Error(), http.StatusInternalServerError)
return
}
s.syncItemShortlink(itemID, locID)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"status":"success","message":"Item moved successfully"}`))
}
func (s *Server) handleLocationMove(w http.ResponseWriter, r *http.Request) {
locIDStr := r.FormValue("location_id")
parentIDStr := r.FormValue("parent_id")
locID, err1 := strconv.ParseInt(locIDStr, 10, 64)
parentID, err2 := strconv.ParseInt(parentIDStr, 10, 64)
if err1 != nil {
http.Error(w, "Invalid location_id", http.StatusBadRequest)
return
}
if err2 != nil {
parentID = 0
}
if locID == parentID {
http.Error(w, "A location cannot be a parent of itself", http.StatusBadRequest)
return
}
if parentID > 0 {
if s.isDescendantOf(locID, parentID) {
http.Error(w, "Cycle detected: cannot move a location inside its own sub-folder", http.StatusBadRequest)
return
}
}
err := s.db.MoveLocationParent(locID, parentID)
if err != nil {
http.Error(w, "Failed to move location: "+err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"status":"success","message":"Location moved successfully"}`))
}