Beispiel #1
0
 public function testLink()
 {
     $this->assertSame('/test', CM_Util::link('/test'));
     $this->assertSame('/test?a=1&b=%C3%B8', CM_Util::link('/test', ['a' => 1, 'b' => 'ø']));
     $this->assertSame('/test?a=1#anchor', CM_Util::link('/test', ['a' => 1], 'anchor'));
     $this->assertSame('/test#anchor', CM_Util::link('/test', null, 'anchor'));
 }
Beispiel #2
0
 /**
  * @param string|null $email
  * @param int|null    $size
  * @param string|null $default
  * @return string
  */
 public function getUrl($email, $size = null, $default = null)
 {
     if (null !== $email) {
         $email = (string) $email;
     }
     if (null !== $size) {
         $size = (int) $size;
     }
     if (null !== $default) {
         $default = (string) $default;
     }
     if (null === $email && null !== $default) {
         return $default;
     }
     $url = 'https://secure.gravatar.com/avatar';
     if (null !== $email) {
         $url .= '/' . md5(strtolower(trim($email)));
     }
     $params = array();
     if (null !== $size) {
         $params['s'] = $size;
     }
     if (null !== $default) {
         $params['d'] = $default;
     }
     return CM_Util::link($url, $params);
 }
Beispiel #3
0
 public function testLink()
 {
     $this->assertSame('/test', CM_Util::link('/test'));
     $this->assertSame('/test?a=1&b=%C3%B8', CM_Util::link('/test', ['a' => 1, 'b' => 'ø']));
     $this->assertSame('/test?foo%5Bbar%5D=12', CM_Util::link('/test', ['foo' => ['bar' => 12]]));
     $this->assertSame('/test?a=1#anchor', CM_Util::link('/test', ['a' => 1], 'anchor'));
     $this->assertSame('/test#anchor', CM_Util::link('/test', null, 'anchor'));
 }
Beispiel #4
0
 /**
  * @param CM_Params                  $params
  * @param CM_Http_Response_View_Ajax $response
  * @throws CM_Exception_Invalid
  * @return array
  */
 public function loadPage(CM_Params $params, CM_Http_Response_View_Ajax $response)
 {
     $path = $params->getString('path');
     $currentLayoutClass = $params->getString('currentLayout');
     $request = $this->_createGetRequestWithUrl($path);
     $responseFactory = new CM_Http_ResponseFactory($this->getServiceManager());
     $count = 0;
     $fragments = [];
     do {
         $fragment = CM_Util::link($request->getPath(), $request->getQuery());
         $fragments[] = $fragment;
         $url = $this->getRender()->getSite()->getUrlBase() . $fragment;
         if ($count++ > 10) {
             throw new CM_Exception_Invalid('Page redirect loop detected (' . implode(' -> ', $fragments) . ').');
         }
         $responsePage = $responseFactory->getResponse($request);
         if (!$responsePage->getSite()->equals($this->getSite())) {
             $redirectExternalFragment = CM_Util::link($responsePage->getRequest()->getPath(), $responsePage->getRequest()->getQuery());
             return array('redirectExternal' => $responsePage->getRender()->getUrl($redirectExternalFragment));
         }
         $responseEmbed = new CM_Http_Response_Page_Embed($responsePage->getRequest(), $responsePage->getSite(), $this->getServiceManager());
         $responseEmbed->process();
         $request = $responseEmbed->getRequest();
         if ($redirectUrl = $responseEmbed->getRedirectUrl()) {
             if (!$this->_isPageOnSameSite($redirectUrl)) {
                 return array('redirectExternal' => $redirectUrl);
             }
         }
     } while ($redirectUrl);
     foreach ($responseEmbed->getCookies() as $name => $cookieParameters) {
         $response->setCookie($name, $cookieParameters['value'], $cookieParameters['expire'], $cookieParameters['path']);
     }
     $page = $responseEmbed->getPage();
     $this->_setStringRepresentation(get_class($page));
     $frontend = $responseEmbed->getRender()->getGlobalResponse();
     $html = $responseEmbed->getContent();
     $js = $frontend->getJs();
     $autoId = $frontend->getTreeRoot()->getValue()->getAutoId();
     $frontend->clear();
     $layoutRendering = null;
     $layoutClass = $page->getLayout($this->getRender()->getEnvironment());
     if ($layoutClass !== $currentLayoutClass) {
         $layout = new $layoutClass();
         $layoutRendering = $this->_getComponentRendering($layout);
     }
     $title = $responseEmbed->getTitle();
     $menuList = array_merge($this->getSite()->getMenus($this->getRender()->getEnvironment()), $responseEmbed->getRender()->getMenuList());
     $menuEntryHashList = $this->_getMenuEntryHashList($menuList, get_class($page), $page->getParams());
     $jsTracking = $responseEmbed->getRender()->getServiceManager()->getTrackings()->getJs();
     return ['pageRendering' => ['js' => $js, 'html' => $html, 'autoId' => $autoId], 'layoutRendering' => $layoutRendering, 'title' => $title, 'url' => $url, 'menuEntryHashList' => $menuEntryHashList, 'jsTracking' => $jsTracking];
 }
Beispiel #5
0
 /**
  * @param array|null $params
  * @return string
  */
 public static function getPath(array $params = null)
 {
     $pageClassName = get_called_class();
     $list = explode('_', $pageClassName);
     // Remove first parts
     foreach ($list as $index => $entry) {
         unset($list[$index]);
         if ($entry == 'Page') {
             break;
         }
     }
     // Converts upper case letters to dashes: CodeOfHonor => code-of-honor
     foreach ($list as $index => $entry) {
         $list[$index] = CM_Util::uncamelize($entry);
     }
     $path = '/' . implode('/', $list);
     if ($path == '/index') {
         $path = '/';
     }
     return CM_Util::link($path, $params);
 }
Beispiel #6
0
 /**
  * @param CM_Params                  $params
  * @param CM_Http_Response_View_Ajax $response
  * @throws CM_Exception_Invalid
  * @return array
  */
 public function loadPage(CM_Params $params, CM_Http_Response_View_Ajax $response)
 {
     $request = new CM_Http_Request_Get($params->getString('path'), $this->getRequest()->getHeaders(), $this->getRequest()->getServer(), $this->getRequest()->getViewer());
     $count = 0;
     $paths = array($request->getPath());
     do {
         $url = $this->getRender()->getUrl(CM_Util::link($request->getPath(), $request->getQuery()));
         if ($count++ > 10) {
             throw new CM_Exception_Invalid('Page redirect loop detected (' . implode(' -> ', $paths) . ').');
         }
         $responsePage = new CM_Http_Response_Page_Embed($request, $this->getServiceManager());
         $responsePage->process();
         $request = $responsePage->getRequest();
         $paths[] = $request->getPath();
         if ($redirectUrl = $responsePage->getRedirectUrl()) {
             $redirectExternal = 0 !== mb_stripos($redirectUrl, $this->getRender()->getUrl());
             if ($redirectExternal) {
                 return array('redirectExternal' => $redirectUrl);
             }
         }
     } while ($redirectUrl);
     foreach ($responsePage->getCookies() as $name => $cookieParameters) {
         $response->setCookie($name, $cookieParameters['value'], $cookieParameters['expire'], $cookieParameters['path']);
     }
     $page = $responsePage->getPage();
     $this->_setStringRepresentation(get_class($page));
     $frontend = $responsePage->getRender()->getGlobalResponse();
     $html = $responsePage->getContent();
     $js = $frontend->getJs();
     $autoId = $frontend->getTreeRoot()->getValue()->getAutoId();
     $frontend->clear();
     $title = $responsePage->getTitle();
     $layoutClass = get_class($page->getLayout($this->getRender()->getEnvironment()));
     $menuList = array_merge($this->getSite()->getMenus(), $responsePage->getRender()->getMenuList());
     $menuEntryHashList = $this->_getMenuEntryHashList($menuList, get_class($page), $page->getParams());
     $jsTracking = $responsePage->getRender()->getServiceManager()->getTrackings()->getJs();
     return array('autoId' => $autoId, 'html' => $html, 'js' => $js, 'title' => $title, 'url' => $url, 'layoutClass' => $layoutClass, 'menuEntryHashList' => $menuEntryHashList, 'jsTracking' => $jsTracking);
 }
Beispiel #7
0
 /**
  * @param CM_Mail $mail
  * @return string
  * @throws CM_Exception_Invalid
  */
 public function getUrlEmailTracking(CM_Mail $mail)
 {
     if (!$mail->getRecipient()) {
         throw new CM_Exception_Invalid('Needs user');
     }
     $params = array('user' => $mail->getRecipient()->getId(), 'mailType' => $mail->getType());
     return CM_Util::link($this->getSite()->getUrl() . '/emailtracking/' . $this->getSite()->getId(), $params);
 }
Beispiel #8
0
 /**
  * Download MaxMind IP geolocation data
  *
  * @return CM_File
  * @codeCoverageIgnore
  */
 private function _downloadGeoIpFile()
 {
     if (!$this->_geoIpFile) {
         $licenseKey = self::_getConfig()->licenseKey;
         if (null === $licenseKey) {
             $this->_geoIpFile = $this->_getFileTmp('GeoLiteCity.zip');
             $this->_streamOutput->writeln('Downloading GeoLite database…');
             $this->_download($this->_geoIpFile, self::GEO_LITE_CITY_URL);
         } else {
             $parameterList = ['edition_id' => 134, 'suffix' => 'zip', 'license_key' => $licenseKey];
             $geoIpUrl = CM_Util::link(self::GEO_IP_URL, $parameterList);
             $this->_geoIpFile = $this->_getFileTmp('GeoIP-134.zip');
             $this->_streamOutput->writeln('Downloading GeoIP database…');
             $this->_download($this->_geoIpFile, $geoIpUrl);
         }
     }
     return $this->_geoIpFile;
 }
Beispiel #9
0
 /**
  * @param CM_Http_Request_Abstract               $request
  * @param CM_Http_Response_Page_ProcessingResult $result
  * @throws CM_Exception
  * @throws Exception
  * @return boolean
  */
 private function _processPage(CM_Http_Request_Abstract $request, CM_Http_Response_Page_ProcessingResult $result)
 {
     return $this->_runWithCatching(function () use($request, $result) {
         $path = CM_Util::link($request->getPath(), $request->getQuery());
         $result->addPath($path);
         $this->getSite()->rewrite($request);
         $pageParams = CM_Params::factory($request->getQuery(), true);
         try {
             $className = CM_Page_Abstract::getClassnameByPath($this->getRender(), $request->getPath());
             $page = CM_Page_Abstract::factory($className, $pageParams);
         } catch (CM_Exception $ex) {
             throw new CM_Exception_Nonexistent('Cannot load page `' . $request->getPath() . '`: ' . $ex->getMessage());
         }
         $result->addPage($page);
         $environment = $this->getRender()->getEnvironment();
         $page->prepareResponse($environment, $this);
         if ($this->getRedirectUrl()) {
             $request->setUri($this->getRedirectUrl());
             return true;
         }
         if ($page->getCanTrackPageView()) {
             $this->getRender()->getServiceManager()->getTrackings()->trackPageView($environment, $result->getPathTracking());
         }
         $result->setHtml($this->_renderPage($page));
         return true;
     }, function (CM_Exception $ex, array $errorOptions) use($request) {
         $this->getRender()->getGlobalResponse()->clear();
         /** @var CM_Page_Abstract $errorPage */
         $errorPage = $errorOptions['errorPage'];
         $request->setPath($errorPage::getPath());
         $request->setQuery(array());
         return false;
     });
 }
Beispiel #10
0
 protected function _processContentOrRedirect()
 {
     $render = $this->getRender();
     if ($this->_site->getHost() !== $this->_request->getHost()) {
         $path = CM_Util::link($this->_request->getPath(), $this->_request->getQuery());
         $this->redirectUrl($render->getUrl($path, $this->_site));
     }
     if ($this->_request->getLanguageUrl() && $this->getViewer()) {
         $path = CM_Util::link($this->_request->getPath(), $this->_request->getQuery());
         $this->redirectUrl($render->getUrl($path, $this->_site));
         $this->_request->setLanguageUrl(null);
     }
     if (!$this->getRedirectUrl()) {
         $path = CM_Util::link($this->_request->getPath(), $this->_request->getQuery());
         $render->getServiceManager()->getTrackings()->trackPageView($render->getEnvironment(), $path);
         $html = $this->_processPageLoop($this->getRequest());
         $this->_setContent($html);
     }
 }
Beispiel #11
0
 /**
  * @return string
  */
 public function getWebSocketAddressSubscribeOnly()
 {
     return CM_Util::link($this->getWebSocketAddress(), ['subscribeOnly' => 1]);
 }