]> git.wh0rd.org - tt-rss.git/blame - functions.php
protype unmark page/all buttons (disabled ATM)
[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
f4c10d44 9 marked = false AND
f92db4f5 10 date_entered < NOW() - INTERVAL '".PURGE_OLD_DAYS." days'");
c3a8d71a
AD
11 }
12 }
13
9c9c7e6b 14 function update_all_feeds($link, $fetch) {
40d13c28 15
b0b4abcf
AD
16 if (WEB_DEMO_MODE) return;
17
b82af8c3
AD
18 pg_query("BEGIN");
19
9c9c7e6b
AD
20 if (!$fetch) {
21
22 $result = pg_query($link, "SELECT feed_url,id FROM ttrss_feeds WHERE
23 last_updated is null OR title = '' OR
24 EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM last_updated) > " .
25 MIN_UPDATE_TIME);
26
27 } else {
28
29 $result = pg_query($link, "SELECT feed_url,id FROM ttrss_feeds");
30 }
40d13c28
AD
31
32 while ($line = pg_fetch_assoc($result)) {
466001c4 33 update_rss_feed($link, $line["feed_url"], $line["id"]);
40d13c28
AD
34 }
35
c3a8d71a
AD
36 purge_old_posts();
37
b82af8c3
AD
38 pg_query("COMMIT");
39
40d13c28
AD
40 }
41
78800912
AD
42 function check_feed_favicon($feed_url, $feed) {
43 $feed_url = str_replace("http://", "", $feed_url);
44 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
45
46 $icon_url = "http://$feed_url/favicon.ico";
47 $icon_file = ICONS_DIR . "/$feed.ico";
48
49 if (!file_exists($icon_file)) {
e695fdc8 50
78800912
AD
51 error_reporting(0);
52 $r = fopen($icon_url, "r");
53 error_reporting (E_ERROR | E_WARNING | E_PARSE);
54
55 if ($r) {
56 $tmpfname = tempnam("/tmp", "ttrssicon");
57
58 $t = fopen($tmpfname, "w");
59
60 while (!feof($r)) {
61 $buf = fread($r, 16384);
62 fwrite($t, $buf);
63 }
64
65 fclose($r);
66 fclose($t);
67
e695fdc8
AD
68 error_reporting(0);
69 if (!rename($tmpfname, $icon_file)) {
70 unlink($tmpfname);
71 }
72 error_reporting (E_ERROR | E_WARNING | E_PARSE);
78800912
AD
73
74 }
75 }
76 }
77
40d13c28
AD
78 function update_rss_feed($link, $feed_url, $feed) {
79
b0b4abcf
AD
80 if (WEB_DEMO_MODE) return;
81
3ad5aa85 82 error_reporting(0);
40d13c28 83 $rss = fetch_rss($feed_url);
3ad5aa85 84 error_reporting (E_ERROR | E_WARNING | E_PARSE);
76798ff3 85
f48ba3c9 86 pg_query("BEGIN");
b7f4bda2
AD
87
88 $feed = pg_escape_string($feed);
40d13c28
AD
89
90 if ($rss) {
b82af8c3 91
78800912
AD
92 if (ENABLE_FEED_ICONS) {
93 check_feed_favicon($feed_url, $feed);
94 }
95
b7f4bda2 96 $result = pg_query("SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
331900c6
AD
97
98 $registered_title = pg_fetch_result($result, 0, "title");
b7f4bda2 99 $orig_icon_url = pg_fetch_result($result, 0, "icon_url");
331900c6
AD
100
101 if (!$registered_title) {
331900c6
AD
102 $feed_title = $rss->channel["title"];
103 pg_query("UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
104 }
40d13c28 105
b7f4bda2
AD
106// print "I: " . $rss->channel["image"]["url"];
107
108 $icon_url = $rss->image["url"];
109
110 if ($icon_url && !$orig_icon_url) {
111 $icon_url = pg_escape_string($icon_url);
112 pg_query("UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
113 }
114
e6155a06
AD
115
116 $filters = array();
117
118 $result = pg_query("SELECT regexp,
119 (SELECT name FROM ttrss_filter_types
120 WHERE id = filter_type) as name
121 FROM ttrss_filters");
122
123 while ($line = pg_fetch_assoc($result)) {
124 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
125 array_push($filters[$line["name"]], $line["regexp"]);
126 }
127
40d13c28
AD
128 foreach ($rss->items as $item) {
129
130 $entry_guid = $item["id"];
131
132 if (!$entry_guid) $entry_guid = $item["guid"];
133 if (!$entry_guid) $entry_guid = $item["link"];
466001c4
AD
134
135 if (!$entry_guid) continue;
a116f569 136
9c9c7e6b 137 $entry_timestamp = "";
b82af8c3 138
9c9c7e6b
AD
139 $rss_2_date = $item['pubdate'];
140 $rss_1_date = $item['dc']['date'];
141 $atom_date = $item['issued'];
b197f117 142
9c9c7e6b
AD
143 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
144 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
145 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
b82af8c3
AD
146
147 if ($entry_timestamp == "") {
148 $entry_timestamp = time();
149 $no_orig_date = 'true';
466001c4
AD
150 } else {
151 $no_orig_date = 'false';
b82af8c3 152 }
b197f117 153
466001c4 154 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
71ad3959 155
40d13c28
AD
156 $entry_title = $item["title"];
157 $entry_link = $item["link"];
71ad3959
AD
158
159 if (!$entry_title) continue;
160 if (!$entry_link) continue;
161
40d13c28 162 $entry_content = $item["description"];
466001c4 163 if (!$entry_content) $entry_content = $item["content:escaped"];
40d13c28 164 if (!$entry_content) $entry_content = $item["content"];
a2015351 165
a116f569 166// if (!$entry_content) continue;
a2015351 167
466001c4 168 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
cb0bd8bd 169
a1ea1e12
AD
170 $entry_comments = $item["comments"];
171
2651fc4f
AD
172 $entry_guid = pg_escape_string($entry_guid);
173
40d13c28
AD
174 $result = pg_query($link, "
175 SELECT
466001c4 176 id,last_read,no_orig_date,title,feed_id,content_hash,
b197f117 177 EXTRACT(EPOCH FROM updated) as updated_timestamp
40d13c28
AD
178 FROM
179 ttrss_entries
180 WHERE
a2015351 181 guid = '$entry_guid'");
466001c4 182
40d13c28 183 if (pg_num_rows($result) == 0) {
466001c4 184
e6155a06
AD
185 error_reporting(0);
186 if (is_filtered($entry_title, $entry_content, $filters)) {
187 continue;
188 }
189 error_reporting (E_ERROR | E_WARNING | E_PARSE);
190
2651fc4f 191 //$entry_guid = pg_escape_string($entry_guid);
466001c4
AD
192 $entry_content = pg_escape_string($entry_content);
193 $entry_title = pg_escape_string($entry_title);
194 $entry_link = pg_escape_string($entry_link);
a1ea1e12 195 $entry_comments = pg_escape_string($entry_comments);
466001c4
AD
196
197 $query = "INSERT
198 INTO ttrss_entries
199 (title,
200 guid,
201 link,
202 updated,
203 content,
204 content_hash,
205 feed_id,
a1ea1e12 206 comments,
466001c4 207 no_orig_date)
40d13c28 208 VALUES
466001c4
AD
209 ('$entry_title',
210 '$entry_guid',
211 '$entry_link',
212 '$entry_timestamp_fmt',
213 '$entry_content',
214 '$content_hash',
215 '$feed',
a1ea1e12 216 '$entry_comments',
466001c4
AD
217 $no_orig_date)";
218
76798ff3
AD
219 $result = pg_query($link, $query);
220
40d13c28 221 } else {
466001c4
AD
222
223 $orig_entry_id = pg_fetch_result($result, 0, "id");
224 $orig_feed_id = pg_fetch_result($result, 0, "feed_id");
225
226 if ($orig_feed_id != $feed) {
227// print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
228 continue;
229 }
ad3024fc
AD
230
231 $entry_is_modified = false;
466001c4
AD
232
233 $orig_timestamp = pg_fetch_result($result, 0, "updated_timestamp");
234 $orig_content_hash = pg_fetch_result($result, 0, "content_hash");
235 $orig_last_read = pg_fetch_result($result, 0, "last_read");
236 $orig_no_orig_date = pg_fetch_result($result, 0, "no_orig_date");
b82af8c3 237 $orig_title = pg_fetch_result($result, 0, "title");
cac95b8d 238
2d84262b
AD
239 $last_read_qpart = "";
240
241// if ("$orig_title" != "$entry_title") {
242// $last_read_qpart = 'last_read = null,';
243// }
244
ad3024fc
AD
245 if ($orig_content_hash != $content_hash) {
246 if (UPDATE_POST_ON_CHECKSUM_CHANGE) {
247 $last_read_qpart = 'last_read = null,';
248 }
249 $entry_is_modified = true;
250 }
251
252 if ($orig_title != $entry_title) {
253 $entry_is_modified = true;
254 }
255
256 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
257 $entry_is_modified = true;
cac95b8d
AD
258 }
259
a1ea1e12 260// if (!$no_orig_date && $orig_timestamp < $entry_timestamp) {
f48ba3c9
AD
261// $last_read_qpart = 'last_read = null,';
262// }
a2015351 263
ad3024fc 264 if ($entry_is_modified) {
a2015351 265
ad3024fc
AD
266 $entry_comments = pg_escape_string($entry_comments);
267 $entry_content = pg_escape_string($entry_content);
268 $entry_title = pg_escape_string($entry_title);
269 $entry_link = pg_escape_string($entry_link);
a2015351 270
ad3024fc
AD
271// print "update object $entry_guid<br>";
272
273 $query = "UPDATE ttrss_entries
274 SET
275 $last_read_qpart
276 title = '$entry_title',
277 link = '$entry_link',
278 updated = '$entry_timestamp_fmt',
279 content = '$entry_content',
280 comments = '$entry_comments',
281 content_hash = '$content_hash'
282 WHERE
283 id = '$orig_entry_id'";
284
285 $result = pg_query($link, $query);
286 }
466001c4 287 }
40d13c28
AD
288 }
289
76798ff3
AD
290 if ($result) {
291 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
292 }
40d13c28
AD
293
294 }
295
f48ba3c9
AD
296 pg_query("COMMIT");
297
40d13c28
AD
298 }
299
a0d53889
AD
300 function print_select($id, $default, $values) {
301 print "<select id=\"$id\">";
302 foreach ($values as $v) {
303 if ($v == $default)
304 $sel = " selected";
305 else
306 $sel = "";
307
308 print "<option$sel>$v</option>";
309 }
310 print "</select>";
311 }
40d13c28 312
e6155a06
AD
313 function is_filtered($title, $content, $filters) {
314
315 if ($filters["title"]) {
316 foreach ($filters["title"] as $title_filter) {
317 if (preg_match("/$title_filter/i", $title))
318 return true;
319 }
320 }
321
322 if ($filters["content"]) {
323 foreach ($filters["content"] as $content_filter) {
324 if (preg_match("/$content_filter/i", $content))
325 return true;
326 }
327 }
328
329 if ($filters["both"]) {
330 foreach ($filters["both"] as $filter) {
331 if (preg_match("/$filter/i", $title) || preg_match("/$filter/i", $content))
332 return true;
333 }
334 }
335
336 return false;
337 }
338
40d13c28 339?>