Esempio n. 1
0
 /**
  * Handle a request for a cached view
  *
  * @param array $path URL path
  * @return void
  */
 public function handleRequest($path)
 {
     $config = $this->config;
     $request = $this->parsePath($path);
     if (!$request) {
         $this->send403();
     }
     $ts = $request['ts'];
     $view = $request['view'];
     $viewtype = $request['viewtype'];
     $contentType = $this->getContentType($view);
     if (!empty($contentType)) {
         header("Content-Type: {$contentType}", true);
     }
     // this may/may not have to connect to the DB
     $this->setupSimplecache();
     // we can't use $config->get yet. It fails before the core is booted
     if (!$config->getVolatile('simplecache_enabled')) {
         $this->application->bootCore();
         if (!\_elgg_is_view_cacheable($view)) {
             $this->send403();
         } else {
             echo $this->renderView($view, $viewtype);
         }
         exit;
     }
     $etag = "\"{$ts}\"";
     // If is the same ETag, content didn't change.
     if (isset($this->server_vars['HTTP_IF_NONE_MATCH']) && trim($this->server_vars['HTTP_IF_NONE_MATCH']) === $etag) {
         header("HTTP/1.1 304 Not Modified");
         exit;
     }
     $filename = $config->getVolatile('dataroot') . 'views_simplecache/' . md5("{$viewtype}|{$view}");
     if (file_exists($filename)) {
         $this->sendCacheHeaders($etag);
         readfile($filename);
         exit;
     }
     $this->application->bootCore();
     elgg_set_viewtype($viewtype);
     if (!\_elgg_is_view_cacheable($view)) {
         $this->send403();
     }
     $cache_timestamp = (int) $config->get('lastcache');
     if ($cache_timestamp == $ts) {
         $this->sendCacheHeaders($etag);
         $content = $this->getProcessedView($view, $viewtype);
         $dir_name = $config->getDataPath() . 'views_simplecache/';
         if (!is_dir($dir_name)) {
             mkdir($dir_name, 0700);
         }
         file_put_contents($filename, $content);
     } else {
         // if wrong timestamp, don't send HTTP cache
         $content = $this->renderView($view, $viewtype);
     }
     echo $content;
     exit;
 }
Esempio n. 2
0
 /**
  * Handle a request for a cached view
  *
  * @param array $get_vars    $_GET variables
  * @param array $server_vars $_SERVER variables
  * @return void
  */
 public function handleRequest($get_vars, $server_vars)
 {
     if (empty($get_vars['request'])) {
         $this->send403();
     }
     $request = $this->parseRequestVar($get_vars['request']);
     if (!$request) {
         $this->send403();
     }
     $ts = $request['ts'];
     $view = $request['view'];
     $viewtype = $request['viewtype'];
     $this->sendContentType($view);
     // this may/may not have to connect to the DB
     $this->setupSimplecache();
     if (!$this->config->simplecache_enabled) {
         $this->loadEngine();
         if (!_elgg_is_view_cacheable($view)) {
             $this->send403();
         } else {
             echo $this->renderView($view, $viewtype);
         }
         exit;
     }
     $etag = "\"{$ts}\"";
     // If is the same ETag, content didn't change.
     if (isset($server_vars['HTTP_IF_NONE_MATCH']) && trim($server_vars['HTTP_IF_NONE_MATCH']) === $etag) {
         header("HTTP/1.1 304 Not Modified");
         exit;
     }
     $filename = $this->config->dataroot . 'views_simplecache/' . md5("{$viewtype}|{$view}");
     if (file_exists($filename)) {
         $this->sendCacheHeaders($etag);
         readfile($filename);
         exit;
     }
     $this->loadEngine();
     elgg_set_viewtype($viewtype);
     if (!_elgg_is_view_cacheable($view)) {
         $this->send403();
     }
     $cache_timestamp = (int) _elgg_services()->config->get('lastcache');
     if ($cache_timestamp == $ts) {
         $this->sendCacheHeaders($etag);
         $content = $this->getProcessedView($view, $viewtype);
         $dir_name = $this->config->dataroot . 'views_simplecache/';
         if (!is_dir($dir_name)) {
             mkdir($dir_name, 0700);
         }
         file_put_contents($filename, $content);
     } else {
         // if wrong timestamp, don't send HTTP cache
         $content = $this->renderView($view, $viewtype);
     }
     echo $content;
     exit;
 }
Esempio n. 3
0
 /**
  * Handle a request for a cached view
  *
  * @param array $path URL path
  * @return void
  */
 public function handleRequest($path)
 {
     $config = $this->config;
     $request = $this->parsePath($path);
     if (!$request) {
         $this->send403();
     }
     $ts = $request['ts'];
     $view = $request['view'];
     $viewtype = $request['viewtype'];
     $contentType = $this->getContentType($view);
     if (empty($contentType)) {
         $this->send403("Asset must have a valid file extension");
     }
     header("Content-Type: {$contentType}", true);
     // this may/may not have to connect to the DB
     $this->setupSimplecache();
     // we can't use $config->get yet. It fails before the core is booted
     if (!$config->getVolatile('simplecache_enabled')) {
         $this->application->bootCore();
         if (!\_elgg_is_view_cacheable($view)) {
             $this->send403("Requested view is not an asset");
         } else {
             $content = $this->renderView($view, $viewtype);
             $etag = '"' . md5($content) . '"';
             $this->sendRevalidateHeaders($etag);
             $this->handle304($etag);
             echo $content;
         }
         exit;
     }
     $etag = "\"{$ts}\"";
     $this->handle304($etag);
     // trust the client but check for an existing cache file
     $filename = $config->getVolatile('dataroot') . "views_simplecache/{$ts}/{$viewtype}/{$view}";
     if (file_exists($filename)) {
         $this->sendCacheHeaders($etag);
         readfile($filename);
         exit;
     }
     // the hard way
     $this->application->bootCore();
     elgg_set_viewtype($viewtype);
     if (!\_elgg_is_view_cacheable($view)) {
         $this->send403("Requested view is not an asset");
     }
     $lastcache = (int) $config->get('lastcache');
     $filename = $config->getVolatile('dataroot') . "views_simplecache/{$lastcache}/{$viewtype}/{$view}";
     if ($lastcache == $ts) {
         $this->sendCacheHeaders($etag);
         $content = $this->getProcessedView($view, $viewtype);
         $dir_name = dirname($filename);
         if (!is_dir($dir_name)) {
             mkdir($dir_name, 0700, true);
         }
         file_put_contents($filename, $content);
     } else {
         // if wrong timestamp, don't send HTTP cache
         $content = $this->renderView($view, $viewtype);
     }
     echo $content;
     exit;
 }