/**
  * @return AppBarControl
  */
 public static function instance()
 {
     if( is_null(self::$_instance) )
     {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
    private function iframeBuildResult()
    {
        $url = $_COOKIE[$this->cookieKey()];

        CookieTool::destroyCookie($this->cookieKey(), '/', null);

        if($url == 1)
        {
            if ( $this->applicationObject()->externalLinkHandler() )
            {
                $url = $this->applicationObject()->externalLinkHandler()->getNodeUrl( $this->node );
                $url = $this->getDeeplink( $url );
            }
            else
            {
                $url = '/bad_config';
            }
        }
        if ($url == null && isset($_REQUEST['r']))
        {
            $url = urldecode($_REQUEST['r']);
        }

        $backUrl    = isset($_REQUEST['bu']) ? $_REQUEST['bu'] : eZINI::instance()->variable('SiteAccessSettings', 'BaseUrl');

        if ( $this->isDeeplinkApplication() )
        {
            $redirect = !$this->displayIframe();
        }
        else
        {
            $redirect = ( $this->mustRedirect() || ( isset($_REQUEST['r']) && $_REQUEST['r'] ) );
        }

        if ( !$this->isOnExitPageView() && !$this->displayIframe() )
        {
            $redirect = true;
        }

        // Trick used for pages that needed POST form submit, that coudn't be done by CURL.
        $usePostMethod = SolrSafeOperatorHelper::getCustomParameter( $this->applicationObject->identifier, 'usePostMethod', 'application' );
        if( $redirect && !$usePostMethod && (!$this->isOnExitPageView() || !$this->displayIframe()))
        {
            header('Location: '.$url);
            eZExecution::cleanExit();
        }

        $applicationList = AppBarControl::instance()->applicationIds();
        sort($applicationList);

        $displayIframe = true;
        if ( !$this->isOnExitPageView() )
        {
            $displayIframe = $this->displayIframe();
        }
        $url = in_array('arg', array_keys($this->_params['UserParameters'])) ? $url . $this->_params['UserParameters']['arg'] : $url;
        $this->pushResult('iframe'          , $displayIframe);
        $this->pushResult('url'             , $url);
        $this->pushResult('back_url'        , $backUrl);
        $this->pushResult('app_list_hash'   , md5( $this->user() ? json_encode($applicationList) : 'anonymous' ));
        $this->pushResult('add_to_my_apps'  , ( $this->user() && $this->applicationObject->getParent() != null && !in_array( $this->getApplicationId(), AppBarControl::instance()->applicationIds() ) && $this->getApplicationId() != 0));
        $this->pushResult('app'             , $this->applicationObject());
        $oldSkipPage = SolrSafeOperatorHelper::getCustomParameter($this->applicationObject->identifier, 'SkipExitPage', 'application');
        $skipPage = $oldSkipPage || !$this->exitStrategy();
        // hack to redirect external apps which doesn't have entry exits
        if( $this->isOnExitPageView() && $skipPage )
        {
            $this->pushResult('skip_exit_page_redirect', true);
        }
    }
    /**
     * @return array
     */
    public static function applicationList()
    {
        # Hotfix #23450 note-8
        if ( !isset($_COOKIE['remember_me']) )
        {
            $cookieExpiration = time() + eZINI::instance('merck.ini')->variable('TIBCOCookieSettings', 'TIBCOCookieExpiration');
            $uuid = MMUsers::getCurrentUserId();
            setcookie('remember_me', MMUserLogin::encryptText(json_encode($uuid)), $cookieExpiration, '/', CookieTool::getCookieDomain());
        }

        $out = array('applicationList' => array());
        $host = 'http://' . self::host();

        if ( isset($_POST['application']) )
        {
            $applicationIdList = array((int) $_POST['application']);
        }
        else
        {
            $applicationIdList = AppBarControl::instance()->applicationIds();
        }

        foreach ( $applicationIdList as $applicationId )
        {
            if ( !is_numeric($applicationId) )
            {
                continue;
            }
            $applicationLocalized = CacheApplicationTool::buildLocalizedApplicationByApplication($applicationId);
            $application = ($applicationLocalized) ? $applicationLocalized->applicationObject() : null;

            if ( $applicationLocalized )
            {
                $application = $applicationLocalized->applicationObject();
            }

            if ( isset($application) )
            {
                $applicationType = $application->applicationType();

                $icons = array(
                    'icon' => array(
                        'path' => '',
                        'md5' => '',
                        'file' => 'ico.png',
                    ),
                    'icon_hd' => array(
                        'path' => '',
                        'md5' => '',
                        'file' => 'ico_notext_hd.png',
                    ),
                    'icon_active' => array(
                        'path' => '',
                        'md5' => '',
                        'file' => 'ico_notext_active.png',
                    ),
                    'icon_active_hd' => array(
                        'path' => '',
                        'md5' => '',
                        'file' => 'ico_notext_active_hd.png'
                    ),
                );

                foreach($icons as $key => $iconData)
                {
                    $path = sprintf('apps/%s/%s', $application->attribute('identifier'), $iconData['file']);
                    $fullPath = StaticData::clusterFilePath(ClusterTool::clusterIdentifier(), $path);
                    if ($fullPath)
                    {
                        $icons[$key]['path'] = StaticData::externalUrl(ClusterTool::clusterIdentifier(), $path);
                        $icons[$key]['md5'] = md5_file($fullPath);
                    }
                }

                $path = '/' . $applicationLocalized->attribute('url');
                $url = $host . $path;
                if ( $application->isPureExternal() )
                {
                    $url = $applicationLocalized->attribute("external_url");
                }

                $outApplicationListArray = array(
                    'id'         => $applicationId,
                    'identifier' => $application->attribute('identifier'),
                    'name'       => $applicationLocalized->attribute('name'),
                    'headline'   => $applicationLocalized->attribute('headline'),
                    'type'       => $applicationType->attribute('internal_type'),
                    'url'        => $url,
                    'path'       => $path,
                    'javascript' => self::getJavascript($application->attribute('identifier'), 'application'));

                foreach($icons as $key => $icon)
                {
                    $outApplicationListArray[$key] = array(
                        '100' => array('path' => $icon['path'], $icon['md5']),
                    );
                }
                $out['applicationList'][] = $outApplicationListArray;
            }
        }
        return $out;
    }
    /**
     * @return bool
     */
    protected function addToApplicationList()
    {
        if ( isset($_REQUEST['id']) && is_numeric($_REQUEST['id']) )
        {
            $user = MMUsers::getCurrentUserObject();
            if ( !$user )
            {
                return false;
            }

            $applicationList = $user->getApplicationList();

            if ( !is_array($applicationList) )
            {
                $applicationList = array();
            }

            $applicationList[] = $_REQUEST['id'];
            AppBarControl::instance()->store($applicationList);

            return true;
        }

        return false;
    }
    /**
     * Logs in the user if applied username and password is
     * valid. The userID is returned if successful, false if not.
     *
     * @param array $params
     * @return bool
     */
    public static function loginUser($params = null)
    {
        $uncryptedTicket = array();

        if($params)
        {
            $uncryptedTicket["uuid"]           = $params["Username"];
            $uncryptedTicket["customerType"]   = $params["Customer_type"];
            $uncryptedTicket["userSpeciality"] = $params["User_specialty"];
            $uncryptedTicket["state"]          = $params["Province"];
            $uncryptedTicket["country"]        = $params["Country_of_registration"];
            $uncryptedTicket['toUValidated']   = isset( $params['toUValidated'] ) ? $params['toUValidated'] : true;
            $uncryptedTicket['autologin']      = isset( $params['autologin'] ) ? $params['autologin'] : false;
        }
        else
        {
            $http = eZHTTPTool::instance();

            $hashedTicket = $http->getVariable( 't' );
            if ( empty( $hashedTicket ) )
            {
                return false;
            }

            $uncryptedTicket = self::uncryptTicket ( $hashedTicket );
        }
        
        if ( !self::validateTicket($uncryptedTicket) )
        {
            return false;
        }

        $mmUser = self::createOrUpdateMMUser($uncryptedTicket);
        if( isset($uncryptedTicket['toUValidated']) )
        {
            $mmUser->toUValidated( $uncryptedTicket['toUValidated'] );
        }
        if( isset($uncryptedTicket['autologin']) )
        {
            $mmUser->isAutologin( $uncryptedTicket['autologin'] );
        }
        MMUsers::setCurrentUserObject($mmUser);

        AppBarControl::instance()->store();
        MMSelections::setCookie();
        MMUsers::setCookieUserPreferences();

        $catchUpSpe = $mmUser->getPreference( 'catchUpSpe' );
        if ( !empty( $catchUpSpe ) )
        {
            setcookie ('catchUpSpe', '1', CookieTool::getDefaultCookieExpiration(), '/', CookieTool::getCookieDomain() );
        }
        return true;
    }