SimplePortal

Support => English Support => Topic started by: pluteus on November 22, 2015, 02:08:26 PM

Title: cutom html block problem
Post by: pluteus on November 22, 2015, 02:08:26 PM
i recently added a custom html block to the portal on our forum with links to facebook and twitter on it,
and am now getting this error in the logs.

8192: : The /e modifier is deprecated, use preg_replace_callback instead
Apply Filter: Only show the errors from this file
File: /var/www/html/forum/Sources/PortalBlocks.php
Line: 2028



this is line 2028 from sourced.php
   $data = preg_replace('~<\!\[CDATA\[(.+?)\]\]>~e' . ($context['utf8'] ? 'u' : ''), '\'#cdata_escape_encode#\' . $smcFunc[\'htmlspecialchars\'](\'$1\')', $data);


i have tried changing the  "preg_replace"() part with  "preg_replace_callback" instead, but this gives me a blank white page.

as you can probably tell, i well out of my depth with any php related stuff,
so can anyone tell me what i need to change to correct the error?

smf version 2.0.11
simple portal 2.3.6
Title: Re: cutom html block problem
Post by: emanuele on November 22, 2015, 04:22:26 PM
I'm not the best person to give preg_replace answers, but it may be:
Code: [Select]
preg_replace_callback('~<\!\[CDATA\[(.+?)\]\]>~' . ($context['utf8'] ? 'u' : ''), function($matches) {
    global $smcFunc;

    return '#cdata_escape_encode#' . $smcFunc['htmlspecialchars']($matches[0]);
}, $data);
Title: Re: cutom html block problem
Post by: Chen Zhen on November 22, 2015, 04:38:04 PM
Closures require the use language construct whereas the global will not work with what you posted there.
You need to ensure the global is called prior to executing the closure and then pass that global via the use construct.
Title: Re: cutom html block problem
Post by: pluteus on November 23, 2015, 04:30:52 AM
thanks guys  :)

i have tried the code suggested by emanuele, and it seems to have done the trick, as the error logs are not showing the error anymore,
and the custom block (and the rest of the forum) is working fine  :)

i dont see any errors or problems anywhere, so i'm assuming every thing is good  :)
Title: Re: cutom html block problem
Post by: emanuele on November 23, 2015, 08:16:07 AM
Closures require the use language construct whereas the global will not work with what you posted there.
You need to ensure the global is called prior to executing the closure and then pass that global via the use construct.

As far as I know, closures work exactly like any other function, with the addition that you can use the use construct.

So, both the one I proposed and:
Code: [Select]
preg_replace_callback('~<\!\[CDATA\[(.+?)\]\]>~' . ($context['utf8'] ? 'u' : ''), function($matches) use ($smcFunc) {
    return '#cdata_escape_encode#' . $smcFunc['htmlspecialchars']($matches[0]);
}, $data);
should work the same way. ;)
Title: Re: cutom html block problem
Post by: Chen Zhen on November 23, 2015, 08:04:34 PM
From what is stated on php.net it says any variable needs to be passed via the use construct.
However it does not state anything about globals specifically. If it works while testing then I will assume they do hold there value.
When I applied this fix some time ago I used the latter.

ref. http://simpleportal.net/index.php?issue=471.0
Title: Re: cutom html block problem
Post by: emanuele on November 24, 2015, 08:48:48 AM
Even though it's kind of OT:
From what is stated on php.net it says any variable needs to be passed via the use construct.
However it does not state anything about globals specifically. If it works while testing then I will assume they do hold there value.
Quote from: http://php.net/manual/en/functions.anonymous.php
Inheriting variables from the parent scope is not the same as using global variables. Global variables exist in the global scope, which is the same no matter what function is executing. The parent scope of a closure is the function in which the closure was declared (not necessarily the function it was called from). See the following example:
That means exactly what is written, nothing more, nothing less: "use" is not the same as "global".
The difference is that you can use "use" to "import" (from the function you are "using" the closure from) a variable into a closure. But that variable you "import" can either be a local or global, it doesn't really matter.
On the other hand, a closure it's a function, so you can use "global" to "import" a global variable into the closure scope.
Title: Re: cutom html block problem
Post by: Chen Zhen on November 24, 2015, 11:44:10 PM

You are correct.. I read the rule above the one you just quoted and did not see that one.
Even though I looked on the page for a global reference I missed it in error.
I've been passing globals via the use construct all this time which appears to have been unnecessary.
Thanks for pointing it out.
Title: Re: cutom html block problem
Post by: ccbtimewiz on November 24, 2015, 11:54:46 PM
And at the end of the day you get more experience and everyone learns, including myself.
SimplePortal 2.3.8 © 2008-2024, SimplePortal