source: trunk/www.guidonia.net/wp/wp-content/plugins/feeder-v0.3/feeder.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 15 years ago
File size: 8.2 KB
Line 
1<?php
2/*
3Plugin Name: Feeder
4Plugin URI: http://xubz.com/weblog/2007/03/feeder-wordpress-plugin
5Description: RSS 2.0 Feed Reader with 'Feed-Cache' Function. Parses the Given Feed into Links for Display in Blogs, Read the Documentation for more Info.
6Author: Subbu
7Author URI: http://xubz.com/
8Version: 0.3
9License: GNU General Public License (v2 or Later)
10*/
11
12if ('feeder.php' == basename($_SERVER['SCRIPT_FILENAME']))
13 die ('Please do not load this directly');
14
15class feeder
16{
17 var $feeder_ver = 0.30;
18
19 function readFeed($filename, $xml_key="") //XML Parser, Most Part of it comes from PHP Manual (duh!)
20 {
21 $data = implode("", file($filename));
22 $parser = xml_parser_create();
23 $xml_key = ($xml_key == "") ? "item" : $xml_key;
24
25 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
26 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
27 xml_parse_into_struct($parser, $data, $values, $tags);
28 xml_parser_free($parser);
29
30 foreach ($tags as $key => $val)
31 {
32 if ($key == $xml_key)
33 {
34 $feedranges = $val;
35 for ($i = 0; $i < count($feedranges); $i += 2)
36 {
37 $offset = $feedranges[$i] + 1;
38 $len = $feedranges[$i + 1] - $offset;
39 $fdb[] = $this->parseFeed(array_slice($values, $offset, $len));
40 }
41 }
42 else
43 {
44 continue;
45 }
46 }
47 return $fdb;
48 }
49
50 function parseFeed($values)
51 {
52 for ($i = 0; $i < 2; $i++)
53 {
54 $feed[$values[$i]["tag"]] = $values[$i]["value"];
55 }
56 return $feed;
57 }
58
59 function clean_title($title) //Clean the Titles of some unexpected characters.. Donno why those Appear :-/
60 {
61 $title = trim($title);
62 $title = stripslashes($title);
63 $title = str_replace("’", "&#39;", $title);
64 $title = str_replace("â€", "&quot;", $title);
65 $title = str_replace("œ", "", $title);
66 $title = htmlspecialchars($title);
67 return $title;
68 }
69
70 function process_data($feed_array)
71 {
72 $linkcount = 0;
73 $linksshown = 1;
74 $processed = array();
75
76 foreach($feed_array as $array)
77 {
78 $link = $array['link'];
79 $title = $this->clean_title($array['title']);
80
81 $tooltip = $title;
82 $title = substr($title, 0, $this->numchars); //Limit the Number of Characters to be Shown in Title
83 $title = (strlen($title) >= $this->numchars) ? ($title . $this->prefix) : $title;
84
85 $processed[$linkcount] = sprintf("\t%s<a href=\"%s\" title=\"%s\" class=\"%s\" target=\"%s\">%s</a>%s\n", $this->tag1, $link, $tooltip, $this->link_class, $this->link_target, $title, $this->tag2);
86 $linkcount = $linkcount + 1;
87 }
88
89 foreach ($processed as $show_link)
90 {
91 if ($linksshown <= $this->numlinks)
92 {
93 echo $show_link;
94 $linksshown = $linksshown + 1;
95 }
96 }
97
98 return ($linksshown - 1);
99 }
100
101 function feeder($filename = "", $numlinks = 0, $tag1 = "", $tag2 = "", $link_class = "", $link_target = "", $numchars = 0, $cache = 0, $update_time = 0) //Class Constructor
102 {
103 if ($filename != "") //Check if the given Feed Path is Empty
104 {
105 error_reporting(0);
106 $filep = fopen($filename, "r"); //Try to open it.. Throws a insane amount of errors if the server can't,
107 //So Disable the Error Reporting for a While :-/ (Have a better Method??)
108
109 error_reporting(E_ALL & ~E_NOTICE);
110
111 //Assign the basic variables
112 $cached = false;
113 $feed_file;
114
115 $upload_path = ABSPATH . 'wp-content/uploads/';
116 if (!is_dir($upload_path)) { echo "\n<li><a href=\"#\" title=\"Please Create a Uploads Directory in /wp-content/\">Feeder: Upload Dir. Doesn't Exist</a></li>"; }
117
118 $cache_path = $upload_path . 'feeder_' . substr(md5($filename), 0, 8) . '.xml'; //Prefix a Random String Made from the Filename
119
120 if ($filep) //Check if the Feed is Readable, See Line 106..
121 {
122 //Validate all the Function Variables and set Default Values
123 $this->prefix = "...";
124 $this->tag1 = ($tag1 == "") ? "<li>" : $tag1;
125 $this->tag2 = ($tag2 == "") ? "</li>" : $tag2;
126 $this->numchars = ($numchars == 0 or !is_integer($numchars) or $numchars < 1) ? 32 : $numchars;
127 $this->numlinks = ($numlinks == 0 or !is_integer($numlinks) or $numlinks < 1) ? 10 : $numlinks;
128 $this->link_class = ($link_class == "") ? "feeder-link" : $link_class;
129 $this->link_target = ($link_target == "" or ($link_target != "_self" and $link_target != "_blank")) ? "_self" : $link_target;
130 $update_time = ($update_time == 0 or !is_integer($update_time) or $update_time < 60) ? 3600 : $update_time;
131 $cache = ($cache == 1) ? 1 : 0;
132
133 if ($cache == 1) //If Cache is Enabled
134 {
135 $feed_file = $cache_path;
136
137 if (file_exists($cache_path)) //If the Feed was already Cached
138 {
139 //Get the Cache and Current Time (UNIX_TIMESTAMP)
140 $file_timestamp = filemtime($cache_path);
141 $current_timestamp = time();
142 $time_difference = $current_timestamp - $file_timestamp;
143
144 if ($time_difference >= $update_time) //Check whether the cache is deprecated (a simple validation Uh?)
145 {
146 $cached = false; //Its Deprecated, so set Cached as False
147 }
148 else
149 {
150 $cached = true; //Quite New, No Need to Update the Cache
151 }
152 }
153 else //Cache Doesn't Exist, Create it..
154 {
155 $cached = false;
156 }
157
158 if (!$cached) //The file is NOT Cached, so Create it.
159 {
160 if (file_exists($cache_path)) { unlink($cache_path); } //Unlink the old Cache to Update it.
161
162 $feeder_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?" . ">\r"; //Start Creating the Cache Content
163 $feeder_xml .= "<!-- generator=\"Feeder " . $this->feeder_ver . "\" -->\r";
164 $feeder_xml .= "<feeder>\r";
165
166 $feed_array = $this->readFeed($filename, "item"); //Parse the Feed into the Cache
167 if ($feed_array != "")
168 {
169 foreach ($feed_array as $array) //Add XML Elements
170 {
171 $link = $array['link'];
172 $title = $this->clean_title($array['title']);
173 $feeder_xml .= sprintf("<url>\n\t<title>%s</title>\n\t<link>%s</link>\n</url>\r", $title, $link);
174 }
175 }
176 else //Could not parse the feed, So Display a Error
177 {
178 echo "\n<li><a href=\"#\" title=\"The Resource provided does not seem to be a Valid RSS 2.0 Feed\">Feeder: Resource NOT a Feed</a></li>";
179 $bad_feed = 1;
180 }
181
182 $feeder_xml .= "</feeder>";
183
184 if (!isset($bad_feed) and $bad_feed != 1) //If There was no error, Continue
185 {
186 error_reporting(0); //If only PHP(5) had a better working try-catch Method.. :-/
187 if ($cachep = fopen($cache_path, "w")) //Write the Cache to the File
188 {
189 fputs($cachep, $feeder_xml);
190 fclose($cachep);
191 echo "\t<!-- Feeder: Cache Updated/Created.. Next Update on " . date("r", time() + $update_time) . " -->";
192 $cached = true;
193 }
194 else //Could not Write, Display Error, Set Cached as False
195 {
196 echo "\n<li><a href=\"#\" title=\"Could not Write/Read Cache, Please make sure uploads folder is set to CHMOD 777\">Feeder: Cache Permission Denied</a></li>";
197 $cached = false;
198 }
199 error_reporting(E_ALL & ~E_NOTICE);
200 }
201 else //Error in the Given Feed, Set Cached as False
202 {
203 $cached = false;
204 }
205 }
206 }
207
208 if ($cached) //Check if Cached is True, Parse the Links from the Cache
209 {
210 $feed_array = $this->readFeed($feed_file, "url");
211 if ($feed_array != "")
212 {
213 $links_shown = $this->process_data($feed_array);
214 echo "\t<!-- Feeder: " . $links_shown . " links parsed from Cache -->";
215 }
216 else //No Data in Cache, set Cached as False and Continue
217 {
218 $cached = false;
219 }
220 }
221
222 if ($cache != 1 or !$cached) //Cache is Disabled and Cached is False
223 {
224 if (file_exists($cache_path)) { unlink($cache_path); } //Delete the Cache, Create it later (Failproof Method)
225
226 $feed_array = $this->readFeed($filename, "item");
227 if ($feed_array != "")
228 {
229 $links_shown = $this->process_data($feed_array);
230 echo "\t<!-- Feeder: " . $links_shown . " links parsed directly from Feed -->";
231 }
232 else //Throw some Errors....
233 {
234 echo "\n<li><a href=\"#\" title=\"The Resource provided does not seem to be a Valid RSS 2.0 Feed\">Feeder: Resource NOT a Feed</a></li>";
235 }
236 }
237 }
238 else
239 {
240 echo "\n<li><a href=\"#\" title=\"Host Seems to be Down/Feed Doesn't Exist\">Feeder: (404) Feed not Found</a></li>";
241 }
242 }
243 else
244 {
245 echo "\n<li><a href=\"#\">Feeder: No URL Provided</a></li>";
246 }
247 }
248}
249//Thats all folks!
250?>
Note: See TracBrowser for help on using the repository browser.