/**
  * Output buffering callback
  *
  * @param string $buffer
  * @return string
  */
 function ob_callback($buffer)
 {
     if ($buffer != '') {
         if (w3_is_xml($buffer) && $this->_can_cache2()) {
             $compression = $this->_get_compression();
             $compressions = $this->_get_compressions();
             if ($this->_enhanced_mode) {
                 $is_404 = false;
                 $headers = array();
             } else {
                 $is_404 = function_exists('is_404') ? is_404() : false;
                 $headers = $this->_get_cached_headers();
             }
             $time = time();
             $cache =& $this->_get_cache();
             foreach ($compressions as $_compression) {
                 $_page_key = $this->_get_page_key($_SERVER['REQUEST_URI'], $_compression);
                 /**
                  * Encode content
                  */
                 switch ($_compression) {
                     case false:
                         $_content = $buffer;
                         break;
                     case 'gzip':
                         $_content = gzencode($buffer);
                         break;
                     case 'deflate':
                         $_content = gzdeflate($buffer);
                         break;
                 }
                 /**
                  * Store cache data
                  */
                 if ($this->_enhanced_mode) {
                     $cache->set($_page_key, $_content);
                 } else {
                     $_data = array('404' => $is_404, 'headers' => $headers, 'time' => $time, 'content' => $_content);
                     $cache->set($_page_key, $_data, $this->_lifetime);
                 }
                 if ($compression == $_compression) {
                     $page_key = $_page_key;
                     $buffer = $_content;
                 }
             }
             /**
              * Calculate content etag
              */
             $etag = md5($buffer);
             /**
              * Send headers
              */
             $this->_send_headers($is_404, $time, $etag, $compression, $headers);
             /**
              * Append debug info
              */
             if ($this->_debug) {
                 $time_total = w3_microtime() - $this->_time_start;
                 $debug_info = $this->_get_debug_info($page_key, true, '', false, $time_total);
                 $this->_append_content($buffer, "\r\n\r\n" . $debug_info, $compression);
             }
         } elseif ($this->_debug) {
             /**
              * Append debug info
              */
             $page_key = $this->_get_page_key($_SERVER['REQUEST_URI'], false);
             $time_total = w3_microtime() - $this->_time_start;
             $debug_info = $this->_get_debug_info($page_key, false, $this->cache_reject_reason, false, $time_total);
             $this->_append_content($buffer, "\r\n\r\n" . $debug_info, false);
         }
     }
     return $buffer;
 }
Exemplo n.º 2
0
 /**
  * Get from the cache
  *
  * @param string $id
  * @param string $group
  * @return mixed
  */
 function get($id, $group = 'transient')
 {
     if ($this->_debug) {
         $time_start = w3_microtime();
     }
     $key = $this->_get_cache_key($id, $group);
     list($fragment_group, $fragment_group_expiration) = $this->_fragment_group($id);
     $internal = isset($this->cache[$fragment_group . $group][$key]);
     if ($internal) {
         $value = $this->cache[$fragment_group . $group][$key];
     } elseif ($this->_caching && !in_array($group, $this->nonpersistent_groups)) {
         $cache = $this->_get_cache(null, $group);
         $v = $cache->get($key, $fragment_group);
         if (is_array($v) && $v['content'] != null) {
             $value = $v['content'];
         } else {
             $value = false;
         }
     } else {
         $value = false;
     }
     if ($value === null) {
         $value = false;
     }
     if (is_object($value)) {
         $value = clone $value;
     }
     $this->cache[$fragment_group . $group][$key] = $value;
     $this->cache_total++;
     if ($value !== false) {
         $cached = true;
         $this->cache_hits++;
     } else {
         $cached = false;
         $this->cache_misses++;
     }
     /**
      * Add debug info
      */
     if ($this->_debug) {
         $time = w3_microtime() - $time_start;
         $this->time_total += $time;
         if (!$group) {
             $group = 'transient';
         }
         $this->debug_info[] = array('id' => $id, 'group' => $group, 'cached' => $cached, 'internal' => $internal, 'data_size' => $value ? strlen(serialize($value)) : '', 'time' => $time);
     }
     return $value;
 }
Exemplo n.º 3
0
 /**
  * @param $buffer
  * @param $mobile_group
  * @param $referrer_group
  * @param $encryption
  * @param $compression
  * @param $content_type
  * @return string
  */
 private function _debugging_caching(&$buffer, $mobile_group, $referrer_group, $encryption, $compression = '', $content_type = '')
 {
     /**
      * Set page key for debug
      */
     $this->_page_key = $this->_get_page_key($mobile_group, $referrer_group, $encryption, $compression, $content_type);
     /**
      * Append debug info
      */
     $time_total = w3_microtime() - $this->_time_start;
     $debug_info = $this->_get_debug_info(true, '', false, $time_total);
     $buffer .= "\r\n\r\n" . $debug_info;
     return $buffer;
 }
Exemplo n.º 4
0
 /**
  * Get from the cache
  *
  * @param string $id
  * @param string $group
  * @return mixed
  */
 function get($id, $group = 'default')
 {
     if ($this->_debug) {
         $time_start = w3_microtime();
     }
     $key = $this->_get_cache_key($id, $group);
     $internal = isset($this->cache[$key]);
     if ($internal) {
         $value = $this->cache[$key];
     } elseif ($this->_caching && !in_array($group, $this->nonpersistent_groups)) {
         $cache =& $this->_get_cache();
         $value = $cache->get($key);
     } else {
         $value = false;
     }
     if ($value === null) {
         $value = false;
     }
     if (is_object($value)) {
         $value = nxt_clone($value);
     }
     $this->cache[$key] = $value;
     $this->cache_total++;
     if ($value !== false) {
         $cached = true;
         $this->cache_hits++;
     } else {
         $cached = false;
         $this->cache_misses++;
     }
     /**
      * Add debug info
      */
     if ($this->_debug) {
         $time = w3_microtime() - $time_start;
         $this->time_total += $time;
         if (!$group) {
             $group = 'default';
         }
         $this->debug_info[] = array('id' => $id, 'group' => $group, 'cached' => $cached, 'internal' => $internal, 'data_size' => $value ? strlen(serialize($value)) : 0, 'time' => $time);
     }
     return $value;
 }
Exemplo n.º 5
0
 /**
  * Output buffering callback
  *
  * @param string $buffer
  * @return string
  */
 function ob_callback(&$buffer)
 {
     if ($buffer != '' && w3_is_xml($buffer)) {
         $compression = false;
         $has_dynamic = $this->_has_dynamic($buffer);
         $can_cache = $this->_can_cache2($buffer);
         if ($can_cache) {
             $mobile_group = $this->_get_mobile_group();
             $referrer_group = $this->_get_referrer_group();
             $encryption = $this->_get_encryption();
             $compression = $this->_get_compression();
             /**
              * Don't use compression for debug mode or dynamic tags
              * because we need to modify buffer before send it to client
              */
             $raw = $this->_debug || $has_dynamic;
             if ($raw) {
                 $compressions = array(false);
             } else {
                 $compressions = $this->_get_compressions();
             }
             if ($this->_enhanced_mode) {
                 $is_404 = false;
                 $headers = array();
             } else {
                 $is_404 = function_exists('is_404') ? is_404() : false;
                 $headers = $this->_get_cached_headers();
             }
             $time = time();
             $cache =& $this->_get_cache();
             /**
              * Store different versions of cache
              */
             $buffers = array();
             foreach ($compressions as $_compression) {
                 $_page_key = $this->_get_page_key($this->_request_uri, $mobile_group, $referrer_group, $encryption, $_compression);
                 /**
                  * Compress content
                  */
                 $buffers[$_compression] = $buffer;
                 $this->_compress($buffers[$_compression], $_compression);
                 /**
                  * Store cache data
                  */
                 if ($this->_enhanced_mode) {
                     $cache->set($_page_key, $buffers[$_compression]);
                 } else {
                     $_data = array('404' => $is_404, 'headers' => $headers, 'time' => $time, 'content' => &$buffers[$_compression]);
                     $cache->set($_page_key, $_data, $this->_lifetime);
                 }
             }
             /**
              * Change buffer if using compression
              */
             if ($compression && isset($buffers[$compression])) {
                 $buffer =& $buffers[$compression];
             }
             /**
              * Calculate content etag
              */
             $etag = md5($buffer);
             /**
              * Send headers
              */
             $this->_send_headers($is_404, $time, $etag, $compression, $headers);
             if ($raw) {
                 if ($this->_debug) {
                     /**
                      * Set page key for debug
                      */
                     $this->_page_key = $this->_get_page_key($this->_request_uri, $mobile_group, $referrer_group, $encryption, $compression);
                     /**
                      * Append debug info
                      */
                     $time_total = w3_microtime() - $this->_time_start;
                     $debug_info = $this->_get_debug_info(true, '', false, $time_total);
                     $buffer .= "\r\n\r\n" . $debug_info;
                 }
                 /**
                  * Don't use shutdown function below
                  */
                 if (!$has_dynamic) {
                     $this->_compress($buffer, $compression);
                 }
             }
         } elseif ($this->_debug) {
             $mobile_group = $this->_get_mobile_group();
             $referrer_group = $this->_get_referrer_group();
             $encryption = $this->_get_encryption();
             /**
              * Set page key for debug
              */
             $this->_page_key = $this->_get_page_key($this->_request_uri, $mobile_group, $referrer_group, $encryption, $compression);
             /**
              * Append debug info
              */
             $time_total = w3_microtime() - $this->_time_start;
             $debug_info = $this->_get_debug_info(false, $this->cache_reject_reason, false, $time_total);
             $buffer .= "\r\n\r\n" . $debug_info;
         }
         /**
          * We can't capture output in ob_callback
          * so we use shutdown function
          */
         if ($has_dynamic) {
             $this->_shutdown_buffer = $buffer;
             $this->_shutdown_compression = $compression;
             $buffer = '';
             register_shutdown_function(array(&$this, 'shutdown'));
         }
     }
     return $buffer;
 }
Exemplo n.º 6
0
 /**
  * Output buffering callback
  *
  * @param string $buffer
  * @return string
  */
 function ob_callback(&$buffer)
 {
     if ($buffer != '' && $this->_is_cacheable_content_type()) {
         $compression = false;
         $has_dynamic = $this->_has_dynamic($buffer);
         $can_cache = $this->_can_cache2($buffer);
         if ($can_cache) {
             $mobile_group = $this->_get_mobile_group();
             $referrer_group = $this->_get_referrer_group();
             $encryption = $this->_get_encryption();
             $compression = $this->_get_compression();
             $compressions = $this->_get_compressions();
             /**
              * Don't use compression for debug mode or dynamic tags
              * because we need to modify buffer before send it to client
              */
             $raw = $this->_debug || $has_dynamic;
             if ($raw) {
                 $compression = false;
                 $compressions = array(false);
             }
             $content_type = '';
             $is_404 = function_exists('is_404') ? is_404() : false;
             $headers = $this->_get_cached_headers();
             if ($this->_enhanced_mode && !$this->_late_init) {
                 $this->_check_rules_present();
                 if (isset($headers['Content-Type'])) {
                     $content_type = $headers['Content-Type'];
                 }
             }
             $time = time();
             $cache = $this->_get_cache();
             /**
              * Store different versions of cache
              */
             $buffers = array();
             $group = '0';
             if (!isset($this->_sitemap_matched)) {
                 $sitemap_regex = $this->_config->get_string('pgcache.purge.sitemap_regex');
                 if ($sitemap_regex && preg_match('/' . $sitemap_regex . '/', basename($this->_request_uri))) {
                     $group = 'sitemaps';
                     $this->_sitemap_matched = true;
                 }
             } elseif ($this->_sitemap_matched) {
                 $group = 'sitemaps';
             }
             foreach ($compressions as $_compression) {
                 $_page_key = $this->_get_page_key($mobile_group, $referrer_group, $encryption, $_compression, $content_type);
                 /**
                  * Compress content
                  */
                 $buffers[$_compression] = $buffer;
                 $this->_compress($buffers[$_compression], $_compression);
                 /**
                  * Store cache data
                  */
                 $_data = array('404' => $is_404, 'headers' => $headers, 'time' => $time, 'content' => &$buffers[$_compression]);
                 $cache->set($_page_key, $_data, $this->_lifetime, $group);
             }
             /**
              * Change buffer if using compression
              */
             if ($compression && isset($buffers[$compression])) {
                 $buffer =& $buffers[$compression];
             }
             /**
              * Calculate content etag
              */
             $etag = md5($buffer);
             /**
              * Send headers
              */
             $this->_send_headers($is_404, $time, $etag, $compression, $headers);
             if ($raw) {
                 if ($this->_debug && w3_can_print_comment($buffer)) {
                     /**
                      * Set page key for debug
                      */
                     $this->_page_key = $this->_get_page_key($mobile_group, $referrer_group, $encryption, $compression, $content_type);
                     /**
                      * Append debug info
                      */
                     $time_total = w3_microtime() - $this->_time_start;
                     $debug_info = $this->_get_debug_info(true, '', false, $time_total);
                     $buffer .= "\r\n\r\n" . $debug_info;
                 }
                 /**
                  * Don't use shutdown function below
                  */
                 if (!$has_dynamic) {
                     $this->_compress($buffer, $compression);
                 }
             }
         } elseif ($this->_debug && w3_can_print_comment($buffer)) {
             $mobile_group = $this->_get_mobile_group();
             $referrer_group = $this->_get_referrer_group();
             $encryption = $this->_get_encryption();
             /**
              * Set page key for debug
              */
             $this->_page_key = $this->_get_page_key($mobile_group, $referrer_group, $encryption, $compression);
             /**
              * Append debug info
              */
             $time_total = w3_microtime() - $this->_time_start;
             $debug_info = $this->_get_debug_info(false, $this->cache_reject_reason, false, $time_total);
             $buffer .= "\r\n\r\n" . $debug_info;
         }
         /**
          * We can't capture output in ob_callback
          * so we use shutdown function
          */
         if ($has_dynamic) {
             $this->_shutdown_buffer = $buffer;
             $this->_shutdown_compression = $compression;
             $buffer = '';
             register_shutdown_function(array(&$this, 'shutdown'));
         }
     }
     return $buffer;
 }