Simplify evaluate_cmsimple_scripting()

Discussions and requests related to new CMSimple features, plugins, templates etc. and how to develop.
Please don't ask for support at this forums!
Post Reply
cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Simplify evaluate_cmsimple_scripting()

Post by cmb » Fri May 23, 2014 8:59 pm

Hello Community,

evaluate_cmsimple_scripting() is a real hack, because it has to evaluate arbitrary PHP code inside a function as if that code would be evaluated in the global scope.

At least the following simplification should be possible without any side-effects. Replace:

Code: Select all

    global $output;
    foreach ($GLOBALS as $__name => $__dummy) {
        $$__name = &$GLOBALS[$__name];
    } 
with:

Code: Select all

    extract($GLOBALS, EXTR_REFS);
(This should also be a bit faster.)

We have to double-check that this change wouldn't break anything (at least it passes the current unit-tests), and if so, I suggest to make this change for CMSimple_XH 1.6.3.

However, I would like to get rid of the following code also:

Code: Select all

                $__scope_before = array_keys(get_defined_vars());
                eval($__script);
                $__scope_after = array_keys(get_defined_vars());
                $__diff = array_diff($__scope_after, $__scope_before);
                foreach ($__diff as $__var) {
                    $GLOBALS[$__var] = $$__var;
                } 
(The eval() has to stay, of course.)

This code is there to promote any newly defined variables to the global scope. I assume it is mainly necessary for $keywords and $description. I'm not even sure if it makes sense to allow these variables to be set by CMSimple scripting (meta_tags offers a much nicer solution), but we could easily cater for these variables:

Code: Select all

    global $keywords, $description;
    extract($GLOBALS, EXTR_REFS);
Other variables could be introduced by using $GLOBALS, which is documented since a long time.

We should consider doing this simplification for CMSimple_XH 1.7/2.0. Even if it might break a few sites, the simplification of evaluate_cmsimple_scripting() is worth it.

Christoph
Christoph M. Becker – Plugins for CMSimple_XH

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: Simplify evaluate_cmsimple_scripting()

Post by cmb » Mon Aug 18, 2014 6:52 pm

Part 1 done (r1353).
Christoph M. Becker – Plugins for CMSimple_XH

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: Simplify evaluate_cmsimple_scripting()

Post by cmb » Wed May 13, 2015 1:12 am

I propose to merge r1610 (in branches/eval-scripting) into the trunk. Note that this revision does not allow to set $keywords and $description, nor any other new global variables, but that it's still possible to modify existing global variables (such as $cf, what is sometimes used to make plugins more flexible). Also note, that this uses html_entity_decode() instead of the former simplified fallback (str_replace() for some common HTML entities), which was necessary for PHP 4 compatibility only. Note additionally, that XH_evaluateSinglePluginCall() has been simplified similar to part 1 that has been done for evaluate_cmsimple_scripting() with XH 1.6.3.
Christoph M. Becker – Plugins for CMSimple_XH

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: Simplify evaluate_cmsimple_scripting()

Post by cmb » Mon Jun 01, 2015 4:26 pm

Done (r1631).
Christoph M. Becker – Plugins for CMSimple_XH

cmss
Posts: 244
Joined: Mon Jan 02, 2017 6:15 pm

Re: Simplify evaluate_cmsimple_scripting()

Post by cmss » Fri Jun 16, 2017 7:41 am

In einer größeren Installation von xh169 - curl bekam ich beim Anklicken einiger Links
ständig einen Hänger des Systems mit einer Fehlermeldung
zu Zeile 184 der functions.php: eval($__script);
Ich habe dann den Block mit

$__scope_before = array_keys(get_defined_vars());
eval($__script);
$__scope_after = array_keys(get_defined_vars());
$__diff = array_diff($__scope_after, $__scope_before);
foreach ($__diff as $__var) {
$GLOBALS[$__var] = $$__var;
}
auskommentiert - seitdem geht alles und viel flüssiger.

In welche Stelle müsste ich
global $keywords, $description;
extract($GLOBALS, EXTR_REFS);
einfügen - oder gibt es eine bessere Lösung ? -
es mag auch mit Modulen zusammenhängen, die möchte ich aber belassen

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: Simplify evaluate_cmsimple_scripting()

Post by cmb » Fri Jun 16, 2017 9:33 am

cmss wrote:In einer größeren Installation von xh169 - curl bekam ich beim Anklicken einiger Links
ständig einen Hänger des Systems mit einer Fehlermeldung
zu Zeile 184 der functions.php: eval($__script);
Was war das für eine Fehlermeldung?
cmss wrote:Ich habe dann den Block mit

$__scope_before = array_keys(get_defined_vars());
eval($__script);
$__scope_after = array_keys(get_defined_vars());
$__diff = array_diff($__scope_after, $__scope_before);
foreach ($__diff as $__var) {
$GLOBALS[$__var] = $$__var;
}
auskommentiert - seitdem geht alles und viel flüssiger.
Wenn Du auch das eval() selbst auskommentiert hast, dann ist nicht klar, ob es vorher wegen der potentiellen Zuweisung an neue Variablen, oder wegen des zu evaluierenden Codes so langsam war. Ich vermute mal, wegen letzterem.

Grundsätzlich vermeide ich selbst CMSimple-Skripting, wenn möglich, und verwende statt dessen Pluginaufrufe – finde ich weitaus lesbarer.
cmss wrote:In welche Stelle müsste ich
global $keywords, $description;
extract($GLOBALS, EXTR_REFS);
einfügen - oder gibt es eine bessere Lösung ?
Du kannst versuchen, ob evaluate_cmsimple_scripting() von 1.7 funktioniert. Aber wie gesagt, ich vermute es hat wenig mit der Evaluierung an sich, sondern mit dem zu evaluierenden Code zu tun.
Christoph M. Becker – Plugins for CMSimple_XH

cmss
Posts: 244
Joined: Mon Jan 02, 2017 6:15 pm

Re: Simplify evaluate_cmsimple_scripting()

Post by cmss » Fri Jun 16, 2017 7:55 pm

Der Austausch der ganzen Funktion führte wieder zu

Fatal error: Call to undefined function auto_pagemenu() in /home/www/web704/html/inst/cmsimple/functions.php(174) : eval()'d code on line 1

Ich habe allerdings einige module aktiv. Ausser metatags - expandcontract - hi-updatecheck und gxsecurity und ckeditor sind alle von Ihnen

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: Simplify evaluate_cmsimple_scripting()

Post by cmb » Fri Jun 16, 2017 8:10 pm

cmss wrote:Der Austausch der ganzen Funktion führte wieder zu

Fatal error: Call to undefined function auto_pagemenu() in /home/www/web704/html/inst/cmsimple/functions.php(174) : eval()'d code on line 1
Offensichtlich ist die Funktion auto_pagemenu() (bei der es sich um die in https://cmsimpleforum.com/viewtopic.php ... 283#p55283 vorgestellte handeln könnte) nicht definiert, wird aber irgendwo im Content aufgerufen. Also entweder den Aufruf entfernen, oder die Funktion definieren.
Christoph M. Becker – Plugins for CMSimple_XH

cmss
Posts: 244
Joined: Mon Jan 02, 2017 6:15 pm

Re: Simplify evaluate_cmsimple_scripting()

Post by cmss » Sat Jun 17, 2017 10:19 am

Nachdem ich weder in content.htm noch in functions.php oder userfunctions.php den autopage-Aufruf finden konnte, habe ich einfach die drei Funktionen des obigen Links in die userfunctions eingefügt und es geht jetzt - nur warum der Scripting-fehler ? - aber was solls - das Problem ist erst einmal gelöst - danke

Post Reply