<?php
/*
+-------------------------------------------------------------------+
 (c) Vendorama LTD.
+-------------------------------------------------------------------+
 Description: XML Sitemap
 Last Update: Nov 02 2005 -jm
+-Notes-------------------------------------------------------------+
http://www.google.com/webmasters/sitemaps/docs/en/protocol.html
@todo: maybe add priority based on page popularity?
	
	Category status
   1 Enabled
   2 Enabled but hide menu link
   5 Enabled but hide menu link and exclude from search results
   4 Enabled (Wholesale only)
   3 Disabled
   
+-------------------------------------------------------------------+
*/

$sitemap_file=$_SERVER['DOCUMENT_ROOT']."/data/".$_SERVER['HTTP_HOST']."/sitemap.xml";
if(is_file($sitemap_file) && filemtime($sitemap_file)>(time()-86400)) {
	header("HTTP/1.1 200 OK");
	header('Content-Type: text/xml');
	print file_get_contents($sitemap_file);
	exit;
}

#include_once('global.inc');
include_once('mini.inc');
if(!$sDB) die('system/404.inc');

include_once('lib/shoplink.inc');
include_once('lib/cur_format.inc');
include_once('lib/tax.inc');

if(!isset($http)) $http = (isset($_SERVER['HTTPS']))?'https':'http';
//$store_db = "$_CONF[DATA_ROOT]/data/store.db";			
//force xml header
#header('Content-Type: text/xml; charset=UTF-8');
header('Content-Type: text/xml');

//Content of the XML file
$s='<?xml version="1.0" encoding="UTF-8"' . '?' . '>'. "\n";

if($_CONF['vendor_id']!=121) $s.= '<!-- Sitemap for ' . htmlentities($_CONF['vendor_name']) . ".\n 
 Short urls go to:\t ".$http."://$_SERVER[HTTP_HOST]/sitemap.xml?surl=1 
 For products only:\t ".$http."://$_SERVER[HTTP_HOST]/sitemap.xml?products=1 
 Newer products first:\t ".$http."://$_SERVER[HTTP_HOST]/sitemap.xml?newest=1
-->\n";
$s.='<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'. "\n";


//homepage
$s.= "\t<url>\n";
$s.= "\t\t<loc>".$http."://$_SERVER[HTTP_HOST]/</loc>\n";
if($_CONF['vendor_id']==121) {
	$lastmod = time();
} else {
	$lastmod = vendo_sqlite_fetch_single_sqlite_query($sDB, "select time from content where content_id=1"); 
}
if($lastmod) $s.= "\t\t<lastmod>" . getSitemapDate($lastmod) . "</lastmod>\n";
$s.= "\t\t<changefreq>daily</changefreq>\n";
$s.= "\t\t<priority>1</priority>\n";
$s.= "\t</url>\n";

if($_CONF['vendor_id']==121) include('sitemap_vendo.inc');

# disabled categories
$categories_disabled=array(); $and_categories="";
$sql="SELECT category_id,parent_id FROM category WHERE status_id='3' or status_id='4' or url='none'";
$rs=sqlite_query($sDB,$sql);
$c=0;
while ($data=sqlite_fetch_array($rs)){
	$and_categories.=" and category_id!=".$data['category_id'];
	# $categories_disabled[$data['category_id']]=$data['category_id'];
	if($data['parent_id']>0) {
		$rs2=sqlite_query($sDB,"SELECT category_id FROM category WHERE parent_id = '$data[parent_id]'");
		while ($data2=sqlite_fetch_array($rs2)){
			$and_categories.=" and category_id!=".$data2['category_id'];
			$c++;
			if($c>20) break;
		}
	}
			$c++;
			if($c>20) break;
}
//product pages
$order_by = 'product_id desc';
if($_GET['newest']) $order_by = 'updated desc';
$order_by = 'updated desc';
$s.= "<!-- Product pages -->\n";
$sql="SELECT url, created, updated, product_id, category_id FROM product where category_id!=1 and category_id!='' $and_categories and url !='' and name !='' and (parent_product_id='0' or parent_product_id is null) and status_id!=3 and status_id!=4 ORDER BY $order_by limit 10000";

#$sql="SELECT p.url url, p.created created, p.updated updated, p.name name , p.category_id category_id, p.product_id product_id CASE WHEN sale_price > 0 THEN sale_price ELSE price END AS price, product_img.small_w, product_img.small_h FROM product p left join product_img on product_img.product_id = p.product_id AND product_img.img_id = 0 and p.status_id!=3 WHERE p.parent_product_id='' $and_categories and url !='' and name !='' and url not like 'none/%' order by $order_by limit 10";

$rs=sqlite_query($sDB,$sql);

while ($data=sqlite_fetch_array($rs)){
	$lastmod= $data['updated']? $data['updated']:$data['created'];
	if($_GET['surl'] or empty($data['url'])) $data['url']='p/'.$data['product_id'];
	$s.= "\t<url>\n";
	$s.= "\t\t<loc>".$http."://$_SERVER[HTTP_HOST]/".shoplink()."/".ltrim($data['url'],'/')."/</loc>\n";
	if($lastmod) $s.= "\t\t<lastmod>" . getSitemapDate($lastmod) . "</lastmod>\n";
	$t = ($lastmod>(time()-604800)) ? 'weekly':'monthly';
	$s.= "\t\t<changefreq>$t</changefreq>\n";
	$s.= "\t\t<priority>0.5</priority>\n";
	$s.= "\t</url>\n";
}

if(!$_GET['products']) {
//category pages
#$order_by = 'category_id asc';
#if($_GET['newest']) $order_by = 'updated desc';
$order_by = 'updated desc';
$s.= "<!-- Category pages -->\n";
$sql="SELECT url,category_id,updated,created FROM category WHERE url!='none' and status_id=1 order by $order_by";
$rs=sqlite_query($sDB,$sql);

while ($data=sqlite_fetch_array($rs)){
	$lastmod= $data['updated']? $data['updated']:$data['created'];
	if($_GET['surl'] or empty($data['url'])) $data['url']='c/'.$data['category_id'];
	$s.= "\t<url>\n";
	$s.= "\t\t<loc>".$http."://$_SERVER[HTTP_HOST]/".shoplink()."/$data[url]/</loc>\n";
	if($lastmod) $s.= "\t\t<lastmod>" . getSitemapDate($lastmod) . "</lastmod>\n";
	$t = ($lastmod>(time()-604800)) ? 'weekly':'monthly';
	$s.= "\t\t<changefreq>$t</changefreq>\n";
	$s.= "\t\t<priority>0.3</priority>\n";
	$s.= "\t</url>\n";
}

//information pages
#$order_by = 'content_id asc';
#if($_GET['newest']) $order_by = 'time desc';
$order_by = 'time desc';
$s.= "<!-- Information pages -->\n";
$sql="SELECT url, time, content_id FROM content WHERE url!='' and (section='i' or section='f') and section!='n' and url not like '/order/%' and url not like '/purchase/%' and url not like '/login%' and url not like '/account%' and enabled=1 ORDER BY $order_by ";
$rs=sqlite_query($sDB,$sql);

while ($data=sqlite_fetch_array($rs)){
	if($_GET['surl']) $data['url']='i/'.$data['content_id'];
	$s.= "\t<url>\n";
	$s.= "\t\t<loc>".$http."://$_SERVER[HTTP_HOST]/$data[url]/</loc>\n";
	if($data['time']>1000) $s.= "\t\t<lastmod>" . getSitemapDate($data['time']) . "</lastmod>\n";
	$t = ($data['time']>(time()-604800)) ? 'weekly':'monthly';
	$s.= "\t\t<changefreq>$t</changefreq>\n";
	$s.= "\t\t<priority>0.1</priority>\n";
	$s.= "\t</url>\n";

}
}
$s.="</urlset>";
print $s;
file_put_contents($sitemap_file,$s);
function getSitemapDate($time){
	return date("Y-m-d", $time);
}


