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