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