-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpostMessage.php
76 lines (67 loc) · 1.77 KB
/
postMessage.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
<?php
include 'conn.php';
$responce=array();
if(isset($_POST['senderId'],$_POST['receiverId'],$_POST['text']))
{
$senderId=$_POST['senderId'];
$receiverId=$_POST['receiverId'];
$text=$_POST['text'];
$timestamp=$_POST['timestamp'];
if(isset($_POST['image'])){
$image=$_POST['image'];
$directory="message_images/";
$image_name=rand()."-".$senderId."-".$receiverId.".jpeg";
$target_directory=$directory."/".$image_name;
file_put_contents($target_directory, base64_decode($image));
$query="INSERT INTO `messages`(`senderId`, `receiverId`, `text`, `timestamp`, `image`)
VALUES ('$senderId','$receiverId','$text','$timestamp','$target_directory')";
$res=mysqli_query($con,$query);
if($res)
{
$responce["id"]=mysqli_insert_id($con);
$responce["msg"]="Contact Inserted";
$responce["responce"]="1";
}
else {
$responce["id"]="NA";
$responce["msg"]="Error Inserting Contact";
$responce["responce"]="0";
}
}
else{
$query="INSERT INTO `messages`(`senderId`, `receiverId`, `text`, `timestamp`, `image`)
VALUES ('$senderId','$receiverId','$text','$timestamp','Not Available')";
$res=mysqli_query($con,$query);
if($res)
{
$responce["id"]=mysqli_insert_id($con);
$responce["msg"]="Contact Inserted";
$responce["responce"]="1";
}
else {
$responce["id"]="NA";
$responce["msg"]="Error Inserting Contact";
$responce["responce"]="0";
}
}
}
else {
$responce["id"]="NA";
$responce["msg"]="Incomplete Request, Error Inserting";
$responce["responce"]="0";
}
$x = json_encode(utf8ize($responce)); #Converts Array into JSON
echo $x; ## Prints the JSON
function utf8ize($d) {
if (is_array($d)) {
foreach ($d as $k => $v)
{
$d[$k] = utf8ize($v);
}
}
else if (is_string ($d)) {
return utf8_encode($d);
}
return $d;
}
?>