-
Notifications
You must be signed in to change notification settings - Fork 2
/
submit_post.php
94 lines (76 loc) · 3.25 KB
/
submit_post.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
<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_SESSION['username'])) {
$imageURL = '';
if ($_FILES["postImage"]["error"] === 0) {
$targetDir = "assets/images/";
$randomFilename = uniqid() . "_" . basename($_FILES["postImage"]["name"]);
$targetFile = $targetDir . $randomFilename;
if (move_uploaded_file($_FILES["postImage"]["tmp_name"], $targetFile)) {
$imageURL = $targetFile;
}
}
if (isset($_POST['postContent'])) {
$postContent = trim($_POST['postContent']);
$charLimit = 60000;
if (strlen($postContent) == 0 && empty($imageURL)) {
$response = array('status' => 'error', 'message' => 'Please enter text or upload an image to post.');
echo json_encode($response);
echo '<script>
setTimeout(function() {
window.history.back();
}, 5000);
</script>';
exit;
} elseif (strlen($postContent) > $charLimit) {
$response = array('status' => 'error', 'message' => 'Text input must be between 1 and ' . $charLimit . ' characters. This is due to MySQL.');
echo json_encode($response);
echo '<script>
setTimeout(function() {
window.history.back();
}, 5000);
</script>';
exit;
}
} else {
$postContent = '';
}
$imageLink = $_POST['imageLink'];
$postLink = $_POST['postLink'];
include("important/db.php");
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$username = $_SESSION['username'];
// Insert the post into the posts table
$query = "INSERT INTO posts (username, content, image_url, image_link_url, post_link_url, created_at) VALUES (?, ?, ?, ?, ?, NOW())";
$stmt = $conn->prepare($query);
$stmt->bind_param("sssss", $username, $postContent, $imageURL, $imageLink, $postLink);
$stmt->execute();
// Get the auto-generated post ID
$newPostID = $stmt->insert_id;
$stmt->close();
// Create and send notifications to mentioned users
preg_match_all('/\+([A-Za-z0-9_]+)/', $postContent, $matches);
if (!empty($matches[1])) {
foreach ($matches[1] as $mentionedUser) {
// Create a personalized notification message
$notificationContent = "Mentioned you in a post!";
// Insert a notification for each mentioned user
$query = "INSERT INTO notifications (sender, recipient, content, post_id, created_at) VALUES (?, ?, ?, ?, NOW())";
$stmt = $conn->prepare($query);
$stmt->bind_param("sssi", $username, $mentionedUser, $notificationContent, $newPostID);
$stmt->execute();
$stmt->close();
}
}
$conn->close();
$response = array('status' => 'success', 'message' => 'Post successfully submitted.');
echo json_encode($response);
header("Location: " . $_SERVER['HTTP_REFERER']);
exit;
}
$response = array('status' => 'error', 'message' => 'Error submitting the post.');
echo json_encode($response);
?>