4 require_once 'config.php';
5 require_once 'db-prefs.php';
7 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
9 function purge_feed($link, $feed_id, $purge_interval) {
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'");
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)");
24 function global_purge_old_posts($link, $do_output = false) {
26 $result = db_query($link,
27 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds");
29 while ($line = db_fetch_assoc($result)) {
31 $feed_id = $line["id"];
32 $purge_interval = $line["purge_interval"];
33 $owner_uid = $line["owner_uid"];
35 if ($purge_interval == 0) {
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'");
41 if (db_num_rows($tmp_result) != 0) {
42 $purge_interval = db_fetch_result($tmp_result, 0, "value");
47 print "<feed id='$feed_id' p_intl='$purge_interval'/>";
50 if ($purge_interval > 0) {
51 purge_feed($link, $feed_id, $purge_interval);
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");
61 function purge_old_posts($link) {
63 $user_id = $_SESSION["uid"];
65 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
66 WHERE owner_uid = '$user_id'");
68 while ($line = db_fetch_assoc($result)) {
70 $feed_id = $line["id"];
71 $purge_interval = $line["purge_interval"];
73 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
75 if ($purge_interval > 0) {
76 purge_feed($link, $feed_id, $purge_interval);
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");
85 function update_all_feeds($link, $fetch) {
87 if (WEB_DEMO_MODE
) return;
89 if (get_pref($link, 'DAEMON_REFRESH_ONLY')) {
90 if (!$_GET["daemon"]) {
95 db_query($link, "BEGIN");
97 $user_id = $_SESSION["uid"];
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'");
103 while ($line = db_fetch_assoc($result)) {
104 $upd_intl = $line["update_interval"];
106 if (!$upd_intl ||
$upd_intl == 0) {
107 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL');
110 if ($fetch ||
(!$line["last_updated"] ||
111 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
113 update_rss_feed($link, $line["feed_url"], $line["id"]);
117 purge_old_posts($link);
119 db_query($link, "COMMIT");
123 function check_feed_favicon($feed_url, $feed, $link) {
124 $feed_url = str_replace("http://", "", $feed_url);
125 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
127 $icon_url = "http://$feed_url/favicon.ico";
128 $icon_file = ICONS_DIR
. "/$feed.ico";
130 if (!file_exists($icon_file)) {
133 $r = fopen($icon_url, "r");
134 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
137 $tmpfname = tempnam("/tmp", "ttrssicon");
139 $t = fopen($tmpfname, "w");
142 $buf = fread($r, 16384);
150 if (!rename($tmpfname, $icon_file)) {
154 chmod($icon_file, 0644);
156 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
162 function update_rss_feed($link, $feed_url, $feed) {
164 if (WEB_DEMO_MODE
) return;
166 $feed = db_escape_string($feed);
169 $rss = fetch_rss($feed_url);
171 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
173 db_query($link, "BEGIN");
175 $feed = db_escape_string($feed);
179 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
180 check_feed_favicon($feed_url, $feed, $link);
183 $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
185 $registered_title = db_fetch_result($result, 0, "title");
186 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
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'");
193 // print "I: " . $rss->channel["image"]["url"];
195 $icon_url = $rss->image
["url"];
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'");
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"]);
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"]);
215 foreach ($rss->items
as $item) {
217 $entry_guid = $item["id"];
219 if (!$entry_guid) $entry_guid = $item["guid"];
220 if (!$entry_guid) $entry_guid = $item["link"];
222 if (!$entry_guid) continue;
224 $entry_timestamp = "";
226 $rss_2_date = $item['pubdate'];
227 $rss_1_date = $item['dc']['date'];
228 $atom_date = $item['issued'];
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);
234 if ($entry_timestamp == "") {
235 $entry_timestamp = time();
236 $no_orig_date = 'true';
238 $no_orig_date = 'false';
241 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
243 $entry_title = $item["title"];
244 $entry_link = $item["link"];
246 if (!$entry_title) continue;
247 if (!$entry_link) continue;
249 $entry_content = $item["content:escaped"];
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"];
255 // if (!$entry_content) continue;
258 if (is_array($entry_content)) {
259 $entry_content = $entry_content["encoded"];
260 if (!$entry_content) $entry_content = $entry_content["escaped"];
264 // print_r($entry_content);
266 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
268 $entry_comments = $item["comments"];
270 $entry_guid = db_escape_string($entry_guid);
272 $result = db_query($link, "SELECT id FROM ttrss_entries
273 WHERE guid = '$entry_guid'");
275 $owner_uid = $_SESSION["uid"];
277 if (db_num_rows($result) == 0) {
279 // base post entry does not exist, create it
282 if (is_filtered($entry_title, $entry_content, $filters)) {
285 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
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);
292 $result = db_query($link,
293 "INSERT INTO ttrss_entries
307 '$entry_timestamp_fmt',
312 '$entry_comments')");
315 // now it should exist, if not - bad luck then
317 $result = db_query($link, "SELECT
318 id,content_hash,no_orig_date,title,
319 substring(updated,1,19) as updated
322 WHERE guid = '$entry_guid'");
324 if (db_num_rows($result) == 1) {
326 // this will be used below in update handler
327 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
328 // $orig_timestamp = strtotime(db_fetch_result($result, 0, "updated"));
329 // $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
330 $orig_title = db_fetch_result($result, 0, "title");
332 $ref_id = db_fetch_result($result, 0, "id");
334 // check for user post link to main table
336 // do we allow duplicate posts with same GUID in different feeds?
337 if (get_pref($link, "ALLOW_DUPLICATE_POSTS")) {
338 $dupcheck_qpart = "AND feed_id = '$feed'";
340 $dupcheck_qpart = "";
343 $result = db_query($link,
344 "SELECT ref_id FROM ttrss_user_entries WHERE
345 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
348 // okay it doesn't exist - create user entry
349 if (db_num_rows($result) == 0) {
350 $result = db_query($link,
351 "INSERT INTO ttrss_user_entries
352 (ref_id, owner_uid, feed_id)
353 VALUES ('$ref_id', '$owner_uid', '$feed')");
356 $post_needs_update = false;
358 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE") &&
359 ($content_hash != $orig_content_hash)) {
360 $post_needs_update = true;
363 if ($orig_title != $entry_title) {
364 $post_needs_update = true;
367 // this doesn't seem to be very reliable
369 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
370 // $post_needs_update = true;
373 // if post needs update, update it and mark all user entries
374 // linking to this post as updated
376 if ($post_needs_update) {
378 // print "<!-- post $orig_title needs update : $post_needs_update -->";
380 $entry_content = db_escape_string($entry_content);
381 $entry_title = db_escape_string($entry_title);
383 db_query($link, "UPDATE ttrss_entries
384 SET title = '$entry_title', content = '$entry_content'
385 WHERE id = '$ref_id'");
387 db_query($link, "UPDATE ttrss_user_entries
388 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
393 /* $result = db_query($link, "
395 id,last_read,no_orig_date,title,feed_id,content_hash,
396 substring(updated,1,19) as updated
400 guid = '$entry_guid' AND
401 owner_uid = " . $_SESSION["uid"]." AND
404 if (db_num_rows($result) == 0) {
407 if (is_filtered($entry_title, $entry_content, $filters)) {
410 error_reporting (E_ERROR | E_WARNING | E_PARSE);
412 //$entry_guid = db_escape_string($entry_guid);
413 $entry_content = db_escape_string($entry_content);
414 $entry_title = db_escape_string($entry_title);
415 $entry_link = db_escape_string($entry_link);
416 $entry_comments = db_escape_string($entry_comments);
435 '$entry_timestamp_fmt',
441 NOW(),".$_SESSION["uid"].")";
443 $result = db_query($link, $query);
447 $orig_entry_id = db_fetch_result($result, 0, "id");
448 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
450 // print "OED: $orig_entry_id; OID: $orig_feed_id ; FID: $feed<br>";
452 if ($orig_feed_id != $feed) {
453 // print "<p>GUID $entry_guid: update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
457 $entry_is_modified = false;
459 $orig_timestamp = strtotime(db_fetch_result($result, 0, "updated"));
461 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
462 $orig_last_read = db_fetch_result($result, 0, "last_read");
463 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
464 $orig_title = db_fetch_result($result, 0, "title");
466 $last_read_qpart = "";
468 if ($orig_content_hash != $content_hash) {
469 // print "$orig_content_hash :: $content_hash<br>";
471 if (get_pref($link, 'UPDATE_POST_ON_CHECKSUM_CHANGE')) {
472 $last_read_qpart = 'last_read = null,';
474 $entry_is_modified = true;
477 if ($orig_title != $entry_title) {
478 $entry_is_modified = true;
481 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
482 $entry_is_modified = true;
485 if ($entry_is_modified) {
487 // print "$entry_guid Modified!<br>";
489 $entry_comments = db_escape_string($entry_comments);
490 $entry_content = db_escape_string($entry_content);
491 $entry_title = db_escape_string($entry_title);
492 $entry_link = db_escape_string($entry_link);
494 $query = "UPDATE ttrss_entries
497 title = '$entry_title',
498 link = '$entry_link',
499 updated = '$entry_timestamp_fmt',
500 content = '$entry_content',
501 comments = '$entry_comments',
502 content_hash = '$content_hash'
504 id = '$orig_entry_id'";
506 $result = db_query($link, $query);
511 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
515 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content,
518 $entry_tags = $entry_tags[1];
520 if (count($entry_tags) > 0) {
522 $result = db_query($link, "SELECT id,int_id
523 FROM ttrss_entries,ttrss_user_entries
524 WHERE guid = '$entry_guid'
525 AND feed_id = '$feed' AND ref_id = id
526 AND owner_uid = " . $_SESSION["uid"]);
528 if (!$result ||
db_num_rows($result) != 1) {
532 $entry_id = db_fetch_result($result, 0, "id");
533 $entry_int_id = db_fetch_result($result, 0, "int_id");
535 foreach ($entry_tags as $tag) {
536 $tag = db_escape_string(strtolower($tag));
538 $tag = str_replace("technorati tag: ", "", $tag);
540 $result = db_query($link, "SELECT id FROM ttrss_tags
541 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
542 owner_uid = ".$_SESSION["uid"]." LIMIT 1");
544 // print db_fetch_result($result, 0, "id");
546 if ($result && db_num_rows($result) == 0) {
548 // print "tagging $entry_id as $tag<br>";
550 db_query($link, "INSERT INTO ttrss_tags
551 (owner_uid,tag_name,post_int_id)
552 VALUES ('".$_SESSION["uid"]."','$tag', '$entry_int_id')");
558 db_query($link, "UPDATE ttrss_feeds
559 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
562 $error_msg = db_escape_string(magpie_error());
564 "UPDATE ttrss_feeds SET last_error = '$error_msg',
565 last_updated = NOW() WHERE id = '$feed'");
568 db_query($link, "COMMIT");
572 function print_select($id, $default, $values, $attributes = "") {
573 print "<select id=\"$id\" $attributes>";
574 foreach ($values as $v) {
580 print "<option$sel>$v</option>";
585 function is_filtered($title, $content, $filters) {
587 if ($filters["title"]) {
588 foreach ($filters["title"] as $title_filter) {
589 if (preg_match("/$title_filter/i", $title))
594 if ($filters["content"]) {
595 foreach ($filters["content"] as $content_filter) {
596 if (preg_match("/$content_filter/i", $content))
601 if ($filters["both"]) {
602 foreach ($filters["both"] as $filter) {
603 if (preg_match("/$filter/i", $title) ||
preg_match("/$filter/i", $content))
611 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link) {
613 if (file_exists($icon_file) && filesize($icon_file) > 0) {
614 $feed_icon = "<img src=\"$icon_file\">";
616 $feed_icon = "<img src=\"images/blank_icon.gif\">";
619 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
621 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
622 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
626 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
631 $fctr_class = "class=\"invisible\"";
634 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
635 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
641 function getmicrotime() {
642 list($usec, $sec) = explode(" ",microtime());
643 return ((float)$usec +
(float)$sec);
646 function print_radio($id, $default, $values, $attributes = "") {
647 foreach ($values as $v) {
655 $sel .= " value=\"1\"";
657 $sel .= " value=\"0\"";
660 print "<input type=\"radio\" $sel $attributes name=\"$id\"> $v ";
665 function initialize_user_prefs($link, $uid) {
667 $uid = db_escape_string($uid);
669 db_query($link, "BEGIN");
671 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
673 $u_result = db_query($link, "SELECT pref_name
674 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
676 $active_prefs = array();
678 while ($line = db_fetch_assoc($u_result)) {
679 array_push($active_prefs, $line["pref_name"]);
682 while ($line = db_fetch_assoc($result)) {
683 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
684 // print "adding " . $line["pref_name"] . "<br>";
686 db_query($link, "INSERT INTO ttrss_user_prefs
687 (owner_uid,pref_name,value) VALUES
688 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
693 db_query($link, "COMMIT");
697 function authenticate_user($link, $login, $password) {
699 $pwd_hash = 'SHA1:' . sha1($password);
701 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
702 login = '$login' AND (pwd_hash = '$password' OR pwd_hash = '$pwd_hash')");
704 if (db_num_rows($result) == 1) {
705 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
706 $_SESSION["name"] = db_fetch_result($result, 0, "login");
707 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
709 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
719 function http_authenticate_user($link) {
721 if (!$_SERVER['PHP_AUTH_USER']) {
723 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
724 header('HTTP/1.0 401 Unauthorized');
725 print "<h1>401 Unathorized</h1>";
730 $login = db_escape_string($_SERVER['PHP_AUTH_USER']);
731 $password = db_escape_string($_SERVER['PHP_AUTH_PW']);
733 return authenticate_user($link, $login, $password);
737 function make_password($length = 8) {
740 $possible = "0123456789bcdfghjkmnpqrstvwxyz";
744 while ($i < $length) {
745 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
747 if (!strstr($password, $char)) {
755 // this is called after user is created to initialize default feeds, labels
758 // user preferences are checked on every login, not here
760 function initialize_user($link, $uid) {
762 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
763 values ('$uid','unread = true', 'Unread articles')");
765 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
766 values ('$uid','last_read is null and unread = false', 'Updated articles')");
768 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
769 values ('$uid', 'Tiny Tiny RSS Dev. Feed',
770 'http://bah.spb.su/darcsweb/darcsweb.cgi?r=tt-rss;a=rss')");