view sitemap.php @ 43:f42dbf44b661

Beautify file.
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Fri, 05 Oct 2012 03:16:23 +0200
parents f37be50a70cf
children 79f708a48a7c
line wrap: on
line source

<?
header('Content-type: application/xml');
print '<?xml version="1.0" encoding="UTF-8"?>';
print "\n";
?>
<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">
<?
include_once('php/Options.inc');

function startswith($haystack, $needle) {
    return strpos($haystack, $needle) === 0;
}

function endsWith($haystack, $needle) {
  $l = strlen($haystack) - strlen($needle);
  return strrpos($haystack, $needle) === $l;
}

function getHeaders($url)
{
  $response = http_head($url, array("timeout"=>1), $info);
  $headers = array();
  $str = explode("\n", $response);
  foreach($str as $kv) {
    $p = strpos($kv, ":");
    if ($p) {
      $key = substr($kv, 0, $p);
      $value = trim(substr($kv, $p + 1));
      $headers[$key] = $value;
    }
  }
  return $headers;
}

function opttostring($opts)
{
  $str = '';
  foreach (array_keys($opts) as $key) {
    $value = $opts[$key];
    if ($str) {
      $str .= "&${key}=${value}";
    }
    else {
      $str = "?${key}=${value}";
    }
  }
  return $str;
}


$master = new DOMDocument();
$master->load("master.xml");

$options = new Options($master);

$base = 'http://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
$base = substr($base, 0, strrpos($base, '/'));

$acceptedLanguages = $options->getAcceptedLanguages();

foreach($options->getAcceptedLanguages() as $lang) {
  if ($handle = opendir("${lang}")) {
    while (false !== ($entry = readdir($handle))) {
      if (endsWith($entry, '.xml')) {
	$fentry = "${lang}/${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 != $options->getInputDefault('name')) {
	      $opts['name'] = $name;
	    }

	    $optstring = opttostring($opts);

	    $location = "${base}/${optstring}";
	    $headers = getHeaders($location);

	    $location = htmlentities($location);

	    $lastmod = $headers["Last-Modified"];
	    $lastmod = strtotime($lastmod);
	    $lastmod = date(DateTime::W3C, $lastmod);

	    print "<url>\n";
	    print "<loc>${location}</loc>\n";
	    print "<lastmod>${lastmod}</lastmod>\n";
	    print "</url>\n";
	  }
	}
      }
    }
    closedir($handle);
  }
}

?>
</urlset>