2 require_once 'config.php';
4 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
6 function purge_old_posts($link) {
7 if (PURGE_OLD_DAYS
> 0) {
9 if (DB_TYPE
== "pgsql") {
10 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
12 date_entered < NOW() - INTERVAL '".PURGE_OLD_DAYS
." days'");
14 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
16 date_entered < DATE_SUB(NOW(), INTERVAL ".PURGE_OLD_DAYS
." DAY)");
21 function update_all_feeds($link, $fetch) {
23 if (WEB_DEMO_MODE
) return;
25 if (DAEMON_REFRESH_ONLY
) {
26 if (!$_GET["daemon"]) {
31 db_query($link, "BEGIN");
33 $result = db_query($link, "SELECT feed_url,id,
34 substring(last_updated,1,19) as last_updated,
35 update_interval FROM ttrss_feeds");
37 while ($line = db_fetch_assoc($result)) {
38 $upd_intl = $line["update_interval"];
40 if (!$upd_intl ||
$upd_intl == 0) $upd_intl = DEFAULT_UPDATE_INTERVAL
;
42 if (!$line["last_updated"] ||
43 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
45 update_rss_feed($link, $line["feed_url"], $line["id"]);
49 purge_old_posts($link);
51 db_query($link, "COMMIT");
55 function check_feed_favicon($feed_url, $feed) {
56 $feed_url = str_replace("http://", "", $feed_url);
57 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
59 $icon_url = "http://$feed_url/favicon.ico";
60 $icon_file = ICONS_DIR
. "/$feed.ico";
62 if (!file_exists($icon_file)) {
65 $r = fopen($icon_url, "r");
66 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
69 $tmpfname = tempnam("/tmp", "ttrssicon");
71 $t = fopen($tmpfname, "w");
74 $buf = fread($r, 16384);
82 if (!rename($tmpfname, $icon_file)) {
85 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
91 function update_rss_feed($link, $feed_url, $feed) {
93 if (WEB_DEMO_MODE
) return;
95 $feed = db_escape_string($feed);
98 $rss = fetch_rss($feed_url);
100 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
102 db_query($link, "BEGIN");
104 $feed = db_escape_string($feed);
108 if (ENABLE_FEED_ICONS
) {
109 check_feed_favicon($feed_url, $feed);
112 $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
114 $registered_title = db_fetch_result($result, 0, "title");
115 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
117 if (!$registered_title) {
118 $feed_title = db_escape_string($rss->channel
["title"]);
119 db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
122 // print "I: " . $rss->channel["image"]["url"];
124 $icon_url = $rss->image
["url"];
126 if ($icon_url && !$orig_icon_url) {
127 $icon_url = db_escape_string($icon_url);
128 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
134 $result = db_query($link, "SELECT reg_exp,
135 (SELECT name FROM ttrss_filter_types
136 WHERE id = filter_type) as name
137 FROM ttrss_filters");
139 while ($line = db_fetch_assoc($result)) {
140 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
141 array_push($filters[$line["name"]], $line["reg_exp"]);
144 foreach ($rss->items
as $item) {
146 $entry_guid = $item["id"];
148 if (!$entry_guid) $entry_guid = $item["guid"];
149 if (!$entry_guid) $entry_guid = $item["link"];
151 if (!$entry_guid) continue;
153 $entry_timestamp = "";
155 $rss_2_date = $item['pubdate'];
156 $rss_1_date = $item['dc']['date'];
157 $atom_date = $item['issued'];
159 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
160 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
161 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
163 if ($entry_timestamp == "") {
164 $entry_timestamp = time();
165 $no_orig_date = 'true';
167 $no_orig_date = 'false';
170 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
172 $entry_title = $item["title"];
173 $entry_link = $item["link"];
175 if (!$entry_title) continue;
176 if (!$entry_link) continue;
178 $entry_content = $item["content:escaped"];
180 if (!$entry_content) $entry_content = $item["content:encoded"];
181 if (!$entry_content) $entry_content = $item["content"];
182 if (!$entry_content) $entry_content = $item["description"];
184 // if (!$entry_content) continue;
187 if (is_array($entry_content)) {
188 $entry_content = $entry_content["encoded"];
189 if (!$entry_content) $entry_content = $entry_content["escaped"];
193 // print_r($entry_content);
195 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
197 $entry_comments = $item["comments"];
199 $entry_guid = db_escape_string($entry_guid);
201 $result = db_query($link, "
203 id,last_read,no_orig_date,title,feed_id,content_hash,
204 substring(updated,1,19) as updated
208 guid = '$entry_guid'");
210 // print db_num_rows($result) . "$entry_guid<br/>";
212 if (db_num_rows($result) == 0) {
215 if (is_filtered($entry_title, $entry_content, $filters)) {
218 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
220 //$entry_guid = db_escape_string($entry_guid);
221 $entry_content = db_escape_string($entry_content);
222 $entry_title = db_escape_string($entry_title);
223 $entry_link = db_escape_string($entry_link);
224 $entry_comments = db_escape_string($entry_comments);
242 '$entry_timestamp_fmt',
250 $result = db_query($link, $query);
254 $orig_entry_id = db_fetch_result($result, 0, "id");
255 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
257 // print "OED: $orig_entry_id; OID: $orig_feed_id ; FID: $feed<br>";
259 if ($orig_feed_id != $feed) {
260 // print "<p>GUID $entry_guid: update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
264 $entry_is_modified = false;
266 $orig_timestamp = strtotime(db_fetch_result($result, 0, "updated"));
268 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
269 $orig_last_read = db_fetch_result($result, 0, "last_read");
270 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
271 $orig_title = db_fetch_result($result, 0, "title");
273 $last_read_qpart = "";
275 if ($orig_content_hash != $content_hash) {
276 // print "$orig_content_hash :: $content_hash<br>";
278 if (UPDATE_POST_ON_CHECKSUM_CHANGE
) {
279 $last_read_qpart = 'last_read = null,';
281 $entry_is_modified = true;
284 if ($orig_title != $entry_title) {
285 $entry_is_modified = true;
288 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
289 $entry_is_modified = true;
292 if ($entry_is_modified) {
294 // print "$entry_guid Modified!<br>";
296 $entry_comments = db_escape_string($entry_comments);
297 $entry_content = db_escape_string($entry_content);
298 $entry_title = db_escape_string($entry_title);
299 $entry_link = db_escape_string($entry_link);
301 $query = "UPDATE ttrss_entries
304 title = '$entry_title',
305 link = '$entry_link',
306 updated = '$entry_timestamp_fmt',
307 content = '$entry_content',
308 comments = '$entry_comments',
309 content_hash = '$content_hash'
311 id = '$orig_entry_id'";
313 $result = db_query($link, $query);
318 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
322 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content,
325 $entry_tags = $entry_tags[1];
327 if (count($entry_tags) > 0) {
329 $result = db_query($link, "SELECT id FROM ttrss_entries
330 WHERE guid = '$entry_guid'");
332 if (!$result ||
db_num_rows($result) != 1) {
336 $entry_id = db_fetch_result($result, 0, "id");
338 foreach ($entry_tags as $tag) {
339 $tag = db_escape_string(strtolower($tag));
341 $result = db_query($link, "SELECT id FROM ttrss_tags
342 WHERE tag_name = '$tag' AND post_id = '$entry_id' LIMIT 1");
344 if ($result && db_num_rows($result) == 0) {
346 // print "tagging $entry_id as $tag<br>";
348 db_query($link, "INSERT INTO ttrss_tags (tag_name,post_id)
349 VALUES ('$tag', '$entry_id')");
355 db_query($link, "UPDATE ttrss_feeds
356 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
359 $error_msg = db_escape_string(magpie_error());
361 "UPDATE ttrss_feeds SET last_error = '$error_msg',
362 last_updated = NOW() WHERE id = '$feed'");
365 db_query($link, "COMMIT");
369 function print_select($id, $default, $values, $attributes = "") {
370 print "<select id=\"$id\" $attributes>";
371 foreach ($values as $v) {
377 print "<option$sel>$v</option>";
382 function is_filtered($title, $content, $filters) {
384 if ($filters["title"]) {
385 foreach ($filters["title"] as $title_filter) {
386 if (preg_match("/$title_filter/i", $title))
391 if ($filters["content"]) {
392 foreach ($filters["content"] as $content_filter) {
393 if (preg_match("/$content_filter/i", $content))
398 if ($filters["both"]) {
399 foreach ($filters["both"] as $filter) {
400 if (preg_match("/$filter/i", $title) ||
preg_match("/$filter/i", $content))
408 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file) {
410 if (file_exists($icon_file) && filesize($icon_file) > 0) {
411 $feed_icon = "<img src=\"$icon_file\">";
413 $feed_icon = "<img src=\"images/blank_icon.gif\">";
416 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
418 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
419 if (ENABLE_FEED_ICONS
) {
423 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
428 $fctr_class = "class=\"invisible\"";
431 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
432 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
438 function getmicrotime() {
439 list($usec, $sec) = explode(" ",microtime());
440 return ((float)$usec +
(float)$sec);