1 (edited by 2009-10-05 17:47:15)

Topic: [HACK] import RSS feed in CuteNews

Name: Import RSS feed in CuteNews
Author: FUNimations
CuteNews Compatibility: 1.3.6 - * (no incompatibility reported yet)
Description: Reads an rss feed and imports the news items into CuteNews. Feeds are saved in CuteNews, any changes made to the original post won't be noticed by CuteNews!! This hack makes use of a public script. The license agreement etc. can be found in one of the files you'll have to make.
The feed will be loaded each time you visit the Cn panels main page.
Requirements: RSS2.0 feed
Instructions:
open shows.inc.php find

$output = str_replace("[link]","[url=]", $output);

add above

if( substr($news_arr[4], 0, 5) == "<RSS>" )
        $output = str_replace("[link]","<a target=\"_blank\" href=\"".substr($news_arr[4], 5)."\">", $output);
        else


find 2 times

if($config_comments_popup == "yes"){
                        $output = str_replace("[com-link]","<a href=\"#\" onclick=\"window.open('$config_http_script_dir/show_news.php?subaction=showcomments&template=$template&id=$news_arr[0]&archive=$archive&start_from=$my_start_from&ucat=$news_arr[6]', '_News', '$config_comments_popup_string');return false;\">", $output);

   
replace with

if( substr($news_arr[4], 0, 5) == "<RSS>" )
$output = preg_replace("'\\[com-link\\].*?\\[/com-link\\]'si","<!--rss feed can't be commented-->", $output);
elseif($config_comments_popup == "yes"){
                        $output = str_replace("[com-link]","<a href=\"#\" onclick=\"window.open('$config_http_script_dir/show_news.php?subaction=showcomments&template=$template&id=$news_arr[0]&archive=$archive&start_from=$my_start_from&ucat=$news_arr[6]', '_News', '$config_comments_popup_string');return false;\">", $output);


open functions.inc.php and find

///////////////////////////////////////////////////////
// Function:         formatsize
// Description: Format the size of given file

add above

///////////////////////////////////////////////////////
// Function:         ResynchronizeRSS
// Description:      Auto-Adds rss
function AutoSyncRSS()
{
    require_once './inc/rssReader.php'; // include library
    include './inc/rssImportFunction.mdu';
}


open the CuteNews index.php and find

 if($mod == ""){ require("./inc/main.mdu"); }

replace with

if($mod == ""){ require("./inc/main.mdu");AutoSyncRSS(); }


Make a new file called rss_update.db.php, put it in the data folder and chmodd to 777.
make a new file called rssReader.php and, put it in the inc folder. Add the following code to the file<div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'><?php
/**
* Rss Feed Reader
*
* After two years, I decided to rewrite and fix this
* class. Thanks to all of you who sent me bug reports
* and sorry that I didn't replied. I'm lazy when it
* comes to talking.
*
* Changelog v2.1:
* - added Text parser support. Got this idea today
* while working on something else. Maybe help in
* some cases where XML and SimpleXML fails. To be
* honest, it's not really the finest solution but
* it works and I was too tired to think more about
* it...
*
* Changelog v2.0:
* - new version, hopefully without stupid bugs
* if left in first version http://cutephp.com/forum/style_emoticons/default/smile.gif
* - added SimpleXML parser support
*
* Bugs:
*     - Opening local files won't work for some reason,
*     probably because of headers or something. Using
* SXML or TXT instead of XML may help.
*
* Issues:
* - Because of some PHP configurations deny to open
* remote files with fopen(), I used file_get_contents()
* but you still can switch to fopen(). See code for
* more informations.
*
* Usage:
* - See example.php
*
* Notes:
* - If you study this code a little, you can see that
* SXML is just a little wrapper around very few lines
* of code. SimpleXML is really nice and simple way to
* parse XML, but it has problems with special chars
* entities.
* - Even that this class is released under GPL licence,
* I'm not responsible for anything that happens to you
* (e.g. you die by reading these dull lines of text)
* or your computer/website/whatever.
*
* Copyright 2007-2009, Daniel Tlach
*
* Licensed under GNU GPL
*
* @copyright        Copyright 2007-2009, Daniel Tlach
* @link            <a href="http://www.danaketh.com" target="_blank">http://www.danaketh.com[/url]
* @version            2.1
* @license            http://www.gnu.org/licenses/gpl.txt
*/
class Rss
{

    private $parser;
    private $feed_url;
    private $item;
    private $tag;
    private $output;
    private $counter = 0;

    private $title = NULL;
    private $description = NULL;
    private $link = NULL;
    private $pubDate = NULL;

    // {{{ construct
    /**
    * Constructor
    *
    * @author        Daniel Tlach <mail@danaketh.com>
    */
    function __construct(  )
    {
    }
    // }}}

    // {{{ getFeed
    /**
    * Get RSS feed from given URL, parse it and return
    * as classic array. You can switch between XML
    * and SimpleXML method of reading.
    *
    * @author        Daniel Tlach <mail@danaketh.com>
    * @access         public
    * @param        string $url Feed URL
    * @param        constant $method Reading method
    */
    public function getFeed($url, $method = 'XML')
    {
        $this->counter = 0;
        switch($method)    {
            case 'TXT':
                return $this->txtParser($url);
                break;
            case 'SXML':
                return $this->sXmlParser($url);
                break;
            default:
            case 'XML':
                return $this->xmlParser($url);
                break;
        }
    }
    // }}}

    // {{{ sXmlParser
    /**
    * Parser for the SimpleXML way.
    *
    * @author        Daniel Tlach <mail@danaketh.com>
    * @access         private
    * @param        string $url Feed URL
    * @return        array $feed Array of items
    */
    private function sXmlParser($url)
    {
        $xml = simplexml_load_file($url);
        foreach($xml->channel->item as $item)    {
            $this->output[$this->counter]['title'] = $item->title;
            $this->output[$this->counter]['description'] = $item->description;
            $this->output[$this->counter]['link'] = $item->link;
            $this->output[$this->counter]['date'] = $item->pubDate;
            $this->counter++;
        }

        return $this->output;
    }
    // }}}

    // {{{ xmlParser
    /**
    * Parser for the XML way.
    *
    * @author        Daniel Tlach <mail@danaketh.com>
    * @access         private
    * @param        string $url Feed URL
    * @return        array $feed Array of items
    */
    private function xmlParser($url)
    {
        $this->parser = xml_parser_create();
        xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);
        $this->feed_url = $url;
        xml_set_object($this->parser,&$this);
        xml_set_element_handler($this->parser, "xmlStartElement", "xmlEndElement");
        xml_set_character_data_handler($this->parser, "xmlCharacterData");

        $this->xmlOpenFeed();

        return $this->output;
    }
    // }}}

    // {{{ txtParser
    /**
    * Parser for the Text way.
    *
    * @author        Daniel Tlach <mail@danaketh.com>
    * @access         private
    * @param        string $url Feed URL
    * @return        array $feed Array of items
    */
    private function txtParser($url)
    {
        //* Comment following lines
        $feed = file_get_contents($url);
        /*/
        /* And uncomment these
        $feed = '';
        $fh = fopen($url, 'r');
        while(!feof($fh))    {
            $feed .= fread($fh, 4096);
        } // while
        */

        $this->txtParseFeed($feed);

        return $this->output;
    }
    // }}}

    // {{{ xmlOpenFeed
    /**
    * Parser for the XML way.
    * I used file_get_contents() as a method to get
    * content of the feed, because I found that some
    * have denied opening remote files with fopen().
    * Tho you still can try switch functions if you
    * like...
    *
    * @author        Daniel Tlach <mail@danaketh.com>
    * @access         private
    * @return        void
    */
    private function xmlOpenFeed()
    {
        //* Comment following lines
        $feed = file_get_contents($this->feed_url);
        xml_parse($this->parser, $feed, TRUE);
        /*/
        /* And uncomment these
        $fh = fopen($this->feed_url, 'r');
        while($feed = fread($fh, 4096))    {
            xml_parse($this->parser, $feed, feof($fh));
        } // while
        */
        xml_parser_free($this->parser);
    }
    // }}}

    // {{{ xmlStartElement
    /**
    * Item start
    *
    * @author        Daniel Tlach <mail@danaketh.com>
    * @access         private
    * @param        object $parser Parser reference
    * @param        string $tag Tag
    * @return        void
    */
    private function xmlStartElement($parser, $tag)
    {
        if ($this->item === TRUE)    {
            $this->tag = $tag;
        }
        else if ($tag === "ITEM")    {
            $this->item = TRUE;
        }
    }
    // }}}

    // {{{ xmlCharacterElement
    /**
    * Item content
    *
    * @author        Daniel Tlach <mail@danaketh.com>
    * @access         private
    * @param        object $parser Parser reference
    * @param        string $data Content data
    * @return        void
    */
    private function xmlCharacterData($parser, $data)
    {
        if ($this->item === TRUE)    {
            // read the content tags
            switch ($this->tag)    {
                case "TITLE":
                    $this->title .= $data;
                    break;
                case "DESCRIPTION":
                    $this->description .= $data;
                    break;
                case "LINK":
                    $this->link .= $data;
                    break;
                case "PUBDATE":
                    $this->pubDate .= $data;
                    break;
            }
        }
    }
    // }}}

    // {{{ xmlEndElement
    /**
    * Item end
    *
    * @author        Daniel Tlach <mail@danaketh.com>
    * @access         private
    * @param        object $parser Parser reference
    * @param        string $tag Tag
    * @return        void
    */
    function xmlEndElement($parser, $tag)
    {
        if ($tag == 'ITEM')    {
            $this->output[$this->counter]['title'] = trim($this->title);
            $this->output[$this->counter]['description'] = trim($this->description);
            $this->output[$this->counter]['link'] = trim($this->link);
            $this->output[$this->counter]['date'] = trim($this->pubDate);
            $this->counter++;
            $this->title = NULL;
            $this->description = NULL;
            $this->link = NULL;
            $this->pubDate = NULL;
            $this->item = FALSE;
        }
    }
    // }}}

    // {{{ txtParseFeed
    /**
    * Parse feed using regexp
    *
    * @author        Daniel Tlach <mail@danaketh.com>
    * @access         private
    * @param        string $feed Feed string
    * @return        void
    */
    private function txtParseFeed($feed)
    {
        $feed = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $feed);
        $feed = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $feed);
        preg_match_all('|<item>(.*)</item>|U', $feed, $m);
        foreach($m[1] as $item)    {
            preg_match('|<title>(.*)</title>|U', $item, $title);
            preg_match('|<link>(.*)</link>|U', $item, $link);
            preg_match('|<description>(.*)</description>|U', $item, $description);
            preg_match('|<pubDate>(.*)</pubDate>|U', $item, $pubdate);
            $this->output[$this->counter]['title'] = $title[1];
            $this->output[$this->counter]['description'] = $description[1];
            $this->output[$this->counter]['link'] = $link[1];
            $this->output[$this->counter]['date'] = $pubdate[1];
            $this->counter++;
        }
    }
    // }}}

    // {{{ destruct
    /**
    * Destructor
    *
    * @author        Daniel Tlach <mail@danaketh.com>
    */
    function __destruct()
    {
    }
    // }}}

}

?></div></div>
make a file called rssImportFunction.mdu, put it in the inc folder. Add the following code to the file
<div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'><?php
//#########CONFIGURATION##########################################################
//category to post the feed in. May be multiple, seperated by a ','
$category = '';
//author name
$author = 'RSS FEED';
//Feed url
$feed_url = '';
//Avatar url for feed author
$avatar_url='';
//################################################################################
//#########DO NOT EDIT BELOW##############################################################
require_once './inc/rssReader.php'; // include library
$Rss = new Rss; // create object
$feed = $Rss->getFeed($feed_url);
$first_feed = false;
$output = '';

$loaded_rss = file("./data/rss_update.db.php");
$all_db = file("./data/news.txt");
$news_file = fopen("./data/news.txt", "w");
rsort( $feed );
foreach($feed as $item)
{
    $item[date] = strtotime($item[date]);
    if(!$first_feed) {$first_feed = $item[date];}
    if( $item[date] <= (int)$loaded_rss[0] ) continue;

    foreach($all_db as $news_line){
            $news_arr = explode("|", $news_line);
            if($news_arr[0] == $item[date]){ $item[date]--; }
    }
    reset($all_db);


    $output .= "$item[date]|$author|$item[title]|$item[description]|<RSS>$item[link]|$avatar|$category||\n";
}
$news_file = fopen("./data/news.txt", "w");
fwrite($news_file, $output );
foreach ($all_db as $line){ fwrite($news_file, "$line");}
fclose($news_file);
$fp = @fopen("./data/rss_update.db.php", "w");
@flock ($fp,2);
@fwrite($fp, $first_feed);
@flock ($fp,3);
@fclose($fp);
?></div>Don't forget the configuration section in this file