<?php
require_once 'library/config.php';

function CategoryName($catId) {
     $sql = "SELECT cat_name
	        FROM tbl_category
			WHERE cat_id = '$catId'";

    $result    = dbQuery($sql);
    $row       = dbFetchAssoc($result);
    $titleCat = $row['cat_name'];
    return $titleCat;
}

$q = "SELECT cat_parent_id, tbl_product.cat_id, cat_name, bnd_name, pd_pn, pd_id, pd_name, pd_description, pd_image, pd_price
        FROM tbl_product
        LEFT JOIN tbl_category ON tbl_product.cat_id = tbl_category.cat_id
        LEFT JOIN tbl_brand ON tbl_brand.bnd_id=tbl_product.bnd_id
        ORDER BY cat_id ASC";
$r = @mysql_query($q);

$sqlc="SELECT cat_parent_id, cat_id, cat_name
        FROM tbl_category
        ORDER BY cat_parent_id ASC";
$rc = @mysql_query($sqlc);

// There are no errors so we can continue
if (!mysql_error()) {
    header('Content-type: application/xml; charset="utf-8"',true);
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
          <urlset
                xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"
                xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
                xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9
                      http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">
          <!-- baseonline.ro -->

          <url>
            <loc>http://www.baseonline.ro/</loc>
            <changefreq>daily</changefreq>
            <priority>1.00</priority>
          </url>
          <url>
            <loc>http://www.baseonline.ro/index.php</loc>
            <changefreq>daily</changefreq>
            <priority>0.80</priority>
          </url>
          <url>
            <loc>http://www.baseonline.ro/index.php?cart=1&amp;action=view</loc>
            <changefreq>daily</changefreq>
            <priority>0.80</priority>
          </url>
          <url>
            <loc>http://www.baseonline.ro/promotii</loc>
            <changefreq>daily</changefreq>
            <priority>0.80</priority>
          </url>
          <url>
            <loc>http://www.baseonline.ro/cum-cumpar</loc>
            <changefreq>daily</changefreq>
            <priority>0.80</priority>
          </url>
          <url>
            <loc>http://www.baseonline.ro/contact</loc>
            <changefreq>daily</changefreq>
            <priority>0.80</priority>
          </url>";

    while (list($cat_parent_id, $cat_id, $cat_name) = mysql_fetch_row($rc)) {
		// Clean product name (new lines)
		$cat_name = str_replace("\n", "", strip_tags($cat_name));

		$cat_url = function_to_get_category_url($cat_id, $cat_name);

		// Strip html from category name
        if ($cat_parent_id!=0) $cat_name = CategoryName($cat_parent_id).">".$cat_name;
		$cat_name = html_to_text($cat_name);

		// Output the datafeed content
		echo "<url>\n";
        echo  "<loc>". $cat_url . "</loc>\n";
        echo "<changefreq>daily</changefreq>\n";
        echo "<priority>0.80</priority>\n";
        echo "</url>\n";
	}

	while (list($cat_parent_id, $cat_id, $cat_name, $prod_manufacturer, $prod_model, $prod_id, $prod_name, $prod_desc, $prod_image, $prod_price) = mysql_fetch_row($r)) {
		// Clean product name (new lines)
		$prod_name = str_replace("\n", "", strip_tags($prod_name));

		$prod_url = function_to_get_product_url($prod_id, $cat_id, $prod_name, $cat_name);
        $buy_url = function_to_get_buy_url($prod_id, $prod_name);
		//$prod_image = function_to_get_product_image($prod_image);

		// Strip html from category name
        if ($cat_parent_id!=0) $cat_name = CategoryName($cat_parent_id).">".$cat_name;
		$prod_url =  utf8_encode($prod_url);
        $buy_url =  utf8_encode($buy_url);
		// Output the datafeed content
        echo "<url>\n";
        echo  "<loc>". $prod_url . "</loc>\n";
        echo "<changefreq>daily</changefreq>\n";
        echo "<priority>0.70</priority>\n";
        echo "</url>\n";

        echo "<url>\n";
        echo  "<loc>". $buy_url . "</loc>\n";
        echo "<changefreq>daily</changefreq>\n";
        echo "<priority>0.65</priority>\n";
        echo "</url>\n";
	}
 echo "</urlset> ";
}
else {
	print "Query error: " . mysql_error();
}

// Function to return the Product URL based on your product ID
function function_to_get_category_url($cat_id, $cat_name){
	return "http://www.baseonline.ro/". "categorie-$cat_id-".generate_seo_link($cat_name);
}

function function_to_get_buy_url($prod_id, $prod_name){
	return "http://www.baseonline.ro/"."cumpara-$prod_id-".generate_seo_link($prod_name);
}

function function_to_get_product_url($prod_id, $cat_id, $prod_name, $cat_name){
	return "http://www.baseonline.ro/". "categorie-$cat_id-".generate_seo_link($cat_name)."/produs-$prod_id-".generate_seo_link($prod_name);
}

// Function to return the Product Image based on your product image or optionally Product ID
function function_to_get_product_image($prod_image){
	return "http://www.baseonline.ro/images/product/" . $prod_image;
}

function html_to_text($string){

	$search = array (
		"'<script[^>]*?>.*?</script>'si",  // Strip out javascript
		"'<[\/\!]*?[^<>]*?>'si",  // Strip out html tags
		"'([\r\n])[\s]+'",  // Strip out white space
		"'&(quot|#34);'i",  // Replace html entities
		"'&(amp|#38);'i",
		"'&(lt|#60);'i",
		"'&(gt|#62);'i",
		"'&(nbsp|#160);'i",
		"'&(iexcl|#161);'i",
		"'&(cent|#162);'i",
		"'&(pound|#163);'i",
		"'&(copy|#169);'i",
		"'&(reg|#174);'i",
		"'&#8482;'i",
		"'&#149;'i",
		"'&#151;'i",
		"'&#(\d+);'e"
		);  // evaluate as php

	$replace = array (
		" ",
		" ",
		"\\1",
		"\"",
		"&",
		"<",
		">",
		" ",
		"&iexcl;",
		"&cent;",
		"&pound;",
		"&copy;",
		"&reg;",
		"<sup><small>TM</small></sup>",
		"&bull;",
		"-",
		"uchr(\\1)"
		);

	$text = preg_replace ($search, $replace, $string);
	return $text;

}

function replace_not_in_tags($find_str, $replace_str, $string) {

	$find = array($find_str);
	$replace = array($replace_str);
	preg_match_all('#[^>]+(?=<)|[^>]+$#', $string, $matches, PREG_SET_ORDER);
	foreach ($matches as $val) {
		if (trim($val[0]) != "") {
			$string = str_replace($val[0], str_replace($find, $replace, $val[0]), $string);
		}
	}
	return $string;
}
?>