<?

    error_reporting(E_ALL);  

    define( '_VALID_MOS', 1 );  
    include("db.php");
    
    $db_host = "db1";
    $db_user = "devqj";
    $db_pass = "masterpass";
    $db_db   = "j_dash";
    
    $database = new database($db_host, $db_user, $db_pass, $db_db, ""); 

    if (isset($_GET['h'])) {
        $h= $_GET['h'];
    }
    
    if ($h < 1 or $h > 11) {
        $h = 1;
    }
    
    if ($h == 1) {
        $title = "The Dashhacks Network";
        $base = "http://www.dashhacks.com";
    }
    if ($h == 2) {
        $title = "Hack your Apple iPhone, iPad and iPod!";
        $base = "http://www.dashhacks.com/iphone.html";
    }
    if ($h == 3) {
        $title = "Kinect Hacks";
        $base = "http://www.dashhacks.com/kinect.html";
    }
    if ($h == 4) {
        $title = "PS3-Hacks :: Hack your PlayStation 3";
        $base = "http://www.dashhacks.com/ps3.html";
    }
    if ($h == 5) {
        $title = "PSP-Hacks: Hack your PlayStation Portable";
        $base = "http://www.dashhacks.com/psp.html";
    }
    if ($h == 6) {
        $title = "PS Vita-Hacks: Hack your PlayStation Vita";
        $base = "http://www.dashhacks.com/ps-vita.html";
    }  
    if ($h == 7) {
        $title = "Xbox360-Hacks :: Hack your Xbox 360";
        $base = "http://www.dashhacks.com/xbox-360.html";
    }   
    if ($h == 8) {
        $title = "Wii U-Hacks :: Hack your Wii U";
        $base = "http://www.dashhacks.com/wii-u.html";
    }  
    if ($h == 10) {
        $title = "PS4-Hacks :: Hack your PlayStation 4";
        $base = "http://www.dashhacks.com/ps4.html";
    }  
    if ($h == 11) {
        $title = "Xbox-One-Hacks :: Hack your Xbox One";
        $base = "http://www.dashhacks.com/xbox-one.html";
    }  

    dostart($title, $base);  
    $ptitle = $title;
    
    $dt = date("Y-m-d H:i:s");

    $sec = "";
    if ($h > 1) {
        $sec = "and sectionid = $h";
    }
    
    $dt = date('Y-m-d H:i:s', mktime(date("H")+4, date("i"), date("s"), date("m") , date("d"), date("Y"))); 
    
    $sql = "select * from jos_content where state=1 $sec and (publish_up <= '$dt' or publish_up = '0000-00-00 00:00:00') order by created desc, publish_up desc limit 20";
    //echo "SQL: $sql<BR>";
    $database->setQuery($sql);
    $rows = $database->loadObjectList();

    for ($a=0;$a<count($rows);$a++) {
        
        
        $row = $rows[$a];
        
        $longdate = $row->created;
        $title = $row->title;
        $desc = $row->introtext;
        
        $id  = $row->id;
        $cat = $row->catid;
        $sec = $row->sectionid;
        
        $alias = $row->alias;
        $alias = str_replace(".","",$alias);
        
        
        $sefchk = "index.php?option=com_content&catid=$cat&id=$id&view=article";
        $sql = "select * from jos_acesef_urls where url_real = '$sefchk'";
        $database->setQuery($sql);
        $srows = $database->loadObjectList();
        
        if (count($srows) > 0) {
            $srow=$srows[0];
            $moo = $srow->url_sef;
            $newurl = "http://www.dashhacks.com/" . $moo;
        }
        else {
        
        
            $sql = "select alias from jos_categories where id = $cat";
            $database->setQuery($sql);
            $catalias = $database->loadResult();
            
            $sql = "select alias from jos_sections where id = $sec";
            $database->setQuery($sql);
            $secalias = $database->loadResult();
            
            $newurl = "http://www.dashhacks.com/$secalias/$catalias/$alias.html";
        
        }
        
        
        
        $img = "/" . $row->images;
        
        $w = 200;
        $h = 142;        
        
        if ($img == "/") {
            $img = "/img/dh.png";
            $w = 180;
            $h = 180;
        }
        $img = "http://www.dashhacks.com" . $img;
        
        // pre-process teaser
        
        //echo "desc: $desc<BR><BR>";
        
        $title = strtr ($title,chr(10),chr(32));
        $title = str_replace(chr(180), "", $title);   
        $title = str_replace(chr(237), "", $title);
        $title = str_replace(chr(31), "",  $title);  
        
        $title = str_replace(chr(146), "", $title);
        $title = str_replace(chr(147), "", $title);
        $title = str_replace(chr(148), "", $title);  

        $desc = strtr ($desc,chr(10),chr(32));
        $desc = str_replace(chr(180), "", $desc);   
        $desc = str_replace(chr(237), "", $desc);
        $desc = str_replace(chr(31), "",  $desc);  
        
        $desc = str_replace(chr(146), "", $desc);
        $desc = str_replace(chr(147), "", $desc);
        $desc = str_replace(chr(148), "", $desc);   
        
        $desc = strip_tags($desc); // . $title;
    
        if (str_word_count($desc) > 35) {
            $desc = wordLimit($desc, 35);
        } 

        $link = $newurl;
        //$desc = "<a href='$link' ><img src='$img' width=$w height=$h align=right></a>" . $desc;
        

        doitem($title, $link, $longdate, $desc, "", "");     
        
        
        
    }






doend();



// ===============================================================================

function dostart($title, $baseurl) {

    echo '<?xml version="1.0" encoding="ISO-8859-1"?>';
    $longdate = date("D, d M Y H:i:s O");
    
?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
    <channel>
    
        <title><? echo $title; ?></title>
        <link><? echo $baseurl; ?></link>
        <description />
        <language>en</language>

        <lastBuildDate><? echo $longdate; ?></lastBuildDate>
        <generator>JamRSS</generator>
        <ttl>1</ttl>
        
<?

}        
  
// ===============================================================================    
        
function doitem($title, $link, $pubdate, $desc, $cat, $creator) {


    
    //$desc .= " <a href='$link'>Read Full Article</a>";
    
?>     
        
        <item>
            <title><![CDATA[<? echo $title; ?>]]></title>
            <link><? echo $link; ?></link>
            <pubDate><? echo $pubdate; ?></pubDate>
            <description><![CDATA[<? echo $desc; ?>]]></description>

            <guid isPermaLink="true"><? echo $link; ?></guid>
        </item>

<?

}        
        
// ===============================================================================          
        
function doend() {
            
?>        

    </channel>
</rss>      

<?

}

// ===============================================================================  
        
function wordLimit($text,$limit=20){
       
    $explode = explode(' ',$text);
    $ecnt = count($explode);
    
    $string  = '';
       
    $dots = '...';
    if(count($explode) <= $limit){
        $dots = '';
    }
    for($i=0;$i<$limit;$i++){
        if ($ecnt >= $i)  {
            $string .= $explode[$i]." ";
        }
    }
       
    return $string.$dots;

}

// ===============================================================================  

?>
