]> git.wh0rd.org - tt-rss.git/blame - plugins/instances/instances.php
update_rss_feed: escape error string immediately
[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);
19 }
20
21 function get_prefs_js() {
22 return file_get_contents(dirname(__FILE__) . "/instances.js");
23 }
24
25 function hook_prefs_tabs($args) {
26 if ($_SESSION["access_level"] >= 10 || SINGLE_USER_MODE) {
27 ?><div id="instanceConfigTab" dojoType="dijit.layout.ContentPane"
28 href="backend.php?op=pref-instances"
29 title="<?php echo __('Linked') ?>"></div><?php
30 }
31 }
32
8484ce22
AD
33 function csrf_ignore($method) {
34 $csrf_ignored = array("index", "edit");
35
36 return array_search($method, $csrf_ignored) !== false;
37 }
38
17f9d200 39 function before($method) {
6cbe53c9 40 if ($_SESSION["uid"]) {
5f0a3741
AD
41 if ($_SESSION["access_level"] < 10) {
42 print __("Your access level is insufficient to open this tab.");
43 return false;
44 }
45 return true;
46 }
47 return false;
48 }
49
6cbe53c9
AD
50 function after() {
51 return true;
52 }
53
5f0a3741
AD
54 function remove() {
55 $ids = db_escape_string($_REQUEST['ids']);
56
57 db_query($this->link, "DELETE FROM ttrss_linked_instances WHERE
58 id IN ($ids)");
59 }
60
61 function add() {
62 $id = db_escape_string($_REQUEST["id"]);
63 $access_url = db_escape_string($_REQUEST["access_url"]);
64 $access_key = db_escape_string($_REQUEST["access_key"]);
65
66 db_query($this->link, "BEGIN");
67
68 $result = db_query($this->link, "SELECT id FROM ttrss_linked_instances
69 WHERE access_url = '$access_url'");
70
71 if (db_num_rows($result) == 0) {
72 db_query($this->link, "INSERT INTO ttrss_linked_instances
73 (access_url, access_key, last_connected, last_status_in, last_status_out)
74 VALUES
75 ('$access_url', '$access_key', '1970-01-01', -1, -1)");
76
77 }
78
79 db_query($this->link, "COMMIT");
80 }
81
82 function edit() {
83 $id = db_escape_string($_REQUEST["id"]);
84
85 $result = db_query($this->link, "SELECT * FROM ttrss_linked_instances WHERE
86 id = '$id'");
87
88 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"id\" value=\"$id\">";
89 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-instances\">";
90 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"editSave\">";
91
92 print "<div class=\"dlgSec\">".__("Instance")."</div>";
93
94 print "<div class=\"dlgSecCont\">";
95
96 /* URL */
97
98 $access_url = htmlspecialchars(db_fetch_result($result, 0, "access_url"));
99
100 print __("URL:") . " ";
101
102 print "<input dojoType=\"dijit.form.ValidationTextBox\" required=\"1\"
103 placeHolder=\"".__("Instance URL")."\"
104 regExp='^(http|https)://.*'
105 style=\"font-size : 16px; width: 20em\" name=\"access_url\"
106 value=\"$access_url\">";
107
108 print "<hr/>";
109
110 $access_key = htmlspecialchars(db_fetch_result($result, 0, "access_key"));
111
112 /* Access key */
113
114 print __("Access key:") . " ";
115
116 print "<input dojoType=\"dijit.form.ValidationTextBox\" required=\"1\"
117 placeHolder=\"".__("Access key")."\" regExp='\w{40}'
118 style=\"width: 20em\" name=\"access_key\" id=\"instance_edit_key\"
119 value=\"$access_key\">";
120
121 print "<p class='insensitive'>" . __("Use one access key for both linked instances.");
122
123 print "</div>";
124
125 print "<div class=\"dlgButtons\">
126 <div style='float : left'>
127 <button dojoType=\"dijit.form.Button\"
128 onclick=\"return dijit.byId('instanceEditDlg').regenKey()\">".
129 __('Generate new key')."</button>
130 </div>
131 <button dojoType=\"dijit.form.Button\"
132 onclick=\"return dijit.byId('instanceEditDlg').execute()\">".
133 __('Save')."</button>
134 <button dojoType=\"dijit.form.Button\"
135 onclick=\"return dijit.byId('instanceEditDlg').hide()\"\">".
136 __('Cancel')."</button></div>";
137
138 }
139
140 function editSave() {
141 $id = db_escape_string($_REQUEST["id"]);
142 $access_url = db_escape_string($_REQUEST["access_url"]);
143 $access_key = db_escape_string($_REQUEST["access_key"]);
144
145 db_query($this->link, "UPDATE ttrss_linked_instances SET
146 access_key = '$access_key', access_url = '$access_url',
147 last_connected = '1970-01-01'
148 WHERE id = '$id'");
149
150 }
151
152 function index() {
153
154 if (!function_exists('curl_init')) {
155 print "<div style='padding : 1em'>";
156 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.");
157 print "</div>";
158 }
159
160 print "<div id=\"pref-instance-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
161 print "<div id=\"pref-instance-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
162
163 print "<div id=\"pref-instance-toolbar\" dojoType=\"dijit.Toolbar\">";
164
165 $sort = db_escape_string($_REQUEST["sort"]);
166
167 if (!$sort || $sort == "undefined") {
168 $sort = "access_url";
169 }
170
171 print "<div dojoType=\"dijit.form.DropDownButton\">".
172 "<span>" . __('Select')."</span>";
173 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
174 print "<div onclick=\"selectTableRows('prefInstanceList', 'all')\"
175 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
176 print "<div onclick=\"selectTableRows('prefInstanceList', 'none')\"
177 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
178 print "</div></div>";
179
180 print "<button dojoType=\"dijit.form.Button\" onclick=\"addInstance()\">".__('Link instance')."</button>";
181 print "<button dojoType=\"dijit.form.Button\" onclick=\"editSelectedInstance()\">".__('Edit')."</button>";
182 print "<button dojoType=\"dijit.form.Button\" onclick=\"removeSelectedInstances()\">".__('Remove')."</button>";
183
184 print "</div>"; #toolbar
185
186 $result = db_query($this->link, "SELECT *,
187 (SELECT COUNT(*) FROM ttrss_linked_feeds
188 WHERE instance_id = ttrss_linked_instances.id) AS num_feeds
189 FROM ttrss_linked_instances
190 ORDER BY $sort");
191
192 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:");
193
194 print " <a href=\"#\" onclick=\"alert('".htmlspecialchars(get_self_url_prefix())."')\">(display url)</a>";
195
196 print "<p><table width='100%' id='prefInstanceList' class='prefInstanceList' cellspacing='0'>";
197
198 print "<tr class=\"title\">
199 <td align='center' width=\"5%\">&nbsp;</td>
200 <td width=''><a href=\"#\" onclick=\"updateInstanceList('access_url')\">".__('Instance URL')."</a></td>
201 <td width='20%'><a href=\"#\" onclick=\"updateInstanceList('access_key')\">".__('Access key')."</a></td>
202 <td width='10%'><a href=\"#\" onclick=\"updateUsersList('last_connected')\">".__('Last connected')."</a></td>
32532f1c 203 <td width='10%'><a href=\"#\" onclick=\"updateUsersList('last_status_out')\">".__('Status')."</a></td>
5f0a3741
AD
204 <td width='10%'><a href=\"#\" onclick=\"updateUsersList('num_feeds')\">".__('Stored feeds')."</a></td>
205 </tr>";
206
207 $lnum = 0;
208
209 while ($line = db_fetch_assoc($result)) {
210 $class = ($lnum % 2) ? "even" : "odd";
211
212 $id = $line['id'];
213 $this_row_id = "id=\"LIRR-$id\"";
214
215 $line["last_connected"] = make_local_datetime($this->link, $line["last_connected"], false);
216
217 print "<tr class=\"$class\" $this_row_id>";
218
219 print "<td align='center'><input onclick='toggleSelectRow(this);'
220 type=\"checkbox\" id=\"LICHK-$id\"></td>";
221
222 $onclick = "onclick='editInstance($id, event)' title='".__('Click to edit')."'";
223
224 $access_key = mb_substr($line['access_key'], 0, 4) . '...' .
225 mb_substr($line['access_key'], -4);
226
227 print "<td $onclick>" . htmlspecialchars($line['access_url']) . "</td>";
228 print "<td $onclick>" . htmlspecialchars($access_key) . "</td>";
229 print "<td $onclick>" . htmlspecialchars($line['last_connected']) . "</td>";
32532f1c 230 print "<td $onclick>" . $this->status_codes[$line['last_status_out']] . "</td>";
5f0a3741
AD
231 print "<td $onclick>" . htmlspecialchars($line['num_feeds']) . "</td>";
232
233 print "</tr>";
234
235 ++$lnum;
236 }
237
238 print "</table>";
239
240 print "</div>"; #pane
6065f3ad
AD
241
242 global $pluginhost;
243 $pluginhost->run_hooks($pluginhost::HOOK_PREFS_TAB,
244 "hook_prefs_tab", "prefInstances");
245
5f0a3741
AD
246 print "</div>"; #container
247
248 }
6cbe53c9 249
5f0a3741
AD
250}
251?>
6cbe53c9 252