dependencies.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. serverError('Error de validación', 'Los datos enviados no son válidos');
  17. exit();
  18. }
  19. }
  20. function returnResponse($status = 200, $data = null, $error = null, $message = null)
  21. {
  22. header('Content-Type: application/json');
  23. http_response_code($status);
  24. $response = [];
  25. if ($error) {
  26. $response['error'] = true;
  27. if ($message) {
  28. $response['message'] = $message; // User-friendly error message
  29. }
  30. if ($data) {
  31. $response['details'] = $data; // Full error details (optional based on system config)
  32. }
  33. } else {
  34. $response['error'] = false;
  35. if ($data !== null) {
  36. $response['data'] = $data; // Data payload for successful response
  37. }
  38. if ($message) {
  39. $response['message'] = $message; // Success message or additional info
  40. }
  41. }
  42. echo json_encode($response);
  43. exit();
  44. }
  45. /*
  46. // Usage:
  47. // For a successful response with data
  48. returnResponse(200, ['id' => 1, 'name' => 'John Doe']);
  49. // For an error response with a message and full error details
  50. returnResponse(500, $exception->getTraceAsString(), true, 'Internal Server Error');
  51. // For a not found response with just a message
  52. returnResponse(404, null, true, 'The requested resource was not found.');
  53. */
  54. // Function server error
  55. function serverError($title, $message)
  56. {
  57. header('Content-Type: text/html');
  58. http_response_code(500);
  59. require "{$_SERVER['DOCUMENT_ROOT']}/action/error.php";
  60. exit();
  61. }
  62. // DOTENV: load environment variables from .env file
  63. try {
  64. $dotenv = Dotenv\Dotenv::createImmutable($_SERVER['DOCUMENT_ROOT']);
  65. $dotenv->load();
  66. } catch (Dotenv\Exception\InvalidPathException $e) {
  67. serverError('Error de configuración', 'No se pudo cargar el archivo de configuración');
  68. exit();
  69. }
  70. // POSTGRES: load PostgresDb class
  71. use \SeinopSys\PostgresDb;
  72. function makeConnection($hostOrConnectionString, $port = null, $dbname = null, $user = null, $password = null): PostgresDb
  73. {
  74. $connectionString = is_null($port) ? $hostOrConnectionString : "pgsql:host=$hostOrConnectionString;port=$port;dbname=$dbname;user=$user;password=$password;sslmode=disable";
  75. try {
  76. $pdo = new PDO($connectionString);
  77. $db = new PostgresDb();
  78. $db->setConnection($pdo);
  79. return $db;
  80. } catch (PDOException $e) {
  81. // hide connection string password
  82. $connectionString = preg_replace('/password=[^;]+/', 'password=********', $connectionString);
  83. serverError('Error de conexión', 'No se pudo conectar a la base de datos: ' . $e->getMessage() . ' ' . $connectionString);
  84. exit();
  85. }
  86. }
  87. # default DB connection
  88. $db = makeConnection($_ENV['POSTGRES_HOST'], $_ENV['POSTGRES_PORT'], $_ENV['POSTGRES_DBNAME'], $_ENV['POSTGRES_USER'], $_ENV['POSTGRES_PASSWORD']);
  89. $sgi_db = makeConnection($_ENV['SGI_POSTGRES_HOST'], $_ENV['SGI_POSTGRES_PORT'], $_ENV['SGI_POSTGRES_DBNAME'], $_ENV['SGI_POSTGRES_USER'], $_ENV['SGI_POSTGRES_PASSWORD']);
  90. if (isset($_SESSION['moodle_db'])) {
  91. $moodle_db = makeConnection($_SESSION['moodle_db']);
  92. }