NP_RSSItem.php

RSSItem

<?
/*
    history:
        0.9 initial version (2003-06-24)
        0.91 added an image option
             when there is no update on the item, the xml file will be empty
             they can still get the rss, if there is an update, the new items will show up again
        0.92 use sql_table
        0.93 add language option
             fix FancyURL link <link>
             fix table create error
             add unInstall
             add supportsFeature
        0.93a fix description body format, some idea borrowed from NP_XMLTools
        0.93b minor cleanup
        0.94 add plugin dep check
        0.95 add getTableList()
 
        THIS PLUGIN REQUIRED NP_Trackback in order to work.
*/
 
class NP_RSSItem extends NucleusPlugin {
    function getName() {        return 'RSS Item';      }
    function getAuthor()  { return 'Appie Verschoor -=xiffy=- | Edmond Hui (admun)';    }
    function getURL() {         return 'http://xiffy.nl/weblog/'; }
    function getVersion() {     return '0.95'; }
    function getDescription() {
        return 'A Template and Skin plugin to create an RSS Link and export the item with all of it s comments as a RSS File, cooperates
with Trackback, might work without but untested';
    }
 
    function supportsFeature($what) {
        switch($what) {
            case 'SqlTablePrefix':
                 return 1;
            default:
                 return 0;
        }
    }
 
    function getPluginDep() {
        return array('NP_TrackBack');
    }
 
    function init() {
        $this->defaultlook = ($this->getOption('RSSLink'));
        $this->rssimage = ($this->getOption(RSSImage));
        $this->lang = ($this->getOption('lang'));
    }
 
    function install() {
        $this->createOption('lang',  'Language to use','text','en');
        $this->createOption('RSSLink',  'How does the rss link look like?','text','<img src="http://xiffy.nl/weblog/siteimg/xml.png" bord
er="0" alt="RSS Version of this item with comments" />');
        $this->createOption('RSSImage', 'What image should be displayed as your site logo?', 'text', 'http://somesite.com/nucleus/nucleus
2.gif');
        sql_query ("CREATE TABLE IF NOT EXISTS `".sql_table('plugin_rssitem')."`
                    ( `itemid` int(11),
                      `client` VARCHAR(60) NOT NULL,
                      `lastchecked` TIMESTAMP NOT NULL,
                      `lastupdate` TIMESTAMP NOT NULL,
                      KEY `item` (`itemid`),
                      KEY `client` (`client`))");
    }
 
    function getTableList()   { return array(sql_table("plugin_rssitem")); }
 
    function unInstall() {
         sql_query('DROP TABLE '.sql_table('plugin_rssitem'));
    }
 
    function doAction($type) {
        global $manager;
 
        $itemid         = intRequestVar('itemid');
        $item =& $manager->getItem($itemid,false,true);
        $blog =& $manager->getBlog(getBlogIDFromItemID($itemid));
 
        // should this user be unsubscribed yet?
        // Subscriber administration
        $client = $_SERVER['REMOTE_ADDR'];
        // get this client from the db
        $clientquery = "SELECT UNIX_TIMESTAMP(lastchecked) as lastchecked, "
                     . "       UNIX_TIMESTAMP(lastupdate)  as lastupdate "
                     . " FROM ".sql_table('plugin_rssitem')." AS rsscheck WHERE itemid = ". $itemid
                     . "  AND client = '".$client."'";
        $result = mysql_query($clientquery);
        if (mysql_num_rows($result) == 0) {
            // create a client reference
            $clientquery = "INSERT INTO ".sql_table('plugin_rssitem')." (itemid, client) VALUES (".$itemid.",'".$client."')";
            $lastchecked = time();
            mysql_query($clientquery);
        } else {
            $row = mysql_fetch_object($result);
            $lastchecked = $row->lastchecked;
        }
        // check if new comments or trackbacks have arrived
        // if not then we return a rather empty xml stating so ...
        $cr = sql_query("SELECT UNIX_TIMESTAMP(ctime) AS ct FROM ".sql_table('comment')." WHERE citem = $itemid ");
        if (mysql_num_rows($cr) > 0) {
            $lc = mysql_fetch_object($cr);
            $commenttime = time();
            $commentime = $lc->ct;
        } else { $commenttime = mktime(0,0,0,0,0,0); }
        // same for trackback
        $latesttrackbackresult = sql_query("SELECT MAX(timestamp) AS trackbacktime FROM ".sql_table('plugin_tb')." AS tb WHERE tb_id = $i
temid GROUP BY timestamp");
        if (mysql_num_rows($latesttrackbackresult) > 0) {
            $latesttrackback = mysql_fetch_object($latesttrackbackresult);
            $trackbacktime = time();
            $trackbacktime = $latesttrackback->trackbacktime;
        } else { $trackbacktime = mktime(0,0,0,0,0,0); }
 
        // whatever happens, register we've been here
        // (do we really have to? don't know for sure yet)
        $clientquery = "UPDATE ".sql_table('plugin_rssitem')." SET lastchecked = now() WHERE "
                     . "itemid = ". $itemid . " AND client = '".$client."'";
        mysql_query($clientquery);
 
        // check timestamp
        if ($lastchecked < $commenttime ||
            $lastchecked < $trackbacktime) {
           $sendRSS = false;
        } else {
            $sendRSS = true;
        }
 
        if ($sendRSS) {
            // initial hack: do it yourself :-(
            // I'd rather call xml-rss2.php?full=yes, but not evryone has XML_Tools installed ...
 
            // publish headers:
            $this->putHeader($item, $blog);
 
            // Now publish the item so that it gets on top initially ...
            $this->putItem($item, $blog);
            // now the comments ...
            $result = mysql_query("SELECT c.citem AS item, c.cbody AS body, c.cuser AS user, c.cmember AS member, c.cnumber AS commentid,
 cmail AS link
                                   FROM ".sql_table('comment')." c
                                   WHERE c.citem = $itemid
                                   ORDER BY c.cnumber ASC");
            while ($row = mysql_fetch_object($result)) {
                $this->putComment($row);
            }
            // and trackbacks ...
            $result = mysql_query("SELECT tb.tb_id as itemid, tb.url as url, tb.title as title, tb.excerpt as body, tb.blog_name as blogn
ame
                                   FROM ".sql_table('plugin_tb')." tb
                                   WHERE tb.tb_id = $itemid
                                   ORDER BY tb.timestamp");
            while ($row = mysql_fetch_object($result)) {
                $this->putTrackback($row);
            }
            $this->putEnd();
        } // if sendRss
        else
        {
            $this->putHeader($item, $blog);
            $this->putend();
        }
 
    }
 
    function doSkinVar($type, $what)
    {
        global $manager, $blogid;
    }
 
    function doTemplateVar(&$item)
    {
        global $manager, $blogid;
        $link = $this->getRSSLink($item->itemid);
        echo "<a href=\"".$link."\">".$this->defaultlook."</a>";
    }
 
    function getRSSLink($itemid)
    {   global $CONF;
        return $CONF['IndexURL'] . 'action.php?action=plugin&amp;name=RSSItem&amp;itemid='.$itemid;
    }
 
    function replaceImageCallback($matches)
    {
        global $CONF;
        return ('<img src="' . $CONF['MediaURL'] . 'RSSITEM_AUTHOR/' . $matches[1] . '" width="'. $matches[2] . '" height="' . $mat
ches[3] . '" alt="' . $matches[4] . '">');
    }
 
    function replaceMediaCallback($matches)
    {
        global $CONF;
        return ('<a href="' . $CONF['MediaURL'] . 'RSSITEM_AUTHOR/' . $matches[1] . '">');
    }
 
    function encode_xml(&$data, $iauthor = 'unknown')
    {
        $to_entities = get_html_translation_table(HTML_ENTITIES);
        $from_entities = array_flip($to_entities);
        $data = strtr($data,$from_entities);
        $data = strtr($data,$to_entities);
        $data = html_entity_decode($data);
        if ($iauthor != 'unknown') {
            // One bug.... the preg screw up if there are not 4 params in <%image%>
            $data = preg_replace_callback("#<\%popup\((.*?)\|(.*?)\|(.*?)\|(.*?)\)%\>#", array(&$this, 'replaceImageCallback'), $data);
            $data = preg_replace_callback("#<\%image\((.*?)\|(.*?)\|(.*?)\|(.*?)\)%\>#", array(&$this, 'replaceImageCallback'), $data);
            $data = preg_replace_callback("#<\%media\((.*?)\|(.*?)\%\>#", array(&$this, 'replaceMediaCallback'), $data);
            $data = str_replace("RSSITEM_AUTHOR", $iauthor, $data);
        }
        echo htmlentities($data);
    }
 
    // "put" functions:
    // create the xml, hardly any processing here (8(O) Doh!
    //
    // PutHeader: create the xml-tag and some channel info ...
    function putHeader($item, $blog)
    {   global $CONF, $manager;
 
        // a little hack to get a working permanent URL for the item
        if (!$CONF['ItemURL'])
                $CONF['ItemURL'] = $blog->getURL();
 
        header ("Content-type: text/xml");
        echo '<' . '?xml version="1.0" encoding="ISO-8859-1"?' . '>';
        ?>
<rss version="2.0">
<channel>
    <title><?echo $blog->getName()." - ".htmlspecialchars($item[title]); ?></title>
    <link><?echo createItemLink($item[itemid]);?></link>
    <description><?
        $this->encode_xml(shorten(strip_tags($item[body]),200,'..'), $item[authorid]);
    ?></description>
    <language><? echo $this->lang; ?></language>
    <image>
        <url><?echo $this->rssimage; ?></url>
        <title><?echo $item[title];?></title>
        <link><?echo createItemLink($item[itemid]);?></link>
    </image>
    <docs>http://backend.userland.com/rss</docs>
            <?
    } // putHeader
 
    // Make an RSS Item for each comment:
    // Todo, make a link if that's available; don't reveal the email address!
    function putComment($comment) {
        global $CONF, $blog;
        // a little hack to get a working permanent URL for the item
        if (!$CONF['ItemURL'])
                $CONF['ItemURL'] = $blog->getURL();
 
        if ($comment->member > 0) {
            $result = mysql_query("SELECT mname AS nick, murl AS link FROM ".sql_table('member')." WHERE mnumber = ".$comment->member);
            $member = mysql_fetch_object($result);
            $authorlink = "<a href=\"".$member->link."\">".$member->nick."</a>";
            $author = "Comments by: " .$member->nick;
        } else {
            $authorlink = "<a href=\"".$comment->link."\">".$comment->user."</a>";
            $author = "Comments by: " .$comment->user;
        }
        ?>
<item>
<title><? $this->encode_xml($author);?></title>
<link><? $link = $comment->item."#".$comment->commentid;
         $link = createItemLink($link);
         $this->encode_xml($link)?></link>
<description><? $data = $authorlink."<br />".$comment->body; $this->encode_xml($data); ?></description>
<pubDate></pubDate>
</item>
        <?
    } // put comment
 
    // putTrackback:
    // put the trackbacks for this item as am item
    function putTrackback($tb) {
        global $CONF;
        ?>
<item>
<title><? echo "Trackback: "; $this->encode_xml($tb->title);?></title>
<link><? echo $tb->url;?></link>
<description><? $this->encode_xml($tb->body); ?></description>
<pubDate></pubDate>
</item>
        <?
    } // putTrackback
 
    // putItem: display te item as an rss-item.
    function putItem($item, $blog) {
        global $CONF;
        // a little hack to get a working permanent URL for the item
        if (!$CONF['ItemURL'])
                $CONF['ItemURL'] = $blog->getURL();
 
        ?>
<item>
<title><?echo $blog->getName()." - ".$item[title]?></title>
<link><?echo createItemLink($item[itemid]);?></link>
<description><? $text = $item[body].$item[more]; $this->encode_xml($text, $item[authorid]); ?></description>
<pubDate></pubDate>
</item>
        <?
    } // putItem
 
    // putEnd: end the rss file ...
    function putEnd() {
        ?>
    </channel>
</rss>
        <?
    } // putEnd
 
}
 
?>

RSSItem

rssitemcode.txt · Last modified: 2007/05/07 16:34 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki