]> git.wh0rd.org - tt-rss.git/blob - functions.php
hardcode minimum 30 minute rss refetch interval
[tt-rss.git] / functions.php
1 <?
2 require_once 'config.php';
3
4 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
5
6 function purge_old_posts($link) {
7 if (PURGE_OLD_DAYS > 0) {
8 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
9 marked = false AND
10 date_entered < NOW() - INTERVAL '".PURGE_OLD_DAYS." days'");
11 }
12 }
13
14 function update_all_feeds($link, $fetch) {
15
16 if (WEB_DEMO_MODE) return;
17
18 db_query($link, "BEGIN");
19
20 $result = db_query($link, "SELECT feed_url,id,last_updated FROM ttrss_feeds");
21
22 while ($line = db_fetch_assoc($result)) {
23 if (!$line["last_updated"] || time() - strtotime($line["last_updated"]) > 1800) {
24 update_rss_feed($link, $line["feed_url"], $line["id"]);
25 }
26 }
27
28 purge_old_posts($link);
29
30 db_query($link, "COMMIT");
31
32 }
33
34 function check_feed_favicon($feed_url, $feed) {
35 $feed_url = str_replace("http://", "", $feed_url);
36 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
37
38 $icon_url = "http://$feed_url/favicon.ico";
39 $icon_file = ICONS_DIR . "/$feed.ico";
40
41 if (!file_exists($icon_file)) {
42
43 error_reporting(0);
44 $r = fopen($icon_url, "r");
45 error_reporting (E_ERROR | E_WARNING | E_PARSE);
46
47 if ($r) {
48 $tmpfname = tempnam("/tmp", "ttrssicon");
49
50 $t = fopen($tmpfname, "w");
51
52 while (!feof($r)) {
53 $buf = fread($r, 16384);
54 fwrite($t, $buf);
55 }
56
57 fclose($r);
58 fclose($t);
59
60 error_reporting(0);
61 if (!rename($tmpfname, $icon_file)) {
62 unlink($tmpfname);
63 }
64 error_reporting (E_ERROR | E_WARNING | E_PARSE);
65
66 }
67 }
68 }
69
70 function update_rss_feed($link, $feed_url, $feed) {
71
72 if (WEB_DEMO_MODE) return;
73
74 error_reporting(0);
75 $rss = fetch_rss($feed_url);
76 error_reporting (E_ERROR | E_WARNING | E_PARSE);
77
78 db_query($link, "BEGIN");
79
80 $feed = db_escape_string($feed);
81
82 if ($rss) {
83
84 if (ENABLE_FEED_ICONS) {
85 check_feed_favicon($feed_url, $feed);
86 }
87
88 $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
89
90 $registered_title = db_fetch_result($result, 0, "title");
91 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
92
93 if (!$registered_title) {
94 $feed_title = $rss->channel["title"];
95 db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
96 }
97
98 // print "I: " . $rss->channel["image"]["url"];
99
100 $icon_url = $rss->image["url"];
101
102 if ($icon_url && !$orig_icon_url) {
103 $icon_url = db_escape_string($icon_url);
104 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
105 }
106
107
108 $filters = array();
109
110 $result = db_query($link, "SELECT reg_exp,
111 (SELECT name FROM ttrss_filter_types
112 WHERE id = filter_type) as name
113 FROM ttrss_filters");
114
115 while ($line = db_fetch_assoc($result)) {
116 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
117 array_push($filters[$line["name"]], $line["reg_exp"]);
118 }
119
120 foreach ($rss->items as $item) {
121
122 $entry_guid = $item["id"];
123
124 if (!$entry_guid) $entry_guid = $item["guid"];
125 if (!$entry_guid) $entry_guid = $item["link"];
126
127 if (!$entry_guid) continue;
128
129 $entry_timestamp = "";
130
131 $rss_2_date = $item['pubdate'];
132 $rss_1_date = $item['dc']['date'];
133 $atom_date = $item['issued'];
134
135 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
136 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
137 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
138
139 if ($entry_timestamp == "") {
140 $entry_timestamp = time();
141 $no_orig_date = 'true';
142 } else {
143 $no_orig_date = 'false';
144 }
145
146 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
147
148 $entry_title = $item["title"];
149 $entry_link = $item["link"];
150
151 if (!$entry_title) continue;
152 if (!$entry_link) continue;
153
154 $entry_content = $item["description"];
155 if (!$entry_content) $entry_content = $item["content:escaped"];
156 if (!$entry_content) $entry_content = $item["content"];
157
158 // if (!$entry_content) continue;
159
160 // WTF
161 if (is_array($entry_content)) {
162 $entry_content = $entry_content["encoded"];
163 }
164
165 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
166
167 $entry_comments = $item["comments"];
168
169 $entry_guid = db_escape_string($entry_guid);
170
171 if (DB_TYPE == "pgsql") {
172 $extract_ts_qpart = ",EXTRACT(EPOCH FROM updated) as updated_timestamp";
173 }
174
175 $result = db_query($link, "
176 SELECT
177 id,last_read,no_orig_date,title,feed_id,content_hash
178 $extract_ts_qpart
179 FROM
180 ttrss_entries
181 WHERE
182 guid = '$entry_guid'");
183
184 if (db_num_rows($result) == 0) {
185
186 error_reporting(0);
187 if (is_filtered($entry_title, $entry_content, $filters)) {
188 continue;
189 }
190 error_reporting (E_ERROR | E_WARNING | E_PARSE);
191
192 //$entry_guid = db_escape_string($entry_guid);
193 $entry_content = db_escape_string($entry_content);
194 $entry_title = db_escape_string($entry_title);
195 $entry_link = db_escape_string($entry_link);
196 $entry_comments = db_escape_string($entry_comments);
197
198 $query = "INSERT
199 INTO ttrss_entries
200 (title,
201 guid,
202 link,
203 updated,
204 content,
205 content_hash,
206 feed_id,
207 comments,
208 no_orig_date)
209 VALUES
210 ('$entry_title',
211 '$entry_guid',
212 '$entry_link',
213 '$entry_timestamp_fmt',
214 '$entry_content',
215 '$content_hash',
216 '$feed',
217 '$entry_comments',
218 $no_orig_date)";
219
220 $result = db_query($link, $query);
221
222 } else {
223
224 $orig_entry_id = db_fetch_result($result, 0, "id");
225 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
226
227 if ($orig_feed_id != $feed) {
228 // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
229 continue;
230 }
231
232 $entry_is_modified = false;
233
234 $orig_timestamp = db_fetch_result($result, 0, "updated_timestamp");
235 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
236 $orig_last_read = db_fetch_result($result, 0, "last_read");
237 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
238 $orig_title = db_fetch_result($result, 0, "title");
239
240 $last_read_qpart = "";
241
242 if ($orig_content_hash != $content_hash) {
243 if (UPDATE_POST_ON_CHECKSUM_CHANGE) {
244 $last_read_qpart = 'last_read = null,';
245 }
246 $entry_is_modified = true;
247 }
248
249 if ($orig_title != $entry_title) {
250 $entry_is_modified = true;
251 }
252
253 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
254 $entry_is_modified = true;
255 }
256
257 if ($entry_is_modified) {
258
259 $entry_comments = db_escape_string($entry_comments);
260 $entry_content = db_escape_string($entry_content);
261 $entry_title = db_escape_string($entry_title);
262 $entry_link = db_escape_string($entry_link);
263
264 $query = "UPDATE ttrss_entries
265 SET
266 $last_read_qpart
267 title = '$entry_title',
268 link = '$entry_link',
269 updated = '$entry_timestamp_fmt',
270 content = '$entry_content',
271 comments = '$entry_comments',
272 content_hash = '$content_hash'
273 WHERE
274 id = '$orig_entry_id'";
275
276 $result = db_query($link, $query);
277 }
278 }
279
280 /* taaaags */
281 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
282
283 $entry_tags = null;
284
285 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content,
286 $entry_tags);
287
288 $entry_tags = $entry_tags[1];
289
290 if (count($entry_tags) > 0) {
291
292 $result = db_query($link, "SELECT id FROM ttrss_entries
293 WHERE guid = '$entry_guid'");
294
295 if (!$result || db_num_rows($result) != 1) {
296 return;
297 }
298
299 $entry_id = db_fetch_result($result, 0, "id");
300
301 foreach ($entry_tags as $tag) {
302 $tag = db_escape_string(strtolower($tag));
303
304 $result = db_query($link, "SELECT id FROM ttrss_tags
305 WHERE tag_name = '$tag' AND post_id = '$entry_id' LIMIT 1");
306
307 if ($result && db_num_rows($result) == 0) {
308
309 // print "tagging $entry_id as $tag<br>";
310
311 db_query($link, "INSERT INTO ttrss_tags (tag_name,post_id)
312 VALUES ('$tag', '$entry_id')");
313 }
314 }
315 }
316 }
317
318 db_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
319
320 }
321
322 db_query($link, "COMMIT");
323
324 }
325
326 function print_select($id, $default, $values, $attributes = "") {
327 print "<select id=\"$id\" $attributes>";
328 foreach ($values as $v) {
329 if ($v == $default)
330 $sel = " selected";
331 else
332 $sel = "";
333
334 print "<option$sel>$v</option>";
335 }
336 print "</select>";
337 }
338
339 function is_filtered($title, $content, $filters) {
340
341 if ($filters["title"]) {
342 foreach ($filters["title"] as $title_filter) {
343 if (preg_match("/$title_filter/i", $title))
344 return true;
345 }
346 }
347
348 if ($filters["content"]) {
349 foreach ($filters["content"] as $content_filter) {
350 if (preg_match("/$content_filter/i", $content))
351 return true;
352 }
353 }
354
355 if ($filters["both"]) {
356 foreach ($filters["both"] as $filter) {
357 if (preg_match("/$filter/i", $title) || preg_match("/$filter/i", $content))
358 return true;
359 }
360 }
361
362 return false;
363 }
364
365 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file) {
366
367 if (file_exists($icon_file) && filesize($icon_file) > 0) {
368 $feed_icon = "<img src=\"$icon_file\">";
369 } else {
370 $feed_icon = "<img src=\"images/blank_icon.gif\">";
371 }
372
373 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
374
375 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
376 if (ENABLE_FEED_ICONS) {
377 print "$feed_icon";
378 }
379
380 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
381
382 if ($unread != 0) {
383 $fctr_class = "";
384 } else {
385 $fctr_class = "class=\"invisible\"";
386 }
387
388 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
389 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
390
391 print "</li>";
392
393 }
394
395 ?>