THIS source compiler<?php
/*
DAT/SITEMAP.PHP - This creates and updates the file sitemap.xml.
It's the initial version and all the strings and HTML need to go
into dat/messages.php or in .html files.
It turned out to be rather easy to manipulate the XML file after
we created the parse_xml_file() function as an experiment. The
function array_to_xml() is not as yet versatile to handle a
complex (multi) array, it's rather specific to the sitemap format.
This will be reshaped.
*/
$host = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$url = "$host?op=sitemap&update=";
$xmlns = 'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"';
// view file (must be first; you'll see)
if (isset($_GET['view'])) {
$sitemap = file_get_contents('sitemap.xml');
echo '<pre class="sitemap">'.htmlentities($sitemap).'</pre>';
echo '<form method="post" action=""><button name="button" value="done">done</button></form>';
return;
}
// create new file
if (isset($_GET['create'])) {
$sitemap = sitemap_create($xmlns);
if ($sitemap)
echo '<p>The sitemap.xml file has been created.</p>';
else
echo '<p>There was an error: '.php_error().' (if blank enable <tt>showerrors</tt>).</p>';
echo '<form method="post" action=""><button name="button" value="done">done</button></form>';
if ($sitemap)
echo "<form method='post' action='$url&view='><p><button name='button' value='sitemap'>view</button></p></form>";
return;
}
// update existing file
if (isset($_GET['update'])) {
foreach ($_POST as $section => $unused) {
if ($section == 'button') continue;
$sections[] = $section;
}
if (!isset($sections)) {
echo '<p>No sections were selected.</p>';
return;
}
$sitemap = sitemap_update($sections,$xmlns);
if ($sitemap)
echo '<p>The sitemap.xml file has been updated.</p>';
else
echo '<p>There was an error: '.php_error().'(if blank enable <tt>showerrors</tt>).</p>';
echo '<form method="post" action=""><button name="button" value="done">done</button></form>';
if ($sitemap)
echo "<form method='post' action='$url&view='><p><button name='button' value='sitemap'>view</button></p></form>";
return;
}
/* introduction text and form */
if (!is_file('sitemap.xml')) {
echo '<p>The file <tt>sitemap.xml</tt> does not exist. It needs to exist (and can be empty at first) and needs PHP write permissions.</p>';
echo '<form method="post" action=""><input type="submit" name="button" value="done"></form>';
return;
}
if (!is_writeable('sitemap.xml')) {
echo '<p>The file <tt>sitemap.xml</tt> exists but is not writeable. It needs PHP write permissions.</p>';
echo '<form method="post" action=""><input type="submit" name="button" value="done"></form>';
return;
}
if (filesize('sitemap.xml') == 0) {
sitemap_create($xmlns);
echo "<p>A new sitemap has been created with the current date as it's <tt>lastmod</tt>. Every time you add posts, come back here to update the <tt>lastmod</tt> for the sections that have been updated.</p>
<form method='post' action='$url&view='>
<p><button name='button' value='sitemap' title='view sistemap'>view</button></p>
</form>
";
}
echo "<div class='sitemap'>
<p><b>Update the <tt>sitemap.xml</tt> File</b></p>
<p>Select the sections that have had recent posts for update.</p>";
echo "<form method='post' action='$url'>\n";
$sections = config('sections');
foreach ($sections as $name => $section) {
if (isset($section['disabled'])) continue;
echo "<label style='width:88px;display:inline-block;' for='$name'>$name</label>";
echo "<input type='checkbox' name='$name'><br>";
}
echo "
<p><button name='button' value='sitemap' title='update sitemap'>sitemap</button>
<button name='button' value='done' title='cancel'>done</button><p>
</form>
<form method='post' action='$url&create='>
<button name='button' value='sitemap' title='re-create sitemap'>re-create</button> If you ever change the <tt>sections.ini</tt> file you will have to run <tt>re-create</tt> to update the file.</form>
<form method='post' action='$url&view='>
<p><button name='button' value='sitemap' title='view sistemap'>view</button></p>
</form>
</div>
";
/* end of code - start of support functions */
/* array_to_xml - convert assoc. array to XML format */
// kind of specific to the sitemap format for now
function array_to_xml($data, $name, $attr = '') {
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$xml .= "<$name $attr>\n";
foreach ($data as $item => $dat) {
foreach ($dat as $d) {
$xml .= " <$item>\n";
foreach ($d as $k => $v)
$xml .= " <$k>$v</$k>\n";
$xml .= " </$item>\n";
}
}
$xml .= "</$name>";
return $xml;
}
/* parse_xml_file - read XML file into an associative array */
// $name will be set to the XML open tag
function parse_xml_file($file, &$name) {
$data = file_get_contents($file);
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $data, $values, $index);
$xml_error = xml_get_error_code($xml_parser);
if ($xml_error)
echo '<p><b>parse_xml_file</b> '.xml_error_string($xml_error).'</p>';
xml_parser_free($xml_parser);
$open = array_shift($values);
$name = strtolower($open['tag']);
$a = xml_get_section($values,$index);
return $a;
}
function xml_get_section(&$values,$index) {
$a = array();
while ($val = array_shift($values)) {
if ($val['type'] == 'complete') {
$v = strtolower($val['tag']);
$d = (isset($val['value'])) ? $val['value'] : '';
$a[$v] = trim($d);
}
if ($val['type'] == 'close') {
break;
}
if ($val['type'] == 'open') {
$v = strtolower($val['tag']);
$a[$v][] = xml_get_section($values,$index);
}
}
return $a;
}
/* sitemap_update - update our sitemap based on the changed sections */
function sitemap_update($sections, $xmlns) {
$data = parse_xml_file('sitemap.xml',$name);
if ($name != 'urlset') {
echo "<p>Found XML name of '$name' - expecting 'urlset'</p>";
echo '<p>Sitemap may need to be created.</p>';
return;
}
foreach ($data['url'] as $i => $val) {
foreach ($sections as $section) {
if ($section == 'root') {
$data['url'][0]['lastmod'] = date('Y-m-d');
continue;
}
if (strstr($val['loc'],$section))
$data['url'][$i]['lastmod'] = date('Y-m-d');
}
}
$sitexml = array_to_xml($data,'urlset',$xmlns);
return file_put_contents('sitemap.xml',$sitexml);
}
/* sitemap_create - create our sitemap based on the configured sections */
// expects the file 'sitemap.xml' to exist and be writeable
function sitemap_create($attr) {
$siteurl = config('siteurl');
$lastmod = date('Y-m-d');
$sitexml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<urlset $attr>
<url>
<loc>$siteurl</loc>
<lastmod>$lastmod</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
XML;
$sections = config('sections');
foreach ($sections as $name => $section) {
if ($name == 'root') continue;
if (isset($section['disabled'])) continue;
$sitexml .= <<<XML
<url>
<loc>$siteurl?arg=$name</loc>
<lastmod>$lastmod</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
XML;
}
$sitexml .= '</urlset>';
return file_put_contents('sitemap.xml',$sitexml);
}
Source Code Index