-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiLog.php
81 lines (69 loc) · 2.7 KB
/
apiLog.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
// include_once $_SERVER['DOCUMENT_ROOT'].'/photoforyou/assets/config/functions.inc.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/photoforyou/assets/config/tokenAdminAPI.inc.php';
// Ip/Adresse
$servernameDB = "localhost";
// identifiant
$usernameDB = "root";
// Mot de passe
$passwordDB = "";
// Nom de la base de données
$nameDB = "sab_photoforyou";
//Connexion en base de donnée
try {
$db = new PDO("mysql:host=$servernameDB:3306;dbname=$nameDB","$usernameDB","$passwordDB");
$db->exec('SET NAMES utf8');
}
catch(PDOException $e) {
echo "Erreur : ".$e->getMessage();
die();
}
function noToken() {
// Création de l'objet JSON d'erreur
$error = array(
'error' => array(
'code' => 401,
'message' => 'Unauthorized',
'details' => 'Invalid API token provided'
)
);
// Conversion de l'objet JSON en chaîne JSON
$json_error = json_encode($error);
// Envoi de la réponse HTTP avec le code d'erreur et le contenu JSON
http_response_code(401);
header('Content-Type: application/json');
return $json_error;
}
if(isset($_GET['token'])) {
if($tokenAdminAPI == $_GET['token']) {
// Requête SQL pour récupérer les informations de la table log
if(isset($_GET['table'])) {
$table = $_GET['table'];
$sql = "SELECT date, table_name, type, detail FROM log WHERE table_name = '$table' ORDER BY date DESC";
if($_GET['table'] == "all") {
$sql = "SELECT date, table_name, type, detail FROM log ORDER BY date DESC";
}
}
// Exécution de la requête SQL
$stmt = $db->query($sql);
// Récupération des résultats sous forme d'un tableau associatif
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Traitement des caractères d'échappement
$results = array_map(function($row) {
$row['detail'] = json_decode($row['detail']);
if (is_string($row['detail'])) {
$row['detail'] = str_replace('\\', '', $row['detail']);
$row['detail'] = json_encode($row['detail'], JSON_UNESCAPED_UNICODE);
}
return $row;
}, $results);
// Affichage des résultats au format JSON
header('Content-Type: application/json');
echo json_encode($results);
} else {
echo(noToken());
}
} else {
echo(noToken());
}
?>