]> git.wh0rd.org - tt-rss.git/blame - functions.php
purging of old posts is actually configurable now, added comments for some configurat...
[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
c3a8d71a 6 function purge_old_posts() {
f92db4f5 7 if (PURGE_OLD_DAYS > 0) {
c3a8d71a 8 $result = pg_query("DELETE FROM ttrss_entries WHERE
f92db4f5 9 date_entered < NOW() - INTERVAL '".PURGE_OLD_DAYS." days'");
c3a8d71a
AD
10 }
11 }
12
9c9c7e6b 13 function update_all_feeds($link, $fetch) {
40d13c28 14
b0b4abcf
AD
15 if (WEB_DEMO_MODE) return;
16
b82af8c3
AD
17 pg_query("BEGIN");
18
9c9c7e6b
AD
19 if (!$fetch) {
20
21 $result = pg_query($link, "SELECT feed_url,id FROM ttrss_feeds WHERE
22 last_updated is null OR title = '' OR
23 EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM last_updated) > " .
24 MIN_UPDATE_TIME);
25
26 } else {
27
28 $result = pg_query($link, "SELECT feed_url,id FROM ttrss_feeds");
29 }
40d13c28
AD
30
31 while ($line = pg_fetch_assoc($result)) {
466001c4 32 update_rss_feed($link, $line["feed_url"], $line["id"]);
40d13c28
AD
33 }
34
c3a8d71a
AD
35 purge_old_posts();
36
b82af8c3
AD
37 pg_query("COMMIT");
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
f48ba3c9 85 pg_query("BEGIN");
b7f4bda2
AD
86
87 $feed = pg_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
b7f4bda2 95 $result = pg_query("SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
331900c6
AD
96
97 $registered_title = pg_fetch_result($result, 0, "title");
b7f4bda2 98 $orig_icon_url = pg_fetch_result($result, 0, "icon_url");
331900c6
AD
99
100 if (!$registered_title) {
331900c6
AD
101 $feed_title = $rss->channel["title"];
102 pg_query("UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
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) {
110 $icon_url = pg_escape_string($icon_url);
111 pg_query("UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
112 }
113
40d13c28
AD
114 foreach ($rss->items as $item) {
115
116 $entry_guid = $item["id"];
117
118 if (!$entry_guid) $entry_guid = $item["guid"];
119 if (!$entry_guid) $entry_guid = $item["link"];
466001c4
AD
120
121 if (!$entry_guid) continue;
a116f569 122
9c9c7e6b 123 $entry_timestamp = "";
b82af8c3 124
9c9c7e6b
AD
125 $rss_2_date = $item['pubdate'];
126 $rss_1_date = $item['dc']['date'];
127 $atom_date = $item['issued'];
b197f117 128
9c9c7e6b
AD
129 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
130 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
131 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
b82af8c3
AD
132
133 if ($entry_timestamp == "") {
134 $entry_timestamp = time();
135 $no_orig_date = 'true';
466001c4
AD
136 } else {
137 $no_orig_date = 'false';
b82af8c3 138 }
b197f117 139
466001c4 140 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
71ad3959 141
40d13c28
AD
142 $entry_title = $item["title"];
143 $entry_link = $item["link"];
71ad3959
AD
144
145 if (!$entry_title) continue;
146 if (!$entry_link) continue;
147
40d13c28 148 $entry_content = $item["description"];
466001c4 149 if (!$entry_content) $entry_content = $item["content:escaped"];
40d13c28 150 if (!$entry_content) $entry_content = $item["content"];
a2015351 151
a116f569 152// if (!$entry_content) continue;
a2015351 153
466001c4 154 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
cb0bd8bd 155
a1ea1e12
AD
156 $entry_comments = $item["comments"];
157
2651fc4f
AD
158 $entry_guid = pg_escape_string($entry_guid);
159
40d13c28
AD
160 $result = pg_query($link, "
161 SELECT
466001c4 162 id,last_read,no_orig_date,title,feed_id,content_hash,
b197f117 163 EXTRACT(EPOCH FROM updated) as updated_timestamp
40d13c28
AD
164 FROM
165 ttrss_entries
166 WHERE
a2015351 167 guid = '$entry_guid'");
466001c4 168
40d13c28 169 if (pg_num_rows($result) == 0) {
466001c4 170
2651fc4f 171 //$entry_guid = pg_escape_string($entry_guid);
466001c4
AD
172 $entry_content = pg_escape_string($entry_content);
173 $entry_title = pg_escape_string($entry_title);
174 $entry_link = pg_escape_string($entry_link);
a1ea1e12 175 $entry_comments = pg_escape_string($entry_comments);
466001c4
AD
176
177 $query = "INSERT
178 INTO ttrss_entries
179 (title,
180 guid,
181 link,
182 updated,
183 content,
184 content_hash,
185 feed_id,
a1ea1e12 186 comments,
466001c4 187 no_orig_date)
40d13c28 188 VALUES
466001c4
AD
189 ('$entry_title',
190 '$entry_guid',
191 '$entry_link',
192 '$entry_timestamp_fmt',
193 '$entry_content',
194 '$content_hash',
195 '$feed',
a1ea1e12 196 '$entry_comments',
466001c4
AD
197 $no_orig_date)";
198
76798ff3
AD
199 $result = pg_query($link, $query);
200
40d13c28 201 } else {
466001c4
AD
202
203 $orig_entry_id = pg_fetch_result($result, 0, "id");
204 $orig_feed_id = pg_fetch_result($result, 0, "feed_id");
205
206 if ($orig_feed_id != $feed) {
207// print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
208 continue;
209 }
ad3024fc
AD
210
211 $entry_is_modified = false;
466001c4
AD
212
213 $orig_timestamp = pg_fetch_result($result, 0, "updated_timestamp");
214 $orig_content_hash = pg_fetch_result($result, 0, "content_hash");
215 $orig_last_read = pg_fetch_result($result, 0, "last_read");
216 $orig_no_orig_date = pg_fetch_result($result, 0, "no_orig_date");
b82af8c3 217 $orig_title = pg_fetch_result($result, 0, "title");
cac95b8d 218
2d84262b
AD
219 $last_read_qpart = "";
220
221// if ("$orig_title" != "$entry_title") {
222// $last_read_qpart = 'last_read = null,';
223// }
224
ad3024fc
AD
225 if ($orig_content_hash != $content_hash) {
226 if (UPDATE_POST_ON_CHECKSUM_CHANGE) {
227 $last_read_qpart = 'last_read = null,';
228 }
229 $entry_is_modified = true;
230 }
231
232 if ($orig_title != $entry_title) {
233 $entry_is_modified = true;
234 }
235
236 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
237 $entry_is_modified = true;
cac95b8d
AD
238 }
239
a1ea1e12 240// if (!$no_orig_date && $orig_timestamp < $entry_timestamp) {
f48ba3c9
AD
241// $last_read_qpart = 'last_read = null,';
242// }
a2015351 243
ad3024fc 244 if ($entry_is_modified) {
a2015351 245
ad3024fc
AD
246 $entry_comments = pg_escape_string($entry_comments);
247 $entry_content = pg_escape_string($entry_content);
248 $entry_title = pg_escape_string($entry_title);
249 $entry_link = pg_escape_string($entry_link);
a2015351 250
ad3024fc
AD
251// print "update object $entry_guid<br>";
252
253 $query = "UPDATE ttrss_entries
254 SET
255 $last_read_qpart
256 title = '$entry_title',
257 link = '$entry_link',
258 updated = '$entry_timestamp_fmt',
259 content = '$entry_content',
260 comments = '$entry_comments',
261 content_hash = '$content_hash'
262 WHERE
263 id = '$orig_entry_id'";
264
265 $result = pg_query($link, $query);
266 }
466001c4 267 }
40d13c28
AD
268 }
269
76798ff3
AD
270 if ($result) {
271 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
272 }
40d13c28
AD
273
274 }
275
f48ba3c9
AD
276 pg_query("COMMIT");
277
40d13c28
AD
278 }
279
280
40d13c28 281?>