init.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. class Instances extends Plugin implements IHandler {
  3. private $link;
  4. private $host;
  5. private $status_codes = array(
  6. 0 => "Connection failed",
  7. 1 => "Success",
  8. 2 => "Invalid object received",
  9. 16 => "Access denied" );
  10. function about() {
  11. return array(1.0,
  12. "Support for linking tt-rss instances together and sharing popular feeds.",
  13. "fox",
  14. true);
  15. }
  16. function init($host) {
  17. $this->link = $host->get_link();
  18. $this->host = $host;
  19. $host->add_hook($host::HOOK_PREFS_TABS, $this);
  20. $host->add_handler("pref-instances", "*", $this);
  21. $host->add_handler("public", "fbexport", $this);
  22. $host->add_command("get-feeds", "receive popular feeds from linked instances", $this);
  23. $host->add_hook($host::HOOK_UPDATE_TASK, $this);
  24. }
  25. function hook_update_task($args) {
  26. _debug("Get linked feeds...");
  27. $this->get_linked_feeds($this->link);
  28. }
  29. // Status codes:
  30. // -1 - never connected
  31. // 0 - no data received
  32. // 1 - data received successfully
  33. // 2 - did not receive valid data
  34. // >10 - server error, code + 10 (e.g. 16 means server error 6)
  35. function get_linked_feeds($link, $instance_id = false) {
  36. if ($instance_id)
  37. $instance_qpart = "id = '$instance_id' AND ";
  38. else
  39. $instance_qpart = "";
  40. if (DB_TYPE == "pgsql") {
  41. $date_qpart = "last_connected < NOW() - INTERVAL '6 hours'";
  42. } else {
  43. $date_qpart = "last_connected < DATE_SUB(NOW(), INTERVAL 6 HOUR)";
  44. }
  45. $result = db_query($link, "SELECT id, access_key, access_url FROM ttrss_linked_instances
  46. WHERE $instance_qpart $date_qpart ORDER BY last_connected");
  47. while ($line = db_fetch_assoc($result)) {
  48. $id = $line['id'];
  49. _debug("Updating: " . $line['access_url'] . " ($id)");
  50. $fetch_url = $line['access_url'] . '/public.php?op=fbexport';
  51. $post_query = 'key=' . $line['access_key'];
  52. $feeds = fetch_file_contents($fetch_url, false, false, false, $post_query);
  53. // try doing it the old way
  54. if (!$feeds) {
  55. $fetch_url = $line['access_url'] . '/backend.php?op=fbexport';
  56. $feeds = fetch_file_contents($fetch_url, false, false, false, $post_query);
  57. }
  58. if ($feeds) {
  59. $feeds = json_decode($feeds, true);
  60. if ($feeds) {
  61. if ($feeds['error']) {
  62. $status = $feeds['error']['code'] + 10;
  63. // access denied
  64. if ($status == 16) {
  65. db_query($link, "DELETE FROM ttrss_linked_feeds
  66. WHERE instance_id = '$id'");
  67. }
  68. } else {
  69. $status = 1;
  70. if (count($feeds['feeds']) > 0) {
  71. db_query($link, "DELETE FROM ttrss_linked_feeds
  72. WHERE instance_id = '$id'");
  73. foreach ($feeds['feeds'] as $feed) {
  74. $feed_url = db_escape_string($this->link, $feed['feed_url']);
  75. $title = db_escape_string($this->link, $feed['title']);
  76. $subscribers = db_escape_string($this->link, $feed['subscribers']);
  77. $site_url = db_escape_string($this->link, $feed['site_url']);
  78. db_query($link, "INSERT INTO ttrss_linked_feeds
  79. (feed_url, site_url, title, subscribers, instance_id, created, updated)
  80. VALUES
  81. ('$feed_url', '$site_url', '$title', '$subscribers', '$id', NOW(), NOW())");
  82. }
  83. } else {
  84. // received 0 feeds, this might indicate that
  85. // the instance on the other hand is rebuilding feedbrowser cache
  86. // we will try again later
  87. // TODO: maybe perform expiration based on updated here?
  88. }
  89. _debug("Processed " . count($feeds['feeds']) . " feeds.");
  90. }
  91. } else {
  92. $status = 2;
  93. }
  94. } else {
  95. $status = 0;
  96. }
  97. _debug("Status: $status");
  98. db_query($link, "UPDATE ttrss_linked_instances SET
  99. last_status_out = '$status', last_connected = NOW() WHERE id = '$id'");
  100. }
  101. }
  102. function get_feeds() {
  103. $this->get_linked_feeds($this->link, false);
  104. }
  105. function get_prefs_js() {
  106. return file_get_contents(dirname(__FILE__) . "/instances.js");
  107. }
  108. function hook_prefs_tabs($args) {
  109. if ($_SESSION["access_level"] >= 10 || SINGLE_USER_MODE) {
  110. ?><div id="instanceConfigTab" dojoType="dijit.layout.ContentPane"
  111. href="backend.php?op=pref-instances"
  112. title="<?php echo __('Linked') ?>"></div><?php
  113. }
  114. }
  115. function csrf_ignore($method) {
  116. $csrf_ignored = array("index", "edit");
  117. return array_search($method, $csrf_ignored) !== false;
  118. }
  119. function before($method) {
  120. if ($_SESSION["uid"]) {
  121. if ($_SESSION["access_level"] < 10) {
  122. print __("Your access level is insufficient to open this tab.");
  123. return false;
  124. }
  125. return true;
  126. }
  127. return false;
  128. }
  129. function after() {
  130. return true;
  131. }
  132. function remove() {
  133. $ids = db_escape_string($this->link, $_REQUEST['ids']);
  134. db_query($this->link, "DELETE FROM ttrss_linked_instances WHERE
  135. id IN ($ids)");
  136. }
  137. function add() {
  138. $id = db_escape_string($this->link, $_REQUEST["id"]);
  139. $access_url = db_escape_string($this->link, $_REQUEST["access_url"]);
  140. $access_key = db_escape_string($this->link, $_REQUEST["access_key"]);
  141. db_query($this->link, "BEGIN");
  142. $result = db_query($this->link, "SELECT id FROM ttrss_linked_instances
  143. WHERE access_url = '$access_url'");
  144. if (db_num_rows($result) == 0) {
  145. db_query($this->link, "INSERT INTO ttrss_linked_instances
  146. (access_url, access_key, last_connected, last_status_in, last_status_out)
  147. VALUES
  148. ('$access_url', '$access_key', '1970-01-01', -1, -1)");
  149. }
  150. db_query($this->link, "COMMIT");
  151. }
  152. function edit() {
  153. $id = db_escape_string($this->link, $_REQUEST["id"]);
  154. $result = db_query($this->link, "SELECT * FROM ttrss_linked_instances WHERE
  155. id = '$id'");
  156. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"id\" value=\"$id\">";
  157. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-instances\">";
  158. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"editSave\">";
  159. print "<div class=\"dlgSec\">".__("Instance")."</div>";
  160. print "<div class=\"dlgSecCont\">";
  161. /* URL */
  162. $access_url = htmlspecialchars(db_fetch_result($result, 0, "access_url"));
  163. print __("URL:") . " ";
  164. print "<input dojoType=\"dijit.form.ValidationTextBox\" required=\"1\"
  165. placeHolder=\"".__("Instance URL")."\"
  166. regExp='^(http|https)://.*'
  167. style=\"font-size : 16px; width: 20em\" name=\"access_url\"
  168. value=\"$access_url\">";
  169. print "<hr/>";
  170. $access_key = htmlspecialchars(db_fetch_result($result, 0, "access_key"));
  171. /* Access key */
  172. print __("Access key:") . " ";
  173. print "<input dojoType=\"dijit.form.ValidationTextBox\" required=\"1\"
  174. placeHolder=\"".__("Access key")."\" regExp='\w{40}'
  175. style=\"width: 20em\" name=\"access_key\" id=\"instance_edit_key\"
  176. value=\"$access_key\">";
  177. print "<p class='insensitive'>" . __("Use one access key for both linked instances.");
  178. print "</div>";
  179. print "<div class=\"dlgButtons\">
  180. <div style='float : left'>
  181. <button dojoType=\"dijit.form.Button\"
  182. onclick=\"return dijit.byId('instanceEditDlg').regenKey()\">".
  183. __('Generate new key')."</button>
  184. </div>
  185. <button dojoType=\"dijit.form.Button\"
  186. onclick=\"return dijit.byId('instanceEditDlg').execute()\">".
  187. __('Save')."</button>
  188. <button dojoType=\"dijit.form.Button\"
  189. onclick=\"return dijit.byId('instanceEditDlg').hide()\"\">".
  190. __('Cancel')."</button></div>";
  191. }
  192. function editSave() {
  193. $id = db_escape_string($this->link, $_REQUEST["id"]);
  194. $access_url = db_escape_string($this->link, $_REQUEST["access_url"]);
  195. $access_key = db_escape_string($this->link, $_REQUEST["access_key"]);
  196. db_query($this->link, "UPDATE ttrss_linked_instances SET
  197. access_key = '$access_key', access_url = '$access_url',
  198. last_connected = '1970-01-01'
  199. WHERE id = '$id'");
  200. }
  201. function index() {
  202. if (!function_exists('curl_init')) {
  203. print "<div style='padding : 1em'>";
  204. print_error("This functionality requires CURL functions. Please enable CURL in your PHP configuration (you might also want to disable open_basedir in php.ini) and reload this page.");
  205. print "</div>";
  206. }
  207. print "<div id=\"pref-instance-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
  208. print "<div id=\"pref-instance-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
  209. print "<div id=\"pref-instance-toolbar\" dojoType=\"dijit.Toolbar\">";
  210. $sort = db_escape_string($this->link, $_REQUEST["sort"]);
  211. if (!$sort || $sort == "undefined") {
  212. $sort = "access_url";
  213. }
  214. print "<div dojoType=\"dijit.form.DropDownButton\">".
  215. "<span>" . __('Select')."</span>";
  216. print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  217. print "<div onclick=\"selectTableRows('prefInstanceList', 'all')\"
  218. dojoType=\"dijit.MenuItem\">".__('All')."</div>";
  219. print "<div onclick=\"selectTableRows('prefInstanceList', 'none')\"
  220. dojoType=\"dijit.MenuItem\">".__('None')."</div>";
  221. print "</div></div>";
  222. print "<button dojoType=\"dijit.form.Button\" onclick=\"addInstance()\">".__('Link instance')."</button>";
  223. print "<button dojoType=\"dijit.form.Button\" onclick=\"editSelectedInstance()\">".__('Edit')."</button>";
  224. print "<button dojoType=\"dijit.form.Button\" onclick=\"removeSelectedInstances()\">".__('Remove')."</button>";
  225. print "</div>"; #toolbar
  226. $result = db_query($this->link, "SELECT *,
  227. (SELECT COUNT(*) FROM ttrss_linked_feeds
  228. WHERE instance_id = ttrss_linked_instances.id) AS num_feeds
  229. FROM ttrss_linked_instances
  230. ORDER BY $sort");
  231. print "<p class=\"insensitive\" style='margin-left : 1em;'>" . __("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:");
  232. print " <a href=\"#\" onclick=\"alert('".htmlspecialchars(get_self_url_prefix())."')\">(display url)</a>";
  233. print "<p><table width='100%' id='prefInstanceList' class='prefInstanceList' cellspacing='0'>";
  234. print "<tr class=\"title\">
  235. <td align='center' width=\"5%\">&nbsp;</td>
  236. <td width=''><a href=\"#\" onclick=\"updateInstanceList('access_url')\">".__('Instance URL')."</a></td>
  237. <td width='20%'><a href=\"#\" onclick=\"updateInstanceList('access_key')\">".__('Access key')."</a></td>
  238. <td width='10%'><a href=\"#\" onclick=\"updateUsersList('last_connected')\">".__('Last connected')."</a></td>
  239. <td width='10%'><a href=\"#\" onclick=\"updateUsersList('last_status_out')\">".__('Status')."</a></td>
  240. <td width='10%'><a href=\"#\" onclick=\"updateUsersList('num_feeds')\">".__('Stored feeds')."</a></td>
  241. </tr>";
  242. $lnum = 0;
  243. while ($line = db_fetch_assoc($result)) {
  244. $class = ($lnum % 2) ? "even" : "odd";
  245. $id = $line['id'];
  246. $this_row_id = "id=\"LIRR-$id\"";
  247. $line["last_connected"] = make_local_datetime($this->link, $line["last_connected"], false);
  248. print "<tr class=\"$class\" $this_row_id>";
  249. print "<td align='center'><input onclick='toggleSelectRow(this);'
  250. type=\"checkbox\" id=\"LICHK-$id\"></td>";
  251. $onclick = "onclick='editInstance($id, event)' title='".__('Click to edit')."'";
  252. $access_key = mb_substr($line['access_key'], 0, 4) . '...' .
  253. mb_substr($line['access_key'], -4);
  254. print "<td $onclick>" . htmlspecialchars($line['access_url']) . "</td>";
  255. print "<td $onclick>" . htmlspecialchars($access_key) . "</td>";
  256. print "<td $onclick>" . htmlspecialchars($line['last_connected']) . "</td>";
  257. print "<td $onclick>" . $this->status_codes[$line['last_status_out']] . "</td>";
  258. print "<td $onclick>" . htmlspecialchars($line['num_feeds']) . "</td>";
  259. print "</tr>";
  260. ++$lnum;
  261. }
  262. print "</table>";
  263. print "</div>"; #pane
  264. global $pluginhost;
  265. $pluginhost->run_hooks($pluginhost::HOOK_PREFS_TAB,
  266. "hook_prefs_tab", "prefInstances");
  267. print "</div>"; #container
  268. }
  269. function fbexport() {
  270. $access_key = db_escape_string($this->link, $_POST["key"]);
  271. // TODO: rate limit checking using last_connected
  272. $result = db_query($this->link, "SELECT id FROM ttrss_linked_instances
  273. WHERE access_key = '$access_key'");
  274. if (db_num_rows($result) == 1) {
  275. $instance_id = db_fetch_result($result, 0, "id");
  276. $result = db_query($this->link, "SELECT feed_url, site_url, title, subscribers
  277. FROM ttrss_feedbrowser_cache ORDER BY subscribers DESC LIMIT 100");
  278. $feeds = array();
  279. while ($line = db_fetch_assoc($result)) {
  280. array_push($feeds, $line);
  281. }
  282. db_query($this->link, "UPDATE ttrss_linked_instances SET
  283. last_status_in = 1 WHERE id = '$instance_id'");
  284. print json_encode(array("feeds" => $feeds));
  285. } else {
  286. print json_encode(array("error" => array("code" => 6)));
  287. }
  288. }
  289. }
  290. ?>