From 51914794453fb9de9525bc3bfb7434eed73ca150 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Thu, 27 Aug 2015 10:02:56 +1200 Subject: [PATCH] BUG Fix controller bypassing init --- code/controllers/ShareDraftController.php | 51 +++++++++++++++++------ 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/code/controllers/ShareDraftController.php b/code/controllers/ShareDraftController.php index 47aa9a2..fcf5f7c 100644 --- a/code/controllers/ShareDraftController.php +++ b/code/controllers/ShareDraftController.php @@ -42,25 +42,50 @@ public function preview(SS_HTTPRequest $request) { return $this->errorPage(); } - $page = $shareToken->Page(); + $page = Versioned::get_one_by_stage( + 'SiteTree', + 'Stage', + sprintf('"SiteTree"."ID" = \'%d\'', $shareToken->PageID) + ); - $latest = $page->Versions(null, 'Version DESC')->first(); + $latest = Versioned::get_latest_version('SiteTree', $shareToken->PageID); - $controller = $this->getControllerFor($latest); + $controller = $this->getControllerFor($page); if(!$shareToken->isExpired() && $page->generateKey($shareToken->Token) === $key) { Requirements::css(SHAREDRAFTCONTENT_DIR . '/css/top-bar.css'); - $rendered = $controller->render(); - - $data = new ArrayData(array( - 'Page' => $page, - 'Latest' => $latest, - )); - - $include = (string) $data->renderWith('Includes/TopBar'); - - return str_replace('', $include . '', (string) $rendered); + // Temporarily un-secure the draft site and switch to draft + $oldSecured = Session::get('unsecuredDraftSite'); + $oldMode = Versioned::get_reading_mode(); + $restore = function() use ($oldSecured, $oldMode) { + Session::set('unsecuredDraftSite', $oldSecured); + Versioned::set_reading_mode($oldMode); + }; + + // Process page inside an unsecured draft container + try { + Session::set('unsecuredDraftSite', true); + Versioned::reading_stage('Stage'); + + // Create mock request; Simplify request to single top level reqest + $pageRequest = new SS_HTTPRequest('GET', $page->URLSegment); + $pageRequest->match('$URLSegment//$Action/$ID/$OtherID', true); + $rendered = $controller->handleRequest($pageRequest, $this->model); + + // Render draft heading + $data = new ArrayData(array( + 'Page' => $page, + 'Latest' => $latest, + )); + $include = (string) $data->renderWith('Includes/TopBar'); + } catch(Exception $ex) { + $restore(); + throw $ex; + } + $restore(); + + return str_replace('', $include . '', (string) $rendered->getBody()); } else { return $this->errorPage(); }