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