Пример #1
0
 /**
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function generatePage()
 {
     // update scheduled versions
     PageVersionSchedule::checkPageVersionIds();
     $pageLoader = ['class' => PageLoader::class, 'args' => []];
     $pageBuilder = ['class' => PageBuilderInstance::class, 'args' => []];
     // try to load cms page for current request
     event(new InitializePageBuilder($pageLoader, $pageBuilder));
     PageBuilder::setClass($pageBuilder['class'], $pageBuilder['args'], $pageLoader['class'], $pageLoader['args']);
     try {
         // check for forced redirects
         $redirect = PageRedirect::uriHasRedirect();
         if (!empty($redirect) && $redirect->force == 1) {
             throw new CmsPageException('forced redirect', 0, null, redirect($redirect->to, $redirect->type));
         }
         // check for unforced redirects
         if (PageBuilder::getData('is404') && !empty($redirect)) {
             throw new CmsPageException('redirect', 0, null, redirect($redirect->to, $redirect->type));
         }
         // 404, no cms page for current request
         if (PageBuilder::getData('is404')) {
             throw new Exception('cms page not found', 404);
         }
         // 404, hidden page
         if (!PageBuilder::getData('previewVersion') && !PageBuilder::getData('isLive')) {
             throw new Exception('cms page not live', 404);
         }
         // check for form submissions
         if (!empty($_POST)) {
             $formData = PageBuilder::getData('externalTemplate') ? Request::input(config('coaster::frontend.external_form_input')) : Request::all();
             if (!empty($formData['block_id']) && empty($formData['coaster_check'])) {
                 // honeypot option
                 if (!($block = Block::find($formData['block_id']))) {
                     throw new Exception('no block handler for this form data', 500);
                 } else {
                     $pageId = !empty($formData['page_id']) ? $formData['page_id'] : 0;
                     unset($formData['_token']);
                     unset($formData['block_id']);
                     unset($formData['page_id']);
                     unset($formData['coaster_check']);
                     if ($formSubmitResponse = $block->setPageId($pageId)->setVersionId(PageBuilder::pageLiveVersionId())->getTypeObject()->submission($formData)) {
                         throw new CmsPageException('form submission response', 0, null, $formSubmitResponse);
                     }
                 }
             }
         }
         // load page with template
         $templatePath = PageBuilder::templatePath();
         event(new LoadPageTemplate($templatePath));
         if (View::exists($templatePath)) {
             $this->_setHeader('Content-Type', PageBuilder::getData('contentType'));
             $this->responseContent = View::make($templatePath)->render();
         } else {
             throw new Exception('cms page found with non existent template - ' . $templatePath, 500);
         }
         // if declared as a search page, must have search block
         if (Search::searchBlockRequired() && !Search::searchBlockExists()) {
             throw new Exception('cms page found without search function', 404);
         }
     } catch (CmsPageException $e) {
         $this->responseContent = $e->getAlternateResponse();
     } catch (Exception $e) {
         $this->_setErrorContent($e);
     }
     $response = $this->_createResponse();
     event(new LoadedPageResponse($response));
     // if response content is html, run modifications
     if (!empty($response->headers->get('content-type')) && stripos($response->headers->get('content-type'), 'html') !== false) {
         $domDocument = new DOMDocument();
         $domDocument->loadHTML($response->getContent());
         $domDocument->addMetaTag('generator', 'Coaster CMS ' . config('coaster::site.version'));
         if (config('coaster::frontend.strong_tags') == 1) {
             $keywords = explode(", ", str_replace(" and ", ", ", PageBuilder::block('meta_keywords')));
             $domDocument->addStrongTags($keywords);
         }
         // save page content
         if (PageBuilder::getData('externalTemplate')) {
             $domDocument->appendInputFieldNames(config('coaster::frontend.external_form_input'));
             $response->setContent($this->responseContent = $domDocument->saveBodyHMTL());
         } else {
             $response->setContent($domDocument->saveHTML($domDocument));
         }
     }
     return $response;
 }
Пример #2
0
 /**
  * Load all page levels for current request.
  * Also check if search page or feed page.
  */
 protected function _loadPageLevels()
 {
     $urlSegments = count(Request::segments());
     // check for homepage feed
     if ($urlSegments == 1 && substr(Request::segment(1), 0, 5) == 'root.') {
         if ($this->feedExtension = Feed::getFeedExtensionFromPath(Request::segment(1))) {
             $urlSegments = 0;
         }
     }
     // load homepage
     if (empty($this->pageLevels[0])) {
         $this->pageLevels[0] = self::_loadHomePage();
     }
     // load sub pages
     if ($urlSegments) {
         for ($i = 1; $i <= $urlSegments; $i++) {
             if (empty($this->pageLevels[$i])) {
                 $currentSegment = Request::segment($i);
                 if ($urlSegments == $i && ($this->feedExtension = Feed::getFeedExtensionFromPath($currentSegment))) {
                     Feed::removeFeedExtensionFromPath($currentSegment);
                 }
                 $parentPage = $this->pageLevels[$i - 1];
                 if ($i == 1) {
                     $parentPage = clone $parentPage;
                     $parentPage->id = 0;
                 }
                 $this->pageLevels[$i] = self::_loadSubPage($currentSegment, $parentPage);
                 if (empty($this->pageLevels[$i])) {
                     if (self::_isSearchPage($currentSegment, $parentPage)) {
                         Search::setSearchBlockRequired();
                         $this->searchQuery = implode('/', array_slice(Request::segments(), $i - 1));
                         unset($this->pageLevels[$i]);
                         $urlSegments = $i - 1;
                     }
                     break;
                 }
             }
         }
     }
     if (!empty($this->pageLevels[$urlSegments])) {
         $this->is404 = false;
     }
     if ($this->searchQuery === false && ($query = Request::input('q'))) {
         $this->searchQuery = $query;
     }
     $this->searchQuery = $this->searchQuery !== false ? urldecode($this->searchQuery) : false;
     $this->pageLevels = array_filter($this->pageLevels);
 }
 /**
  * @param array $options
  * @return string
  */
 public function search($options = [])
 {
     Search::searchBlockFound();
     $pages = [];
     if ($this->searchQuery !== false) {
         $pages = PageSearchData::lookup($this->searchQuery);
         if (!empty($pages)) {
             if (!empty($options['templates'])) {
                 foreach ($pages as $k => $page) {
                     if (!isset($page->template) || !in_array($page->template, $options['templates'])) {
                         unset($pages[$k]);
                     }
                 }
             }
             if (!empty($options['groups'])) {
                 $pageIds = [];
                 $pageGroupPages = PageGroupPage::whereIn('group_id', $options['groups'])->get();
                 foreach ($pageGroupPages as $pageGroupPage) {
                     $pageIds[] = $pageGroupPage->page_id;
                 }
                 foreach ($pages as $k => $page) {
                     if (!in_array($page->id, $pageIds)) {
                         unset($pages[$k]);
                     }
                 }
             }
             $results = count($pages);
             $showing = "";
             if ($results > 20) {
                 $page = (int) Request::input('page');
                 $page = $page < 1 ? 1 : $page;
                 $max = $page * 20 > $results ? $results : $page * 20;
                 $showing = " [showing " . (($page - 1) * 20 + 1) . " - " . $max . "]";
             }
             $options['content'] = "Search results for '" . $this->searchQuery . "' (" . $results . " match" . (count($pages) > 1 ? 'es' : null) . " found)" . $showing . ":";
         } else {
             $options['content'] = "No results found for '" . $this->searchQuery . "'.";
         }
     } else {
         $options['content'] = array_key_exists('content', $options) ? $options['content'] : "No search query entered.";
     }
     return $this->_renderCategory(0, $pages, $options);
 }