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