Beispiel #1
0
    /**
     * Root Modules Render Method (called by Backend Visitor)
     * @param \Render\APIs\RootAPIv1\RootRenderAPI $rootApi
     * @param \Render\Unit $rootUnit
     * @param \Render\ModuleInfo $moduleInfo
     */
    public function render($rootApi, $rootUnit, $moduleInfo)
    {
        // lang/locale
        $pageLang = $rootApi->getFormValue($rootUnit, 'lang', 'en-US');
        $pageLangLowDash = str_replace('-', '_', $pageLang);
        setlocale(LC_ALL, $pageLangLowDash . '.UTF8', $pageLang);
        $rootApi->setLocale($pageLangLowDash);
        // read custom data provided by module implementations (per module and per unit)
        $moduleData = $rootApi->getAllModuleData($rootUnit);
        $unitData = $rootApi->getAllUnitData($rootUnit);
        $nav = $rootApi->getNavigation();
        // http header redirect (only in live and preview mode)
        if ($rootApi->isLiveMode() || $rootApi->isPreviewMode()) {
            foreach ($unitData as $d) {
                $redirect = isset($d['redirect']) ? $d['redirect'] : null;
                if (is_array($redirect) && array_key_exists('url', $redirect)) {
                    header('Location: ' . $redirect['url'], true);
                    exit;
                }
            }
            $currentPage = $nav->getPage($nav->getCurrentPageId());
            $pageTitle = $currentPage->getTitle();
            $pageAttributes = $currentPage->getPageAttributes();
            $redirectFirstChild = false;
            if (array_key_exists('enableRedirect', $pageAttributes)) {
                if ($pageAttributes['enableRedirect'] == 1) {
                    if ($pageAttributes['redirectType'] == "firstChild") {
                        $redirectFirstChild = true;
                    } else {
                        if ($pageAttributes['redirectType'] == "page") {
                            if (array_key_exists('redirectPageId', $pageAttributes)) {
                                $redirectPageId = $pageAttributes['redirectPageId'];
                                if ($redirectPageId != '' && $redirectPageId != $nav->getCurrentPageId()) {
                                    $redirectUrl = $nav->getPage($redirectPageId)->getUrl();
                                    if ($redirectUrl != '') {
                                        header('Location: ' . $redirectUrl);
                                        exit;
                                    }
                                }
                            }
                        } else {
                            if (array_key_exists('redirectUrl', $pageAttributes)) {
                                $redirectUrl = $pageAttributes['redirectUrl'];
                                if (preg_match("/^[http]/", $redirectUrl)) {
                                    if ($rootApi->isLiveMode()) {
                                        header('Location: ' . $redirectUrl);
                                        exit;
                                    } else {
                                        $i18n = new Translator($rootApi, $moduleInfo);
                                        $msg1 = $i18n->translate('error.redirectLiveOnly1');
                                        $msg2 = $i18n->translate('error.redirectLiveOnly2');
                                        echo '<html><body style="margin:0;background-color: #454444;font-family:\'Trebuchet MS\',sans-serif;font-size:20px;color: #ffffff;">';
                                        echo '<div style="display:flex;height:100vh;width:80%;padding:0 10%;justify-content:center;align-items:center;text-align:center;">' . $msg1 . $redirectUrl . $msg2 . '</div>';
                                        echo '</body></html>';
                                        exit;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            // redirect to first child page if page title equal [redirect]
            if (strtolower($pageTitle) == '[weiterleiten]' || strtolower($pageTitle) == '[redirect]' || $redirectFirstChild) {
                $childrenIds = $nav->getChildrenIds($nav->getCurrentPageId());
                if (count($childrenIds)) {
                    $currentPage = $nav->getPage($nav->getCurrentPageId());
                    $curUrl = $currentPage->getUrl();
                    $fstChild = $nav->getPage($childrenIds[0]);
                    $url = $fstChild->getUrl();
                    if (!empty($url) && $url != $curUrl) {
                        header('Location: ' . $url);
                        exit;
                    }
                }
            }
        }
        // protected pages
        $password_protection = $rootApi->getWebsiteSettings('password_protection');
        if ($password_protection['enableProtectedPage'] && $rootApi->isPage()) {
            if ($rootApi->isLiveMode() || $password_protection['inPreviewMode'] && $rootApi->isPreviewMode()) {
                $currentPageId = $nav->getCurrentPageId();
                $protectedPage = $password_protection['protectedPage'];
                $parentPages = $nav->getNavigatorIds($currentPageId);
                if (in_array($protectedPage, $parentPages) || $protectedPage == '') {
                    $validLogin = $this->authenticate($password_protection['loginPasswords']);
                    if (!$validLogin) {
                        exit;
                    }
                }
            }
        }
        // error reporting
        if ($rootApi->isEditMode() && $rootApi->getFormValue($rootUnit, 'debugShowPhpErrors')) {
            error_reporting(E_ALL);
            ini_set('display_errors', true);
        }
        // session TODO: remove?
        if (!$rootApi->isEditMode()) {
            if ($rootApi->getFormValue($rootUnit, 'enablePHPSession') && !isset($_SESSION)) {
                session_start();
            }
        }
        // start output
        echo '<!DOCTYPE html>';
        echo "<html lang='{$pageLang}'>";
        echo '<head>';
        // static meta tags
        echo '<meta charset="utf-8">';
        echo '<meta http-equiv="X-UA-Compatible" content="IE=edge">';
        echo '<meta name="viewport" content="initial-scale=1">';
        // favicon and appleTouchIcon
        $this->insertFavicon($rootApi, $rootUnit);
        // title tag and meta description
        $this->insertTitleAndDescription($rootApi, $rootUnit);
        // JS API
        echo $rootApi->isEditMode() ? '<script src="' . $rootApi->getJsApiUrl() . '"></script>' : '';
        // srcset polyfill (load before modernizer because of the htmlshiv, load before CSS according to doc)
        // http://caniuse.com/#feat=srcset (Safari, Android 4.x, Firefox)
        echo <<<EOF
    <script>
    if(!window.HTMLPictureElement){
      document.createElement('picture');
      function loadJS(u){var r = document.getElementsByTagName( "script" )[ 0 ], s = document.createElement( "script" );s.src = u;r.parentNode.insertBefore( s, r );}
      loadJS('{$moduleInfo->getAssetUrl('js/respimage.js')}');
    }
    </script>
EOF;
        // CSS Assets (static css files via <link>)
        $this->insertCssAssets($moduleData);
        // Dynamic CSS (only in edit mode - see css for live mode)
        if ($rootApi->isEditMode()) {
            $dynCss = $this->getDynCSSLib($moduleInfo);
            $dynCss->generateCSS($rootApi, $rootUnit, true);
            // Selectors Data (TODO: this could be also accessed by CMS.getCustomData(moduleId).selectors ?)
            echo '<script type="text/x-text-json" id="available_selectors">';
            echo $this->insertSelectorsJson($moduleData);
            echo '</script>';
        } else {
            echo '<link rel="stylesheet" type="text/css" href="' . $rootApi->getCssUrl() . '">';
        }
        //
        // JS
        //
        // responsive images
        if (!$rootApi->isEditMode()) {
            echo "<script>window.lazySizesConfig = { preloadAfterLoad: !(/mobi/i.test(navigator.userAgent)) };</script>";
        }
        echo "<script src='{$moduleInfo->getAssetUrl('js/lazysizes.js')}' async=''></script>";
        echo "<script src='{$moduleInfo->getAssetUrl('js/ls.progressive.js')}' async=''></script>";
        // modernizer for feature detection (and htmlshiv)
        echo "<script src='{$moduleInfo->getAssetUrl('js/modernizer.js')}'></script>";
        // insert jquery here, so it's already available in body
        echo "<script src='{$moduleInfo->getAssetUrl('js/jquery-2.1.4.js')}'></script>";
        // HTML/CSS polyfills
        if (!$rootApi->isEditMode()) {
            // vw/vh css units (http://caniuse.com/#feat=viewport-units)
            // Polyfill "prefixfree.viewport-units": modified to support browser resize
            echo "<script>Modernizr.load({test: Modernizr.cssvwunit && Modernizr.cssvhunit, nope: ['{$moduleInfo->getAssetUrl('js/prefixfree.stylefix.js')}', '{$moduleInfo->getAssetUrl('js/prefixfree.viewport-units.js')}']});</script>";
        }
        // HTML HEAD HOOK (provided by children)
        $this->insertHtmlHead($moduleData, $unitData);
        echo '</head>';
        // Body
        $rootClasses = array();
        if ($rootApi->isTemplate()) {
            $rootClasses[] = 'isTemplate';
        }
        echo '<body id="' . $rootUnit->getId() . '" class="' . implode(' ', $rootClasses) . '">';
        // CONTENT (child modules of the root module)
        $rootApi->renderChildren($rootUnit);
        // JS Assets (load after DOM)
        $this->insertJsAssets($moduleData);
        $this->insertJsModules($moduleData, $moduleInfo, $rootApi->isEditMode());
        // HTML BOTTOM HOOK (provided by children)
        $this->insertHtmlBottom($moduleData);
        // CSS Error Console
        if (isset($dynCss) && $rootApi->isEditMode() && $rootApi->getFormValue($rootUnit, 'debugShowDynCssErrors')) {
            $this->insertCssErrorPanel($dynCss->getErrors());
        }
        echo '</body>';
        echo '</html>';
    }