<?php
require_once ('./includes/db_connect.inc.php');

$isoLastModifiedSite = "";
$newLine = "\n";
$indent = " ";
$rootUrl = "http://www.prepaidcreditcardscompared.com";

$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;
}

//static pages
if (!$isoLastModifiedSite) { // last modification of web site
    $isoLastModifiedSite = makeIso8601TimeStamp(date('Y-m-d H:i:s'));
}

$urlsetValue .= makeUrlTag ("$rootUrl/index.html", $isoLastModifiedSite, "monthly", "1.0");
$urlsetValue .= makeUrlTag ("$rootUrl/visa-prepaid-credit-cards", $isoLastModifiedSite, "monthly", "1.0");
$urlsetValue .= makeUrlTag ("$rootUrl/mastercard-prepaid-credit-cards", $isoLastModifiedSite, "monthly", "1.0");
$urlsetValue .= makeUrlTag ("$rootUrl/rushcard-prepaid-credit-cards", $isoLastModifiedSite, "monthly", "1.0");
$urlsetValue .= makeUrlTag ("$rootUrl/pink-prepaid-credit-cards", $isoLastModifiedSite, "monthly", "1.0");
$urlsetValue .= makeUrlTag ("$rootUrl/black-prepaid-credit-cards", $isoLastModifiedSite, "monthly", "1.0");
$urlsetValue .= makeUrlTag ("$rootUrl/blue-prepaid-credit-cards", $isoLastModifiedSite, "monthly", "1.0");
$urlsetValue .= makeUrlTag ("$rootUrl/silver-prepaid-credit-cards", $isoLastModifiedSite, "monthly", "1.0");

$query = "Select ID, StateTerritory, Abbreviations From stateabbr ORDER BY StateTerritory ASC";
$result = mysql_query($query);

while($row = mysql_fetch_array($result))
{
	$state = rtrim($row['StateTerritory']);
	$stateLcase = strtolower($state);
	$stateLcase = str_replace(' ','-',$stateLcase);
	$pageUrl = $stateLcase.'-prepaid-credit-card';
	$urlsetValue .= makeUrlTag ("$rootUrl/state/$pageUrl", $isoLastModifiedSite, "monthly", "1.0");
}

$urlsetValue .= makeUrlTag ("$rootUrl/about.html", $isoLastModifiedSite, "monthly", "1.0");
$urlsetValue .= makeUrlTag ("$rootUrl/contact.html", $isoLastModifiedSite, "monthly", "1.0");
$urlsetValue .= makeUrlTag ("$rootUrl/privacy.html", $isoLastModifiedSite, "monthly", "1.0");
$urlsetValue .= makeUrlTag ("$rootUrl/disclaimer.html", $isoLastModifiedSite, "monthly", "1.0");



header('Content-type: application/xml; charset="utf-8"',true);
print "$xmlHeader
$urlsetOpen
$urlsetValue
$urlsetClose
";
?>