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