2011-03-22 10:26:53 +01:00
|
|
|
<?php
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
namespace SilverStripe\CMS\Controllers;
|
|
|
|
|
2016-08-10 06:08:39 +02:00
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Control\Director;
|
2016-09-09 01:26:24 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
use SilverStripe\Control\HTTPResponse;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Core\ClassInfo;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
2017-01-26 05:21:00 +01:00
|
|
|
use SilverStripe\Core\Resettable;
|
2016-06-16 06:57:19 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2016-07-22 01:32:32 +02:00
|
|
|
|
2017-01-26 05:21:00 +01:00
|
|
|
class RootURLController extends Controller implements Resettable
|
2017-01-25 21:59:25 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected static $is_at_root = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $default_homepage_link = 'home';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected static $cached_homepage_link;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the full form (e.g. /home/) relative link to the home page for the current HTTP_HOST value. Note that the
|
|
|
|
* link is trimmed of leading and trailing slashes before returning to ensure consistency.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function get_homepage_link()
|
|
|
|
{
|
|
|
|
if (!self::$cached_homepage_link) {
|
2019-03-28 08:40:33 +01:00
|
|
|
$link = Config::inst()->get(__CLASS__, 'default_homepage_link');
|
|
|
|
singleton(__CLASS__)->extend('updateHomepageLink', $link);
|
|
|
|
self::$cached_homepage_link = $link;
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
return self::$cached_homepage_link;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns TRUE if a request to a certain page should be redirected to the site root (i.e. if the page acts as the
|
|
|
|
* home page).
|
|
|
|
*
|
|
|
|
* @param SiteTree $page
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function should_be_on_root(SiteTree $page)
|
|
|
|
{
|
2022-04-13 07:07:59 +02:00
|
|
|
return (!self::$is_at_root && self::get_homepage_link() == trim($page->RelativeLink(true) ?? '', '/'));
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the cached homepage link value - useful for testing.
|
|
|
|
*/
|
|
|
|
public static function reset()
|
|
|
|
{
|
|
|
|
self::$cached_homepage_link = null;
|
|
|
|
}
|
|
|
|
|
2017-06-08 08:02:18 +02:00
|
|
|
protected function beforeHandleRequest(HTTPRequest $request)
|
2017-01-25 21:59:25 +01:00
|
|
|
{
|
2017-06-08 08:02:18 +02:00
|
|
|
parent::beforeHandleRequest($request);
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
self::$is_at_root = true;
|
|
|
|
|
|
|
|
if (!DB::is_active() || !ClassInfo::hasTable('SiteTree')) {
|
|
|
|
$this->getResponse()->redirect(Controller::join_links(
|
|
|
|
Director::absoluteBaseURL(),
|
|
|
|
'dev/build',
|
2020-04-19 06:18:01 +02:00
|
|
|
'?' . http_build_query([
|
2017-01-25 21:59:25 +01:00
|
|
|
'returnURL' => isset($_GET['url']) ? $_GET['url'] : null,
|
2020-04-19 06:18:01 +02:00
|
|
|
])
|
2017-01-25 21:59:25 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 07:21:09 +02:00
|
|
|
public function handleRequest(HTTPRequest $request): HTTPResponse
|
2017-01-25 21:59:25 +01:00
|
|
|
{
|
|
|
|
self::$is_at_root = true;
|
2017-06-08 08:02:18 +02:00
|
|
|
$this->beforeHandleRequest($request);
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
if (!$this->getResponse()->isFinished()) {
|
|
|
|
if (!DB::is_active() || !ClassInfo::hasTable('SiteTree')) {
|
|
|
|
$this->getResponse()->redirect(Director::absoluteBaseURL() . 'dev/build?returnURL=' . (isset($_GET['url']) ? urlencode($_GET['url']) : null));
|
|
|
|
return $this->getResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
$request->setUrl(self::get_homepage_link() . '/');
|
|
|
|
$request->match('$URLSegment//$Action', true);
|
|
|
|
$controller = new ModelAsController();
|
|
|
|
|
2017-06-08 08:02:18 +02:00
|
|
|
$response = $controller->handleRequest($request);
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
$this->prepareResponse($response);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->afterHandleRequest();
|
|
|
|
|
|
|
|
return $this->getResponse();
|
|
|
|
}
|
2011-03-22 10:26:53 +01:00
|
|
|
}
|