![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/.cursor-server/data/User/History/-4bae8054/ |
<?php
/**
* API RESPONSE STANDARDIZATION
* Provides consistent JSON API responses across all endpoints
*/
class ApiResponse {
/**
* Send success response
*/
public static function success($data = null, $message = 'Success', $code = 200) {
$response = [
'success' => true,
'message' => $message,
'data' => $data,
'timestamp' => time(),
'code' => $code
];
self::output($response, $code);
}
/**
* Send error response
*/
public static function error($message = 'Error', $code = 400, $details = null) {
$response = [
'success' => false,
'message' => $message,
'code' => $code,
'timestamp' => time()
];
if ($details) {
$response['details'] = $details;
}
self::output($response, $code);
}
/**
* Send validation error response
*/
public static function validationError($errors, $message = 'Validation failed') {
$response = [
'success' => false,
'message' => $message,
'errors' => $errors,
'code' => 422,
'timestamp' => time()
];
self::output($response, 422);
}
/**
* Send authentication error response
*/
public static function authError($message = 'Authentication required') {
self::error($message, 401);
}
/**
* Send permission error response
*/
public static function permissionError($message = 'Insufficient permissions') {
self::error($message, 403);
}
/**
* Send not found error response
*/
public static function notFound($message = 'Resource not found') {
self::error($message, 404);
}
/**
* Send rate limit error response
*/
public static function rateLimitError($message = 'Rate limit exceeded', $retryAfter = null) {
$response = [
'success' => false,
'message' => $message,
'code' => 429,
'timestamp' => time()
];
if ($retryAfter) {
$response['retry_after'] = $retryAfter;
}
self::output($response, 429);
}
/**
* Send paginated response
*/
public static function paginated($data, $page, $perPage, $total, $message = 'Success') {
$totalPages = ceil($total / $perPage);
$response = [
'success' => true,
'message' => $message,
'data' => $data,
'pagination' => [
'page' => (int)$page,
'per_page' => (int)$perPage,
'total' => (int)$total,
'total_pages' => (int)$totalPages,
'has_next' => $page < $totalPages,
'has_prev' => $page > 1
],
'timestamp' => time(),
'code' => 200
];
self::output($response, 200);
}
/**
* Output JSON response
*/
private static function output($response, $httpCode = 200) {
// Set HTTP status code
http_response_code($httpCode);
// Set JSON content type
header('Content-Type: application/json');
// Set CORS headers if needed
if (isset($_SERVER['HTTP_ORIGIN'])) {
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
}
// Output JSON
echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
exit;
}
/**
* Validate required fields
*/
public static function validateRequired($data, $requiredFields) {
$errors = [];
foreach ($requiredFields as $field) {
if (!isset($data[$field]) || empty($data[$field])) {
$errors[$field] = ucfirst(str_replace('_', ' ', $field)) . ' is required';
}
}
if (!empty($errors)) {
self::validationError($errors);
}
return true;
}
/**
* Validate email format
*/
public static function validateEmail($email) {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
self::validationError(['email' => 'Invalid email format']);
}
return true;
}
/**
* Validate integer range
*/
public static function validateInteger($value, $field, $min = null, $max = null) {
$int = filter_var($value, FILTER_VALIDATE_INT);
if ($int === false) {
self::validationError([$field => ucfirst($field) . ' must be a number']);
}
if ($min !== null && $int < $min) {
self::validationError([$field => ucfirst($field) . " must be at least $min"]);
}
if ($max !== null && $int > $max) {
self::validationError([$field => ucfirst($field) . " must be no more than $max"]);
}
return $int;
}
/**
* Validate string length
*/
public static function validateStringLength($value, $field, $minLength = null, $maxLength = null) {
$length = strlen($value);
if ($minLength !== null && $length < $minLength) {
self::validationError([$field => ucfirst($field) . " must be at least $minLength characters"]);
}
if ($maxLength !== null && $length > $maxLength) {
self::validationError([$field => ucfirst($field) . " must be no more than $maxLength characters"]);
}
return true;
}
/**
* Sanitize input data
*/
public static function sanitizeInput($data) {
$sanitized = [];
foreach ($data as $key => $value) {
if (is_string($value)) {
$sanitized[$key] = htmlspecialchars(trim($value), ENT_QUOTES, 'UTF-8');
} else {
$sanitized[$key] = $value;
}
}
return $sanitized;
}
}
// Helper functions for common API operations
function apiSuccess($data = null, $message = 'Success') {
ApiResponse::success($data, $message);
}
function apiError($message = 'Error', $code = 400) {
ApiResponse::error($message, $code);
}
function apiValidationError($errors, $message = 'Validation failed') {
ApiResponse::validationError($errors, $message);
}
function apiAuthError($message = 'Authentication required') {
ApiResponse::authError($message);
}
function apiPermissionError($message = 'Insufficient permissions') {
ApiResponse::permissionError($message);
}
function apiNotFound($message = 'Resource not found') {
ApiResponse::notFound($message);
}
function apiRateLimitError($message = 'Rate limit exceeded', $retryAfter = null) {
ApiResponse::rateLimitError($message, $retryAfter);
}
?>