You are here

function services_session_load in Services 6.3

Same name and namespace in other branches
  1. 5 services.module \services_session_load()
  2. 6 services.module \services_session_load()
  3. 6.2 services.module \services_session_load()
  4. 7 services.module \services_session_load()

Backup current session data and import user session.

File

./services.runtime.inc, line 349
Contains functions that only are necessary when a service call is made. This has broken out so that this code isn't loaded for every page load.

Code

function services_session_load($sessid) {
  global $user;

  // If user's session is already loaded, just return current user's data
  if ($user->sid == $sessid) {
    return $user;
  }

  // Make backup of current user and session data
  $backup = $user;
  $backup->session = session_encode();

  // Empty current session data
  $_SESSION = array();

  // Some client/servers, like XMLRPC, do not handle cookies, so imitate it to make sess_read() function try to look for user,
  // instead of just loading anonymous user :).
  $session_name = session_name();
  if (!isset($_COOKIE[$session_name])) {
    $_COOKIE[$session_name] = $sessid;
  }

  // Load session data
  session_id($sessid);
  sess_read($sessid);

  // Check if it really loaded user and, for additional security, if user was logged from the same IP. If not, then revert automatically.
  if ($user->sid != $sessid) {
    services_session_unload($backup);
    return NULL;
  }

  // Prevent saving of this impersonation in case of unexpected failure.
  session_save_session(FALSE);
  return $backup;
}