-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditEvent.php
More file actions
182 lines (154 loc) · 8.12 KB
/
Copy patheditEvent.php
File metadata and controls
182 lines (154 loc) · 8.12 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
<?php
ob_start(); // Start output buffering
session_start();
// Error reporting settings
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Check if user is logged in
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
header("Location: login.php");
exit();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save'])) {
// Input sanitization and validation
$eventId = filter_input(INPUT_POST, 'event_id', FILTER_SANITIZE_NUMBER_INT);
if (!is_numeric($eventId) || $eventId <= 0) {
error_log("Invalid event ID: " . $eventId); // Log to PHP error log
$response = ["status" => "error", "message" => "Invalid event ID."];
echo json_encode($response);
exit;
}
$eventTitle = filter_input(INPUT_POST, 'event_title', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$eventDesc = filter_input(INPUT_POST, 'event_desc', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$eventDetailedDesc = filter_input(INPUT_POST, 'event_detailed_desc', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$eventTime = filter_input(INPUT_POST, 'event_time', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$eventType = filter_input(INPUT_POST, 'event_type', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$eventDate = filter_input(INPUT_POST, 'event_date', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
// Handle image upload (optional)
$target_dir = "scheduleMEDIA/";
$eventImg = null;
if (isset($_FILES["event_img"])) {
$uploadError = $_FILES["event_img"]["error"];
if ($uploadError === UPLOAD_ERR_OK) {
$target_file = $target_dir . basename($_FILES["event_img"]["name"]);
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// Image validation
$check = getimagesize($_FILES["event_img"]["tmp_name"]);
if ($check === false) {
error_log("File is not an image: " . $_FILES["event_img"]["name"]); // Log to PHP error log
$response = ["status" => "error", "message" => "File is not an image."];
echo json_encode($response);
exit;
}
// File size check
if ($_FILES["event_img"]["size"] > 5000000) {
error_log("File too large: " . $_FILES["event_img"]["name"] . " (" . $_FILES["event_img"]["size"] . " bytes)"); // Log to PHP error log
$response = ["status" => "error", "message" => "File is too large."];
echo json_encode($response);
exit;
}
// File type check
$allowedTypes = ["jpg", "png", "jpeg", "gif"];
if (!in_array($imageFileType, $allowedTypes)) {
error_log("Invalid file type: " . $imageFileType . " for file: " . $_FILES["event_img"]["name"]); // Log to PHP error log
$response = ["status" => "error", "message" => "Invalid file type."];
echo json_encode($response);
exit;
}
// Move uploaded file
if (!move_uploaded_file($_FILES["event_img"]["tmp_name"], $target_file)) {
error_log("Error uploading file: " . $_FILES["event_img"]["name"] . " - " . error_get_last()['message']); // Log to PHP error log
$response = ["status" => "error", "message" => "Error uploading file."];
echo json_encode($response);
exit;
}
$eventImg = $target_file;
} else {
// Image deletion handling
$eventId = filter_input(INPUT_POST, 'event_id', FILTER_SANITIZE_NUMBER_INT);
$initialImgPath = filter_input(INPUT_POST, 'initial_img_path', FILTER_SANITIZE_FULL_SPECIAL_CHARS); // Get initial image path
if ($eventId && $initialImgPath !== '') {
$target_dir = "scheduleMEDIA/"; //Make sure this path is correct
$imagePathToDelete = $initialImgPath; //Use the initial image path
if (file_exists($imagePathToDelete)) {
if (unlink($imagePathToDelete)) {
error_log("Image deleted successfully: " . $imagePathToDelete);
$eventImg = null; // Set eventImg to null to update the database
} else {
error_log("Error deleting image: " . $imagePathToDelete . " - " . error_get_last()['message']);
$response = ["status" => "error", "message" => "Error deleting image."];
echo json_encode($response);
exit;
}
} else {
error_log("Image file not found: " . $imagePathToDelete);
//It's okay if the file doesn't exist; it might have been deleted already.
}
}
}
} else if (isset($_POST['event_img']) && $_POST['event_img'] === 'null') {
// Image deletion handling
$eventId = filter_input(INPUT_POST, 'event_id', FILTER_SANITIZE_NUMBER_INT);
$initialImgPath = filter_input(INPUT_POST, 'initial_img_path', FILTER_SANITIZE_FULL_SPECIAL_CHARS); // Get initial image path
if ($eventId && $initialImgPath) {
//Check if the image file exists before attempting to delete it.
if (file_exists($initialImgPath)) {
if (unlink($initialImgPath)) {
error_log("Image deleted successfully: " . $initialImgPath);
$eventImg = null; // Set eventImg to null to update the database
} else {
error_log("Error deleting image: " . $initialImgPath . " - " . error_get_last()['message']);
$response = ["status" => "error", "message" => "Error deleting image."];
echo json_encode($response);
exit;
}
} else {
error_log("Image file not found: " . $initialImgPath);
$response = ["status" => "error", "message" => "Image file not found."];
echo json_encode($response);
exit;
}
} else {
error_log("Error deleting image: Missing event ID or initial image path.");
$response = ["status" => "error", "message" => "Error deleting image."];
echo json_encode($response);
exit;
}
}
// Database connection
require_once('db_config.php');
$conn = new mysqli($host, $username, $password, $dbname);
if ($conn->connect_error) {
error_log("Database connection failed: " . $conn->connect_error); // Log to PHP error log
$response = ["status" => "error", "message" => "Database connection failed."];
echo json_encode($response);
exit;
}
try {
// Update query (prepared statement)
$sql = "UPDATE events SET event_title=?, event_desc=?, event_detailed_desc=?, event_time=?, event_type=?, event_date=?, event_img=? WHERE ID=?";
$stmt = $conn->prepare($sql);
if ($stmt === false) {
throw new Exception("Error preparing statement: " . $conn->error);
}
// Handle null values gracefully using the null coalescing operator
$eventImg = $eventImg ?? null;
$stmt->bind_param("sssssssi", $eventTitle, $eventDesc, $eventDetailedDesc, $eventTime, $eventType, $eventDate, $eventImg, $eventId);
$stmt->execute();
if ($stmt->affected_rows > 0) {
$response = ["status" => "success", "message" => "Event updated successfully."];
} else {
error_log("Error updating event: " . $stmt->error . " - Affected rows: " . $stmt->affected_rows);
$response = ["status" => "error", "message" => "Error updating event."];
}
} catch (Exception $e) {
error_log("Database error: " . $e->getMessage());
$response = ["status" => "error", "message" => "An error occurred."];
} finally {
if ($stmt) $stmt->close();
if ($conn) $conn->close();
}
echo json_encode($response);
}
?>