Page 1 of 1

[B3.1][ML/MS] OpenLink() uses wrong encoding (ISO, not UTF8)

Posted: 20 Mar 2013, 20:56
by m4rcel
When opening a link using OpenLink() in ManiaScript, the script fails to correctly encode the URL and its parameters.

See the following test script:

Code: Select all

<?php

$script = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];
$param = htmlspecialchars(isset($_GET['param'])? $_GET['param'] : '', ENT_QUOTES, 'UTF-8');

header('Content-Type: text/xml;charset=utf-8');
echo <<<EOT
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<manialink version="1" background="0">
   <entry id="entry" name="inputParam" sizen="100 5" default="Some special characters: äöüß 日本語"/>
   <label id="label" posn="0 -10" style="CardButtonMediumWide" text="Send! (with ManiaScript)" ScriptEvents="1" />
   <label posn="0 -20" style="CardButtonMediumWide" text="Send! (with label-manialink)" manialink="{$script}?param=inputParam" />
   <label posn="0 -30" textcolor="000F">{$param}</label>
   <script><!--
while(True) {
   foreach(Event in PendingEvents) {
      if (Event.Type == CGameManialinkScriptEvent::Type::MouseClick && Event.ControlId == "label") {
         declare Text Link = "{$script}?param=" ^ (Page.GetFirstChild("entry") as CGameManialinkEntry).Value;
         log("OpenLink: " ^ Link);
         OpenLink(Link, ::LinkType::ManialinkBrowser);
      }
   }
   yield;
}
--></script>
</manialink>
EOT;

?>
The value of the entry is appended to the URL as a parameter. When using the "classic" method, the manialink-attribute of a Control, the encoding is UTF8, which is correct. But as soon as you use the Entry-value in OpenLink(), the encoding falls back to ISO-8859-whatever, having the specialk characters destroyed.


Already heard of that bug? Then you may remember my bug report over a year ago, yet it still persists... How about eventually fixing it? :D

Re: [B3.1][ML/MS] OpenLink() uses wrong encoding (ISO, not U

Posted: 21 May 2013, 18:49
by Gugli
Thank for you report.

The problem was a function missing in TextLib.

I can not encode the whole URL : if the URL is "http://localhost/test.php?param1=value1&param2=value2", you don't want the "?", the "=" or the "&" to be encoded, but only "value1" and "value2"

You will have to encode the parameters yourself in the script by calling the brand new function : TextLib::URLEncode(...)

Code: Select all

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- http://localhost/TestOpenLink.xml -->
<manialink version="1" background="0">
   <entry id="entry" name="inputParam" sizen="100 5" default="Some special characters: äöüß 日本語"/>
   <label id="label" posn="0 -10" style="CardButtonMediumWide" text="Send! (using ManiaScript)" ScriptEvents="1" />
   <label posn="0 -20" style="CardButtonMediumWide" text="Send! (using classic method)" manialink="http://localhost/TestOpenLink.xml?param=inputParam" />
   <label posn="0 -30" textcolor="000F">param</label>
   <script><!--
#Include "TextLib" as TextLib
while(True) {
   foreach(Event in PendingEvents) {
      if (Event.Type == CMlEvent::Type::MouseClick && Event.ControlId == "label") {
         declare Text Link = "http://localhost/TestOpenLink.xml?param=";
         Link ^= TextLib::URLEncode((Page.GetFirstChild("entry") as CMlEntry).Value); // It works now !
         log("OpenLink: " ^ Link);
         OpenLink(Link, ::LinkType::ManialinkBrowser);
      }
   }
   yield;
}
--></script>
</manialink>
Thanks again for your report. The function TextLib::URLEncode() will be shipped with the next update.

Re: [B3.1][ML/MS] OpenLink() uses wrong encoding (ISO, not U

Posted: 21 May 2013, 18:52
by steeffeen
that's great! :yes:

Gugli, you're doing quite some work today :lol:

Re: [B3.1][ML/MS] OpenLink() uses wrong encoding (ISO, not U

Posted: 21 May 2013, 19:05
by Gugli
steeffeen wrote: Gugli, you're doing quite some work today :lol:
I should have done most of this much earlier :S.

I've also done a long-overdue chat-accounts migration ^_^.

Let's call it a night ! Have fun ingame ;)

Re: [B3.1][ML/MS] OpenLink() uses wrong encoding (ISO, not U

Posted: 21 May 2013, 19:17
by spaii
Nice !! :thumbsup: