<?PHP
//=========================================
/*
Version: RSS Generator v2.0
URL: http://www.nichesinabox.com

Function : 
Automatically generates a feed when a page is loaded
using articles located in the "datas/articles"
You can alter the number of articles used to generate the feed
as well as the size of description used in the item description

Also, it adds an keyword anchor text linking back to a given article
using the permalink feature : you can create keyword relevant backlink
this is a powerful RSS Generator. you can even syndicate your own feed on your site. 

Author: Jean-Philippe Schoeffel
Author URI: http://www.nichesinabox.com/

Copyright: Copyright (c) 2006 JP Schoeffel. All rights reserved.
This not Open-Source nor free, and is an included
companion to my sites. Only by purchasing
directly from http://www.nichesinabox.com, adsensefiresale.com entitles you to use the code
on your own Domains; However, you are not allowed to reverse engineer,
sell, or distribute this code to others.

If you have received these scripts without purchase please
report violation to ethiccash@gmail.com A free license
will be granted and your identity will remain anonymous.

*/
//============================================

 $no_of_articles=12; //No of articles which you want to put in feed.xml ;     
	$path = './datas/articles/';  //folder in which the articles text files are kept
	
	$size =400; //Size of description in chartectors . 
  

//Do not edit anything below this line.  
   
   $site = "http://"; 
   $site .= $_SERVER["HTTP_HOST"]; 
   
   $dh = @opendir($path);



   while (false !== ($file=readdir($dh)))

   {


       if (substr($file,0,1)!="." && $file!="feed.rss" && strtolower(strrev(substr(strrev($file),0,strpos(strrev($file),"."))))=='txt')
        $files[]=$file;  #2-D array

   }

  closedir($dh);
  shuffle($files);
  $count=count($files);
  //echo($count);
  if($count<$no_of_articles)
 {
 $no_of_articles=$count;
 //echo($no_of_articles);
 //die();
 }

if($files)
{
$xml="<?xml version='1.0' ?>
 
<rss version='2.0'>

<channel>

<title>RSS feed for ".$mysitename."</title>

<link>".$rooturl."sitemap.php </link>

<description> RSS feed for ".$mysitename."</description>

<language>en-us</language>

";

  
  for($i=0;$i<$no_of_articles;$i++)
  {
   
    $con=file($path.$files[$i]);
	
    $row=count($con);
    $title=$con[0];
	//echo($title);
	//$title=html2txt($title);
	//$title=htmlspecialchars($title);
	$title=html2txt($title);
 //$title=GetXmlString($title);
 $title=stripslashes($title);
 $title= strip_htmlentities($title);  
 $title=str_replace('&',' ',$title);          
 $title=str_replace('`',' ',$title);
 $title=str_replace('’',' ',$title);
   $title=str_replace('“',' ',$title);
   $title=str_replace('”',' ',$title);
   $title=str_replace('.',' ',$title);
 
 /*
   $title=htmlspecialchars($title);
   $title=str_replace('.',' ',$title);
   $title=str_replace('…',' ',$title);
   $title=str_replace('&',' ',$title);
   $title=str_replace('–',' ',$title);
   $title=str_replace(',',' ',$title);
	*/
	$description="";
	//echo($title."<br>");
	for ($j=2;$j<$row;$j++)
	{
    $description.=$con[$j];
	}
	
	#XML starts from here 
$xml.='<item>

';


    $xml.='<title> '.str_replace('_',' ',substr($title,0,-2)).' </title>
	';

   
	$xml.="<link> ".$rooturl."permalink.php?article=".$files[$i].' </link>
	
	';
	
if(strlen($description)<=$size)
{
$description=$description;
}
else
{

$description=substr($description, 0, $size);

$lnk="..... <a href='".$rooturl."permalink.php?article=".$files[$i]."'>More on ".$keyword."</a>";
    
 
}
    //echo($description."<br>");
 $description=html2txt($description);
 $description=GetXmlString($description);
 $description=stripslashes($description);
 $description= strip_htmlentities($description);            
 $description.=$lnk;
 $description=htmlspecialchars($description);
	$xml.='<description> '.$description.'</description>

';

    $xml.='</item>

';

}    

    

$xml.='</channel>

</rss>';

$fh=fopen('feed.xml','w+');

fwrite($fh,$xml);

fclose($fh);

}
  function html2txt($document){
$search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
               '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
               '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
               '@<![\s\S]*?--,[ \t\n\r]*>@'        // Strip multi-line comments including CDATA
);
$text = preg_replace($search, '', $document);
return $text;
}
  function GetXmlString($d)
  {
  $d=htmlspecialchars($d);
  $d=strip_htmlentities($d);
  $d=stripslashes($d);
  //$d=str_replace('?',' ',$d); 
   $d=str_replace('’',' ',$d);
   $d=str_replace('“',' ',$d);
   $d=str_replace('”',' ',$d);
   return $d;
  }
  function strip_htmlentities($text)
{
        
//    '&ndash;'  => '-',      // '-'
		
 //   '&nbsp;'   => CHR( 32), // ' '
	
	$html2iso = array(

        '&ndash;'  => '-',      // '-'
		
		'&#150;'  => '–',
		
       //'&nbsp;'   => CHR( 32), // ' '
	
		'&lt;'  => '<',
		
	    '&gt;'  => '>',
	 
		'&#128;'=> "€", // '"€'

        '&#150;'  => '–',      // '-–'

        '&#8217;'   => "'", // '''

        '&#145;'   => "'", // '''

        '&#146;'   => "'", // '''

        '&#147;'   => '"', // '"'

        '&#148;'   => '"', // '"'

        '&#133;' => CHR(133), //…

        '&quot;'   => CHR( 34), // '"'

        '&iexcl;'  => CHR(161), // '¡'

        '&cent;'   => CHR(162), // '¢'

        '&pound;'  => CHR(163), // '£'

        '&curren;' => CHR(164), // '¤'

        '&yen;'    => CHR(165), // '¥'

        '&brvbar;' => CHR(166), // '¦'

        '&sect;'   => CHR(167), // '§'

        '&uml;'    => CHR(168), // '¨'

        '&copy;'   => CHR(169), // '©'

        '&ordf;'   => CHR(170), // 'ª'

        '&laquo;'  => CHR(171), // '«'

        '&not;'    => CHR(172), // '¬'

        '&shy;'    => CHR(173), // '­'

        '&reg;'    => CHR(174), // '®'

        '&macr;'   => CHR(175), // '¯'

        '&deg;'    => CHR(176), // '°'

        '&plusmn;' => CHR(177), // '±'

        '&sup2;'   => CHR(178), // '²'

        '&sup3;'   => CHR(179), // '³'

        '&acute;'  => CHR(180), // '´'

        '&micro;'  => CHR(181), // 'µ'

        '&para;'   => CHR(182), // '¶'

        '&middot;' => CHR(183), // '·'

        '&cedil;'  => CHR(184), // '¸'

        '&sup1;'   => CHR(185), // '¹'

        '&ordm;'   => CHR(186), // 'º'

        '&raquo;'  => CHR(187), // '»'

        '&frac14;' => CHR(188), // '¼'

        '&frac12;' => CHR(189), // '½'

        '&frac34;' => CHR(190), // '¾'

        '&iquest;' => CHR(191), // '¿'

        '&Agrave;' => CHR(192), // 'À'

        '&Aacute;' => CHR(193), // 'Á'

        '&Acirc;'  => CHR(194), // 'Â'

        '&Atilde;' => CHR(195), // 'Ã'

        '&Auml;'   => CHR(196), // 'Ä'

        '&Aring;'  => CHR(197), // 'Å'

        '&AElig;'  => CHR(198), // 'Æ'

        '&Ccedil;' => CHR(199), // 'Ç'

        '&Egrave;' => CHR(200), // 'È'

        '&Eacute;' => CHR(201), // 'É'

        '&Ecirc;'  => CHR(202), // 'Ê'

        '&Euml;'   => CHR(203), // 'Ë'

        '&Igrave;' => CHR(204), // 'Ì'

        '&Iacute;' => CHR(205), // 'Í'

        '&Icirc;'  => CHR(206), // 'Î'

        '&Iuml;'   => CHR(207), // 'Ï'

        '&ETH;'    => CHR(208), // 'Ð'

        '&Ntilde;' => CHR(209), // 'Ñ'

        '&Ograve;' => CHR(210), // 'Ò'

        '&Oacute;' => CHR(211), // 'Ó'

        '&Ocirc;'  => CHR(212), // 'Ô'

        '&Otilde;' => CHR(213), // 'Õ'

        '&Ouml;'   => CHR(214), // 'Ö'

        '&times;'  => CHR(215), // '×'

        '&Oslash;' => CHR(216), // 'Ø'

        '&Ugrave;' => CHR(217), // 'Ù'

        '&Uacute;' => CHR(218), // 'Ú'

        '&Ucirc;'  => CHR(219), // 'Û'

        '&Uuml;'   => CHR(220), // 'Ü'

        '&Yacute;' => CHR(221), // 'Ý'

        '&THORN;'  => CHR(222), // 'Þ'

        '&szlig;'  => CHR(223), // 'ß'

        '&agrave;' => CHR(224), // 'à'

        '&aacute;' => CHR(225), // 'á'

        '&acirc;'  => CHR(226), // 'â'

        '&atilde;' => CHR(227), // 'ã'

        '&auml;'   => CHR(228), // 'ä'

        '&aring;'  => CHR(229), // 'å'

        '&aelig;'  => CHR(230), // 'æ'

        '&ccedil;' => CHR(231), // 'ç'

        '&egrave;' => CHR(232), // 'è'

        '&eacute;' => CHR(233), // 'é'

        '&ecirc;'  => CHR(234), // 'ê'

        '&euml;'   => CHR(235), // 'ë'

        '&igrave;' => CHR(236), // 'ì'

        '&iacute;' => CHR(237), // 'í'

        '&icirc;'  => CHR(238), // 'î'

        '&iuml;'   => CHR(239), // 'ï'

        '&eth;'    => CHR(240), // 'ð'

        '&ntilde;' => CHR(241), // 'ñ'

        '&ograve;' => CHR(242), // 'ò'

        '&oacute;' => CHR(243), // 'ó'

        '&ocirc;'  => CHR(244), // 'ô'

        '&otilde;' => CHR(245), // 'õ'

        '&ouml;'   => CHR(246), // 'ö'

        '&divide;' => CHR(247), // '÷'

        '&oslash;' => CHR(248), // 'ø'

        '&ugrave;' => CHR(249), // 'ù'

        '&uacute;' => CHR(250), // 'ú'

        '&ucirc;'  => CHR(251), // 'û'

        '&uuml;'   => CHR(252), // 'ü'

        '&yacute;' => CHR(253), // 'ý'

        '&thorn;'  => CHR(254), // 'þ'

        '&yuml;'   => CHR(255)  // 'ÿ'

    );



    reset($html2iso); unset($entities); unset($isochars);

    while( list($key, $value) = each($html2iso) )

    {

       $entities[] = "/$key/";



      // replacement

      $isochars[] = $value;



      $text = str_replace( $value,$key, $text);

    }



    return $text;

}

// end html  function 
?>