-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.php
154 lines (148 loc) · 5.2 KB
/
chat.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
149
150
151
152
153
154
<?php
session_start();
require_once './vendor/autoload.php';
require_once './utils/php/db_functions.php';
$app_id = '846919';
$app_key = '2fb071cffdac6f3ff0d7';
$app_secret = 'b06a98782716d59c6079';
$options = [
'cluster' => 'us2',
'useTLS' => true
];
$pusher = new Pusher\Pusher(
$app_key,
$app_secret,
$app_id,
$options
);
if ($_POST && !empty($_POST['username'])) {
$username = implode("", explode(" ",trim($_POST['username'])));
$_SESSION['username'] = $username;
setcookie('username', $username);
insertOnlineUser($username);
$pusher->trigger('channel', 'userAction', getOnlineUsers());
} else {
header('Location: index.html');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<!-- <meta http-equiv="refresh" content="5"> -->
<title>Chat PHP</title>
<!-- bootstrap -->
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous"
/>
<link
href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"
rel="stylesheet"
integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN"
crossorigin="anonymous"
/>
<link rel="stylesheet" href="./assets/css/global.css" />
<link rel="stylesheet" href="./assets/css/chat.css" />
<script
defer
src="https://code.jquery.com/jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous"
></script>
<script
defer
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
crossorigin="anonymous"
></script>
<script
defer
src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"
></script>
<!-- custom scripts -->
<script defer src="utils/js/scrollDown.js"></script>
<script defer src="utils/js/getCookie.js"></script>
<!-- pusher scripts -->
<script defer src="https://js.pusher.com/5.0/pusher.min.js"></script>
<script defer src="utils/js/listener.js"></script>
</head>
<body>
<main>
<header>
<nav class="navbar navbar-light bg-transparent">
<a href="index.html" class="navbar-brand">
<img
src="./assets/img/logo.png"
width="30"
height="30"
class="d-inline-block align-top"
alt=""
/>
Chatzin
</a>
<form action="utils/php/logout.php" method="POST" class="form-inline">
<button class="btn btn-outline-danger my-2 my-sm-0" type="submit">
Logout
</button>
</form>
</nav>
</header>
<div class="chat-container">
<div class="col-sm-6 frame screen">
<ul id="screen">
<?php foreach(getMessages() as $chat) : ?>
<li style="width:100%">
<div class="<?= $chat['username'] === $_SESSION['username'] ? 'msj-rta' : 'msj' ?> macro">
<div class="text <?= $chat['username'] === $_SESSION['username'] ? 'text-l' : 'text-r' ?>">
<?php if($chat['username'] != $_SESSION['username']) : ?>
<p class="user font-weight-bold"><?= $chat['username'] ?></p>
<?php endif; ?>
<p class="message"><?= $chat['message'] ?></p>
<p class="time"><small><?= substr($chat['time'], 0, 5)?></small></p>
</div>
</div>
</li>
<?php endforeach; ?>
</ul>
<form id="form" class="input-container">
<input
id="username"
type="hidden"
value="<?= $_SESSION['username'] ?>"
placeholder="Type your username"
/>
<input id="message" type="text" placeholder="Type a message" />
<button type="submit">
<i class="fa fa-arrow-right"></i>
</button>
</form>
</div>
<div class="col-sm-3 online-users">
<h4 class="mb-6">Online Users</h4>
<div>
<p class="text-success font-weight-bold"><?= $_SESSION['username'] ?></p>
<div class="users">
<?php foreach(getOnlineUsers() as $onlineUser) : ?>
<?php if ($onlineUser['username'] != $_SESSION['username']) : ?>
<p class="text-success"><?= $onlineUser['username'] ?></p>
<?php endif; ?>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
</main>
<script>
window.onload = () => {
scrollDown('#screen', 100)
}
</script>
</body>
</html>