|
@@ -0,0 +1,38 @@
|
|
|
+<?php
|
|
|
+require_once "{$_SERVER['DOCUMENT_ROOT']}/dependencies.php";
|
|
|
+
|
|
|
+use Respect\Validation\Validator as v;
|
|
|
+
|
|
|
+// method must be POST
|
|
|
+methods(['POST' => v::keySet(
|
|
|
+ v::key('etiqueta', v::stringType()->notEmpty()),
|
|
|
+ v::key('host', v::stringType()->notEmpty()),
|
|
|
+ v::key('puerto', v::intType()->notEmpty()),
|
|
|
+ v::key('usuario', v::stringType()->notEmpty()),
|
|
|
+ v::key('base_datos', v::stringType()->notEmpty()),
|
|
|
+ v::key('password', v::stringType()->notEmpty()),
|
|
|
+ v::key('periodos', v::arrayType()->notEmpty()->each(v::intType()))
|
|
|
+)]);
|
|
|
+
|
|
|
+try {
|
|
|
+ $db->query('BEGIN');
|
|
|
+ $params = array(
|
|
|
+ 'etiqueta' => $_POST['etiqueta'],
|
|
|
+ 'host' => $_POST['host'],
|
|
|
+ 'puerto' => $_POST['puerto'],
|
|
|
+ 'postgres_user' => $_POST['usuario'],
|
|
|
+ 'postgres_dbname' => $_POST['base_datos'],
|
|
|
+ 'postgres_password' => $_POST['password'],
|
|
|
+ 'periodos_gema' => '{' . implode(',', $_POST['periodos']) . '}',
|
|
|
+ );
|
|
|
+ $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);
|
|
|
+
|
|
|
+ $db->query('COMMIT');
|
|
|
+} catch (\PDOException $th) {
|
|
|
+ $db->query('ROLLBACK');
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => $th->getMessage()]);
|
|
|
+ exit();
|
|
|
+}
|
|
|
+
|
|
|
+returnResponse(message: "Host {$_POST['etiqueta']} agregado correctamente");
|