dependencies.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. require_once 'vendor/autoload.php';
  3. session_start();
  4. function methods($methodSchemas)
  5. {
  6. $requestMethod = $_SERVER['REQUEST_METHOD'];
  7. if (!array_key_exists($requestMethod, $methodSchemas)) {
  8. http_response_code(405);
  9. echo json_encode(['error' => 'Method not allowed']);
  10. exit();
  11. }
  12. $data = json_decode(file_get_contents('php://input'), true);
  13. // Validate against the schema for the current request method
  14. $schema = $methodSchemas[$requestMethod];
  15. if (!$schema->validate($data)) {
  16. http_response_code(403);
  17. echo json_encode(['error' => 'Invalid input']);
  18. exit();
  19. }
  20. }
  21. function returnResponse($status = 200, $data = null, $error = null, $message = null)
  22. {
  23. header('Content-Type: application/json');
  24. http_response_code($status);
  25. $response = [];
  26. if ($error) {
  27. $response['error'] = true;
  28. if ($message) {
  29. $response['message'] = $message; // User-friendly error message
  30. }
  31. if ($data) {
  32. $response['details'] = $data; // Full error details (optional based on system config)
  33. }
  34. } else {
  35. $response['error'] = false;
  36. if ($data !== null) {
  37. $response['data'] = $data; // Data payload for successful response
  38. }
  39. if ($message) {
  40. $response['message'] = $message; // Success message or additional info
  41. }
  42. }
  43. echo json_encode($response);
  44. exit();
  45. }
  46. /*
  47. // Usage:
  48. // For a successful response with data
  49. returnResponse(200, ['id' => 1, 'name' => 'John Doe']);
  50. // For an error response with a message and full error details
  51. returnResponse(500, $exception->getTraceAsString(), true, 'Internal Server Error');
  52. // For a not found response with just a message
  53. returnResponse(404, null, true, 'The requested resource was not found.');
  54. */
  55. // Function server error
  56. function serverError($title, $message)
  57. {
  58. header('Content-Type: text/html');
  59. http_response_code(500);
  60. require "{$_SERVER['DOCUMENT_ROOT']}/action/error.php";
  61. exit();
  62. }
  63. // DOTENV: load environment variables from .env file
  64. try {
  65. $dotenv = Dotenv\Dotenv::createImmutable($_SERVER['DOCUMENT_ROOT']);
  66. $dotenv->load();
  67. } catch (Dotenv\Exception\InvalidPathException $e) {
  68. serverError('Error de configuración', 'No se pudo cargar el archivo de configuración');
  69. exit();
  70. }
  71. // POSTGRES: load PostgresDb class
  72. use \SeinopSys\PostgresDb;
  73. function makeConnection($hostOrConnectionString, $port = null, $dbname = null, $user = null, $password = null): PostgresDb
  74. {
  75. $connectionString = is_null($port) ? $hostOrConnectionString : "pgsql:host=$hostOrConnectionString;port=$port;dbname=$dbname;user=$user;password=$password";
  76. try {
  77. $pdo = new PDO($connectionString);
  78. $db = new PostgresDb();
  79. $db->setConnection($pdo);
  80. return $db;
  81. } catch (PDOException $e) {
  82. serverError('Error de conexión', 'No se pudo conectar a la base de datos');
  83. exit();
  84. }
  85. }
  86. # default DB connection
  87. $db = makeConnection($_ENV['POSTGRES_HOST'], $_ENV['POSTGRES_PORT'], $_ENV['POSTGRES_DBNAME'], $_ENV['POSTGRES_USER'], $_ENV['POSTGRES_PASSWORD']);
  88. $sgi_db = makeConnection($_ENV['SGI_POSTGRES_HOST'], $_ENV['SGI_POSTGRES_PORT'], $_ENV['SGI_POSTGRES_DBNAME'], $_ENV['SGI_POSTGRES_USER'], $_ENV['SGI_POSTGRES_PASSWORD']);