Пример #1
0
 /**
  * @covers \Bolt\Controller\Zone::get
  * @covers \Bolt\Controller\Zone::isFrontend
  */
 public function testControllerZone()
 {
     $app = $this->getApp();
     $this->setRequest(Request::create('/'));
     $request = $this->getRequest();
     $kernel = $this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
     $app['dispatcher']->dispatch(KernelEvents::REQUEST, new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST));
     $this->assertEquals('frontend', Zone::get($request));
     $this->assertTrue(Zone::isFrontend($request));
 }
Пример #2
0
 /**
  * Check if snippets are allowed for this request.
  *
  * @param FilterResponseEvent $event
  */
 protected function isEnabled(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest()) {
         return false;
     }
     if (Zone::isFrontend($event->getRequest())) {
         return true;
     }
     return $event->getRequest()->attributes->get('allow_snippets', false);
 }
Пример #3
0
 /**
  * Parse textquery into useable arguments.
  *
  * This is tightly coupled to $this->getContent()
  *
  * @see $this->decodeContentQuery()
  *
  * @param $textquery
  * @param array $decoded         a pre-set decoded array to fill
  * @param array $metaParameters  meta parameters
  * @param array $ctypeParameters contenttype parameters
  */
 private function parseTextQuery($textquery, array &$decoded, array &$metaParameters, array &$ctypeParameters)
 {
     // Our default callback
     $decoded['queries_callback'] = [$this, 'executeGetContentQueries'];
     // Some special cases, like 'entry/1' or 'page/about' need to be caught before further processing.
     if (preg_match('#^/?([a-z0-9_-]+)/([0-9]+)$#i', $textquery, $match)) {
         // like 'entry/12' or '/page/12345'
         $decoded['contenttypes'] = $this->decodeContentTypesFromText($match[1]);
         $decoded['return_single'] = true;
         // if allow_numeric_slug option is set on contenttype, interpret number as slug instead of id
         $contenttype = $this->getContentType($decoded['contenttypes'][0]);
         $field = $contenttype['allow_numeric_slugs'] === true ? 'slug' : 'id';
         $ctypeParameters[$field] = $match[2];
     } elseif (preg_match('#^/?([a-z0-9_(\\),-]+)/search(/([0-9]+))?$#i', $textquery, $match)) {
         // like 'page/search or '(entry,page)/search'
         $decoded['contenttypes'] = $this->decodeContentTypesFromText($match[1]);
         $metaParameters['order'] = [$this, 'compareSearchWeights'];
         if (count($match) >= 3) {
             $metaParameters['limit'] = $match[3];
         }
         $decoded['queries_callback'] = [$this, 'executeGetContentSearch'];
     } elseif (preg_match('#^/?([a-z0-9_-]+)/([a-z0-9_-]+)$#i', $textquery, $match)) {
         // like 'page/lorem-ipsum-dolor' or '/page/home'
         $decoded['contenttypes'] = $this->decodeContentTypesFromText($match[1]);
         $decoded['return_single'] = true;
         $ctypeParameters['slug'] = $match[2];
     } elseif (preg_match('#^/?([a-z0-9_-]+)/(latest|first)/([0-9]+)$#i', $textquery, $match)) {
         // like 'page/latest/5'
         $decoded['contenttypes'] = $this->decodeContentTypesFromText($match[1]);
         if (!isset($metaParameters['order']) || $metaParameters['order'] === false) {
             $metaParameters['order'] = 'datepublish ' . ($match[2] == 'latest' ? 'DESC' : 'ASC');
         }
         if (!isset($metaParameters['limit'])) {
             $metaParameters['limit'] = $match[3];
         }
     } elseif (preg_match('#^/?([a-z0-9_-]+)/random/([0-9]+)$#i', $textquery, $match)) {
         // like 'page/random/4'
         $decoded['contenttypes'] = $this->decodeContentTypesFromText($match[1]);
         $dboptions = $this->app['config']->get('general/database');
         $metaParameters['order'] = $dboptions['randomfunction'];
         // 'RAND()' or 'RANDOM()'
         if (!isset($metaParameters['limit'])) {
             $metaParameters['limit'] = $match[2];
         }
     } else {
         $decoded['contenttypes'] = $this->decodeContentTypesFromText($textquery);
         if (isset($ctypeParameters['id']) && is_numeric($ctypeParameters['id'])) {
             $decoded['return_single'] = true;
         }
     }
     // When using from the frontend, we assume (by default) that we only want published items,
     // unless something else is specified explicitly
     $request = $this->app['request_stack']->getCurrentRequest();
     $isFrontend = $request ? Zone::isFrontend($request) : true;
     if ($isFrontend && empty($ctypeParameters['status'])) {
         $ctypeParameters['status'] = 'published';
     }
     if (isset($metaParameters['returnsingle'])) {
         $decoded['return_single'] = $metaParameters['returnsingle'];
         unset($metaParameters['returnsingle']);
     }
 }
Пример #4
0
 /**
  * Insert jQuery, if it's not inserted already.
  *
  * Some of the patterns that 'match' are:
  * - jquery.js
  * - jquery.min.js
  * - jquery-latest.js
  * - jquery-latest.min.js
  * - jquery-1.8.2.min.js
  * - jquery-1.5.js
  *
  * @param Request  $request
  * @param Response $response
  */
 protected function addJquery(Request $request, Response $response)
 {
     if (!$this->config->get('general/add_jquery', false) && !$this->config->get('theme/add_jquery', false)) {
         return;
     }
     if (Zone::isFrontend($request) === false) {
         return;
     }
     $html = $response->getContent();
     $regex = '/<script(.*)jquery(-latest|-[0-9\\.]*)?(\\.min)?\\.js/';
     if (!preg_match($regex, $html)) {
         $jqueryfile = $this->resources->getPath('app/view/js/jquery-2.2.4.min.js');
         $asset = (new Snippet())->setLocation(Target::BEFORE_JS)->setCallback('<script src="' . $jqueryfile . '"></script>');
         $this->injector->inject($asset, $asset->getLocation(), $response);
     }
 }