private function deleteRouteIfExist($routeName) { $routeHinder = Staticroute::getByName($routeName); if ($routeHinder instanceof Staticroute) { $routeHinder->delete(); } }
public function url($urlOptions = array(), $name = null, $reset = false, $encode = true) { if (!$urlOptions) { $urlOptions = array(); } if (!$name) { if (Staticroute::getCurrentRoute() instanceof Staticroute) { $name = Staticroute::getCurrentRoute()->getName(); } } if ($name && ($route = Staticroute::getByName($name))) { return $route->assemble($urlOptions, $reset, $encode); } // 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"); } }
/** * Loads a list of static routes for the specicifies parameters, returns an array of Staticroute elements * * @return array */ public function load() { $routesData = $this->db->fetchAll("SELECT id FROM staticroutes" . $this->getCondition() . $this->getOrder() . $this->getOffsetLimit(), $this->model->getConditionVariables()); $routes = array(); foreach ($routesData as $routeData) { $routes[] = Staticroute::getById($routeData["id"]); } $this->model->setRoutes($routes); return $routes; }
/** * Check if at least one static route is present. * This is the minimum required for the plugin to work. * @return bool TRUE if at least one static route is present. */ public static function hasStaticRoutes() { $conf = self::getStaticRoutesConfig(); foreach ($conf->routes->route as $def) { $route = Staticroute::getByName($def->name); if ($route) { return true; } } return false; }
public function init() { parent::init(); try { $this->_locale = Zend_Registry::get('Zend_Locale'); } catch (Exception $e) { $this->_locale = new Zend_Locale('en'); Zend_Registry::set('Zend_Locale', $this->_locale); } $this->view->locale = $this->_locale; $this->_translate = $this->initTranslation(); $this->view->setScriptPath(array_merge($this->view->getScriptPaths(), array(PIMCORE_WEBSITE_PATH . '/views/scripts/', PIMCORE_WEBSITE_PATH . '/views/layouts/', PIMCORE_WEBSITE_PATH . '/views/scripts/blog/'))); // additional helpers $this->_helper->addPrefix('Modern_Controller_Action_Helper'); $this->view->addHelperPath('Modern/View/Helper', 'Modern_View_Helper_'); $this->_messenger = $this->_helper->getHelper('FlashMessenger'); Modern_View_Helper_FlashMessenger::setPartial('partial/messenger.php'); Zend_View_Helper_PaginationControl::setDefaultViewPartial('partial/paginator-search.php'); // fix main blog list pagination if (!Staticroute::getCurrentRoute() instanceof Staticroute) { Staticroute::setCurrentRoute(Staticroute::getByName('blog')); } }
/** * @param array $urlOptions * @param bool $reset * @param bool $encode * @return mixed|string */ public function assemble(array $urlOptions = array(), $reset = false, $encode = true) { // get request parameters $blockedRequestParams = array("controller", "action", "module", "document"); $front = \Zend_Controller_Front::getInstance(); if ($reset) { $requestParameters = array(); } else { $requestParameters = $front->getRequest()->getParams(); // remove blocked parameters from request foreach ($blockedRequestParams as $key) { if (array_key_exists($key, $requestParameters)) { unset($requestParameters[$key]); } } } $defaultValues = $this->getDefaultsArray(); // apply values (controller,action,module, ... ) from previous match if applicable (only when ) if ($reset) { if (self::$_currentRoute && self::$_currentRoute->getName() == $this->getName()) { $defaultValues = array_merge($defaultValues, self::$_currentRoute->_values); } } // merge with defaults $urlParams = array_merge($requestParameters, $defaultValues, $urlOptions); $parametersInReversePattern = array(); $parametersGet = array(); $parametersNotNamed = array(); $url = $this->getReverse(); $forbiddenCharacters = array("#", ":", "?"); // check for named variables foreach ($urlParams as $key => $param) { if (strpos($this->getReverse(), "%" . $key) !== false) { $parametersInReversePattern[$key] = $param; } else { if (is_numeric($key)) { $parametersNotNamed[$key] = $param; } else { // only append the get parameters if there are defined in $urlOptions // or if they are defined in $_GET an $reset is false if (array_key_exists($key, $urlOptions) || !$reset && array_key_exists($key, $_GET)) { $parametersGet[$key] = $param; } } } } $urlEncodeEscapeCharacters = "~|urlen" . md5(microtime()) . "code|~"; // replace named variables uksort($parametersInReversePattern, 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 ($parametersInReversePattern as $key => $value) { $value = str_replace($forbiddenCharacters, "", $value); if (strlen($value) > 0) { $url = str_replace("%" . $key, str_replace("%", $urlEncodeEscapeCharacters, $encode ? urlencode_ignore_slash($value) : $value), $url); } } // not named parameters $o = array(); foreach ($parametersNotNamed as $option) { $option = str_replace($forbiddenCharacters, "", $option); $o[] = str_replace("%", $urlEncodeEscapeCharacters, $encode ? urlencode_ignore_slash($option) : $option); } // remove optional parts $url = preg_replace("/\\{([^\\}]+)?%[^\\}]+\\}/", "", $url); $url = str_replace(array("{", "}"), "", $url); $url = @vsprintf($url, $o); if (empty($url)) { $url = "ERROR_IN_YOUR_URL_CONFIGURATION:~ONE_PARAMETER_IS_MISSING_TO_GENERATE_THE_URL"; return $url; } // optional get parameters if (!empty($parametersGet)) { if ($encode) { $getParams = array_urlencode($parametersGet); } else { $getParams = array_toquerystring($parametersGet); } $url .= "?" . $getParams; } // convert tmp urlencode escape char back to real escape char $url = str_replace($urlEncodeEscapeCharacters, "%", $url); return $url; }
public function staticroutesAction() { if ($this->_getParam("data")) { if ($this->getUser()->isAllowed("routes")) { if ($this->_getParam("xaction") == "destroy") { $id = Zend_Json::decode($this->_getParam("data")); $route = Staticroute::getById($id); $route->delete(); $this->_helper->json(array("success" => true, "data" => array())); } else { if ($this->_getParam("xaction") == "update") { $data = Zend_Json::decode($this->_getParam("data")); // save routes $route = Staticroute::getById($data["id"]); $route->setValues($data); $route->save(); $this->_helper->json(array("data" => $route, "success" => true)); } else { if ($this->_getParam("xaction") == "create") { $data = Zend_Json::decode($this->_getParam("data")); unset($data["id"]); // save route $route = new Staticroute(); $route->setValues($data); $route->save(); $this->_helper->json(array("data" => $route, "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 Staticroute_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("`name` LIKE " . $list->quote("%" . $this->_getParam("filter") . "%") . " OR `pattern` LIKE " . $list->quote("%" . $this->_getParam("filter") . "%") . " OR `reverse` LIKE " . $list->quote("%" . $this->_getParam("filter") . "%") . " OR `controller` LIKE " . $list->quote("%" . $this->_getParam("filter") . "%") . " OR `action` LIKE " . $list->quote("%" . $this->_getParam("filter") . "%")); } $list->load(); $routes = array(); foreach ($list->getRoutes() as $route) { $routes[] = $route; } $this->_helper->json(array("data" => $routes, "success" => true, "total" => $list->getTotalCount())); } $this->_helper->json(false); }
/** * @param $path * @param bool $partial * @return array|bool */ public function match($path, $partial = false) { $matchFound = false; $config = Pimcore_Config::getSystemConfig(); $routeingDefaults = Pimcore_Tool::getRoutingDefaults(); $params = array_merge($_GET, $_POST); $params = array_merge($routeingDefaults, $params); // set the original path $originalPath = $path; // check for a registered site try { if ($config->general->domain != $_SERVER["HTTP_HOST"]) { $domain = $_SERVER["HTTP_HOST"]; $site = Site::getByDomain($domain); $site->setRootPath($site->getRootDocument()->getFullPath()); $path = $site->getRootDocument()->getFullPath() . $path; Zend_Registry::set("pimcore_site", $site); } } catch (Exception $e) { } // check for direct definition of controller/action if (!empty($_REQUEST["controller"]) && !empty($_REQUEST["action"])) { $matchFound = true; //$params["document"] = $this->getNearestDocumentByPath($path); } // you can also call a page by it's ID /?pimcore_document=XXXX if (!$matchFound) { if (!empty($params["pimcore_document"]) || !empty($params["pdid"])) { $doc = Document::getById($params["pimcore_document"] ? $params["pimcore_document"] : $params["pdid"]); if ($doc instanceof Document) { $path = $doc->getFullPath(); } } } // test if there is a suitable redirect with override = all (=> priority = 99) if (!$matchFound) { $this->checkForRedirect(true); } // test if there is a suitable page if (!$matchFound) { try { $document = Document::getByPath($path); // check for a parent hardlink with childs if (!$document instanceof Document) { $hardlinkedParentDocument = $this->getNearestDocumentByPath($path, true); if ($hardlinkedParentDocument instanceof Document_Hardlink) { if ($hardLinkedDocument = Document_Hardlink_Service::getChildByPath($hardlinkedParentDocument, $path)) { $document = $hardLinkedDocument; } } } // check for direct hardlink if ($document instanceof Document_Hardlink) { $hardlinkParentDocument = $document; $document = Document_Hardlink_Service::wrap($hardlinkParentDocument); } if ($document instanceof Document) { if (in_array($document->getType(), array("page", "snippet", "email"))) { if (!empty($params["pimcore_version"]) || !empty($params["pimcore_preview"]) || !empty($params["pimcore_admin"]) || !empty($params["pimcore_editmode"]) || $document->isPublished() || !empty($_COOKIE["pimcore_admin_sid"])) { $params["document"] = $document; if ($controller = $document->getController()) { $params["controller"] = $controller; $params["action"] = "index"; } if ($action = $document->getAction()) { $params["action"] = $action; } if ($module = $document->getModule()) { $params["module"] = $module; } // check for a trailing slash in path, if exists, redirect to this page without the slash // the only reason for this is: SEO, Analytics, ... there is no system specific reason, pimcore would work also with a trailing slash without problems // use $originalPath because of the sites if ($config->documents->allowtrailingslash) { if ($config->documents->allowtrailingslash == "no") { if (substr($originalPath, strlen($originalPath) - 1, 1) == "/" && $originalPath != "/") { $redirectUrl = rtrim($originalPath, "/"); if ($_SERVER["QUERY_STRING"]) { $redirectUrl .= "?" . $_SERVER["QUERY_STRING"]; } header("Location: " . $redirectUrl, true, 301); exit; } } } if ($config->documents->allowcapitals) { if ($config->documents->allowcapitals == "no") { if (strtolower($originalPath) != $originalPath) { $redirectUrl = strtolower($originalPath); if ($_SERVER["QUERY_STRING"]) { $redirectUrl .= "?" . $_SERVER["QUERY_STRING"]; } header("Location: " . $redirectUrl, true, 301); exit; } } } $matchFound = true; } } else { if ($document->getType() == "link") { // if the document is a link just redirect to the location/href of the link header("Location: " . $document->getHref(), true, 301); exit; } } } } catch (Exception $e) { // no suitable page found } } // test if there is a suitable static route if (!$matchFound) { try { $cacheKey = "system_route_staticroute"; if (!($routes = Pimcore_Model_Cache::load($cacheKey))) { $list = new Staticroute_List(); $list->setOrderKey("priority"); $list->setOrder("DESC"); $routes = $list->load(); Pimcore_Model_Cache::save($routes, $cacheKey, array("system", "staticroute", "route"), null, 998); } foreach ($routes as $route) { if (@preg_match($route->getPattern(), $originalPath) && !$matchFound) { $params = array_merge($route->getDefaultsArray(), $params); $variables = explode(",", $route->getVariables()); preg_match_all($route->getPattern(), $originalPath, $matches); if (is_array($matches) && count($matches) > 1) { foreach ($matches as $index => $match) { if ($variables[$index - 1]) { $params[$variables[$index - 1]] = $match[0]; } } } $controller = $route->getController(); $action = $route->getAction(); $module = trim($route->getModule()); // check for dynamic controller / action / module $dynamicRouteReplace = function ($item, $params) { if (strpos($item, "%") !== false) { 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; } // try to get nearest document to the route $params["document"] = $this->getNearestDocumentByPath($path); $matchFound = true; Staticroute::setCurrentRoute($route); break; } } } catch (Exception $e) { // no suitable route found } } // test if there is a suitable redirect if (!$matchFound) { $this->checkForRedirect(false); } if (!$matchFound && $site instanceof Site) { if ($config->general->domain) { header("Location: http://" . $_SERVER["HTTP_HOST"], true, 301); } else { $errorMessage = "You have to specify a main domain in system-settings (Settings -> System -> Website -> Domain) if you want to use sites!"; Logger::emerg($errorMessage); die($errorMessage); } exit; } if (!$matchFound) { return false; } // remove pimcore magic parameters unset($params["pimcore_outputfilters_disabled"]); unset($params["pimcore_document"]); unset($params["nocache"]); return $params; }
/** * @param array $urlOptions * @param bool $reset * @param bool $encode * @return mixed|string */ public function assemble(array $urlOptions = [], $reset = false, $encode = true) { // get request parameters $blockedRequestParams = ["controller", "action", "module", "document"]; $front = \Zend_Controller_Front::getInstance(); if ($reset) { $requestParameters = []; } else { $requestParameters = $front->getRequest()->getParams(); // remove blocked parameters from request foreach ($blockedRequestParams as $key) { if (array_key_exists($key, $requestParameters)) { unset($requestParameters[$key]); } } } $defaultValues = $this->getDefaultsArray(); // apply values (controller,action,module, ... ) from previous match if applicable (only when ) if ($reset) { if (self::$_currentRoute && self::$_currentRoute->getName() == $this->getName()) { $defaultValues = array_merge($defaultValues, self::$_currentRoute->_values); } } // merge with defaults $urlParams = array_merge($defaultValues, $requestParameters, $urlOptions); $parametersInReversePattern = []; $parametersGet = []; $url = $this->getReverse(); $forbiddenCharacters = ["#", ":", "?"]; // check for named variables uksort($urlParams, 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); }); $tmpReversePattern = $this->getReverse(); foreach ($urlParams as $key => $param) { if (strpos($tmpReversePattern, "%" . $key) !== false) { $parametersInReversePattern[$key] = $param; // we need to replace the found variable to that it cannot match again a placeholder // eg. %abcd prior %ab if %abcd matches already %ab shouldn't match again on the same placeholder $tmpReversePattern = str_replace("%" . $key, "---", $tmpReversePattern); } else { // only append the get parameters if there are defined in $urlOptions // or if they are defined in $_GET an $reset is false if (array_key_exists($key, $urlOptions) || !$reset && array_key_exists($key, $_GET)) { $parametersGet[$key] = $param; } } } $urlEncodeEscapeCharacters = "~|urlen" . md5(microtime()) . "code|~"; // replace named variables uksort($parametersInReversePattern, 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 ($parametersInReversePattern as $key => $value) { $value = str_replace($forbiddenCharacters, "", $value); if (strlen($value) > 0) { if ($encode) { $value = urlencode_ignore_slash($value); } $value = str_replace("%", $urlEncodeEscapeCharacters, $value); $url = str_replace("%" . $key, $value, $url); } } // remove optional parts $url = preg_replace("/\\{([^\\}]+)?%[^\\}]+\\}/", "", $url); $url = str_replace(["{", "}"], "", $url); // optional get parameters if (!empty($parametersGet)) { if ($encode) { $getParams = array_urlencode($parametersGet); } else { $getParams = array_toquerystring($parametersGet); } $url .= "?" . $getParams; } // convert tmp urlencode escape char back to real escape char $url = str_replace($urlEncodeEscapeCharacters, "%", $url); $results = \Pimcore::getEventManager()->trigger("frontend.path.staticroute", $this, ["frontendPath" => $url, "params" => $urlParams, "reset" => $reset, "encode" => $encode]); if ($results->count()) { $url = $results->last(); } return $url; }
public function removeStaticRoutes() { $conf = new Zend_Config_Xml(PIMCORE_PLUGINS_PATH . '/Blog/install/staticroutes.xml'); foreach ($conf->routes->route as $def) { $route = Staticroute::getByName($def->name); if ($route) { $route->delete(); } } }
/** * @static * @param $route * @return void */ public static function setCurrentRoute($route) { self::$_currentRoute = $route; }