Commit 03fd6960 authored by Eric Rasmussen's avatar Eric Rasmussen
Browse files

Merge pull request #714 from tsteiner2/issue-697

Closes #697
parents d1c31fff 8a264cd8
......@@ -217,13 +217,21 @@ function unl_url_get_contents($url, $context = NULL, &$headers = array())
// If cached in the static array, return it.
if (array_key_exists($url, $static)) {
$headers = $static[$url]['headers'];
// Don't let this page be cached since it contains uncacheable content.
$GLOBALS['conf']['cache'] = FALSE;
return $static[$url]['body'];
}
// If cached in the drupla cache, return it.
// If cached in the drupal cache, return it.
$data = cache_get(__FUNCTION__ . $url);
if ($data && time() < $data->data['expires']) {
$headers = $data->data['headers'];
// Don't let this page be cached any longer than the retrieved content.
$GLOBALS['conf']['page_cache_maximum_age'] = min(variable_get('page_cache_maximum_age', 0), $data->data['expires'] - time());
return $data->data['body'];
}
......@@ -290,6 +298,9 @@ function unl_url_get_contents($url, $context = NULL, &$headers = array())
'expires' => $expires,
);
cache_set(__FUNCTION__ . $url, $data, 'cache', $expires);
// Don't let this page be cached any longer than the retrieved content.
$GLOBALS['conf']['page_cache_maximum_age'] = min(variable_get('page_cache_maximum_age', 0), $expires - time());
}
// Otherwise just save to the static per-request cache
else {
......@@ -297,6 +308,9 @@ function unl_url_get_contents($url, $context = NULL, &$headers = array())
'body' => $body,
'headers' => $headers,
);
// Don't let this page be cached since it contains uncacheable content.
$GLOBALS['conf']['cache'] = FALSE;
}
return $body;
......
......@@ -1111,12 +1111,16 @@ function unl_filter_ssi_process($text, $filter, $format, $langcode, $cache, $cac
// Break down the URL target then rebuild it as absolute.
$url = substr($match, 1, -1);
$url = html_entity_decode($url);
$parts = parse_url($url);
if (!isset($parts['scheme'])) {
$parts['scheme'] = $_SERVER['HTTPS'] ? 'https' : 'http';
$parts['scheme'] = isset($_SERVER['HTTPS']) ? 'https' : 'http';
}
if (!isset($parts['host'])) {
$parts['host'] = $_SERVER['HTTP_HOST'];
$parts['host'] = $_SERVER['SERVER_NAME'];
}
if (!isset($parts['port']) && !in_array($_SERVER['SERVER_PORT'], array(80, 443))) {
$parts['port'] = $_SERVER['SERVER_PORT'];
}
if (isset($parts['path']) && substr($parts['path'], 0, 1) != '/') {
if (variable_get('unl_use_base_tag')) {
......@@ -1128,8 +1132,10 @@ function unl_filter_ssi_process($text, $filter, $format, $langcode, $cache, $cac
if (!isset($parts['path'])) {
$parts['path'] = '/';
}
$url = $parts['scheme'] . '://' . $parts['host'] . $parts['path'];
$url = $parts['scheme']
. '://' . $parts['host']
. (isset($parts['port']) ? ':' . $parts['port'] : '')
. $parts['path'];
// If this is a request to another UNL site, add format=partial to the query.
if (substr($parts['host'], -7) == 'unl.edu') {
......@@ -1149,31 +1155,19 @@ function unl_filter_ssi_process($text, $filter, $format, $langcode, $cache, $cac
$url .= '#' . $parts['fragment'];
}
$ssiDepth = 0;
if (array_key_exists('HTTP_X_UNL_SSI_DEPTH', $_SERVER)) {
$ssiDepth = $_SERVER['HTTP_X_UNL_SSI_DEPTH'];
}
$ssiDepth++;
$context = stream_context_create(array(
'http' => array(
'header' => "x-unl-ssi-depth: $ssiDepth\r\n",
),
));
if ($ssiDepth > 3) {
watchdog('unl', 'Server Side Include: Recursion depth limit reached.', array(), WATCHDOG_ERROR);
drupal_add_http_header('x-unl-ssi-error', 'Too deep!');
$content = '<!-- Error: Too many recursive includes! Content from ' . $url . ' was not included! -->';
// If the varnish module is enabled, and the SSI is for a URL on our server, do an ESI.
if (module_exists('varnish') &&
isset($_SERVER['HTTP_X_VARNISH']) &&
gethostbyname($parts['host']) == gethostbyname($_SERVER['SERVER_NAME']) &&
$parts['scheme'] == 'http'
) {
$content = _unl_ssi_to_esi($url);
}
// Otherwise, emulate the SSI in drupal.
else {
$headers = array();
$content = unl_url_get_contents($url, $context, $headers);
if (array_key_exists('x-unl-ssi-error', $headers)) {
watchdog('unl', 'Server Side Include: An included URL reached the depth limit.', array(), WATCHDOG_WARNING);
drupal_add_http_header('x-unl-ssi-error', 'The included URL caused recursion that was too deep!');
}
$content = _unl_ssi_emulate($url);
}
$replacements[$full_match] = PHP_EOL
. '<!-- Begin content from ' . $url . ' -->' . PHP_EOL
. $content . PHP_EOL
......@@ -1186,3 +1180,49 @@ function unl_filter_ssi_process($text, $filter, $format, $langcode, $cache, $cac
return $text;
}
/**
* Used by unl_filter_ssi_process() to emulate the SSI process inside drupal.
* @param $url
* @return string
*/
function _unl_ssi_emulate($url) {
$ssiDepth = 0;
if (array_key_exists('HTTP_X_UNL_SSI_DEPTH', $_SERVER)) {
$ssiDepth = $_SERVER['HTTP_X_UNL_SSI_DEPTH'];
}
$ssiDepth++;
$context = stream_context_create(array(
'http' => array(
'header' => "x-unl-ssi-depth: $ssiDepth\r\n",
),
));
if ($ssiDepth > 3) {
watchdog('unl', 'Server Side Include: Recursion depth limit reached.', array(), WATCHDOG_ERROR);
drupal_add_http_header('x-unl-ssi-error', 'Too deep!');
$content = '<!-- Error: Too many recursive includes! Content from ' . $url . ' was not included! -->';
}
else {
$headers = array();
$content = unl_url_get_contents($url, $context, $headers);
if (array_key_exists('x-unl-ssi-error', $headers)) {
watchdog('unl', 'Server Side Include: An included URL reached the depth limit.', array(), WATCHDOG_WARNING);
drupal_add_http_header('x-unl-ssi-error', 'The included URL caused recursion that was too deep!');
}
}
return $content;
}
/**
* Used by unl_filter_ssi_process() to change the SSI into an ESI
* @param $url
* @return string
*/
function _unl_ssi_to_esi($url) {
// Set a header so that Varnish knows to do ESI processing on this response.
drupal_add_http_header('X-ESI', 'yes');
return '<esi:include src="' . check_plain($url) . '"/>';
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment