Inital commit
This commit is contained in:
@@ -15,9 +15,9 @@ namespace Thelia\Tools;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
use Symfony\Component\Validator\Constraints\UrlValidator;
|
||||
use Thelia\Model\ConfigQuery;
|
||||
use Thelia\Model\LangQuery;
|
||||
use Thelia\Rewriting\RewritingResolver;
|
||||
use Thelia\Rewriting\RewritingRetriever;
|
||||
|
||||
use Thelia\Core\HttpFoundation\Request;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
@@ -47,13 +47,23 @@ class URL
|
||||
// in TheliaHttpKernel, by calling $this->container->get('thelia.url.manager');
|
||||
self::$instance = $this;
|
||||
|
||||
if ($container !== null)
|
||||
if ($container !== null) {
|
||||
$this->requestContext = $container->get('router.admin')->getContext();
|
||||
}
|
||||
|
||||
$this->retriever = new RewritingRetriever();
|
||||
$this->resolver = new RewritingResolver();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RequestContext $requestContext
|
||||
* @since Version 2.2
|
||||
*/
|
||||
public function setRequestContext(RequestContext $requestContext)
|
||||
{
|
||||
$this->requestContext = $requestContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return this class instance, only once instanciated.
|
||||
*
|
||||
@@ -62,7 +72,9 @@ class URL
|
||||
*/
|
||||
public static function getInstance()
|
||||
{
|
||||
if (self::$instance == null) throw new \RuntimeException("URL instance is not initialized.");
|
||||
if (self::$instance == null) {
|
||||
throw new \RuntimeException("URL instance is not initialized.");
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
@@ -77,12 +89,10 @@ class URL
|
||||
public function getBaseUrl($scheme_only = false)
|
||||
{
|
||||
if (null === $this->baseUrlScheme) {
|
||||
|
||||
$scheme = "http";
|
||||
$port = 80;
|
||||
|
||||
if ($host = $this->requestContext->getHost()) {
|
||||
|
||||
$scheme = $this->requestContext->getScheme();
|
||||
|
||||
$port = '';
|
||||
@@ -122,9 +132,8 @@ class URL
|
||||
*/
|
||||
public function absoluteUrl($path, array $parameters = null, $path_only = self::WITH_INDEX_PAGE)
|
||||
{
|
||||
// Already absolute ?
|
||||
// Already absolute ?
|
||||
if (substr($path, 0, 4) != 'http') {
|
||||
|
||||
// Prevent duplication of the subdirectory name when Thelia is installed in a subdirectory.
|
||||
// This happens when $path was calculated with Router::generate(), which returns an absolute URL,
|
||||
// starting at web server root. For example, if Thelia is installed in /thelia2, we got something like /thelia2/my/path
|
||||
@@ -146,29 +155,32 @@ class URL
|
||||
|
||||
// If only a path is requested, be sure to remove the script name (index.php or index_dev.php), if any.
|
||||
if ($path_only == self::PATH_TO_FILE) {
|
||||
if (substr($base_url, -3) == 'php') $base_url = dirname($base_url);
|
||||
if (substr($base_url, -3) == 'php') {
|
||||
$base_url = dirname($base_url);
|
||||
}
|
||||
}
|
||||
|
||||
// Normalize the given path
|
||||
$base = rtrim($base_url, '/') . '/' . ltrim($path, '/');
|
||||
} else
|
||||
} else {
|
||||
$base = $path;
|
||||
}
|
||||
|
||||
$base = str_replace('&', '&', $base);
|
||||
|
||||
$queryString = '';
|
||||
$anchor = '';
|
||||
|
||||
if (! is_null($parameters)) {
|
||||
foreach ($parameters as $name => $value) {
|
||||
|
||||
// Remove this parameter from base URL to prevent duplicate parameters
|
||||
$base = preg_replace('/([?&])'.$name.'=([^&])*(&|$)/', '$1', $base);
|
||||
$base = preg_replace('`([?&])'.preg_quote($name, '`').'=(?:[^&]*)(?:&|$)`', '$1', $base);
|
||||
|
||||
$queryString .= sprintf("%s=%s&", urlencode($name), urlencode($value));
|
||||
}
|
||||
}
|
||||
|
||||
if ('' !== $queryString = rtrim($queryString, "&")) {
|
||||
|
||||
// url could contain anchor
|
||||
$pos = strrpos($base, '#');
|
||||
if ($pos !== false) {
|
||||
@@ -204,115 +216,118 @@ class URL
|
||||
/**
|
||||
* Returns the Absolute URL to a view
|
||||
*
|
||||
* @param string $viewName the view name (e.g. login for login.html)
|
||||
* @param mixed $parameters An array of parameters
|
||||
* @param string $viewName the view name (e.g. login for login.html)
|
||||
* @param mixed $parameters An array of parameters
|
||||
*
|
||||
* @return string The generated URL
|
||||
*/
|
||||
public function viewUrl($viewName, array $parameters = array())
|
||||
{
|
||||
$path = sprintf("?view=%s", $viewName);
|
||||
public function viewUrl($viewName, array $parameters = array())
|
||||
{
|
||||
$path = sprintf("?view=%s", $viewName);
|
||||
|
||||
return $this->absoluteUrl($path, $parameters);
|
||||
}
|
||||
return $this->absoluteUrl($path, $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a rewritten URL from a view, a view id and a locale
|
||||
*
|
||||
* @param $view
|
||||
* @param $viewId
|
||||
* @param $viewLocale
|
||||
*
|
||||
* @return RewritingRetriever You can access $url and $rewrittenUrl properties
|
||||
*/
|
||||
public function retrieve($view, $viewId, $viewLocale)
|
||||
{
|
||||
if (ConfigQuery::isRewritingEnable()) {
|
||||
$this->retriever->loadViewUrl($view, $viewLocale, $viewId);
|
||||
} else {
|
||||
$allParametersWithoutView = array();
|
||||
$allParametersWithoutView['locale'] = $viewLocale;
|
||||
if (null !== $viewId) {
|
||||
$allParametersWithoutView[$view . '_id'] = $viewId;
|
||||
}
|
||||
$this->retriever->rewrittenUrl = null;
|
||||
$this->retriever->url = URL::getInstance()->viewUrl($view, $allParametersWithoutView);
|
||||
}
|
||||
/**
|
||||
* Retrieve a rewritten URL from a view, a view id and a locale
|
||||
*
|
||||
* @param $view
|
||||
* @param $viewId
|
||||
* @param $viewLocale
|
||||
*
|
||||
* @return RewritingRetriever You can access $url and $rewrittenUrl properties
|
||||
*/
|
||||
public function retrieve($view, $viewId, $viewLocale)
|
||||
{
|
||||
if (ConfigQuery::isRewritingEnable()) {
|
||||
$this->retriever->loadViewUrl($view, $viewLocale, $viewId);
|
||||
} else {
|
||||
$allParametersWithoutView = array();
|
||||
$allParametersWithoutView['lang'] = $viewLocale;
|
||||
if (null !== $viewId) {
|
||||
$allParametersWithoutView[$view . '_id'] = $viewId;
|
||||
}
|
||||
$this->retriever->rewrittenUrl = null;
|
||||
$this->retriever->url = URL::getInstance()->viewUrl($view, $allParametersWithoutView);
|
||||
}
|
||||
|
||||
return $this->retriever;
|
||||
}
|
||||
return $this->retriever;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a rewritten URL from the current GET parameters
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return RewritingRetriever You can access $url and $rewrittenUrl properties or use toString method
|
||||
*/
|
||||
public function retrieveCurrent(Request $request)
|
||||
{
|
||||
if (ConfigQuery::isRewritingEnable()) {
|
||||
$view = $request->attributes->get('_view', null);
|
||||
$viewLocale = $request->query->get('locale', null);
|
||||
$viewId = $view === null ? null : $request->query->get($view . '_id', null);
|
||||
/**
|
||||
* Retrieve a rewritten URL from the current GET parameters
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return RewritingRetriever You can access $url and $rewrittenUrl properties or use toString method
|
||||
*/
|
||||
public function retrieveCurrent(Request $request)
|
||||
{
|
||||
if (ConfigQuery::isRewritingEnable()) {
|
||||
$view = $request->attributes->get('_view', null);
|
||||
|
||||
$allOtherParameters = $request->query->all();
|
||||
if ($view !== null) {
|
||||
unset($allOtherParameters['view']);
|
||||
if ($viewId !== null) {
|
||||
unset($allOtherParameters[$view . '_id']);
|
||||
}
|
||||
}
|
||||
if ($viewLocale !== null) {
|
||||
unset($allOtherParameters['locale']);
|
||||
}
|
||||
$viewLocale = $this->getViewLocale($request);
|
||||
|
||||
$this->retriever->loadSpecificUrl($view, $viewLocale, $viewId, $allOtherParameters);
|
||||
} else {
|
||||
$allParametersWithoutView = $request->query->all();
|
||||
$view = $request->attributes->get('_view');
|
||||
if (isset($allOtherParameters['view'])) {
|
||||
unset($allOtherParameters['view']);
|
||||
}
|
||||
$this->retriever->rewrittenUrl = null;
|
||||
$this->retriever->url = URL::getInstance()->viewUrl($view, $allParametersWithoutView);
|
||||
}
|
||||
$viewId = $view === null ? null : $request->query->get($view . '_id', null);
|
||||
|
||||
return $this->retriever;
|
||||
}
|
||||
$allOtherParameters = $request->query->all();
|
||||
if ($view !== null) {
|
||||
unset($allOtherParameters['view']);
|
||||
if ($viewId !== null) {
|
||||
unset($allOtherParameters[$view . '_id']);
|
||||
}
|
||||
}
|
||||
if ($viewLocale !== null) {
|
||||
unset($allOtherParameters['lang']);
|
||||
unset($allOtherParameters['locale']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a rewritten URL from the current GET parameters or use toString method
|
||||
*
|
||||
* @param $url
|
||||
*
|
||||
* @return RewritingResolver
|
||||
*/
|
||||
public function resolve($url)
|
||||
{
|
||||
$this->resolver->load($url);
|
||||
$this->retriever->loadSpecificUrl($view, $viewLocale, $viewId, $allOtherParameters);
|
||||
} else {
|
||||
$allParametersWithoutView = $request->query->all();
|
||||
$view = $request->attributes->get('_view');
|
||||
if (isset($allOtherParameters['view'])) {
|
||||
unset($allOtherParameters['view']);
|
||||
}
|
||||
$this->retriever->rewrittenUrl = null;
|
||||
$this->retriever->url = URL::getInstance()->viewUrl($view, $allParametersWithoutView);
|
||||
}
|
||||
|
||||
return $this->resolver;
|
||||
}
|
||||
return $this->retriever;
|
||||
}
|
||||
|
||||
protected function sanitize($string, $force_lowercase = true, $alphabetic_only = false)
|
||||
{
|
||||
static $strip = array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", "[", "{", "]",
|
||||
/**
|
||||
* Retrieve a rewritten URL from the current GET parameters or use toString method
|
||||
*
|
||||
* @param $url
|
||||
*
|
||||
* @return RewritingResolver
|
||||
*/
|
||||
public function resolve($url)
|
||||
{
|
||||
$this->resolver->load($url);
|
||||
|
||||
return $this->resolver;
|
||||
}
|
||||
|
||||
protected function sanitize($string, $force_lowercase = true, $alphabetic_only = false)
|
||||
{
|
||||
static $strip = array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", "[", "{", "]",
|
||||
"}", "\\", "|", ";", ":", "\"", "'", "‘", "’", "“", "”", "–", "—",
|
||||
"—", "–", ",", "<", ".", ">", "/", "?");
|
||||
|
||||
$clean = trim(str_replace($strip, "", strip_tags($string)));
|
||||
$clean = trim(str_replace($strip, "", strip_tags($string)));
|
||||
|
||||
$clean = preg_replace('/\s+/', "-", $clean);
|
||||
$clean = preg_replace('/\s+/', "-", $clean);
|
||||
|
||||
$clean = ($alphabetic_only) ? preg_replace("/[^a-zA-Z0-9]/", "", $clean) : $clean ;
|
||||
$clean = ($alphabetic_only) ? preg_replace("/[^a-zA-Z0-9]/", "", $clean) : $clean ;
|
||||
|
||||
return ($force_lowercase) ?
|
||||
return ($force_lowercase) ?
|
||||
(function_exists('mb_strtolower')) ?
|
||||
mb_strtolower($clean, 'UTF-8') :
|
||||
strtolower($clean) :
|
||||
$clean;
|
||||
}
|
||||
}
|
||||
|
||||
public static function checkUrl($url, array $protocols = ["http", "https"])
|
||||
{
|
||||
@@ -320,4 +335,21 @@ class URL
|
||||
|
||||
return (bool) preg_match($pattern, $url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the locale code from the lang attribute in URL.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return null|string
|
||||
*/
|
||||
private function getViewLocale(Request $request)
|
||||
{
|
||||
$viewLocale = $request->query->get('lang', null);
|
||||
if (null === $viewLocale) {
|
||||
// fallback for old parameter
|
||||
$viewLocale = $request->query->get('locale', null);
|
||||
}
|
||||
|
||||
return $viewLocale;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user