Page 1 of 1

[Solved] Global Variable $gCms replacement

Posted: Wed Oct 06, 2010 12:48 am
by jaslorax
As this will be gone in the next iteration of CMSM 1.9 - see quote below - is there a generic replacement or is the global nature the whole argument? I'm not sure how to prep my site for this and switch over the variables that rely on it . . .

"15) Removed the $gCms variable from smarty.
This has been a contentious issue, but it was decided that for security reasons this variable should not be exported to smarty.  This may break some sites that are using some of the internal CMSMS variables to do some things directly in smarty." from: http://forum.cmsmadesimple.org/index.ph ... 209.0.html

Re: Global Variable $gCms replacement

Posted: Wed Oct 06, 2010 9:47 am
by Peciura
You will not be able to access members of $gCms directly. But you can do everything in UDT.
$gCms = cmsms();
//or even better (but not the best)
$smarty = cmsms()->GetSmarty();
There will be even easier ways to access smarty and some commonly used system and custom variables.

Similar discussion is here http://forum.cmsmadesimple.org/index.ph ... 403.0.html

Re: Global Variable $gCms replacement

Posted: Wed Oct 06, 2010 4:33 pm
by jaslorax
Sorry if I am a little daft about this but just to clarify everywhere I have used (in my UDTs):
global $gCms;
$smarty = &$gCms->GetSmarty();
it simply needs to change to:
$gCms = cmsms(); //global $gCms;
$smarty = &$gCms->GetSmarty();
All this does is allow the UDT to be a global variable right? I don't think I fully understand the $gCms and what it does . . . haven't located a great explanation.

But this is something I can do now and it will work and if I didn't my understanding is that though it will be deprecated it won't be removed right?

Here is a simply example of my usage for grabbing the query string in a uri:

udt name: current_qs
global $gCms;
$smarty = &$gCms->GetSmarty();

if($_SERVER["QUERY_STRING"]) {
$the_qs = '?'.$_SERVER["QUERY_STRING"];
}
else {
$the_qs ='';
}
$smarty->assign('the_qs', $the_qs);
so I'd change to:
$gCms = cmsms(); //global $gCms;
$smarty = &$gCms->GetSmarty();

if($_SERVER["QUERY_STRING"]) {
$the_qs = '?'.$_SERVER["QUERY_STRING"];
}
else {
$the_qs ='';
}
$smarty->assign('the_qs', $the_qs);
What was the issue with the old method?

Re: Global Variable $gCms replacement

Posted: Wed Oct 06, 2010 5:04 pm
by Peciura
A the moment that is the best you can do.
But this is something I can do now and it will work and if I didn't my understanding is that though it will be deprecated it won't be removed right?
It might be removed some time in a future without warning.

"public global" $gCms was just another way to reduce stability of CMSms. It can be used the way it was not meant to thus affecting all system.

Re: Global Variable $gCms replacement

Posted: Wed Oct 06, 2010 5:54 pm
by NaN
By the way, this one:
jaslorax wrote:
"15) Removed the $gCms variable from smarty.
has nothing to do with what you're talking about.
It is removed from smarty means it is removed from smarty. Not from the code at all.
That means you cannot use it in templates anymore.
You won't be able to use

Code: Select all


{$gCms-> ...}

in a template since the var is just not available in a template anymore.
Or this one in a UDT:

Code: Select all


$smarty->_tpl_vars['gCms'];

won't work anymore.
But this is nothing really critical because if anyone really did use the template var gCms it will be his own fault. This code will have to fail someday.

Just consider the following: Smarty contains a reference of the gCms object and the gCms object contains a reference to smarty. Both were public. So you could do this: $smarty->_tpl_vars['gCms']->smarty->_tpl_vars['gCms']->smarty-> ... ::)

So the code line

Code: Select all


$smarty->assign_by_ref('gCms', $gCms);

has to be removed from the include.php since it is no good design to have a public reference inside an object that contians a public reference to the other object ... (just try to use the smarty plugin {debug}; whenever it comes to the $gCms object it will crash because of endless recursion)

Thats all.

The "public global $gCms" stuff is something completely different. Even if it has also to with design. As you can see it is neccessary to make certain methods, vars or members of an object private to make sure nothing weird happens.
And this will be all that is gonna happen.
If there really is need to access private data of the CMSms internals there will be a public method to get these.
Additionally you won't be able to SET those data anymore.
Again, only if there is really any need to do so, there will be a public method that allows you to set something.

You won't have any trouble with your code if you switch from

Code: Select all


global $gCms

to

Code: Select all


$gCms = cmsms();

in all your code now since this function is already available in CMSms 1.8.2.
What exactly is going on inside the cmsms() function or what $gCms actually is, is completely irrelevant to you. Main thing is that it returns something that provides (hopefully well documented) public functions to you to access certain core functions. Thats all you need to know about the $gCms stuff.