<?php
/**
* @copyright 68 Classifieds
*
* @author $Author: suzkaw $
* @version $Revision: 4 $
* @package 68Classifieds
* @link http://www.68classifieds.com
*
* @Updated: $Date: 2008-10-30 13:24:53 -0400 (Thu, 30 Oct 2008) $
*/
require_once('includes/init.php');
require_once(
FILESYSTEM_PATH .'includes/classes/kernel/Categories.php');
$Categories = new Categories;
//error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
    
error_reporting(E_ALL);
    
ini_set('display_errors'1);
    
$type = ( isset($_GET['type']) ) ? $_GET['type'] : 'new';
    
$limit = ( isset($_GET['limit']) ) ? (int)$_GET['limit'] : '15';
    
$where=" WHERE p.expiration > NOW() AND p.display = 'Y'";
    
    switch(
$type)
    {
        case 
"new":
            
$orderby " ORDER BY p.dateadded DESC";
        break;
        case 
"featured":
            
$orderby " ORDER BY p.dateadded DESC";
            
$where .= " AND p.featured='Y'";
        break;
        case 
"rating":
            
$orderby " ORDER BY totalscores DESC";
        break;
        case 
"top":
            
$orderby " ORDER BY p.hitcount DESC";
        break;
        case 
"rand":
            
$orderby " ORDER BY RAND()";
        default:
            
$orderby " ORDER BY p.dateadded DESC";
    }
    
//now setup the search query
    
$sSQL "SELECT p.id, p.owner, p.title, p.featured, p.section, p.shortDescription, p.description, p.price, SUM(score) AS totalscores FROM ".PREFIX."listings AS p LEFT JOIN ".PREFIX."rating AS r ON p.id = lid";
    
    
//sub categories
    
if(isset($_REQUEST['cat']))
    {
        
$slug=$_REQUEST['cat'];
        
$cat=$Categories->getCategoryBySlug($slug);    
        
$arr_childs = array((int)$cat['id']);
        
$Categories->get_ids($arr_childs);
        
$options['arr_childs']=$arr_childs;
        
$where .= ( empty($options['arr_childs']) ) ? ''" AND p.section IN ("implode(', '$options['arr_childs']) .")";
    }
    if(isset(
$_REQUEST['category']))
    {
        if(isset(
$_REQUEST['showchild']) && $_REQUEST['showchild'] == 'Y')
        {
            require_once(
FILESYSTEM_PATH .'includes/classes/kernel/Categories.php');
            
$Categories = new Categories;
            
$arr_childs = array((int)$_REQUEST['category']);
            
$Categories->get_ids($arr_childs);
            
$options['arr_childs']=$arr_childs;
            
$where .= ( empty($options['arr_childs']) ) ? ''" AND p.section IN ("implode(', '$options['arr_childs']) .")";
        }
        else
        {
            
$where .= ( isset($_REQUEST['category']) ) ? ' AND p.section = '.(int)$_REQUEST['category'] : '';
        }
    }
    
    
//any additional where statements
    
$where .= ( isset($_GET['state']) ) ? ' AND u.state = "'.mysql_real_escape_string($_GET['state']).'"' '';
    
$where .= ( isset($_GET['city']) ) ? ' AND u.city = "'.mysql_real_escape_string($_GET['city']).'"' '';
    
$where .= ( isset($_GET['country']) ) ? ' AND u.country = "'.mysql_real_escape_string($_GET['country']).'"' '';
    
$where .= ( isset($_GET['owner']) ) ? ' AND p.owner = '.(int)$_GET['owner'] : '';
    
    if (isset(
$_REQUEST['searchtext'])&& $_REQUEST['searchtext']<>"")
    {
        
$searchtext=trim(mysql_real_escape_string($_REQUEST['searchtext']));
        
$where .= " AND (p.title LIKE '%".$searchtext."%' OR p.description LIKE '%".$searchtext."%')";
    }
    
//search price//
    
$minprice=(int)trim(@$_REQUEST['minprice']);
    
$maxprice=(int)trim(@$_REQUEST['maxprice']);
    if(
$minprice == "")
        
$minprice=0;
    if(
$maxprice == "")
        
$maxprice=9999999;
    
$where .= " AND p.price BETWEEN "$minprice ." AND "$maxprice;
    
    
$where .= " GROUP BY p.id";
    
$sSQL $sSQL $where $orderby ." LIMIT "$limit."";
    
//echo $sSQL;die;
    
    
$ads = array();
    
$result $db->query($sSQL);
    while (
$rs=$result->fetch()) 
    { 
        
$ads[] = $rs;
    }
    
    
// set XML type and nocache headers
    
header('Content-Type: text/xml');
    
header('Expires: ' gmdate('D, d M Y H:i:s') . ' GMT');
    
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    
header('Pragma: public');
    
    
// print out the page header
    
echo '<?xml version="1.0" encoding="UTF-8"?>' "\r\n";
    echo 
'<rss version="2.0">' "\r\n";
    echo 
"<channel>\r\n";
    echo 
"\t<title>" html_entity_decode($title) . "</title>\r\n";
    echo 
"\t<link>".URL."</link>\r\n";
    echo 
"\t<description>" html_entity_decode($Core->settings['description']). "</description>\r\n";
    echo 
"\t<language>en</language>\r\n";
    echo 
"\t<generator>68 Classifieds</generator>\r\n";
    echo 
"\t<lastBuildDate>" gmdate('D, d M Y H:i:s') . " GMT</lastBuildDate>\r\n";
    echo 
"\t<pubDate>" gmdate('D, d M Y H:i:s') . " GMT</pubDate>\r\n";
      
   
/*

    
    
    //echo '<rss version="2.0">' . "\r\n";
    echo '<rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0">'."\r\n";
    echo "<channel>\r\n";
    echo "\t<title>" . html_entity_decode($title) . "</title>\r\n";
    echo "\t<link>".URL."</link>\r\n";
    echo "\t<description>" . html_entity_decode($Core->settings['description']). "</description>\r\n";
    echo "\t<docs>http://backend.userland.com/rss092</docs>\r\n";
    echo "\t<language>en</language>\r\n";
    echo "\t<generator>68 Classifieds</generator>\r\n";
    echo "\t<lastBuildDate>" . gmdate('D, d M Y H:i:s') . " GMT</lastBuildDate>\r\n";
    echo "\t<pubDate>" . gmdate('D, d M Y H:i:s') . " GMT</pubDate>\r\n";
    */
    // list returned threads
    
if (!empty($ads))
    {
        foreach (
$ads as $listing)
        {
            
//item info
            
if($listing['price'] == 0)
            {
                
$price '';
            }
            else
            {
                
$price FormatCurrency($listing['price']);
            }
            
            if(
$price == '')
            {
                
$seperator "";
            }
            else
            {
                
$seperator ' - ';
            }
            
$string=html_entity_decode(strtolower($listing['title']));
            
$string=str_replace(' ''_'$string);
            
$string=preg_replace('/\W/e'''$string);
            if(
SEO_STRING == 'dash') {
                
$string=str_replace('_''-'$string);
            }
            echo 
"\t\t".'<item>'."\r\n";
            echo 
"\t\t\t".'<title>'$listing['title'].'</title>'."\r\n";
            echo 
"\t\t\t".'<guid>'.URL ."/viewlisting.php?view=".$listing['id'].'-'.$string.'</guid>'."\r\n";
            echo 
"\t\t\t"."<link>"URL ."/viewlisting.php?view=".$listing['id'].'-'.$string."</link>"."\r\n";
            
            
$timestamp =  strtotime($listing['dateadded']); 
            
$pubDate gmdate('D, d M Y H:i:s'$timestamp).' GMT';
            echo 
"\t\t\t".'<pubDate>'$pubDate .'</pubDate>'."\r\n";
            
            
//get images
            
$isSQL sprintf("SELECT pid,image FROM ".PREFIX."prodimages WHERE pid = %s ORDER BY rank ASC LIMIT 1"$listing['id']);
            
$iresult $db->query($isSQL);
            
$irs=$iresult->fetch();
            if(
$iresult->size() == 1)
            {
               
$imDescription "<a href="URL ."/viewlisting.php?view=".$listing['id']."><img src='"URL "/thumbs/small_" $irs['image'] . "' align='left' style='margin: 0 5px 5px 0;' border='0'  /></a>".$listing['description'].'<br clear="all" />';
            }
            else
            {
                
$imDescription "<a href="URL ."/viewlisting.php?view=".$listing['id']."><img src='"URL ."/images/nophoto.gif' align='left'  style='margin: 0 5px 5px 0;' border='0'  /></a>".$listing['description'].'<br clear="all" />';
            } 
            echo 
"\t\t\t""<description>"htmlspecialchars($imDescription) . "</description>"."\r\n";
            
            
//close the item
            
echo "\t\t".'</item>'."\r\n";
        }
    }
    
    
//close the feed
    
echo "</channel>\r\n";
    echo 
"</rss>";

/* End of file external.php */
/* Location: ./upload/external.php */