-
Notifications
You must be signed in to change notification settings - Fork 28
/
ShareDraftController.php
171 lines (147 loc) · 5.33 KB
/
ShareDraftController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
namespace SilverStripe\ShareDraftContent\Controllers;
use BadMethodCallException;
use PageController;
use SilverStripe\CMS\Controllers\ContentController;
use SilverStripe\CMS\Controllers\ModelAsController;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
use SilverStripe\Control\Session;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\ShareDraftContent\Extensions\ShareDraftContentSiteTreeExtension;
use SilverStripe\ShareDraftContent\Models\ShareToken;
use SilverStripe\Versioned\Versioned;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
class ShareDraftController extends Controller
{
/**
* Controller for rendering draft pages.
*
* @config
*
* @var string
*/
private static $controller = PageController::class;
/**
* @var array
*/
private static $allowed_actions = array(
'preview'
);
/**
* @var array
*/
private static $url_handlers = array(
'$Key/$Token' => 'preview'
);
/**
* @param HTTPRequest $request
*
* @return string|DBHTMLText
*/
public function preview(HTTPRequest $request)
{
// Ensure this URL doesn't get picked up by HTTP caches
HTTPCacheControlMiddleware::singleton()->disableCache();
$key = $request->param('Key');
$token = $request->param('Token');
try {
$session = $this->getRequest()->getSession();
} catch (BadMethodCallException $e) {
// Create a new session
$session = $this->getRequest()
->setSession(Injector::inst()->create(Session::class, []))
->getSession();
}
/** @var ShareToken $shareToken */
$shareToken = ShareToken::get()->filter('Token', $token)->first();
if (!$shareToken) {
return $this->errorPage();
}
/** @var SiteTree|ShareDraftContentSiteTreeExtension $page */
$page = Versioned::get_by_stage(SiteTree::class, Versioned::DRAFT)
->byID($shareToken->PageID);
$latest = Versioned::get_latest_version(SiteTree::class, $shareToken->PageID);
$controller = $this->getControllerFor($page);
if (!$shareToken->isExpired() && $page->generateKey($shareToken->Token) === $key) {
Requirements::css('silverstripe/sharedraftcontent: client/dist/styles/bundle-frontend.css');
// Temporarily un-secure the draft site and switch to draft
$oldSecured = $this->getIsDraftSecured($session);
$oldMode = Versioned::get_reading_mode();
// Process page inside an unsecured draft container
try {
$this->setIsDraftSecured($session, false);
Versioned::set_stage('Stage');
// Hack to get around ContentController::init() redirecting on home page
$_FILES = array(array());
// Create mock request; Simplify request to single top level request
$pageRequest = new HTTPRequest('GET', $page->URLSegment);
$pageRequest->match('$URLSegment//$Action/$ID/$OtherID', true);
$pageRequest->setSession($session);
$rendered = $controller->handleRequest($pageRequest);
// Render draft heading
$data = new ArrayData(array(
'Page' => $page,
'Latest' => $latest,
));
$include = (string) $data->renderWith('Includes/TopBar');
} finally {
$this->setIsDraftSecured($session, $oldSecured);
Versioned::set_reading_mode($oldMode);
}
return str_replace('</body>', $include . '</body>', (string) $rendered->getBody());
} else {
return $this->errorPage();
}
}
/**
* @return DBHTMLText
*/
protected function errorPage()
{
Requirements::css('silverstripe/sharedraftcontent: client/dist/styles/bundle-frontend.css');
return $this->renderWith('ShareDraftContentError');
}
/**
* @param SiteTree $page
* @return ContentController
*/
protected function getControllerFor($page)
{
return ModelAsController::controller_for($page);
}
/**
* Check if the draft site is secured
*
* @param Session $session
* @return bool True if the draft site is secured
*/
protected function getIsDraftSecured(Session $session)
{
// Versioned >=1.2
if (method_exists(Versioned::class, 'get_draft_site_secured')) {
return Versioned::get_draft_site_secured();
}
// Fall back to session
return !$session->get('unsecuredDraftSite');
}
/**
* Set draft site security
*
* @param Session $session
* @param bool $secured True if draft site should be secured
*/
protected function setIsDraftSecured(Session $session, $secured)
{
// Versioned >=1.2
if (method_exists(Versioned::class, 'set_draft_site_secured')) {
Versioned::set_draft_site_secured($secured);
}
// Set session variable anyway
$session->set('unsecuredDraftSite', !$secured);
}
}