diff --git a/backend.php b/backend.php
index 308ade0a7..c4edd92ea 100644
--- a/backend.php
+++ b/backend.php
@@ -60,7 +60,7 @@
if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds" &&
$op != "rss" && $op != "getUnread" && $op != "getProfiles" &&
- $op != "logout" && $op != "pubsub") {
+ $op != "fbexport" && $op != "logout" && $op != "pubsub") {
header("Content-Type: text/plain");
print json_encode(array("error" => array("code" => 6)));
@@ -121,6 +121,7 @@
require_once "modules/pref-filters.php";
require_once "modules/pref-labels.php";
require_once "modules/pref-users.php";
+ require_once "modules/pref-instances.php";
$error = sanity_check($link);
@@ -435,6 +436,10 @@
module_pref_feed_browser($link);
break; // pref-feed-browser
+ case "pref-instances":
+ module_pref_instances($link);
+ break; // pref-instances
+
case "rss":
$feed = db_escape_string($_REQUEST["id"]);
$key = db_escape_string($_REQUEST["key"]);
@@ -504,7 +509,7 @@
header("Content-type: text/html");
print __("Loading, please wait...") . " " .
"
";
- break;
+ break; // loading
case "getProfiles":
$login = db_escape_string($_REQUEST["login"]);
@@ -529,7 +534,7 @@
$_SESSION = array();
}
- break;
+ break; // getprofiles
case "pubsub":
$mode = db_escape_string($_REQUEST['hub_mode']);
@@ -571,17 +576,48 @@
header('HTTP/1.0 404 Not Found');
}
- break;
+ break; // pubsub
case "logout":
logout_user();
header("Location: tt-rss.php");
- break;
+ break; // logout
+
+ case "fbexport":
+
+ // TODO: change to _POST
+ $access_key = db_escape_string($_REQUEST["key"]);
+
+ // TODO: rate limit checking using last_connected
+ $result = db_query($link, "SELECT id FROM ttrss_linked_instances
+ WHERE access_key = '$access_key'");
+
+ if (db_num_rows($result) == 1) {
+
+ $instance_id = db_fetch_result($result, 0, "id");
+
+ $result = db_query($link, "SELECT feed_url, title, subscribers
+ FROM ttrss_feedbrowser_cache ORDER BY subscribers DESC LIMIT 100");
+
+ $feeds = array();
+
+ while ($line = db_fetch_assoc($result)) {
+ array_push($feeds, $line);
+ }
+
+ db_query($link, "UPDATE ttrss_linked_instances SET last_connected = NOW(),
+ last_status_in = 1 WHERE id = '$instance_id'");
+
+ print json_encode(array("feeds" => $feeds));
+ } else {
+ print json_encode(array("error" => array("code" => 6)));
+ }
+ break; // fbexport
default:
header("Content-Type: text/plain");
print json_encode(array("error" => array("code" => 7)));
- break;
+ break; // fallback
} // Select action according to $op value.
// We close the connection to database.
diff --git a/functions.php b/functions.php
index 0da69094a..7fd9df780 100644
--- a/functions.php
+++ b/functions.php
@@ -358,7 +358,7 @@
}
}
- function fetch_file_contents($url, $type = false, $login = false, $pass = false) {
+ function fetch_file_contents($url, $type = false, $login = false, $pass = false, $post_query = false) {
$login = urlencode($login);
$pass = urlencode($pass);
@@ -374,6 +374,11 @@
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
+ if ($post_query) {
+ curl_setopt($ch, CURLOPT_POST, true);
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $post_query);
+ }
+
if ($login && $pass)
curl_setopt($ch, CURLOPT_USERPWD, "$login:$pass");
@@ -7351,4 +7356,87 @@
return $query;
}
+
+ // Status codes:
+ // -1 - never connected
+ // 0 - no data received
+ // 1 - data received successfully
+ // 2 - did not receive valid data
+ // >10 - server error, code + 10 (e.g. 16 means server error 6)
+
+ function get_linked_feeds($link, $instance_id = false) {
+ if ($instance_id)
+ $instance_qpart = "id = '$instance_id' AND ";
+ else
+ $instance_qpart = "";
+
+ if (DB_TYPE == "pgsql") {
+ $date_qpart = "last_connected < NOW() - INTERVAL '6 hours'";
+ } else {
+ $date_qpart = "last_connected < DATE_SUB(NOW(), INTERVAL 6 HOUR)";
+ }
+
+ $result = db_query($link, "SELECT id, access_key, access_url FROM ttrss_linked_instances
+ WHERE $instance_qpart $date_qpart ORDER BY last_connected");
+
+ while ($line = db_fetch_assoc($result)) {
+ $id = $line['id'];
+
+ _debug("Updating: " . $line['access_url'] . " ($id)");
+
+ $fetch_url = $line['access_url'] . '/backend.php?op=fbexport';
+ $post_query = 'key=' . $line['access_key'];
+
+ $feeds = fetch_file_contents($fetch_url, false, false, false, $post_query);
+
+ if ($feeds) {
+ $feeds = json_decode($feeds, true);
+
+ if ($feeds) {
+ if ($feeds['error']) {
+ $status = $feeds['error']['code'] + 10;
+ } else {
+ $status = 1;
+
+ if (count($feeds['feeds']) > 0) {
+
+ db_query($link, "DELETE FROM ttrss_linked_feeds
+ WHERE instance_id = '$id'");
+
+ foreach ($feeds['feeds'] as $feed) {
+ $feed_url = db_escape_string($feed['feed_url']);
+ $title = db_escape_string($feed['title']);
+ $subscribers = db_escape_string($feed['subscribers']);
+
+ db_query($link, "INSERT INTO ttrss_linked_feeds
+ (feed_url, title, subscribers, instance_id, created, updated)
+ VALUES
+ ('$feed_url', '$title', '$subscribers', '$id', NOW(), NOW())");
+ }
+ } else {
+ // received 0 feeds, this might indicate that
+ // the instance on the other hand is rebuilding feedbrowser cache
+ // we will try again later
+
+ // TODO: maybe perform expiration based on updated here?
+ }
+
+ _debug("Processed " . count($feeds['feeds']) . " feeds.");
+ }
+ } else {
+ $status = 2;
+ }
+
+ } else {
+ $status = 0;
+ }
+
+ _debug("Status: $status");
+
+ db_query($link, "UPDATE ttrss_linked_instances SET
+ last_status_out = '$status', last_connected = NOW() WHERE id = '$id'");
+
+ }
+
+ }
?>
diff --git a/modules/backend-rpc.php b/modules/backend-rpc.php
index f21ca5814..75ce6886a 100644
--- a/modules/backend-rpc.php
+++ b/modules/backend-rpc.php
@@ -834,6 +834,13 @@
return;
}
+ if ($subop == "genHash") {
+ $hash = sha1(uniqid(rand(), true));
+
+ print json_encode(array("hash" => $hash));
+ return;
+ }
+
print json_encode(array("error" => array("code" => 7,
"message" => "Unknown method: $subop")));
}
diff --git a/modules/popup-dialog.php b/modules/popup-dialog.php
index 2eb63af9d..6cb60eef4 100644
--- a/modules/popup-dialog.php
+++ b/modules/popup-dialog.php
@@ -990,6 +990,61 @@
type=\"submit\">".
__('Close this window')."";
print "";
+ }
+
+ if ($id == "addInstance") {
+
+ print "";
+ print "";
+
+ print "
" . __("Use one access key for both linked instances."); + + print "
" . __("Use one access key for both linked instances."); + + print "
" . __("You can connect other instances of Tiny Tiny RSS to this one to share Popular feeds. Link to this instance of Tiny Tiny RSS by using this URL:"); + + print " (display url)"; + + print "
| + | ".__('Instance URL')." | +".__('Access key')." | +".__('Last connected')." | +".__('Stored feeds')." | +
| "; + + $onclick = "onclick='editInstance($id, event)' title='".__('Click to edit')."'"; + + $access_key = mb_substr($line['access_key'], 0, 4) . '...' . + mb_substr($line['access_key'], -4); + + print " | " . htmlspecialchars($line['access_url']) . " | "; + print "" . htmlspecialchars($access_key) . " | "; + print "" . htmlspecialchars($line['last_connected']) . " | "; + print "" . htmlspecialchars($line['num_feeds']) . " | "; + + print "