]> git.wh0rd.org - tt-rss.git/blob - backend.php
feed editor, xmlhttp locking in preferences, try to initialize xmlhttp_rpc via Active...
[tt-rss.git] / backend.php
1 <?
2 // header("Content-Type: application/xml");
3
4 require_once "config.php";
5 require_once "functions.php";
6 require_once "magpierss/rss_fetch.inc";
7
8 error_reporting(0);
9
10 $link = pg_connect(DB_CONN);
11
12 error_reporting (E_ERROR | E_WARNING | E_PARSE);
13
14 if (!$link) {
15 print "Could not connect to database. Please check local configuration.";
16 return;
17 }
18
19 pg_query("set client_encoding = 'utf-8'");
20
21 $op = $_GET["op"];
22 $fetch = $_GET["fetch"];
23
24 function outputFeedList($link) {
25
26 $result = pg_query($link, "SELECT *,
27 (SELECT count(id) FROM ttrss_entries
28 WHERE feed_id = ttrss_feeds.id) AS total,
29 (SELECT count(id) FROM ttrss_entries
30 WHERE feed_id = ttrss_feeds.id AND unread = true) as unread
31 FROM ttrss_feeds ORDER BY title");
32
33 print "<table width=\"100%\" class=\"feeds\">";
34
35 $lnum = 0;
36
37 $total_unread = 0;
38
39 while ($line = pg_fetch_assoc($result)) {
40
41 $feed = $line["title"];
42 $feed_id = $line["id"];
43
44 $total = $line["total"];
45 $unread = $line["unread"];
46
47 $class = ($lnum % 2) ? "even" : "odd";
48
49 if ($unread > 0) $class .= "Unread";
50
51 $total_unread += $unread;
52
53 print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
54
55 $feed = "<a href=\"javascript:viewfeed($feed_id, 0);\">$feed</a>";
56 print "<td id=\"FEEDN-$feed_id\">$feed</td>";
57 print "<td>";
58 print "<span id=\"FEEDU-$feed_id\">$unread</span>&nbsp;/&nbsp;";
59 print "<span id=\"FEEDT-$feed_id\">$total</span>";
60 print "</td>";
61
62 print "</tr>";
63 ++$lnum;
64 }
65
66 // print "<tr><td class=\"footer\" colspan=\"3\">
67 // <a href=\"javascript:update_feed_list(false,true)\">Update all feeds</a></td></tr>";
68
69 // print "<tr><td class=\"footer\" colspan=\"2\">&nbsp;";
70 // print "</td></tr>";
71
72 print "</table>";
73
74 print "<p align=\"center\">All feeds:
75 <a class=\"button\"
76 href=\"javascript:scheduleFeedUpdate(true)\">Update</a>";
77
78 print "&nbsp;<a class=\"button\"
79 href=\"javascript:catchupAllFeeds()\">Mark as read</a></p>";
80
81 print "<div class=\"invisible\" id=\"FEEDTU\">$total_unread</div>";
82
83
84
85 }
86
87
88 if ($op == "rpc") {
89
90 $subop = $_GET["subop"];
91
92 if ($subop == "forceUpdateAllFeeds") {
93 update_all_feeds($link, true);
94 outputFeedList($link);
95 }
96
97 if ($subop == "updateAllFeeds") {
98 update_all_feeds($link, false);
99 outputFeedList($link);
100 }
101
102 if ($subop == "catchupPage") {
103
104 $ids = split(",", $_GET["ids"]);
105
106 foreach ($ids as $id) {
107
108 pg_query("UPDATE ttrss_entries SET unread=false,last_read = NOW()
109 WHERE id = '$id'");
110
111 }
112
113 print "Marked active page as read.";
114 }
115
116 }
117
118 if ($op == "feeds") {
119
120 $subop = $_GET["subop"];
121
122 if ($subop == "catchupAll") {
123 pg_query("UPDATE ttrss_entries SET last_read = NOW(),unread = false");
124 }
125
126 outputFeedList($link);
127
128 }
129
130 if ($op == "view") {
131
132 $id = $_GET["id"];
133
134 $result = pg_query("UPDATE ttrss_entries SET unread = false,last_read = NOW() WHERE id = '$id'");
135
136 $result = pg_query("SELECT title,link,content FROM ttrss_entries
137 WHERE id = '$id'");
138
139 if ($result) {
140
141 $line = pg_fetch_assoc($result);
142
143 /* print "<table class=\"feedOverview\">";
144 print "<tr><td><b>Title:</b></td><td>".$line["title"]."</td></tr>";
145 print "<tr><td><b>Link:</b></td><td><a href=\"".$line["link"]."\">".$line["link"]."</a></td></tr>";
146 print "</table>";
147
148 print $line["content"]; */
149
150
151 print "<table class=\"postTable\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">";
152 print "<tr class=\"titleTop\"><td align=\"right\"><b>Title:</b></td>
153 <td width=\"100%\">".$line["title"]."</td></tr>";
154
155 print "<tr class=\"titleBottom\"><td align=\"right\"><b>Link:</b></td>
156 <td><a href=\"".$line["link"]."\">".$line["link"]."</a></td></tr>";
157
158 print "<tr><td class=\"post\" colspan=\"2\">" . $line["content"] . "</td></tr>";
159 print "</table>";
160 }
161 }
162
163 if ($op == "viewfeed") {
164
165 $feed = $_GET["feed"];
166 $skip = $_GET["skip"];
167 $subop = $_GET["subop"];
168
169 if (!$skip) $skip = 0;
170
171 if ($subop == "undefined") $subop = "";
172
173 // FIXME: check for null value here
174
175 $result = pg_query("SELECT *,SUBSTRING(last_updated,1,16) as last_updated,
176 EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM last_updated) as update_timeout
177 FROM ttrss_feeds WHERE id = '$feed'");
178
179 if ($result) {
180
181 $line = pg_fetch_assoc($result);
182
183 if ($subop == "ForceUpdate" ||
184 (!$subop && $line["update_timeout"] > MIN_UPDATE_TIME)) {
185
186 update_rss_feed($link, $line["feed_url"], $feed);
187
188 } else {
189
190 if ($subop == "MarkAllRead") {
191
192 pg_query("UPDATE ttrss_entries SET unread = false,last_read = NOW()
193 WHERE feed_id = '$feed'");
194 }
195 }
196 }
197
198 print "<table class=\"headlinesList\" id=\"headlinesList\" width=\"100%\">";
199
200 $feed_last_updated = "Updated: " . $line["last_updated"];
201
202 print "<tr><td class=\"search\" colspan=\"3\">
203 Search: <input onchange=\"javascript:search($feed,this);\"></td></tr>";
204 print "<tr>
205 <td colspan=\"3\" class=\"title\">" . $line["title"] . "</td></tr>";
206
207 if ($ext == "SEARCH") {
208 $search = $_GET["search"];
209 $search_query_part = "(upper(title) LIKE upper('%$search%')
210 OR content LIKE '%$search%') AND";
211 }
212
213 $result = pg_query("SELECT count(id) AS total_entries
214 FROM ttrss_entries WHERE feed_id = '$feed'");
215
216 $total_entries = pg_fetch_result($result, 0, "total_entries");
217
218 $result = pg_query("SELECT
219 id,title,updated,unread,feed_id,
220 EXTRACT(EPOCH FROM last_read) AS last_read_ts,
221 EXTRACT(EPOCH FROM updated) AS updated_ts
222 FROM
223 ttrss_entries
224 WHERE
225 $search_query_part
226 feed_id = '$feed' ORDER BY updated DESC LIMIT ".HEADLINES_PER_PAGE." OFFSET $skip");
227
228 $lnum = 0;
229
230 while ($line = pg_fetch_assoc($result)) {
231
232 $class = ($lnum % 2) ? "even" : "odd";
233
234 if ($line["last_read_ts"] < $line["updated_ts"] && $line["unread"] == "f") {
235 $update_pic = "<img src=\"updated.png\" alt=\"Updated\">";
236 } else {
237 $update_pic = "&nbsp;";
238 }
239
240 if ($line["unread"] == "t")
241 $class .= "Unread";
242
243 $id = $line["id"];
244 $feed_id = $line["feed_id"];
245
246 $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
247 $line["title"] . "</a>";
248
249 print "<tr class='$class' id='RROW-$id'>";
250
251 print "<td id='FUPDPIC-$id' valign='center' class='headlineUpdateMark'>$update_pic</td>";
252
253 print "<td class='headlineUpdated'>".$line["updated"]."</td>";
254 print "<td class='headlineTitle'>$content_link</td>";
255
256 print "</tr>";
257
258 ++$lnum;
259 }
260
261 if ($lnum == 0) {
262 print "<tr><td align='center'>No entries found.</td></tr>";
263 }
264
265 while ($lnum < HEADLINES_PER_PAGE) {
266 ++$lnum;
267 print "<tr><td>&nbsp;</td></tr>";
268 }
269
270 // start unholy navbar block
271
272 print "<tr><td colspan=\"3\" class=\"headlineToolbar\">";
273
274 $next_skip = $skip + HEADLINES_PER_PAGE;
275 $prev_skip = $skip - HEADLINES_PER_PAGE;
276
277 print "Navigate: ";
278
279 if ($prev_skip >= 0) {
280 print "<a class=\"button\"
281 href=\"javascript:viewfeed($feed, $prev_skip);\">Previous Page</a>";
282 } else {
283 print "<a class=\"disabledButton\">Previous Page</a>";
284 }
285 print "&nbsp;";
286
287 if ($next_skip < $total_entries) {
288 print "<a class=\"button\"
289 href=\"javascript:viewfeed($feed, $next_skip);\">Next Page</a>";
290 } else {
291 print "<a class=\"disabledButton\">Next Page</a>";
292 }
293 print "&nbsp;&nbsp;Feed: ";
294
295 print "<a class=\"button\"
296 href=\"javascript:viewfeed($feed, 0, 'ForceUpdate');\">Update</a>";
297
298 print "&nbsp;&nbsp;Mark as read: ";
299
300 print "<a class=\"button\"
301 href=\"javascript:catchupPage($feed);\">This Page</a>";
302 print "&nbsp;";
303 print "<a class=\"button\"
304 href=\"javascript:viewfeed($feed, $skip, 'MarkAllRead');\">All Posts</a>";
305
306 print "</td></tr>";
307
308 // end unholy navbar block
309
310
311 print "</table>";
312
313 $result = pg_query("SELECT id, (SELECT count(id) FROM ttrss_entries
314 WHERE feed_id = ttrss_feeds.id) AS total,
315 (SELECT count(id) FROM ttrss_entries
316 WHERE feed_id = ttrss_feeds.id AND unread = true) as unread
317 FROM ttrss_feeds WHERE id = '$feed'");
318
319 $total = pg_fetch_result($result, 0, "total");
320 $unread = pg_fetch_result($result, 0, "unread");
321
322 print "<div class=\"invisible\" id=\"FACTIVE\">$feed</div>";
323 print "<div class=\"invisible\" id=\"FTOTAL\">$total</div>";
324 print "<div class=\"invisible\" id=\"FUNREAD\">$unread</div>";
325
326 }
327
328 if ($op == "pref-rpc") {
329
330 $subop = $_GET["subop"];
331
332 if ($subop == "unread") {
333 $ids = split(",", $_GET["ids"]);
334 foreach ($ids as $id) {
335 pg_query("UPDATE ttrss_entries SET unread = true WHERE feed_id = '$id'");
336 }
337
338 print "Marked selected feeds as read.";
339 }
340
341 if ($subop == "read") {
342 $ids = split(",", $_GET["ids"]);
343 foreach ($ids as $id) {
344 pg_query("UPDATE ttrss_entries
345 SET unread = false,last_read = NOW() WHERE feed_id = '$id'");
346 }
347
348 print "Marked selected feeds as unread.";
349
350 }
351
352 }
353
354 if ($op == "pref-feeds") {
355
356 $subop = $_GET["subop"];
357
358 if ($subop == "edit") {
359
360 $feed_id = $_GET["id"];
361
362 $result = pg_query("SELECT title,feed_url
363 FROM ttrss_feeds WHERE id = '$feed_id'");
364
365 $fedit_link = pg_fetch_result($result, 0, "feed_url");
366 $fedit_title = pg_fetch_result($result, 0, "title");
367
368 print "<table class=\"prefAddFeed\">
369 <td>Title:</td><td><input id=\"fedit_title\" value=\"$fedit_title\"></td></tr>
370 <td>Link:</td><td><input id=\"fedit_link\" value=\"$fedit_link\"></td></tr>
371 <tr><td colspan=\"2\" align=\"right\">
372 <a class=\"button\" href=\"javascript:feedEditCancel()\">Cancel</a>
373 <a class=\"button\" href=\"javascript:feedEditSave($feed_id)\">Save</a>
374 </td></tr>
375 </table>";
376
377 } else {
378
379 print "<table class=\"prefAddFeed\">
380 <td><input id=\"fadd_link\"></td>
381 <td colspan=\"4\" align=\"right\">
382 <a class=\"button\" href=\"javascript:addFeed()\">Add feed</a></td></tr>
383 </table>";
384
385 }
386
387 if ($subop == "editSave") {
388 $feed_title = pg_escape_string($_GET["t"]);
389 $feed_link = pg_escape_string($_GET["l"]);
390 $feed_id = $_GET["id"];
391
392 $result = pg_query("UPDATE ttrss_feeds SET
393 title = '$feed_title', feed_url = '$feed_link' WHERE id = '$feed_id'");
394
395 }
396
397 if ($subop == "remove") {
398
399 if (!WEB_DEMO_MODE) {
400
401 $ids = split(",", $_GET["ids"]);
402
403 foreach ($ids as $id) {
404 pg_query("BEGIN");
405 pg_query("DELETE FROM ttrss_entries WHERE feed_id = '$id'");
406 pg_query("DELETE FROM ttrss_feeds WHERE id = '$id'");
407 pg_query("COMMIT");
408 }
409 }
410 }
411
412 if ($subop == "add") {
413
414 if (!WEB_DEMO_MODE) {
415
416 $feed_link = pg_escape_string($_GET["link"]);
417
418 $result = pg_query(
419 "INSERT INTO ttrss_feeds (feed_url,title) VALUES ('$feed_link', '')");
420
421 $result = pg_query("SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'");
422
423 $feed_id = pg_fetch_result($result, 0, "id");
424
425 if ($feed_id) {
426 update_rss_feed($link, $feed_link, $feed_id);
427 }
428 }
429 }
430
431 $result = pg_query("SELECT * FROM ttrss_feeds ORDER by title");
432
433 print "<p><table width=\"100%\" class=\"prefFeedList\" id=\"prefFeedList\">";
434 print "<tr class=\"title\">
435 <td>Select</td><td>Title</td><td>Link</td><td>Last Updated</td></tr>";
436
437 $lnum = 0;
438
439 while ($line = pg_fetch_assoc($result)) {
440
441 $class = ($lnum % 2) ? "even" : "odd";
442
443 $feed_id = $line["id"];
444
445 print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
446
447 print "<td><input onclick='toggleSelectRow(this);'
448 type=\"checkbox\" id=\"FRCHK-".$line["id"]."\"></td>";
449 print "<td><a href=\"javascript:editFeed($feed_id);\">" .
450 $line["title"] . "</td>";
451 print "<td><a href=\"javascript:editFeed($feed_id);\">" .
452 $line["feed_url"] . "</td>";
453
454 print "<td>" . $line["last_updated"] . "</td>";
455 print "</tr>";
456
457 ++$lnum;
458 }
459
460 print "</table>";
461
462 }
463
464 pg_close($link);
465 ?>