]> git.wh0rd.org - tt-rss.git/blame - functions.php
change ttrss_entries.title type to text
[tt-rss.git] / functions.php
CommitLineData
40d13c28
AD
1<?
2 require_once 'config.php';
3
a3ee2a38
AD
4 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
5
b6eefba5 6 function purge_old_posts($link) {
f92db4f5 7 if (PURGE_OLD_DAYS > 0) {
b6eefba5 8 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
f4c10d44 9 marked = false AND
f92db4f5 10 date_entered < NOW() - INTERVAL '".PURGE_OLD_DAYS." days'");
c3a8d71a
AD
11 }
12 }
13
9c9c7e6b 14 function update_all_feeds($link, $fetch) {
40d13c28 15
b0b4abcf
AD
16 if (WEB_DEMO_MODE) return;
17
b6eefba5 18 db_query($link, "BEGIN");
b82af8c3 19
8143ae1f 20 $result = db_query($link, "SELECT feed_url,id,last_updated FROM ttrss_feeds");
40d13c28 21
b6eefba5 22 while ($line = db_fetch_assoc($result)) {
8add756a 23 if (!$line["last_updated"] || time() - strtotime($line["last_updated"]) > 1800) {
8143ae1f
AD
24 update_rss_feed($link, $line["feed_url"], $line["id"]);
25 }
40d13c28
AD
26 }
27
b6eefba5 28 purge_old_posts($link);
c3a8d71a 29
b6eefba5 30 db_query($link, "COMMIT");
b82af8c3 31
40d13c28
AD
32 }
33
78800912
AD
34 function check_feed_favicon($feed_url, $feed) {
35 $feed_url = str_replace("http://", "", $feed_url);
36 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
37
38 $icon_url = "http://$feed_url/favicon.ico";
39 $icon_file = ICONS_DIR . "/$feed.ico";
40
41 if (!file_exists($icon_file)) {
e695fdc8 42
78800912
AD
43 error_reporting(0);
44 $r = fopen($icon_url, "r");
45 error_reporting (E_ERROR | E_WARNING | E_PARSE);
46
47 if ($r) {
48 $tmpfname = tempnam("/tmp", "ttrssicon");
49
50 $t = fopen($tmpfname, "w");
51
52 while (!feof($r)) {
53 $buf = fread($r, 16384);
54 fwrite($t, $buf);
55 }
56
57 fclose($r);
58 fclose($t);
59
e695fdc8
AD
60 error_reporting(0);
61 if (!rename($tmpfname, $icon_file)) {
62 unlink($tmpfname);
63 }
64 error_reporting (E_ERROR | E_WARNING | E_PARSE);
78800912
AD
65
66 }
67 }
68 }
69
40d13c28
AD
70 function update_rss_feed($link, $feed_url, $feed) {
71
b0b4abcf
AD
72 if (WEB_DEMO_MODE) return;
73
3ad5aa85 74 error_reporting(0);
40d13c28 75 $rss = fetch_rss($feed_url);
3ad5aa85 76 error_reporting (E_ERROR | E_WARNING | E_PARSE);
76798ff3 77
b6eefba5 78 db_query($link, "BEGIN");
b7f4bda2 79
b6eefba5 80 $feed = db_escape_string($feed);
40d13c28
AD
81
82 if ($rss) {
b82af8c3 83
78800912
AD
84 if (ENABLE_FEED_ICONS) {
85 check_feed_favicon($feed_url, $feed);
86 }
87
b6eefba5 88 $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
331900c6 89
b6eefba5
AD
90 $registered_title = db_fetch_result($result, 0, "title");
91 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
331900c6
AD
92
93 if (!$registered_title) {
331900c6 94 $feed_title = $rss->channel["title"];
b6eefba5 95 db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
331900c6 96 }
40d13c28 97
b7f4bda2
AD
98// print "I: " . $rss->channel["image"]["url"];
99
100 $icon_url = $rss->image["url"];
101
102 if ($icon_url && !$orig_icon_url) {
b6eefba5
AD
103 $icon_url = db_escape_string($icon_url);
104 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
b7f4bda2
AD
105 }
106
e6155a06
AD
107
108 $filters = array();
109
4b3dff6e 110 $result = db_query($link, "SELECT reg_exp,
e6155a06
AD
111 (SELECT name FROM ttrss_filter_types
112 WHERE id = filter_type) as name
113 FROM ttrss_filters");
114
b6eefba5 115 while ($line = db_fetch_assoc($result)) {
e6155a06 116 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
4b3dff6e 117 array_push($filters[$line["name"]], $line["reg_exp"]);
e6155a06
AD
118 }
119
40d13c28
AD
120 foreach ($rss->items as $item) {
121
122 $entry_guid = $item["id"];
123
124 if (!$entry_guid) $entry_guid = $item["guid"];
125 if (!$entry_guid) $entry_guid = $item["link"];
466001c4
AD
126
127 if (!$entry_guid) continue;
a116f569 128
9c9c7e6b 129 $entry_timestamp = "";
b82af8c3 130
9c9c7e6b
AD
131 $rss_2_date = $item['pubdate'];
132 $rss_1_date = $item['dc']['date'];
133 $atom_date = $item['issued'];
b197f117 134
9c9c7e6b
AD
135 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
136 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
137 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
b82af8c3
AD
138
139 if ($entry_timestamp == "") {
140 $entry_timestamp = time();
141 $no_orig_date = 'true';
466001c4
AD
142 } else {
143 $no_orig_date = 'false';
b82af8c3 144 }
b197f117 145
466001c4 146 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
71ad3959 147
40d13c28
AD
148 $entry_title = $item["title"];
149 $entry_link = $item["link"];
71ad3959
AD
150
151 if (!$entry_title) continue;
152 if (!$entry_link) continue;
153
40d13c28 154 $entry_content = $item["description"];
466001c4 155 if (!$entry_content) $entry_content = $item["content:escaped"];
40d13c28 156 if (!$entry_content) $entry_content = $item["content"];
a2015351 157
a116f569 158// if (!$entry_content) continue;
a2015351 159
8add756a
AD
160 // WTF
161 if (is_array($entry_content)) {
162 $entry_content = $entry_content["encoded"];
163 }
164
466001c4 165 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
cb0bd8bd 166
a1ea1e12
AD
167 $entry_comments = $item["comments"];
168
b6eefba5 169 $entry_guid = db_escape_string($entry_guid);
2651fc4f 170
8158c57a
AD
171 if (DB_TYPE == "pgsql") {
172 $extract_ts_qpart = ",EXTRACT(EPOCH FROM updated) as updated_timestamp";
173 }
174
b6eefba5 175 $result = db_query($link, "
40d13c28 176 SELECT
8158c57a
AD
177 id,last_read,no_orig_date,title,feed_id,content_hash
178 $extract_ts_qpart
40d13c28
AD
179 FROM
180 ttrss_entries
181 WHERE
a2015351 182 guid = '$entry_guid'");
466001c4 183
b6eefba5 184 if (db_num_rows($result) == 0) {
466001c4 185
e6155a06
AD
186 error_reporting(0);
187 if (is_filtered($entry_title, $entry_content, $filters)) {
188 continue;
189 }
190 error_reporting (E_ERROR | E_WARNING | E_PARSE);
191
b6eefba5
AD
192 //$entry_guid = db_escape_string($entry_guid);
193 $entry_content = db_escape_string($entry_content);
194 $entry_title = db_escape_string($entry_title);
195 $entry_link = db_escape_string($entry_link);
196 $entry_comments = db_escape_string($entry_comments);
466001c4
AD
197
198 $query = "INSERT
199 INTO ttrss_entries
200 (title,
201 guid,
202 link,
203 updated,
204 content,
205 content_hash,
206 feed_id,
deaaa02c 207 comments,
466001c4 208 no_orig_date)
40d13c28 209 VALUES
466001c4
AD
210 ('$entry_title',
211 '$entry_guid',
212 '$entry_link',
213 '$entry_timestamp_fmt',
214 '$entry_content',
215 '$content_hash',
216 '$feed',
a1ea1e12 217 '$entry_comments',
466001c4
AD
218 $no_orig_date)";
219
b6eefba5 220 $result = db_query($link, $query);
76798ff3 221
40d13c28 222 } else {
466001c4 223
b6eefba5
AD
224 $orig_entry_id = db_fetch_result($result, 0, "id");
225 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
466001c4
AD
226
227 if ($orig_feed_id != $feed) {
228// print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
229 continue;
230 }
ad3024fc
AD
231
232 $entry_is_modified = false;
466001c4 233
b6eefba5
AD
234 $orig_timestamp = db_fetch_result($result, 0, "updated_timestamp");
235 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
236 $orig_last_read = db_fetch_result($result, 0, "last_read");
237 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
238 $orig_title = db_fetch_result($result, 0, "title");
cac95b8d 239
2d84262b
AD
240 $last_read_qpart = "";
241
ad3024fc
AD
242 if ($orig_content_hash != $content_hash) {
243 if (UPDATE_POST_ON_CHECKSUM_CHANGE) {
244 $last_read_qpart = 'last_read = null,';
245 }
246 $entry_is_modified = true;
247 }
248
249 if ($orig_title != $entry_title) {
250 $entry_is_modified = true;
251 }
252
253 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
254 $entry_is_modified = true;
cac95b8d
AD
255 }
256
ad3024fc 257 if ($entry_is_modified) {
a2015351 258
b6eefba5
AD
259 $entry_comments = db_escape_string($entry_comments);
260 $entry_content = db_escape_string($entry_content);
261 $entry_title = db_escape_string($entry_title);
262 $entry_link = db_escape_string($entry_link);
a2015351 263
ad3024fc
AD
264 $query = "UPDATE ttrss_entries
265 SET
266 $last_read_qpart
267 title = '$entry_title',
268 link = '$entry_link',
269 updated = '$entry_timestamp_fmt',
270 content = '$entry_content',
271 comments = '$entry_comments',
272 content_hash = '$content_hash'
273 WHERE
274 id = '$orig_entry_id'";
275
b6eefba5 276 $result = db_query($link, $query);
ad3024fc 277 }
466001c4 278 }
40d13c28 279
eb36b4eb
AD
280 /* taaaags */
281 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
282
283 $entry_tags = null;
284
285 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content,
286 $entry_tags);
287
288 $entry_tags = $entry_tags[1];
289
290 if (count($entry_tags) > 0) {
291
292 $result = db_query($link, "SELECT id FROM ttrss_entries
293 WHERE guid = '$entry_guid'");
294
295 if (!$result || db_num_rows($result) != 1) {
296 return;
297 }
298
299 $entry_id = db_fetch_result($result, 0, "id");
300
301 foreach ($entry_tags as $tag) {
302 $tag = db_escape_string(strtolower($tag));
303
304 $result = db_query($link, "SELECT id FROM ttrss_tags
305 WHERE tag_name = '$tag' AND post_id = '$entry_id' LIMIT 1");
306
307 if ($result && db_num_rows($result) == 0) {
308
309// print "tagging $entry_id as $tag<br>";
310
311 db_query($link, "INSERT INTO ttrss_tags (tag_name,post_id)
312 VALUES ('$tag', '$entry_id')");
313 }
314 }
315 }
76798ff3 316 }
40d13c28 317
eb36b4eb
AD
318 db_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
319
40d13c28
AD
320 }
321
b6eefba5 322 db_query($link, "COMMIT");
f48ba3c9 323
40d13c28
AD
324 }
325
f175937c
AD
326 function print_select($id, $default, $values, $attributes = "") {
327 print "<select id=\"$id\" $attributes>";
a0d53889
AD
328 foreach ($values as $v) {
329 if ($v == $default)
330 $sel = " selected";
331 else
332 $sel = "";
333
334 print "<option$sel>$v</option>";
335 }
336 print "</select>";
337 }
40d13c28 338
e6155a06
AD
339 function is_filtered($title, $content, $filters) {
340
341 if ($filters["title"]) {
342 foreach ($filters["title"] as $title_filter) {
343 if (preg_match("/$title_filter/i", $title))
344 return true;
345 }
346 }
347
348 if ($filters["content"]) {
349 foreach ($filters["content"] as $content_filter) {
350 if (preg_match("/$content_filter/i", $content))
351 return true;
352 }
353 }
354
355 if ($filters["both"]) {
356 foreach ($filters["both"] as $filter) {
357 if (preg_match("/$filter/i", $title) || preg_match("/$filter/i", $content))
358 return true;
359 }
360 }
361
362 return false;
363 }
364
254e0e4b
AD
365 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file) {
366
367 if (file_exists($icon_file) && filesize($icon_file) > 0) {
368 $feed_icon = "<img src=\"$icon_file\">";
369 } else {
370 $feed_icon = "<img src=\"images/blank_icon.gif\">";
371 }
372
8143ae1f 373 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
254e0e4b
AD
374
375 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
376 if (ENABLE_FEED_ICONS) {
377 print "$feed_icon";
378 }
379
380 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
381
382 if ($unread != 0) {
383 $fctr_class = "";
384 } else {
385 $fctr_class = "class=\"invisible\"";
386 }
387
388 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
389 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
390
391 print "</li>";
392
393 }
394
40d13c28 395?>