| home / scripts / sidebar | [previous] [next] |
|
Our Perl code is just short of 350 lines long and has a bunch of comments, so we won't display the whole listing here (feel free to download the zipped package and have a look for yourself, though). We will however touch on some key points and discuss a few code snippets. The basic task of the Perl code is as follows:
Several global variables are defined near the top of the feedtab script:
$maxInArrays will be the maximum number of links that will be displayed
(rotated) within a single page. Generally you will set this to be a pretty high
number; of course, a higher number here means more bytes for the user.$thisURL is the URL of the actual feedtab.cgi script.$home is typically your document root. It will be prepended to the
feed locations requested by the user, so that the user doesn't have to see your
directory structure in their links. For example, if you have two feeds that are located in
/websites/mysite/mydocroot/myfeeddir1/feed.rss and
/websites/mysite/mydocroot/myfeeddir2/feed.rss, the home directory could
be set to /websites/mysite/mydocroot/, and the two feed locations would
be set to myfeeddir1/feed.rss and myfeeddir2/feed.rss. In this
way, when the user views the link used to get to a particular feed, they would see
http://yourdomain.com/cgi-bin/feedtab.cgi?feed=myfeeddir1/feed.rss instead
of the full path to the selected RSS file.%defHash is a hash of default data entries to be used in feeds. By assigning
values here, you can provide default values for key display parameters, while still allowing
each variable to be overridden by the specific feed, if necessary. The defaults currently
available as of this writing are:dark = color for the outer (dark) divlight = color for the inner (light) divbg = color for the page backgroundli, al, vl = link, active link, and visited link colorshbg = hover background colortotlinks = total number of links to display at a timetemplate_all = HTML template to use for master feed listingtemplate_one = HTML template to use for a single feed listingli, al, and vl are not used.Retrieving your feed listing is the purpose of the &getFeeds function; it is discussed
next.
sub getFeeds {
#
# Example initial feed array. Modify/expand this; or
# retrieve the structure via your own mechanism (DB, external
# configuration file, etc.)
#
my @NEWSFEEDS = (
{
location => 'webreference.rdf',
title => 'WebReference.com',
shortTitle => 'WebReference Features',
dark => '#fc0',
light => '#ffed9a',
bg => '#fff',
hbg => '#fd3',
},
{
location => 'js/tips/channels/last1512.rdf',
title => 'JavaScript Tip of the Day',
dark => '#fc6',
light => '#ffd',
bg => '#fff',
hbg => '#fd3',
},
);
my %retHash=();
foreach my $feed (@NEWSFEEDS) {
my %feedHash=();
# assign all properties from provided feeds
foreach my $key (keys %$feed) {
$feedHash{$key} =$feed->{$key};
}
# check for/assign default
# info if not initially present
foreach my $key (keys %defHash) {
$feedHash{$key} = $defHash{$key} unless (defined($feedHash{$key}));
}
$retHash{$feed->{'location'}}=\%feedHash;
}
return %retHash;
}
#
# end sub getFeeds
###############################################
We left &getFeeds purposely fairly bare, since how you retrieve your
feeds for listing will depend entirely on what structure you have them in already.
In the worse case, you can simply modify and extend the hashes in the @NEWSFEEDS
array to accommodate all of your RSS feed listings. The location and
title fields are the only required fields; all others will serve as overrides
for the defaults for this particular feed (any of the defaults discussed in the earlier
%defHash discussion can be used). The shortTitle field, in
particular, can be used as a specifically formatted feed title in the event that you
don't like the title included within the selected RSS file (which will otherwise be
used as the default).
Two other main routines are used in the Perl code; depending on whether the
user has chosen to view a specific feed or to view the feed listing for further
selection. Since both routines are very similar, we'll focus on a few details of the
specific feed display and leave the &showAll routine for your
own examination. As a security precaution, feedtab.cgi checks to see
that any feed requested by the user matches a feed as listed in the
&getFeeds() hash. This prevents a malicous end user from crafting a
feedtab request that will attempt to read other files on our system; i.e., only
those feeds that we explicitly allow may be read using feedtab. All other requests
are shuttled off to the main feed display, &showAll.
| home / scripts / sidebar | [previous] [next] |
Created: May 17, 2002
Revised: May 17, 2002
URL: http://webreference.com/scripts/sidebar/4.html