/** * @param \Zend_Controller_Request_Abstract $request * @throws mixed */ protected function _handleError(\Zend_Controller_Request_Abstract $request) { // remove zend error handler $front = \Zend_Controller_Front::getInstance(); $front->unregisterPlugin("Zend_Controller_Plugin_ErrorHandler"); $response = $this->getResponse(); if ($response->isException() && !$this->_isInsideErrorHandlerLoop) { // get errorpage try { // enable error handler $front->setParam('noErrorHandler', false); $errorPath = Config::getSystemConfig()->documents->error_pages->default; if (Site::isSiteRequest()) { $site = Site::getCurrentSite(); $errorPath = $site->getErrorDocument(); } if (empty($errorPath)) { $errorPath = "/"; } $document = Document::getByPath($errorPath); if (!$document instanceof Document\Page) { // default is home $document = Document::getById(1); } if ($document instanceof Document\Page) { $params = Tool::getRoutingDefaults(); if ($module = $document->getModule()) { $params["module"] = $module; } if ($controller = $document->getController()) { $params["controller"] = $controller; $params["action"] = "index"; } if ($action = $document->getAction()) { $params["action"] = $action; } $this->setErrorHandler($params); $request->setParam("document", $document); \Zend_Registry::set("pimcore_error_document", $document); // ensure that a viewRenderer exists, and is enabled if (!\Zend_Controller_Action_HelperBroker::hasHelper("viewRenderer")) { $viewRenderer = new \Pimcore\Controller\Action\Helper\ViewRenderer(); \Zend_Controller_Action_HelperBroker::addHelper($viewRenderer); } $viewRenderer = \Zend_Controller_Action_HelperBroker::getExistingHelper("viewRenderer"); $viewRenderer->setNoRender(false); if ($viewRenderer->view === null) { $viewRenderer->initView(PIMCORE_WEBSITE_PATH . "/views"); } } } catch (\Exception $e) { \Logger::emergency("error page not found"); } } // call default ZF error handler parent::_handleError($request); }
/** * @param Document $document * @return bool */ public static function isDocumentInCurrentSite($document) { if (Site::isSiteRequest()) { $site = Site::getCurrentSite(); if ($site instanceof Site) { return self::isDocumentInSite($site, $document); } } return true; }
private function getErrorDocument() { $config = Config::getSystemConfig(); $errorDocPath = $config->documents->error_pages->default; if (Site::isSiteRequest()) { $site = Site::getCurrentSite(); $errorDocPath = $site->getErrorDocument(); } $errorDoc = Document::getByPath($errorDocPath); \Zend_Registry::set("pimcore_error_document", $errorDoc); return $errorDoc; }
/** * @static * @return mixed|\Zend_Config */ public static function getWebsiteConfig() { if (\Zend_Registry::isRegistered("pimcore_config_website")) { $config = \Zend_Registry::get("pimcore_config_website"); } else { $cacheKey = "website_config"; $siteId = null; if (Model\Site::isSiteRequest()) { $siteId = Model\Site::getCurrentSite()->getId(); $cacheKey = $cacheKey . "_site_" . $siteId; } if (!($config = Cache::load($cacheKey))) { $settingsArray = array(); $cacheTags = array("website_config", "system", "config", "output"); $list = new Model\WebsiteSetting\Listing(); $list = $list->load(); foreach ($list as $item) { $key = $item->getName(); $itemSiteId = $item->getSiteId(); if ($itemSiteId != 0 && $itemSiteId != $siteId) { continue; } $s = null; switch ($item->getType()) { case "document": case "asset": case "object": $s = Model\Element\Service::getElementById($item->getType(), $item->getData()); break; case "bool": $s = (bool) $item->getData(); break; case "text": $s = (string) $item->getData(); break; } if ($s instanceof Model\Element\ElementInterface) { $cacheTags = $s->getCacheTags($cacheTags); } if (isset($s)) { $settingsArray[$key] = $s; } } $config = new \Zend_Config($settingsArray, true); Cache::save($config, $cacheKey, $cacheTags, null, 998); } self::setWebsiteConfig($config); } return $config; }
/** * @return array|mixed * @throws \Zend_Exception */ protected function getData() { if (\Zend_Registry::isRegistered("Zend_Locale")) { $locale = (string) \Zend_Registry::get("Zend_Locale"); } else { return array(); } $siteId = ""; try { $site = Model\Site::getCurrentSite(); if ($site instanceof Model\Site) { $siteId = $site->getId(); } } catch (\Exception $e) { // not inside a site } $cacheKey = "glossary_" . $locale . "_" . $siteId; try { $data = \Zend_Registry::get($cacheKey); return $data; } catch (\Exception $e) { } if (!($data = Model\Cache::load($cacheKey))) { $list = new Model\Glossary\Listing(); $list->setCondition("(language = ? OR language IS NULL OR language = '') AND (site = ? OR site IS NULL OR site = '')", array($locale, $siteId)); $list->setOrderKey("LENGTH(`text`)", false); $list->setOrder("DESC"); $data = $list->getDataArray(); $data = $this->prepareData($data); Model\Cache::save($data, $cacheKey, array("glossary"), null, 995); \Zend_Registry::set($cacheKey, $data); } return $data; }
/** * @param $activeDocument * @param null $navigationRootDocument * @param null $htmlMenuIdPrefix * @param null $pageCallback * @param bool|string $cache * @return mixed|\Zend_Navigation * @throws \Exception * @throws \Zend_Navigation_Exception */ public function getNavigation($activeDocument, $navigationRootDocument = null, $htmlMenuIdPrefix = null, $pageCallback = null, $cache = true) { $cacheEnabled = (bool) $cache; $this->_htmlMenuIdPrefix = $htmlMenuIdPrefix; if (!$navigationRootDocument) { $navigationRootDocument = Document::getById(1); } $siteSuffix = ""; if (Site::isSiteRequest()) { $site = Site::getCurrentSite(); $siteSuffix = "__site_" . $site->getId(); } $cacheId = $navigationRootDocument->getId(); if (is_string($cache)) { $cacheId .= "_" . $cache; } $cacheKey = "navigation_" . $cacheId . $siteSuffix; $navigation = CacheManager::load($cacheKey); if (!$navigation || !$cacheEnabled) { $navigation = new \Zend_Navigation(); if ($navigationRootDocument->hasChilds()) { $rootPage = $this->buildNextLevel($navigationRootDocument, true, $pageCallback); $navigation->addPages($rootPage); } // we need to force caching here, otherwise the active classes and other settings will be set and later // also written into cache (pass-by-reference) ... when serializing the data directly here, we don't have this problem if ($cacheEnabled) { CacheManager::save($navigation, $cacheKey, ["output", "navigation"], null, 999, true); } } // set active path $activePage = $navigation->findOneBy("realFullPath", $activeDocument->getRealFullPath()); if (!$activePage) { // find by link target $activePage = $navigation->findOneBy("uri", $activeDocument->getRealFullPath()); } if ($activePage) { // we found an active document, so we can build the active trail by getting respectively the parent $this->addActiveCssClasses($activePage, true); } else { // we don't have an active document, so we try to build the trail on our own $allPages = $navigation->findAllBy("uri", "/.*/", true); foreach ($allPages as $page) { $activeTrail = false; if (strpos($activeDocument->getRealFullPath(), $page->getRealFullPath() . "/") === 0) { $activeTrail = true; } if ($page->getDocumentType() == "link") { if (strpos($activeDocument->getFullPath(), $page->getUri() . "/") === 0) { $activeTrail = true; } } if ($activeTrail) { $page->setActive(true); $page->setClass($page->getClass() . " active active-trail"); } } } return $navigation; }
/** * @return string */ public function getPath() { // check for site, if so rewrite the path for output try { if (!\Pimcore::inAdmin() && Site::isSiteRequest()) { $site = Site::getCurrentSite(); if ($site instanceof Site) { if ($site->getRootDocument() instanceof Document\Page && $site->getRootDocument() !== $this) { $rootPath = $site->getRootPath(); $rootPath = preg_quote($rootPath); return preg_replace("@^" . $rootPath . "@", "", $this->path); } } } } catch (\Exception $e) { \Logger::error($e); } return $this->path; }
/** * Checks for a suitable redirect * @throws Exception * @param bool $override * @return void */ protected function checkForRedirect($override = false) { // not for admin requests if (Tool::isFrontentRequestByAdmin()) { return; } try { $front = \Zend_Controller_Front::getInstance(); $config = Config::getSystemConfig(); // get current site if available $sourceSite = null; if (Site::isSiteRequest()) { $sourceSite = Site::getCurrentSite(); } $cacheKey = "system_route_redirect"; if (empty($this->redirects) && !($this->redirects = Cache::load($cacheKey))) { $list = new Redirect\Listing(); $list->setOrder("DESC"); $list->setOrderKey("priority"); $this->redirects = $list->load(); Cache::save($this->redirects, $cacheKey, array("system", "redirect", "route"), null, 998); } $requestScheme = $_SERVER['HTTPS'] == 'on' ? \Zend_Controller_Request_Http::SCHEME_HTTPS : \Zend_Controller_Request_Http::SCHEME_HTTP; $matchRequestUri = $_SERVER["REQUEST_URI"]; $matchUrl = $requestScheme . "://" . $_SERVER["HTTP_HOST"] . $matchRequestUri; foreach ($this->redirects as $redirect) { $matchAgainst = $matchRequestUri; if ($redirect->getSourceEntireUrl()) { $matchAgainst = $matchUrl; } // if override is true the priority has to be 99 which means that overriding is ok if (!$override || $override && $redirect->getPriority() == 99) { if (@preg_match($redirect->getSource(), $matchAgainst, $matches)) { // check for a site if ($sourceSite) { if ($sourceSite->getId() != $redirect->getSourceSite()) { continue; } } array_shift($matches); $target = $redirect->getTarget(); if (is_numeric($target)) { $d = Document::getById($target); if ($d instanceof Document\Page || $d instanceof Document\Link || $d instanceof Document\Hardlink) { $target = $d->getFullPath(); } else { \Logger::error("Target of redirect no found (Document-ID: " . $target . ")!"); continue; } } // replace escaped % signs so that they didn't have effects to vsprintf (PIMCORE-1215) $target = str_replace("\\%", "###URLENCODE_PLACEHOLDER###", $target); $url = vsprintf($target, $matches); $url = str_replace("###URLENCODE_PLACEHOLDER###", "%", $url); // support for pcre backreferences $url = replace_pcre_backreferences($url, $matches); if ($redirect->getTargetSite() && !preg_match("@http(s)?://@i", $url)) { try { $targetSite = Site::getById($redirect->getTargetSite()); // if the target site is specified and and the target-path is starting at root (not absolute to site) // the root-path will be replaced so that the page can be shown $url = preg_replace("@^" . $targetSite->getRootPath() . "/@", "/", $url); $url = $requestScheme . "://" . $targetSite->getMainDomain() . $url; } catch (\Exception $e) { \Logger::error("Site with ID " . $redirect->getTargetSite() . " not found."); continue; } } else { if (!preg_match("@http(s)?://@i", $url) && $config->general->domain && $redirect->getSourceEntireUrl()) { // prepend the host and scheme to avoid infinite loops when using "domain" redirects $url = ($front->getRequest()->isSecure() ? "https" : "http") . "://" . $config->general->domain . $url; } } // pass-through parameters if specified $queryString = $_SERVER["QUERY_STRING"]; if ($redirect->getPassThroughParameters() && !empty($queryString)) { $glue = "?"; if (strpos($url, "?")) { $glue = "&"; } $url .= $glue; $url .= $queryString; } header($redirect->getHttpStatus()); header("Location: " . $url, true, $redirect->getStatusCode()); // log all redirects to the redirect log \Pimcore\Log\Simple::log("redirect", Tool::getAnonymizedClientIp() . " \t Custom-Redirect ID: " . $redirect->getId() . " , Source: " . $_SERVER["REQUEST_URI"] . " -> " . $url); exit; } } } } catch (\Exception $e) { // no suitable route found } }
/** * @param array $urlOptions * @param null $name * @param bool $reset * @param bool $encode * @return string|void * @throws \Exception */ public function url(array $urlOptions = array(), $name = null, $reset = false, $encode = true) { if (!$urlOptions) { $urlOptions = array(); } // when using $name = false we don't use the default route (happens when $name = null / ZF default behavior) // but just the query string generation using the given parameters // eg. $this->url(["foo" => "bar"], false) => /?foo=bar if ($name === null) { if (Staticroute::getCurrentRoute() instanceof Staticroute) { $name = Staticroute::getCurrentRoute()->getName(); } } $siteId = null; if (Site::isSiteRequest()) { $siteId = Site::getCurrentSite()->getId(); } // check for a site in the options, if valid remove it from the options $hostname = null; if (isset($urlOptions["site"])) { $config = Config::getSystemConfig(); $site = $urlOptions["site"]; if (!empty($site)) { try { $site = Site::getBy($site); unset($urlOptions["site"]); $hostname = $site->getMainDomain(); $siteId = $site->getId(); } catch (\Exception $e) { \Logger::warn("passed site doesn't exists"); \Logger::warn($e); } } else { if ($config->general->domain) { $hostname = $config->general->domain; } } } if ($name && ($route = Staticroute::getByName($name, $siteId))) { // assemble the route / url in Staticroute::assemble() $url = $route->assemble($urlOptions, $reset, $encode); // if there's a site, prepend the host to the generated URL if ($hostname && !preg_match("/^http/i", $url)) { $url = "//" . $hostname . $url; } if (Config::getSystemConfig()->documents->allowcapitals == 'no') { $urlParts = parse_url($url); $url = str_replace($urlParts["path"], strtolower($urlParts["path"]), $url); } return $url; } // this is to add support for arrays as values for the default \Zend_View_Helper_Url $unset = array(); foreach ($urlOptions as $optionName => $optionValues) { if (is_array($optionValues)) { foreach ($optionValues as $key => $value) { $urlOptions[$optionName . "[" . $key . "]"] = $value; } $unset[] = $optionName; } } foreach ($unset as $optionName) { unset($urlOptions[$optionName]); } try { return parent::url($urlOptions, $name, $reset, $encode); } catch (\Exception $e) { if (Tool::isFrontentRequestByAdmin()) { // routes can be site specific, so in editmode it's possible that we don't get // the right route (sites are not registered in editmode), so we cannot throw exceptions there return "ERROR_IN_YOUR_URL_CONFIGURATION:~ROUTE--" . $name . "--DOES_NOT_EXIST"; } throw new \Exception("Route '" . $name . "' for building the URL not found"); } }
/** * @param array $urlOptions * @param null $name * @param bool $reset * @param bool $encode * @return string|void * @throws \Exception */ public function url(array $urlOptions = array(), $name = null, $reset = false, $encode = true) { if (!$urlOptions) { $urlOptions = array(); } if (!$name) { if (Staticroute::getCurrentRoute() instanceof Staticroute) { $name = Staticroute::getCurrentRoute()->getName(); } } $siteId = null; if (Site::isSiteRequest()) { $siteId = Site::getCurrentSite()->getId(); } // check for a site in the options, if valid remove it from the options $hostname = null; if (isset($urlOptions["site"])) { $config = Config::getSystemConfig(); $site = $urlOptions["site"]; if (!empty($site)) { try { $site = Site::getBy($site); unset($urlOptions["site"]); $hostname = $site->getMainDomain(); $siteId = $site->getId(); } catch (\Exception $e) { \Logger::warn("passed site doesn't exists"); \Logger::warn($e); } } else { if ($config->general->domain) { $hostname = $config->general->domain; } } } if ($name && ($route = Staticroute::getByName($name, $siteId))) { // assemble the route / url in Staticroute::assemble() $url = $route->assemble($urlOptions, $reset, $encode); // if there's a site, prepend the host to the generated URL if ($hostname && !preg_match("/^http/i", $url)) { $url = "//" . $hostname . $url; } if (Config::getSystemConfig()->documents->allowcapitals == 'no') { $urlParts = parse_url($url); $url = str_replace($urlParts["path"], strtolower($urlParts["path"]), $url); } return $url; } // this is to add support for arrays as values for the default \Zend_View_Helper_Url $unset = array(); foreach ($urlOptions as $optionName => $optionValues) { if (is_array($optionValues)) { foreach ($optionValues as $key => $value) { $urlOptions[$optionName . "[" . $key . "]"] = $value; } $unset[] = $optionName; } } foreach ($unset as $optionName) { unset($urlOptions[$optionName]); } try { return parent::url($urlOptions, $name, $reset, $encode); } catch (\Exception $e) { throw new \Exception("Route '" . $name . "' for building the URL not found"); } }
/** * @static * @return mixed|\Zend_Config */ public static function getWebsiteConfig() { if (\Zend_Registry::isRegistered("pimcore_config_website")) { $config = \Zend_Registry::get("pimcore_config_website"); } else { $cacheKey = "website_config"; $siteId = null; if (Model\Site::isSiteRequest()) { $siteId = Model\Site::getCurrentSite()->getId(); } elseif (Tool::isFrontentRequestByAdmin()) { // this is necessary to set the correct settings in editmode/preview (using the main domain) $front = \Zend_Controller_Front::getInstance(); $originDocument = $front->getRequest()->getParam("document"); if ($originDocument) { $site = Tool\Frontend::getSiteForDocument($originDocument); if ($site) { $siteId = $site->getId(); } } } if ($siteId) { $cacheKey = $cacheKey . "_site_" . $siteId; } if (!($config = Cache::load($cacheKey))) { $settingsArray = []; $cacheTags = ["website_config", "system", "config", "output"]; $list = new Model\WebsiteSetting\Listing(); $list = $list->load(); foreach ($list as $item) { $key = $item->getName(); $itemSiteId = $item->getSiteId(); if ($itemSiteId != 0 && $itemSiteId != $siteId) { continue; } $s = null; switch ($item->getType()) { case "document": case "asset": case "object": $s = Model\Element\Service::getElementById($item->getType(), $item->getData()); break; case "bool": $s = (bool) $item->getData(); break; case "text": $s = (string) $item->getData(); break; } if ($s instanceof Model\Element\ElementInterface) { $cacheTags = $s->getCacheTags($cacheTags); } if (isset($s)) { $settingsArray[$key] = $s; } } $config = new \Zend_Config($settingsArray, true); Cache::save($config, $cacheKey, $cacheTags, null, 998); } self::setWebsiteConfig($config); } return $config; }
/** * @param $activeDocument * @param null $navigationRootDocument * @param null $htmlMenuIdPrefix * @param null $pageCallback * @param bool|string $cache * @return mixed|\Zend_Navigation * @throws \Exception * @throws \Zend_Navigation_Exception */ public function getNavigation($activeDocument, $navigationRootDocument = null, $htmlMenuIdPrefix = null, $pageCallback = null, $cache = true) { $cacheEnabled = (bool) $cache; $this->_htmlMenuIdPrefix = $htmlMenuIdPrefix; if (!$navigationRootDocument) { $navigationRootDocument = Document::getById(1); } $cacheKeys = []; if (Site::isSiteRequest()) { $site = Site::getCurrentSite(); $cacheKeys[] = "site__" . $site->getId(); } $cacheKeys[] = "root_id__" . $navigationRootDocument->getId(); if (is_string($cache)) { $cacheKeys[] = "custom__" . $cache; } if ($pageCallback instanceof \Closure) { $cacheKeys[] = "pageCallback_" . closureHash($pageCallback); } $cacheKey = "nav_" . md5(serialize($cacheKeys)); $navigation = CacheManager::load($cacheKey); if (!$navigation || !$cacheEnabled) { $navigation = new \Zend_Navigation(); if ($navigationRootDocument->hasChilds()) { $rootPage = $this->buildNextLevel($navigationRootDocument, true, $pageCallback); $navigation->addPages($rootPage); } // we need to force caching here, otherwise the active classes and other settings will be set and later // also written into cache (pass-by-reference) ... when serializing the data directly here, we don't have this problem if ($cacheEnabled) { CacheManager::save($navigation, $cacheKey, ["output", "navigation"], null, 999, true); } } // set active path $front = \Zend_Controller_Front::getInstance(); $request = $front->getRequest(); // try to find a page matching exactly the request uri $activePages = $navigation->findAllBy("uri", $request->getRequestUri()); if (empty($activePages)) { // try to find a page matching the path info $activePages = $navigation->findAllBy("uri", $request->getPathInfo()); } if (empty($activePages)) { // use the provided pimcore document $activePages = $navigation->findAllBy("realFullPath", $activeDocument->getRealFullPath()); } if (empty($activePages)) { // find by link target $activePages = $navigation->findAllBy("uri", $activeDocument->getFullPath()); } // cleanup active pages from links // pages have priority, if we don't find any active page, we use all we found $tmpPages = []; foreach ($activePages as $page) { if ($page instanceof Uri && $page->getDocumentType() != "link") { $tmpPages[] = $page; } } if (count($tmpPages)) { $activePages = $tmpPages; } if (!empty($activePages)) { // we found an active document, so we can build the active trail by getting respectively the parent foreach ($activePages as $activePage) { $this->addActiveCssClasses($activePage, true); } } else { // we don't have an active document, so we try to build the trail on our own $allPages = $navigation->findAllBy("uri", "/.*/", true); foreach ($allPages as $page) { $activeTrail = false; if ($page->getUri() && strpos($activeDocument->getRealFullPath(), $page->getUri() . "/") === 0) { $activeTrail = true; } if ($page instanceof Uri) { if ($page->getDocumentType() == "link") { if ($page->getUri() && strpos($activeDocument->getFullPath(), $page->getUri() . "/") === 0) { $activeTrail = true; } } } if ($activeTrail) { $page->setActive(true); $page->setClass($page->getClass() . " active active-trail"); } } } return $navigation; }
/** * @param string $path * @param array $params * @return array|bool * @throws \Exception */ public function match($path, $params = []) { if (@preg_match($this->getPattern(), $path)) { // check for site if ($this->getSiteId()) { if (!Site::isSiteRequest() || $this->getSiteId() != Site::getCurrentSite()->getId()) { return false; } } // we need to unset this 3 params here, because otherwise the defaults wouldn't have an effect if used // in combination with dynamic action/controller/module configurations unset($params["controller"], $params["action"], $params["module"]); $params = array_merge($this->getDefaultsArray(), $params); $variables = explode(",", $this->getVariables()); preg_match_all($this->getPattern(), $path, $matches); if (is_array($matches) && count($matches) > 1) { foreach ($matches as $index => $match) { if (isset($variables[$index - 1]) && $variables[$index - 1]) { $paramValue = urldecode($match[0]); if (!empty($paramValue) || !array_key_exists($variables[$index - 1], $params)) { $params[$variables[$index - 1]] = $paramValue; } } } } $controller = $this->getController(); $action = $this->getAction(); $module = trim($this->getModule()); // check for dynamic controller / action / module $dynamicRouteReplace = function ($item, $params) { if (strpos($item, "%") !== false) { uksort($params, function ($a, $b) { // order by key length, longer key have priority // (%abcd prior %ab, so that %ab doesn't replace %ab in [%ab]cd) return strlen($b) - strlen($a); }); foreach ($params as $key => $value) { $dynKey = "%" . $key; if (strpos($item, $dynKey) !== false) { return str_replace($dynKey, $value, $item); } } } return $item; }; $controller = $dynamicRouteReplace($controller, $params); $action = $dynamicRouteReplace($action, $params); $module = $dynamicRouteReplace($module, $params); $params["controller"] = $controller; $params["action"] = $action; if (!empty($module)) { $params["module"] = $module; } // remember for reverse assemble $this->_values = $params; return $params; } return []; }
/** * */ public function dispatchLoopShutdown() { if (!\Pimcore\Tool::isHtmlResponse($this->getResponse())) { return; } $list = new Tag\Config\Listing(); $tags = $list->load(); if (empty($tags)) { return; } $html = null; $body = $this->getResponse()->getBody(); $requestParams = array_merge($_GET, $_POST); foreach ($tags as $tag) { $method = strtolower($tag->getHttpMethod()); $pattern = $tag->getUrlPattern(); $textPattern = $tag->getTextPattern(); // site check if (Site::isSiteRequest() && $tag->getSiteId()) { if (Site::getCurrentSite()->getId() != $tag->getSiteId()) { continue; } } else { if (!Site::isSiteRequest() && $tag->getSiteId()) { continue; } } $requestPath = rtrim($this->getRequest()->getPathInfo(), "/"); if (($method == strtolower($this->getRequest()->getMethod()) || empty($method)) && (empty($pattern) || @preg_match($pattern, $requestPath)) && (empty($textPattern) || strpos($body, $textPattern) !== false)) { $paramsValid = true; foreach ($tag->getParams() as $param) { if (!empty($param["name"])) { if (!empty($param["value"])) { if (!array_key_exists($param["name"], $requestParams) || $requestParams[$param["name"]] != $param["value"]) { $paramsValid = false; } } else { if (!array_key_exists($param["name"], $requestParams)) { $paramsValid = false; } } } } if (is_array($tag->getItems()) && $paramsValid) { foreach ($tag->getItems() as $item) { if (!empty($item["element"]) && !empty($item["code"]) && !empty($item["position"])) { if (!$html) { include_once "simple_html_dom.php"; $html = str_get_html($body); } if ($html) { $element = $html->find($item["element"], 0); if ($element) { if ($item["position"] == "end") { $element->innertext = $element->innertext . "\n\n" . $item["code"] . "\n\n"; } else { // beginning $element->innertext = "\n\n" . $item["code"] . "\n\n" . $element->innertext; } // we havve to reinitialize the html object, otherwise it causes problems with nested child selectors $body = $html->save(); $html->clear(); unset($html); $html = null; } } } } } } } if ($html && method_exists($html, "clear")) { $html->clear(); unset($html); } $this->getResponse()->setBody($body); }