2 require_once 'config.php';
4 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
6 function purge_old_posts($link) {
7 if (PURGE_OLD_DAYS
> 0) {
8 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
10 date_entered < NOW() - INTERVAL '".PURGE_OLD_DAYS
." days'");
14 function update_all_feeds($link, $fetch) {
16 if (WEB_DEMO_MODE
) return;
18 db_query($link, "BEGIN");
20 $result = db_query($link, "SELECT feed_url,id FROM ttrss_feeds");
22 while ($line = db_fetch_assoc($result)) {
23 update_rss_feed($link, $line["feed_url"], $line["id"]);
26 purge_old_posts($link);
28 db_query($link, "COMMIT");
32 function check_feed_favicon($feed_url, $feed) {
33 $feed_url = str_replace("http://", "", $feed_url);
34 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
36 $icon_url = "http://$feed_url/favicon.ico";
37 $icon_file = ICONS_DIR
. "/$feed.ico";
39 if (!file_exists($icon_file)) {
42 $r = fopen($icon_url, "r");
43 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
46 $tmpfname = tempnam("/tmp", "ttrssicon");
48 $t = fopen($tmpfname, "w");
51 $buf = fread($r, 16384);
59 if (!rename($tmpfname, $icon_file)) {
62 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
68 function update_rss_feed($link, $feed_url, $feed) {
70 if (WEB_DEMO_MODE
) return;
73 $rss = fetch_rss($feed_url);
74 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
76 db_query($link, "BEGIN");
78 $feed = db_escape_string($feed);
82 if (ENABLE_FEED_ICONS
) {
83 check_feed_favicon($feed_url, $feed);
86 $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
88 $registered_title = db_fetch_result($result, 0, "title");
89 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
91 if (!$registered_title) {
92 $feed_title = $rss->channel
["title"];
93 db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
96 // print "I: " . $rss->channel["image"]["url"];
98 $icon_url = $rss->image
["url"];
100 if ($icon_url && !$orig_icon_url) {
101 $icon_url = db_escape_string($icon_url);
102 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
108 $result = db_query($link, "SELECT reg_exp,
109 (SELECT name FROM ttrss_filter_types
110 WHERE id = filter_type) as name
111 FROM ttrss_filters");
113 while ($line = db_fetch_assoc($result)) {
114 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
115 array_push($filters[$line["name"]], $line["reg_exp"]);
118 foreach ($rss->items
as $item) {
120 $entry_guid = $item["id"];
122 if (!$entry_guid) $entry_guid = $item["guid"];
123 if (!$entry_guid) $entry_guid = $item["link"];
125 if (!$entry_guid) continue;
127 $entry_timestamp = "";
129 $rss_2_date = $item['pubdate'];
130 $rss_1_date = $item['dc']['date'];
131 $atom_date = $item['issued'];
133 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
134 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
135 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
137 if ($entry_timestamp == "") {
138 $entry_timestamp = time();
139 $no_orig_date = 'true';
141 $no_orig_date = 'false';
144 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
146 $entry_title = $item["title"];
147 $entry_link = $item["link"];
149 if (!$entry_title) continue;
150 if (!$entry_link) continue;
152 $entry_content = $item["description"];
153 if (!$entry_content) $entry_content = $item["content:escaped"];
154 if (!$entry_content) $entry_content = $item["content"];
156 // if (!$entry_content) continue;
158 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
160 $entry_comments = $item["comments"];
162 $entry_guid = db_escape_string($entry_guid);
164 if (DB_TYPE
== "pgsql") {
165 $extract_ts_qpart = ",EXTRACT(EPOCH FROM updated) as updated_timestamp";
168 $result = db_query($link, "
170 id,last_read,no_orig_date,title,feed_id,content_hash
175 guid = '$entry_guid'");
177 if (db_num_rows($result) == 0) {
180 if (is_filtered($entry_title, $entry_content, $filters)) {
183 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
185 //$entry_guid = db_escape_string($entry_guid);
186 $entry_content = db_escape_string($entry_content);
187 $entry_title = db_escape_string($entry_title);
188 $entry_link = db_escape_string($entry_link);
189 $entry_comments = db_escape_string($entry_comments);
206 '$entry_timestamp_fmt',
213 $result = db_query($link, $query);
217 $orig_entry_id = db_fetch_result($result, 0, "id");
218 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
220 if ($orig_feed_id != $feed) {
221 // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
225 $entry_is_modified = false;
227 $orig_timestamp = db_fetch_result($result, 0, "updated_timestamp");
228 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
229 $orig_last_read = db_fetch_result($result, 0, "last_read");
230 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
231 $orig_title = db_fetch_result($result, 0, "title");
233 $last_read_qpart = "";
235 if ($orig_content_hash != $content_hash) {
236 if (UPDATE_POST_ON_CHECKSUM_CHANGE
) {
237 $last_read_qpart = 'last_read = null,';
239 $entry_is_modified = true;
242 if ($orig_title != $entry_title) {
243 $entry_is_modified = true;
246 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
247 $entry_is_modified = true;
250 if ($entry_is_modified) {
252 $entry_comments = db_escape_string($entry_comments);
253 $entry_content = db_escape_string($entry_content);
254 $entry_title = db_escape_string($entry_title);
255 $entry_link = db_escape_string($entry_link);
257 $query = "UPDATE ttrss_entries
260 title = '$entry_title',
261 link = '$entry_link',
262 updated = '$entry_timestamp_fmt',
263 content = '$entry_content',
264 comments = '$entry_comments',
265 content_hash = '$content_hash'
267 id = '$orig_entry_id'";
269 $result = db_query($link, $query);
275 $result = db_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
280 db_query($link, "COMMIT");
284 function print_select($id, $default, $values, $attributes = "") {
285 print "<select id=\"$id\" $attributes>";
286 foreach ($values as $v) {
292 print "<option$sel>$v</option>";
297 function is_filtered($title, $content, $filters) {
299 if ($filters["title"]) {
300 foreach ($filters["title"] as $title_filter) {
301 if (preg_match("/$title_filter/i", $title))
306 if ($filters["content"]) {
307 foreach ($filters["content"] as $content_filter) {
308 if (preg_match("/$content_filter/i", $content))
313 if ($filters["both"]) {
314 foreach ($filters["both"] as $filter) {
315 if (preg_match("/$filter/i", $title) ||
preg_match("/$filter/i", $content))