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