-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.php
More file actions
280 lines (230 loc) · 8.38 KB
/
Copy pathaction.php
File metadata and controls
280 lines (230 loc) · 8.38 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
<?php
/**
* CyberCon25 - Action Handler
* Process form submissions (Registration & Contact)
*/
// Enable error reporting for development
error_reporting(E_ALL);
ini_set('display_errors', 0);
// Set content type
header('Content-Type: application/json');
// Start session
session_start();
// Function to sanitize input
function sanitize_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// Function to validate email
function validate_email($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
// Initialize response array
$response = array(
'success' => false,
'message' => ''
);
// Check if form is submitted via POST
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$action = isset($_POST['action']) ? sanitize_input($_POST['action']) : '';
switch ($action) {
case 'register':
handleRegistration();
break;
case 'contact':
handleContact();
break;
default:
$response['message'] = "Invalid action";
echo json_encode($response);
break;
}
} else {
$response['message'] = "Invalid request method";
echo json_encode($response);
}
/**
* Handle Registration Form
*/
function handleRegistration() {
global $response;
// Get form data
$fullName = isset($_POST['fullName']) ? sanitize_input($_POST['fullName']) : '';
$studentId = isset($_POST['studentId']) ? sanitize_input($_POST['studentId']) : '';
$university = isset($_POST['university']) ? sanitize_input($_POST['university']) : '';
$department = isset($_POST['department']) ? sanitize_input($_POST['department']) : '';
$batch = isset($_POST['batch']) ? sanitize_input($_POST['batch']) : '';
$section = isset($_POST['section']) ? sanitize_input($_POST['section']) : '';
$email = isset($_POST['email']) ? sanitize_input($_POST['email']) : '';
$phone = isset($_POST['phone']) ? sanitize_input($_POST['phone']) : '';
$queries = isset($_POST['queries']) ? sanitize_input($_POST['queries']) : '';
$paymentMethod = isset($_POST['paymentMethod']) ? sanitize_input($_POST['paymentMethod']) : '';
$paymentNumber = isset($_POST['paymentNumber']) ? sanitize_input($_POST['paymentNumber']) : '';
$transactionId = isset($_POST['transactionId']) ? sanitize_input($_POST['transactionId']) : '';
$ticketPrice = isset($_POST['ticketPrice']) ? sanitize_input($_POST['ticketPrice']) : '200 BDT';
// Validation
$errors = array();
if (empty($fullName) || strlen($fullName) < 2) {
$errors[] = "Valid name is required";
}
if (empty($studentId)) {
$errors[] = "Student ID is required";
}
if (empty($email) || !validate_email($email)) {
$errors[] = "Valid university email is required";
}
// Check if email domain is correct
if (!preg_match('/@(uttara\.ac\.bd|uttarauniversity\.edu\.bd)$/', $email)) {
$errors[] = "Must use @uttara.ac.bd or @uttarauniversity.edu.bd email";
}
if (empty($phone)) {
$errors[] = "Phone number is required";
}
if (empty($transactionId)) {
$errors[] = "Transaction ID is required";
}
if (empty($paymentMethod)) {
$errors[] = "Payment method is required";
}
// Check for errors
if (!empty($errors)) {
$response['message'] = implode(", ", $errors);
echo json_encode($response);
return;
}
// Generate unique 4-digit ticket ID
$ticketId = generateUniqueTicketId();
// Here you would typically:
// 1. Save to database
// 2. Send confirmation email
// 3. Generate ticket
// For now, we'll simulate success
$response['success'] = true;
$response['message'] = "Registration successful!";
$response['ticketId'] = $ticketId;
$response['email'] = $email;
// Optional: Save to file or database
saveRegistration(array(
'ticketId' => $ticketId,
'fullName' => $fullName,
'studentId' => $studentId,
'email' => $email,
'phone' => $phone,
'queries' => $queries,
'paymentMethod' => $paymentMethod,
'paymentNumber' => $paymentNumber,
'transactionId' => $transactionId,
'ticketPrice' => $ticketPrice,
'registrationDate' => date('Y-m-d H:i:s')
));
echo json_encode($response);
}
/**
* Handle Contact Form
*/
function handleContact() {
global $response;
// Get form data
$name = isset($_POST['name']) ? sanitize_input($_POST['name']) : '';
$email = isset($_POST['email']) ? sanitize_input($_POST['email']) : '';
$subject = isset($_POST['subject']) ? sanitize_input($_POST['subject']) : 'Contact Form Submission';
$message = isset($_POST['message']) ? sanitize_input($_POST['message']) : '';
// Validation
$errors = array();
if (empty($name) || strlen($name) < 2) {
$errors[] = "Name must be at least 2 characters";
}
if (empty($email) || !validate_email($email)) {
$errors[] = "Invalid email format";
}
if (empty($message) || strlen($message) < 10) {
$errors[] = "Message must be at least 10 characters";
}
// Check for errors
if (!empty($errors)) {
$response['message'] = implode(", ", $errors);
echo json_encode($response);
return;
}
// Email configuration
$to = "cybersecurity@club.uttara.ac.bd";
$email_subject = "CyberCon Contact: " . $subject;
// Email body
$email_body = "You have received a new message from the CyberCon website contact form.\n\n";
$email_body .= "Here are the details:\n\n";
$email_body .= "Name: $name\n";
$email_body .= "Email: $email\n";
$email_body .= "Subject: $subject\n\n";
$email_body .= "Message:\n$message\n";
// Email headers
$headers = "From: noreply@cybercon.org\r\n";
$headers .= "Reply-To: $email\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
// Send email (uncomment when ready)
// if (mail($to, $email_subject, $email_body, $headers)) {
$response['success'] = true;
$response['message'] = "Thank you! Your message has been sent successfully.";
// } else {
// $response['message'] = "Sorry, there was an error sending your message.";
// }
echo json_encode($response);
}
/**
* Generate unique 4-digit ticket ID
*/
function generateUniqueTicketId() {
$attempts = 0;
$maxAttempts = 10;
$filename = __DIR__ . '/registrations.json';
// Read existing registrations to check for duplicates
$existingIds = [];
if (file_exists($filename)) {
$content = file_get_contents($filename);
$registrations = json_decode($content, true);
if (is_array($registrations)) {
foreach ($registrations as $reg) {
if (isset($reg['ticketId'])) {
$existingIds[] = $reg['ticketId'];
}
}
}
}
while ($attempts < $maxAttempts) {
// Generate ID using time * random number, then get last 4 digits
$timeComponent = microtime(true) * 1000; // milliseconds
$randomComponent = rand(1000, 9999);
$combined = $timeComponent * $randomComponent;
// Get last 4 digits and ensure it's 4 digits
$ticketId = str_pad(substr((string)abs((int)$combined), -4), 4, '0', STR_PAD_LEFT);
// Check if ticket ID already exists
if (!in_array($ticketId, $existingIds)) {
return $ticketId;
}
$attempts++;
usleep(1000); // Wait 1ms before retry
}
// Fallback: use timestamp-based ID
return str_pad(substr((string)time(), -4), 4, '0', STR_PAD_LEFT);
}
/**
* Save registration to file
* (In production, you should save to a database)
*/
function saveRegistration($data) {
$filename = __DIR__ . '/registrations.json';
// Read existing data
$registrations = array();
if (file_exists($filename)) {
$content = file_get_contents($filename);
$registrations = json_decode($content, true);
if (!is_array($registrations)) {
$registrations = array();
}
}
// Add new registration
$registrations[] = $data;
// Save to file
file_put_contents($filename, json_encode($registrations, JSON_PRETTY_PRINT));
}