<?php
/*
COPYRIGHT (C) 2005 BY SMART-IT-CONSULTING.COM
* Do not remove this header
* This program is provided AS IS 
* Use this program at your own risk
* Don't publish this code, link to http://www.smart-it-consulting.com/ instead
* Modified for use by NetStuffers, LLC
*/

define('GO_NINJA_GO',1);
global $sql;
session_start();

// MANDATORY INCLUDES
include "includes/config.php";
include "includes/global.php";
include "classes/sql_class.php";
include "classes/debug_class.php";
include "classes/security_class.php";
include "classes/validation_class.php";

// MANDATORY CLASS DECLARATIONS
$debug = new debug();
$sql = new sql();

$security = new security();
$validation = new validation();


if($_SERVER["HTTPS"]=="on"){$c.="https://";}else{$c.="http://";}
$s=preg_split('/\./',$_SERVER["SERVER_NAME"]);
if(count($s)==3){$sub=$s[0];$d=$s[1].'.'.$s[2];}else{$sub='';$d=$s[0].'.'.$s[1];}
$res=$sql->query("SELECT `id` FROM `config` WHERE `sub`='$sub' || `id`='1' ORDER BY `id` DESC LIMIT 0,1");
$row=$sql->getrow($res);
$siteid=$row[0];

//If the siteid is 2, then we're in the admin.  Die silently since we don't want people sniffing around in here
if($siteid==2){die();}

if($sub!=''){$d = $sub.".".$d;}

$isoLastModifiedSite = "";
$newLine = "\n";
$indent = " ";
if (!$rootUrl) $rootUrl = $c.$d;

$xmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>$newLine";

$urlsetOpen = "<urlset xmlns=\"http://www.google.com/schemas/sitemap/0.84\"	
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"	
xsi:schemaLocation=\"http://www.google.com/schemas/sitemap/0.84	
http://www.google.com/schemas/sitemap/0.84/sitemap.xsd\">$newLine";
$urlsetValue = "";
$urlsetClose = "</urlset>$newLine";

function makeUrlString ($urlString) {
    return htmlentities($urlString, ENT_QUOTES, 'UTF-8'); 
}

function makeIso8601TimeStamp ($dateTime) {
    if (!$dateTime) {
        $dateTime = date('Y-m-d H:i:s');
    }
    if (is_numeric(substr($dateTime, 11, 1))) {
        $isoTS = substr($dateTime, 0, 10) ."T" 
                 .substr($dateTime, 11, 8) ."+00:00";
    }
    else {
        $isoTS = substr($dateTime, 0, 10);
    }
    return $isoTS;
}

function makeUrlTag ($url, $modifiedDateTime, $changeFrequency, $priority) {
    GLOBAL $newLine;
    GLOBAL $indent;
    GLOBAL $isoLastModifiedSite;
    $urlOpen = "$indent<url>$newLine";
    $urlValue = "";
    $urlClose = "$indent</url>$newLine";
    $locOpen = "$indent$indent<loc>";
    $locValue = "";
    $locClose = "</loc>$newLine";
    $lastmodOpen = "$indent$indent<lastmod>";
    $lastmodValue = "";
    $lastmodClose = "</lastmod>$newLine";
    $changefreqOpen = "$indent$indent<changefreq>";
    $changefreqValue = "";
    $changefreqClose = "</changefreq>$newLine";
    $priorityOpen = "$indent$indent<priority>";
    $priorityValue = "";
    $priorityClose = "</priority>$newLine";

    $urlTag	 = $urlOpen;
    $urlValue     = $locOpen .makeUrlString("$url") .$locClose;
    if ($modifiedDateTime) {
     $urlValue .= $lastmodOpen .makeIso8601TimeStamp($modifiedDateTime) .$lastmodClose; 
     if (!$isoLastModifiedSite) { // last modification of web site
         $isoLastModifiedSite = makeIso8601TimeStamp($modifiedDateTime); 
     } 
    }
    if ($changeFrequency) {
     $urlValue .= $changefreqOpen .$changeFrequency .$changefreqClose; 
    }
    if ($priority) {
     $urlValue .= $priorityOpen .$priority .$priorityClose; 
    }
    $urlTag .= $urlValue;
    $urlTag .= $urlClose;
    return $urlTag;
}

$urlsetValue .= makeUrlTag ($pageUrl, $pageLastModified, $pageChangeFrequency, $pagePriority);

if (!$isoLastModifiedSite) { // last modification of web site
    $isoLastModifiedSite = makeIso8601TimeStamp(date('Y-m-d H:i:s')); 
} 

$res=$sql->query("SELECT pagedisplay FROM pages WHERE domain='$siteid' && id>5");
while( $row=$sql->getrow($res) ){
	$urlsetValue .= makeUrlTag ("$rootUrl/".$row[0]."/", $isoLastModifiedSite, "daily", "1.0");	
}

header('Content-type: application/xml; charset="utf-8"',true);
print "$xmlHeader
$urlsetOpen
$urlsetValue
$urlsetClose
";
?>
