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