<?php

session_start();

$pages = array();

$ipages = -1 ;

sitemap("www.yo9bxc.com/","www.yo9bxc.com/");

//echo "Numar total de pagini : $ipages<br />";

$rootUrl = "www.yo9bxc.com";

//echo pingGoogleSitemap($rootUrl);

$isoLastModifiedSite = "";

$newLine = "\n";

$indent = " ";

if (!$rootUrl) $rootUrl = "http://www.yo9bxc.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";

foreach($pages as $kkk )

{

//	$urlsetValue .= makeUrlTag ($pageUrl, $pageLastModified, $pageChangeFrequency, $pagePriority);	

	$urlsetValue .= makeUrlTag ("http://".$rootUrl.$kkk, date('Y-m-d H:i:s'), "daily", "0.5");	

}



header('Content-type: application/xml; charset="utf-8"',true);

print "$xmlHeader

$urlsetOpen

$urlsetValue

$urlsetClose

";



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;

}



function pingGoogleSitemap ( $rootUrl ) {

    $fileName = "http://www.google.com/webmasters/sitemaps/ping?sitemap=" .urlencode("$rootUrl/sitemap/sitemap.xml");

    $url = parse_url($fileName);

    if (!isset($url["port"])) $url["port"] = 80;

    if (!isset($url["path"])) $url["path"] = "/";

    $fp = @fsockopen($url["host"],

                     $url["port"],

                     &$errno, &$errstr, 30);



    if ($fp) {

        $head = "";

        $httpRequest = "HEAD ". $url["path"] ."?"

                     .$url["query"] ." HTTP/1.1\r\n"

                     ."Host: ". $url["host"] ."\r\n"

                     ."Connection: close\r\n\r\n";



        fputs($fp, $httpRequest);

        while(!feof($fp)) $head .= fgets($fp, 1024);

        fclose($fp);



        return $head;



    }

return "ERROR";

}





function storeLink($url,$gathered_from) 

{

//	$query = "INSERT INTO links (url, gathered_from) VALUES ('$url', '$gathered_from')";

//	mysql_query($query) or die('Error, insert query failed');

	echo "$url<br>";

    ob_flush();

    flush();	

}



function sitemap($site,$root)

{				 

	set_time_limit(0);

	global $ipages, $pages;

	if ( $site<>$root ) 

		{

			$target_url = $root.$site ;

		}

	else 

		{

			$target_url = $site ;

		}

	

	$userAgent = 'sitemap generator';

	// make the cURL request to $target_url

	$ch = curl_init();

	curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);

	curl_setopt($ch, CURLOPT_URL,$target_url);

	curl_setopt($ch, CURLOPT_FAILONERROR, true);

	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

	curl_setopt($ch, CURLOPT_AUTOREFERER, true);

	curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);

	curl_setopt($ch, CURLOPT_TIMEOUT, 10);

	$html= curl_exec($ch);

	if (!$html) {

//		echo "<br />cURL error number:".$target_url;

//		echo "<br />cURL error number:" .curl_errno($ch);

//		echo "<br />cURL error:" . curl_error($ch);

		return;

	}

	// parse the html into a DOMDocument

	$dom = new DOMDocument();

	@$dom->loadHTML($html);

	// grab all the on the page

	$xpath = new DOMXPath($dom);

	$hrefs = $xpath->evaluate("/html/body//a");

	for ($i = 0; $i < $hrefs->length; $i++) 

		{

			$href = $hrefs->item($i);

			$url = $href->getAttribute('href');

			if( !eregi("click.link",$url) and !eregi("http",$url) and !eregi("domdocument.load",$url) and !eregi("mailto",$url) and !eregi("item_link",$url) and !eregi("channel_link",$url) and !in_array($url,$pages,true) )

				{

					$ipages++;


					$pages[$ipages] = $url;

	//				storeLink($url,$target_url);

					sitemap($url,$root);

				}

		}

}

?>