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 return; // FIXME disabled for now
13 if (DB_TYPE
== "pgsql") {
14 db_query($link, "DELETE FROM ttrss_entries WHERE
15 marked = false AND feed_id = '$feed_id' AND
16 date_entered < NOW() - INTERVAL '$purge_interval days'");
18 db_query($link, "DELETE FROM ttrss_entries WHERE
19 marked = false AND feed_id = '$feed_id' AND
20 date_entered < 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);
57 function purge_old_posts($link) {
59 $user_id = $_SESSION["uid"];
61 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
62 WHERE owner_uid = '$user_id'");
64 while ($line = db_fetch_assoc($result)) {
66 $feed_id = $line["id"];
67 $purge_interval = $line["purge_interval"];
69 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
71 if ($purge_interval > 0) {
72 purge_feed($link, $feed_id, $purge_interval);
77 function update_all_feeds($link, $fetch) {
79 if (WEB_DEMO_MODE
) return;
81 if (get_pref($link, 'DAEMON_REFRESH_ONLY')) {
82 if (!$_GET["daemon"]) {
87 db_query($link, "BEGIN");
89 $user_id = $_SESSION["uid"];
91 $result = db_query($link, "SELECT feed_url,id,
92 substring(last_updated,1,19) as last_updated,
93 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'");
95 while ($line = db_fetch_assoc($result)) {
96 $upd_intl = $line["update_interval"];
98 if (!$upd_intl ||
$upd_intl == 0) {
99 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL');
102 if ($fetch ||
(!$line["last_updated"] ||
103 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
105 update_rss_feed($link, $line["feed_url"], $line["id"]);
109 purge_old_posts($link);
111 db_query($link, "COMMIT");
115 function check_feed_favicon($feed_url, $feed, $link) {
116 $feed_url = str_replace("http://", "", $feed_url);
117 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
119 $icon_url = "http://$feed_url/favicon.ico";
120 $icon_file = ICONS_DIR
. "/$feed.ico";
122 if (!file_exists($icon_file)) {
125 $r = fopen($icon_url, "r");
126 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
129 $tmpfname = tempnam("/tmp", "ttrssicon");
131 $t = fopen($tmpfname, "w");
134 $buf = fread($r, 16384);
142 if (!rename($tmpfname, $icon_file)) {
146 chmod($icon_file, 0644);
148 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
154 function update_rss_feed($link, $feed_url, $feed) {
156 if (WEB_DEMO_MODE
) return;
158 $feed = db_escape_string($feed);
161 $rss = fetch_rss($feed_url);
163 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
165 db_query($link, "BEGIN");
167 $feed = db_escape_string($feed);
171 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
172 check_feed_favicon($feed_url, $feed, $link);
175 $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
177 $registered_title = db_fetch_result($result, 0, "title");
178 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
180 if (!$registered_title) {
181 $feed_title = db_escape_string($rss->channel
["title"]);
182 db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
185 // print "I: " . $rss->channel["image"]["url"];
187 $icon_url = $rss->image
["url"];
189 if ($icon_url && !$orig_icon_url) {
190 $icon_url = db_escape_string($icon_url);
191 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
197 $result = db_query($link, "SELECT reg_exp,
198 (SELECT name FROM ttrss_filter_types
199 WHERE id = filter_type) as name
200 FROM ttrss_filters WHERE owner_uid = ".$_SESSION["uid"]);
202 while ($line = db_fetch_assoc($result)) {
203 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
204 array_push($filters[$line["name"]], $line["reg_exp"]);
207 foreach ($rss->items
as $item) {
209 $entry_guid = $item["id"];
211 if (!$entry_guid) $entry_guid = $item["guid"];
212 if (!$entry_guid) $entry_guid = $item["link"];
214 if (!$entry_guid) continue;
216 $entry_timestamp = "";
218 $rss_2_date = $item['pubdate'];
219 $rss_1_date = $item['dc']['date'];
220 $atom_date = $item['issued'];
222 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
223 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
224 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
226 if ($entry_timestamp == "") {
227 $entry_timestamp = time();
228 $no_orig_date = 'true';
230 $no_orig_date = 'false';
233 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
235 $entry_title = $item["title"];
236 $entry_link = $item["link"];
238 if (!$entry_title) continue;
239 if (!$entry_link) continue;
241 $entry_content = $item["content:escaped"];
243 if (!$entry_content) $entry_content = $item["content:encoded"];
244 if (!$entry_content) $entry_content = $item["content"];
245 if (!$entry_content) $entry_content = $item["description"];
247 // if (!$entry_content) continue;
250 if (is_array($entry_content)) {
251 $entry_content = $entry_content["encoded"];
252 if (!$entry_content) $entry_content = $entry_content["escaped"];
256 // print_r($entry_content);
258 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
260 $entry_comments = $item["comments"];
262 $entry_guid = db_escape_string($entry_guid);
264 $result = db_query($link, "SELECT id FROM ttrss_entries WHERE guid = '$entry_guid'");
266 $owner_uid = $_SESSION["uid"];
268 if (db_num_rows($result) == 0) {
270 // base post entry does not exist, create it
273 if (is_filtered($entry_title, $entry_content, $filters)) {
276 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
278 $entry_content = db_escape_string($entry_content);
279 $entry_title = db_escape_string($entry_title);
280 $entry_link = db_escape_string($entry_link);
281 $entry_comments = db_escape_string($entry_comments);
283 $result = db_query($link,
284 "INSERT INTO ttrss_entries
298 '$entry_timestamp_fmt',
303 '$entry_comments')");
306 // now it should exist, if not - bad luck then
308 $result = db_query($link, "SELECT id FROM ttrss_entries WHERE guid = '$entry_guid'");
310 if (db_num_rows($result) == 1) {
312 $ref_id = db_fetch_result($result, 0, "id");
314 // check for user post link to main table
316 $result = db_query($link,
317 "SELECT ref_id FROM ttrss_user_entries WHERE
318 ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND feed_id = '$feed'");
320 // okay it doesn't exist - create user entry
322 if (db_num_rows($result) == 0) {
324 $result = db_query($link,
325 "INSERT INTO ttrss_user_entries
326 (ref_id, owner_uid, feed_id)
327 VALUES ('$ref_id', '$owner_uid', '$feed')");
332 /* $result = db_query($link, "
334 id,last_read,no_orig_date,title,feed_id,content_hash,
335 substring(updated,1,19) as updated
339 guid = '$entry_guid' AND
340 owner_uid = " . $_SESSION["uid"]." AND
343 if (db_num_rows($result) == 0) {
346 if (is_filtered($entry_title, $entry_content, $filters)) {
349 error_reporting (E_ERROR | E_WARNING | E_PARSE);
351 //$entry_guid = db_escape_string($entry_guid);
352 $entry_content = db_escape_string($entry_content);
353 $entry_title = db_escape_string($entry_title);
354 $entry_link = db_escape_string($entry_link);
355 $entry_comments = db_escape_string($entry_comments);
374 '$entry_timestamp_fmt',
380 NOW(),".$_SESSION["uid"].")";
382 $result = db_query($link, $query);
386 $orig_entry_id = db_fetch_result($result, 0, "id");
387 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
389 // print "OED: $orig_entry_id; OID: $orig_feed_id ; FID: $feed<br>";
391 if ($orig_feed_id != $feed) {
392 // print "<p>GUID $entry_guid: update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
396 $entry_is_modified = false;
398 $orig_timestamp = strtotime(db_fetch_result($result, 0, "updated"));
400 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
401 $orig_last_read = db_fetch_result($result, 0, "last_read");
402 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
403 $orig_title = db_fetch_result($result, 0, "title");
405 $last_read_qpart = "";
407 if ($orig_content_hash != $content_hash) {
408 // print "$orig_content_hash :: $content_hash<br>";
410 if (get_pref($link, 'UPDATE_POST_ON_CHECKSUM_CHANGE')) {
411 $last_read_qpart = 'last_read = null,';
413 $entry_is_modified = true;
416 if ($orig_title != $entry_title) {
417 $entry_is_modified = true;
420 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
421 $entry_is_modified = true;
424 if ($entry_is_modified) {
426 // print "$entry_guid Modified!<br>";
428 $entry_comments = db_escape_string($entry_comments);
429 $entry_content = db_escape_string($entry_content);
430 $entry_title = db_escape_string($entry_title);
431 $entry_link = db_escape_string($entry_link);
433 $query = "UPDATE ttrss_entries
436 title = '$entry_title',
437 link = '$entry_link',
438 updated = '$entry_timestamp_fmt',
439 content = '$entry_content',
440 comments = '$entry_comments',
441 content_hash = '$content_hash'
443 id = '$orig_entry_id'";
445 $result = db_query($link, $query);
450 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
452 /* $entry_tags = null;
454 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content,
457 $entry_tags = $entry_tags[1];
459 if (count($entry_tags) > 0) {
461 $result = db_query($link, "SELECT id FROM ttrss_entries
462 WHERE guid = '$entry_guid'
463 AND feed_id = '$feed'
464 AND owner_uid = " . $_SESSION["uid"]);
466 if (!$result || db_num_rows($result) != 1) {
470 $entry_id = db_fetch_result($result, 0, "id");
472 foreach ($entry_tags as $tag) {
473 $tag = db_escape_string(strtolower($tag));
475 $tag = str_replace("technorati tag: ", "", $tag);
477 $result = db_query($link, "SELECT id FROM ttrss_tags
478 WHERE tag_name = '$tag' AND post_id = '$entry_id' AND
479 owner_uid = ".$_SESSION["uid"]." LIMIT 1");
481 // print db_fetch_result($result, 0, "id");
483 if ($result && db_num_rows($result) == 0) {
485 // print "tagging $entry_id as $tag<br>";
487 db_query($link, "INSERT INTO ttrss_tags (owner_uid,tag_name,post_id)
488 VALUES ('".$_SESSION["uid"]."','$tag', '$entry_id')");
494 db_query($link, "UPDATE ttrss_feeds
495 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
498 $error_msg = db_escape_string(magpie_error());
500 "UPDATE ttrss_feeds SET last_error = '$error_msg',
501 last_updated = NOW() WHERE id = '$feed'");
504 db_query($link, "COMMIT");
508 function print_select($id, $default, $values, $attributes = "") {
509 print "<select id=\"$id\" $attributes>";
510 foreach ($values as $v) {
516 print "<option$sel>$v</option>";
521 function is_filtered($title, $content, $filters) {
523 if ($filters["title"]) {
524 foreach ($filters["title"] as $title_filter) {
525 if (preg_match("/$title_filter/i", $title))
530 if ($filters["content"]) {
531 foreach ($filters["content"] as $content_filter) {
532 if (preg_match("/$content_filter/i", $content))
537 if ($filters["both"]) {
538 foreach ($filters["both"] as $filter) {
539 if (preg_match("/$filter/i", $title) ||
preg_match("/$filter/i", $content))
547 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link) {
549 if (file_exists($icon_file) && filesize($icon_file) > 0) {
550 $feed_icon = "<img src=\"$icon_file\">";
552 $feed_icon = "<img src=\"images/blank_icon.gif\">";
555 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
557 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
558 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
562 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
567 $fctr_class = "class=\"invisible\"";
570 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
571 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
577 function getmicrotime() {
578 list($usec, $sec) = explode(" ",microtime());
579 return ((float)$usec +
(float)$sec);
582 function print_radio($id, $default, $values, $attributes = "") {
583 foreach ($values as $v) {
591 $sel .= " value=\"1\"";
593 $sel .= " value=\"0\"";
596 print "<input type=\"radio\" $sel $attributes name=\"$id\"> $v ";
601 function initialize_user_prefs($link, $uid) {
603 $uid = db_escape_string($uid);
605 db_query($link, "BEGIN");
607 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
609 $u_result = db_query($link, "SELECT pref_name
610 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
612 $active_prefs = array();
614 while ($line = db_fetch_assoc($u_result)) {
615 array_push($active_prefs, $line["pref_name"]);
618 while ($line = db_fetch_assoc($result)) {
619 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
620 // print "adding " . $line["pref_name"] . "<br>";
622 db_query($link, "INSERT INTO ttrss_user_prefs
623 (owner_uid,pref_name,value) VALUES
624 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
629 db_query($link, "COMMIT");
633 function authenticate_user($link, $login, $password) {
635 $pwd_hash = 'SHA1:' . sha1($password);
637 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
638 login = '$login' AND (pwd_hash = '$password' OR pwd_hash = '$pwd_hash')");
640 if (db_num_rows($result) == 1) {
641 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
642 $_SESSION["name"] = db_fetch_result($result, 0, "login");
643 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
645 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
655 function http_authenticate_user($link) {
657 if (!$_SERVER['PHP_AUTH_USER']) {
659 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
660 header('HTTP/1.0 401 Unauthorized');
661 print "<h1>401 Unathorized</h1>";
666 $login = db_escape_string($_SERVER['PHP_AUTH_USER']);
667 $password = db_escape_string($_SERVER['PHP_AUTH_PW']);
669 return authenticate_user($link, $login, $password);
673 function make_password($length = 8) {
676 $possible = "0123456789bcdfghjkmnpqrstvwxyz";
680 while ($i < $length) {
681 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
683 if (!strstr($password, $char)) {
691 // this is called after user is created to initialize default feeds, labels
694 // user preferences are checked on every login, not here
696 function initialize_user($link, $uid) {
698 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
699 values ('$uid','unread = true', 'Unread articles')");
701 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
702 values ('$uid','last_read is null and unread = false', 'Updated articles')");
704 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
705 values ('$uid', 'Tiny Tiny RSS Dev. Feed',
706 'http://bah.spb.su/darcsweb/darcsweb.cgi?r=tt-rss;a=rss')");