-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.php
More file actions
290 lines (249 loc) · 8.47 KB
/
process.php
File metadata and controls
290 lines (249 loc) · 8.47 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
// Enable error reporting for development
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Set content type to JSON for API responses
header('Content-Type: application/json');
// Enable CORS if needed
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
// Handle preflight requests
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit();
}
// Database configuration (SQLite for simplicity)
$database_file = 'user_data.db';
// Initialize database
function initDatabase($db_file) {
try {
$pdo = new PDO("sqlite:$db_file");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Create users table if it doesn't exist
$sql = "CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT NOT NULL,
message TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)";
$pdo->exec($sql);
return $pdo;
} catch (PDOException $e) {
error_log("Database error: " . $e->getMessage());
return false;
}
}
// Validate input data
function validateInput($data) {
$errors = [];
// Validate name
if (empty($data['name']) || strlen(trim($data['name'])) < 2) {
$errors[] = "Name must be at least 2 characters long";
}
// Validate email
if (empty($data['email']) || !filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
$errors[] = "Please enter a valid email address";
}
// Validate message
if (empty($data['message']) || strlen(trim($data['message'])) < 10) {
$errors[] = "Message must be at least 10 characters long";
}
return $errors;
}
// Sanitize input data
function sanitizeInput($data) {
return [
'name' => htmlspecialchars(trim($data['name']), ENT_QUOTES, 'UTF-8'),
'email' => filter_var(trim($data['email']), FILTER_SANITIZE_EMAIL),
'message' => htmlspecialchars(trim($data['message']), ENT_QUOTES, 'UTF-8')
];
}
// Save user data to database
function saveUserData($pdo, $data) {
try {
$sql = "INSERT INTO users (name, email, message) VALUES (:name, :email, :message)";
$stmt = $pdo->prepare($sql);
$result = $stmt->execute([
':name' => $data['name'],
':email' => $data['email'],
':message' => $data['message']
]);
if ($result) {
return $pdo->lastInsertId();
}
return false;
} catch (PDOException $e) {
error_log("Database insert error: " . $e->getMessage());
return false;
}
}
// Get all users (for admin purposes)
function getAllUsers($pdo) {
try {
$sql = "SELECT * FROM users ORDER BY created_at DESC";
$stmt = $pdo->query($sql);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
error_log("Database select error: " . $e->getMessage());
return false;
}
}
// Send email notification (mock function)
function sendEmailNotification($data) {
// In a real application, you would use a proper email service
// For demonstration, we'll just log the email
$email_content = "
New form submission received:
Name: {$data['name']}
Email: {$data['email']}
Message: {$data['message']}
Time: " . date('Y-m-d H:i:s') . "
";
error_log("Email notification: " . $email_content);
return true;
}
// Main processing logic
function processRequest() {
// Initialize database
$pdo = initDatabase($GLOBALS['database_file']);
if (!$pdo) {
return [
'success' => false,
'message' => 'Database connection failed',
'errors' => ['Database is currently unavailable']
];
}
// Handle different request methods
switch ($_SERVER['REQUEST_METHOD']) {
case 'POST':
return handlePostRequest($pdo);
case 'GET':
return handleGetRequest($pdo);
default:
return [
'success' => false,
'message' => 'Method not allowed',
'errors' => ['Only POST and GET methods are supported']
];
}
}
// Handle POST requests (form submissions)
function handlePostRequest($pdo) {
// Get POST data
$input_data = [
'name' => $_POST['name'] ?? '',
'email' => $_POST['email'] ?? '',
'message' => $_POST['message'] ?? ''
];
// Validate input
$validation_errors = validateInput($input_data);
if (!empty($validation_errors)) {
return [
'success' => false,
'message' => 'Validation failed',
'errors' => $validation_errors
];
}
// Sanitize input
$clean_data = sanitizeInput($input_data);
// Save to database
$user_id = saveUserData($pdo, $clean_data);
if (!$user_id) {
return [
'success' => false,
'message' => 'Failed to save data',
'errors' => ['Database error occurred']
];
}
// Send email notification
sendEmailNotification($clean_data);
// Return success response
return [
'success' => true,
'message' => "Thank you {$clean_data['name']}! Your message has been received.",
'data' => [
'user_id' => $user_id,
'name' => $clean_data['name'],
'submitted_at' => date('Y-m-d H:i:s')
]
];
}
// Handle GET requests (view submissions)
function handleGetRequest($pdo) {
// Check if requesting specific action
$action = $_GET['action'] ?? 'list';
switch ($action) {
case 'list':
$users = getAllUsers($pdo);
if ($users === false) {
return [
'success' => false,
'message' => 'Failed to retrieve data',
'errors' => ['Database error occurred']
];
}
return [
'success' => true,
'message' => 'Data retrieved successfully',
'data' => $users,
'count' => count($users)
];
case 'stats':
try {
$sql = "SELECT COUNT(*) as total_submissions,
DATE(created_at) as submission_date,
COUNT(*) as daily_count
FROM users
GROUP BY DATE(created_at)
ORDER BY submission_date DESC
LIMIT 7";
$stmt = $pdo->query($sql);
$stats = $stmt->fetchAll(PDO::FETCH_ASSOC);
$total_sql = "SELECT COUNT(*) as total FROM users";
$total_stmt = $pdo->query($total_sql);
$total = $total_stmt->fetch(PDO::FETCH_ASSOC);
return [
'success' => true,
'message' => 'Statistics retrieved successfully',
'data' => [
'total_submissions' => $total['total'],
'daily_stats' => $stats
]
];
} catch (PDOException $e) {
return [
'success' => false,
'message' => 'Failed to retrieve statistics',
'errors' => ['Database error occurred']
];
}
default:
return [
'success' => false,
'message' => 'Invalid action',
'errors' => ['Supported actions: list, stats']
];
}
}
// Execute the main processing
try {
$response = processRequest();
// Set appropriate HTTP status code
http_response_code($response['success'] ? 200 : 400);
// Output JSON response
echo json_encode($response, JSON_PRETTY_PRINT);
} catch (Exception $e) {
// Handle unexpected errors
error_log("Unexpected error: " . $e->getMessage());
http_response_code(500);
echo json_encode([
'success' => false,
'message' => 'Internal server error',
'errors' => ['An unexpected error occurred']
], JSON_PRETTY_PRINT);
}
// Log request for debugging
error_log("PHP Request processed: " . $_SERVER['REQUEST_METHOD'] . " " . $_SERVER['REQUEST_URI']);
?>