This frustrated me for a while. I found a workaround, but it’s really ugly. Hopefully you guys can tell me where I’m going wrong.
I have a custom plugin, invoked like so:
{exp:my_registration:process to="http://www.site.com/registration/confirm"}
The plugin processes the values specified by the user, which it gets from $IN->GBL(‘blah’, ‘POST’), and when it’s finished, it uses $FNS->redirect() to send the user to the URL specified in the to parameter.
Trouble is, $TMPL->fetch_param(‘to’) returns
http//www.site.com/registration/form
So my workaround is to do the reverse of what core.template.php does on line 444:
443: // Replace forward slashes with entity to prevent preg_replace errors.
444: $this->template = str_replace('/', SLASH, $this->template);
Namely:
if( ! $to_url = $TMPL->fetch_param('to')) {
$to_url = 'http://www.default-value.com/';
} else {
// re-convert / to /, since $TMPL->fetch_param will munge the data
$to_url = str_replace(SLASH, '/', $to_url);
}
Of course, this stinks.
Another thing that stinks: if there are embedded tags in parameter values, they’re not parsed. So I can’t do this:
{exp:my_registration:process to="{path="registration/confirm"}"}
Is there a simple way for me to parse parameter values, and ensure that forward slashes in them aren’t URL-encoded?
Thanks, Ben
The slashes are just gonna be that way, so the only workaround for you is to str_replace them.
A neater way for you to accept input from that parameter would be to allow values such as “registration/confirm” for local addresses, and “http://www.example.com” for external ones.
You could then just do a quick strpos()/substr() to see if http is present, if not, run the input through $FNS->create_url(); Im not sure if create_url can handle &# 47;’s, if it can then bonus.
This is intended, and your solution is fine. When you need actual slashes, just convert the SLASH constant back to ‘/’.
OK, thanks Derek.
On my last question: can I get EE to parse any EE-code inside the parameter values in the template before it gets to my plugin? And if not, is there a simple way to invoke EE’s parser from my plugin, maybe if I detect LD…RD in the value of the parameter?
Thanks! Ben
No, you cannot alter the order of the template parser for variables. The better solution would be to not have your users submit path variables. They should use either a full URL, or the standard template group / template name, which is consistent with the rest of the application. For your part, you can use $FNS->create_url().
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.