conectar_moodle.php 1.3 KB

1234567891011121314151617181920212223242526272829
  1. <?php
  2. require_once "{$_SERVER['DOCUMENT_ROOT']}/dependencies.php";
  3. use Respect\Validation\Validator as v;
  4. /* methods(['POST' => v::keySet(
  5. v::key('moodle-host', v::stringType()->notEmpty()),
  6. )]); */
  7. // method must be POST
  8. if (!isset($_SESSION['user'])) {
  9. serverError(title: 'Error de conexión', message: 'No se ha iniciado sesión');
  10. exit();
  11. }
  12. try {
  13. // $db->query("INSERT INTO moodle_host (etiqueta, host, puerto, postgres_user, postgres_dbname, postgres_password, periodos_gema) VALUES (:etiqueta, :host, :puerto, :postgres_user, :postgres_dbname, PGP_SYM_ENCRYPT(:postgres_password, '{$_ENV['KEY_ENCRYPT']}'), :periodos_gema)", $params);
  14. ['connection_string' => $string_connection, 'moodle_host_id' => $moodle_id] = $db->querySingle(
  15. "SELECT CONCAT('pgsql:host=', host, ';port=', puerto, ';dbname=', postgres_dbname, ';user=', postgres_user, ';password=', PGP_SYM_DECRYPT(postgres_password, '{$_ENV['KEY_ENCRYPT']}')) AS connection_string, moodle_host_id FROM moodle_host WHERE host = :host",
  16. ['host' => $_POST['moodle-host']]
  17. );
  18. $_SESSION['moodle_db'] = $string_connection;
  19. $_SESSION['moodle_id'] = $moodle_id;
  20. header('Location: /');
  21. } catch (\PDOException $th) {
  22. serverError(title: 'Error de conexión', message: $string_connection);
  23. exit();
  24. }