Link to home
Start Free TrialLog in
Avatar of FourMat
FourMat

asked on

Session Conflict between Joomla 3.x and OSCommerce

Question: Is there a way to disable sessions for Joomla 3 when loading the framework into an external script? Or make Joomla 3 sessions play nice with OSCommerce?

Situation: I am trying to load the a Joomla 3.x module into an OSCommerce installation and I'm am running into a session conflict where Joomla overrides the OSC, disabling OSC usability.

Background: I embedded the Joomla framework and module using the advice and instructions from several different locations and it seems to be working well.

 
  <?php

    define( '_JEXEC', 1 );
        define('JPATH_BASE', '../cms/');
    define( 'DS', DIRECTORY_SEPARATOR );
    require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
    require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
    //$mainframe =& JFactory::getApplication('site');
    $mainframe =& JFactory::getApplication('site',array('session'=>false));
    $mainframe->initialise();

    jimport( 'joomla.application.module.helper' );

    function renderModule ($positionName){
        $document =& JFactory::getDocument();
        $renderer = $document->loadRenderer('module');
        $db =& JFactory::getDBO();
        $db->setQuery("SELECT * FROM #__modules WHERE position='$positionName' AND published=1 ORDER BY ordering");
        $modules = $db->loadObjectList();
        if( count( $modules ) > 0 ){
            foreach( $modules as $module ){
                //just to get rid of that stupid php warning
                $module->user = '';
                $params = array('style'=>'xhtml');
                echo $renderer->render($module, $params);
        }  
       }
    }


echo renderModule('position-33');   
    ?>

Open in new window



The problem I now have is that the Joomla session is overriding the OSCommerce session.

I've added code to output the session information and this is what I get:

Before adding Joomla framework code:

Array
(
    [cart] => shoppingCart Object
        (
            [contents] => Array
                (
                )

            [total] => 0
            [weight] => 0
            [content_type] => 
            [shiptotal] => 0
        )

    [language] => english
    [languages_id] => 1
    [currency] => USD
    [navigation] => navigationHistory Object
        (
            [path] => Array
                (
                    [0] => Array
                        (
                            [page] => index.php
                            [mode] => NONSSL
                            [get] => Array
                                (
                                )

                            [post] => Array
                                (
                                )

                        )

                )

            [snapshot] => Array
                (
                )

        )

    [affiliate_ref] => 
    [affiliate_clickthroughs_id] => 
)

Open in new window


After adding Joomla framework code, no OSC session variables exist:

Array
(
    [joomla] => TzoyNDoiSm9vbWxhXFJlZ2lzdHJ5XFJlZ2lzdHJ5IjoyOntzOjc6IgAqAGRhdGEiO086ODoic3RkQ2xhc3MiOjE6e3M6OToiX19kZWZhdWx0IjtPOjg6InN0ZENsYXNzIjozOntzOjc6InNlc3Npb24iO086ODoic3RkQ2xhc3MiOjI6e3M6NzoiY291bnRlciI7aTo0MjtzOjU6InRpbWVyIjtPOjg6InN0ZENsYXNzIjozOntzOjU6InN0YXJ0IjtpOjE0NTM3Mjk0MjA7czo0OiJsYXN0IjtpOjE0NTM3NDY4NTU7czozOiJub3ciO2k6MTQ1Mzc0Njg1Nzt9fXM6ODoicmVnaXN0cnkiO086MjQ6Ikpvb21sYVxSZWdpc3RyeVxSZWdpc3RyeSI6Mjp7czo3OiIAKgBkYXRhIjtPOjg6InN0ZENsYXNzIjowOnt9czo5OiJzZXBhcmF0b3IiO3M6MToiLiI7fXM6NDoidXNlciI7Tzo1OiJKVXNlciI6MjY6e3M6OToiACoAaXNSb290IjtiOjA7czoyOiJpZCI7aTowO3M6NDoibmFtZSI7TjtzOjg6InVzZXJuYW1lIjtOO3M6NToiZW1haWwiO047czo4OiJwYXNzd29yZCI7TjtzOjE0OiJwYXNzd29yZF9jbGVhciI7czowOiIiO3M6NToiYmxvY2siO047czo5OiJzZW5kRW1haWwiO2k6MDtzOjEyOiJyZWdpc3RlckRhdGUiO047czoxMzoibGFzdHZpc2l0RGF0ZSI7TjtzOjEwOiJhY3RpdmF0aW9uIjtOO3M6NjoicGFyYW1zIjtOO3M6NjoiZ3JvdXBzIjthOjE6e2k6MDtzOjE6IjkiO31zOjU6Imd1ZXN0IjtpOjE7czoxMzoibGFzdFJlc2V0VGltZSI7TjtzOjEwOiJyZXNldENvdW50IjtOO3M6MTI6InJlcXVpcmVSZXNldCI7TjtzOjEwOiIAKgBfcGFyYW1zIjtPOjI0OiJKb29tbGFcUmVnaXN0cnlcUmVnaXN0cnkiOjI6e3M6NzoiACoAZGF0YSI7Tzo4OiJzdGRDbGFzcyI6MDp7fXM6OToic2VwYXJhdG9yIjtzOjE6Ii4iO31zOjE0OiIAKgBfYXV0aEdyb3VwcyI7YToyOntpOjA7aToxO2k6MTtpOjk7fXM6MTQ6IgAqAF9hdXRoTGV2ZWxzIjthOjM6e2k6MDtpOjE7aToxO2k6MTtpOjI7aTo1O31zOjE1OiIAKgBfYXV0aEFjdGlvbnMiO047czoxMjoiACoAX2Vycm9yTXNnIjtOO3M6MTM6IgAqAHVzZXJIZWxwZXIiO086MTg6IkpVc2VyV3JhcHBlckhlbHBlciI6MDp7fXM6MTA6IgAqAF9lcnJvcnMiO2E6MDp7fXM6MzoiYWlkIjtpOjA7fX19czo5OiJzZXBhcmF0b3IiO3M6MToiLiI7fQ==
)

Open in new window


I have tried inserting the joomla framework before and after the session call for OSCommerce, but it overrides it. I have also tried to disable a Joomla session when the framework is loaded into OSC. Apparently there is something fundamental I'm not understanding about sessions.

Is there a way to make these two separate types of session systems play nice together? If there is no way to make the systems play together, is there a way to disable sessions for Joomla 3 when loading the framework into an external script?
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

When you described the session data before and after adding Joomla, what variable are you dumping?

Joomla has its own session variable, which is a JRegistry object.  AFAIK, it only uses the $_SESSION superglobal to create the necessary tie between the user's session cookie and that object as stored in the database.  

I don't know how OSCommerce works, but one or the other should be able to be "tolerant" of something else using $_SESSION, if that is what you are referencing.
Avatar of FourMat
FourMat

ASKER

Hi Steve, Thanks for the response.  Yes I dumped the $_SESSION variable using this code:

echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';

Open in new window


OSC stores everything in the session array variables that you see above.  I'm really unfamiliar with the Joomla side of things.  

I was hoping that there might be a way to make the two systems use the same Array in the superglobal,  with it just tacking on the [joomla] variable onto the already existing OSC generated session array.  It seems that the Joomla framework just wipes out whatever is in the current session.    Any insights into what to do about it?
I'll have to look at that specific framework code to refresh my memory.  Joomla generally maintains a non-destructive approach to the environment, so the idea that it consumes any existing $_SESSION data points seems out of norm.

What happens if you include Joomla Framework, then initialize OSCommerce?
Avatar of FourMat

ASKER

I have the joomla framework calls at the very top of the page and are the first thing to be loaded in.  I have tried loading it in several different positions hoping that the OSC Session data would be added, but the Joomla framework takes it over no matter where it is.
I checkout some of the session handling, and I don't think you'll be able to work around this easily.

Joomla registers session functions using session_set_save_handler, with the actual handlers being determined by a setting in your admin panel.  Unless you set it to "none", Joomla is going to handle all calls to session-related work, such as saving or reading variables.  

Even more to the point, Joomla does actually destroy the current session if one exists when the application is bootstrapped.  This makes it absolutely necessary to bootstrap the framework before OSCommerce.

Once Joomla is bootstrapped, session related functions *should* work transparently with OSCommerce, assuming it does not also try to attach session handlers.  For it to not be working is a bit of a curiosity and implies it is trying to register and use its own session handling facilities.

So, you can try setting Joomla to use "none" as the session handler, and see if that gets you anywhere.  Functionally, this will revert all handling functions back to the PHP default behavior, which should let OSCommerce do whatever it wants.  It remains to be seen if Joomla will actually function in whatever environment OSCommerce creates.
Avatar of FourMat

ASKER

HI Steve, thanks for looking into all of that for me.  I have tried a few different scenarios, and variations on the session handers, and it doesn't seem to make a difference.  The OSC session is destroyed no matter what I do.  If I bootstrap the Joomla framework before, the OSC session code doesn't tack on it's variables to that particular session.  I don't quite fully understand why, but I believe it would require a rewrite of the OSC session system which is beyond my talent level at this point.

Thinking outside the box a little, I think I'm going to try another approach and sandbox the output of the Joomla calls.  I think I can create a file that takes the Joomla framework and outputs the module HTML to a text file that can be included into the OSC template.  I have scoped out various ways to do this.  Right now it looks like I can run the Joomla framework script via an exec() which can be dangerous, or via a sanitation process by using something similar to this:

<?php
   // test content  
   $code = <<<PHP
   echo "test"; //insert joomla framework calls here
PHP;

   // create temporary file
   $d=rand();
   $myfile="$d.php";
   file_put_contents($myfile,"<?php\n$code\n?>");

   // start capture output
   ob_start();

   // include generate file
   // NOTE: user-provided code is unsafe, they could e.g. replace this file.
   include($myfile);

   // get capture output
   $result = ob_get_clean();

   // remove temporary file
   unlink($myfile);

   // output result
   echo "================\n" . $result . "\n================\n" ;

Open in new window


If it comes to it, I can just build a totally separate system that is executed via Cron jobs periodically because the menu structures and modules I want to use aren't something that's going to need to be changed on each page call or refresh.
ASKER CERTIFIED SOLUTION
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of FourMat

ASKER

I think I found a workable solution for my needs.  One of the other issues I ran into with the Joomla modules was the occurrence relative paths.  Integrating menus and such into OSC would have required required hard links in the joomla CMS.  So even if I could rewrite the session classes to work together, I would still have the issue of the paths not matching up.  

So instead, I just created a script to isolate the two system.  I put the joomla_framework script in the Joomla folder (thereby solving the relative path problem reference from the native Joomla folder) and used a cURL call in OSC to generate static cache files that contain the output of the modules I need.  Then in the OSC template, I just "include" them into the appropriate place.  The cache can be set to regenerate on a timed interval, and will update properly when I make a change inside of Joomla.  This fixed both the relative path issues and the session conflicts I was having.  It may be a bit of a kludge, but so far it's working for me.  

Thanks for the advice and pushing me in the right direction.
Avatar of FourMat

ASKER

Thanks for the comprehensive look into my problem.  It was much appreciated.  Even though this wasn't the perfect solution to the ultimate problem, it gave me enough information to make some decisions toward going another direction, so I'm going to accept this as the best answer.