Skip to content

Commit

Permalink
Uri::referrer() now accepts third parameter, if set to true, it r…
Browse files Browse the repository at this point in the history
…eturns route without base or language code
  • Loading branch information
mahagr committed Sep 20, 2021
1 parent 31b0510 commit 5593327
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

1. [](#new)
* Composer update
2. [](#improved)
* `Uri::referrer()` now accepts third parameter, if set to `true`, it returns route without base or language code

# v1.7.22
## 09/16/2021
Expand Down
23 changes: 13 additions & 10 deletions system/src/Grav/Common/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,31 +588,34 @@ public function currentPage()
*
* @param string|null $default
* @param string|null $attributes
* @param bool $withoutBaseRoute Set to true if you want not to include `/base/lang` (useful for redirects).
* @return string
*/
public function referrer($default = null, $attributes = null)
public function referrer($default = null, $attributes = null, bool $withoutBaseRoute = false)
{
$referrer = $_SERVER['HTTP_REFERER'] ?? null;

// Check that referrer came from our site.
$root = $this->rootUrl(true);
if ($referrer) {
// Referrer should always have host set and it should come from the same base address.
if (stripos($referrer, $root) !== 0) {
$referrer = null;
}
if ($withoutBaseRoute) {
/** @var Pages $pages */
$pages = Grav::instance()['pages'];
$base = $pages->baseUrl(null, true);
} else {
$base = $this->rootUrl(true);
}

if (!$referrer) {
// Referrer should always have host set and it should come from the same base address.
if (!is_string($referrer) || !str_starts_with($referrer, $base)) {
$referrer = $default ?: $this->route(true, true);
}

// Relative path from grav root.
$referrer = substr($referrer, strlen($base));
if ($attributes) {
$referrer .= $attributes;
}

// Return relative path.
return substr($referrer, strlen($root));
return $referrer;
}

/**
Expand Down

0 comments on commit 5593327

Please sign in to comment.