1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Subsites\Middleware; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Admin\AdminRootController; |
6
|
|
|
use SilverStripe\Control\HTTPRequest; |
7
|
|
|
use SilverStripe\Control\Middleware\HTTPMiddleware; |
8
|
|
|
use SilverStripe\Core\Config\Configurable; |
9
|
|
|
use SilverStripe\Core\Injector\Injector; |
10
|
|
|
use SilverStripe\ORM\Connect\DatabaseException; |
11
|
|
|
use SilverStripe\Subsites\Model\Subsite; |
12
|
|
|
use SilverStripe\Subsites\State\SubsiteState; |
13
|
|
|
|
14
|
|
|
class InitStateMiddleware implements HTTPMiddleware |
15
|
|
|
{ |
16
|
|
|
use Configurable; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* URL paths that should be considered as admin only, i.e. not frontend |
20
|
|
|
* |
21
|
|
|
* @config |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private static $admin_url_paths = [ |
|
|
|
|
25
|
|
|
'dev/', |
26
|
|
|
'graphql/', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
public function process(HTTPRequest $request, callable $delegate) |
30
|
|
|
{ |
31
|
|
|
try { |
32
|
|
|
// Initialise and register the State |
33
|
|
|
$state = SubsiteState::create(); |
34
|
|
|
Injector::inst()->registerService($state); |
35
|
|
|
|
36
|
|
|
// Detect whether the request was made in the CMS area (or other admin-only areas) |
37
|
|
|
$isAdmin = $this->getIsAdmin($request); |
38
|
|
|
$state->setUseSessions($isAdmin); |
39
|
|
|
|
40
|
|
|
// Detect the subsite ID |
41
|
|
|
$subsiteId = $this->detectSubsiteId($request); |
42
|
|
|
$state->setSubsiteId($subsiteId); |
43
|
|
|
|
44
|
|
|
return $delegate($request); |
45
|
|
|
} catch (DatabaseException $ex) { |
46
|
|
|
// No-op, database is not ready |
47
|
|
|
} finally { |
48
|
|
|
// Persist to the session if using the CMS |
49
|
|
|
if ($state->getUseSessions()) { |
50
|
|
|
$request->getSession()->set('SubsiteID', $state->getSubsiteId()); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Determine whether the website is being viewed from an admin protected area or not |
57
|
|
|
* |
58
|
|
|
* @param HTTPRequest $request |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function getIsAdmin(HTTPRequest $request) |
62
|
|
|
{ |
63
|
|
|
$adminPaths = static::config()->get('admin_url_paths'); |
64
|
|
|
$adminPaths[] = AdminRootController::config()->get('url_base') . '/'; |
65
|
|
|
$currentPath = rtrim($request->getURL(), '/') . '/'; |
66
|
|
|
foreach ($adminPaths as $adminPath) { |
67
|
|
|
if (substr($currentPath, 0, strlen($adminPath)) === $adminPath) { |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Use the given request to detect the current subsite ID |
76
|
|
|
* |
77
|
|
|
* @param HTTPRequest $request |
78
|
|
|
* @return int |
79
|
|
|
*/ |
80
|
|
|
protected function detectSubsiteId(HTTPRequest $request) |
81
|
|
|
{ |
82
|
|
|
if ($request->getVar('SubsiteID') !== null) { |
83
|
|
|
return (int) $request->getVar('SubsiteID'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (SubsiteState::singleton()->getUseSessions() && $request->getSession()->get('SubsiteID') !== null) { |
87
|
|
|
return (int) $request->getSession()->get('SubsiteID'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$subsiteIdFromDomain = Subsite::getSubsiteIDForDomain(); |
91
|
|
|
if ($subsiteIdFromDomain !== null) { |
92
|
|
|
return (int) $subsiteIdFromDomain; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// Default fallback |
96
|
|
|
return 0; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.