Url = $url; $this->Id = SITE_URL . $url; $this->Title = $title; $this->ParentUrl = $parentUrl; } protected function Sha1Entries(string $xmlString): string{ try{ $xml = new SimpleXMLElement(str_replace('xmlns=', 'ns=', $xmlString)); $xml->registerXPathNamespace('dc', 'http://purl.org/dc/elements/1.1/'); $xml->registerXPathNamespace('schema', 'http://schema.org/'); $entries = $xml->xpath('/feed/entry') ?? []; $output = ''; foreach($entries as $entry){ // Remove any elements, we don't want to compare against those. // This makes it easier to for example generate a new subjects index, // while updating it at the same time. foreach($xml->xpath('/feed/entry/updated') as $element){ unset($element[0]); } $output .= $entry->asXml(); } return sha1(preg_replace('/\s/ius', '', $output)); } catch(Exception $ex){ // Invalid XML return ''; } } protected function SaveIfChanged(string $path, string $feed, string $updatedTimestamp): void{ $tempFilename = tempnam('/tmp/', 'se-opds-'); file_put_contents($tempFilename, $feed); exec('se clean ' . escapeshellarg($tempFilename)); // Did we actually update the feed? If so, write to file and update the index if(!is_file($path) || ($this->Sha1Entries($feed) != $this->Sha1Entries(file_get_contents($path)))){ // Files don't match, save the file and update the parent navigation feed with the last updated timestamp $parentFilepath = WEB_ROOT . str_replace(SITE_URL, '', $this->ParentUrl); if(!is_file($parentFilepath)){ $parentFilepath .= '/index.xml'; } $xml = new SimpleXMLElement(str_replace('xmlns=', 'ns=', file_get_contents($parentFilepath))); $feedEntry = ($xml->xpath('/feed/entry[id="' . $this->Id . '"]/updated') ?? [])[0]; $feedEntry[0] = $updatedTimestamp; file_put_contents($parentFilepath, str_replace(" ns=", " xmlns=", $xml->asXml() ?? '')); rename($tempFilename, $path); } } }