Skip to content

NOW MANAGED ON CODEBERG

Started refactor of URL system to better extend Laravel

Dan Brown30da10581272
diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php
index 78a8d33c0..21ded2355 100644
--- a/app/Http/Controllers/Auth/LoginController.php
+++ b/app/Http/Controllers/Auth/LoginController.php
@@ -106,9 +106,7 @@ class LoginController extends Controller
$this->ldapService->syncGroups($user, $request->get($this->username()));
}
- $path = session()->pull('url.intended', '/');
- $path = baseUrl($path, true);
- return redirect($path);
+ return redirect()->intended('/');
}
/**
diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php
index e45a0be92..6bc1f9047 100644
--- a/app/Providers/AppServiceProvider.php
+++ b/app/Providers/AppServiceProvider.php
@@ -8,6 +8,7 @@ use BookStack\Entities\Chapter;
use BookStack\Entities\Page;
use BookStack\Settings\Setting;
use BookStack\Settings\SettingService;
+use BookStack\UrlGenerator;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
@@ -72,5 +73,10 @@ class AppServiceProvider extends ServiceProvider
$this->app->singleton(SettingService::class, function ($app) {
return new SettingService($app->make(Setting::class), $app->make('Illuminate\Contracts\Cache\Repository'));
});
+
+ $this->app->bind(
+ \Illuminate\Contracts\Routing\UrlGenerator::class,
+ UrlGenerator::class
+ );
}
}
diff --git a/app/UrlGenerator.php b/app/UrlGenerator.php
new file mode 100644
index 000000000..471792cdf
--- /dev/null
+++ b/app/UrlGenerator.php
@@ -0,0 +1,58 @@
+<?php
+
+namespace BookStack;
+
+class UrlGenerator extends \Illuminate\Routing\UrlGenerator
+{
+
+ /**
+ * Generate an absolute URL to the given path.
+ *
+ * @param string $path
+ * @param mixed $extra
+ * @param bool|null $secure
+ * @return string
+ */
+ public function to($path, $extra = [], $secure = null)
+ {
+ $tail = implode('/', array_map(
+ 'rawurlencode', (array) $this->formatParameters($extra))
+ );
+
+ $defaultRoot = $this->formatRoot($this->formatScheme($secure));
+
+ list($path, $query) = $this->extractQueryString($path);
+
+ return $this->formatWithBase(
+ $defaultRoot, trim($path.'/'.$tail, '/')
+ ).$query;
+ }
+
+ /**
+ * Format the given URL segments into a single URL.
+ *
+ * @param string $defaultRoot
+ * @param string $path
+ * @return string
+ */
+ public function formatWithBase($defaultRoot, $path)
+ {
+ $isFullPath = strpos($path, 'http') === 0;
+ $setBasePath = trim(config('app.url'), '/');
+
+ if ($isFullPath) {
+ return $path;
+ }
+
+ if (! empty($setBasePath)) {
+ $defaultRoot = $setBasePath;
+ }
+
+ // TODO - Add mechanism to set path correctly for intended() and back() redirects
+ // TODO - Update tests to align with new system
+ // TODO - Clean up helpers and refactor their usage.
+
+ return trim($defaultRoot. '/' .$path, '/');
+ }
+
+}
\ No newline at end of file
diff --git a/app/helpers.php b/app/helpers.php
index 8cb3fa3f4..7bc116555 100644
--- a/app/helpers.php
+++ b/app/helpers.php
@@ -112,6 +112,7 @@ function setting($key = null, $default = false)
*/
function baseUrl($path, $forceAppDomain = false)
{
+ return url($path);
$isFullUrl = strpos($path, 'http') === 0;
if ($isFullUrl && !$forceAppDomain) {
return $path;