Skip to content

Commit 802701a

Browse files
authored
feat(search): extend input compatibility to all steam id formats (#13)
1 parent 7230be1 commit 802701a

File tree

1 file changed

+84
-6
lines changed

1 file changed

+84
-6
lines changed

web/pages/search-class.php

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,52 @@
3636
For support and installation notes visit http://www.hlxcommunity.com
3737
*/
3838

39+
// Helper functions for SteamID conversions
40+
41+
/**
42+
* Convert Steam64 ID to database format (1:Z)
43+
* @param string $steam64
44+
* @return string|false
45+
*/
46+
function steam64ToDatabaseFormat($steam64) {
47+
if (!is_numeric($steam64)) {
48+
return false;
49+
}
50+
$steam64 = (int)$steam64;
51+
$y = $steam64 % 2;
52+
$z = ($steam64 - 76561197960265728) >> 1;
53+
return "$y:$z";
54+
}
55+
56+
/**
57+
* Convert Steam3 ID to database format (1:Z)
58+
* @param string $steam3
59+
* @return string|false
60+
*/
61+
function steam3ToDatabaseFormat($steam3) {
62+
if (preg_match("/^U:1:(\d+)$/", $steam3, $matches)) {
63+
$z = (int)$matches[1];
64+
$y = $z % 2;
65+
$z = $z >> 1;
66+
return "$y:$z";
67+
}
68+
return false;
69+
}
70+
71+
/**
72+
* Convert Steam2 ID to database format (1:Z)
73+
* @param string $steam2
74+
* @return string|false
75+
*/
76+
function steam2ToDatabaseFormat($steam2) {
77+
if (preg_match("/^STEAM_\d+:(\d+):(\d+)$/", $steam2, $matches)) {
78+
$y = (int)$matches[1];
79+
$z = (int)$matches[2];
80+
return "$y:$z";
81+
}
82+
return false;
83+
}
84+
3985
// Search Class
4086
class Search
4187
{
@@ -60,7 +106,7 @@ function __construct($query, $type, $game)
60106
}
61107
}
62108

63-
function drawForm ($getvars = array(), $searchtypes = -1)
109+
function drawForm($getvars = array(), $searchtypes = -1)
64110
{
65111
global $g_options, $db;
66112

@@ -91,7 +137,7 @@ function drawForm ($getvars = array(), $searchtypes = -1)
91137
<tr style="vertical-align:middle;" class="bg1">
92138
<td nowrap="nowrap" style="width:30%;">Search For:</td>
93139
<td style="width:70%;">
94-
<input type="text" name="q" size="20" maxlength="128" value="<?php echo htmlspecialchars($this->query, ENT_QUOTES); ?>" style="width:300px;" />
140+
<input type="text" name="q" size="20" maxlength="128" value="<?php echo htmlspecialchars($this->query, ENT_QUOTES); ?>" style="width:300px;" placeholder="Enter Name, Steam2, Steam3, or Steam64 ID" />
95141
</td>
96142
</tr>
97143
<tr style="vertical-align:middle;" class="bg1">
@@ -106,7 +152,7 @@ function drawForm ($getvars = array(), $searchtypes = -1)
106152
<td nowrap="nowrap" style="width:30%;">Game:</td>
107153
<td style="width:70%;">
108154
<?php
109-
$games = array ();
155+
$games = array();
110156
$games[''] = '(All)';
111157
$result = $db->query("
112158
SELECT
@@ -139,7 +185,7 @@ function drawForm ($getvars = array(), $searchtypes = -1)
139185

140186
<?php
141187
}
142-
function drawResults ($link_player=-1, $link_clan=-1)
188+
function drawResults($link_player = -1, $link_clan = -1)
143189
{
144190
global $g_options, $db;
145191
if ($link_player == -1) $link_player = "mode=playerinfo&amp;player=%k";
@@ -152,9 +198,19 @@ function drawResults ($link_player=-1, $link_clan=-1)
152198
<br /><br />
153199

154200
<?php
155-
$sr_query = preg_replace('/^STEAM_\d+?\:/i','',$this->query);
201+
if (empty($this->query))
202+
{
203+
echo "Search query cannot be empty.";
204+
return;
205+
}
206+
207+
if ($this->type != 'uniqueid') // Always remove the SteamID prefix for non-uniqueid searches
208+
{
209+
$sr_query = preg_replace('/^STEAM_\d+?\:/i','',$this->query);
210+
$sr_query = preg_replace('/\s/', '%', $sr_query);
211+
}
212+
156213
$sr_query = $db->escape($sr_query);
157-
$sr_query = preg_replace('/\s/', '%', $sr_query);
158214
if ($this->type == 'player')
159215
{
160216
$table = new Table
@@ -248,6 +304,28 @@ function drawResults ($link_player=-1, $link_clan=-1)
248304
}
249305
elseif ($this->type == 'uniqueid')
250306
{
307+
// Detect and convert SteamID to database format
308+
$sr_query = trim($this->query);
309+
$sr_query = trim($this->query, "[]");
310+
311+
if (preg_match("/^U:1:\d+$/", $sr_query)) // Convert Steam3 to database format
312+
{
313+
$sr_query = steam3ToDatabaseFormat($sr_query);
314+
}
315+
elseif (is_numeric($sr_query) && strlen($sr_query) >= 17) // Convert Steam64 to database format
316+
{
317+
$sr_query = steam64ToDatabaseFormat($sr_query);
318+
}
319+
elseif (preg_match("/^STEAM_\d+:\d+:\d+$/", $sr_query)) // Convert Steam2 to database format
320+
{
321+
$sr_query = steam2ToDatabaseFormat($sr_query);
322+
}
323+
else
324+
{
325+
echo "Invalid SteamID format.";
326+
return; // Stop execution if the SteamID is invalid
327+
}
328+
251329
$table = new Table
252330
(
253331
array

0 commit comments

Comments
 (0)