<?php
error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);
/*
GoogleSiteMap
generates a sitemap in xml format
for google's webmaster
can handle normal and dynamic files/folders
dynamic files/folders designed
to work with mod_rewrite
Created by Isaac Scott <hide@address.com>
Last modified 15th September 2008
*/
require_once dirname(__FILE__) . '/GoogleSiteMapEntry.php';
class GoogleSiteMap
{
// definitions
private
$basedomain,
$dir = '.',
$dirs,
$self,
$entries,
$excludes,
$allowedextensions,
$recursive,
$xml;
// constructor
public function __construct()
{
$this->entries = array();
$this->files = array();
$this->dirs = array();
$this->excludes = array();
$this->recursive = true;
$this->allowedextensions = array('htm', 'html', 'php');
$this->basedomain = ($_SERVER['SERVER_PORT'] == 80) ? "http://" : "https://";
$this->basedomain .= $_SERVER['SERVER_NAME'];
$this->basedomain .= "/";
$this->self = substr($_SERVER['SCRIPT_URL'], 1);
$this->xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
return true;
}
// deconstructor
public function __destruct()
{
unset($this->dirs);
unset($this->files);
}
/*
PUBLIC methods
*/
// takes a single argument that is boolean
public function setRecursive($recursive)
{
$this->recursive = $recursive;
}
// takes a single argument that can be either a single
// extension or an array of extension to be excluded
public function removeExt($extension)
{
if (is_array($extension))
{
foreach ($extension AS $e)
$this->removeExt($e);
}
else
{
foreach ($this->allowedextensions AS $key => $value)
if ($extension == $value)
unset($this->allowedextensions[$key]);
}
}
// takes a single argument that can be either a single
// extension or an array of extension to be included
public function addExt($extension)
{
if (is_array($extension))
{
foreach ($extension AS $e)
$this->addExt($e);
}
else
{
if (!in_array($extension, $this->allowedextensions))
$this->allowedextensions[] = $extension;
}
}
// takes two required arguments and one optional one
public function dynamicEntry($file, $time, $priority = '')
{
$entry = new GoogleSiteMapEntry("{$this->basedomain}$file");
if (!empty($priority))
$entry->setPriority($priority);
$entry->setLastMod($time);
$this->entries[] = $entry;
}
// takes a single argument that can be either a single
// file/folder or an array of files/folders
public function excludeEntry($item)
{
if (is_array($item))
{
foreach ($item AS $i)
$this->excludeEntry($i);
}
else
{
$item = preg_replace('/(\/)$/', '', $item);
array_push($this->excludes, $item);
}
}
// generates and returns the actual xml
public function generateXml()
{
$this->findFiles();
foreach ($this->dirs AS $this->dir)
$this->findFiles();
$this->dir = '.';
$this->xml .= "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
foreach ($this->entries AS $entry)
$this->xml .= $entry->getXml();
$this->xml .= "</urlset>\n";
header("Content-type: text/xml");
return $this->xml;
}
/*
PRIVATE methods
*/
// loops through all directories and generates the xml for each file
// this function works recursively throught the directory tree
private function findFiles()
{
if ($dh = opendir($this->dir))
{
if ($this->dir == '.')
$this->dir = '';
while (($file = readdir($dh)) !== false)
{
if ($file != '.' && $file != '..' && ($this->dir . $file) != $this->self && !in_array($file, $this->excludes))
{
switch(filetype($this->dir . $file))
{
case 'dir':
if ($this->recursive)
array_push($this->dirs, $this->dir . $file . "/");
break;
case 'file':
$parts = explode(".", $file);
if (in_array($parts[count($parts) - 1], $this->allowedextensions))
$this->entries[] = new GoogleSiteMapEntry("{$this->basedomain}{$this->dir}$file");
break;
default:
break;
}
}
}
}
}
}
?>