5 define('DEFAULT_ERROR_LEVEL', E_ALL
);
7 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE
);
10 require_once 'config.php';
11 require_once 'db-prefs.php';
13 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
15 function purge_feed($link, $feed_id, $purge_interval) {
17 if (DB_TYPE
== "pgsql") {
18 db_query($link, "DELETE FROM ttrss_user_entries WHERE
19 marked = false AND feed_id = '$feed_id' AND
20 (SELECT date_entered FROM ttrss_entries WHERE
21 id = ref_id) < NOW() - INTERVAL '$purge_interval days'");
23 db_query($link, "DELETE FROM ttrss_user_entries WHERE
24 marked = false AND feed_id = '$feed_id' AND
25 (SELECT date_entered FROM ttrss_entries WHERE
26 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
30 function global_purge_old_posts($link, $do_output = false) {
32 $result = db_query($link,
33 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds");
35 while ($line = db_fetch_assoc($result)) {
37 $feed_id = $line["id"];
38 $purge_interval = $line["purge_interval"];
39 $owner_uid = $line["owner_uid"];
41 if ($purge_interval == 0) {
43 $tmp_result = db_query($link,
44 "SELECT value FROM ttrss_user_prefs WHERE
45 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
47 if (db_num_rows($tmp_result) != 0) {
48 $purge_interval = db_fetch_result($tmp_result, 0, "value");
53 print "<feed id='$feed_id' p_intl='$purge_interval'/>";
56 if ($purge_interval > 0) {
57 purge_feed($link, $feed_id, $purge_interval);
61 // purge orphaned posts in main content table
62 db_query($link, "DELETE FROM ttrss_entries WHERE
63 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
67 function purge_old_posts($link) {
69 $user_id = $_SESSION["uid"];
71 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
72 WHERE owner_uid = '$user_id'");
74 while ($line = db_fetch_assoc($result)) {
76 $feed_id = $line["id"];
77 $purge_interval = $line["purge_interval"];
79 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
81 if ($purge_interval > 0) {
82 purge_feed($link, $feed_id, $purge_interval);
86 // purge orphaned posts in main content table
87 db_query($link, "DELETE FROM ttrss_entries WHERE
88 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
91 function update_all_feeds($link, $fetch, $user_id = false) {
93 if (WEB_DEMO_MODE
) return;
96 $user_id = $_SESSION["uid"];
97 purge_old_posts($link);
100 if (DAEMON_REFRESH_ONLY
&& !$_GET["daemon"]) {
104 // db_query($link, "BEGIN");
106 $result = db_query($link, "SELECT feed_url,id,
107 substring(last_updated,1,19) as last_updated,
108 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'");
110 while ($line = db_fetch_assoc($result)) {
111 $upd_intl = $line["update_interval"];
113 if (!$upd_intl ||
$upd_intl == 0) {
114 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
117 if ($fetch ||
(!$line["last_updated"] ||
118 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
120 update_rss_feed($link, $line["feed_url"], $line["id"]);
124 // db_query($link, "COMMIT");
128 function check_feed_favicon($feed_url, $feed, $link) {
129 $feed_url = str_replace("http://", "", $feed_url);
130 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
132 $icon_url = "http://$feed_url/favicon.ico";
133 $icon_file = ICONS_DIR
. "/$feed.ico";
135 if (!file_exists($icon_file)) {
138 $r = fopen($icon_url, "r");
139 error_reporting (DEFAULT_ERROR_LEVEL
);
142 $tmpfname = tempnam("/tmp", "ttrssicon");
144 $t = fopen($tmpfname, "w");
147 $buf = fread($r, 16384);
155 if (!rename($tmpfname, $icon_file)) {
159 chmod($icon_file, 0644);
161 error_reporting (DEFAULT_ERROR_LEVEL
);
167 function update_rss_feed($link, $feed_url, $feed) {
169 if (WEB_DEMO_MODE
) return;
171 $feed = db_escape_string($feed);
174 $rss = fetch_rss($feed_url);
176 error_reporting (DEFAULT_ERROR_LEVEL
);
178 $feed = db_escape_string($feed);
182 db_query($link, "BEGIN");
184 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
185 FROM ttrss_feeds WHERE id = '$feed'");
187 $registered_title = db_fetch_result($result, 0, "title");
188 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
189 $orig_site_url = db_fetch_result($result, 0, "site_url");
191 $owner_uid = db_fetch_result($result, 0, "owner_uid");
193 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid)) {
194 check_feed_favicon($feed_url, $feed, $link);
197 if (!$registered_title) {
198 $feed_title = db_escape_string($rss->channel
["title"]);
199 db_query($link, "UPDATE ttrss_feeds SET
200 title = '$feed_title' WHERE id = '$feed'");
203 $site_url = $rss->channel
["link"];
205 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
206 $site_url = db_escape_string($rss->channel
["link"]);
207 db_query($link, "UPDATE ttrss_feeds SET
208 site_url = '$site_url' WHERE id = '$feed'");
211 // print "I: " . $rss->channel["image"]["url"];
213 $icon_url = $rss->image
["url"];
215 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
216 $icon_url = db_escape_string($icon_url);
217 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
223 $result = db_query($link, "SELECT reg_exp,
224 (SELECT name FROM ttrss_filter_types
225 WHERE id = filter_type) as name
226 FROM ttrss_filters WHERE
227 owner_uid = $owner_uid AND
228 (feed_id IS NULL OR feed_id = '$feed')");
230 while ($line = db_fetch_assoc($result)) {
231 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
232 array_push($filters[$line["name"]], $line["reg_exp"]);
235 foreach ($rss->items
as $item) {
237 $entry_guid = $item["id"];
239 if (!$entry_guid) $entry_guid = $item["guid"];
240 if (!$entry_guid) $entry_guid = $item["link"];
242 if (!$entry_guid) continue;
244 $entry_timestamp = "";
246 $rss_2_date = $item['pubdate'];
247 $rss_1_date = $item['dc']['date'];
248 $atom_date = $item['issued'];
250 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
251 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
252 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
254 if ($entry_timestamp == "") {
255 $entry_timestamp = time();
256 $no_orig_date = 'true';
258 $no_orig_date = 'false';
261 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
263 $entry_title = $item["title"];
264 $entry_link = $item["link"];
266 if (!$entry_title) continue;
267 if (!$entry_link) continue;
269 $entry_content = $item["content:escaped"];
271 if (!$entry_content) $entry_content = $item["content:encoded"];
272 if (!$entry_content) $entry_content = $item["content"];
273 if (!$entry_content) $entry_content = $item["description"];
275 $entry_content_unescaped = $entry_content;
277 // if (!$entry_content) continue;
280 if (is_array($entry_content)) {
281 $entry_content = $entry_content["encoded"];
282 if (!$entry_content) $entry_content = $entry_content["escaped"];
286 // print_r($entry_content);
288 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
290 $entry_comments = $item["comments"];
292 $entry_guid = db_escape_string($entry_guid);
294 $result = db_query($link, "SELECT id FROM ttrss_entries
295 WHERE guid = '$entry_guid'");
297 $entry_content = db_escape_string($entry_content);
298 $entry_title = db_escape_string($entry_title);
299 $entry_link = db_escape_string($entry_link);
300 $entry_comments = db_escape_string($entry_comments);
302 if (db_num_rows($result) == 0) {
304 // base post entry does not exist, create it
306 $result = db_query($link,
307 "INSERT INTO ttrss_entries
321 '$entry_timestamp_fmt',
326 '$entry_comments')");
329 // now it should exist, if not - bad luck then
331 $result = db_query($link, "SELECT
332 id,content_hash,no_orig_date,title,
333 substring(updated,1,19) as updated
336 WHERE guid = '$entry_guid'");
338 if (db_num_rows($result) == 1) {
340 // this will be used below in update handler
341 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
342 // $orig_timestamp = strtotime(db_fetch_result($result, 0, "updated"));
343 // $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
344 $orig_title = db_fetch_result($result, 0, "title");
346 $ref_id = db_fetch_result($result, 0, "id");
348 // check for user post link to main table
350 // do we allow duplicate posts with same GUID in different feeds?
351 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) {
352 $dupcheck_qpart = "AND feed_id = '$feed'";
354 $dupcheck_qpart = "";
358 if (is_filtered($entry_title, $entry_content, $entry_link, $filters)) {
361 error_reporting (DEFAULT_ERROR_LEVEL
);
363 $result = db_query($link,
364 "SELECT ref_id FROM ttrss_user_entries WHERE
365 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
368 // okay it doesn't exist - create user entry
369 if (db_num_rows($result) == 0) {
370 $result = db_query($link,
371 "INSERT INTO ttrss_user_entries
372 (ref_id, owner_uid, feed_id)
373 VALUES ('$ref_id', '$owner_uid', '$feed')");
376 $post_needs_update = false;
378 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) &&
379 ($content_hash != $orig_content_hash)) {
380 $post_needs_update = true;
383 if ($orig_title != $entry_title) {
384 $post_needs_update = true;
387 // this doesn't seem to be very reliable
389 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
390 // $post_needs_update = true;
393 // if post needs update, update it and mark all user entries
394 // linking to this post as updated
395 if ($post_needs_update) {
397 // print "<!-- post $orig_title needs update : $post_needs_update -->";
399 db_query($link, "UPDATE ttrss_entries
400 SET title = '$entry_title', content = '$entry_content'
401 WHERE id = '$ref_id'");
403 db_query($link, "UPDATE ttrss_user_entries
404 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
410 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
414 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i",
415 $entry_content_unescaped, $entry_tags);
417 // print "<br>$entry_title : $entry_content_unescaped<br>";
418 // print_r($entry_tags);
420 $entry_tags = $entry_tags[1];
422 if (count($entry_tags) > 0) {
424 $result = db_query($link, "SELECT id,int_id
425 FROM ttrss_entries,ttrss_user_entries
426 WHERE guid = '$entry_guid'
427 AND feed_id = '$feed' AND ref_id = id
428 AND owner_uid = '$owner_uid'");
430 if (db_num_rows($result) == 1) {
432 $entry_id = db_fetch_result($result, 0, "id");
433 $entry_int_id = db_fetch_result($result, 0, "int_id");
435 foreach ($entry_tags as $tag) {
436 $tag = db_escape_string(strtolower($tag));
438 $tag = str_replace("technorati tag: ", "", $tag);
440 $result = db_query($link, "SELECT id FROM ttrss_tags
441 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
442 owner_uid = '$owner_uid' LIMIT 1");
444 // print db_fetch_result($result, 0, "id");
446 if ($result && db_num_rows($result) == 0) {
448 // print "tagging $entry_id as $tag<br>";
450 db_query($link, "INSERT INTO ttrss_tags
451 (owner_uid,tag_name,post_int_id)
452 VALUES ('$owner_uid','$tag', '$entry_int_id')");
459 db_query($link, "UPDATE ttrss_feeds
460 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
462 db_query($link, "COMMIT");
465 $error_msg = db_escape_string(magpie_error());
467 "UPDATE ttrss_feeds SET last_error = '$error_msg',
468 last_updated = NOW() WHERE id = '$feed'");
473 function print_select($id, $default, $values, $attributes = "") {
474 print "<select id=\"$id\" $attributes>";
475 foreach ($values as $v) {
481 print "<option$sel>$v</option>";
486 function is_filtered($title, $content, $link, $filters) {
488 if ($filters["title"]) {
489 foreach ($filters["title"] as $title_filter) {
490 if (preg_match("/$title_filter/i", $title))
495 if ($filters["content"]) {
496 foreach ($filters["content"] as $content_filter) {
497 if (preg_match("/$content_filter/i", $content))
502 if ($filters["both"]) {
503 foreach ($filters["both"] as $filter) {
504 if (preg_match("/$filter/i", $title) ||
preg_match("/$filter/i", $content))
509 if ($filters["link"]) {
510 foreach ($filters["link"] as $link_filter) {
511 if (preg_match("/$link_filter/i", $link))
519 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link) {
521 if (file_exists($icon_file) && filesize($icon_file) > 0) {
522 $feed_icon = "<img src=\"$icon_file\">";
524 $feed_icon = "<img src=\"images/blank_icon.gif\">";
527 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
529 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
530 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
534 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
539 $fctr_class = "class=\"invisible\"";
542 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
543 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
549 function getmicrotime() {
550 list($usec, $sec) = explode(" ",microtime());
551 return ((float)$usec +
(float)$sec);
554 function print_radio($id, $default, $values, $attributes = "") {
555 foreach ($values as $v) {
563 $sel .= " value=\"1\"";
565 $sel .= " value=\"0\"";
568 print "<input type=\"radio\" $sel $attributes name=\"$id\"> $v ";
573 function initialize_user_prefs($link, $uid) {
575 $uid = db_escape_string($uid);
577 db_query($link, "BEGIN");
579 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
581 $u_result = db_query($link, "SELECT pref_name
582 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
584 $active_prefs = array();
586 while ($line = db_fetch_assoc($u_result)) {
587 array_push($active_prefs, $line["pref_name"]);
590 while ($line = db_fetch_assoc($result)) {
591 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
592 // print "adding " . $line["pref_name"] . "<br>";
594 db_query($link, "INSERT INTO ttrss_user_prefs
595 (owner_uid,pref_name,value) VALUES
596 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
601 db_query($link, "COMMIT");
605 function authenticate_user($link, $login, $password) {
607 $pwd_hash = 'SHA1:' . sha1($password);
609 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
610 login = '$login' AND (pwd_hash = '$password' OR pwd_hash = '$pwd_hash')");
612 if (db_num_rows($result) == 1) {
613 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
614 $_SESSION["name"] = db_fetch_result($result, 0, "login");
615 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
617 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
620 $user_theme = get_user_theme_path($link);
622 $_SESSION["theme"] = $user_theme;
624 initialize_user_prefs($link, $_SESSION["uid"]);
633 function make_password($length = 8) {
636 $possible = "0123456789bcdfghjkmnpqrstvwxyz";
640 while ($i < $length) {
641 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
643 if (!strstr($password, $char)) {
651 // this is called after user is created to initialize default feeds, labels
654 // user preferences are checked on every login, not here
656 function initialize_user($link, $uid) {
658 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
659 values ('$uid','unread = true', 'Unread articles')");
661 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
662 values ('$uid','last_read is null and unread = false', 'Updated articles')");
664 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
665 values ('$uid', 'Tiny Tiny RSS: New Releases',
666 'http://tt-rss.spb.ru/releases.rss')");
670 function logout_user() {
674 function login_sequence($link) {
675 if (!SINGLE_USER_MODE
) {
677 if (!USE_HTTP_AUTH
) {
678 if (!$_SESSION["uid"]) {
679 header("Location: login.php?rt=tt-rss.php");
683 if (!$_SESSION["uid"]) {
684 if (!$_SERVER["PHP_AUTH_USER"]) {
686 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
687 header('HTTP/1.0 401 Unauthorized');
691 $auth_result = authenticate_user($link,
692 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
695 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
696 header('HTTP/1.0 401 Unauthorized');
703 $_SESSION["uid"] = 1;
704 $_SESSION["name"] = "admin";
705 initialize_user_prefs($link, 1);
709 function truncate_string($str, $max_len) {
710 if (strlen($str) > $max_len) {
711 return substr($str, 0, $max_len) . "...";
717 function get_user_theme_path($link) {
718 $result = db_query($link, "SELECT theme_path FROM ttrss_themes
719 WHERE id = (SELECT theme_id FROM ttrss_users
720 WHERE id = " . $_SESSION["uid"] . ")");
721 if (db_num_rows($result) != 0) {
722 return db_fetch_result($result, 0, "theme_path");