Mercurial > SimpleWebPresenter
diff Sitemap.inc.php @ 134:b6b4a58c7625
Using .inc.php rather than just .inc for include files.
| author | Tom Fredrik Blenning <bfg@bfgconsult.no> |
|---|---|
| date | Sun, 22 Jan 2023 19:22:00 +0100 |
| parents | Sitemap.inc@00255ca89459 |
| children | 60bc8f62384d |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Sitemap.inc.php Sun Jan 22 19:22:00 2023 +0100 @@ -0,0 +1,193 @@ +<?php +include_once 'ScriptIncludeCache.inc.php'; + +/// @cond +$baseDir = dirname(__FILE__); +$cache = ScriptIncludeCache::instance(__FILE__); +$cache->includeOnce('Http.inc.php', $baseDir); +$cache->includeOnce('Page.inc.php', $baseDir); +$cache->includeOnce('common-functions.inc.php', $baseDir); +$cache->includeOnce('CacheTimeCheck.inc.php', $baseDir); +/// @endcond + +class SimpleCache extends Cache { + private $time; + + function __construct($time = 0) { + $this->time = $time; + } + function getNewest() { + return $this->time; + } +}; + +function cmp_length_lex($a, $b) +{ + if ($a == $b) { + return 0; + } + $la = strlen($a); + $lb = strlen($b); + if ($la == $lb) { + return ($la < $lb) ? -1 : 1; + } + return ($a < $b) ? -1 : 1; +} + + +/** + * Functionality for generating a sitemap + */ +class Sitemap extends Page +{ + private $master; + private $options; + + /** + * Constructs a sitemap object from a master document + * + * @param $path location of master document + */ + function __construct($path) { + $this->master = new DOMDocument(); + $this->master->load($path); + + $this->options = new Options($this->master); + $this->lastmod=0; + } + + function cacheCheck() + { + if ($this->lastmod == 0) + return Cacheable::UNDETERMINED; + else + return Cacheable::YES; + } + + function mayValidate() + { + return false; + } + + private function processDir($dir, $lang, $acceptedLanguages, $baseurl) { + $urls = array(); + + $base=basePath(); + $base=$_SERVER['DOCUMENT_ROOT']; + + if ($handle = opendir($base . "/${dir}")) { + while (false !== ($entry = readdir($handle))) { + if (endsWith($entry, '.xml')) { + $fentry = $base . "/${dir}/${entry}"; + $doc = new DOMDocument(); + + if (file_exists($fentry)) { + $doc->load($fentry); + + $opts = array(); + if (count($acceptedLanguages) > 1) { + $opts['lang'] = $lang; + } + + $toplevel = $doc->getElementsByTagName("toplevel"); + + if($toplevel->length) { + $name = substr($entry, 0, -4); + + if ($name != $this->options->getInputDefault('name')) { + $opts['name'] = $name; + } + + $optstring = genUrl($opts, array(), array('lang', 'name')); + $location = "${baseurl}${optstring}"; + array_push($urls, $location); + } + } + } + } + closedir($handle); + } + return $urls; + } + + function generateContent() { + + /// The final output variable + $out = '<?xml version="1.0" encoding="UTF-8"?>'; + $out .= "\n"; + $out .= '<?xml-stylesheet type="text/xsl" href="/css/gss.xsl"?>'; + $out .= "\n"; + $out .= '<urlset + xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 + http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> +'; + + $proto= $_SERVER["REQUEST_SCHEME"]; + if (array_key_exists('HTTP_X_FORWARDED_PROTO', $_SERVER)) { + $proto=$_SERVER['HTTP_X_FORWARDED_PROTO']; + } + $base = $proto ."://". $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; + $l = strrpos($base, '/'); + if ($l) + $base = substr($base, 0, $l); + + $acceptedLanguages = $this->options->getAcceptedLanguages(); + + $urls = array(); + + foreach($this->options->getAcceptedLanguages() as $lang) { + $urls=array_merge($urls, + $this->processDir($lang, $lang, + $acceptedLanguages, $base) + ); + $urls=array_merge($urls, + $this->processDir("common", $lang, + $acceptedLanguages, $base) + ); + } + + usort($urls, "cmp_length_lex"); + + foreach($urls as $location) { + $headers = Http::getHeaders($location, 5); + + $location = htmlentities($location); + + if (array_key_exists('Last-Modified', $headers)) { + $lastmod = $headers["Last-Modified"]; + } + + $n = StatusCodes::codeFromHeader($headers['']); + + if ($n == StatusCodes::HTTP_OK) { + if (isset($lastmod)) { + + $lastmod = strtotime($lastmod); + if ($lastmod > $this->lastmod) { + $this->lastmod = $lastmod; + } + $lastmod = date(DateTime::W3C, $lastmod); + } + + $out .= "<url>\n"; + $out .= "<loc>${location}</loc>\n"; + if (isset($lastmod)) { + $out .= "<lastmod>${lastmod}</lastmod>\n"; + } + $out .= "</url>\n"; + } + } + + $out .= "</urlset>"; + + $res = new PageContent($out); + $cache=new SimpleCache($this->lastmod); + $cache->setMaxAge(86400); + $this->setCache($cache); + $res->setHeader('Content-type', 'application/xml'); + return $res; + } +} +?>
