Parcourir la source

Ajout d'une vérification du statut abonné sur Asclepia dans la page de conversion classement -> tableau

Guichen il y a 4 ans
Parent
commit
534f0e5c42
2 fichiers modifiés avec 225 ajouts et 1 suppressions
  1. 221 0
      apiTableauFromClassement.php
  2. 4 1
      libs/_settings.sample.php

+ 221 - 0
apiTableauFromClassement.php

@@ -0,0 +1,221 @@
+<?php
+require_once "libs/fnMain.php";
+require_once "vendor/autoload.php";
+use \Firebase\JWT\JWT;
+
+/**************************
+ * Contrôle du token JWT
+ **************************/
+$isClassementValid = isset($_GET["classement"]) && (int) $_GET["classement"] !== 0;
+if (!isset($_GET["jwt"], $_SETTINGS["jwt_secret"])) {
+	echo "Manque d'arguments";
+	die();
+}
+
+try {
+	$jwtPayload = JWT::decode($_GET["jwt"], $_SETTINGS["jwt_secret"], ["HS256"]);
+} catch (\Firebase\JWT\SignatureInvalidException $e) {
+	echo "Identité invalide";
+	die();
+} catch (Exception $e) {
+	// echo $e->getMessage();
+	echo "Erreur lors de la vérification de l'identité";
+	die();
+}
+
+/**************************
+ * Contrôle du statut premium de l'utilisateur
+ **************************/
+$curl = curl_init();
+curl_setopt($curl, CURLOPT_URL, "https://interface.asclepia.io/auth/user");
+curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
+curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
+curl_setopt($curl, CURLOPT_TIMEOUT, 60);
+curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
+curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
+curl_setopt($curl, CURLOPT_HTTPHEADER, [
+	"authorization: JWT ".$_GET["jwt"],
+	"accept: application/json, text/plain, */*"
+]);
+$response = curl_exec($curl);
+
+// Vérification de la réponse
+$httpResponseCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
+if ($httpResponseCode === 200) {
+	$userData = json_decode($response, true);
+	if ($userData === NULL || !isset($userData["premium"])) {
+		echo "Echec de récupération des données utilisateur";
+		die();
+	}
+	
+	// Vérification du statut premium de l'utilisateur
+	if ($userData["premium"] === FALSE) {
+		echo "Abonnement requis.";
+		die();
+	}
+} else {
+	echo "Echec de l'authentification utilisateur";
+	die();
+}
+
+/**************************
+ * GENERATION DES CLASSEMENTS
+ **************************/
+if ($isClassementValid) {
+	$rangMoyen = (int) $_GET["classement"];
+	// HTML renvoyé au site
+	$html = <<<EOF
+	<link rel="stylesheet" href="assets/bootstrap.min.css">
+	<style>
+	/* Tableau */
+		table {
+		    font-size: 13px;
+		    text-align: center;
+		    border-collapse: collapse;
+		    white-space: nowrap;
+		    overflow: auto;
+		}
+		
+		th, td {
+		    padding: 2px;
+		    border: 1px solid black;
+		    --text-color: lightgrey;
+		}
+		
+		.all-choices {
+		    background-color: #28a745;
+		    /* color: #8fd19e; */
+		    color: white;
+		}
+		
+		.half-choices {
+		    background-color: #8fd19e;
+		    color: black;
+		}
+		
+		.last-choice {
+		    background-color: #ffdf7e;
+		    color: grey;
+		}
+		
+		.no-choice {
+		    background-color: #ed969e;
+		}
+		
+		.never-available {
+		    background-color: lightgrey;
+		}
+	</style>
+	<h2>Tableau de classement pour le rang {$rangMoyen}</h2>
+	<h3>Légende</h3>
+	<table>
+		<thead>
+		<th colspan="5">Légende (sur toutes les années)</th>
+		</thead>
+		<tbody>
+		<tr>
+			<td>Jamais proposé</td>
+			<td class="no-choice">Aucun choix</td>
+			<td class="last-choice">Une seule année</td>
+			<td class="half-choices">Plusieurs choix</td>
+			<td class="all-choices">Tous les choix</td>
+		</tr>
+		</tbody>
+	</table>
+	<p>
+		Chaque nombre dans chaque case correspond au nombre d'années où ce choix était dispo au classement rentré <br>
+	</p>
+EOF;
+	
+	$html .= "<h3>Résultats</h3>";
+	
+	// On va récupérer tous les rangs limited dispo triés par idChoix
+	$reqRL = $db->query("SELECT idChoix, annee, rangLimite FROM dataset");
+	$rangLimites = $reqRL->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC);
+	
+	// Récupération des datasets
+	$specialityDatasetAbrev = getCsvToArrayKeyValue($_SETTINGS["datasetFolder"]."/liste_specialites_abrev.csv");
+	$specialityDataset = getCsvToArrayKeyValue($_SETTINGS["datasetFolder"]."/liste_specialites.csv");
+	$cityDataset = getCsvToArrayKeyValue($_SETTINGS["datasetFolder"]."/liste_villes.csv");
+	$inputClassement = $rangMoyen;
+	
+	// Calcul et affichage des données
+	// Affichage de la 1ère ligne = Titre des colonnes
+	$html .= "<table><thead><tr><th></th>";
+	foreach ($specialityDatasetAbrev as $specialityName) {
+		$html .= "<th scope='col'>".$specialityName."</th>";
+	}
+	$html .= "</tr></thead><tbody>";
+	
+	// Affichage de la suite du tableau avec le calcul
+	foreach ($cityDataset as $cityId => $cityName) {
+		// On gère la séparation des différentes régions entre elles (représentées dans le csv villes par les lignes XX_:XX)
+		// La notation des lignes de séparation est sous forme XX_x car si on laissait XX, la fonction getCsvToArrayKeyValue
+		// ne retourne la position que pour la 1ère occurence de XX;XX
+		// La manière de fix ça de manière la plus opti et sale, c'est de faire un substr et de tester les 2 premiers charactères
+		// si ils sont égaux à XX, on fait une séparation
+		// TODO : Faire un parseur csv qui prend en compte chaque ligne du csv dans l'ordre du fichier
+		if (strpos($cityId, "XX") !== FALSE) {
+			$html .= "<tr><th></th>";
+			foreach ($specialityDatasetAbrev as $specialityName) {
+				$html .= "<th scope='col'>".$specialityName."</th>";
+			}
+			$html .= "</th>";
+			continue;
+		}
+		
+		$html .= "<tr><th scope='row'>".$cityName."</th>";
+		foreach ($specialityDatasetAbrev AS $specialityId => $specialityName) {
+			$idChoice = "0".$cityId.$specialityId;
+			
+			if (!isset($rangLimites[$idChoice])) {
+				$nbAnneePropose = 0;
+				$nbPossibleAnneePropose = 0;
+			} else {
+				$nbAnneePropose = count($rangLimites[$idChoice]);
+				$nbPossibleAnneePropose = 0;
+				foreach ($rangLimites[$idChoice] as $rangLimite) {
+					if ((int) $rangLimite["rangLimite"] >= $inputClassement) {$nbPossibleAnneePropose++;}
+				}
+			}
+			
+			// Affichage de la cellule
+			$html .= "<td class=\"";
+			switch (TRUE) {
+				case ($nbPossibleAnneePropose === 0 && $nbAnneePropose === 0):
+					$html .= "never-available";
+					break;
+				
+				case ($nbPossibleAnneePropose === 0):
+					$html .= "no-choice";
+					break;
+				
+				case ($nbAnneePropose > $nbPossibleAnneePropose && $nbPossibleAnneePropose === 1):
+					$html .= "last-choice";
+					break;
+				
+				case ($nbAnneePropose > $nbPossibleAnneePropose && $nbPossibleAnneePropose !== 1):
+					$html .= "half-choices";
+					break;
+				
+				case ($nbAnneePropose === $nbPossibleAnneePropose):
+					$html .= "all-choices";
+					break;
+			}
+			$html .= "\">";
+			if ($nbPossibleAnneePropose > 0) {
+				$html .= $nbPossibleAnneePropose;
+			}
+			$html .= "</td>";
+		}
+		$html .= "</tr>";
+	}
+	
+	$html .= "</tbody></table><br>";
+	
+	// Affichage du HTML demandé
+	echo $html;
+} else {
+	echo "Classement entré incorrect.";
+}
+

+ 4 - 1
libs/_settings.sample.php

@@ -13,5 +13,8 @@ return [
 	
 	// Réglage des seuils
 	"seuil_low" => 0.3,
-	"seuil_high" => 0.5
+	"seuil_high" => 0.5,
+	
+	// JWT secret key (cf. config django +++)
+	"jwt_secret" => ''
 ];