forked from Kccorp/PA_2021_ESGI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
138 lines (105 loc) · 4.7 KB
/
functions.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
require "conf.inc.php";
//fonction connexion BDD
function connectDB()
{
try{
$connection = new PDO(DBDRIVER.":host=".DBHOST.";port =".DBPORT.";dbname=".DBNAME , DBUSER, DBPWD, );
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}catch(Exception $e){
die("Erreur SQL : ". $e->getMessage());
}
return $connection;
}
//Fonction d'envoie d'image
function sendMail($email, $content){
//Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.gmail.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = '[email protected]'; //SMTP username
$mail->Password = EMAILPWD; //SMTP password
$mail->SMTPSecure = 'ssl'; //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 465; //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('[email protected]', 'no-reply');
$mail->addAddress($email); //Add a recipient
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
//$mail->addAttachment('Images\logo.svg', 'logo.svg'); //Optional name
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Ton code de validation PLAY.fr';
$mail->Body = $content;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
//echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
function getMessages(){
$connection = connectDB();
// Requête de la base de donnés pour sortir les 20 derniers messages
if (!empty($_SESSION['team']['id_matchs'])) {
$resultats = $connection->prepare("SELECT author, content, creationTime, ".PRE."Team.color from ".PRE."Message JOIN ".PRE."Team on ".PRE."Team.id_team=".PRE."Message.team where ".PRE."Message.matchs=:matchs ORDER BY creationTime DESC ;");
$resultats->execute(["matchs" => $_SESSION['team']['id_matchs']]);
// Extraction des résultats
$messages = $resultats->fetchAll(PDO::FETCH_ASSOC);
}else {
$resultats = $connection->prepare("SELECT author, content, creationTime, ".PRE."User.color from ".PRE."Message join ".PRE."User on ".PRE."Message.author=".PRE."User.username where ".PRE."Message.team=:team AND matchs=0 ORDER BY creationTime DESC ;");
$resultats->execute(["team" => $_SESSION['team']['id_team']]);
// Extraction des résultats
$messages = $resultats->fetchAll(PDO::FETCH_ASSOC);
}
// Afficher les données au format JSON
echo json_encode($messages);
}
function postMessage(){
$connection = connectDB();
//Analyse des paramètres passés en POST
if (!array_key_exists('content',$_POST)){
echo json_encode(["status" => "error", "message" => "Certains champs ne sont pas renseignés !"]);
return;
}else{
if (!empty($_SESSION['team']['id_matchs'])) {
$author = $_SESSION['info']['username'];
$content = $_POST['content'];
$match =$_SESSION['team']['id_matchs'];
$team =$_SESSION['team']['id_team'];
//Créer la requête qui permettra d'insérer ces données
$query = $connection->prepare("INSERT INTO ".PRE."Message SET author = :author, content = :content, team = :team, matchs = :matchs, creationTime = NOW()");
$query->execute([
"author" => $author,
"content" => $content,
"matchs" => $match,
"team" => $team
]);
//Donner statut de succes ou erreur au format JSON
echo json_encode(["status" => "success"]);
}else {
$author = $_SESSION['info']['username'];
$content = $_POST['content'];
$team =$_SESSION['team']['id_team'];
//Créer la requête qui permettra d'insérer ces données
$query = $connection->prepare("INSERT INTO ".PRE."Message SET author = :author, content = :content, team = :team, creationTime = NOW()");
$query->execute([
"author" => $author,
"content" => $content,
"team" => $team
]);
//Donner statut de succes ou erreur au format JSON
echo json_encode(["status" => "success"]);
}
}
}