Skip to content

Commit

Permalink
Add WebFix_URLEncode support
Browse files Browse the repository at this point in the history
* Add urlencode 

URLEncode is necessary
Otherwise the redirection of the wrong  some address is in a particular case 
for example
https://bbs.93x.net/forum.php?mod=viewthread&tid=89686#newpost

https://csgottt.com/redirect.html?url=https://bbs.93x.net/forum.php?mod=viewthread&tid=89686#newpost
equal = https://bbs.93x.net/forum.php?mod=viewthread is an incorrect address

* Update webfix.inc

URLEncode is necessary
Otherwise the redirection of the wrong  some address is in a particular case 
for example
https://bbs.93x.net/forum.php?mod=viewthread&tid=89686#newpost

https://csgottt.com/redirect.html?url=https://bbs.93x.net/forum.php?mod=viewthread&tid=89686#newpost
equal = https://bbs.93x.net/forum.php?mod=viewthread is an incorrect address

* Some code style adjustments

* Some code style adjustments

* Some code style adjustments
  • Loading branch information
e54385991 authored and Bara committed Jun 25, 2018
1 parent 0ac8c02 commit 45c3c85
Showing 1 changed file with 182 additions and 106 deletions.
288 changes: 182 additions & 106 deletions addons/sourcemod/scripting/include/webfix.inc
Expand Up @@ -32,8 +32,8 @@ static bool s_WebFix_IsRedirectUrlSet = false;
*/
stock void WebFix_SetRedirectUrl(char[] url)
{
Format(s_WebFix_RedirectUrl, sizeof(s_WebFix_RedirectUrl), url);
s_WebFix_IsRedirectUrlSet = true;
Format(s_WebFix_RedirectUrl, sizeof(s_WebFix_RedirectUrl), url);
s_WebFix_IsRedirectUrlSet = true;
}

/**
Expand All @@ -47,41 +47,56 @@ stock void WebFix_SetRedirectUrl(char[] url)
* @param height Optional pointer to store allow/deny setting. 0 (default) uses clients full screen height.
* @return true if successful, false if error encountered.
*/
stock bool WebFix_OpenUrl(int client, const char[] title, const char[] url, bool hidden = false, int width = 0, int height = 0)
stock bool WebFix_OpenUrl(int client, const char[] title, const char[] url, bool hidden = false, int width = 0, int height = 0,bool UrlEncode = true)
{
//Attempt to read redirect url from config file
if (!s_WebFix_IsRedirectUrlSet) {
WebFix_ReadRedirectUrlFromConfig();
}
//Attempt to read redirect url from config file
if (!s_WebFix_IsRedirectUrlSet)
{
WebFix_ReadRedirectUrlFromConfig();
}

//If url is still not set and we're using non-hidden mode, fail
if (!s_WebFix_IsRedirectUrlSet && !hidden)
{
LogError("Attempting to use WebFix_OpenUrl in non-hidden mode without a redirect URL being set.");
return false;
}

//If url is still not set and we're using non-hidden mode, fail
if (!s_WebFix_IsRedirectUrlSet && !hidden) {
LogError("Attempting to use WebFix_OpenUrl in non-hidden mode without a redirect URL being set.");
return false;
}

//Confirm URL is valid
if (StrContains(url, "http://", false) != 0 && StrContains(url, "https://", false) != 0 && !StrEqual(url, "about:blank")) {
LogError("Invalid protocol provided. Only 'http://', 'https://' and 'about:blank' are supported.");
return false;
}

if (!IsClientInGame(client) || IsFakeClient(client))
return false;

//Load invalid (non-existing) URL
ShowMOTDPanel(client, "WebFix", "http://invaliddomain.invalidtld", MOTDPANEL_TYPE_URL);

DataPack pack;
CreateDataTimer(1.0, WebFix_OpenUrl_OpenTarget, pack);
pack.WriteCell(EntIndexToEntRef(client));
pack.WriteString(title);
pack.WriteString(url);
pack.WriteCell(hidden);
pack.WriteCell(width);
pack.WriteCell(height);

return true;
//Confirm URL is valid
if (!UrlEncode && StrContains(url, "http://", false) != 0 && StrContains(url, "https://", false) != 0 && !StrEqual(url, "about:blank"))
{
LogError("Invalid protocol provided. Only 'http://', 'https://' and 'about:blank' are supported.error-> %s",url);
return false;
}

char sUrlencode[WEBFIX_URL_LENGTH];
if(UrlEncode)
{
WebFix_URLEncode(url,sUrlencode,WEBFIX_URL_LENGTH);
}
else
{
strcopy(sUrlencode,WEBFIX_URL_LENGTH,url);
}

if (!IsClientInGame(client) || IsFakeClient(client))
{
return false;
}

//Load invalid (non-existing) URL
ShowMOTDPanel(client, "WebFix", "http://invaliddomain.invalidtld", MOTDPANEL_TYPE_URL);

DataPack pack;
CreateDataTimer(1.0, WebFix_OpenUrl_OpenTarget, pack);
pack.WriteCell(EntIndexToEntRef(client));
pack.WriteString(title);
pack.WriteString(sUrlencode);
pack.WriteCell(hidden);
pack.WriteCell(width);
pack.WriteCell(height);

return true;
}

/**
Expand All @@ -96,82 +111,143 @@ stock bool WebFix_OpenUrl(int client, const char[] title, const char[] url, bool
*/
stock bool WebFix_OpenUrlAll(const char[] title, const char[] url, bool hidden = false, int width = 0, int height = 0)
{
bool returnValue = true;
for (int i = 1; i <= MaxClients; ++i) {
if (IsClientInGame(i) && !IsFakeClient(i)){
bool success = WebFix_OpenUrl(i, title, url, hidden, width, height);
if (!success)
returnValue = false;
}
}

return returnValue;
bool returnValue = true;
for (int i = 1; i <= MaxClients; ++i)
{
if (IsClientInGame(i) && !IsFakeClient(i))
{
bool success = WebFix_OpenUrl(i, title, url, hidden, width, height);
if (!success)
{
returnValue = false;
}
}
}

return returnValue;
}

//Internal Command! Do not call.

/**
* Internal Command! Do not call.
*/
public Action WebFix_OpenUrl_OpenTarget(Handle timer, DataPack pack)
{
pack.Reset();

int client = EntRefToEntIndex(pack.ReadCell());

if (!IsClientInGame(client))
return Plugin_Handled;

char title[WEBFIX_TITLE_LENGTH];
pack.ReadString(title, sizeof(title));
char url[WEBFIX_URL_LENGTH];
pack.ReadString(url, sizeof(url));
bool hidden = view_as<bool>(pack.ReadCell());
int width = pack.ReadCell();
int height = pack.ReadCell();

//Use redirect url for non-hidden windows
if (!hidden)
Format(url, sizeof(url), "%s?url=%s&width=%d&height=%d", s_WebFix_RedirectUrl, url, width, height);

//Show VGUI Panel
Handle panel = CreateKeyValues("data");

KvSetString(panel, "title", title);
KvSetNum(panel, "type", MOTDPANEL_TYPE_URL);
KvSetString(panel, "msg", url);
ShowVGUIPanel(client, "info", panel, hidden);
delete panel;

return Plugin_Handled;
pack.Reset();

int client = EntRefToEntIndex(pack.ReadCell());

if (!IsClientInGame(client))
{
return Plugin_Handled;
}

char title[WEBFIX_TITLE_LENGTH];
pack.ReadString(title, sizeof(title));
char url[WEBFIX_URL_LENGTH];
pack.ReadString(url, sizeof(url));
bool hidden = view_as<bool>(pack.ReadCell());
int width = pack.ReadCell();
int height = pack.ReadCell();

//Use redirect url for non-hidden windows
if (!hidden)
{
Format(url, sizeof(url), "%s?url=%s&width=%d&height=%d", s_WebFix_RedirectUrl, url, width, height);
}

//Show VGUI Panel
Handle panel = CreateKeyValues("data");

KvSetString(panel, "title", title);
KvSetNum(panel, "type", MOTDPANEL_TYPE_URL);
KvSetString(panel, "msg", url);
ShowVGUIPanel(client, "info", panel, hidden);
delete panel;

return Plugin_Handled;
}

//Internal Command
/**
* Internal Command! Do not call.
*/
static void WebFix_ReadRedirectUrlFromConfig()
{
char configFilePath[PLATFORM_MAX_PATH];
Format(configFilePath, sizeof(configFilePath), "configs/webfix_redirecturl.txt");
BuildPath(Path_SM, configFilePath, PLATFORM_MAX_PATH, configFilePath);

if (FileExists(configFilePath)) {
//Open config file
File file = OpenFile(configFilePath, "r");

if (file != null) {
char buffer[PLATFORM_MAX_PATH];
if (file.ReadLine(buffer, sizeof(buffer))) { //Read a single line

//Remove final new line
//buffer length > 0 check needed in case file is completely empty and there is no new line '\n' char after empty string ""
if (strlen(buffer) > 0 && buffer[strlen(buffer) - 1] == '\n')
buffer[strlen(buffer) - 1] = '\0';

//Remove any whitespace at either end
TrimString(buffer);

if (StrContains(buffer, "http://", false) != 0 && StrContains(buffer, "https://", false) != 0)
LogError("Invalid redirect URL read from config file. Only 'http://' and 'https://' are supported.");
else
WebFix_SetRedirectUrl(buffer);
}
}

file.Close();
}
}
char configFilePath[PLATFORM_MAX_PATH];
Format(configFilePath, sizeof(configFilePath), "configs/webfix_redirecturl.txt");
BuildPath(Path_SM, configFilePath, PLATFORM_MAX_PATH, configFilePath);

if (FileExists(configFilePath))
{
//Open config file
File file = OpenFile(configFilePath, "r");

if (file != null)
{
char buffer[PLATFORM_MAX_PATH];
if (file.ReadLine(buffer, sizeof(buffer))) //Read a single line
{
//Remove final new line
//buffer length > 0 check needed in case file is completely empty and there is no new line '\n' char after empty string ""
if (strlen(buffer) > 0 && buffer[strlen(buffer) - 1] == '\n')
{
buffer[strlen(buffer) - 1] = '\0';
}

//Remove any whitespace at either end
TrimString(buffer);

if (StrContains(buffer, "http://", false) != 0 && StrContains(buffer, "https://", false) != 0)
{
LogError("Invalid redirect URL read from config file. Only 'http://' and 'https://' are supported.");
}
else
{
WebFix_SetRedirectUrl(buffer);
}
}
}

file.Close();
}
}

stock int WebFix_URLEncode( const char[] szInput, char[] szOutput, const int iLen)
{
static const HEXCHARS[ 16 ] = {
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
};

int iPos, cChar, iFLen;
while( ( cChar = szInput[ iPos ] ) && iFLen < iLen )
{
if( cChar == 0x20 )
szOutput[ iFLen++ ] = 0x2B;
else if( !( 0x41 <= cChar <= 0x5A )
&& !( 0x61 <= cChar <= 0x7A )
&& !( 0x30 <= cChar <= 0x39 )
&& cChar != 0x2D
&& cChar != 0x2E
&& cChar != 0x5F )
{

if( ( iFLen + 3 ) > iLen )
break;
else if( cChar > 0xFF
|| cChar < 0x00 )
cChar = 0x2A;

szOutput[ iFLen++ ] = 0x25;
szOutput[ iFLen++ ] = HEXCHARS[ cChar >> 4 ];
szOutput[ iFLen++ ] = HEXCHARS[ cChar & 15 ];
}
else
szOutput[ iFLen++ ] = cChar;

iPos++;
}

szOutput[ iFLen ] = 0;
return iFLen;
}

0 comments on commit 45c3c85

Please sign in to comment.