public function renderFile() { header("Content-Type: application/json;charset=utf-8"); $aRequest = array(); $sContentType = 'application/x-www-form-urlencoded'; if (isset($_SERVER['CONTENT_TYPE'])) { $sContentType = $_SERVER['CONTENT_TYPE']; } if (isset($_SERVER['HTTP_CONTENT_TYPE'])) { $sContentType = $_SERVER['HTTP_CONTENT_TYPE']; } if (StringUtil::startsWith($sContentType, 'application/json')) { $aRequest = json_decode(file_get_contents('php://input'), true); } else { foreach ($_REQUEST as $sKey => $sValue) { $aRequest[$sKey] = json_decode($sValue, true); } } $oSession = Session::getSession(); if (!isset($aRequest['session_language']) && $oSession->isAuthenticated()) { $aRequest['session_language'] = $oSession->getUser()->getLanguageId(); } $sPrevSessionLanguage = null; $sSetSessionLanguage = null; if (isset($aRequest['session_language'])) { $sPrevSessionLanguage = Session::language(); $sSetSessionLanguage = $aRequest['session_language']; $oSession->setLanguage($sSetSessionLanguage); } try { try { $mJSONData = $this->getJSON($aRequest); $sResult = json_encode($mJSONData); $iError = json_last_error(); if ($iError !== JSON_ERROR_NONE) { throw new LocalizedException('wns.error.json', array('json_error' => $iError, 'json_error_message' => self::$JSON_ERRORS[$iError])); } print $sResult; } catch (Exception $e) { //Handle the gift, not the wrapping… if ($e->getPrevious()) { throw $e->getPrevious(); } else { throw $e; } } } catch (LocalizedException $ex) { LinkUtil::sendHTTPStatusCode(500, 'Server Error'); print json_encode(array('exception' => array('message' => $ex->getLocalizedMessage(), 'code' => $ex->getCode(), 'file' => $ex->getFile(), 'line' => $ex->getLine(), 'trace' => $ex->getTrace(), 'parameters' => $ex->getMessageParameters(), 'exception_type' => $ex->getExceptionType()))); } catch (Exception $ex) { LinkUtil::sendHTTPStatusCode(500, 'Server Error'); ErrorHandler::handleException($ex, true); print json_encode(array('exception' => array('message' => "Exception when trying to execute the last action {$ex->getMessage()}", 'code' => $ex->getCode(), 'file' => $ex->getFile(), 'line' => $ex->getLine(), 'trace' => $ex->getTrace(), 'exception_type' => get_class($ex)))); } //If we changed the session language and no widget willfully changed it as well, reset it to the previous state if ($sPrevSessionLanguage !== null && Session::language() === $sSetSessionLanguage) { $oSession->setLanguage($sPrevSessionLanguage); } }
/** * render() */ public function render() { FilterModule::getFilters()->handleRequestStarted(); $bIsDynamic = false; $bIsAjaxRequest = Manager::isPost() && Manager::isXMLHttpRequest(); $aAjaxSections = array('container' => array(), 'navigation' => array()); ///@todo remove legacy support when the need fades $bIsLegacyAjaxRequest = $bIsAjaxRequest && isset($_REQUEST['container_only']); if ($bIsAjaxRequest) { if ($bIsLegacyAjaxRequest) { $_REQUEST['ajax_containers'] = array($_REQUEST['container_only']); } if (isset($_REQUEST['ajax_containers'])) { sort($_REQUEST['ajax_containers']); $aAjaxSections['container'] = $_REQUEST['ajax_containers']; } if (isset($_REQUEST['ajax_navigations'])) { sort($_REQUEST['ajax_navigations']); $aAjaxSections['navigation'] = $_REQUEST['ajax_navigations']; } if (isset($_REQUEST['ajax_title'])) { $aAjaxSections = array_merge($aAjaxSections, array('page_title' => true, 'link_text' => true, 'title' => true)); } asort($aAjaxSections); } $sPageType = self::$CURRENT_PAGE->getPageType(); $this->oPageType = PageTypeModule::getModuleInstance($sPageType, self::$CURRENT_PAGE, self::$CURRENT_NAVIGATION_ITEM); $aAllowedParams = $this->oPageType->acceptedRequestParams($bIsAjaxRequest ? $aAjaxSections['container'] : null); $bParamsNotAllowed = count(array_intersect($this->aPathRequestParams, $aAllowedParams)) !== count($this->aPathRequestParams); $this->bIsNotFound = $this->bIsNotFound || $bParamsNotAllowed; FilterModule::getFilters()->handlePageNotFoundDetectionComplete($this->bIsNotFound, self::$CURRENT_PAGE, self::$CURRENT_NAVIGATION_ITEM, array(&$this->bIsNotFound)); if ($this->bIsNotFound) { FilterModule::getFilters()->handlePageNotFound(); LinkUtil::sendHTTPStatusCode(404, 'Not Found'); $sErrorPageName = Settings::getSetting('error_pages', 'not_found', null); $oPage = null; if ($sErrorPageName) { $oPage = PageQuery::create()->findOneByName($sErrorPageName); } if ($oPage === null) { die(TranslationPeer::getString('wns.page.not_found')); } self::$CURRENT_PAGE = $oPage; self::$CURRENT_NAVIGATION_ITEM = PageNavigationItem::navigationItemForPage($oPage); //Set correct page type of 404 page $sPageType = self::$CURRENT_PAGE->getPageType(); $this->oPageType = PageTypeModule::getModuleInstance($sPageType, self::$CURRENT_PAGE); } else { $this->addCanonicalLink(); } if (!$bIsAjaxRequest) { $oOutput = $this->getXHTMLOutput(); $oOutput->render(); } else { if (!$bIsLegacyAjaxRequest) { header("Content-Type: application/json;charset=utf-8"); } } $sPageIdentifier = implode('/', self::$CURRENT_NAVIGATION_ITEM->getLink()) . '_' . Session::language(); if ($bIsAjaxRequest) { $sPageIdentifier .= '_' . serialize($aAjaxSections); } // Init the template if ($bIsLegacyAjaxRequest) { $this->oTemplate = new Template(TemplateIdentifier::constructIdentifier('container', $_REQUEST['container_only']), null, true, true); } else { if ($bIsAjaxRequest) { $this->oTemplate = new AjaxTemplate($aAjaxSections, true); } else { $this->oTemplate = self::$CURRENT_PAGE->getTemplate(true); } } FilterModule::getFilters()->handleBeforePageFill(self::$CURRENT_PAGE, $this->oTemplate); if (!$bIsLegacyAjaxRequest) { $this->fillAttributes(); $this->fillNavigation(); $this->fillAutofill(); } $this->fillContent(); $this->renderTemplate(); while (ob_get_level() > 0) { ob_end_flush(); } FilterModule::getFilters()->handleRequestFinished(array(self::$CURRENT_PAGE, $bIsDynamic, $bIsAjaxRequest)); }