I agree that the escape function should be moved elsewhere.
However, it is currently defined in header.pl, and using header.pl is precisely what causes the captive portal page to be hidden/not displayed correctly.
I think this function should be moved to general-functions.pl, but that would require refactoring all the CGI pages that currently rely on it being defined in header.pl.
Edit : This discussion should probably be in Bugzilla.