Exemplo n.º 1
0
 /**
  * @see Document::delete and Document_PageSnippet::delete
  * @return void
  */
 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_List();
     $redirects->setCondition("target = ?", $this->getId());
     $redirects->load();
     foreach ($redirects->getRedirects() as $redirect) {
         $redirect->delete();
     }
     parent::delete();
 }
Exemplo n.º 2
0
 public function redirectsAction()
 {
     if ($this->_getParam("data")) {
         if ($this->getUser()->isAllowed("redirects")) {
             if ($this->_getParam("xaction") == "destroy") {
                 $id = Zend_Json::decode($this->_getParam("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 {
             Logger::err("user [" . $this->getUser()->getId() . "] attempted to modify static routes, but has no permission to do so.");
         }
     } else {
         // get list of routes
         $list = new Redirect_List();
         $list->setLimit($this->_getParam("limit"));
         $list->setOffset($this->_getParam("start"));
         if ($this->_getParam("sort")) {
             $list->setOrderKey($this->_getParam("sort"));
             $list->setOrder($this->_getParam("dir"));
         }
         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);
 }
Exemplo n.º 3
0
 /**
  * Checks for a suitable redirect
  * @throws Exception
  * @param bool $override
  * @return void
  */
 protected function checkForRedirect($override = false)
 {
     try {
         $cacheKey = "system_route_redirect";
         if (empty($this->redirects) && !($this->redirects = Pimcore_Model_Cache::load($cacheKey))) {
             $list = new Redirect_List();
             $list->setOrder("DESC");
             $list->setOrderKey("priority");
             $this->redirects = $list->load();
             Pimcore_Model_Cache::save($this->redirects, $cacheKey, array("system", "redirect", "route"), null, 998);
         }
         foreach ($this->redirects as $redirect) {
             // 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(), $_SERVER["REQUEST_URI"], $matches)) {
                     array_shift($matches);
                     $target = $redirect->getTarget();
                     if (is_numeric($target)) {
                         $d = Document::getById($target);
                         if ($d instanceof Document_Page) {
                             $target = $d->getFullPath();
                         } else {
                             throw new Exception("Target of redirect no found!");
                         }
                     }
                     $url = vsprintf($target, $matches);
                     header($redirect->getHttpStatus());
                     header("Location: " . $url);
                     exit;
                 }
             }
         }
     } catch (Exception $e) {
         // no suitable route found
     }
 }
Exemplo n.º 4
0
 /**
  * Checks for a suitable redirect
  * @throws Exception
  * @param bool $override
  * @return void
  */
 protected function checkForRedirect($override = false)
 {
     try {
         $cacheKey = "system_route_redirect";
         if (empty($this->redirects) && !($this->redirects = Pimcore_Model_Cache::load($cacheKey))) {
             $list = new Redirect_List();
             $list->setOrder("DESC");
             $list->setOrderKey("priority");
             $this->redirects = $list->load();
             Pimcore_Model_Cache::save($this->redirects, $cacheKey, array("system", "redirect", "route"), null, 998);
         }
         foreach ($this->redirects as $redirect) {
             // 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(), $_SERVER["REQUEST_URI"], $matches)) {
                     array_shift($matches);
                     $target = $redirect->getTarget();
                     if (is_numeric($target)) {
                         $d = Document::getById($target);
                         if ($d instanceof Document_Page) {
                             $target = $d->getFullPath();
                         } else {
                             throw new Exception("Target of redirect no found!");
                         }
                     }
                     // 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);
                     header($redirect->getHttpStatus());
                     header("Location: " . $url, true, $redirect->getStatusCode());
                     // log all redirects to the redirect log
                     Pimcore_Log_Simple::log("redirect", Pimcore_Tool::getClientIp() . " \t Source: " . $_SERVER["REQUEST_URI"] . " -> " . $url);
                     exit;
                 }
             }
         }
     } catch (Exception $e) {
         // no suitable route found
     }
 }
Exemplo n.º 5
0
 public function saveAction()
 {
     if ($this->_getParam("id")) {
         $page = Document_Page::getById($this->_getParam("id"));
         $page = $this->getLatestVersion($page);
         $page->setUserModification($this->getUser()->getId());
         // save to session
         $key = "document_" . $this->_getParam("id");
         $session = new Zend_Session_Namespace("pimcore_documents");
         $session->{$key} = $page;
         if ($this->_getParam("task") == "unpublish") {
             $page->setPublished(false);
         }
         if ($this->_getParam("task") == "publish") {
             $page->setPublished(true);
         }
         // check for redirects
         if ($this->getUser()->isAllowed("redirects") && $this->_getParam("settings")) {
             $settings = Zend_Json::decode($this->_getParam("settings"));
             if (is_array($settings)) {
                 $redirectList = new Redirect_List();
                 $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();
                 }
             }
         }
         // 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->_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->_helper->json(array("success" => true));
                 } catch (Exception $e) {
                     Logger::err($e);
                     $this->_helper->json(array("success" => false, "message" => $e->getMessage()));
                 }
             }
         }
     }
     $this->_helper->json(false);
 }
Exemplo n.º 6
0
 /**
  *
  */
 public static function maintenanceCleanUp()
 {
     $list = new Redirect_List();
     $list->setCondition("expiry < " . time() . " AND expiry IS NOT NULL AND expiry != ''");
     $list->load();
     foreach ($list->getRedirects() as $redirect) {
         echo $redirect->getSource() . "\n";
         $redirect->delete();
     }
 }