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