function advagg_relocate_get_remote_data in Advanced CSS/JS Aggregation 7.2
Gets external CSS and JS files; caches and returns response.
Parameters
string $urls: URLs to get.
string $type: Will be css or js.
array $options: Array of settings for the http request.
bool $force_check: TRUE if you want to force check the external source.
Return value
array Array of http responses.
2 calls to advagg_relocate_get_remote_data()
- advagg_relocate_css_post_alter in advagg_relocate/
advagg_relocate.module - Alter the css array.
- advagg_relocate_js_post_alter in advagg_relocate/
advagg_relocate.module - Alter the js array.
File
- advagg_relocate/
advagg_relocate.advagg.inc, line 365 - Advanced aggregation relocate module.
Code
function advagg_relocate_get_remote_data($urls, $type, array $options = array(), $force_check = FALSE) {
// Set arguments for drupal_http_request().
$options += array(
'headers' => array(
'Accept-Encoding' => 'gzip, deflate',
'Connection' => 'close',
'Referer' => $GLOBALS['base_root'] . request_uri(),
),
'timeout' => 8,
'version' => '1.0',
);
if (function_exists('brotli_uncompress')) {
$options['headers']['Accept-Encoding'] .= ', br';
}
// Build CID.
$cids = array();
foreach ($urls as $k => $v) {
$cids["advagg_relocate_{$type}_external:{$k}"] = "advagg_relocate_{$type}_external:{$k}";
}
// Try local cache.
$return = array();
$responses = array();
$cached_data = cache_get_multiple($cids, 'cache_advagg_info');
$cached_data = array_merge($cids, $cached_data);
$url_to_cid = array();
$request_sent = FALSE;
foreach ($cached_data as $cid => $cache) {
// CID not set, skip.
if (empty($cid)) {
continue;
}
// Set cid, filename and get url.
$options['cid'] = $cid;
$options['filename'] = substr($cid, 26 + strlen($type));
// Filename lookup failure, skip.
if (empty($urls[$options['filename']])) {
continue;
}
// Add url to the lookup array.
$url = advagg_force_https_path($urls[$options['filename']]);
$url_to_cid[$url] = $cid;
// Reset headers if needed.
if (isset($options['headers']['If-None-Match'])) {
unset($options['headers']['If-None-Match']);
}
if (isset($options['headers']['If-Modified-Since'])) {
unset($options['headers']['If-Modified-Since']);
}
// Use cached data or setup for 304.
if (!empty($cache->data)) {
if ($cache->expire >= REQUEST_TIME && isset($cache->data->url) && empty($force_check)) {
$return[$cache->data->url] = $cache->data;
continue;
}
else {
// Set header for 304 response.
if (isset($cached_data->data->headers['etag'])) {
$options['headers']['If-None-Match'] = $cached_data->data->headers['etag'];
}
if (isset($cached_data->created)) {
$options['headers']['If-Modified-Since'] = gmdate('D, d M Y H:i:s T', $cached_data->created);
}
}
}
// Get data.
if (module_exists('httprl')) {
$request_sent = TRUE;
httprl_request($url, $options);
}
else {
$request_sent = TRUE;
$responses[$url] = drupal_http_request($url, $options);
if (!isset($responses[$url]->options)) {
$responses[$url]->options = $options;
}
if (!isset($responses[$url]->url)) {
$responses[$url]->url = $url;
}
}
}
if ($request_sent && module_exists('httprl')) {
$responses = httprl_send_request();
}
if (empty($responses)) {
return $return;
}
// Try failures again.
advagg_relocate_try_failures_again($responses);
// Process remote data.
foreach ($responses as $url => $response) {
// Content length does not match the response data.
if (!empty($response->headers['content-length']) && $response->headers['content-length'] > strlen($response->data)) {
continue;
}
// No url is a no go.
if (empty($response->url)) {
$response->url = $url;
}
if (isset($response->options['cid'])) {
$cid = $response->options['cid'];
}
elseif (isset($url_to_cid[$response->url])) {
$cid = $url_to_cid[$response->url];
}
else {
// Can't match up url to the cid.
continue;
}
// Update object.
if (!isset($response->options['filename'])) {
$response->options['filename'] = substr($cid, 26 + strlen($type));
}
if (!isset($response->options['cid'])) {
$response->options['cid'] = $cid;
}
advagg_relocate_process_http_request($response, $type);
if ($response->code == 304 && !empty($cached_data->data)) {
// Update cache expire time.
cache_set($cid, $cached_data->data, 'cache_advagg_info', REQUEST_TIME + $response->ttl);
// Return cached data.
$return[$cached_data->data->url] = $cached_data->data;
}
// Skip if not a 200.
if ($response->code != 200 && $response->code != 201 && $response->code != 202 && $response->code != 206) {
continue;
}
if (empty($response->data)) {
continue;
}
$response->local_cache = FALSE;
// Save data to the cache.
if (!empty($response->data)) {
$response->hash = drupal_hash_base64($response->data);
$response->local_cache = TRUE;
cache_set($cid, $response, 'cache_advagg_info', REQUEST_TIME + $response->ttl);
$response->local_cache = FALSE;
}
$return[$response->url] = $response;
}
return $return;
}