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 (! ($_GET["daemon"] && DAEMON_REFRESH_ONLY
)) {
29 db_query($link, "BEGIN");
31 $result = db_query($link, "SELECT feed_url,id,
32 substring(last_updated,1,19) as last_updated,
33 update_interval FROM ttrss_feeds");
35 while ($line = db_fetch_assoc($result)) {
36 $upd_intl = $line["update_interval"];
38 if (!$upd_intl ||
$upd_intl == 0) $upd_intl = DEFAULT_UPDATE_INTERVAL
;
40 if (!$line["last_updated"] ||
41 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
43 update_rss_feed($link, $line["feed_url"], $line["id"]);
47 purge_old_posts($link);
49 db_query($link, "COMMIT");
53 function check_feed_favicon($feed_url, $feed) {
54 $feed_url = str_replace("http://", "", $feed_url);
55 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
57 $icon_url = "http://$feed_url/favicon.ico";
58 $icon_file = ICONS_DIR
. "/$feed.ico";
60 if (!file_exists($icon_file)) {
63 $r = fopen($icon_url, "r");
64 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
67 $tmpfname = tempnam("/tmp", "ttrssicon");
69 $t = fopen($tmpfname, "w");
72 $buf = fread($r, 16384);
80 if (!rename($tmpfname, $icon_file)) {
83 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
89 function update_rss_feed($link, $feed_url, $feed) {
91 if (WEB_DEMO_MODE
) return;
94 $rss = fetch_rss($feed_url);
95 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
97 db_query($link, "BEGIN");
99 $feed = db_escape_string($feed);
103 if (ENABLE_FEED_ICONS
) {
104 check_feed_favicon($feed_url, $feed);
107 $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
109 $registered_title = db_fetch_result($result, 0, "title");
110 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
112 if (!$registered_title) {
113 $feed_title = db_escape_string($rss->channel
["title"]);
114 db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
117 // print "I: " . $rss->channel["image"]["url"];
119 $icon_url = $rss->image
["url"];
121 if ($icon_url && !$orig_icon_url) {
122 $icon_url = db_escape_string($icon_url);
123 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
129 $result = db_query($link, "SELECT reg_exp,
130 (SELECT name FROM ttrss_filter_types
131 WHERE id = filter_type) as name
132 FROM ttrss_filters");
134 while ($line = db_fetch_assoc($result)) {
135 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
136 array_push($filters[$line["name"]], $line["reg_exp"]);
139 foreach ($rss->items
as $item) {
141 $entry_guid = $item["id"];
143 if (!$entry_guid) $entry_guid = $item["guid"];
144 if (!$entry_guid) $entry_guid = $item["link"];
146 if (!$entry_guid) continue;
148 $entry_timestamp = "";
150 $rss_2_date = $item['pubdate'];
151 $rss_1_date = $item['dc']['date'];
152 $atom_date = $item['issued'];
154 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
155 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
156 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
158 if ($entry_timestamp == "") {
159 $entry_timestamp = time();
160 $no_orig_date = 'true';
162 $no_orig_date = 'false';
165 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
167 $entry_title = $item["title"];
168 $entry_link = $item["link"];
170 if (!$entry_title) continue;
171 if (!$entry_link) continue;
173 $entry_content = $item["content:escaped"];
175 if (!$entry_content) $entry_content = $item["content:encoded"];
176 if (!$entry_content) $entry_content = $item["content"];
177 if (!$entry_content) $entry_content = $item["description"];
179 // if (!$entry_content) continue;
182 if (is_array($entry_content)) {
183 $entry_content = $entry_content["encoded"];
184 if (!$entry_content) $entry_content = $entry_content["escaped"];
188 // print_r($entry_content);
190 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
192 $entry_comments = $item["comments"];
194 $entry_guid = db_escape_string($entry_guid);
196 $result = db_query($link, "
198 id,last_read,no_orig_date,title,feed_id,content_hash,
199 substring(updated,1,19) as updated
203 guid = '$entry_guid'");
205 // print db_num_rows($result) . "$entry_guid<br/>";
207 if (db_num_rows($result) == 0) {
210 if (is_filtered($entry_title, $entry_content, $filters)) {
213 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
215 //$entry_guid = db_escape_string($entry_guid);
216 $entry_content = db_escape_string($entry_content);
217 $entry_title = db_escape_string($entry_title);
218 $entry_link = db_escape_string($entry_link);
219 $entry_comments = db_escape_string($entry_comments);
237 '$entry_timestamp_fmt',
245 $result = db_query($link, $query);
249 $orig_entry_id = db_fetch_result($result, 0, "id");
250 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
252 // print "OED: $orig_entry_id; OID: $orig_feed_id ; FID: $feed<br>";
254 if ($orig_feed_id != $feed) {
255 // print "<p>GUID $entry_guid: update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
259 $entry_is_modified = false;
261 $orig_timestamp = strtotime(db_fetch_result($result, 0, "updated"));
263 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
264 $orig_last_read = db_fetch_result($result, 0, "last_read");
265 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
266 $orig_title = db_fetch_result($result, 0, "title");
268 $last_read_qpart = "";
270 if ($orig_content_hash != $content_hash) {
271 // print "$orig_content_hash :: $content_hash<br>";
273 if (UPDATE_POST_ON_CHECKSUM_CHANGE
) {
274 $last_read_qpart = 'last_read = null,';
276 $entry_is_modified = true;
279 if ($orig_title != $entry_title) {
280 $entry_is_modified = true;
283 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
284 $entry_is_modified = true;
287 if ($entry_is_modified) {
289 // print "$entry_guid Modified!<br>";
291 $entry_comments = db_escape_string($entry_comments);
292 $entry_content = db_escape_string($entry_content);
293 $entry_title = db_escape_string($entry_title);
294 $entry_link = db_escape_string($entry_link);
296 $query = "UPDATE ttrss_entries
299 title = '$entry_title',
300 link = '$entry_link',
301 updated = '$entry_timestamp_fmt',
302 content = '$entry_content',
303 comments = '$entry_comments',
304 content_hash = '$content_hash'
306 id = '$orig_entry_id'";
308 $result = db_query($link, $query);
313 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
317 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content,
320 $entry_tags = $entry_tags[1];
322 if (count($entry_tags) > 0) {
324 $result = db_query($link, "SELECT id FROM ttrss_entries
325 WHERE guid = '$entry_guid'");
327 if (!$result ||
db_num_rows($result) != 1) {
331 $entry_id = db_fetch_result($result, 0, "id");
333 foreach ($entry_tags as $tag) {
334 $tag = db_escape_string(strtolower($tag));
336 $result = db_query($link, "SELECT id FROM ttrss_tags
337 WHERE tag_name = '$tag' AND post_id = '$entry_id' LIMIT 1");
339 if ($result && db_num_rows($result) == 0) {
341 // print "tagging $entry_id as $tag<br>";
343 db_query($link, "INSERT INTO ttrss_tags (tag_name,post_id)
344 VALUES ('$tag', '$entry_id')");
350 db_query($link, "UPDATE ttrss_feeds SET last_updated = NOW() WHERE id = $feed");
354 db_query($link, "COMMIT");
358 function print_select($id, $default, $values, $attributes = "") {
359 print "<select id=\"$id\" $attributes>";
360 foreach ($values as $v) {
366 print "<option$sel>$v</option>";
371 function is_filtered($title, $content, $filters) {
373 if ($filters["title"]) {
374 foreach ($filters["title"] as $title_filter) {
375 if (preg_match("/$title_filter/i", $title))
380 if ($filters["content"]) {
381 foreach ($filters["content"] as $content_filter) {
382 if (preg_match("/$content_filter/i", $content))
387 if ($filters["both"]) {
388 foreach ($filters["both"] as $filter) {
389 if (preg_match("/$filter/i", $title) ||
preg_match("/$filter/i", $content))
397 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file) {
399 if (file_exists($icon_file) && filesize($icon_file) > 0) {
400 $feed_icon = "<img src=\"$icon_file\">";
402 $feed_icon = "<img src=\"images/blank_icon.gif\">";
405 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
407 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
408 if (ENABLE_FEED_ICONS
) {
412 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
417 $fctr_class = "class=\"invisible\"";
420 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
421 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
427 function getmicrotime() {
428 list($usec, $sec) = explode(" ",microtime());
429 return ((float)$usec +
(float)$sec);