<?php
/*
Project:     playRSSwriter: RSS 2.0 writer with images support
File:        playRSSwriter.php
Author:      hombrelobo <minilobo@gmail.com>
Version:     0.95

For the latest version of the writer, questions, help, comments,
etc., please visit:
http://wolfb.com/2006/01/playrsswriter-rss-20-feed-writer-with_01.html

What is playRSSwriter ?
When working on creating a rss feed for our site http://playlingerie.com,
we realized that all the existing php writers were either for rss 1.0 or for
rss 2.0 but without support for embeded images. So I created this code.
You can see it in action in:
http://feeds.feedburner.com/sexy-lingerie

Enjoy it !!

*************************************
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.

This work is hereby released into the Public Domain. To view a copy of
the public domain dedication, visit
http://creativecommons.org/licenses/publicdomain/ or send a letter to
Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
*************************************
*/

// First we start defining all the variables that we will insert in the
// xml code.

// Insert here the the title of your feed
$rss_title= "MarcaAcme.com";  
// Insert your site, in the format site.com
$rss_site= "http://www.marcaacme.com" ;
// Insert the description of your website
$rss_description= "Noticias Culturales de Nicaragua y Centroamerica";
// Applicable language of the feed. For spanish, change to "es"
$rss_language="es";                     
// Address of the logo file. Only need to put the file name
$rss_logo=$rss_site."images/logorss.gif";  
// the feed's author email
$emailadmin="penalba@gmail.com";   

// set the file's content type and character set
// this must be called before any output
header("Content-Type: text/xml;charset=iso-8859-1");

$phpversion = phpversion();

//set the beginning of the xml file
ECHO <<<END
<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss">
    <channel>
      <title>$rss_title</title>
      <link>http://$rss_site/$url_product</link>
      <description>$rss_description</description>
      <language>$rss_language-$rss_language</language>
      <docs>http://backend.userland.com/rss</docs>
      <generator>PHP/$phpversion</generator>
    <image>
<url>$rss_logo</url>
<title>$rss_site</title>
<link>http://$rss_site/</link>
</image>
END;

// Now the variables about the individual items to appear in the feed. Here we
// take them from a MySQL database, but can also come from a included file

// Define database

$server = "localhost";                  // Server
$rss_database = "ma_v2" ;            	// Database name
$db_user ="ma_acme" ;               	// Database user
$db_pass = "du" ;                       // Database password

// Open the database

$db = mysql_connect($server,$db_user,$db_pass);        
mysql_select_db($rss_database,$db);

// Calculate the number of records in the table "Products"

$num_rows = mysql_num_rows(mysql_query("select * from noticia"));  

// Select all the records from the table "Products". You can modify the
// query to only select some records, change the order, etc.

$query = "select * from noticia ORDER BY id DESC LIMIT 10" ;
$result = mysql_query($query) or die("Query failed") ;

//Make a loop to create the feed for all the items selected                                                

while ($results = mysql_fetch_array($result))
  {

// Pass the database field Photo_name to the variable //$photo_name                                                
// The Photo_name has to include the relative path,
// for instance: $photo_name="images/photo1.jpg";

$photo_name = $rss_site."/noticias/".$results['foto'];
$nofoto = "http://www.marcaacme.com/noticias/No Foto";
$logo = "http://www.marcaacme.com/images/logo.jpg";

if ($photo_name=='$nofoto') {$photo_name=='$logo';}

// Pass the record URL_product to the variable $url_product. It also
// has to include the relative path, like in: "path/product1.htm"

$url_product = "noticia.php?id=".$results['id'];

// Pass the record Description to the variable $description

$description = $results['contenido'];

// Clean the description

$description = str_replace ("&amp","",htmlspecialchars(stripslashes($description)));

// Pass tags to describe the product

$rss_tags = $results['tags'];     
$pubdate = $results['pubdate'];
  
// If you don't have tags for the product, simply change to a fixed value,
// for instance put:
// $rss_tags="tag1 tag2";

// Make a short description for titles of only 75 characters

$short_description = $results['titulo'].": ".$results['subtitulo'];

$counter ++ ;

// Calculate the size of the image and resize it to a thumbnail

$photo_size= getimagesize($photo_name) ;
$photo_width = $photo_size[0] ;
$photo_height = $photo_size[1] ;
unset($height_limit,$width_limit) ;

/*if ($photo_height > $photo_width )
	{$photo_height= "250" ;}
else{$photo_width = "250" ;}*/

$photo_width= "250" ;

// Create the html for the description item. No need to modify this unless
// you want to change the look

$content="$description<br /><br />
<img src=\"$photo_name\" /><br /><br />
$rss_site/$url_product<br /><br />
Etiquetas: $rss_tags<br /><br />" ;

// Escape all the descriptions

$content = preg_replace(array('/</', '/>/', '/"/'), array('&lt;', '&gt;', '&quot;'), $content);

// display an item

ECHO <<<END

<item>

<title>$short_description</title>

<link>$rss_site/$url_product</link>

<description>$content</description>

<author>$emailadmin</author>

<pubDate>$pubdate</pubDate>

<media:title>$description</media:title>
<media:text type="html">
$description\n\r
$rss_site\n\r
</media:text>

<media:category scheme="urn:$rss_site/etiquetas.php?clave=$rss_tags">$rss_tags</media:category>

</item>
END;

  }

// Close the database

mysql_close();

// And end the xml file

ECHO <<<END
   </channel>
</rss>
END;

// Done !!  Use http://feedvalidator.org to verify that it's correct.

?>