Example #1
0
 /**
  *
  */
 public function dispatchLoopShutdown()
 {
     if (!\Pimcore\Tool::isHtmlResponse($this->getResponse())) {
         return;
     }
     if ($this->enabled) {
         include_once "simple_html_dom.php";
         $body = $this->getResponse()->getBody();
         $body = \Pimcore\Tool\Less::processHtml($body);
         $this->getResponse()->setBody($body);
     }
 }
Example #2
0
 /**
  * @param $string
  * @param null $document
  * @return mixed
  * @throws \Exception
  */
 public static function embedAndModifyCss($string, $document = null)
 {
     if ($document && $document instanceof Model\Document == false) {
         throw new \Exception('$document has to be an instance of Document');
     }
     //matches all <link> Tags
     preg_match_all("@<link.*?href\\s*=\\s*[\"']([^http].*?)[\"'].*?(/?>|</\\s*link>)@is", $string, $matches);
     if (!empty($matches[0])) {
         $css = "";
         foreach ($matches[0] as $key => $value) {
             $fullMatch = $matches[0][$key];
             $path = $matches[1][$key];
             $fileInfo = self::getNormalizedFileInfo($path, $document);
             if (in_array($fileInfo['fileExtension'], array('css', 'less'))) {
                 if (is_readable($fileInfo['filePathNormalized'])) {
                     if ($fileInfo['fileExtension'] == 'css') {
                         $fileContent = file_get_contents($fileInfo['filePathNormalized']);
                     } else {
                         $fileContent = \Pimcore\Tool\Less::compile($fileInfo['filePathNormalized']);
                         $fileContent = str_replace('/**** compiled with lessphp ****/', '', $fileContent);
                     }
                     if ($fileContent) {
                         $fileContent = self::normalizeCssContent($fileContent, $fileInfo);
                         $css .= "\n\n\n";
                         $css .= $fileContent;
                         // remove <link> tag
                         $string = str_replace($fullMatch, '', $string);
                     }
                 }
             }
         }
         $autoloader = \Zend_Loader_Autoloader::getInstance();
         $autoloader->registerNamespace('TijsVerkoyen');
         $cssToInlineStyles = new CssToInlineStyles();
         $cssToInlineStyles->setHTML($string);
         $cssToInlineStyles->setCSS($css);
         $string = $cssToInlineStyles->convert();
     }
     return $string;
 }
Example #3
0
 /**
  * @param $string
  * @param null $document
  * @return mixed
  * @throws \Exception
  */
 public static function embedAndModifyCss($string, $document = null)
 {
     if ($document && $document instanceof Model\Document == false) {
         throw new \Exception('$document has to be an instance of Document');
     }
     //matches all <link> Tags
     preg_match_all("@<link.*?href\\s*=\\s*[\"'](.*?)[\"'].*?(/?>|</\\s*link>)@is", $string, $matches);
     if (!empty($matches[0])) {
         $css = "";
         foreach ($matches[0] as $key => $value) {
             $fullMatch = $matches[0][$key];
             $path = $matches[1][$key];
             $fileContent = "";
             $fileInfo = [];
             if (stream_is_local($path)) {
                 $fileInfo = self::getNormalizedFileInfo($path, $document);
                 if (in_array($fileInfo['fileExtension'], ['css', 'less'])) {
                     if (is_readable($fileInfo['filePathNormalized'])) {
                         if ($fileInfo['fileExtension'] == 'css') {
                             $fileContent = file_get_contents($fileInfo['filePathNormalized']);
                         } else {
                             $fileContent = \Pimcore\Tool\Less::compile($fileInfo['filePathNormalized']);
                             $fileContent = str_replace('/**** compiled with lessphp ****/', '', $fileContent);
                         }
                     }
                 }
             } elseif (strpos($path, "http") === 0) {
                 $fileContent = \Pimcore\Tool::getHttpData($path);
                 $fileInfo = ["fileUrlNormalized" => $path];
             }
             if ($fileContent) {
                 $fileContent = self::normalizeCssContent($fileContent, $fileInfo);
                 $css .= "\n\n\n";
                 $css .= $fileContent;
                 // remove <link> tag
                 $string = str_replace($fullMatch, '', $string);
             }
         }
         $cssToInlineStyles = new CssToInlineStyles();
         $cssToInlineStyles->setHTML($string);
         $cssToInlineStyles->setCSS($css);
         $string = $cssToInlineStyles->convert();
     }
     return $string;
 }
Example #4
0
 /**
  * static function to render a document outside of a view
  *
  * @static
  * @param Document $document
  * @param array $params
  * @param bool $useLayout
  * @return string
  */
 public static function render(Document $document, $params = array(), $useLayout = false)
 {
     $layout = null;
     $existingActionHelper = null;
     if (\Zend_Controller_Action_HelperBroker::hasHelper("layout")) {
         $existingActionHelper = \Zend_Controller_Action_HelperBroker::getExistingHelper("layout");
     }
     $layoutInCurrentAction = \Zend_Layout::getMvcInstance() instanceof \Zend_Layout ? \Zend_Layout::getMvcInstance()->getLayout() : false;
     $viewHelper = \Zend_Controller_Action_HelperBroker::getExistingHelper("ViewRenderer");
     if ($viewHelper) {
         if ($viewHelper->view === null) {
             $viewHelper->initView(PIMCORE_WEBSITE_PATH . "/views");
         }
         $view = $viewHelper->view;
     } else {
         $view = new \Pimcore\View();
     }
     // add the view script path from the website module to the view, because otherwise it's not possible to call
     // this method out of other modules to render documents, eg. sending e-mails out of an plugin with Pimcore_Mail
     $moduleDirectory = \Zend_Controller_Front::getInstance()->getModuleDirectory($document->getModule());
     if (!empty($moduleDirectory)) {
         $view->addScriptPath($moduleDirectory . "/views/layouts");
         $view->addScriptPath($moduleDirectory . "/views/scripts");
     } else {
         $view->addScriptPath(PIMCORE_FRONTEND_MODULE . "/views/layouts");
         $view->addScriptPath(PIMCORE_FRONTEND_MODULE . "/views/scripts");
     }
     $documentBackup = null;
     if ($view->document) {
         $documentBackup = $view->document;
     }
     $view->document = $document;
     if ($useLayout) {
         if (!($layout = \Zend_Layout::getMvcInstance())) {
             $layout = \Zend_Layout::startMvc();
             $layout->setViewSuffix(View::getViewScriptSuffix());
             if ($layoutHelper = $view->getHelper("layout")) {
                 $layoutHelper->setLayout($layout);
             }
         }
         $layout->setLayout("--modification-indicator--");
     }
     $params["document"] = $document;
     foreach ($params as $key => $value) {
         if (!$view->{$key}) {
             $view->{$key} = $value;
         }
     }
     $content = $view->action($document->getAction(), $document->getController(), $document->getModule(), $params);
     //has to be called after $view->action so we can determine if a layout is enabled in $view->action()
     if ($useLayout) {
         if ($layout instanceof \Zend_Layout) {
             $layout->{$layout->getContentKey()} = $content;
             if (is_array($params)) {
                 foreach ($params as $key => $value) {
                     $layout->getView()->{$key} = $value;
                 }
             }
             // when using Document\Service::render() you have to set a layout in the view ($this->layout()->setLayout("mylayout"))
             if ($layout->getLayout() != "--modification-indicator--") {
                 $content = $layout->render();
             }
             //deactivate the layout if it was not activated in the called action
             //otherwise we would activate the layout in the called action
             \Zend_Layout::resetMvcInstance();
             if (!$layoutInCurrentAction) {
                 $layout->disableLayout();
             } else {
                 $layout = \Zend_Layout::startMvc();
                 $layout->setViewSuffix(View::getViewScriptSuffix());
                 // set pimcore specifiy view suffix
                 $layout->setLayout($layoutInCurrentAction);
                 $view->getHelper("Layout")->setLayout($layout);
                 if ($existingActionHelper) {
                     \Zend_Controller_Action_HelperBroker::removeHelper("layout");
                     \Zend_Controller_Action_HelperBroker::addHelper($existingActionHelper);
                     $pluginClass = $layout->getPluginClass();
                     $front = $existingActionHelper->getFrontController();
                     if ($front->hasPlugin($pluginClass)) {
                         $plugin = $front->getPlugin($pluginClass);
                         $plugin->setLayoutActionHelper($existingActionHelper);
                     }
                 }
             }
             $layout->{$layout->getContentKey()} = null;
             //reset content
         }
     }
     if ($documentBackup) {
         $view->document = $documentBackup;
     }
     if (\Pimcore\Config::getSystemConfig()->outputfilters->less) {
         $content = \Pimcore\Tool\Less::processHtml($content);
     }
     return $content;
 }
Example #5
0
 /**
  *
  */
 protected function frontend()
 {
     $body = $this->getResponse()->getBody();
     $body = \Pimcore\Tool\Less::processHtml($body);
     $this->getResponse()->setBody($body);
 }