Exemplo n.º 1
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.º 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!");
                         }
                     }
                     // 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
     }
 }