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");
22 $result = db_query($link, "SELECT feed_url,id FROM ttrss_feeds WHERE
23 last_updated is null OR title = '' OR
24 EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM last_updated) > " .
29 $result = db_query($link, "SELECT feed_url,id FROM ttrss_feeds");
32 while ($line = db_fetch_assoc($result)) {
33 update_rss_feed($link, $line["feed_url"], $line["id"]);
36 purge_old_posts($link);
38 db_query($link, "COMMIT");
42 function check_feed_favicon($feed_url, $feed) {
43 $feed_url = str_replace("http://", "", $feed_url);
44 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
46 $icon_url = "http://$feed_url/favicon.ico";
47 $icon_file = ICONS_DIR
. "/$feed.ico";
49 if (!file_exists($icon_file)) {
52 $r = fopen($icon_url, "r");
53 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
56 $tmpfname = tempnam("/tmp", "ttrssicon");
58 $t = fopen($tmpfname, "w");
61 $buf = fread($r, 16384);
69 if (!rename($tmpfname, $icon_file)) {
72 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
78 function update_rss_feed($link, $feed_url, $feed) {
80 if (WEB_DEMO_MODE
) return;
83 $rss = fetch_rss($feed_url);
84 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
86 db_query($link, "BEGIN");
88 $feed = db_escape_string($feed);
92 if (ENABLE_FEED_ICONS
) {
93 check_feed_favicon($feed_url, $feed);
96 $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
98 $registered_title = db_fetch_result($result, 0, "title");
99 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
101 if (!$registered_title) {
102 $feed_title = $rss->channel
["title"];
103 db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
106 // print "I: " . $rss->channel["image"]["url"];
108 $icon_url = $rss->image
["url"];
110 if ($icon_url && !$orig_icon_url) {
111 $icon_url = db_escape_string($icon_url);
112 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
118 $result = db_query($link, "SELECT reg_exp,
119 (SELECT name FROM ttrss_filter_types
120 WHERE id = filter_type) as name
121 FROM ttrss_filters");
123 while ($line = db_fetch_assoc($result)) {
124 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
125 array_push($filters[$line["name"]], $line["reg_exp"]);
128 foreach ($rss->items
as $item) {
130 $entry_guid = $item["id"];
132 if (!$entry_guid) $entry_guid = $item["guid"];
133 if (!$entry_guid) $entry_guid = $item["link"];
135 if (!$entry_guid) continue;
137 $entry_timestamp = "";
139 $rss_2_date = $item['pubdate'];
140 $rss_1_date = $item['dc']['date'];
141 $atom_date = $item['issued'];
143 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
144 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
145 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
147 if ($entry_timestamp == "") {
148 $entry_timestamp = time();
149 $no_orig_date = 'true';
151 $no_orig_date = 'false';
154 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
156 $entry_title = $item["title"];
157 $entry_link = $item["link"];
159 if (!$entry_title) continue;
160 if (!$entry_link) continue;
162 $entry_content = $item["description"];
163 if (!$entry_content) $entry_content = $item["content:escaped"];
164 if (!$entry_content) $entry_content = $item["content"];
166 // if (!$entry_content) continue;
168 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
170 $entry_comments = $item["comments"];
172 $entry_guid = db_escape_string($entry_guid);
174 $result = db_query($link, "
176 id,last_read,no_orig_date,title,feed_id,content_hash,
177 EXTRACT(EPOCH FROM updated) as updated_timestamp
181 guid = '$entry_guid'");
183 if (db_num_rows($result) == 0) {
186 if (is_filtered($entry_title, $entry_content, $filters)) {
189 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
191 //$entry_guid = db_escape_string($entry_guid);
192 $entry_content = db_escape_string($entry_content);
193 $entry_title = db_escape_string($entry_title);
194 $entry_link = db_escape_string($entry_link);
195 $entry_comments = db_escape_string($entry_comments);
212 '$entry_timestamp_fmt',
219 $result = db_query($link, $query);
223 $orig_entry_id = db_fetch_result($result, 0, "id");
224 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
226 if ($orig_feed_id != $feed) {
227 // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
231 $entry_is_modified = false;
233 $orig_timestamp = db_fetch_result($result, 0, "updated_timestamp");
234 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
235 $orig_last_read = db_fetch_result($result, 0, "last_read");
236 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
237 $orig_title = db_fetch_result($result, 0, "title");
239 $last_read_qpart = "";
241 if ($orig_content_hash != $content_hash) {
242 if (UPDATE_POST_ON_CHECKSUM_CHANGE
) {
243 $last_read_qpart = 'last_read = null,';
245 $entry_is_modified = true;
248 if ($orig_title != $entry_title) {
249 $entry_is_modified = true;
252 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
253 $entry_is_modified = true;
256 if ($entry_is_modified) {
258 $entry_comments = db_escape_string($entry_comments);
259 $entry_content = db_escape_string($entry_content);
260 $entry_title = db_escape_string($entry_title);
261 $entry_link = db_escape_string($entry_link);
263 $query = "UPDATE ttrss_entries
266 title = '$entry_title',
267 link = '$entry_link',
268 updated = '$entry_timestamp_fmt',
269 content = '$entry_content',
270 comments = '$entry_comments',
271 content_hash = '$content_hash'
273 id = '$orig_entry_id'";
275 $result = db_query($link, $query);
281 $result = db_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
286 db_query($link, "COMMIT");
290 function print_select($id, $default, $values, $attributes = "") {
291 print "<select id=\"$id\" $attributes>";
292 foreach ($values as $v) {
298 print "<option$sel>$v</option>";
303 function is_filtered($title, $content, $filters) {
305 if ($filters["title"]) {
306 foreach ($filters["title"] as $title_filter) {
307 if (preg_match("/$title_filter/i", $title))
312 if ($filters["content"]) {
313 foreach ($filters["content"] as $content_filter) {
314 if (preg_match("/$content_filter/i", $content))
319 if ($filters["both"]) {
320 foreach ($filters["both"] as $filter) {
321 if (preg_match("/$filter/i", $title) ||
preg_match("/$filter/i", $content))