Mercurial > SimpleWebPresenter
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 133:00255ca89459 | 134:b6b4a58c7625 |
|---|---|
| 1 <?php | |
| 2 include_once 'ScriptIncludeCache.inc.php'; | |
| 3 | |
| 4 /// @cond | |
| 5 $baseDir = dirname(__FILE__); | |
| 6 $cache = ScriptIncludeCache::instance(__FILE__); | |
| 7 $cache->includeOnce('Http.inc.php', $baseDir); | |
| 8 $cache->includeOnce('Page.inc.php', $baseDir); | |
| 9 $cache->includeOnce('common-functions.inc.php', $baseDir); | |
| 10 $cache->includeOnce('CacheTimeCheck.inc.php', $baseDir); | |
| 11 /// @endcond | |
| 12 | |
| 13 class SimpleCache extends Cache { | |
| 14 private $time; | |
| 15 | |
| 16 function __construct($time = 0) { | |
| 17 $this->time = $time; | |
| 18 } | |
| 19 function getNewest() { | |
| 20 return $this->time; | |
| 21 } | |
| 22 }; | |
| 23 | |
| 24 function cmp_length_lex($a, $b) | |
| 25 { | |
| 26 if ($a == $b) { | |
| 27 return 0; | |
| 28 } | |
| 29 $la = strlen($a); | |
| 30 $lb = strlen($b); | |
| 31 if ($la == $lb) { | |
| 32 return ($la < $lb) ? -1 : 1; | |
| 33 } | |
| 34 return ($a < $b) ? -1 : 1; | |
| 35 } | |
| 36 | |
| 37 | |
| 38 /** | |
| 39 * Functionality for generating a sitemap | |
| 40 */ | |
| 41 class Sitemap extends Page | |
| 42 { | |
| 43 private $master; | |
| 44 private $options; | |
| 45 | |
| 46 /** | |
| 47 * Constructs a sitemap object from a master document | |
| 48 * | |
| 49 * @param $path location of master document | |
| 50 */ | |
| 51 function __construct($path) { | |
| 52 $this->master = new DOMDocument(); | |
| 53 $this->master->load($path); | |
| 54 | |
| 55 $this->options = new Options($this->master); | |
| 56 $this->lastmod=0; | |
| 57 } | |
| 58 | |
| 59 function cacheCheck() | |
| 60 { | |
| 61 if ($this->lastmod == 0) | |
| 62 return Cacheable::UNDETERMINED; | |
| 63 else | |
| 64 return Cacheable::YES; | |
| 65 } | |
| 66 | |
| 67 function mayValidate() | |
| 68 { | |
| 69 return false; | |
| 70 } | |
| 71 | |
| 72 private function processDir($dir, $lang, $acceptedLanguages, $baseurl) { | |
| 73 $urls = array(); | |
| 74 | |
| 75 $base=basePath(); | |
| 76 $base=$_SERVER['DOCUMENT_ROOT']; | |
| 77 | |
| 78 if ($handle = opendir($base . "/${dir}")) { | |
| 79 while (false !== ($entry = readdir($handle))) { | |
| 80 if (endsWith($entry, '.xml')) { | |
| 81 $fentry = $base . "/${dir}/${entry}"; | |
| 82 $doc = new DOMDocument(); | |
| 83 | |
| 84 if (file_exists($fentry)) { | |
| 85 $doc->load($fentry); | |
| 86 | |
| 87 $opts = array(); | |
| 88 if (count($acceptedLanguages) > 1) { | |
| 89 $opts['lang'] = $lang; | |
| 90 } | |
| 91 | |
| 92 $toplevel = $doc->getElementsByTagName("toplevel"); | |
| 93 | |
| 94 if($toplevel->length) { | |
| 95 $name = substr($entry, 0, -4); | |
| 96 | |
| 97 if ($name != $this->options->getInputDefault('name')) { | |
| 98 $opts['name'] = $name; | |
| 99 } | |
| 100 | |
| 101 $optstring = genUrl($opts, array(), array('lang', 'name')); | |
| 102 $location = "${baseurl}${optstring}"; | |
| 103 array_push($urls, $location); | |
| 104 } | |
| 105 } | |
| 106 } | |
| 107 } | |
| 108 closedir($handle); | |
| 109 } | |
| 110 return $urls; | |
| 111 } | |
| 112 | |
| 113 function generateContent() { | |
| 114 | |
| 115 /// The final output variable | |
| 116 $out = '<?xml version="1.0" encoding="UTF-8"?>'; | |
| 117 $out .= "\n"; | |
| 118 $out .= '<?xml-stylesheet type="text/xsl" href="/css/gss.xsl"?>'; | |
| 119 $out .= "\n"; | |
| 120 $out .= '<urlset | |
| 121 xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" | |
| 122 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| 123 xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 | |
| 124 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> | |
| 125 '; | |
| 126 | |
| 127 $proto= $_SERVER["REQUEST_SCHEME"]; | |
| 128 if (array_key_exists('HTTP_X_FORWARDED_PROTO', $_SERVER)) { | |
| 129 $proto=$_SERVER['HTTP_X_FORWARDED_PROTO']; | |
| 130 } | |
| 131 $base = $proto ."://". $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; | |
| 132 $l = strrpos($base, '/'); | |
| 133 if ($l) | |
| 134 $base = substr($base, 0, $l); | |
| 135 | |
| 136 $acceptedLanguages = $this->options->getAcceptedLanguages(); | |
| 137 | |
| 138 $urls = array(); | |
| 139 | |
| 140 foreach($this->options->getAcceptedLanguages() as $lang) { | |
| 141 $urls=array_merge($urls, | |
| 142 $this->processDir($lang, $lang, | |
| 143 $acceptedLanguages, $base) | |
| 144 ); | |
| 145 $urls=array_merge($urls, | |
| 146 $this->processDir("common", $lang, | |
| 147 $acceptedLanguages, $base) | |
| 148 ); | |
| 149 } | |
| 150 | |
| 151 usort($urls, "cmp_length_lex"); | |
| 152 | |
| 153 foreach($urls as $location) { | |
| 154 $headers = Http::getHeaders($location, 5); | |
| 155 | |
| 156 $location = htmlentities($location); | |
| 157 | |
| 158 if (array_key_exists('Last-Modified', $headers)) { | |
| 159 $lastmod = $headers["Last-Modified"]; | |
| 160 } | |
| 161 | |
| 162 $n = StatusCodes::codeFromHeader($headers['']); | |
| 163 | |
| 164 if ($n == StatusCodes::HTTP_OK) { | |
| 165 if (isset($lastmod)) { | |
| 166 | |
| 167 $lastmod = strtotime($lastmod); | |
| 168 if ($lastmod > $this->lastmod) { | |
| 169 $this->lastmod = $lastmod; | |
| 170 } | |
| 171 $lastmod = date(DateTime::W3C, $lastmod); | |
| 172 } | |
| 173 | |
| 174 $out .= "<url>\n"; | |
| 175 $out .= "<loc>${location}</loc>\n"; | |
| 176 if (isset($lastmod)) { | |
| 177 $out .= "<lastmod>${lastmod}</lastmod>\n"; | |
| 178 } | |
| 179 $out .= "</url>\n"; | |
| 180 } | |
| 181 } | |
| 182 | |
| 183 $out .= "</urlset>"; | |
| 184 | |
| 185 $res = new PageContent($out); | |
| 186 $cache=new SimpleCache($this->lastmod); | |
| 187 $cache->setMaxAge(86400); | |
| 188 $this->setCache($cache); | |
| 189 $res->setHeader('Content-type', 'application/xml'); | |
| 190 return $res; | |
| 191 } | |
| 192 } | |
| 193 ?> |
