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