Procházet zdrojové kódy

Add moodle connection functionality and error handling

Alejandro Rosales před 1 rokem
rodič
revize
45180121f3
3 změnil soubory, kde provedl 45 přidání a 16 odebrání
  1. 28 0
      action/conectar_moodle.php
  2. 9 16
      action/desconectar.php
  3. 8 0
      action/error.php

+ 28 - 0
action/conectar_moodle.php

@@ -0,0 +1,28 @@
+<?php
+require_once "{$_SERVER['DOCUMENT_ROOT']}/dependencies.php";
+
+use Respect\Validation\Validator as v;
+
+/* methods(['POST' => v::keySet(
+    v::key('moodle-host', v::stringType()->notEmpty()),
+)]); */
+
+// method must be POST
+if (!isset($_SESSION['user'])) {
+    serverError(title: 'Error de conexión', message: 'No se ha iniciado sesión');
+    exit();
+}
+
+try {
+    // $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);
+    $string_connection = $db->querySingle(
+        "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 FROM moodle_host WHERE host = :host",
+        ['host' => $_POST['moodle-host']]
+    )['connection_string'];
+    $_SESSION['moodle_db'] = $string_connection;
+    header('Location: /');
+} catch (\PDOException $th) {
+    serverError(title: 'Error de conexión', message: $string_connection);
+
+    exit();
+}

+ 9 - 16
action/desconectar.php

@@ -5,23 +5,16 @@ use Respect\Validation\Validator as v;
 
 // only requre POST ['action' => 'desconectar' | 'sign-out']
 methods([
-    'POST' => v::keySet(
-        v::key('action', v::stringType()->notEmpty()->in(['desconectar', 'sign-out']))
-    ), 'GET' => v::alwaysValid()
+    'GET' => v::alwaysValid()
 ]);
 
-if ($_SERVER['REQUEST_METHOD'] === 'GET') {
-    session_destroy();
-} else
-    switch ($_POST['action']) {
-        case 'desconectar':
-            unset($_SESSION['page']);
-            unset($_SESSION['moodle_db']);
-            break;
-
-        case 'sign-out':
-            session_destroy();
-            break;
-    }
+switch ($_GET['action']) {
+    case 'desconectar':
+        unset($_SESSION['page'], $_SESSION['moodle_db']);
+        break;
 
+    case 'sign-out':
+        session_destroy();
+        break;
+}
 header('Location: /');

+ 8 - 0
action/error.php

@@ -15,6 +15,14 @@
                 <h1><?= $title ?></h1>
             </header>
             <p><?= $message ?></p>
+            <div class="grid">
+                <button class="primary" onclick="window.history.back()">
+                    Volver al inicio
+                </button>
+                <button class="secondary" onclick="window.location.href = '/action/desconectar.php?action=sign-out'">
+                    Cerrar sesión
+                </button>
+            </div>
         </article>
     </main>
 </body>