-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathsupport.php
More file actions
351 lines (313 loc) · 14.9 KB
/
support.php
File metadata and controls
351 lines (313 loc) · 14.9 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
session_start();
require_once("classes/_autoload.php");
$system = System::initialize();
$system->db->startTransaction();
$guest_support = true;
$self_link = $system->router->base_url . 'support.php';
$staff_level = 0;
$user_id = 0;
$player = null;
if(isset($_SESSION['user_id'])) {
$guest_support = false;
$player = User::loadFromId($system, $_SESSION['user_id']);
$player->loadData();
$system->setLayoutByName($player->layout);
$staff_level = $player->staff_level;
$user_id = $player->user_id;
$supportSystem = new SupportManager($system, $player);
}
else {
$system->setLayoutByName('shadow_ribbon');
$supportSystem = new SupportManager($system);
}
$request_types = $supportSystem->getSupportTypes($staff_level);
$supportCreated = false;
$system->layout->renderBeforeContentHTML(
system: $system,
player: $player ?? null,
page_title: "Support",
render_header: !is_null($player),
render_sidebar: !is_null($player),
render_topbar: false
);
if($player != null) {
//Form submitted // 11/6/21 SM{V2} supported
if(isset($_POST['add_support']) || isset($_POST['add_support_prem']) || isset($_POST['confirm_prem_support'])) {
try {
$addSupport = true;
$request_type = $system->db->clean($_POST['support_type']);
$subject = $system->db->clean($_POST['subject']);
$subjectLength = strlen($subject);
$message = $system->db->clean($_POST['message']);
$messageLength = strlen($message);
$cost = ($supportSystem->requestPremiumCosts[$request_type] ?? 0);
$premium = ($cost > 0 && isset($_POST['confirm_prem_support'])) ? 1 : 0;
// Validate support
if (!in_array($request_type, $request_types)) {
throw new RuntimeException("Invalid support type!");
}
if ($subjectLength < SupportManager::$validationConstraints['subject']['min']) {
throw new RuntimeException("Subject must be at least " .
SupportManager::$validationConstraints['subject']['min'] . " characters!");
}
if ($subjectLength > SupportManager::$validationConstraints['subject']['max']) {
throw new RuntimeException("Subject must not exceed " .
SupportManager::$validationConstraints['subject']['max'] . " characters!");
}
if ($messageLength < SupportManager::$validationConstraints['message']['min']) {
throw new RuntimeException("Content must be at least " .
SupportManager::$validationConstraints['message']['min'] . " characters!");
}
if ($messageLength > SupportManager::$validationConstraints['message']['max']) {
throw new RuntimeException("Content must not exceed " .
SupportManager::$validationConstraints['message']['max'] . " characters!");
}
// Premium cost
if (isset($_POST['confirm_prem_support'])) {
if($player->getPremiumCredits() < $cost) {
throw new RuntimeException("You need {$cost}AK for this request.");
}
$player->subtractPremiumCredits($cost, "Submitted premium {$request_type} support");
$player->updateData();
}
if(isset($_POST['add_support_prem']) && $cost != 0) {
require('templates/premiumSupportConfirmation.php');
$addSupport = false; //Confirmation required
}
// Add support
if($addSupport) {
if ($supportSystem->createSupport($player->user_name, $request_type, $subject, $message, $premium)) {
$system->message("Support Submitted!");
}
else {
$system->message("Error submitting support.");
}
}
}catch (Exception $e) {
$system->db->rollbackTransaction();
error_log($e->getMessage());
$system->message($e->getMessage());
}
}
if(isset($_POST['add_guest_support'])){
try {
$support_key = $system->db->clean($_POST['support_key']);
$support_id = $supportSystem->getSupportIdByKey($support_key);
if(!$support_id) {
throw new RuntimeException("Support not found!");
}
if($supportSystem->assignGuestSupportToUser($support_id)) {
$system->message("Support assigned to account!");
} else {
$system->message("Error adding support to account or support already assigned!");
}
}catch (Exception $e) {
$system->message($e->getMessage());
}
}
if($system->message && !$system->message_displayed) {
$system->printMessage();
}
if(!isset($_GET['support_id'])) {
// New Ticket form
require('templates/supportTicketForm.php');
// Load user tickets
$supports = $supportSystem->fetchUserSupports();
if (!empty($supports)) {
require('templates/userTickets.php');
}
}
else {
$support_id = (int) $_GET['support_id'];
$support = $supportSystem->fetchSupportByID($support_id);
if(!$support) {
$system->message("Support not found!");
$system->printMessage();
} else {
if(isset($_POST['add_response'])) {
try {
$message = $system->db->clean($_POST['message']);
$messageLength = strlen($message);
// Validate
if ($messageLength < SupportManager::$validationConstraints['message']['min']) {
throw new RuntimeException("Content must be at least " .
SupportManager::$validationConstraints['message']['min'] . " characters!");
}
if ($messageLength > SupportManager::$validationConstraints['message']['max']) {
throw new RuntimeException("Content must not exceed " .
SupportManager::$validationConstraints['message']['max'] . " characters!");
}
if ($supportSystem->addSupportResponse($support_id, $player->user_name, $message)) {
$system->message("Response added!");
}
else {
throw new RuntimeException("Error adding response!");
}
} catch (Exception $e) {
$system->db->rollbackTransaction();
error_log($e->getMessage());
$system->message($e->getMessage());
}
}
if(isset($_POST['close_ticket'])) {
try {
$message = $system->db->clean($_POST['message']);
$messageLength = strlen($message);
// Validate user owns support
if($support['user_id'] != $player->user_id) {
throw new RuntimeException("You can only close your supports!");
}
// Add resopnse
if($message != '') {
// Validate
if ($messageLength < SupportManager::$validationConstraints['message']['min']) {
throw new RuntimeException("Content must be at least " .
SupportManager::$validationConstraints['message']['min'] . " characters!");
}
if ($messageLength > SupportManager::$validationConstraints['message']['max']) {
throw new RuntimeException("Content must not exceed " .
SupportManager::$validationConstraints['message']['max'] . " characters!");
}
$supportSystem->addSupportResponse($support_id, $player->user_name, $message);
if(!$system->db->last_insert_id) {
throw new RuntimeException("Error adding response.");
}
}
if($supportSystem->closeSupport($support_id)) {
$support['open'] = 0;
$system->message("Support closed.");
}
}catch (Exception $e) {
$system->db->rollbackTransaction();
error_log($e->getMessage());
$system->message($e->getMessage());
}
}
$responses = $supportSystem->fetchSupportResponses($support_id);
$self_link .= "?support_id=" . $support_id;
if($system->message && !$system->message_displayed) {
$system->printMessage();
}
require('templates/displayTicket.php');
}
}
if ($system->layout->key != "new_geisha") {
// Load side menu
$system->layout->renderSideMenu($player, $player->system->router);
}
}
else {
// Get support data
if(isset($_GET['support_key'])) {
$support_key = $system->db->clean($_GET['support_key']);
$email = $system->db->clean($_GET['email']);
$supportData = $supportSystem->fetchSupportByKey($support_key, $email);
if(!$supportData) {
$system->message("Support not found!");
}else {
$responses = $supportSystem->fetchSupportResponses($supportData['support_id']);
}
}
// Add guest support
if(isset($_POST['add_support'])) {
try {
$subject = $system->db->clean($_POST['subject']);
$subjectLength = strlen($subject);
$email = $system->db->clean($_POST['email']);
$support_type = $system->db->clean($_POST['support_type']);
$name = $system->db->clean($_POST['name']);
$message = $system->db->clean($_POST['message']);
$messageLength = strlen($message);
$support_key = sha1(mt_rand(0, 255384));
// Name validation
if($name == '') {
throw new RuntimeException("You must enter your name or display name.");
}
if(strlen($name) < 3) {
throw new RuntimeException("Your name must be at least 3 characters long.");
}
if(strlen($name) > 75) {
throw new RuntimeException("Your name may not exceed 75 characters. Please shorten or use a nick name.");
}
// Email validation
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
throw new RuntimeException("You must provide a valid email!");
}
if(in_array(strtolower(explode('@', $email)[1]), System::UNSERVICEABLE_EMAIL_DOMAINS)) {
throw new RuntimeException("We are currently unable to support " .
implode(' / ', System::UNSERVICEABLE_EMAIL_DOMAINS) . " email types.");
}
// Subject validation
if($subjectLength < SupportManager::$validationConstraints['subject']['min']) {
throw new RuntimeException("Subject must be at least " . SupportManager::$validationConstraints['subject']['min']
. " characters long.");
}
if($subjectLength > SupportManager::$validationConstraints['subject']['max']) {
throw new RuntimeException("Subject may not exceed " . SupportManager::$validationConstraints['subject']['max']
. " characters.");
}
// Message validation
if($messageLength < SupportManager::$validationConstraints['message']['min']) {
throw new RuntimeException("Details must be at least " . SupportManager::$validationConstraints['message']['min']
. " characters long.");
}
if($messageLength > SupportManager::$validationConstraints['message']['max']) {
throw new RuntimeException("Details may not exceed " . SupportManager::$validationConstraints['message']['max']
. " characters.");
}
// Create support
if($supportSystem->createSupport($name, $support_type, $subject, $message, 0, $email, $support_key)) {
$supportCreated = true;
// Send email to user
$subject = "Shinobi-Chronicles support request";
$message = "Thank you for submitting your support. Click the link below to access your support: \r\n" .
"{$system->router->base_url}support.php?support_key={$support_key} \r\n" .
"If the link does not work, your support key is: {$support_key}";
$headers = "From: Shinobi-Chronicles<" . System::SC_ADMIN_EMAIL . ">" . "\r\n";
$headers .= "Reply-To: " . System::SC_NO_REPLY_EMAIL . "\r\n";
if(!mail($email, $subject, $message, $headers)) {
$system->message("Email failed to send! Make sure you save your support key somewhere!");
}
} else {
$system->message("Error creating support.");
}
}catch(RuntimeException $e) {
$system->db->rollbackTransaction();
error_log($e->getMessage());
$system->message($e->getMessage());
}
}
// Add guest response
if(isset($_POST['add_response'])) {
try {
$message = $system->db->clean($_POST['message']);
// Message validation
if(strlen($message) < SupportManager::$validationConstraints['message']['min']) {
throw new RuntimeException("Response must be at least " . SupportManager::$validationConstraints['message']['min'] . " characters.");
}
if(strlen($message) > SupportManager::$validationConstraints['message']['max']) {
throw new RuntimeException("Response may not exceed " . SupportManager::$validationConstraints['message']['max'] . " characters.");
}
if(!isset($supportData) || !$supportData) {
throw new RuntimeException("Support not found!");
}
if($supportSystem->addSupportResponse($supportData['support_id'], $supportData['user_name'], $message)) {
$system->message("Response added!");
$responses = $supportSystem->fetchSupportResponses($supportData['support_id']);
} else {
throw new RuntimeException("Error adding response!");
}
}catch (Exception $e) {
$system->db->rollbackTransaction();
$system->message($e->getMessage());
}
}
// Print system message
if($system->message != '' && !$system->message_displayed) {
$system->printMessage();
}
require('templates/guestSupport.php');
}
$system->layout->renderAfterContentHTML($system, $player);
$system->db->commitTransaction();