baseregions, $this->addonRegions); } public function registerRegion($name) { if(array_search($name, $this->addonRegions) === false) { $this->addonRegions[] = $name; } } public function setRegionContent($region, $content) { $this->fragments[$region] = $content; } public function getRegionContent($region) { if(isset($this->fragments[$region])) return (string) $this->fragments[$region]; return NULL; } /** * helper method, which separates the layout variables from the local variables of a * method */ protected function getFileOutput($filename) { if(!file_exists($filename)) return NULL; ob_start(); include ($filename); return ob_get_clean(); } protected function addBaseData(iSystem $system, $siteId) { $this->setRegionContent('TITLE', $system->getSiteParam($siteId, '', 'title', 'Unnamed Page')); $this->setRegionContent('DESC', $system->getSiteParam($siteId, '', 'layout_desc', '')); $this->setRegionContent('KEYWORDS', $system->getSiteParam($siteId, '', 'layout_tags', '')); $this->setRegionContent('LANG', $system->getActiveLocale()); } public function getDocument($media, Response $response = NULL) { $system = System::getInstance(); $db = $system->getDB(); $agent = $system->getRequest()->getUserAgent(); $site = $db->getObjectP("SELECT * FROM websites WHERE id=?", array($system->getSiteId())); $this->setRegionContent('TITLE', $system->getSiteParam($system->getSiteId(), '', 'title', 'Unnamed Page')); $this->setRegionContent('DESC', $system->getSiteParam($system->getSiteId(), '', 'layout_desc', '')); $this->setRegionContent('KEYWORDS', $system->getSiteParam($system->getSiteId(), '', 'layout_tags', '')); $this->setRegionContent('LANG', $system->getActiveLocale()); $this->layout = $system->getSiteParam($system->getSiteId(), '', 'layout', 'default'); $filebase = __DS__ . $this->layout . __DS__; $layoutbase = false; // start offline part if($site->online == 'offline') { foreach(array( _LAYOUTPATHX_ . $filebase . '_offline_.php', _LAYOUTPATH_ . $filebase . '_offline_.php', _LAYOUTPATH_ . __DS__ . '_system_' . __DS__ . '_offline_.php') as $layoutPath) { if(file_exists($layoutPath)) { $layout = $this->getFileOutput($layoutPath); break; } } $template = new CTemplate(); $template->setRoot(''); $template->loadString($layout); unset($layout); $usrSvc = $system->getUserSvc(); $usr = $usrSvc->getUser(); if(is_null($usr)) { $template->setParam('isUser', false); $template->setParam('isMasterAdmin', false); } else { $template->setParam('isUser', true); $template->setParam('isMasterAdmin', $usrSvc->userHasPriv($usr, $system->getSiteId(), false, false)); } foreach($this->getRegions() as $region) $template->setParam($region, $this->getRegionContent($region)); $template->setParam('HEADDATA', $this->buildHeadData()); $template->setParam('DEBUGINFO', false); return $template->getDocument(); } // end offline part if(defined('_SERVICE_')) return $this->fragments['CONTENT']; $num = 0; $layout = NULL; if($media == 'error') { foreach(array( _LAYOUTPATHX_ . $filebase . '_error_.php', _LAYOUTPATH_ . $filebase . '_error_.php', _LAYOUTPATH_ .__DS__. '_system_' . __DS__ . '_error_.php' ) as $layoutPath) { if(file_exists($layoutPath)) { $layout = $this->getFileOutput($layoutPath); $layoutbase = dirname($layoutPath); break; } $num++; } } else { if($agent->isConsole()) { switch($media) { case 'print': $file = 'print.php'; break; default: $file = 'console.php'; break; } } elseif($agent->isMobile()) { switch($media) { case 'print': $file = 'print.php'; break; default: if($agent->isSmallScreen()) $file = 'small.php'; else $file = 'mobile.php'; break; } } elseif($agent->isSmallScreen()) { switch($media) { case 'print': $file = 'print.php'; break; case 'popup': $file = 'popup.php'; break; default: $file = 'small.php'; break; } } else { switch($media) { case 'print': $file = 'print.php'; break; case 'popup': $file = 'popup.php'; break; default: $file = 'default.php'; break; } } foreach(array( array(_LAYOUTPATHX_ . $filebase . $file,_LAYOUTURLX_ . $filebase), array(_LAYOUTPATHX_ . $filebase . 'default.php',_LAYOUTURLX_ . $filebase), array(_LAYOUTPATH_ . $filebase . $file,_LAYOUTURL_ . $filebase), array(_LAYOUTPATH_ . $filebase . 'default.php',_LAYOUTURL_ . $filebase)) as $layoutPath) { if(file_exists($layoutPath[0])) { $layout = $this->getFileOutput($layoutPath[0]); $layoutbase = dirname($layoutPath[0]); $layouturl = str_replace(__DS__, '/', $layoutPath[1]); break; } $num++; } } if(is_null($layout)) throw new RuntimeException($system->getConfig('layout') . ' is an invalid layout (base: ' . $filebase . ')'); $layout = preg_replace('##', '', $layout); // remove hidden comments // minimize html if($system->getSiteParam($system->getSiteId(), '', 'layout_htmlmin', '') == 'y') { $count = 0; do $layout = preg_replace('#
\\s+<(.*?)/head>#is', '<\\2/head>', $layout, 1, $count); while($count > 0); $layout = preg_replace('#\\s+\n#i', "\n", $layout); $layout = preg_replace('#>\\s+<(/?div|/?p|/?h[1-9])#is', "><\\1", $layout); $layout = preg_replace('#^\\s+<(/?div|/?p|/?h[1-9])#im', " <\\1", $layout); } // minimize css if($system->getSiteParam($system->getSiteId(), '', 'layout_cssmin', '') == 'y') { $cachedir = _DATAPATH_ . __DS__ . 'cache' . __DS__ . 'public' . __DS__ . 'mmpilayout'; if(!is_dir($cachedir)) mkdir($cachedir); $cachedir = $cachedir . __DS__ . 'css'; if(!is_dir($cachedir)) mkdir($cachedir); $cacheurl = _DATAURL_ . '/cache/public/mmpilayout/css'; preg_match_all('#]*href="./([a-zA-Z0-9_-]*/){0,1}([a-zA-Z0-9_-]*\.[cC][sS]{2})"[^>]*/>#is', $layout, $match); if(count($match) > 0) { unset($match[0]); foreach($match[2] as $fid=>$cssfile) { if(empty($match[1][$fid])) { $linkurlbase = $layouturl; $basefile = $layoutbase . __DS__ . $cssfile; $baseurl = './' . $cssfile; $targetfile = $cachedir . __DS__ . $system->getSiteId() . __DS__ . $cssfile; $targeturl = $cacheurl . '/' . $system->getSiteId() . '/' . $cssfile; } else { $linkurlbase = $layouturl . '/' . $match[1][$fid]; $basefile = $layoutbase . __DS__ . $match[1][$fid] . __DS__ . $cssfile; $baseurl = './' . $match[1][$fid] . $cssfile; $targetfile = $cachedir . __DS__ . $system->getSiteId() . __DS__ . $match[1][$fid] . __DS__ . $cssfile; $targeturl = $cacheurl . '/' . $system->getSiteId() . '/' . $match[1][$fid] . $cssfile; } if(file_exists($basefile)) { $basetime = filemtime($basefile); $layout = str_replace($baseurl, $targeturl, $layout); if(file_exists($targetfile) && ($basetime == filemtime($targetfile))) continue; $css = file_get_contents($basefile); $css = preg_replace('%url\\(\\./%i', 'url(\\1' . $linkurlbase, $css); $css = \Misc\CSSMin::minify($css); if(!is_dir($cachedir . __DS__ . $system->getSiteId())) mkdir($cachedir . __DS__ . $system->getSiteId()); if(!empty($match[1][$fid]) && !is_dir(dirname($targetfile))) mkdir(dirname($targetfile)); file_put_contents($targetfile, $css); touch($targetfile, $basetime); } } } } // modifiy relative links to match new location $layoutPath = array(_LAYOUTPATHX_,_LAYOUTPATHX_,_LAYOUTPATH_,_LAYOUTPATH_); $layoutPath = $layoutPath[$num] . $filebase; $layoutPath = preg_replace('#^' . addslashes(_BASEPATH_) . '#', '', $layoutPath, 1); $layoutPath = _BASEURL_ . $layoutPath; $layoutPath = str_replace('\\', '/', $layoutPath); $layout = preg_replace('#((href|src)=["\'])\\.[/\\\\]#is', '\\1' . $layoutPath, $layout); unset($file, $filebase, $layoutPath, $num); $template = new CTemplate(); $template->setRoot(''); $template->loadString($layout); unset($layout); $usrSvc = $system->getUserSvc(); $usr = $usrSvc->getUser(); if(is_null($usr)) { $template->setParam('isUser', false); $template->setParam('isMasterAdmin', false); } else { $template->setParam('isUser', true); $template->setParam('isMasterAdmin', $usrSvc->userHasPriv($usr, $system->getSiteId(), false, false)); } if($media == 'error') $template->setParam('ERROR', $this->getRegionContent('ERROR')); foreach($this->getRegions() as $region) $template->setParam($region, $this->getRegionContent($region)); $template->setParam('HEADDATA', $this->buildHeadData($response)); $template->setParam('DEBUGINFO', false); return $template->getDocument(); } protected function buildHeadData(Response $response = NULL) { $headdata = ""; $headdata .= ""; $headdata .= "generator}\"/>"; $headdata .= ""; if($this->addHtml5Fixes) $headdata .= ""; if(!empty($response) && !empty($response->addons[Response::ADDON_CSS])) { foreach($response->addons[Response::ADDON_CSS] as $cssAddOn) { $headdata .= ''; } } if(!empty($this->addHeaders)) { foreach($this->addHeaders as $headAddOn) { $headdata .= $headAddOn; } } if(!empty($response) && !empty($response->addons[Response::ADDON_JSLIB])) { $jsLibs = $response->addons[Response::ADDON_JSLIB]; $jsLibs = array_unique($jsLibs); foreach($jsLibs as $jsAddOn) $headdata .= ''; unset($jsLibs,$jsAddOn); } $headdata .= "\n"; return $headdata; } }