/** * Determine how long a given element can be cached. * * This function looks for the 'cacheDuration' and 'validUntil' attributes to determine * how long a given XML-element is valid. It returns this as na unix timestamp. * * If both the 'cacheDuration' and 'validUntil' attributes are present, the shorter of them * will be returned. * * @param DOMElement $element The element we should determine the expiry time of. * @return int The unix timestamp for when the element should expire. Will be NULL if no * limit is set for the element. */ private static function getExpireTime(DOMElement $element) { if ($element->hasAttribute('cacheDuration')) { $cacheDuration = $element->getAttribute('cacheDuration'); $cacheDuration = SimpleSAML_Utilities::parseDuration($cacheDuration, time()); } else { $cacheDuration = NULL; } if ($element->hasAttribute('validUntil')) { $validUntil = $element->getAttribute('validUntil'); $validUntil = SimpleSAML_Utilities::parseSAML2Time($validUntil); } else { $validUntil = NULL; } if ($cacheDuration !== NULL && $validUntil !== NULL) { /* Both are given. Return the shortest. */ if ($cacheDuration < $validUntil) { return $cacheDuration; } else { return $validUntil; } } elseif ($cacheDuration !== NULL) { return $cacheDuration; } elseif ($validUntil !== NULL) { return $validUntil; } else { return NULL; } }
/** * Determine how long a given element can be cached. * * This function looks for the 'cacheDuration' and 'validUntil' attributes to determine * how long a given XML-element is valid. It returns this as na unix timestamp. * * If both the 'cacheDuration' and 'validUntil' attributes are present, the shorter of them * will be returned. * * @param mixed $element The element we should determine the expiry time of. * @param int|NULL $maxExpireTime The maximum expiration time. * @return int The unix timestamp for when the element should expire. Will be NULL if no * limit is set for the element. */ private static function getExpireTime($element, $maxExpireTime) { if ($element->cacheDuration !== NULL) { $expire = SimpleSAML_Utilities::parseDuration($element->cacheDuration, time()); if ($maxExpireTime !== NULL && $maxExpireTime < $expire) { $expire = $maxExpireTime; } } else { $expire = $maxExpireTime; } if ($element->validUntil !== NULL) { if ($expire === NULL || $expire > $element->validUntil) { $expire = $element->validUntil; } } return $expire; }
/** * Attempt to update our cache file. */ public function updateCache() { if ($this->updateAttempted) { return; } $this->updateAttempted = TRUE; $this->metadata = $this->downloadMetadata(); if ($this->metadata === NULL) { return; } $expires = time() + 24 * 60 * 60; /* Default expires in one day. */ if ($this->metadata->validUntil !== NULL && $this->metadata->validUntil < $expires) { $expires = $this->metadata->validUntil; } if ($this->metadata->cacheDuration !== NULL) { try { $durationTo = SimpleSAML_Utilities::parseDuration($this->metadata->cacheDuration); } catch (Exception $e) { SimpleSAML_Logger::warning($this->logLoc . 'Invalid cacheDuration in metadata from ' . var_export($this->url, TRUE) . ': ' . var_export($this->metadata->cacheDuration, TRUE)); return; } if ($durationTo < $expires) { $expires = $durationTo; } } $metadataSerialized = serialize($this->metadata); $this->aggregator->addCacheItem($this->cacheId, $metadataSerialized, $expires, $this->cacheTag); }