]> git.wh0rd.org - tt-rss.git/blob - functions.php
miscellaneous MSIE compatibility fixes
[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 FROM ttrss_feeds");
21
22 while ($line = db_fetch_assoc($result)) {
23 update_rss_feed($link, $line["feed_url"], $line["id"]);
24 }
25
26 purge_old_posts($link);
27
28 db_query($link, "COMMIT");
29
30 }
31
32 function check_feed_favicon($feed_url, $feed) {
33 $feed_url = str_replace("http://", "", $feed_url);
34 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
35
36 $icon_url = "http://$feed_url/favicon.ico";
37 $icon_file = ICONS_DIR . "/$feed.ico";
38
39 if (!file_exists($icon_file)) {
40
41 error_reporting(0);
42 $r = fopen($icon_url, "r");
43 error_reporting (E_ERROR | E_WARNING | E_PARSE);
44
45 if ($r) {
46 $tmpfname = tempnam("/tmp", "ttrssicon");
47
48 $t = fopen($tmpfname, "w");
49
50 while (!feof($r)) {
51 $buf = fread($r, 16384);
52 fwrite($t, $buf);
53 }
54
55 fclose($r);
56 fclose($t);
57
58 error_reporting(0);
59 if (!rename($tmpfname, $icon_file)) {
60 unlink($tmpfname);
61 }
62 error_reporting (E_ERROR | E_WARNING | E_PARSE);
63
64 }
65 }
66 }
67
68 function update_rss_feed($link, $feed_url, $feed) {
69
70 if (WEB_DEMO_MODE) return;
71
72 error_reporting(0);
73 $rss = fetch_rss($feed_url);
74 error_reporting (E_ERROR | E_WARNING | E_PARSE);
75
76 db_query($link, "BEGIN");
77
78 $feed = db_escape_string($feed);
79
80 if ($rss) {
81
82 if (ENABLE_FEED_ICONS) {
83 check_feed_favicon($feed_url, $feed);
84 }
85
86 $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
87
88 $registered_title = db_fetch_result($result, 0, "title");
89 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
90
91 if (!$registered_title) {
92 $feed_title = $rss->channel["title"];
93 db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
94 }
95
96 // print "I: " . $rss->channel["image"]["url"];
97
98 $icon_url = $rss->image["url"];
99
100 if ($icon_url && !$orig_icon_url) {
101 $icon_url = db_escape_string($icon_url);
102 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
103 }
104
105
106 $filters = array();
107
108 $result = db_query($link, "SELECT reg_exp,
109 (SELECT name FROM ttrss_filter_types
110 WHERE id = filter_type) as name
111 FROM ttrss_filters");
112
113 while ($line = db_fetch_assoc($result)) {
114 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
115 array_push($filters[$line["name"]], $line["reg_exp"]);
116 }
117
118 foreach ($rss->items as $item) {
119
120 $entry_guid = $item["id"];
121
122 if (!$entry_guid) $entry_guid = $item["guid"];
123 if (!$entry_guid) $entry_guid = $item["link"];
124
125 if (!$entry_guid) continue;
126
127 $entry_timestamp = "";
128
129 $rss_2_date = $item['pubdate'];
130 $rss_1_date = $item['dc']['date'];
131 $atom_date = $item['issued'];
132
133 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
134 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
135 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
136
137 if ($entry_timestamp == "") {
138 $entry_timestamp = time();
139 $no_orig_date = 'true';
140 } else {
141 $no_orig_date = 'false';
142 }
143
144 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
145
146 $entry_title = $item["title"];
147 $entry_link = $item["link"];
148
149 if (!$entry_title) continue;
150 if (!$entry_link) continue;
151
152 $entry_content = $item["description"];
153 if (!$entry_content) $entry_content = $item["content:escaped"];
154 if (!$entry_content) $entry_content = $item["content"];
155
156 // if (!$entry_content) continue;
157
158 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
159
160 $entry_comments = $item["comments"];
161
162 $entry_guid = db_escape_string($entry_guid);
163
164 if (DB_TYPE == "pgsql") {
165 $extract_ts_qpart = ",EXTRACT(EPOCH FROM updated) as updated_timestamp";
166 }
167
168 $result = db_query($link, "
169 SELECT
170 id,last_read,no_orig_date,title,feed_id,content_hash
171 $extract_ts_qpart
172 FROM
173 ttrss_entries
174 WHERE
175 guid = '$entry_guid'");
176
177 if (db_num_rows($result) == 0) {
178
179 error_reporting(0);
180 if (is_filtered($entry_title, $entry_content, $filters)) {
181 continue;
182 }
183 error_reporting (E_ERROR | E_WARNING | E_PARSE);
184
185 //$entry_guid = db_escape_string($entry_guid);
186 $entry_content = db_escape_string($entry_content);
187 $entry_title = db_escape_string($entry_title);
188 $entry_link = db_escape_string($entry_link);
189 $entry_comments = db_escape_string($entry_comments);
190
191 $query = "INSERT
192 INTO ttrss_entries
193 (title,
194 guid,
195 link,
196 updated,
197 content,
198 content_hash,
199 feed_id,
200 comments,
201 no_orig_date)
202 VALUES
203 ('$entry_title',
204 '$entry_guid',
205 '$entry_link',
206 '$entry_timestamp_fmt',
207 '$entry_content',
208 '$content_hash',
209 '$feed',
210 '$entry_comments',
211 $no_orig_date)";
212
213 $result = db_query($link, $query);
214
215 } else {
216
217 $orig_entry_id = db_fetch_result($result, 0, "id");
218 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
219
220 if ($orig_feed_id != $feed) {
221 // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
222 continue;
223 }
224
225 $entry_is_modified = false;
226
227 $orig_timestamp = db_fetch_result($result, 0, "updated_timestamp");
228 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
229 $orig_last_read = db_fetch_result($result, 0, "last_read");
230 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
231 $orig_title = db_fetch_result($result, 0, "title");
232
233 $last_read_qpart = "";
234
235 if ($orig_content_hash != $content_hash) {
236 if (UPDATE_POST_ON_CHECKSUM_CHANGE) {
237 $last_read_qpart = 'last_read = null,';
238 }
239 $entry_is_modified = true;
240 }
241
242 if ($orig_title != $entry_title) {
243 $entry_is_modified = true;
244 }
245
246 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
247 $entry_is_modified = true;
248 }
249
250 if ($entry_is_modified) {
251
252 $entry_comments = db_escape_string($entry_comments);
253 $entry_content = db_escape_string($entry_content);
254 $entry_title = db_escape_string($entry_title);
255 $entry_link = db_escape_string($entry_link);
256
257 $query = "UPDATE ttrss_entries
258 SET
259 $last_read_qpart
260 title = '$entry_title',
261 link = '$entry_link',
262 updated = '$entry_timestamp_fmt',
263 content = '$entry_content',
264 comments = '$entry_comments',
265 content_hash = '$content_hash'
266 WHERE
267 id = '$orig_entry_id'";
268
269 $result = db_query($link, $query);
270 }
271 }
272 }
273
274 if ($result) {
275 $result = db_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
276 }
277
278 }
279
280 db_query($link, "COMMIT");
281
282 }
283
284 function print_select($id, $default, $values, $attributes = "") {
285 print "<select id=\"$id\" $attributes>";
286 foreach ($values as $v) {
287 if ($v == $default)
288 $sel = " selected";
289 else
290 $sel = "";
291
292 print "<option$sel>$v</option>";
293 }
294 print "</select>";
295 }
296
297 function is_filtered($title, $content, $filters) {
298
299 if ($filters["title"]) {
300 foreach ($filters["title"] as $title_filter) {
301 if (preg_match("/$title_filter/i", $title))
302 return true;
303 }
304 }
305
306 if ($filters["content"]) {
307 foreach ($filters["content"] as $content_filter) {
308 if (preg_match("/$content_filter/i", $content))
309 return true;
310 }
311 }
312
313 if ($filters["both"]) {
314 foreach ($filters["both"] as $filter) {
315 if (preg_match("/$filter/i", $title) || preg_match("/$filter/i", $content))
316 return true;
317 }
318 }
319
320 return false;
321 }
322
323 ?>