/**
  * Handles logic for caching pages.  Currently checks the root block for the
  * "CachePageFlag".  Pages are only cached if the user is not logged in and does
  * not have items in their cart.
  *
  * Observes: controller_front_send_response_before
  *  controller_front_send_response_after
  *
  * @param $observer
  * @return void
  */
 public function cachePage(Varien_Event_Observer $observer)
 {
     /**
      * @var $engine Brim_PageCache_Model_Engine
      */
     /**
      * prevents method from being called twice. Used for Compat with Magento pre 1.4.2,
      * where controller_front_send_response_before is optimal but does not exist.
      */
     if ($this->_cachePageCalled == true) {
         return $this;
     }
     $this->_cachePageCalled = true;
     $engine = Mage::getSingleton('brim_pagecache/engine');
     if (!$engine->isEnabled()) {
         return;
     }
     $engine->cachePage($observer->getFront()->getResponse());
 }
示例#2
0
 /**
  * Move Css (head & inline) to the bottom. ({excluded_css}{stripped_html}{css}</body></html>)
  *
  * Step 1: Return if module is disabled.
  * Step 2: Load needed data.
  * Step 3: Return if no </body> is found in html.
  * Step 4: Search and replace conditional css units. (example: <!--[if lt IE 7]>{multiple css tags}<![endif]-->)
  * Step 5: Search and replace external css tags. (link-tags must xhtml-compliant closed by "/>")
  * Step 6: Search and replace inline css tags.
  * Step 7: Return if no css is found.
  * Step 8: Remove blank lines from html.
  * Step 9: Recalculating </body> position, insert css groups right before body ends and set response.
  *  Final order:
  *      1. excluded css
  *      2. stripped html
  *      3. conditional css tags
  *      4. external css tags
  *      5. inline css tags
  *      6. </body></html>
  *
  * @param Varien_Event_Observer $observer
  */
 public function parseCssToBottom(Varien_Event_Observer $observer)
 {
     //$timeStart = microtime(true);
     // Step 1
     $helper = Mage::helper('pagespeed_css');
     if (!$helper->isEnabled()) {
         return;
     }
     // Step 2
     $response = $observer->getFront()->getResponse();
     $html = $response->getBody();
     $this->excludeList = $helper->getExcludeList();
     // Step 3
     $closedBodyPosition = strripos($html, self::HTML_TAG_BODY);
     if (false === $closedBodyPosition) {
         return;
     }
     // Step 4
     $html = preg_replace_callback('#\\<\\!--\\[if[^\\>]*>\\s*<link[^>]*type\\="text\\/css"[^>]*/>\\s*<\\!\\[endif\\]-->#isU', 'self::processHit', $html);
     // Step 5
     $html = preg_replace_callback('#<link[^>]*type\\=["\']text\\/css["\'][^>]*/>#isU', 'self::processHit', $html);
     // Step 6
     $html = preg_replace_callback('#<style.*</style>#isUm', 'self::processHit', $html);
     // Step 7
     if (!$this->cssTags) {
         return;
     }
     // Step 8
     $html = preg_replace('/^\\h*\\v+/m', '', $html);
     // Step 9
     $closedBodyPosition = strripos($html, self::HTML_TAG_BODY);
     $html = substr_replace($html, $this->cssTags, $closedBodyPosition, 0);
     $response->setBody($html);
     //Mage::log(round(((microtime(true) - $timeStart) * 1000)) . ' ms taken to parse Css to bottom');
 }
 /**
  * Fix form_key in html coming from cache
  * @param Varien_Event_Observer $observer
  */
 public function controllerFrontSendResponseBefore($observer)
 {
     if (Mage::getStoreConfigFlag(self::CONFIG_SECTION . '/general/enable_formkey_fix') && version_compare(Mage::getVersion(), '1.8', '>=')) {
         /** @var Zend_Controller_Response_Http $response */
         /** @noinspection PhpUndefinedMethodInspection */
         $response = $observer->getFront()->getResponse();
         $headers = $response->getHeaders();
         $isHtml = true;
         // Because it's default in PHP
         foreach ($headers as $header) {
             if ('Content-Type' == $header['name'] && false === strpos($header['value'], 'text/html')) {
                 $isHtml = false;
                 break;
             }
         }
         if ($isHtml) {
             $html = $response->getBody();
             /** @noinspection PhpUndefinedMethodInspection */
             $newFormKey = Mage::getSingleton('core/session')->getFormKey();
             $urlParam = '/' . Mage_Core_Model_Url::FORM_KEY . '/';
             $urlParamQ = preg_quote($urlParam, '#');
             // Fix links
             $html = preg_replace('#' . $urlParamQ . '[a-zA-Z0-9]+#', $urlParam . $newFormKey, $html);
             // Fix hidden inputs in forms
             $matches = array();
             if (preg_match_all('#<input\\s[^>]*name=[\'"]{0,1}form_key[\'"]{0,1}[^>]*>#i', $html, $matches, PREG_SET_ORDER)) {
                 foreach ($matches as $matchData) {
                     $oldTag = $matchData[0];
                     $newTag = preg_replace('#value=[\'"]{0,1}[a-zA-Z0-9]+[\'"]{0,1}#i', 'value="' . $newFormKey . '"', $oldTag);
                     if ($oldTag != $newTag) {
                         $html = str_replace($oldTag, $newTag, $html);
                     }
                 }
             }
             $response->setBody($html);
         }
     }
 }