<?php

abstract class oxERPBase
{

    /**
     * oxERPBase::loadSessionData()
     * load session - should be called on init
     *
     * @param string $sSessionID session id
     * @param bool $blAdminMode admin mode ( default is on, due to #4617 once it must be off )
     * @return null
     */
    public function loadSessionData($sSessionID, $blAdminMode = true)
    {
        if (!$sSessionID) {
            throw new Exception("ERROR: Session ID not valid!");
        }

        $_COOKIE = array('admin_sid' => $sSessionID);

        // change session if needed
        if ($sSessionID != session_id()) {
            if (session_id()) {
                session_write_close();
            }
            session_id($sSessionID);
            session_start();
        }

        $myConfig = oxRegistry::getConfig();
        $myConfig->setConfigParam('blAdmin', 1);
        $myConfig->setAdminMode($blAdminMode);

        $iShopId = null;
        $iLang = null;
        if (oxNew('OXERPCompatibility')->isShopEE()) {
            $oUtils = oxRegistry::getUtils();
            $aSessionCache = $oUtils->fromFileCache("erp_c_fieldnames_" . $sSessionID);

            if (is_array($aSessionCache)) {
                $iLang = isset($aSessionCache['lang']) ? $aSessionCache['lang'] : null;
                $iShopId = isset($aSessionCache['actshop']) ? $aSessionCache['actshop'] : null;
            }
        }

        $mySession = oxRegistry::getSession();

        if ($iLang === null) {
            $iLang = $mySession->getVariable("lang");
        }

        if ($iShopId === null) {
            if (oxNew('OXERPCompatibility')->isShopEE()) {
                $iShopId = $mySession->getVariable("actshop");
                $oUtils = oxRegistry::getUtils();
                $oUtils->toFileCache(
                    "erp_c_fieldnames_" . $sSessionID,
                    array(
                        "actshop" => $iShopId,
                        "lang" => $iLang
                    )
                );
                $oUtils->commitFileCache();
            }
        }

        $myConfig->setShopId($iShopId);

        $sAuth = $mySession->getVariable('auth');
        if (!isset($sAuth) || !$sAuth) {
            throw new Exception("ERROR: Session ID not valid!");
        }

        $this->_iLanguage = $iLang;
        $this->_sUserID = $sAuth;

        $this->_blInit = true;
    }

}
