]> git.wh0rd.org Git - tt-rss.git/blob - functions.php
make Title matching default for filters, change description for filter "both"
[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() {
7                 if (PURGE_OLD_DAYS > 0) {
8                         $result = pg_query("DELETE FROM ttrss_entries WHERE
9                                 date_entered < NOW() - INTERVAL '".PURGE_OLD_DAYS." days'");
10                 }
11         }
12
13         function update_all_feeds($link, $fetch) {
14
15                 if (WEB_DEMO_MODE) return;
16
17                 pg_query("BEGIN");
18
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                 }
30
31                 while ($line = pg_fetch_assoc($result)) {
32                         update_rss_feed($link, $line["feed_url"], $line["id"]);
33                 }
34
35                 purge_old_posts();
36
37                 pg_query("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                 pg_query("BEGIN");
86
87                 $feed = pg_escape_string($feed);
88         
89                 if ($rss) {
90
91                         if (ENABLE_FEED_ICONS) {        
92                                 check_feed_favicon($feed_url, $feed);
93                         }
94                 
95                         $result = pg_query("SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
96
97                         $registered_title = pg_fetch_result($result, 0, "title");
98                         $orig_icon_url = pg_fetch_result($result, 0, "icon_url");
99
100                         if (!$registered_title) {
101                                 $feed_title = $rss->channel["title"];
102                                 pg_query("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 = pg_escape_string($icon_url);
111                                 pg_query("UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
112                         }
113
114
115                         $filters = array();
116
117                         $result = pg_query("SELECT regexp,
118                                 (SELECT name FROM ttrss_filter_types
119                                         WHERE id = filter_type) as name
120                                 FROM ttrss_filters");
121
122                         while ($line = pg_fetch_assoc($result)) {
123                                 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
124                                 array_push($filters[$line["name"]], $line["regexp"]);
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                                 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
168
169                                 $entry_comments = $item["comments"];
170
171                                 $entry_guid = pg_escape_string($entry_guid);
172
173                                 $result = pg_query($link, "
174                                         SELECT 
175                                                 id,last_read,no_orig_date,title,feed_id,content_hash,
176                                                 EXTRACT(EPOCH FROM updated) as updated_timestamp
177                                         FROM
178                                                 ttrss_entries 
179                                         WHERE
180                                                 guid = '$entry_guid'");
181
182                                 if (pg_num_rows($result) == 0) {
183
184                                         error_reporting(0);
185                                         if (is_filtered($entry_title, $entry_content, $filters)) {
186                                                 continue;
187                                         }
188                                         error_reporting (E_ERROR | E_WARNING | E_PARSE);
189
190                                         //$entry_guid = pg_escape_string($entry_guid);
191                                         $entry_content = pg_escape_string($entry_content);
192                                         $entry_title = pg_escape_string($entry_title);
193                                         $entry_link = pg_escape_string($entry_link);
194                                         $entry_comments = pg_escape_string($entry_comments);
195
196                                         $query = "INSERT 
197                                                 INTO ttrss_entries 
198                                                         (title, 
199                                                         guid, 
200                                                         link, 
201                                                         updated, 
202                                                         content, 
203                                                         content_hash,
204                                                         feed_id, 
205                                                         comments,
206                                                         no_orig_date) 
207                                                 VALUES
208                                                         ('$entry_title', 
209                                                         '$entry_guid', 
210                                                         '$entry_link', 
211                                                         '$entry_timestamp_fmt', 
212                                                         '$entry_content', 
213                                                         '$content_hash',
214                                                         '$feed', 
215                                                         '$entry_comments',
216                                                         $no_orig_date)";
217
218                                         $result = pg_query($link, $query);
219
220                                 } else {
221
222                                         $orig_entry_id = pg_fetch_result($result, 0, "id");                     
223                                         $orig_feed_id = pg_fetch_result($result, 0, "feed_id");
224
225                                         if ($orig_feed_id != $feed) {
226 //                                              print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
227                                                 continue;
228                                         }
229
230                                         $entry_is_modified = false;
231                                         
232                                         $orig_timestamp = pg_fetch_result($result, 0, "updated_timestamp");
233                                         $orig_content_hash = pg_fetch_result($result, 0, "content_hash");
234                                         $orig_last_read = pg_fetch_result($result, 0, "last_read");     
235                                         $orig_no_orig_date = pg_fetch_result($result, 0, "no_orig_date");
236                                         $orig_title = pg_fetch_result($result, 0, "title");
237
238                                         $last_read_qpart = "";
239
240 //                                      if ("$orig_title" != "$entry_title") {
241 //                                              $last_read_qpart = 'last_read = null,';
242 //                                      }
243                                 
244                                         if ($orig_content_hash != $content_hash) {
245                                                 if (UPDATE_POST_ON_CHECKSUM_CHANGE) {
246                                                         $last_read_qpart = 'last_read = null,';
247                                                 }
248                                                 $entry_is_modified = true;                                              
249                                         }
250
251                                         if ($orig_title != $entry_title) {
252                                                 $entry_is_modified = true;
253                                         }
254
255                                         if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
256                                                 $entry_is_modified = true;
257                                         }
258
259 //                                      if (!$no_orig_date && $orig_timestamp < $entry_timestamp) {
260 //                                              $last_read_qpart = 'last_read = null,';
261 //                                      }
262
263                                         if ($entry_is_modified) {
264
265                                                 $entry_comments = pg_escape_string($entry_comments);
266                                                 $entry_content = pg_escape_string($entry_content);
267                                                 $entry_title = pg_escape_string($entry_title);                                  
268                                                 $entry_link = pg_escape_string($entry_link);
269
270 //                                              print "update object $entry_guid<br>";
271
272                                                 $query = "UPDATE ttrss_entries 
273                                                         SET 
274                                                                 $last_read_qpart 
275                                                                 title = '$entry_title',
276                                                                 link = '$entry_link', 
277                                                                 updated = '$entry_timestamp_fmt',
278                                                                 content = '$entry_content',
279                                                                 comments = '$entry_comments',
280                                                                 content_hash = '$content_hash'
281                                                         WHERE
282                                                                 id = '$orig_entry_id'";
283
284                                                 $result = pg_query($link, $query);
285                                         }
286                                 }
287                         }
288
289                         if ($result) {
290                                 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
291                         }
292
293                 }
294
295                 pg_query("COMMIT");
296
297         }
298
299         function print_select($id, $default, $values) {
300                 print "<select id=\"$id\">";
301                 foreach ($values as $v) {
302                         if ($v == $default)
303                                 $sel = " selected";
304                          else
305                                 $sel = "";
306                         
307                         print "<option$sel>$v</option>";
308                 }
309                 print "</select>";
310         }
311
312         function is_filtered($title, $content, $filters) {
313
314                 if ($filters["title"]) {
315                         foreach ($filters["title"] as $title_filter) {                  
316                                 if (preg_match("/$title_filter/i", $title)) 
317                                         return true;
318                         }
319                 }
320
321                 if ($filters["content"]) {
322                         foreach ($filters["content"] as $content_filter) {                      
323                                 if (preg_match("/$content_filter/i", $content)) 
324                                         return true;
325                         }
326                 }
327
328                 if ($filters["both"]) {
329                         foreach ($filters["both"] as $filter) {                 
330                                 if (preg_match("/$filter/i", $title) || preg_match("/$filter/i", $content)) 
331                                         return true;
332                         }
333                 }
334
335                 return false;
336         }
337
338 ?>