]> git.wh0rd.org - tt-rss.git/blob - functions.php
functions.php is now uid-aware
[tt-rss.git] / functions.php
1 <?
2 session_start();
3
4 $_SESSION["uid"] = 1; // FIXME: placeholder
5
6 require_once 'config.php';
7 require_once 'db-prefs.php';
8
9 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
10
11 function purge_old_posts($link) {
12
13 $user_id = $_SESSION["uid"];
14
15 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
16 WHERE owner_uid = '$user_id'");
17
18 while ($line = db_fetch_assoc($result)) {
19
20 $feed_id = $line["id"];
21 $purge_interval = $line["purge_interval"];
22
23 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
24
25 if ($purge_interval > 0) {
26
27 if (DB_TYPE == "pgsql") {
28 db_query($link, "DELETE FROM ttrss_entries WHERE
29 marked = false AND feed_id = '$feed_id' AND
30 date_entered < NOW() - INTERVAL '$purge_interval days'");
31 } else {
32 db_query($link, "DELETE FROM ttrss_entries WHERE
33 marked = false AND feed_id = '$feed_id' AND
34 date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
35 }
36 }
37 }
38 }
39
40 function update_all_feeds($link, $fetch) {
41
42 if (WEB_DEMO_MODE) return;
43
44 if (get_pref($link, 'DAEMON_REFRESH_ONLY')) {
45 if (!$_GET["daemon"]) {
46 return;
47 }
48 }
49
50 db_query($link, "BEGIN");
51
52 $user_id = $_SESSION["uid"];
53
54 $result = db_query($link, "SELECT feed_url,id,
55 substring(last_updated,1,19) as last_updated,
56 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'");
57
58 while ($line = db_fetch_assoc($result)) {
59 $upd_intl = $line["update_interval"];
60
61 if (!$upd_intl || $upd_intl == 0) {
62 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL');
63 }
64
65 if (!$line["last_updated"] ||
66 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
67
68 update_rss_feed($link, $line["feed_url"], $line["id"]);
69 }
70 }
71
72 purge_old_posts($link);
73
74 db_query($link, "COMMIT");
75
76 }
77
78 function check_feed_favicon($feed_url, $feed, $link) {
79 $feed_url = str_replace("http://", "", $feed_url);
80 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
81
82 $icon_url = "http://$feed_url/favicon.ico";
83 $icon_file = get_pref($link, 'ICONS_DIR') . "/$feed.ico";
84
85 if (!file_exists($icon_file)) {
86
87 error_reporting(0);
88 $r = fopen($icon_url, "r");
89 error_reporting (E_ERROR | E_WARNING | E_PARSE);
90
91 if ($r) {
92 $tmpfname = tempnam("/tmp", "ttrssicon");
93
94 $t = fopen($tmpfname, "w");
95
96 while (!feof($r)) {
97 $buf = fread($r, 16384);
98 fwrite($t, $buf);
99 }
100
101 fclose($r);
102 fclose($t);
103
104 error_reporting(0);
105 if (!rename($tmpfname, $icon_file)) {
106 unlink($tmpfname);
107 }
108 error_reporting (E_ERROR | E_WARNING | E_PARSE);
109
110 }
111 }
112 }
113
114 function update_rss_feed($link, $feed_url, $feed) {
115
116 if (WEB_DEMO_MODE) return;
117
118 $feed = db_escape_string($feed);
119
120 error_reporting(0);
121 $rss = fetch_rss($feed_url);
122
123 error_reporting (E_ERROR | E_WARNING | E_PARSE);
124
125 db_query($link, "BEGIN");
126
127 $feed = db_escape_string($feed);
128
129 if ($rss) {
130
131 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
132 check_feed_favicon($feed_url, $feed, $link);
133 }
134
135 $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
136
137 $registered_title = db_fetch_result($result, 0, "title");
138 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
139
140 if (!$registered_title) {
141 $feed_title = db_escape_string($rss->channel["title"]);
142 db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
143 }
144
145 // print "I: " . $rss->channel["image"]["url"];
146
147 $icon_url = $rss->image["url"];
148
149 if ($icon_url && !$orig_icon_url) {
150 $icon_url = db_escape_string($icon_url);
151 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
152 }
153
154
155 $filters = array();
156
157 $result = db_query($link, "SELECT reg_exp,
158 (SELECT name FROM ttrss_filter_types
159 WHERE id = filter_type) as name
160 FROM ttrss_filters");
161
162 while ($line = db_fetch_assoc($result)) {
163 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
164 array_push($filters[$line["name"]], $line["reg_exp"]);
165 }
166
167 foreach ($rss->items as $item) {
168
169 $entry_guid = $item["id"];
170
171 if (!$entry_guid) $entry_guid = $item["guid"];
172 if (!$entry_guid) $entry_guid = $item["link"];
173
174 if (!$entry_guid) continue;
175
176 $entry_timestamp = "";
177
178 $rss_2_date = $item['pubdate'];
179 $rss_1_date = $item['dc']['date'];
180 $atom_date = $item['issued'];
181
182 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
183 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
184 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
185
186 if ($entry_timestamp == "") {
187 $entry_timestamp = time();
188 $no_orig_date = 'true';
189 } else {
190 $no_orig_date = 'false';
191 }
192
193 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
194
195 $entry_title = $item["title"];
196 $entry_link = $item["link"];
197
198 if (!$entry_title) continue;
199 if (!$entry_link) continue;
200
201 $entry_content = $item["content:escaped"];
202
203 if (!$entry_content) $entry_content = $item["content:encoded"];
204 if (!$entry_content) $entry_content = $item["content"];
205 if (!$entry_content) $entry_content = $item["description"];
206
207 // if (!$entry_content) continue;
208
209 // WTF
210 if (is_array($entry_content)) {
211 $entry_content = $entry_content["encoded"];
212 if (!$entry_content) $entry_content = $entry_content["escaped"];
213 }
214
215 // print_r($item);
216 // print_r($entry_content);
217
218 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
219
220 $entry_comments = $item["comments"];
221
222 $entry_guid = db_escape_string($entry_guid);
223
224 $result = db_query($link, "
225 SELECT
226 id,last_read,no_orig_date,title,feed_id,content_hash,
227 substring(updated,1,19) as updated
228 FROM
229 ttrss_entries
230 WHERE
231 guid = '$entry_guid'");
232
233 // print db_num_rows($result) . "$entry_guid<br/>";
234
235 if (db_num_rows($result) == 0) {
236
237 error_reporting(0);
238 if (is_filtered($entry_title, $entry_content, $filters)) {
239 continue;
240 }
241 error_reporting (E_ERROR | E_WARNING | E_PARSE);
242
243 //$entry_guid = db_escape_string($entry_guid);
244 $entry_content = db_escape_string($entry_content);
245 $entry_title = db_escape_string($entry_title);
246 $entry_link = db_escape_string($entry_link);
247 $entry_comments = db_escape_string($entry_comments);
248
249 $query = "INSERT
250 INTO ttrss_entries
251 (title,
252 guid,
253 link,
254 updated,
255 content,
256 content_hash,
257 feed_id,
258 comments,
259 no_orig_date,
260 date_entered)
261 VALUES
262 ('$entry_title',
263 '$entry_guid',
264 '$entry_link',
265 '$entry_timestamp_fmt',
266 '$entry_content',
267 '$content_hash',
268 '$feed',
269 '$entry_comments',
270 $no_orig_date,
271 NOW())";
272
273 $result = db_query($link, $query);
274
275 } else {
276
277 $orig_entry_id = db_fetch_result($result, 0, "id");
278 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
279
280 // print "OED: $orig_entry_id; OID: $orig_feed_id ; FID: $feed<br>";
281
282 if ($orig_feed_id != $feed) {
283 // print "<p>GUID $entry_guid: update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
284 continue;
285 }
286
287 $entry_is_modified = false;
288
289 $orig_timestamp = strtotime(db_fetch_result($result, 0, "updated"));
290
291 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
292 $orig_last_read = db_fetch_result($result, 0, "last_read");
293 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
294 $orig_title = db_fetch_result($result, 0, "title");
295
296 $last_read_qpart = "";
297
298 if ($orig_content_hash != $content_hash) {
299 // print "$orig_content_hash :: $content_hash<br>";
300
301 if (get_pref($link, 'UPDATE_POST_ON_CHECKSUM_CHANGE')) {
302 $last_read_qpart = 'last_read = null,';
303 }
304 $entry_is_modified = true;
305 }
306
307 if ($orig_title != $entry_title) {
308 $entry_is_modified = true;
309 }
310
311 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
312 $entry_is_modified = true;
313 }
314
315 if ($entry_is_modified) {
316
317 // print "$entry_guid Modified!<br>";
318
319 $entry_comments = db_escape_string($entry_comments);
320 $entry_content = db_escape_string($entry_content);
321 $entry_title = db_escape_string($entry_title);
322 $entry_link = db_escape_string($entry_link);
323
324 $query = "UPDATE ttrss_entries
325 SET
326 $last_read_qpart
327 title = '$entry_title',
328 link = '$entry_link',
329 updated = '$entry_timestamp_fmt',
330 content = '$entry_content',
331 comments = '$entry_comments',
332 content_hash = '$content_hash'
333 WHERE
334 id = '$orig_entry_id'";
335
336 $result = db_query($link, $query);
337 }
338 }
339
340 /* taaaags */
341 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
342
343 $entry_tags = null;
344
345 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content,
346 $entry_tags);
347
348 $entry_tags = $entry_tags[1];
349
350 if (count($entry_tags) > 0) {
351
352 $result = db_query($link, "SELECT id FROM ttrss_entries
353 WHERE guid = '$entry_guid'");
354
355 if (!$result || db_num_rows($result) != 1) {
356 return;
357 }
358
359 $entry_id = db_fetch_result($result, 0, "id");
360
361 foreach ($entry_tags as $tag) {
362 $tag = db_escape_string(strtolower($tag));
363
364 $result = db_query($link, "SELECT id FROM ttrss_tags
365 WHERE tag_name = '$tag' AND post_id = '$entry_id' LIMIT 1");
366
367 if ($result && db_num_rows($result) == 0) {
368
369 // print "tagging $entry_id as $tag<br>";
370
371 db_query($link, "INSERT INTO ttrss_tags (tag_name,post_id)
372 VALUES ('$tag', '$entry_id')");
373 }
374 }
375 }
376 }
377
378 db_query($link, "UPDATE ttrss_feeds
379 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
380
381 } else {
382 $error_msg = db_escape_string(magpie_error());
383 db_query($link,
384 "UPDATE ttrss_feeds SET last_error = '$error_msg',
385 last_updated = NOW() WHERE id = '$feed'");
386 }
387
388 db_query($link, "COMMIT");
389
390 }
391
392 function print_select($id, $default, $values, $attributes = "") {
393 print "<select id=\"$id\" $attributes>";
394 foreach ($values as $v) {
395 if ($v == $default)
396 $sel = " selected";
397 else
398 $sel = "";
399
400 print "<option$sel>$v</option>";
401 }
402 print "</select>";
403 }
404
405 function is_filtered($title, $content, $filters) {
406
407 if ($filters["title"]) {
408 foreach ($filters["title"] as $title_filter) {
409 if (preg_match("/$title_filter/i", $title))
410 return true;
411 }
412 }
413
414 if ($filters["content"]) {
415 foreach ($filters["content"] as $content_filter) {
416 if (preg_match("/$content_filter/i", $content))
417 return true;
418 }
419 }
420
421 if ($filters["both"]) {
422 foreach ($filters["both"] as $filter) {
423 if (preg_match("/$filter/i", $title) || preg_match("/$filter/i", $content))
424 return true;
425 }
426 }
427
428 return false;
429 }
430
431 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link) {
432
433 if (file_exists($icon_file) && filesize($icon_file) > 0) {
434 $feed_icon = "<img src=\"$icon_file\">";
435 } else {
436 $feed_icon = "<img src=\"images/blank_icon.gif\">";
437 }
438
439 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
440
441 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
442 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
443 print "$feed_icon";
444 }
445
446 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
447
448 if ($unread != 0) {
449 $fctr_class = "";
450 } else {
451 $fctr_class = "class=\"invisible\"";
452 }
453
454 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
455 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
456
457 print "</li>";
458
459 }
460
461 function getmicrotime() {
462 list($usec, $sec) = explode(" ",microtime());
463 return ((float)$usec + (float)$sec);
464 }
465
466 function print_radio($id, $default, $values, $attributes = "") {
467 foreach ($values as $v) {
468
469 if ($v == $default)
470 $sel = "checked";
471 else
472 $sel = "";
473
474 if ($v == "Yes") {
475 $sel .= " value=\"1\"";
476 } else {
477 $sel .= " value=\"0\"";
478 }
479
480 print "<input type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
481
482 }
483 }
484
485 ?>