public function testIsRelativeUrl() { $siteUrl = Director::absoluteBaseURL(); $this->assertFalse(Director::is_relative_url('http://test.com')); $this->assertFalse(Director::is_relative_url('https://test.com')); $this->assertFalse(Director::is_relative_url(' https://test.com/testpage ')); $this->assertTrue(Director::is_relative_url('test.com/testpage')); $this->assertFalse(Director::is_relative_url('ftp://test.com')); $this->assertTrue(Director::is_relative_url('/relative')); $this->assertTrue(Director::is_relative_url('relative')); $this->assertTrue(Director::is_relative_url('/relative/?url=http://test.com')); $this->assertFalse(Director::is_relative_url('http://test.com/?url=' . $siteUrl)); }
/** * Construct a SS_HTTPRequest from a URL relative to the site root. */ function __construct($httpMethod, $url, $getVars = array(), $postVars = array(), $body = null) { $this->httpMethod = strtoupper(self::detect_method($httpMethod, $postVars)); $this->url = $url; if (Director::is_relative_url($url)) { $this->url = preg_replace(array('/\\/+/', '/^\\//', '/\\/$/'), array('/', '', ''), $this->url); } if (preg_match('/^(.*)\\.([A-Za-z][A-Za-z0-9]*)$/', $this->url, $matches)) { $this->url = $matches[1]; $this->extension = $matches[2]; } if ($this->url) { $this->dirParts = preg_split('|/+|', $this->url); } else { $this->dirParts = array(); } $this->getVars = (array) $getVars; $this->postVars = (array) $postVars; $this->body = $body; }
/** * Preferences * * @return void * * @author Sebastian Diel <*****@*****.**>, * Sascha Koehler <*****@*****.**> * @since 03.03.2015 */ public function preferences() { $numberOfDecimalPlaces = false; if ($this->getProduct()->isInCart()) { $this->preferences['submitButtonTitle'] = _t('SilvercartProduct.CHANGE_QUANTITY_CART'); } else { $this->preferences['submitButtonTitle'] = _t('SilvercartProduct.ADD_TO_CART'); } $this->preferences['doJsValidationScrolling'] = false; $this->formFields['productQuantity']['title'] = _t('SilvercartProduct.QUANTITY'); $backLink = Controller::curr()->getRequest()->getURL(); if (Director::is_relative_url($backLink)) { $backLink = Director::absoluteURL($backLink, true); } $this->setCustomParameter('backLink', $backLink); // Get maxlength for quantity field $quantityFieldMaxLength = strlen((string) SilvercartConfig::addToCartMaxQuantity()); if ($quantityFieldMaxLength == 0) { $quantityFieldMaxLength = 1; } if (array_key_exists('productID', $this->customParameters)) { $silvercartProduct = $this->getProduct(); if ($silvercartProduct instanceof SilvercartProduct) { $numberOfDecimalPlaces = $silvercartProduct->SilvercartQuantityUnit()->numberOfDecimalPlaces; } } if ($numberOfDecimalPlaces !== false && $numberOfDecimalPlaces > 0) { if (array_key_exists('isNumbersOnly', $this->formFields['productQuantity']['checkRequirements'])) { unset($this->formFields['productQuantity']['checkRequirements']['isNumbersOnly']); } $this->formFields['productQuantity']['checkRequirements']['isDecimalNumber'] = $numberOfDecimalPlaces; $this->formFields['productQuantity']['maxLength'] = $quantityFieldMaxLength + 1 + $numberOfDecimalPlaces; } else { $this->formFields['productQuantity']['maxLength'] = $quantityFieldMaxLength; } parent::preferences(); }
/** * Allow the setting of a URL * * This is here so that RootURLController can change the URL of the request * without us loosing all the other info attached (like headers) * * @param string The new URL * * @return SS_HTTPRequest The updated request */ public function setUrl($url) { $this->url = $url; // Normalize URL if its relative (strictly speaking), or has leading slashes if (Director::is_relative_url($url) || preg_match('/^\\//', $url)) { $this->url = preg_replace(array('/\\/+/', '/^\\//', '/\\/$/'), array('/', '', ''), $this->url); } if (preg_match('/^(.*)\\.([A-Za-z][A-Za-z0-9]*)$/', $this->url, $matches)) { $this->url = $matches[1]; $this->extension = $matches[2]; } if ($this->url) { $this->dirParts = preg_split('|/+|', $this->url); } else { $this->dirParts = array(); } return $this; }
/** * Will try to include a GET parameter for an existing URL, * preserving existing parameters and fragments. * If no URL is given, falls back to $_SERVER['REQUEST_URI']. * Uses parse_url() to dissect the URL, and http_build_query() to reconstruct it * with the additional parameter. Converts any '&' (ampersand) * URL parameter separators to the more XHTML compliant '&'. * * CAUTION: If the URL is determined to be relative, * it is prepended with Director::absoluteBaseURL(). * This method will always return an absolute URL because * Director::makeRelative() can lead to inconsistent results. * * @param String $varname * @param String $varvalue * @param String $currentURL Relative or absolute URL (Optional). * @param String $separator Separator for http_build_query(). (Optional). * @return String Absolute URL */ public static function setGetVar($varname, $varvalue, $currentURL = null, $separator = '&') { $uri = $currentURL ? $currentURL : Director::makeRelative($_SERVER['REQUEST_URI']); $isRelative = false; // We need absolute URLs for parse_url() if (Director::is_relative_url($uri)) { $uri = Director::absoluteBaseURL() . $uri; $isRelative = true; } // try to parse uri $parts = parse_url($uri); if (!$parts) { throw new InvalidArgumentException("Can't parse URL: " . $uri); } // Parse params and add new variable $params = array(); if (isset($parts['query'])) { parse_str($parts['query'], $params); } $params[$varname] = $varvalue; // Generate URI segments and formatting $scheme = isset($parts['scheme']) ? $parts['scheme'] : 'http'; $user = isset($parts['user']) && $parts['user'] != '' ? $parts['user'] : ''; if ($user != '') { // format in either user:pass@host.com or user@host.com $user .= isset($parts['pass']) && $parts['pass'] != '' ? ':' . $parts['pass'] . '@' : '@'; } $host = isset($parts['host']) ? $parts['host'] : ''; $port = isset($parts['port']) && $parts['port'] != '' ? ':' . $parts['port'] : ''; $path = isset($parts['path']) && $parts['path'] != '' ? $parts['path'] : ''; // handle URL params which are existing / new $params = $params ? '?' . http_build_query($params, null, $separator) : ''; // keep fragments (anchors) intact. $fragment = isset($parts['fragment']) && $parts['fragment'] != '' ? '#' . $parts['fragment'] : ''; // Recompile URI segments $newUri = $scheme . '://' . $user . $host . $port . $path . $params . $fragment; if ($isRelative) { return Director::makeRelative($newUri); } return $newUri; }
/** * 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 validate($value) { if (!$value) { return true; } $this->params[$this->field->getName()] = $value; $query = http_build_query($this->params); $url = $this->method == 'GET' ? $this->url . '?' . $query : $this->url; // If the url is a relative one, use Director::test() to get the response if (Director::is_relative_url($url)) { $url = Director::makeRelative($url); $postVars = $this->method == 'POST' ? $this->params : null; $response = Director::test($url, $postVars = null, Controller::curr()->getSession(), $this->method); $result = $response->getStatusCode() == 200 ? $response->getBody() : 0; // Otherwise CURL to remote url } else { $ch = curl_init(); if ($this->method == 'POST') { curl_setopt($ch, CURLOPT_POSTFIELDS, $query); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, 'ZENVALIDATOR'); $result = curl_exec($ch); curl_close($ch); } // validate result if ($result == '1' || $result == 'true') { if ($this->validator == 'reverse') { return false; } else { return true; } } $isJson = is_string($result) && (is_object(json_decode($result)) || is_array(json_decode($result))) ? true : false; if ($isJson) { $result = Convert::json2obj($result); if (isset($result->success)) { return true; } else { if (isset($result->message)) { $this->setMessage($result->message); } elseif (isset($result->error)) { $this->setMessage($result->error); } } } return false; }
/** * 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; }
public function Absolute() { $relative = $this->getURL(); return Director::is_site_url($relative) && Director::is_relative_url($relative) ? Controller::join_links(Director::protocolAndHost(), $relative) : $relative; }
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']}"); } } } }
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); }
/** * Searches in the xpath and gets the images from the body. After that, the * method sorts the images after their size and returns the path of the * biggest image in the set. * * @param DOMXPath $xpathObject * @param string $url * @return string the src path of the image */ public static function find_content_image($xpathObject, $url) { $images = []; foreach ($xpathObject->query("(/html/body//img)[position() <= " . self::$count_of_images . "]") as $node) { $src = $node->getAttribute('src'); if (Director::is_relative_url($src)) { $parsedUrl = parse_url($url); $src = Controller::join_links("{$parsedUrl['scheme']}://{$parsedUrl['host']}", $src); } $image = ['src' => $src, 'size' => ['width' => 0, 'height' => 0]]; $image['pixels'] = $image['size']['width'] * $image['size']['height']; $images[] = $image; } usort($images, function ($a, $b) { if ($a['pixels'] != $b['pixels']) { return $a['pixels'] > $b['pixels'] ? -1 : 1; } else { return 0; } }); return isset($images[0]) ? $images[0]['src'] : false; }