-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_message.php
More file actions
83 lines (67 loc) · 2.65 KB
/
send_message.php
File metadata and controls
83 lines (67 loc) · 2.65 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
<?php
session_start(); // Start the session
include 'connect.php';
if (isset($_SESSION['chatOption'])) {
$chatOption = $_SESSION['chatOption'];
if ($chatOption === 'anonymous') {
$userId = $_SESSION['userId'];
} else {
if (isset($_SESSION['userId'])) {
$userId = $_SESSION['userId'];
} else {
// Handle unauthenticated access
die("Please log in to chat.");
}
}
} else {
if (isset($_SESSION['userId'])) {
$userId = $_SESSION['userId'];
} else {
die("Please log in to chat.");
}
}
if (isset($_POST['message'])) {
$message = $_POST['message'];
$file = $_FILES['file'];
if ($file && $file['error'] === UPLOAD_ERR_OK) {
$fileExtension = pathinfo($file['name'], PATHINFO_EXTENSION);
//$newFileName = uniqid() . '.' . $fileExtension;
$newFileName = $file['name'];
$uploadDirectory = 'uploads/' . $fileExtension . '/'; // Specify your folder path
$targetPath = $uploadDirectory . $newFileName;
if (!is_dir($uploadDirectory)) {
mkdir($uploadDirectory, 0755, true); // Create the folder with proper permissions
}
$counter = 1;
while (file_exists($targetPath)) {
$newFileName = pathinfo($file['name'], PATHINFO_FILENAME) . "_$counter.$fileExtension";
$targetPath = $uploadDirectory . $newFileName;
$counter++;
}
if (move_uploaded_file($file['tmp_name'], $targetPath)) {
removeExifData($targetPath);
$mediapath = $targetPath;
} else {
echo '<p>Erorr file not uploaded.</p>';
}
}
$currentDateTime = date('Y-m-d H:i:s');
$insertQuery = "INSERT INTO chat (iduser, message, media, datetime) VALUES (:iduser, :message, :media, :datetime)";
$stmt = $pdo->prepare($insertQuery);
$stmt->execute(array(':iduser' => $userId, ':message' => $message, ':media' => $mediapath, ':datetime' => $currentDateTime));
}
// Function to remove EXIF data from an image file
function removeExifData($imagePath) {
if (function_exists('exif_read_data') && function_exists('exif_imagetype')) {
$imageType = exif_imagetype($imagePath);
if ($imageType === IMAGETYPE_JPEG) {
$exif = exif_read_data($imagePath);
if ($exif !== false) {
// Remove EXIF data
$image = imagecreatefromjpeg($imagePath);
imagejpeg($image, $imagePath, 100); // Overwrite the original image
imagedestroy($image);
}
}
}
}