Skip to content
Snippets Groups Projects
Commit 786ab0a9 authored by Tim Steiner's avatar Tim Steiner
Browse files

[gh-438] Replace file_get_contents with a wrapper (unl_url_get_contents) that...

[gh-438] Replace file_get_contents with a wrapper (unl_url_get_contents) that will cache the results based on the requested URL's cache headers.
parent f021bc26
No related branches found
No related tags found
No related merge requests found
......@@ -180,3 +180,80 @@ function unl_user_is_administrator() {
return FALSE;
}
/**
* Fetch the contents at the given URL and cache the result using
* drupal's cache for as long as the response headers allow.
* @param string $url
* @param resource $context
*/
function unl_url_get_contents($url, $context = NULL)
{
// get some per-request static storage
$static = &drupal_static(__FUNCTION__);
if (!isset($static)) {
$static = array();
}
// If cached in the static array, return it.
if (array_key_exists($url, $static)) {
return $static[$url];
}
// If cached in the drupla cache, return it.
$data = cache_get(__FUNCTION__ . $url);
if ($data && time() < $data->data['expires']) {
return $data->data['body'];
}
// Make the request
$http_response_header = array();
$body = file_get_contents($url, NULL, $context);
$headers = array();
foreach ($http_response_header as $rawHeader) {
$headerName = strtolower(trim(substr($rawHeader, 0, strpos($rawHeader, ':'))));
$headerValue = trim(substr($rawHeader, strpos($rawHeader, ':') + 1));
if ($headerName && $headerValue) {
$headers[$headerName] = $headerValue;
}
}
$cacheable = NULL;
$expires = 0;
// Check for a Cache-Control header and the max-age and/or private headers.
if (array_key_exists('cache-control', $headers)) {
$cacheControl = strtolower($headers['cache-control']);
$matches = array();
if (preg_match('/max-age=([0-9]+)/', $cacheControl, $matches)) {
$expires = time() + $matches[1];
$cacheable = TRUE;
}
if (strpos($cacheControl, 'private') !== FALSE) {
$cacheable = FALSE;
}
if (strpos($cacheControl, 'no-cache') !== FALSE) {
$cacheable = FALSE;
}
}
// If there was no Cache-Control header, or if it wasn't helpful, check for an Expires header.
if ($cacheable === NULL && array_key_exists('expires', $headers)) {
$cacheable = TRUE;
$expires = DateTime::createFromFormat(DateTime::RFC1123, $headers['expires'])->getTimestamp();
}
// Save to the drupal cache if caching is ok
if ($cacheable && time() < $expires) {
$data = array(
'body' => $body,
'expires' => $expires,
);
cache_set(__FUNCTION__ . $url, $data, 'cache', $expires);
}
// Otherwise just save to the static per-request cache
else {
$static[$url] = $body;
}
return $body;
}
......@@ -155,7 +155,7 @@ function unl_field_attach_view_alter(&$output, $context) {
$element = isset($output[$field_name]['#field_name']) ? $output[$field_name]['#field_name'] : '';
switch ($element) {
case 'field_hrorgunit':
$result = file_get_contents('http://directory.unl.edu/departments/?view=deptlistings&org_unit='.$element['#items'][0]['value'].'&format=partial');
$result = unl_url_get_contents('http://directory.unl.edu/departments/?view=deptlistings&org_unit='.$element['#items'][0]['value'].'&format=partial');
if (!empty($result) && $result != '<div id="all_employees"></div>') {
drupal_add_css('http://directory.unl.edu/css/peoplefinder_default.css', 'external');
drupal_add_js('http://directory.unl.edu/scripts/peoplefinder.js', 'external');
......@@ -1626,7 +1626,7 @@ function unl_filter_ssi_process($text, $filter, $format, $langcode, $cache, $cac
$url .= '#' . $parts['fragment'];
}
$content = file_get_contents($url);
$content = unl_url_get_contents($url);
$replacements[$full_match] = PHP_EOL
. '<!-- Begin content from ' . $url . ' -->' . PHP_EOL
. $content . PHP_EOL
......
......@@ -160,7 +160,12 @@ function unl_wdn_username_alter(&$name, $account) {
$context = stream_context_create(array(
'http' => array('timeout' => 1)
));
$result = json_decode(@file_get_contents('http://directory.unl.edu/service.php?format=json&uid='.$name, 0, $context));
if (function_exists('unl_url_get_contents')) {
$result = json_decode(@unl_url_get_contents('http://directory.unl.edu/service.php?format=json&uid='.$name, $context));
}
else {
$result = json_decode(@file_get_contents('http://directory.unl.edu/service.php?format=json&uid='.$name, 0, $context));
}
if (!empty($result) && $result->sn) {
$zero = '0';
$firstname = ($result->eduPersonNickname ? $result->eduPersonNickname->$zero : $result->givenName->$zero);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment