]> git.wh0rd.org - tt-rss.git/blob - functions.php
bea443c2765d283ca445c7f253a549dfc84da8a1
[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 $result = db_query($link, "
175 SELECT
176 id,last_read,no_orig_date,title,feed_id,content_hash,
177 EXTRACT(EPOCH FROM updated) as updated_timestamp
178 FROM
179 ttrss_entries
180 WHERE
181 guid = '$entry_guid'");
182
183 if (db_num_rows($result) == 0) {
184
185 error_reporting(0);
186 if (is_filtered($entry_title, $entry_content, $filters)) {
187 continue;
188 }
189 error_reporting (E_ERROR | E_WARNING | E_PARSE);
190
191 //$entry_guid = db_escape_string($entry_guid);
192 $entry_content = db_escape_string($entry_content);
193 $entry_title = db_escape_string($entry_title);
194 $entry_link = db_escape_string($entry_link);
195 $entry_comments = db_escape_string($entry_comments);
196
197 $query = "INSERT
198 INTO ttrss_entries
199 (title,
200 guid,
201 link,
202 updated,
203 content,
204 content_hash,
205 feed_id,
206 comments,
207 no_orig_date)
208 VALUES
209 ('$entry_title',
210 '$entry_guid',
211 '$entry_link',
212 '$entry_timestamp_fmt',
213 '$entry_content',
214 '$content_hash',
215 '$feed',
216 '$entry_comments',
217 $no_orig_date)";
218
219 $result = db_query($link, $query);
220
221 } else {
222
223 $orig_entry_id = db_fetch_result($result, 0, "id");
224 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
225
226 if ($orig_feed_id != $feed) {
227 // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
228 continue;
229 }
230
231 $entry_is_modified = false;
232
233 $orig_timestamp = db_fetch_result($result, 0, "updated_timestamp");
234 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
235 $orig_last_read = db_fetch_result($result, 0, "last_read");
236 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
237 $orig_title = db_fetch_result($result, 0, "title");
238
239 $last_read_qpart = "";
240
241 if ($orig_content_hash != $content_hash) {
242 if (UPDATE_POST_ON_CHECKSUM_CHANGE) {
243 $last_read_qpart = 'last_read = null,';
244 }
245 $entry_is_modified = true;
246 }
247
248 if ($orig_title != $entry_title) {
249 $entry_is_modified = true;
250 }
251
252 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
253 $entry_is_modified = true;
254 }
255
256 if ($entry_is_modified) {
257
258 $entry_comments = db_escape_string($entry_comments);
259 $entry_content = db_escape_string($entry_content);
260 $entry_title = db_escape_string($entry_title);
261 $entry_link = db_escape_string($entry_link);
262
263 $query = "UPDATE ttrss_entries
264 SET
265 $last_read_qpart
266 title = '$entry_title',
267 link = '$entry_link',
268 updated = '$entry_timestamp_fmt',
269 content = '$entry_content',
270 comments = '$entry_comments',
271 content_hash = '$content_hash'
272 WHERE
273 id = '$orig_entry_id'";
274
275 $result = db_query($link, $query);
276 }
277 }
278 }
279
280 if ($result) {
281 $result = db_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
282 }
283
284 }
285
286 db_query($link, "COMMIT");
287
288 }
289
290 function print_select($id, $default, $values, $attributes = "") {
291 print "<select id=\"$id\" $attributes>";
292 foreach ($values as $v) {
293 if ($v == $default)
294 $sel = " selected";
295 else
296 $sel = "";
297
298 print "<option$sel>$v</option>";
299 }
300 print "</select>";
301 }
302
303 function is_filtered($title, $content, $filters) {
304
305 if ($filters["title"]) {
306 foreach ($filters["title"] as $title_filter) {
307 if (preg_match("/$title_filter/i", $title))
308 return true;
309 }
310 }
311
312 if ($filters["content"]) {
313 foreach ($filters["content"] as $content_filter) {
314 if (preg_match("/$content_filter/i", $content))
315 return true;
316 }
317 }
318
319 if ($filters["both"]) {
320 foreach ($filters["both"] as $filter) {
321 if (preg_match("/$filter/i", $title) || preg_match("/$filter/i", $content))
322 return true;
323 }
324 }
325
326 return false;
327 }
328
329 ?>