-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_message.php
More file actions
183 lines (147 loc) · 4.52 KB
/
delete_message.php
File metadata and controls
183 lines (147 loc) · 4.52 KB
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
<?php
session_start();
// Check if the user is logged in, redirect to chat.php
if (!isset($_SESSION['username'])) {
header('Location: login.php');
exit();
}
$username = $_SESSION['username'];
$userDataFile = "users/" . $username . ".php";
// Function to get user data from file
function getUserData($userDataFile) {
$userData = array();
if (file_exists($userDataFile)) {
$lines = file($userDataFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
list($key, $value) = explode(':', $line, 2);
$userData[trim($key)] = trim($value);
}
}
return $userData;
}
$userData = getUserData($userDataFile);
include("lib/logfile.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/style.css">
<title>Register</title>
<style>
body {
font-family: 'Arial', sans-serif;
margin: 0;
padding: 0;
background-color: #f5f5f5;
}
#login-container {
max-width: 400px;
margin: 50px auto;
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h2 {
color: #333;
text-align: center;
}
ul {
list-style: none;
padding: 0;
text-align: center;
}
li {
margin-bottom: 10px;
}
.error {
color: #ff0000;
text-align: center;
margin: 10px 0;
}
form {
display: flex;
flex-direction: column;
}
label {
font-weight: bold;
margin-top: 10px;
}
input, select {
width: 100%;
padding: 8px;
margin-top: 5px;
box-sizing: border-box;
}
#sessionSelect {
max-width: 100%;
overflow: hidden;
text-overflow: ellipsis;
}
#newSessionInput {
display: none;
}
button {
background-color: #3498db;
color: #fff;
padding: 10px;
border: none;
border-radius: 5px;
cursor: pointer;
margin-top: 10px;
}
button:hover {
background-color: #007bb5;
}
</style>
</head>
<body>
<div id="login-container">
<?php
session_start();
if (isset($_GET['file']) && isset($_GET['delete'])) {
$file = $_GET['file'];
$delete = $_GET['delete'];
echo '<div style="border: 1px solid #ccc; padding: 10px; margin: 10px; background-color: #f8d7da; color: #721c24;">
<p>Are you sure you want to delete this message?</p>
<form method="get" action="">
<button type="submit" name="deleteConfirmed" style="background-color: darkred; color: white;">Delete</button>
<input type="text" hidden name="fileConfirmed" value="'.$file.'">
<input type="text" hidden name="messageConfirmed" value="'.$delete.'">
<button><a href="chat.php" style="color: white;">Cancel</a> </button>
</form>
</div>';
}
if (isset($_GET['fileConfirmed']) && isset($_GET['messageConfirmed'])) {
$fileToDelete = $_GET['fileConfirmed'];
$messageToDelete = urldecode($_GET['messageConfirmed']);
// Construct the file path
$filePath = $fileToDelete;
// Read the content of the file
$content = file_get_contents($filePath);
// Extract file names from the message to be deleted
preg_match_all('/\[(pdf|file): ([^\]]+)\]/', $messageToDelete, $matches);
// Remove the message from the content
$updatedContent = preg_replace('/' . preg_quote($messageToDelete, '/') . '\s*/', '', $content);
// Write the updated content back to the file
file_put_contents($filePath, $updatedContent);
// Delete associated files
foreach ($matches[2] as $index => $fileName) {
$fileType = $matches[1][$index];
// Construct the file path based on the file type
$filePathToDelete = ($fileType == 'pdf') ? "pdfs/" : "images/";
$filePathToDelete .= $fileName;
if (file_exists($filePathToDelete)) {
unlink($filePathToDelete);
}
}
// Redirect back to chat.php
header("Location: chat.php");
exit();
}
?>
</div>
</body>
</html>