]> git.wh0rd.org - tt-rss.git/blob - functions.php
new option - ALLOW_DUPLICATE_POSTS
[tt-rss.git] / functions.php
1 <?
2 session_start();
3
4 require_once 'config.php';
5 require_once 'db-prefs.php';
6
7 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
8
9 function purge_feed($link, $feed_id, $purge_interval) {
10
11 if (DB_TYPE == "pgsql") {
12 db_query($link, "DELETE FROM ttrss_user_entries WHERE
13 marked = false AND feed_id = '$feed_id' AND
14 (SELECT date_entered FROM ttrss_entries WHERE
15 id = ref_id) < NOW() - INTERVAL '$purge_interval days'");
16 } else {
17 db_query($link, "DELETE FROM ttrss_user_entries WHERE
18 marked = false AND feed_id = '$feed_id' AND
19 (SELECT date_entered FROM ttrss_entries WHERE
20 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
21 }
22 }
23
24 function global_purge_old_posts($link, $do_output = false) {
25
26 $result = db_query($link,
27 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds");
28
29 while ($line = db_fetch_assoc($result)) {
30
31 $feed_id = $line["id"];
32 $purge_interval = $line["purge_interval"];
33 $owner_uid = $line["owner_uid"];
34
35 if ($purge_interval == 0) {
36
37 $tmp_result = db_query($link,
38 "SELECT value FROM ttrss_user_prefs WHERE
39 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
40
41 if (db_num_rows($tmp_result) != 0) {
42 $purge_interval = db_fetch_result($tmp_result, 0, "value");
43 }
44 }
45
46 if ($do_output) {
47 print "<feed id='$feed_id' p_intl='$purge_interval'/>";
48 }
49
50 if ($purge_interval > 0) {
51 purge_feed($link, $feed_id, $purge_interval);
52 }
53 }
54
55 // purge orphaned posts in main content table
56 db_query($link, "DELETE FROM ttrss_entries WHERE
57 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
58
59 }
60
61 function purge_old_posts($link) {
62
63 $user_id = $_SESSION["uid"];
64
65 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
66 WHERE owner_uid = '$user_id'");
67
68 while ($line = db_fetch_assoc($result)) {
69
70 $feed_id = $line["id"];
71 $purge_interval = $line["purge_interval"];
72
73 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
74
75 if ($purge_interval > 0) {
76 purge_feed($link, $feed_id, $purge_interval);
77 }
78 }
79
80 // purge orphaned posts in main content table
81 db_query($link, "DELETE FROM ttrss_entries WHERE
82 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
83 }
84
85 function update_all_feeds($link, $fetch) {
86
87 if (WEB_DEMO_MODE) return;
88
89 if (get_pref($link, 'DAEMON_REFRESH_ONLY')) {
90 if (!$_GET["daemon"]) {
91 return;
92 }
93 }
94
95 db_query($link, "BEGIN");
96
97 $user_id = $_SESSION["uid"];
98
99 $result = db_query($link, "SELECT feed_url,id,
100 substring(last_updated,1,19) as last_updated,
101 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'");
102
103 while ($line = db_fetch_assoc($result)) {
104 $upd_intl = $line["update_interval"];
105
106 if (!$upd_intl || $upd_intl == 0) {
107 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL');
108 }
109
110 if ($fetch || (!$line["last_updated"] ||
111 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
112
113 update_rss_feed($link, $line["feed_url"], $line["id"]);
114 }
115 }
116
117 purge_old_posts($link);
118
119 db_query($link, "COMMIT");
120
121 }
122
123 function check_feed_favicon($feed_url, $feed, $link) {
124 $feed_url = str_replace("http://", "", $feed_url);
125 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
126
127 $icon_url = "http://$feed_url/favicon.ico";
128 $icon_file = ICONS_DIR . "/$feed.ico";
129
130 if (!file_exists($icon_file)) {
131
132 error_reporting(0);
133 $r = fopen($icon_url, "r");
134 error_reporting (E_ERROR | E_WARNING | E_PARSE);
135
136 if ($r) {
137 $tmpfname = tempnam("/tmp", "ttrssicon");
138
139 $t = fopen($tmpfname, "w");
140
141 while (!feof($r)) {
142 $buf = fread($r, 16384);
143 fwrite($t, $buf);
144 }
145
146 fclose($r);
147 fclose($t);
148
149 error_reporting(0);
150 if (!rename($tmpfname, $icon_file)) {
151 unlink($tmpfname);
152 }
153
154 chmod($icon_file, 0644);
155
156 error_reporting (E_ERROR | E_WARNING | E_PARSE);
157
158 }
159 }
160 }
161
162 function update_rss_feed($link, $feed_url, $feed) {
163
164 if (WEB_DEMO_MODE) return;
165
166 $feed = db_escape_string($feed);
167
168 error_reporting(0);
169 $rss = fetch_rss($feed_url);
170
171 error_reporting (E_ERROR | E_WARNING | E_PARSE);
172
173 db_query($link, "BEGIN");
174
175 $feed = db_escape_string($feed);
176
177 if ($rss) {
178
179 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
180 check_feed_favicon($feed_url, $feed, $link);
181 }
182
183 $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
184
185 $registered_title = db_fetch_result($result, 0, "title");
186 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
187
188 if (!$registered_title) {
189 $feed_title = db_escape_string($rss->channel["title"]);
190 db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
191 }
192
193 // print "I: " . $rss->channel["image"]["url"];
194
195 $icon_url = $rss->image["url"];
196
197 if ($icon_url && !$orig_icon_url) {
198 $icon_url = db_escape_string($icon_url);
199 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
200 }
201
202
203 $filters = array();
204
205 $result = db_query($link, "SELECT reg_exp,
206 (SELECT name FROM ttrss_filter_types
207 WHERE id = filter_type) as name
208 FROM ttrss_filters WHERE owner_uid = ".$_SESSION["uid"]);
209
210 while ($line = db_fetch_assoc($result)) {
211 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
212 array_push($filters[$line["name"]], $line["reg_exp"]);
213 }
214
215 foreach ($rss->items as $item) {
216
217 $entry_guid = $item["id"];
218
219 if (!$entry_guid) $entry_guid = $item["guid"];
220 if (!$entry_guid) $entry_guid = $item["link"];
221
222 if (!$entry_guid) continue;
223
224 $entry_timestamp = "";
225
226 $rss_2_date = $item['pubdate'];
227 $rss_1_date = $item['dc']['date'];
228 $atom_date = $item['issued'];
229
230 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
231 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
232 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
233
234 if ($entry_timestamp == "") {
235 $entry_timestamp = time();
236 $no_orig_date = 'true';
237 } else {
238 $no_orig_date = 'false';
239 }
240
241 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
242
243 $entry_title = $item["title"];
244 $entry_link = $item["link"];
245
246 if (!$entry_title) continue;
247 if (!$entry_link) continue;
248
249 $entry_content = $item["content:escaped"];
250
251 if (!$entry_content) $entry_content = $item["content:encoded"];
252 if (!$entry_content) $entry_content = $item["content"];
253 if (!$entry_content) $entry_content = $item["description"];
254
255 // if (!$entry_content) continue;
256
257 // WTF
258 if (is_array($entry_content)) {
259 $entry_content = $entry_content["encoded"];
260 if (!$entry_content) $entry_content = $entry_content["escaped"];
261 }
262
263 // print_r($item);
264 // print_r($entry_content);
265
266 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
267
268 $entry_comments = $item["comments"];
269
270 $entry_guid = db_escape_string($entry_guid);
271
272 $result = db_query($link, "SELECT id FROM ttrss_entries
273 WHERE guid = '$entry_guid'");
274
275 $owner_uid = $_SESSION["uid"];
276
277 if (db_num_rows($result) == 0) {
278
279 // base post entry does not exist, create it
280
281 error_reporting(0);
282 if (is_filtered($entry_title, $entry_content, $filters)) {
283 continue;
284 }
285 error_reporting (E_ERROR | E_WARNING | E_PARSE);
286
287 $entry_content = db_escape_string($entry_content);
288 $entry_title = db_escape_string($entry_title);
289 $entry_link = db_escape_string($entry_link);
290 $entry_comments = db_escape_string($entry_comments);
291
292 $result = db_query($link,
293 "INSERT INTO ttrss_entries
294 (title,
295 guid,
296 link,
297 updated,
298 content,
299 content_hash,
300 no_orig_date,
301 date_entered,
302 comments)
303 VALUES
304 ('$entry_title',
305 '$entry_guid',
306 '$entry_link',
307 '$entry_timestamp_fmt',
308 '$entry_content',
309 '$content_hash',
310 $no_orig_date,
311 NOW(),
312 '$entry_comments')");
313 }
314
315 // now it should exist, if not - bad luck then
316
317 $result = db_query($link, "SELECT id FROM ttrss_entries WHERE
318 guid = '$entry_guid'");
319
320 if (db_num_rows($result) == 1) {
321
322 $ref_id = db_fetch_result($result, 0, "id");
323
324 // check for user post link to main table
325
326 // do we allow duplicate posts with same GUID in different feeds?
327 if (get_pref($link, "ALLOW_DUPLICATE_POSTS")) {
328 $dupcheck_qpart = "AND feed_id = '$feed'";
329 } else {
330 $dupcheck_qpart = "";
331 }
332
333 $result = db_query($link,
334 "SELECT ref_id FROM ttrss_user_entries WHERE
335 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
336 $dupcheck_qpart");
337
338 // okay it doesn't exist - create user entry
339 if (db_num_rows($result) == 0) {
340 $result = db_query($link,
341 "INSERT INTO ttrss_user_entries
342 (ref_id, owner_uid, feed_id)
343 VALUES ('$ref_id', '$owner_uid', '$feed')");
344 }
345 }
346
347 /* $result = db_query($link, "
348 SELECT
349 id,last_read,no_orig_date,title,feed_id,content_hash,
350 substring(updated,1,19) as updated
351 FROM
352 ttrss_entries
353 WHERE
354 guid = '$entry_guid' AND
355 owner_uid = " . $_SESSION["uid"]." AND
356 feed_id = '$feed'");
357
358 if (db_num_rows($result) == 0) {
359
360 error_reporting(0);
361 if (is_filtered($entry_title, $entry_content, $filters)) {
362 continue;
363 }
364 error_reporting (E_ERROR | E_WARNING | E_PARSE);
365
366 //$entry_guid = db_escape_string($entry_guid);
367 $entry_content = db_escape_string($entry_content);
368 $entry_title = db_escape_string($entry_title);
369 $entry_link = db_escape_string($entry_link);
370 $entry_comments = db_escape_string($entry_comments);
371
372 $query = "INSERT
373 INTO ttrss_entries
374 (title,
375 guid,
376 link,
377 updated,
378 content,
379 content_hash,
380 feed_id,
381 comments,
382 no_orig_date,
383 date_entered,
384 owner_uid)
385 VALUES
386 ('$entry_title',
387 '$entry_guid',
388 '$entry_link',
389 '$entry_timestamp_fmt',
390 '$entry_content',
391 '$content_hash',
392 '$feed',
393 '$entry_comments',
394 $no_orig_date,
395 NOW(),".$_SESSION["uid"].")";
396
397 $result = db_query($link, $query);
398
399 } else {
400
401 $orig_entry_id = db_fetch_result($result, 0, "id");
402 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
403
404 // print "OED: $orig_entry_id; OID: $orig_feed_id ; FID: $feed<br>";
405
406 if ($orig_feed_id != $feed) {
407 // print "<p>GUID $entry_guid: update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
408 continue;
409 }
410
411 $entry_is_modified = false;
412
413 $orig_timestamp = strtotime(db_fetch_result($result, 0, "updated"));
414
415 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
416 $orig_last_read = db_fetch_result($result, 0, "last_read");
417 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
418 $orig_title = db_fetch_result($result, 0, "title");
419
420 $last_read_qpart = "";
421
422 if ($orig_content_hash != $content_hash) {
423 // print "$orig_content_hash :: $content_hash<br>";
424
425 if (get_pref($link, 'UPDATE_POST_ON_CHECKSUM_CHANGE')) {
426 $last_read_qpart = 'last_read = null,';
427 }
428 $entry_is_modified = true;
429 }
430
431 if ($orig_title != $entry_title) {
432 $entry_is_modified = true;
433 }
434
435 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
436 $entry_is_modified = true;
437 }
438
439 if ($entry_is_modified) {
440
441 // print "$entry_guid Modified!<br>";
442
443 $entry_comments = db_escape_string($entry_comments);
444 $entry_content = db_escape_string($entry_content);
445 $entry_title = db_escape_string($entry_title);
446 $entry_link = db_escape_string($entry_link);
447
448 $query = "UPDATE ttrss_entries
449 SET
450 $last_read_qpart
451 title = '$entry_title',
452 link = '$entry_link',
453 updated = '$entry_timestamp_fmt',
454 content = '$entry_content',
455 comments = '$entry_comments',
456 content_hash = '$content_hash'
457 WHERE
458 id = '$orig_entry_id'";
459
460 $result = db_query($link, $query);
461 }
462 } */
463
464 /* taaaags */
465 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
466
467 $entry_tags = null;
468
469 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content,
470 $entry_tags);
471
472 $entry_tags = $entry_tags[1];
473
474 if (count($entry_tags) > 0) {
475
476 $result = db_query($link, "SELECT id,int_id
477 FROM ttrss_entries,ttrss_user_entries
478 WHERE guid = '$entry_guid'
479 AND feed_id = '$feed' AND ref_id = id
480 AND owner_uid = " . $_SESSION["uid"]);
481
482 if (!$result || db_num_rows($result) != 1) {
483 return;
484 }
485
486 $entry_id = db_fetch_result($result, 0, "id");
487 $entry_int_id = db_fetch_result($result, 0, "int_id");
488
489 foreach ($entry_tags as $tag) {
490 $tag = db_escape_string(strtolower($tag));
491
492 $tag = str_replace("technorati tag: ", "", $tag);
493
494 $result = db_query($link, "SELECT id FROM ttrss_tags
495 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
496 owner_uid = ".$_SESSION["uid"]." LIMIT 1");
497
498 // print db_fetch_result($result, 0, "id");
499
500 if ($result && db_num_rows($result) == 0) {
501
502 // print "tagging $entry_id as $tag<br>";
503
504 db_query($link, "INSERT INTO ttrss_tags
505 (owner_uid,tag_name,post_int_id)
506 VALUES ('".$_SESSION["uid"]."','$tag', '$entry_int_id')");
507 }
508 }
509 }
510 }
511
512 db_query($link, "UPDATE ttrss_feeds
513 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
514
515 } else {
516 $error_msg = db_escape_string(magpie_error());
517 db_query($link,
518 "UPDATE ttrss_feeds SET last_error = '$error_msg',
519 last_updated = NOW() WHERE id = '$feed'");
520 }
521
522 db_query($link, "COMMIT");
523
524 }
525
526 function print_select($id, $default, $values, $attributes = "") {
527 print "<select id=\"$id\" $attributes>";
528 foreach ($values as $v) {
529 if ($v == $default)
530 $sel = " selected";
531 else
532 $sel = "";
533
534 print "<option$sel>$v</option>";
535 }
536 print "</select>";
537 }
538
539 function is_filtered($title, $content, $filters) {
540
541 if ($filters["title"]) {
542 foreach ($filters["title"] as $title_filter) {
543 if (preg_match("/$title_filter/i", $title))
544 return true;
545 }
546 }
547
548 if ($filters["content"]) {
549 foreach ($filters["content"] as $content_filter) {
550 if (preg_match("/$content_filter/i", $content))
551 return true;
552 }
553 }
554
555 if ($filters["both"]) {
556 foreach ($filters["both"] as $filter) {
557 if (preg_match("/$filter/i", $title) || preg_match("/$filter/i", $content))
558 return true;
559 }
560 }
561
562 return false;
563 }
564
565 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link) {
566
567 if (file_exists($icon_file) && filesize($icon_file) > 0) {
568 $feed_icon = "<img src=\"$icon_file\">";
569 } else {
570 $feed_icon = "<img src=\"images/blank_icon.gif\">";
571 }
572
573 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
574
575 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
576 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
577 print "$feed_icon";
578 }
579
580 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
581
582 if ($unread != 0) {
583 $fctr_class = "";
584 } else {
585 $fctr_class = "class=\"invisible\"";
586 }
587
588 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
589 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
590
591 print "</li>";
592
593 }
594
595 function getmicrotime() {
596 list($usec, $sec) = explode(" ",microtime());
597 return ((float)$usec + (float)$sec);
598 }
599
600 function print_radio($id, $default, $values, $attributes = "") {
601 foreach ($values as $v) {
602
603 if ($v == $default)
604 $sel = "checked";
605 else
606 $sel = "";
607
608 if ($v == "Yes") {
609 $sel .= " value=\"1\"";
610 } else {
611 $sel .= " value=\"0\"";
612 }
613
614 print "<input type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
615
616 }
617 }
618
619 function initialize_user_prefs($link, $uid) {
620
621 $uid = db_escape_string($uid);
622
623 db_query($link, "BEGIN");
624
625 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
626
627 $u_result = db_query($link, "SELECT pref_name
628 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
629
630 $active_prefs = array();
631
632 while ($line = db_fetch_assoc($u_result)) {
633 array_push($active_prefs, $line["pref_name"]);
634 }
635
636 while ($line = db_fetch_assoc($result)) {
637 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
638 // print "adding " . $line["pref_name"] . "<br>";
639
640 db_query($link, "INSERT INTO ttrss_user_prefs
641 (owner_uid,pref_name,value) VALUES
642 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
643
644 }
645 }
646
647 db_query($link, "COMMIT");
648
649 }
650
651 function authenticate_user($link, $login, $password) {
652
653 $pwd_hash = 'SHA1:' . sha1($password);
654
655 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
656 login = '$login' AND (pwd_hash = '$password' OR pwd_hash = '$pwd_hash')");
657
658 if (db_num_rows($result) == 1) {
659 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
660 $_SESSION["name"] = db_fetch_result($result, 0, "login");
661 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
662
663 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
664 $_SESSION["uid"]);
665
666 return true;
667 }
668
669 return false;
670
671 }
672
673 function http_authenticate_user($link) {
674
675 if (!$_SERVER['PHP_AUTH_USER']) {
676
677 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
678 header('HTTP/1.0 401 Unauthorized');
679 print "<h1>401 Unathorized</h1>";
680 exit;
681
682 } else {
683
684 $login = db_escape_string($_SERVER['PHP_AUTH_USER']);
685 $password = db_escape_string($_SERVER['PHP_AUTH_PW']);
686
687 return authenticate_user($link, $login, $password);
688 }
689 }
690
691 function make_password($length = 8) {
692
693 $password = "";
694 $possible = "0123456789bcdfghjkmnpqrstvwxyz";
695
696 $i = 0;
697
698 while ($i < $length) {
699 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
700
701 if (!strstr($password, $char)) {
702 $password .= $char;
703 $i++;
704 }
705 }
706 return $password;
707 }
708
709 // this is called after user is created to initialize default feeds, labels
710 // or whatever else
711
712 // user preferences are checked on every login, not here
713
714 function initialize_user($link, $uid) {
715
716 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
717 values ('$uid','unread = true', 'Unread articles')");
718
719 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
720 values ('$uid','last_read is null and unread = false', 'Updated articles')");
721
722 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
723 values ('$uid', 'Tiny Tiny RSS Dev. Feed',
724 'http://bah.spb.su/darcsweb/darcsweb.cgi?r=tt-rss;a=rss')");
725
726 }
727
728 ?>