?
avatar OctavianC
OctavianC
8 Jan 2016

Problem

According to libraries/joomla/session.php, the function set() should return the old value of the variable:

/**
     * Set data into the session store.
     *
     * @param   string  $name       Name of a variable.
     * @param   mixed   $value      Value of a variable.
     * @param   string  $namespace  Namespace to use, default to 'default'.
     *
     * @return  mixed  Old value of a variable.
     *
     * @since   11.1
     */
    public function set($name, $value = null, $namespace = 'default')

This is no longer working and I'm guessing since the hardening patch was introduced.

Test instructions

Tested with Joomla! 3.4.8, haven't had a chance to test the staging branch just yet.
Run the following code:

$session = JFactory::getSession();
$session->set('test', 1);
echo $session->set('test', 2);

Expected result:

1

Actual result:

2

As you can see, it no longer returns the old value, it will return the current (set) value instead.

avatar OctavianC OctavianC - open - 8 Jan 2016
avatar bertmert
bertmert - comment - 8 Jan 2016

Confirmed.
Correct me if I'm wrong (and don't kill me verbally ;-) )
I think reason is that $session is represented by a Registry object now ($this->data in class JSession).
Thus line https://github.com/joomla/joomla-cms/blob/staging/libraries/joomla/session/session.php#L528 in JSession::set() returns the new value (expected behavior for Registry objects)

return $this->data->set($namespace . '.' . $name, $value);

I don't know if a fix like this is allowed and makes sense

 public function set($name, $value = null, $namespace = 'default')
 {
  // Add prefix to namespace to avoid collisions
  $namespace = '__' . $namespace;

  if ($this->_state !== 'active')
  {
   // @TODO :: generated error here
   return null;
  }

  $old = $this->data->get($namespace . '.' . $name, null);

  $this->data->set($namespace . '.' . $name, $value);

  return $old;
 }
avatar OctavianC
OctavianC - comment - 11 Jan 2016

I consider it a bug since it breaks backwards compatibility.

avatar bertmert
bertmert - comment - 11 Jan 2016

I've posted my fix not knowing if it is acceptable.
Test it if you want to and provide a PullRequest if it works without crashing something.
Then it's on a higher discussion level or so and people can test it in detail.
Otherwise you have to wait until someone lets us know if it's a BC issue and should be fixed or not.

(Session is a delicate subject at the moment and I'm too shy to provide a PR.)

avatar brianteeman brianteeman - change - 13 Jan 2016
Labels Added: ?
avatar brianteeman brianteeman - change - 1 Mar 2016
Category Documentation Libraries
avatar brianteeman
brianteeman - comment - 2 Mar 2016

Closed as we have a PR for testing please see #9276

avatar brianteeman brianteeman - change - 2 Mar 2016
Status New Closed
Closed_Date 0000-00-00 00:00:00 2016-03-02 08:22:28
Closed_By brianteeman
avatar brianteeman brianteeman - close - 2 Mar 2016

Add a Comment

Login with GitHub to post a comment