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