-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatapp.php
148 lines (125 loc) · 4.94 KB
/
chatapp.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
139
140
141
142
143
144
145
146
147
148
<?php
require("php/parameters.php");?>
<?php
require("parts/header.php");
//Perde i parametri post tra una chiamata e l'altra
if(!isset($_POST['pswAccesso']) || !strcmp($_POST['pswAccesso'],"Azet325K54fA32w")==0 || !isset($_POST['email'])){
header("Location: index.php");
}
?>
<div id="main">
<div id="container_home">
<span id="chat_container">
<span id="chat_content">
<!--
<div class="chatmessagesend">
Messaggio inviato
</div>
<div class="chatmessagerecieved">
Messaggio ricevuto
</div>
-->
</span>
<div id="writemessage">
<form action="" method="post" >
Destinatario: <input readonly="text" id="destinatario" name="destinatario" value="">
<input type="text" id="message" name="message" placeholder="Scrivi qui il tuo messaggio">
<input type="hidden" name="pswAccesso" value="<?php echo $_POST['pswAccesso']; ?>" />
<input type="hidden" name="email" value="<?php echo $_POST['email']; ?>" />
<br>
<input type="submit" id="sendbutton" name="sendbutton" value="Invia" >
</form>
</div>
</span>
<span id="chatlist">
<?php addChat(); ?>
</span>
</div>
</div>
<script type="text/javascript">
/* CHIAMATA AJAX */
function riempiChat(other,user) {
console.log("ok");
var dest = document.getElementById("destinatario");
dest.value =other;
var chat = document.getElementById("chat_content");
chat.innerHTML = synchcall(other,user);
}
function synchcall(other,user) {
xhr = getXMLHttpRequestObject();
xhr.open("POST", "php/riempiChatapp.php/?other="+other+"&email="+user, false);
xhr.send();
if (xhr.readyState==4 && xhr.status==200) {
out = xhr.responseText;
return out;
}
else
return "Error<br/><strong>Code:</strong> " + xhr.status + "<br/><strong>Reason:<strong> " + xhr.statusText;
}
</script>
</body>
<?php
//set up page
function addChat(){
$con = mysqli_connect(SERVER,USER,PSW);
mysqli_select_db($con,DB);
$email = $_POST['email'];
//$email = $_GET['email'];
$query_mittente = 'SELECT DISTINCT destinatario
FROM message
WHERE mittente="'.$email.'" ;';
$query_destinatario = 'SELECT DISTINCT mittente
FROM message
WHERE destinatario="'.$email.'"
AND mittente NOT IN
(SELECT DISTINCT destinatario
FROM message
WHERE mittente="'.$email.'");';
$res = mysqli_query($con,$query_mittente);
$numrows= mysqli_num_rows($res);
for($i = 0; $i<$numrows; $i++){
$row = mysqli_fetch_assoc($res);
// la chat con il primo
/*
if($i==0)
$toShow = $row['destinatario'];
*/
$mod = $i%2;
echo '<div class="listelement'.$mod.'" onclick="riempiChat(\''.$row['destinatario'].'\',\''.$email.'\')">'.$row['destinatario'].'</div>';
}
$res = mysqli_query($con,$query_destinatario);
$numrows= mysqli_num_rows($res);
for($i = 0; $i<$numrows; $i++){
$row = mysqli_fetch_assoc($res);
$mod = $i%2;
echo '<div class="listelement'.$mod.'" onclick="riempiChat(\''.$row['mittente'].'\',\''.$email.'\')">'.$row['mittente'].'</div>';
}
}
?>
<?php
//send message
if(isset($_POST['sendbutton'])){
$text = $_POST['message'];
$dest = $_POST['destinatario'];
//$email = $_GET['email'];
$email = $_POST['email'];
$ok=true;
if(strcmp($dest,"")==0){
echo '<script type="text/javascript">window.alert("Nessun destinatario selezionato")</script>';
$ok = false;
}
if(strcmp($text,"")==0){
echo '<script type="text/javascript">window.alert("Impossibile inviare un messaggio vuoto")</script>';
$ok=false;
}
if($ok){
$con = mysqli_connect(SERVER,USER,PSW);
mysqli_select_db($con,DB);
$text = mysqli_real_escape_string($con,$text);
$query = "INSERT INTO message (mittente, destinatario, testo, datames) VALUES ('".$email."','".$dest."','".$text."',FROM_UNIXTIME(".time()."));";
$res = mysqli_query($con,$query);
echo '<script type="text/javascript">riempiChat(\''.$dest.'\',\''.$email.'\');</script>';
}
}
?>
</html>