]> git.wh0rd.org - tt-rss.git/blob - functions.php
fix auto expiration bug on MySQL backend
[tt-rss.git] / functions.php
1 <?
2 require_once 'config.php';
3
4 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
5
6 function purge_old_posts($link) {
7 if (PURGE_OLD_DAYS > 0) {
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 }
18 }
19 }
20
21 function update_all_feeds($link, $fetch) {
22
23 if (WEB_DEMO_MODE) return;
24
25 db_query($link, "BEGIN");
26
27 $result = db_query($link, "SELECT feed_url,id,last_updated FROM ttrss_feeds");
28
29 while ($line = db_fetch_assoc($result)) {
30 if (!$line["last_updated"] || time() - strtotime($line["last_updated"]) > 1800) {
31 update_rss_feed($link, $line["feed_url"], $line["id"]);
32 }
33 }
34
35 purge_old_posts($link);
36
37 db_query($link, "COMMIT");
38
39 }
40
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)) {
49
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
67 error_reporting(0);
68 if (!rename($tmpfname, $icon_file)) {
69 unlink($tmpfname);
70 }
71 error_reporting (E_ERROR | E_WARNING | E_PARSE);
72
73 }
74 }
75 }
76
77 function update_rss_feed($link, $feed_url, $feed) {
78
79 if (WEB_DEMO_MODE) return;
80
81 error_reporting(0);
82 $rss = fetch_rss($feed_url);
83 error_reporting (E_ERROR | E_WARNING | E_PARSE);
84
85 db_query($link, "BEGIN");
86
87 $feed = db_escape_string($feed);
88
89 if ($rss) {
90
91 if (ENABLE_FEED_ICONS) {
92 check_feed_favicon($feed_url, $feed);
93 }
94
95 $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
96
97 $registered_title = db_fetch_result($result, 0, "title");
98 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
99
100 if (!$registered_title) {
101 $feed_title = db_escape_string($rss->channel["title"]);
102 db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
103 }
104
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 = db_escape_string($icon_url);
111 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
112 }
113
114
115 $filters = array();
116
117 $result = db_query($link, "SELECT reg_exp,
118 (SELECT name FROM ttrss_filter_types
119 WHERE id = filter_type) as name
120 FROM ttrss_filters");
121
122 while ($line = db_fetch_assoc($result)) {
123 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
124 array_push($filters[$line["name"]], $line["reg_exp"]);
125 }
126
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"];
133
134 if (!$entry_guid) continue;
135
136 $entry_timestamp = "";
137
138 $rss_2_date = $item['pubdate'];
139 $rss_1_date = $item['dc']['date'];
140 $atom_date = $item['issued'];
141
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);
145
146 if ($entry_timestamp == "") {
147 $entry_timestamp = time();
148 $no_orig_date = 'true';
149 } else {
150 $no_orig_date = 'false';
151 }
152
153 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
154
155 $entry_title = $item["title"];
156 $entry_link = $item["link"];
157
158 if (!$entry_title) continue;
159 if (!$entry_link) continue;
160
161 $entry_content = $item["description"];
162 if (!$entry_content) $entry_content = $item["content:escaped"];
163 if (!$entry_content) $entry_content = $item["content"];
164
165 // if (!$entry_content) continue;
166
167 // WTF
168 if (is_array($entry_content)) {
169 $entry_content = $entry_content["encoded"];
170 }
171
172 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
173
174 $entry_comments = $item["comments"];
175
176 $entry_guid = db_escape_string($entry_guid);
177
178 if (DB_TYPE == "pgsql") {
179 $extract_ts_qpart = ",EXTRACT(EPOCH FROM updated) as updated_timestamp";
180 }
181
182 $result = db_query($link, "
183 SELECT
184 id,last_read,no_orig_date,title,feed_id,content_hash
185 $extract_ts_qpart
186 FROM
187 ttrss_entries
188 WHERE
189 guid = '$entry_guid'");
190
191 if (db_num_rows($result) == 0) {
192
193 error_reporting(0);
194 if (is_filtered($entry_title, $entry_content, $filters)) {
195 continue;
196 }
197 error_reporting (E_ERROR | E_WARNING | E_PARSE);
198
199 //$entry_guid = db_escape_string($entry_guid);
200 $entry_content = db_escape_string($entry_content);
201 $entry_title = db_escape_string($entry_title);
202 $entry_link = db_escape_string($entry_link);
203 $entry_comments = db_escape_string($entry_comments);
204
205 $query = "INSERT
206 INTO ttrss_entries
207 (title,
208 guid,
209 link,
210 updated,
211 content,
212 content_hash,
213 feed_id,
214 comments,
215 no_orig_date,
216 date_entered)
217 VALUES
218 ('$entry_title',
219 '$entry_guid',
220 '$entry_link',
221 '$entry_timestamp_fmt',
222 '$entry_content',
223 '$content_hash',
224 '$feed',
225 '$entry_comments',
226 $no_orig_date,
227 NOW())";
228
229 $result = db_query($link, $query);
230
231 } else {
232
233 $orig_entry_id = db_fetch_result($result, 0, "id");
234 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
235
236 if ($orig_feed_id != $feed) {
237 // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
238 continue;
239 }
240
241 $entry_is_modified = false;
242
243 $orig_timestamp = db_fetch_result($result, 0, "updated_timestamp");
244 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
245 $orig_last_read = db_fetch_result($result, 0, "last_read");
246 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
247 $orig_title = db_fetch_result($result, 0, "title");
248
249 $last_read_qpart = "";
250
251 if ($orig_content_hash != $content_hash) {
252 if (UPDATE_POST_ON_CHECKSUM_CHANGE) {
253 $last_read_qpart = 'last_read = null,';
254 }
255 $entry_is_modified = true;
256 }
257
258 if ($orig_title != $entry_title) {
259 $entry_is_modified = true;
260 }
261
262 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
263 $entry_is_modified = true;
264 }
265
266 if ($entry_is_modified) {
267
268 $entry_comments = db_escape_string($entry_comments);
269 $entry_content = db_escape_string($entry_content);
270 $entry_title = db_escape_string($entry_title);
271 $entry_link = db_escape_string($entry_link);
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 = db_query($link, $query);
286 }
287 }
288
289 /* taaaags */
290 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
291
292 $entry_tags = null;
293
294 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content,
295 $entry_tags);
296
297 $entry_tags = $entry_tags[1];
298
299 if (count($entry_tags) > 0) {
300
301 $result = db_query($link, "SELECT id FROM ttrss_entries
302 WHERE guid = '$entry_guid'");
303
304 if (!$result || db_num_rows($result) != 1) {
305 return;
306 }
307
308 $entry_id = db_fetch_result($result, 0, "id");
309
310 foreach ($entry_tags as $tag) {
311 $tag = db_escape_string(strtolower($tag));
312
313 $result = db_query($link, "SELECT id FROM ttrss_tags
314 WHERE tag_name = '$tag' AND post_id = '$entry_id' LIMIT 1");
315
316 if ($result && db_num_rows($result) == 0) {
317
318 // print "tagging $entry_id as $tag<br>";
319
320 db_query($link, "INSERT INTO ttrss_tags (tag_name,post_id)
321 VALUES ('$tag', '$entry_id')");
322 }
323 }
324 }
325 }
326
327 db_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
328
329 }
330
331 db_query($link, "COMMIT");
332
333 }
334
335 function print_select($id, $default, $values, $attributes = "") {
336 print "<select id=\"$id\" $attributes>";
337 foreach ($values as $v) {
338 if ($v == $default)
339 $sel = " selected";
340 else
341 $sel = "";
342
343 print "<option$sel>$v</option>";
344 }
345 print "</select>";
346 }
347
348 function is_filtered($title, $content, $filters) {
349
350 if ($filters["title"]) {
351 foreach ($filters["title"] as $title_filter) {
352 if (preg_match("/$title_filter/i", $title))
353 return true;
354 }
355 }
356
357 if ($filters["content"]) {
358 foreach ($filters["content"] as $content_filter) {
359 if (preg_match("/$content_filter/i", $content))
360 return true;
361 }
362 }
363
364 if ($filters["both"]) {
365 foreach ($filters["both"] as $filter) {
366 if (preg_match("/$filter/i", $title) || preg_match("/$filter/i", $content))
367 return true;
368 }
369 }
370
371 return false;
372 }
373
374 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file) {
375
376 if (file_exists($icon_file) && filesize($icon_file) > 0) {
377 $feed_icon = "<img src=\"$icon_file\">";
378 } else {
379 $feed_icon = "<img src=\"images/blank_icon.gif\">";
380 }
381
382 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
383
384 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
385 if (ENABLE_FEED_ICONS) {
386 print "$feed_icon";
387 }
388
389 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
390
391 if ($unread != 0) {
392 $fctr_class = "";
393 } else {
394 $fctr_class = "class=\"invisible\"";
395 }
396
397 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
398 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
399
400 print "</li>";
401
402 }
403
404 ?>