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