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