forked from Talishar/Talishar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubmitChat.php
More file actions
193 lines (166 loc) · 7.21 KB
/
SubmitChat.php
File metadata and controls
193 lines (166 loc) · 7.21 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
<?php
error_reporting(E_ALL);
// Start output buffering to catch any accidental output
ob_start();
session_start();
// CRITICAL: Capture all needed session data immediately and release the lock
// PHP sessions use exclusive file locks - holding the lock during file I/O
// blocks all other requests from this user, causing session deadlock.
$sessionP1AuthKey = $_SESSION["p1AuthKey"] ?? null;
$sessionP2AuthKey = $_SESSION["p2AuthKey"] ?? null;
$sessionUserUid = $_SESSION['useruid'] ?? null;
$sessionIsPatron = isset($_SESSION["isPatron"]);
$sessionIsPvtVoidPatron = isset($_SESSION["isPvtVoidPatron"]);
// Release session lock NOW - before any file operations
session_write_close();
include "Libraries/HTTPLibraries.php";
include_once "Libraries/SHMOPLibraries.php";
include_once "includes/dbh.inc.php";
include_once "includes/MetafyHelper.php";
SetHeaders();
$gameName = $_GET["gameName"];
if (!IsGameNameValid($gameName)) {
http_response_code(400);
die("Invalid game name.");
}
$playerID = intval($_GET["playerID"]);
if($playerID !== 1 && $playerID !== 2) {
http_response_code(400);
die("Invalid player ID.");
}
$authKey = $_GET["authKey"] ?? "";
if ($authKey === "" && ($playerID == 1 || $playerID == 2)) {
if (isset($_COOKIE["lastAuthKey"])) $authKey = $_COOKIE["lastAuthKey"];
}
// Load game file to get Metafy tiers - this populates $p1MetafyTiers and $p2MetafyTiers
include "MenuFiles/ParseGamefile.php";
$allowedOrigins = ['https://talishar.net', 'https://www.talishar.net'];
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
if (in_array($origin, $allowedOrigins)) {
header("Access-Control-Allow-Origin: $origin");
} else {
// If it's a cross-origin request from an unauthorized site
if (!empty($origin)) {
http_response_code(403);
die("CORS policy: Origin not allowed: $origin");
}
}
$targetAuthKey = "";
if ($playerID == 1 && $p1Key !== null) $targetAuthKey = $p1Key;
else if ($playerID == 2 && $p2Key !== null) $targetAuthKey = $p2Key;
if($targetAuthKey === "" || $targetAuthKey === null) {
http_response_code(400);
die("Game does not exist.");
}
if ($authKey !== $targetAuthKey) {
if (isset($_COOKIE["lastAuthKey"])) $authKey = $_COOKIE["lastAuthKey"];
if ($authKey !== $targetAuthKey) {
http_response_code(403);
die("Invalid auth key: " . htmlspecialchars($authKey));
}
}
$uid = "-";
if ($sessionUserUid !== null) $uid = $sessionUserUid;
else $uid = $playerID == 1 ? $p1uid : $p2uid;
if($uid == "starmorgs") exit;
$displayName = ($uid != "-" ? $uid : "Player " . $playerID);
$chatText = "";
if (tryGet("quickChat")) {
$chatText = parseQuickChat($_GET["quickChat"]);
} elseif (isset($_GET["chatText"]) && $_GET["chatText"] !== "") {
$chatText = htmlspecialchars($_GET["chatText"]);
}
// Don't write anything if there's no actual message
if ($chatText === "") {
http_response_code(403);
die("No message.");
}
//array for contributors
$contributors = ["sugitime", "OotTheMonk", "LaustinSpayce", "Tower", "Etasus", "Aegisworn", "PvtVoid", "Bluffkin"];
// List of mod usernames - should match frontend list
$modUsernames = ["OotTheMonk", "LaustinSpayce", "Tower", "PvtVoid", "Aegisworn", "Bluffkin"];
$metafyTiers = ($playerID == 1 ? $p1MetafyTiers : $p2MetafyTiers) ?? [];
if (!is_array($metafyTiers)) $metafyTiers = [];
// Check for Metafy badges first - if user has Metafy badges, only show those
$hasMetafyBadges = false;
if(!empty($metafyTiers)) {
$metafyBadgeHtml = '';
foreach($metafyTiers as $tier) {
$tierImage = GetMetafyTierImage($tier);
if($tierImage) {
$metafyBadgeHtml .= "<a href='https://www.metafy.gg' target='_blank' rel='noopener noreferrer'><img alt='' title='I am a Metafy Supporter of Talishar 💖' style='margin-bottom:3px; height:16px;' src='" . $tierImage . "'/></a>";
}
}
if(!empty($metafyBadgeHtml)) {
$displayName = $metafyBadgeHtml . $displayName;
$hasMetafyBadges = true;
}
}
// Only show Patreon badges if user doesn't have Metafy badges
if(!$hasMetafyBadges) {
//its sort of sloppy, but it this will fail if you're in the contributors array because we want to give you the contributor icon, not the patron icon.
if($sessionIsPatron && $sessionUserUid !== null && !in_array($sessionUserUid, $contributors)) {
$displayName = "<a href='https://metafy.gg/@Talishar' target='_blank' rel='noopener noreferrer'><img title='I am a Metafy Supporter of Talishar 💖' style='margin-bottom:3px; height:16px;' src='./images/patronHeart.webp' /></a>" . $displayName;
}
//This is the code for PvtVoid Patreon
if($sessionIsPvtVoidPatron || $sessionUserUid !== null && in_array($sessionUserUid, ["PvtVoid"])) {
$displayName = "<a href='https://metafy.gg/@Talishar' target='_blank' rel='noopener noreferrer'><img title='I am a Metafy Supporter of Talishar 💖' style='margin-bottom:3px; height:16px;' src='./images/patronEye.webp'/></a>" . $displayName;
}
}
//This is the code for Contributor's icon.
if($sessionUserUid !== null && in_array($sessionUserUid, $contributors)) {
$displayName = "<a href='https://metafy.gg/@Talishar' target='_blank' rel='noopener noreferrer'><img title='I am a developer of Talishar!' style='margin-bottom:3px; height:16px;' src='./images/copper.webp' /></a>" . $displayName;
}
$filename = "./Games/" . $gameName . "/gamelog.txt";
$handler = fopen($filename, "a");
// Check if user is a mod - use gold color (#f0d666) for mods, otherwise use player color
$isMod = $sessionUserUid !== null && in_array($sessionUserUid, $modUsernames);
$chatColor = $isMod ? "#a58703ff" : "<PLAYER" . $playerID . "COLOR>";
$output = "<span style='font-weight:bold; color:" . $chatColor . ";'>" . $displayName . ": </span>" . $chatText;
if ($handler) {
fwrite($handler, $output . "\r\n");
if (GetCachePiece($gameName, 11) >= 3) fwrite($handler, "The lobby is reactivated.\r\n");
fclose($handler);
}
GamestateUpdated($gameName);
if ($playerID == 1) SetCachePiece($gameName, 11, 0);
function parseQuickChat($inputEnum)
{
switch($inputEnum) {
case "1": return "Hello";
case "2": return "Good luck, have fun";
case "3": return "Are you there?";
case "4": return "Be right back";
case "5": return "Can I undo?";
case "6": return "Thanks for the game!";
case "7": return "No problem!";
case "8": return "Thinking... Please bear with me!";
case "9": return "Sorry!";
case "10": return "Refresh the page";
case "11": return "Good Game!";
case "12": return "I have to go";
case "13": return "I think there is a bug";
case "14": return "No thanks";
case "15": return "Okay!";
case "16": return "Do you want to undo?";
case "17": return "Rematch?";
case "18": return "Thanks!";
case "19": return "That was really cool!";
case "20": return "Want to Chat?";
case "21": return "Well played!";
case "22": return "Whoops!";
case "23": return "Yes";
default: return "";
};
}
function GetMetafyTierImage($tierName) {
$tierImages = [
'Fyendal Supporters' => './images/fyendal.webp',
'Seers of Ophidia' => './images/ophidia.webp',
'Arknight Shards' => './images/arknight.webp',
'Lover of Grandeur' => './images/grandeur.webp',
'Sponsors of Trōpal-Dhani' => './images/tropal.webp',
'Light of Sol Gemini Circle' => './images/lightofsol.webp'
];
return $tierImages[$tierName] ?? null;
}