<?php
	// Prevent the browser from caching the result.
	// Date in the past
	header('Expires: Mon, 26 Jul 1997 05:00:00 GMT') ;
	// always modified
	header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT') ;
	// HTTP/1.1
	header('Cache-Control: no-store, no-cache, must-revalidate') ;
	header('Cache-Control: post-check=0, pre-check=0', false) ;
	// HTTP/1.0
	header('Pragma: no-cache') ;

	// Set the response format.
	header( 'Content-Type:text/xml; charset=utf-8' ) ;
	define('ROOT_PATH', './');
	@include(ROOT_PATH.'support/config.inc.php');
	include(ROOT_PATH.'support/functions.inc.php');
	$HTTP = array();
	if ($_GET) {
		$HTTP = $_GET;
		$_REQUEST_TYPE = "get";
	} else if ($_POST) {
		$HTTP = $_POST;
		$_REQUEST_TYPE = "post";
	} else {
		//die("Unknown URI Method!");
	}
	
//-----------------------------------------------------
//--- Start DB ----------------------------------------
//-----------------------------------------------------
	include(ROOT_PATH.'support/db.'.strtolower($config["db_type"]).'.inc.php');
	$site_db = new Db($config["db_host"], $config["db_user"], $config["db_pass"], $config["db_table"]);
	if (!$site_db) {
		die();
	}
//-----------------------------------------------------
//--- Check if site is a partner website --------------
//-----------------------------------------------------
	
	// get host name
	$wid = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '';
	$sql = "SELECT `partner_config` FROM `partner_sites` WHERE `partner_domain` = '".$wid."' AND `partner_active` = '1'";
	$result = $site_db->query_firstrow($sql);
	if ($result && array_key_exists('partner_config', $result) && $result['partner_config'] != '') {
		if (file_exists(ROOT_PATH.'support/'.strtolower($result['partner_config']).'.config.inc.php')) {
			include(ROOT_PATH.'support/'.strtolower($result['partner_config']).'.config.inc.php');
		}
	}
//-----------------------------------------------------
//--- Get Ad Sections ---------------------------------
//-----------------------------------------------------
	$sections = array();
	$sql = "SELECT * FROM `sections`";
	if (array_key_exists('limit_sections', $config)) {
		$sql .= " WHERE".$config['limit_sections'];
	}
	$sql .= " ORDER BY `order`";
	$result = $site_db->query($sql);
	if ($result) {
		while ($row = $site_db->fetch_array($result)) {
			if ($row['section'] == 'directory' || $row['brand_site'] == 1) { continue; }
  		$sections[$row['section']] = $row;
		}
	}
	$site_db->free_result();

//-----------------------------------------------------
//--- Begin XML ---------------------------------------
//-----------------------------------------------------
	CreateXmlHeader() ;
	echo '<title>'.utf8_encode(htmlentities($config["sitename"],ENT_QUOTES)).'</title>';
	echo '<description>'.utf8_encode(htmlentities($config["tagline"],ENT_QUOTES)).'</description>';
	echo '<link>'.utf8_encode(htmlentities($config["site"],ENT_QUOTES)).'</link>';

//-----------------------------------------------------
//--- Check User --------------------------------------
//-----------------------------------------------------
	$do_user = "on";
	$do_db = "";
	if (array_key_exists('user', $HTTP) && $HTTP['user'] != "") {
		$uservar = array();
		$uservar = explode("/", $HTTP['user']);
		$ucount = count($uservar);
		$user = $uservar[$ucount-2];
		foreach ($sections as $key => $val) {
			if ($key == $user) {
				$do_user = "";
				$do_db = "on";
				$HTTP['user'] = $key;
				break;
			}
		}
		
		if ($do_user == "on") {
			$sql = "SELECT COUNT(*) FROM `users` WHERE `user_username` = '$user'";
			$result = $site_db->query_firstrow($sql);
			if ($result[0] > 0) {
				$HTTP["user"] = $user;
			} else {
				$HTTP["user"] = "";
			}
		}
	}
	$site_db->free_result();

//-----------------------------------------------------
//--- Get Latest Ads ----------------------------------
//-----------------------------------------------------
	$i = 0;
	$fields = array();
	foreach ($sections as $key => $val) {
		if ($do_db == "on" && $key != $HTTP['user']) {
			continue;
		}
		if ($val['active'] == "on") {
			$sql = "SELECT * FROM ".$key." WHERE `status` = 'ok'";
			if (array_key_exists('user', $HTTP) && $HTTP['user'] != "" && $do_user == "on") {
				$sql .= " AND `who_modified` = '".$HTTP['user']."'";
			}
			
			if ($do_db == "on") {
				$sql .= " ORDER BY `db_id` DESC LIMIT 100";
			} else {
				$sql .= " ORDER BY `db_id` DESC LIMIT 50";
			}
			$result = $site_db->query($sql);
			if ($result) {
				
				while ($row = $site_db->fetch_array($result)) {
					$row['section'] = $key;
					$row['pg_id'] = $sections[$key]['pg_id'];
					$row['prefix'] = $sections[$key]['preffix'];
					$fields[$i] = $row;
					$i++;
				}
			}
		}
	}
	$site_db->free_result();

	if (count($fields) > 0) {
		foreach ($fields as $key => $val) {
			$mod[$key] = $val['modification_time'];
		}
		array_multisort($mod, SORT_DESC, $fields);
		$counter = 0;
		foreach ($fields as $key => $val) {
			$sect = array();
			$sect = explode("_", $val['section']);
			$section = $sections[$val['section']]['long_name'];
			$link = $val['pg_id'].'/view/'.$val['db_id'];
			if ($val['section'] == 'b_cars' || $val['section'] == 'd_uniq') {
				$title = $val['year'].' '.$val['make'].' '.$val['model'];
				if ($val['series'] != '') {
					$title .= ' '.$val['series'];
				}
				if ($val['badge'] != '') {
					$title .= ' '.$val['badge'];
				}
			} else {
				if ((array_key_exists('caption', $val) && $val['caption'] != "") || (array_key_exists('caption_header', $val) && $val['caption_header'] != "")) {
					$title = (array_key_exists('caption_header', $val) && $val['caption_header'] != "") ? $val['caption_header'].": " : "";
					$title .= (array_key_exists('caption', $val) && $val['caption'] != "") ? $val['caption'] : "";
				} else {
					$title = ((array_key_exists('listing', $val)) ? $val['listing']." ":'').((array_key_exists('category', $val)) ? $val['category']." ":'').((array_key_exists('listing_type', $val)) ? $val['listing_type']:'');
				}
			}
			if (array_key_exists('price', $val) && $val['price'] != "") {
				if (array_key_exists('price_suffix', $val) && $val['price_suffix'] != "") {
					$pr_suf = array();
					$pr_suf = explode('/', $val['price_suffix']);
					if ($pr_suf[0] == 1) {
						$price = $pr_suf[1]." ".$val['price'];
					} else if ($pr_suf[0] == 2) {
						$price = $val['price']." ".$pr_suf[1];
					} else {
						$price = $pr_suf[1];
					}
				} else {
					$price = $val['price'];
				}
			}
			$this_user = $val['who_modified'];
			if (preg_match("/^dealer/", $val['group_modified'])) {
				$sql = "SELECT `user_company` FROM `users` WHERE `user_username` = '".$val['who_modified']."'";
				$user_comp = $site_db->query_firstrow($sql);
				if ($user_comp && $user_comp['user_company'] != '') {
					$this_user = $user_comp['user_company'];
				}
			}
			
			if (array_key_exists('modification_time', $val) && is_numeric($val['modification_time'])) {
				$val['modification_time'] = date('Y-m-d', $val['modification_time']);
			} else {
				// is an old listing which has US time stamp
				$parts = explode('/', $val['modification_time']);
				$val['modification_time'] = $parts[1].'/'.$parts[0].'/'.$parts[2];
			}
			
			if (array_key_exists('text', $val) && $val['text'] != "") {
				$desc = "";
				if ($price != "") {
					$desc .= "Price: ".$price."<br>";
				}
				$val['text'] = str_replace('~nl~', '<br>', $val['text']);
				$val['text'] = str_replace('~p~', '|', $val['text']);
				$desc .= substr($val['text'],0,150)."<br>Posted by: ".$this_user."<br>Date: ".$val['modification_time']."<br>In: ".$section."<br>".$config["site"].$link;
			} else {
				$desc = "Posted by: ".$this_user."<br>Date: ".$val['modification_time']."<br>In: ".$section."<br>".$config["site"].$link;
			}
			
			echo '<item>';
			echo '<title>'.utf8_encode(htmlentities($title,ENT_QUOTES)).'</title>';
			echo '<description>'.utf8_encode(htmlentities($desc,ENT_QUOTES)).'</description>';
			echo '<link>'.utf8_encode(htmlentities($config["site"].$link)).'</link>';
			echo '</item>';
			$counter++;
			if ($counter > 50) {
				break;
			}
		}
	} else {
		echo '<item>';
		echo '<title>'.utf8_encode(htmlentities($config["sitename"],ENT_QUOTES)).'</title>';
		echo '<description>'.utf8_encode(htmlentities($config["tagline"],ENT_QUOTES)).'</description>';
		echo '<link>'.utf8_encode(htmlentities($config["site"],ENT_QUOTES)).'</link>';
		echo '</item>';
	}
	
//-----------------------------------------------------
//--- Finish XML --------------------------------------
//-----------------------------------------------------
	CreateXmlFooter() ;
	$site_db->close();
	exit;

?>