-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.php
266 lines (230 loc) · 11.6 KB
/
index.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
session_start();
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
header("Location: ./login/");
exit;
}
include './config.php';
$query = new Database();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Social-Messenger | Contacts</title>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">
<link rel="stylesheet" href="./src/css/style.css">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="modal fade" id="profileModal" tabindex="-1" aria-labelledby="profileModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header border-bottom-0">
<div class="d-flex align-items-center">
<img src="./src/images/profile-picture/default.png" alt="Profile Image" class="rounded-circle" width="50" height="50" id="modalProfilePicture">
<h5 class="modal-title ml-3" id="profileModalLabel">Edit Profile</h5>
</div>
<button type="button" class="btn-close" style="background-color: transparent; border: none; color: #333; font-size: 1.5rem; cursor: pointer" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="profile-form" method="POST" enctype="multipart/form-data">
<div class="form-group mb-3">
<label for="full_name" class="form-label">Full Name:</label>
<input type="text" id="full_name" name="full_name" class="form-control" required maxlength="30" placeholder="Enter your full name">
</div>
<div class="form-group mb-3">
<label for="profile_picture" class="form-label">Profile Image:</label>
<div class="custom-file">
<input type="file" class="custom-file-input" id="profile_picture" name="profile_picture" accept="image/*">
<label class="custom-file-label" for="profile_picture">Choose an image</label>
</div>
</div>
<div class="form-group mb-3">
<label for="email" class="form-label">Email:</label>
<input type="email" id="email" name="email" class="form-control" readonly>
</div>
<div class="form-group mb-3">
<label for="username" class="form-label">Username:</label>
<input type="text" id="username" name="username" class="form-control" readonly>
</div>
<div class="form-group mb-4">
<label for="password" class="form-label">New Password:</label>
<input type="password" id="password" name="password" class="form-control" maxlength="255" placeholder="Enter new password">
<small class="form-text text-muted">Leave empty if you don't want to change the password.</small>
</div>
<button type="submit" class="btn btn-primary btn-block">Save Changes</button>
</form>
</div>
</div>
</div>
</div>
<div class="container-fluid h-100">
<div class="row justify-content-center h-100">
<div class="col-md-8 col-xl-6 chat">
<div class="card">
<div class="card-header" style="display: flex;">
<div class="input-group">
<span class="input-group-text menu_btn" data-toggle="modal" data-target="#profileModal">
<i class="fas fa-bars"></i>
</span>
<input type="text" placeholder="Search..." name="search" id="search" class="form-control search">
<div class="input-group-prepend">
<span class="input-group-text search_btn"><i class="fas fa-search"></i></span>
</div>
</div>
<span class="input-group-prepend logout_menu" onclick="logout()">
<i class="fas fa-sign-out-alt"></i>
</span>
</div>
<div class="card-body contacts_body">
<ul class="contacts" id="contacts-list"></ul>
</div>
<div class="card-footer"></div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script>
function fetchUserProfile() {
fetch('./api/fetch_profile.php', {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
.then(response => response.json())
.then(result => {
if (result.status === 'success') {
const {
full_name,
email,
username,
profile_picture
} = result.data;
document.getElementById('full_name').value = full_name;
document.getElementById('email').value = email;
document.getElementById('username').value = username;
const profilePic = profile_picture && profile_picture !== 'default.png' ?
'./src/images/profile-picture/' + profile_picture :
'./src/images/profile-picture/default.png';
document.getElementById('modalProfilePicture').src = profilePic;
}
})
}
document.getElementById('profile-form').addEventListener('submit', function(event) {
event.preventDefault();
const formData = new FormData(this);
fetch('./api/fetch_profile.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(result => {
Swal.fire({
icon: 'success',
title: 'Profile Updated',
text: result.message,
showConfirmButton: false,
timer: 1500
}).then(() => window.location.reload());
})
.catch(error => {
Swal.fire({
icon: 'error',
title: 'Server Error',
text: 'Failed to connect to the server. Please try again later.',
});
});
});
fetchUserProfile();
</script>
<script>
// Set Interval
let fetchInterval = setInterval(fetchContacts, 1000);
const searchInput = document.getElementById('search');
let timeout = null;
searchInput.addEventListener('input', function() {
const searchTerm = searchInput.value.trim();
clearInterval(fetchInterval);
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(function() {
fetchContacts(searchTerm);
}, 1000);
});
searchInput.addEventListener('blur', function() {
fetchInterval = setInterval(fetchContacts, 1000);
});
// Fetch Contact
function fetchContacts(searchTerm = '') {
fetch('./api/fetch_contacts.php?search=' + encodeURIComponent(searchTerm))
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
const contacts = data.data;
const contactsList = document.getElementById('contacts-list');
contactsList.innerHTML = '';
contacts.forEach(user => {
const highlightedFullName = highlightSearchTerm(user.full_name, searchTerm);
const highlightedUsername = highlightSearchTerm(user.username, searchTerm);
const unreadMessages = user.unread_messages;
const listItem = document.createElement('li');
listItem.setAttribute('onclick', `window.location.href='chat.php?id=${user.user_id}'`);
listItem.innerHTML = `
<div class="d-flex bd-highlight">
<div class="img_cont">
<img src="./src/images/profile-picture/${user.profile_picture}"
class="rounded-circle user_img"
alt="${user.full_name}">
</div>
<div
<div class="user_info">
<span>${highlightedFullName}</span>
<p>${highlightedUsername}</p>
</div>
<div class="message_count">
${unreadMessages > 0 ? `<span class="badge badge-warning">${unreadMessages}</span>` : ''}
</div>
</div>
`;
contactsList.appendChild(listItem);
});
}
});
}
function highlightSearchTerm(text, searchTerm) {
if (!searchTerm) return text;
const regex = new RegExp(`(${searchTerm})`, 'gi');
return text.replace(regex, '<span style="color: red;">$1</span>');
}
fetchContacts();
// Logout function
function logout() {
Swal.fire({
title: 'Are you sure?',
text: "You want to log out?",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, log me out!',
cancelButtonText: 'Cancel'
}).then((result) => {
if (result.isConfirmed) {
window.location.href = './logout/';
}
});
}
</script>
</body>
</html>