-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendMessage.php
57 lines (49 loc) · 1.24 KB
/
sendMessage.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
<?php
include "config.php";
include "functions.php";
session_start();
if($_POST)
{
$user=$_SESSION['name'];
$to=$_POST['to'];
$msg=escape($_POST['msg']);
$verify = "SELECT * FROM register WHERE name = '$to'";
$verify_user = mysqli_query($conn, $verify);
if(!empty($verify_user) AND mysqli_num_rows($verify_user) > 0){
function encrypt($msg, $encrypt_key){
$key = hex2bin($encrypt_key);
$nonceSize = openssl_cipher_iv_length('aes-256-ctr');
$nonce = openssl_random_pseudo_bytes($nonceSize);
$ciphertext = openssl_encrypt($msg,'aes-256-ctr',$key,OPENSSL_RAW_DATA,$nonce);
return base64_encode($nonce.$ciphertext);
}
$private_secret_key = '1f4276378ad3214c873928dbef42743f';
$encrypted = encrypt($msg, $private_secret_key);
$sql="INSERT INTO `chat`(`name`,`receives`,`message`) VALUES ('".$user."','".$to."', '".$encrypted."')";
$query = mysqli_query($conn,$sql);
if($query)
{
echo '
<script>
alert("Message sent succesfully");
window.location = "/chatpage.php";
</script>';
}
else
{
echo "Something went wrong";
}
}
else{
echo '
<script>
alert("Username does not exist!");
window.location = "/chatpage.php";
</script>';
}
}
else
{
header('location:index.php');
}
?>