Overview
Utility functions provide common helper functionality for data validation, formatting, and image handling across the Refugio system.Data Sanitization
sanitize_input
Sanitize user input to prevent XSS attacks.Raw input data to sanitize
Sanitized string safe for output
Process
- Removes leading/trailing whitespace with
trim() - Removes backslashes with
stripslashes() - Converts special characters to HTML entities with
htmlspecialchars()
Code Example
Implementation
Always sanitize user input before displaying it in HTML to prevent XSS vulnerabilities.
Date Utilities
formatear_fecha
Format a date from YYYY-MM-DD to DD/MM/YYYY for display.Date string in YYYY-MM-DD format
Formatted date in DD/MM/YYYY format
Code Example
Implementation
fecha_en_rango
Check if a date falls within a specific date range.Date to check (YYYY-MM-DD)
Range start date (YYYY-MM-DD)
Range end date (YYYY-MM-DD)
True if date is within range (inclusive), false otherwise
Code Example
Image Validation
validar_imagen
Validate an uploaded image file for security and format compliance.File array from $_FILES superglobal
Validation result:
valido(bool) - Whether file is validmensaje(string) - Validation messageextension(string) - File extension (only if valid)
Validation Rules
- Maximum size: 5MB (5,242,880 bytes)
- Allowed MIME types: image/jpeg, image/jpg, image/png, image/gif
- Allowed extensions: jpg, jpeg, png, gif
- Image verification: Must pass
getimagesize()check
Code Example
Validation Messages
| Condition | Message |
|---|---|
| Upload error | ”Error al subir el archivo” |
| File too large | ”El archivo es demasiado grande (máximo 5MB)“ |
| Invalid MIME type | ”Formato no permitido. Solo JPG, PNG o GIF” |
| Invalid extension | ”Extensión no permitida” |
| Not a real image | ”El archivo no es una imagen válida” |
| Valid | ”Imagen válida” |
Profile Photo Management
subir_foto_perfil
Upload and save a user’s profile photo.Database connection object
User ID
File array from $_FILES
Upload result:
exito(bool) - Success statusmensaje(string) - Result messageruta(string|null) - Saved file path
Process Flow
- Validates image using
validar_imagen() - Creates upload directory if it doesn’t exist
- Gets current photo to delete later
- Generates unique filename:
perfil_{id_usuario}_{timestamp}.{ext} - Moves uploaded file to
uploads/perfiles/ - Updates database with new photo path
- Deletes old photo file
- Rolls back on database error
Code Example
Directory Structure
obtener_foto_perfil
Get the file path of a user’s profile photo.Database connection object
User ID
Relative path to photo file, or null if no photo exists
Code Example
eliminar_foto_perfil
Delete a user’s profile photo from database and filesystem.Database connection object
User ID
Result:
exito(bool) - Success statusmensaje(string) - Result message
Process
- Gets current photo path from database
- Deletes physical file from filesystem
- Sets
foto_perfilcolumn to NULL in database - Returns result
Code Example
Complete Profile Photo Workflow
Security Best Practices
Data Sanitization:
- Sanitize ALL user input before display
- Use prepared statements for SQL queries (already implemented in all database functions)
- Apply
sanitize_input()to form data, URL parameters, and any external data - Remember: sanitization is for output, not storage