-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathapi.php
74 lines (68 loc) · 2.75 KB
/
api.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
<?php
session_start();
include_once('php/config.php');
include_once('php/chat_realtime.php');
$chat = new Chat_realtime($name, $host, $username, $password, $imageDir);
$data = array();
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
if(!empty($_POST['data'])){
if($_POST['data'] == 'cek'){
if(isset($_SESSION['user']) && isset($_SESSION['avatar'])){
$data['status'] = 'success';
$data['user'] = $_SESSION['user'];
$data['avatar'] = $_SESSION['avatar'];
}else{
$data['status'] = 'error';
}
}else if($_POST['data'] == 'login'){
if(!empty($_POST['name']) && !empty($_POST['avatar'])){
$data = $chat->user_login($_POST['name'], $_POST['avatar']);
if($data['status'] == 'success'){
$_SESSION['user'] = $_POST['name'];
$_SESSION['avatar'] = $_POST['avatar'];
}
}
}else if($_POST['data'] == 'message'){
if(!empty($_POST['ke']) && !empty($_POST['tipe'])){
$data = $chat->get_message($_POST['tipe'], $_POST['ke'], $_SESSION['user']);
}
}else if($_POST['data'] == 'user'){
$data = $chat->get_user($_SESSION['user']);
}else if($_POST['data'] == 'send'){
if(isset($_SESSION['user']) && !empty($_POST['ke']) && !empty($_POST['date']) && !empty($_POST['avatar']) && !empty($_POST['tipe']) && isset($_POST['message']) && isset($_POST['images'])){
$images = json_decode($_POST['images']);
if(!empty($_POST['message']) && count($images) < 1){
$data = $chat->send_message($_SESSION['user'], $_POST['ke'], $_POST['message'], "", $_POST['date'], $_POST['avatar'], $_POST['tipe']);
}else if(!empty($_POST['message']) && count($images) > 0){
$h = 0;
foreach($images as $image){
$n = $chat->arrayToBinaryString($image->binary);
$chat->createImg($n, $image->name, 'image/png');
if($h == 0){
$data = $chat->send_message($_SESSION['user'], $_POST['ke'], $_POST['message'], $image->name, $_POST['date'], $_POST['avatar'], $_POST['tipe']);
}else{
$data = $chat->send_message($_SESSION['user'], $_POST['ke'], "", $image->name, $_POST['date'], $_POST['avatar'], $_POST['tipe']);
}
$h++;
}
}else if(empty($_POST['message']) && count($images) > 0){
foreach($images as $image){
$n = $chat->arrayToBinaryString($image->binary);
$chat->createImg($n, $image->name, 'image/png');
$data = $chat->send_message($_SESSION['user'], $_POST['ke'], "", $image->name, $_POST['date'], $_POST['avatar'], $_POST['tipe']);
}
}
}
}else if($_POST['data'] == 'logout'){
$data = $chat->user_logout($_SESSION['user']);
if($data['status'] == 'success'){
session_destroy();
}
}
}
}else{
$data["aa"] = "bb";
}
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
echo json_encode($data);