diff --git a/sites/all/modules/unl/includes/common.php b/sites/all/modules/unl/includes/common.php index cb1a01ca5c1def37ed8ef38801c86f6ce12ca2bb..7928609fe01f19c87b8fc0499b5f511ed8bfea39 100644 --- a/sites/all/modules/unl/includes/common.php +++ b/sites/all/modules/unl/includes/common.php @@ -180,3 +180,93 @@ 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) +{ + unl_load_zend_framework(); + if (!Zend_Uri::check($url)) { + drupal_set_message('A non-url was passed to ' . __FUNCTION__ . '().', 'warning'); + return FALSE; + } + + // 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); + + // If an error occured, just return it now. + if ($body === FALSE) { + $static[$url] = $body; + return $body; + } + + $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; +} diff --git a/sites/all/modules/unl/unl.module b/sites/all/modules/unl/unl.module index c6dbc5775f0f0021260b7cc3df02ee4a95a1b95d..869d2d42ec266544bf23f50e4f5e58ce1b4278b3 100644 --- a/sites/all/modules/unl/unl.module +++ b/sites/all/modules/unl/unl.module @@ -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 diff --git a/sites/all/themes/unl_wdn/template.php b/sites/all/themes/unl_wdn/template.php index 88e6df55c1e9e34ae158c04bb251128cca3ff375..616f96165d3017f9083c2dd250cce2a1be42f31a 100644 --- a/sites/all/themes/unl_wdn/template.php +++ b/sites/all/themes/unl_wdn/template.php @@ -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);