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