/** * @throws \Exception */ public function delete() { if ($this->getId() == 1) { throw new \Exception("root-node cannot be deleted"); } // check for redirects pointing to this document, and delete them too $redirects = new Redirect\Listing(); $redirects->setCondition("target = ?", $this->getId()); $redirects->load(); foreach ($redirects->getRedirects() as $redirect) { $redirect->delete(); } parent::delete(); }
/** * @see Document::delete * @return void */ public function delete() { // hardlinks cannot have direct children in "real" world, so we have to empty them before we delete it $this->childs = []; // check for redirects pointing to this document, and delete them too $redirects = new Redirect\Listing(); $redirects->setCondition("target = ?", $this->getId()); $redirects->load(); foreach ($redirects->getRedirects() as $redirect) { $redirect->delete(); } parent::delete(); // we re-enable the children functionality by setting them to NULL, if requested they'll be loaded again // -> see $this->getChilds() , doesn't make sense when deleting an item but who knows, ... ;-) $this->childs = null; }
public function redirectsAction() { if ($this->getParam("data")) { $this->checkPermission("redirects"); if ($this->getParam("xaction") == "destroy") { $data = \Zend_Json::decode($this->getParam("data")); if (\Pimcore\Tool\Admin::isExtJS6()) { $id = $data["id"]; } else { $id = $data; } $redirect = Redirect::getById($id); $redirect->delete(); $this->_helper->json(array("success" => true, "data" => array())); } else { if ($this->getParam("xaction") == "update") { $data = \Zend_Json::decode($this->getParam("data")); // save redirect $redirect = Redirect::getById($data["id"]); if ($data["target"]) { if ($doc = Document::getByPath($data["target"])) { $data["target"] = $doc->getId(); } } $redirect->setValues($data); $redirect->save(); $redirectTarget = $redirect->getTarget(); if (is_numeric($redirectTarget)) { if ($doc = Document::getById(intval($redirectTarget))) { $redirect->setTarget($doc->getFullPath()); } } $this->_helper->json(array("data" => $redirect, "success" => true)); } else { if ($this->getParam("xaction") == "create") { $data = \Zend_Json::decode($this->getParam("data")); unset($data["id"]); // save route $redirect = new Redirect(); if ($data["target"]) { if ($doc = Document::getByPath($data["target"])) { $data["target"] = $doc->getId(); } } $redirect->setValues($data); $redirect->save(); $redirectTarget = $redirect->getTarget(); if (is_numeric($redirectTarget)) { if ($doc = Document::getById(intval($redirectTarget))) { $redirect->setTarget($doc->getFullPath()); } } $this->_helper->json(array("data" => $redirect, "success" => true)); } } } } else { // get list of routes $list = new Redirect\Listing(); $list->setLimit($this->getParam("limit")); $list->setOffset($this->getParam("start")); $sortingSettings = \Pimcore\Admin\Helper\QueryParams::extractSortingSettings($this->getAllParams()); if ($sortingSettings['orderKey']) { $list->setOrderKey($sortingSettings['orderKey']); $list->setOrder($sortingSettings['order']); } if ($this->getParam("filter")) { $list->setCondition("`source` LIKE " . $list->quote("%" . $this->getParam("filter") . "%") . " OR `target` LIKE " . $list->quote("%" . $this->getParam("filter") . "%")); } $list->load(); $redirects = array(); foreach ($list->getRedirects() as $redirect) { if ($link = $redirect->getTarget()) { if (is_numeric($link)) { if ($doc = Document::getById(intval($link))) { $redirect->setTarget($doc->getFullPath()); } } } $redirects[] = $redirect; } $this->_helper->json(array("data" => $redirects, "success" => true, "total" => $list->getTotalCount())); } $this->_helper->json(false); }
/** * 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 } }
/** * */ public static function maintenanceCleanUp() { $list = new Redirect\Listing(); $list->setCondition("expiry < " . time() . " AND expiry IS NOT NULL AND expiry != ''"); $list->load(); foreach ($list->getRedirects() as $redirect) { $redirect->setActive(false); $redirect->save(); } }
public function saveAction() { if ($this->getParam("id")) { $page = Document\Page::getById($this->getParam("id")); $page = $this->getLatestVersion($page); $page->setUserModification($this->getUser()->getId()); if ($this->getParam("task") == "unpublish") { $page->setPublished(false); } if ($this->getParam("task") == "publish") { $page->setPublished(true); } $settings = array(); if ($this->getParam("settings")) { $settings = \Zend_Json::decode($this->getParam("settings")); } // check for redirects if ($this->getUser()->isAllowed("redirects") && $this->getParam("settings")) { if (is_array($settings)) { $redirectList = new Redirect\Listing(); $redirectList->setCondition("target = ?", $page->getId()); $existingRedirects = $redirectList->load(); $existingRedirectIds = array(); foreach ($existingRedirects as $existingRedirect) { $existingRedirectIds[$existingRedirect->getId()] = $existingRedirect->getId(); } for ($i = 1; $i < 100; $i++) { if (array_key_exists("redirect_url_" . $i, $settings)) { // check for existing if ($settings["redirect_id_" . $i]) { $redirect = Redirect::getById($settings["redirect_id_" . $i]); unset($existingRedirectIds[$redirect->getId()]); } else { // create new one $redirect = new Redirect(); } $redirect->setSource($settings["redirect_url_" . $i]); $redirect->setTarget($page->getId()); $redirect->setStatusCode(301); $redirect->save(); } } // remove existing redirects which were delete foreach ($existingRedirectIds as $existingRedirectId) { $redirect = Redirect::getById($existingRedirectId); $redirect->delete(); } } } // check if settings exist, before saving meta data if ($this->getParam("settings") && is_array($settings)) { $metaData = array(); for ($i = 1; $i < 30; $i++) { if (array_key_exists("metadata_idName_" . $i, $settings)) { $metaData[] = array("idName" => $settings["metadata_idName_" . $i], "idValue" => $settings["metadata_idValue_" . $i], "contentName" => $settings["metadata_contentName_" . $i], "contentValue" => $settings["metadata_contentValue_" . $i]); } } $page->setMetaData($metaData); } // only save when publish or unpublish if ($this->getParam("task") == "publish" && $page->isAllowed("publish") or $this->getParam("task") == "unpublish" && $page->isAllowed("unpublish")) { $this->setValuesToDocument($page); try { $page->save(); $this->saveToSession($page); $this->_helper->json(array("success" => true)); } catch (\Exception $e) { \Logger::err($e); $this->_helper->json(array("success" => false, "message" => $e->getMessage())); } } else { if ($page->isAllowed("save")) { $this->setValuesToDocument($page); try { $page->saveVersion(); $this->saveToSession($page); $this->_helper->json(array("success" => true)); } catch (\Exception $e) { \Logger::err($e); $this->_helper->json(array("success" => false, "message" => $e->getMessage())); } } } } $this->_helper->json(false); }
public function saveAction() { try { if ($this->getParam("id")) { $page = Document\Page::getById($this->getParam("id")); // check if there's a document in session which should be used as data-source // see also self::clearEditableDataAction() | this is necessary to reset all fields and to get rid of // outdated and unused data elements in this document (eg. entries of area-blocks) $pageSession = Session::useSession(function ($session) use($page) { if (isset($session->{"document_" . $page->getId()}) && isset($session->{"document_" . $page->getId() . "_useForSave"})) { if ($session->{"document_" . $page->getId() . "_useForSave"}) { // only use the page from the session once unset($session->{"document_" . $page->getId() . "_useForSave"}); return $session->{"document_" . $page->getId()}; } } return null; }, "pimcore_documents"); if ($pageSession) { $page = $pageSession; } else { $page = $this->getLatestVersion($page); } $page->setUserModification($this->getUser()->getId()); if ($this->getParam("task") == "unpublish") { $page->setPublished(false); } if ($this->getParam("task") == "publish") { $page->setPublished(true); } $settings = []; if ($this->getParam("settings")) { $settings = \Zend_Json::decode($this->getParam("settings")); } // check for redirects if ($this->getUser()->isAllowed("redirects") && $this->getParam("settings")) { if (is_array($settings)) { $redirectList = new Redirect\Listing(); $redirectList->setCondition("target = ?", $page->getId()); $existingRedirects = $redirectList->load(); $existingRedirectIds = []; foreach ($existingRedirects as $existingRedirect) { $existingRedirectIds[$existingRedirect->getId()] = $existingRedirect->getId(); } for ($i = 1; $i < 100; $i++) { if (array_key_exists("redirect_url_" . $i, $settings)) { // check for existing if ($settings["redirect_id_" . $i]) { $redirect = Redirect::getById($settings["redirect_id_" . $i]); unset($existingRedirectIds[$redirect->getId()]); } else { // create new one $redirect = new Redirect(); } $redirect->setSource($settings["redirect_url_" . $i]); $redirect->setTarget($page->getId()); $redirect->setStatusCode(301); $redirect->save(); } } // remove existing redirects which were delete foreach ($existingRedirectIds as $existingRedirectId) { $redirect = Redirect::getById($existingRedirectId); $redirect->delete(); } } } // check if settings exist, before saving meta data if ($this->getParam("settings") && is_array($settings)) { $metaData = []; for ($i = 1; $i < 30; $i++) { if (array_key_exists("metadata_" . $i, $settings)) { $metaData[] = $settings["metadata_" . $i]; } } $page->setMetaData($metaData); } // only save when publish or unpublish if ($this->getParam("task") == "publish" && $page->isAllowed("publish") or $this->getParam("task") == "unpublish" && $page->isAllowed("unpublish")) { $this->setValuesToDocument($page); try { $page->save(); $this->saveToSession($page); $this->_helper->json(["success" => true]); } catch (\Exception $e) { if (\Pimcore\Tool\Admin::isExtJS6() && $e instanceof Element\ValidationException) { throw $e; } Logger::err($e); $this->_helper->json(["success" => false, "message" => $e->getMessage()]); } } else { if ($page->isAllowed("save")) { $this->setValuesToDocument($page); try { $page->saveVersion(); $this->saveToSession($page); $this->_helper->json(["success" => true]); } catch (\Exception $e) { Logger::err($e); $this->_helper->json(["success" => false, "message" => $e->getMessage()]); } } } } } catch (\Exception $e) { Logger::log($e); if (\Pimcore\Tool\Admin::isExtJS6() && $e instanceof Element\ValidationException) { $this->_helper->json(["success" => false, "type" => "ValidationException", "message" => $e->getMessage(), "stack" => $e->getTraceAsString(), "code" => $e->getCode()]); } throw $e; } $this->_helper->json(false); }