]> git.wh0rd.org Git - tt-rss.git/blob - functions.php
e769e79ea38305be5bd0ea6089bfb4290decf475
[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) {
8                         $result = pg_query("DELETE FROM ttrss_entries WHERE
9                                 date_entered < NOW() - INTERVAL '30 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                         foreach ($rss->items as $item) {
115         
116                                 $entry_guid = $item["id"];
117         
118                                 if (!$entry_guid) $entry_guid = $item["guid"];
119                                 if (!$entry_guid) $entry_guid = $item["link"];
120
121                                 if (!$entry_guid) continue;
122         
123                                 $entry_timestamp = "";
124
125                                 $rss_2_date = $item['pubdate'];
126                                 $rss_1_date = $item['dc']['date'];
127                                 $atom_date = $item['issued'];
128                         
129                                 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
130                                 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
131                                 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
132                                 
133                                 if ($entry_timestamp == "") {
134                                         $entry_timestamp = time();
135                                         $no_orig_date = 'true';
136                                 } else {
137                                         $no_orig_date = 'false';
138                                 }
139
140                                 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
141
142                                 $entry_title = $item["title"];
143                                 $entry_link = $item["link"];
144
145                                 if (!$entry_title) continue;
146                                 if (!$entry_link) continue;
147
148                                 $entry_content = $item["description"];
149                                 if (!$entry_content) $entry_content = $item["content:escaped"];
150                                 if (!$entry_content) $entry_content = $item["content"];
151
152                                 if (!$entry_content) continue;
153
154                                 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
155
156                                 $result = pg_query($link, "
157                                         SELECT 
158                                                 id,last_read,no_orig_date,title,feed_id,content_hash,
159                                                 EXTRACT(EPOCH FROM updated) as updated_timestamp
160                                         FROM
161                                                 ttrss_entries 
162                                         WHERE
163                                                 guid = '$entry_guid'");
164
165                                 if (pg_num_rows($result) == 0) {
166
167                                         $entry_content = pg_escape_string($entry_content);
168                                         $entry_title = pg_escape_string($entry_title);
169                                         $entry_link = pg_escape_string($entry_link);
170
171                                         $query = "INSERT 
172                                                 INTO ttrss_entries 
173                                                         (title, 
174                                                         guid, 
175                                                         link, 
176                                                         updated, 
177                                                         content, 
178                                                         content_hash,
179                                                         feed_id, 
180                                                         no_orig_date) 
181                                                 VALUES
182                                                         ('$entry_title', 
183                                                         '$entry_guid', 
184                                                         '$entry_link', 
185                                                         '$entry_timestamp_fmt', 
186                                                         '$entry_content', 
187                                                         '$content_hash',
188                                                         '$feed', 
189                                                         $no_orig_date)";
190
191                                         $result = pg_query($link, $query);
192
193                                 } else {
194
195                                         $orig_entry_id = pg_fetch_result($result, 0, "id");                     
196                                         $orig_feed_id = pg_fetch_result($result, 0, "feed_id");
197
198                                         if ($orig_feed_id != $feed) {
199 //                                              print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
200                                                 continue;
201                                         }
202                                         
203                                         $orig_timestamp = pg_fetch_result($result, 0, "updated_timestamp");
204                                         $orig_content_hash = pg_fetch_result($result, 0, "content_hash");
205                                         $orig_last_read = pg_fetch_result($result, 0, "last_read");     
206                                         $orig_no_orig_date = pg_fetch_result($result, 0, "no_orig_date");
207                                         $orig_title = pg_fetch_result($result, 0, "title");
208
209                                         $last_read_qpart = "";
210
211 //                                      if ("$orig_title" != "$entry_title") {
212 //                                              $last_read_qpart = 'last_read = null,';
213 //                                      }
214                                 
215                                         if (UPDATE_POST_ON_CHECKSUM_CHANGE && 
216                                                         $orig_content_hash != $content_hash) {
217                                                 $last_read_qpart = 'last_read = null,';
218                                         }
219
220 //                                      if ($orig_timestamp < $entry_timestamp) {
221 //                                              $last_read_qpart = 'last_read = null,';
222 //                                      }
223
224                                         $entry_content = pg_escape_string($entry_content);
225                                         $entry_title = pg_escape_string($entry_title);
226                                         $entry_link = pg_escape_string($entry_link);
227
228                                         $query = "UPDATE ttrss_entries 
229                                                 SET 
230                                                         $last_read_qpart 
231                                                         title = '$entry_title',
232                                                         link = '$entry_link', 
233                                                         updated = '$entry_timestamp_fmt',
234                                                         content = '$entry_content',
235                                                         content_hash = '$content_hash'
236                                                 WHERE
237                                                         id = '$orig_entry_id'";
238
239                                         $result = pg_query($link, $query);
240
241                                 }
242                         }
243
244                         if ($result) {
245                                 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
246                         }
247
248                 }
249
250                 pg_query("COMMIT");
251
252         }
253
254
255 ?>