/**
  * Uses {@link Director::test()} to perform in-memory HTTP requests
  * on the passed-in URLs.
  * 
  * @param  array $urls Relative URLs 
  * @return array Result, keyed by URL. Keys: 
  *               - "statuscode": The HTTP status code
  *               - "redirect": A redirect location (if applicable)
  *               - "path": The filesystem path where the cache has been written
  */
 public function publishPages($urls)
 {
     $result = array();
     //nest the config so we can make changes to the config and revert easily
     Config::nest();
     // Do we need to map these?
     // Detect a numerically indexed arrays
     if (is_numeric(join('', array_keys($urls)))) {
         $urls = $this->urlsToPaths($urls);
     }
     // This can be quite memory hungry and time-consuming
     // @todo - Make a more memory efficient publisher
     increase_time_limit_to();
     increase_memory_limit_to();
     // Set the appropriate theme for this publication batch.
     // This may have been set explicitly via StaticPublisher::static_publisher_theme,
     // or we can use the last non-null theme.
     $customTheme = Config::inst()->get('StaticPublisher', 'static_publisher_theme');
     if ($customTheme) {
         Config::inst()->update('SSViewer', 'theme', $customTheme);
     }
     // Ensure that the theme that is set gets used.
     Config::inst()->update('SSViewer', 'theme_enabled', true);
     $staticBaseUrl = Config::inst()->get('FilesystemPublisher', 'static_base_url');
     if ($staticBaseUrl) {
         Config::inst()->update('Director', 'alternate_base_url', $staticBaseUrl);
     }
     if ($this->fileExtension == 'php') {
         Config::inst()->update('SSViewer', 'rewrite_hash_links', 'php');
     }
     if (Config::inst()->get('StaticPublisher', 'echo_progress')) {
         echo $this->class . ": Publishing to " . $staticBaseUrl . "\n";
     }
     $files = array();
     $i = 0;
     $totalURLs = sizeof($urls);
     foreach ($urls as $url => $path) {
         $origUrl = $url;
         $result[$origUrl] = array('statuscode' => null, 'redirect' => null, 'path' => null);
         $i++;
         if ($url && !is_string($url)) {
             user_error("Bad url:" . var_export($url, true), E_USER_WARNING);
             continue;
         }
         if (Config::inst()->get('StaticPublisher', 'echo_progress')) {
             echo " * Publishing page {$i}/{$totalURLs}: {$url}\n";
             flush();
         }
         Requirements::clear();
         if ($url == "") {
             $url = "/";
         }
         if (Director::is_relative_url($url)) {
             $url = Director::absoluteURL($url);
         }
         $response = Director::test(str_replace('+', ' ', $url));
         if (!$response) {
             continue;
         }
         if ($response) {
             $result[$origUrl]['statuscode'] = $response->getStatusCode();
         }
         Requirements::clear();
         singleton('DataObject')->flushCache();
         // Check for ErrorPages generating output - we want to handle this in a special way below.
         $isErrorPage = false;
         $pageObject = null;
         if ($response && is_object($response) && (int) $response->getStatusCode() >= 400) {
             $pageObject = SiteTree::get_by_link($url);
             if ($pageObject && $pageObject instanceof ErrorPage) {
                 $isErrorPage = true;
             }
         }
         // Skip any responses with a 404 status code unless it's the ErrorPage itself.
         if (!$isErrorPage && is_object($response) && $response->getStatusCode() == '404') {
             continue;
         }
         // Generate file content
         // PHP file caching will generate a simple script from a template
         if ($this->fileExtension == 'php') {
             if (is_object($response)) {
                 if ($response->getStatusCode() == '301' || $response->getStatusCode() == '302') {
                     $content = $this->generatePHPCacheRedirection($response->getHeader('Location'));
                 } else {
                     $content = $this->generatePHPCacheFile($response->getBody(), HTTP::get_cache_age(), date('Y-m-d H:i:s'), $response->getHeader('Content-Type'));
                 }
             } else {
                 $content = $this->generatePHPCacheFile($response . '', HTTP::get_cache_age(), date('Y-m-d H:i:s'), $response->getHeader('Content-Type'));
             }
             // HTML file caching generally just creates a simple file
         } else {
             if (is_object($response)) {
                 if ($response->getStatusCode() == '301' || $response->getStatusCode() == '302') {
                     $absoluteURL = Director::absoluteURL($response->getHeader('Location'));
                     $result[$origUrl]['redirect'] = $response->getHeader('Location');
                     $content = "<meta http-equiv=\"refresh\" content=\"2; URL={$absoluteURL}\">";
                 } else {
                     $content = $response->getBody();
                 }
             } else {
                 $content = $response . '';
             }
         }
         if (Config::inst()->get('StaticPublisher', 'include_caching_metadata')) {
             $content = str_replace('</html>', sprintf("</html>\n\n<!-- %s -->", implode(" ", $this->getMetadata($url))), $content);
         }
         if (!$isErrorPage) {
             $files[$origUrl] = array('Content' => $content, 'Folder' => dirname($path) . '/', 'Filename' => basename($path));
         } else {
             // Generate a static version of the error page with a standardised name, so they can be plugged
             // into catch-all webserver statements such as Apache's ErrorDocument.
             $code = (int) $response->getStatusCode();
             $files[$origUrl] = array('Content' => $content, 'Folder' => dirname($path) . '/', 'Filename' => "error-{$code}.html");
         }
     }
     //return config to its previous state
     Config::unnest();
     $base = BASE_PATH . "/{$this->destFolder}";
     foreach ($files as $origUrl => $file) {
         Filesystem::makeFolder("{$base}/{$file['Folder']}");
         $path = "{$base}/{$file['Folder']}{$file['Filename']}";
         $result[$origUrl]['path'] = $path;
         if (isset($file['Content'])) {
             $fh = fopen($path, "w");
             fwrite($fh, $file['Content']);
             fclose($fh);
         } else {
             if (isset($file['Copy'])) {
                 copy($file['Copy'], $path);
             }
         }
     }
     return $result;
 }
 function publishPages($urls)
 {
     // Do we need to map these?
     // Detect a numerically indexed arrays
     if (is_numeric(join('', array_keys($urls)))) {
         $urls = $this->urlsToPaths($urls);
     }
     // This can be quite memory hungry and time-consuming
     // @todo - Make a more memory efficient publisher
     increase_time_limit_to();
     increase_memory_limit_to();
     // Set the appropriate theme for this publication batch.
     // This may have been set explicitly via StaticPublisher::static_publisher_theme,
     // or we can use the last non-null theme.
     if (!StaticPublisher::static_publisher_theme()) {
         SSViewer::set_theme(SSViewer::current_custom_theme());
     } else {
         SSViewer::set_theme(StaticPublisher::static_publisher_theme());
     }
     $currentBaseURL = Director::baseURL();
     if (self::$static_base_url) {
         Director::setBaseURL(self::$static_base_url);
     }
     if ($this->fileExtension == 'php') {
         SSViewer::setOption('rewriteHashlinks', 'php');
     }
     if (StaticPublisher::echo_progress()) {
         echo $this->class . ": Publishing to " . self::$static_base_url . "\n";
     }
     $files = array();
     $i = 0;
     $totalURLs = sizeof($urls);
     foreach ($urls as $url => $path) {
         if (self::$static_base_url) {
             Director::setBaseURL(self::$static_base_url);
         }
         $i++;
         if ($url && !is_string($url)) {
             user_error("Bad url:" . var_export($url, true), E_USER_WARNING);
             continue;
         }
         if (StaticPublisher::echo_progress()) {
             echo " * Publishing page {$i}/{$totalURLs}: {$url}\n";
             flush();
         }
         Requirements::clear();
         if ($url == "") {
             $url = "/";
         }
         if (Director::is_relative_url($url)) {
             $url = Director::absoluteURL($url);
         }
         $response = Director::test(str_replace('+', ' ', $url));
         Requirements::clear();
         singleton('DataObject')->flushCache();
         //skip any responses with a 404 status code. We don't want to turn those into statically cached pages
         if (!$response || $response->getStatusCode() == '404') {
             continue;
         }
         // Generate file content
         // PHP file caching will generate a simple script from a template
         if ($this->fileExtension == 'php') {
             if (is_object($response)) {
                 if ($response->getStatusCode() == '301' || $response->getStatusCode() == '302') {
                     $content = $this->generatePHPCacheRedirection($response->getHeader('Location'));
                 } else {
                     $content = $this->generatePHPCacheFile($response->getBody(), HTTP::get_cache_age(), date('Y-m-d H:i:s'));
                 }
             } else {
                 $content = $this->generatePHPCacheFile($response . '', HTTP::get_cache_age(), date('Y-m-d H:i:s'));
             }
             // HTML file caching generally just creates a simple file
         } else {
             if (is_object($response)) {
                 if ($response->getStatusCode() == '301' || $response->getStatusCode() == '302') {
                     $absoluteURL = Director::absoluteURL($response->getHeader('Location'));
                     $content = "<meta http-equiv=\"refresh\" content=\"2; URL={$absoluteURL}\">";
                 } else {
                     $content = $response->getBody();
                 }
             } else {
                 $content = $response . '';
             }
         }
         $files[] = array('Content' => $content, 'Folder' => dirname($path) . '/', 'Filename' => basename($path));
         // Add externals
         /*
         			$externals = $this->externalReferencesFor($content);
         			if($externals) foreach($externals as $external) {
         				// Skip absolute URLs
         				if(preg_match('/^[a-zA-Z]+:\/\//', $external)) continue;
         				// Drop querystring parameters
         				$external = strtok($external, '?');
         				
         				if(file_exists("../" . $external)) {
         					// Break into folder and filename
         					if(preg_match('/^(.*\/)([^\/]+)$/', $external, $matches)) {
         						$files[$external] = array(
         							"Copy" => "../$external",
         							"Folder" => $matches[1],
         							"Filename" => $matches[2],
         						);
         					
         					} else {
         						user_error("Can't parse external: $external", E_USER_WARNING);
         					}
         				} else {
         					$missingFiles[$external] = true;
         				}
         			}*/
     }
     if (self::$static_base_url) {
         Director::setBaseURL($currentBaseURL);
     }
     if ($this->fileExtension == 'php') {
         SSViewer::setOption('rewriteHashlinks', true);
     }
     $base = BASE_PATH . "/{$this->destFolder}";
     foreach ($files as $file) {
         Filesystem::makeFolder("{$base}/{$file['Folder']}");
         if (isset($file['Content'])) {
             $fh = fopen("{$base}/{$file['Folder']}{$file['Filename']}", "w");
             fwrite($fh, $file['Content']);
             fclose($fh);
         } else {
             if (isset($file['Copy'])) {
                 copy($file['Copy'], "{$base}/{$file['Folder']}{$file['Filename']}");
             }
         }
     }
 }
 /**
  * Uses {@link Director::test()} to perform in-memory HTTP requests
  * on the passed-in URLs.
  * 
  * @param  array $urls Relative URLs 
  * @return array Result, keyed by URL. Keys: 
  *               - "statuscode": The HTTP status code
  *               - "redirect": A redirect location (if applicable)
  *               - "path": The filesystem path where the cache has been written
  */
 public function publishPages($urls)
 {
     $result = array();
     // Do we need to map these?
     // Detect a numerically indexed arrays
     if (is_numeric(join('', array_keys($urls)))) {
         $urls = $this->urlsToPaths($urls);
     }
     // This can be quite memory hungry and time-consuming
     // @todo - Make a more memory efficient publisher
     increase_time_limit_to();
     increase_memory_limit_to();
     Config::inst()->nest();
     // Set the appropriate theme for this publication batch.
     // This may have been set explicitly via StaticPublisher::static_publisher_theme,
     // or we can use the last non-null theme.
     $customTheme = Config::inst()->get('FilesystemPublisher', 'static_publisher_theme');
     if ($customTheme) {
         Config::inst()->update('SSViewer', 'theme', $customTheme);
     }
     // Ensure that the theme that is set gets used.
     Config::inst()->update('SSViewer', 'theme_enabled', true);
     $currentBaseURL = Director::baseURL();
     $staticBaseUrl = Config::inst()->get('FilesystemPublisher', 'static_base_url');
     if ($this->fileExtension == 'php') {
         Config::inst()->update('SSViewer', 'rewrite_hash_links', 'php');
     }
     if (Config::inst()->get('FilesystemPublisher', 'echo_progress')) {
         echo $this->class . ": Publishing to " . $staticBaseUrl . "\n";
     }
     $files = array();
     $i = 0;
     $totalURLs = sizeof($urls);
     foreach ($urls as $url => $path) {
         $origUrl = $url;
         $result[$origUrl] = array('statuscode' => null, 'redirect' => null, 'path' => null);
         $i++;
         if ($url && !is_string($url)) {
             user_error("Bad url:" . var_export($url, true), E_USER_WARNING);
             continue;
         }
         if (Config::inst()->get('FilesystemPublisher', 'echo_progress')) {
             echo " * Publishing page {$i}/{$totalURLs}: {$url}\n";
             flush();
         }
         Requirements::clear();
         if ($url == "") {
             $url = "/";
         }
         if (Director::is_relative_url($url)) {
             $url = Director::absoluteURL($url);
         }
         $sanitizedURL = URLArrayObject::sanitize_url($url);
         $response = Director::test(str_replace('+', ' ', $sanitizedURL));
         // Prevent empty static cache files from being written
         if (is_object($response) && !$response->getBody()) {
             SS_Log::log(new Exception('Prevented blank static cache page write for: ' . $path), SS_Log::NOTICE);
             continue;
         }
         if (!$response) {
             continue;
         }
         if ($response) {
             $result[$origUrl]['statuscode'] = $response->getStatusCode();
         }
         Requirements::clear();
         singleton('DataObject')->flushCache();
         // Check for ErrorPages generating output - we want to handle this in a special way below.
         $isErrorPage = false;
         $pageObject = null;
         if ($response && is_object($response) && (int) $response->getStatusCode() >= 400) {
             $obj = $this->owner->getUrlArrayObject()->getObject($url);
             if ($obj && $obj instanceof ErrorPage) {
                 $isErrorPage = true;
             }
         }
         // Skip any responses with a 404 status code unless it's the ErrorPage itself.
         if (!$isErrorPage && is_object($response) && $response->getStatusCode() == '404') {
             continue;
         }
         // Generate file content.
         // PHP file caching will generate a simple script from a template
         if ($this->fileExtension == 'php') {
             if (is_object($response)) {
                 if ($response->getStatusCode() == '301' || $response->getStatusCode() == '302') {
                     $content = $this->generatePHPCacheRedirection($response->getHeader('Location'));
                 } else {
                     $content = $this->generatePHPCacheFile($response->getBody(), HTTP::get_cache_age(), date('Y-m-d H:i:s'), $response->getHeader('Content-Type'));
                 }
             } else {
                 $content = $this->generatePHPCacheFile($response . '', HTTP::get_cache_age(), date('Y-m-d H:i:s'), $response->getHeader('Content-Type'));
             }
             // HTML file caching generally just creates a simple file
         } else {
             if (is_object($response)) {
                 if ($response->getStatusCode() == '301' || $response->getStatusCode() == '302') {
                     $absoluteURL = Director::absoluteURL($response->getHeader('Location'));
                     $result[$origUrl]['redirect'] = $response->getHeader('Location');
                     $content = "<meta http-equiv=\"refresh\" content=\"2; URL={$absoluteURL}\">";
                 } else {
                     $content = $response->getBody();
                 }
             } else {
                 $content = $response . '';
             }
         }
         if (Config::inst()->get('FilesystemPublisher', 'include_caching_metadata')) {
             $content = str_replace('</html>', sprintf("</html>\n\n<!-- %s -->", implode(" ", $this->getMetadata($url))), $content);
         }
         if (!$isErrorPage) {
             $files[$origUrl] = array('Content' => $content, 'Folder' => dirname($path) . '/', 'Filename' => basename($path));
         } else {
             // Generate a static version of the error page with a standardised name, so they can be plugged
             // into catch-all webserver statements such as Apache's ErrorDocument.
             $code = (int) $response->getStatusCode();
             $files[$origUrl] = array('Content' => $content, 'Folder' => dirname($path) . '/', 'Filename' => "error-{$code}.html");
         }
         // Add externals
         /*
         			$externals = $this->externalReferencesFor($content);
         			if($externals) foreach($externals as $external) {
         				// Skip absolute URLs
         				if(preg_match('/^[a-zA-Z]+:\/\//', $external)) continue;
         				// Drop querystring parameters
         				$external = strtok($external, '?');
         				
         				if(file_exists("../" . $external)) {
         					// Break into folder and filename
         					if(preg_match('/^(.*\/)([^\/]+)$/', $external, $matches)) {
         						$files[$external] = array(
         							"Copy" => "../$external",
         							"Folder" => $matches[1],
         							"Filename" => $matches[2],
         						);
         					
         					} else {
         						user_error("Can't parse external: $external", E_USER_WARNING);
         					}
         				} else {
         					$missingFiles[$external] = true;
         				}
         			}*/
     }
     if ($this->fileExtension == 'php') {
         Config::inst()->update('SSViewer', 'rewrite_hash_links', true);
     }
     $base = BASE_PATH . "/{$this->destFolder}";
     foreach ($files as $origUrl => $file) {
         Filesystem::makeFolder("{$base}/{$file['Folder']}");
         $path = "{$base}/{$file['Folder']}{$file['Filename']}";
         $result[$origUrl]['path'] = $path;
         if (isset($file['Content'])) {
             $fh = fopen($path, "w");
             fwrite($fh, $file['Content']);
             fclose($fh);
         } else {
             if (isset($file['Copy'])) {
                 copy($file['Copy'], $path);
             }
         }
     }
     Config::inst()->unnest();
     return $result;
 }
 protected function publishUrls($urls, $keyPrefix = '', $domain = null)
 {
     if (defined('PROXY_CONFIG_FILE') && !isset($PROXY_CACHE_HOSTMAP)) {
         include_once BASE_PATH . '/' . PROXY_CONFIG_FILE;
     }
     $config = SiteConfig::current_site_config();
     if ($config->DisableSiteCache) {
         return;
     }
     $urls = array_unique($urls);
     // Do we need to map these?
     // Detect a numerically indexed arrays
     if (is_numeric(join('', array_keys($urls)))) {
         $urls = $this->urlsToPaths($urls);
     }
     // This can be quite memory hungry and time-consuming
     // @todo - Make a more memory efficient publisher
     increase_time_limit_to();
     increase_memory_limit_to();
     $currentBaseURL = Director::baseURL();
     $files = array();
     $i = 0;
     $totalURLs = sizeof($urls);
     $cache = $this->getCache();
     if (!defined('PROXY_CACHE_GENERATING')) {
         define('PROXY_CACHE_GENERATING', true);
     }
     foreach ($urls as $url => $path) {
         // work around bug introduced in ss3 whereby top level /bathroom.html would be changed to ./bathroom.html
         $path = ltrim($path, './');
         $url = rtrim($url, '/');
         // TODO: Detect the scheme + host URL from the URL's absolute path
         // and set that as the base URL appropriately
         $baseUrlSrc = $this->staticBaseUrl ? $this->staticBaseUrl : $url;
         $urlBits = parse_url($baseUrlSrc);
         if (isset($urlBits['scheme']) && isset($urlBits['host'])) {
             // now see if there's a host mapping
             // we want to set the base URL correctly
             Config::inst()->update('Director', 'alternate_base_url', $urlBits['scheme'] . '://' . $urlBits['host'] . '/');
         }
         $i++;
         if ($url && !is_string($url)) {
             user_error("Bad url:" . var_export($url, true), E_USER_WARNING);
             continue;
         }
         Requirements::clear();
         if (strrpos($url, '/home') == strlen($url) - 5) {
             $url = substr($url, 0, strlen($url) - 5);
         }
         if ($url == "" || $url == 'home') {
             $url = "/";
         }
         if (Director::is_relative_url($url)) {
             $url = Director::absoluteURL($url);
         }
         $stage = Versioned::current_stage();
         Versioned::reading_stage('Live');
         $GLOBALS[self::CACHE_PUBLISH] = 1;
         Config::inst()->update('SSViewer', 'theme_enabled', true);
         if (class_exists('Multisites')) {
             Multisites::inst()->resetCurrentSite();
         }
         $response = Director::test(str_replace('+', ' ', $url));
         Config::inst()->update('SSViewer', 'theme_enabled', false);
         unset($GLOBALS[self::CACHE_PUBLISH]);
         Versioned::reading_stage($stage);
         Requirements::clear();
         singleton('DataObject')->flushCache();
         $contentType = null;
         // Generate file content
         if (is_object($response)) {
             if ($response->getStatusCode() == '301' || $response->getStatusCode() == '302') {
                 $absoluteURL = Director::absoluteURL($response->getHeader('Location'));
                 $content = null;
             } else {
                 $content = $response->getBody();
                 $type = $response->getHeader('Content-type');
                 $contentType = $type ? $type : $contentType;
             }
         } else {
             $content = $response . '';
         }
         if (!$content) {
             continue;
         }
         if (isset($urlBits['host'])) {
             $domain = $urlBits['host'];
         }
         if ($domain && !$keyPrefix) {
             $keyPrefix = $domain;
         }
         $path = trim($path, '/');
         if ($path == 'home') {
             $path = '';
         }
         $data = new stdClass();
         $data->Content = $content;
         $data->LastModified = date('Y-m-d H:i:s');
         $cacheAge = SiteConfig::current_site_config()->CacheAge;
         if ($cacheAge) {
             $data->Age = $cacheAge;
         } else {
             $data->Age = HTTP::get_cache_age();
         }
         if (!empty($contentType)) {
             $data->ContentType = $contentType;
         }
         $key = $keyPrefix . '/' . $path;
         $cache->store($key, $data);
         if ($domain && isset($PROXY_CACHE_HOSTMAP) && isset($PROXY_CACHE_HOSTMAP[$domain])) {
             $hosts = $PROXY_CACHE_HOSTMAP[$domain];
             foreach ($hosts as $otherDomain) {
                 $key = $otherDomain . '/' . $path;
                 $storeData = clone $data;
                 $storeData->Content = str_replace($domain, $otherDomain, $storeData->Content);
                 $cache->store($key, $storeData);
             }
         }
     }
     Director::setBaseURL($currentBaseURL);
 }