<?
require("/home/cubitnet/public_html/lost/forum/SSI.php");
header('Content-type: text/xml');
function all_ascii( $stringIn ){
    $final = '';
    $search = array(chr(145),chr(146),chr(147),chr(148),chr(150),chr(151));
    $replace = array("'","'",'"','"','-','-');

    $hold = str_replace($search[0],$replace[0],$stringIn);
    $hold = str_replace($search[1],$replace[1],$hold);
    $hold = str_replace($search[2],$replace[2],$hold);
    $hold = str_replace($search[3],$replace[3],$hold);
    $hold = str_replace($search[4],$replace[4],$hold);
    $hold = str_replace($search[5],$replace[5],$hold);

    if(!function_exists('str_split')){
       function str_split($string,$split_length=1){
           $count = strlen($string);
           if($split_length < 1){
               return false;
           } elseif($split_length > $count){
               return array($string);
           } else {
               $num = (int)ceil($count/$split_length);
               $ret = array();
               for($i=0;$i<$num;$i++){
                   $ret[] = substr($string,$i*$split_length,$split_length);
               }
               return $ret;
           }
       }
    }

    $holdarr = str_split($hold);
    foreach ($holdarr as $val) {
       if (ord($val) < 128) $final .= $val;
    }
    return $final;
}
?>
<? echo ('<?xml version="1.0" encoding="utf-8"?>'); ?>
<rss version="0.92" xml:lang="en-US">
<channel>
<title>Sledgeweb's Lost ... Stuff</title>
<description><![CDATA[Latest news and updates from SWLS, a fan site devoted to ABC's LOST]]></description>
<link>http://lost.cubit.net</link>
<copyright>Copyright <?=date("Y");?></copyright>
<?
include "../db.php"; //Include database access

// GET LATEST 20 INVESTIGATIONS AND NEWS ENTRIES COMBINED
$query = "(SELECT smf_gallery_pic.ID_PICTURE as ID, smf_gallery_pic.date as postDate, @type:=0 as ID_TOPIC, smf_gallery_pic.commenttotal as numReplies, smf_gallery_pic.thumbfilename, smf_gallery_pic.description, smf_gallery_pic.title, smf_gallery_pic.date FROM smf_gallery_pic ORDER BY postDate DESC LIMIT 20)
UNION
(SELECT smf_messages.ID_MSG as ID, smf_messages.posterTime as postDate, smf_topics.ID_TOPIC, smf_topics.numReplies, @type:=0 as thumbfilename, @type:=0 as description, @type:=0 as title, @type:=0 as date FROM smf_topics INNER JOIN smf_messages ON smf_messages.ID_MSG = smf_topics.ID_FIRST_MSG WHERE smf_topics.ID_BOARD = 20 ORDER BY postDate DESC LIMIT 20) order by postDate DESC";
$result = mysql_query($query);
$rows = mysql_num_rows($result);
// CYCLE THROUGH RESULTS
for($i=0; $i<$rows; $i++)
	{
	// Get ID and Number of Replies
	$articleId = mysql_result($result, $i, 'ID');
	// Process Item if News Story
	if (mysql_result($result, $i, 'ID_TOPIC') != 0)
		{
			$article = ssi_grabMessage($articleId, 'array');
			$viewUrl = "viewArticle.php";
			$splitarticle = explode("<br /><br />&nbsp;<br />", $article[0][body]);
			if ($splitarticle[1] != "") {  $article[0][body] = $splitarticle[0] . " <a href=\"" . $viewUrl . "?id=" . $articleId . "\">Continue Reading Article</a>"; }
			$body = $article[0][body];
			$title=$article[0][subject];
			$articleDate = mysql_result($result, $i, 'postDate');
		}
	// Process Item if Investigation Egg
	else if (mysql_result($result, $i, 'ID_TOPIC') == 0)
		{
			$viewUrl = "viewEgg.php";
			$description = mysql_result($result, $i, 'description');
			$pattern = '/\[url\=(.*)\](.*)\[\/url\]/';
			//Convert Links
			while (preg_match($pattern, $description, $matches)) {
				$bodyParts = explode("[", $matches[0]);
				$urlParts = explode("]", $bodyParts[1]);
				$url = explode("=", $urlParts[0]);
				$linktext = explode("=", $urlParts[1]);
				$descriptionLink = "<a href=\"$url[1]\">$linktext[0]</a>";
				$description = str_replace($matches[0], $descriptionLink, $description);
				}
		$body = "<a href=\"http://lost.cubit.net/".$viewUrl."?id=".$articleId."\"><img src=\"http://lost.cubit.net/forum/gallery/" . mysql_result($result, $i, 'thumbfilename') . "\"></a>" . $description;
		$title = mysql_result($result, $i, 'title');
		$articleDate = mysql_result($result, $i, 'postDate');
		}
?>
     <item>
        <title><![CDATA[<?=str_replace("&#039;", "'", str_replace('&quot;', '"', $title)); ?>]]></title>
		<link>http://lost.cubit.net/<?=$viewUrl;?>?id=<?=$articleId;?></link>
        <description><![CDATA[<?=all_ascii($body);?>]]></description>
		<comments>http://lost.cubit.net/<?=$viewUrl;?>?id=<?=$articleId;?>#comments</comments>
        <pubDate><?=strftime( "%a, %d %b %Y %T %Z" , $articleDate); ?></pubDate>
		<guid>http://lost.cubit.net/<?=$viewUrl;?>?id=<?=$articleId;?></guid>
     </item>  
<? } ?> 
</channel>
</rss>