-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimit.php
More file actions
executable file
·125 lines (109 loc) · 3.91 KB
/
limit.php
File metadata and controls
executable file
·125 lines (109 loc) · 3.91 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
<?php
ignore_user_abort(true);
header("Cache-Control: no-cache");
header("content-type: application/json");
header("Access-Control-Allow-Methods: 'OPTIONS, POST'");
header("Access-Control-Allow-Origin: *"); // Origin
header("Access-Control-Allow-Headers: 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers'");
$webinarShortId = ""; // Webinar short id
$validateWithEMail = true; // email duplication handling
$validateWithIP = false; // IP address duplication handling
$maximumRegistrationCount = 1; // Maximum registration
# Handle preflight request
if ('OPTIONS' == $_SERVER['REQUEST_METHOD']) {
http_response_code(204);
exit;
}
# Handle request
if ('POST' == $_SERVER['REQUEST_METHOD']) {
# Get ip address
$ip = $_SERVER['REMOTE_ADDR'];
# Check content type
if (!isset($_SERVER['CONTENT_TYPE']) || 'application/json' != $_SERVER['CONTENT_TYPE']) {
http_response_code(412);
echo json_encode("content-type is not set to application/json");
exit;
}
# Get user data
$users = json_decode(file_get_contents(__DIR__ . '/user.json'));
# Get request data
$raw_data = file_get_contents("php://input");
if (!$data = json_decode($raw_data)) {
http_response_code(422);
echo json_encode("registration data is not valid json");
exit;
}
# User email
$email = $data->email;
$duplicate = false;
# is email exists
foreach ($users as $key => $user) {
# Check email
if ($validateWithEMail && $email === $user->email) {
# Is maximum count exceed
if ($maximumRegistrationCount < $user->count + 1) {
http_response_code(406);
echo json_encode([
"message" => "you are already registered in this event, please check your email!",
]); // Email matched message
exit;
}
$duplicate = true;
}
# Check ip
if ($validateWithIP && $ip === $user->ip) {
# Is maximum count exceed
if ($maximumRegistrationCount < $user->count + 1) {
http_response_code(406);
echo json_encode([
"message" => "you are already registered in this event, please check your email!",
]); // IP matched message
exit;
}
$duplicate = true;
}
# Update count
if ($duplicate) {
$users[$key]->count = $user->count + 1;
}
}
# Add ip address to custom filed
if (isset($data->customFields)) {
$data->customFields->ip_address = $ip;
} else {
$data->customFields = [
"ip_address" => $ip,
];
}
# Post data to StealthSeminar
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "https://api.joinnow.live/webinars/$webinarShortId/registration");
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen(json_encode($data)),
]);
$response = curl_exec($curl_handle);
$http_code = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
curl_close($curl_handle);
# Return data to user
http_response_code($http_code);
echo $response;
# Add to user list
if (!$duplicate) {
array_push($users, [
"email" => $email,
"ip" => $ip,
"time" => time(),
"count" => 1,
]);
}
if ($http_code === 200) {
file_put_contents(__DIR__ . '/user.json', json_encode($users));
}
exit;
}
# Default response
http_response_code(405);