]> git.wh0rd.org - tt-rss.git/blob - backend.php
OPML import
[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\" id=\"feedsList\">";
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 $subop = $_GET["subop"];
45
46 $total = $line["total"];
47 $unread = $line["unread"];
48
49 $class = ($lnum % 2) ? "even" : "odd";
50
51 if ($unread > 0) $class .= "Unread";
52
53 $total_unread += $unread;
54
55 print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
56
57 $icon_file = ICONS_DIR . "/$feed_id.ico";
58
59 if ($subop != "piggie") {
60
61 if (file_exists($icon_file) && filesize($icon_file) > 0) {
62 $feed_icon = "<img width=\"16\" height=\"16\"
63 src=\"" . ICONS_URL . "/$feed_id.ico\">";
64 } else {
65 $feed_icon = "&nbsp;";
66 }
67 } else {
68 $feed_icon = "<img width=\"16\" height=\"16\"
69 src=\"http://madoka.spb.ru/stuff/fox/tiny_piggie.png\">";
70 }
71
72 $feed = "<a href=\"javascript:viewfeed($feed_id, 0);\">$feed</a>";
73 if (ENABLE_FEED_ICONS) {
74 print "<td>$feed_icon</td>";
75 }
76 print "<td id=\"FEEDN-$feed_id\">$feed</td>";
77 print "<td>";
78 print "<span id=\"FEEDU-$feed_id\">$unread</span>&nbsp;/&nbsp;";
79 print "<span id=\"FEEDT-$feed_id\">$total</span>";
80 print "</td>";
81
82 print "</tr>";
83 ++$lnum;
84 }
85
86 // print "<tr><td class=\"footer\" colspan=\"3\">
87 // <a href=\"javascript:update_feed_list(false,true)\">Update all feeds</a></td></tr>";
88
89 // print "<tr><td class=\"footer\" colspan=\"2\">&nbsp;";
90 // print "</td></tr>";
91
92 print "</table>";
93
94 print "<div class=\"invisible\" id=\"FEEDTU\">$total_unread</div>";
95
96 /*
97 print "<p align=\"center\">All feeds:
98 <a class=\"button\"
99 href=\"javascript:scheduleFeedUpdate(true)\">Update</a>";
100
101 print "&nbsp;<a class=\"button\"
102 href=\"javascript:catchupAllFeeds()\">Mark as read</a></p>";
103
104 print "<div class=\"invisible\" id=\"FEEDTU\">$total_unread</div>";
105 */
106
107
108 }
109
110
111 if ($op == "rpc") {
112
113 $subop = $_GET["subop"];
114
115 if ($subop == "updateFeed") {
116 $feed_id = $_GET["feed"];
117
118 $result = pg_query($link,
119 "SELECT feed_url FROM ttrss_feeds WHERE id = '$feed_id'");
120
121 if (pg_num_rows($result) > 0) {
122 $feed_url = pg_fetch_result($result, 0, "feed_url");
123 // update_rss_feed($link, $feed_url, $feed_id);
124 }
125
126 print "DONE-$feed_id";
127
128 return;
129 }
130
131 if ($subop == "forceUpdateAllFeeds") {
132 update_all_feeds($link, true);
133 outputFeedList($link);
134 }
135
136 if ($subop == "updateAllFeeds") {
137 update_all_feeds($link, false);
138 outputFeedList($link);
139 }
140
141 if ($subop == "catchupPage") {
142
143 $ids = split(",", $_GET["ids"]);
144
145 foreach ($ids as $id) {
146
147 pg_query("UPDATE ttrss_entries SET unread=false,last_read = NOW()
148 WHERE id = '$id'");
149
150 }
151
152 print "Marked active page as read.";
153 }
154
155 }
156
157 if ($op == "feeds") {
158
159 $subop = $_GET["subop"];
160
161 if ($subop == "catchupAll") {
162 pg_query("UPDATE ttrss_entries SET last_read = NOW(),unread = false");
163 }
164
165 outputFeedList($link);
166
167 }
168
169 if ($op == "view") {
170
171 $id = $_GET["id"];
172
173 $result = pg_query("UPDATE ttrss_entries SET unread = false,last_read = NOW() WHERE id = '$id'");
174
175 $result = pg_query("SELECT title,link,content,feed_id,comments,
176 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url
177 FROM ttrss_entries
178 WHERE id = '$id'");
179
180 if ($result) {
181
182 $line = pg_fetch_assoc($result);
183
184 if ($line["icon_url"]) {
185 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
186 } else {
187 $feed_icon = "&nbsp;";
188 }
189
190 print "<table class=\"postTable\" width=\"100%\" cellspacing=\"0\"
191 cellpadding=\"0\">";
192
193 print "<tr class=\"titleTop\"><td align=\"right\"><b>Title:</b></td>
194 <td width=\"100%\">".$line["title"]."</td>
195 <td>&nbsp;</td></tr>";
196
197 if ($line["comments"] && $line["comments"] != $line["link"]) {
198 // print "<tr class=\"titleInner\"><td align=\"right\"><b>Comments:</b></td>
199 // <td><a href=\"".$line["comments"]."\">".$line["comments"]."</a></td>
200 // <td>&nbsp;</td> </tr>";
201
202 $comments_prompt = "(<a href=\"".$line["comments"]."\">Comments</a>)";
203 }
204
205 print "<tr class=\"titleBottom\"><td align=\"right\"><b>Link:</b></td>
206 <td><a href=\"".$line["link"]."\">".$line["link"]."</a> $comments_prompt</td>
207 <td>&nbsp;</td></tr>";
208 print "<tr><td valign=\"top\" class=\"post\"
209 colspan=\"2\">" . $line["content"] . "</td>
210 <td valign=\"top\">$feed_icon</td>
211 </tr>";
212 print "</table>";
213
214 }
215 }
216
217 if ($op == "viewfeed") {
218
219 $feed = $_GET["feed"];
220 $skip = $_GET["skip"];
221 $subop = $_GET["subop"];
222
223 if (!$skip) $skip = 0;
224
225 if ($subop == "undefined") $subop = "";
226
227 // FIXME: check for null value here
228
229 $result = pg_query("SELECT *,SUBSTRING(last_updated,1,16) as last_updated,
230 EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM last_updated) as update_timeout
231 FROM ttrss_feeds WHERE id = '$feed'");
232
233 if ($result) {
234
235 $line = pg_fetch_assoc($result);
236
237 if ($subop == "ForceUpdate" ||
238 (!$subop && $line["update_timeout"] > MIN_UPDATE_TIME)) {
239
240 update_rss_feed($link, $line["feed_url"], $feed);
241
242 } else {
243
244 if ($subop == "MarkAllRead") {
245
246 pg_query("UPDATE ttrss_entries SET unread = false,last_read = NOW()
247 WHERE feed_id = '$feed'");
248 }
249 }
250 }
251
252 print "<table class=\"headlinesList\" id=\"headlinesList\" width=\"100%\">";
253
254 $feed_last_updated = "Updated: " . $line["last_updated"];
255
256 print "<tr><td class=\"search\" colspan=\"3\">
257 Search: <input id=\"searchbox\"
258 onblur=\"javascript:enableHotkeys()\" onfocus=\"javascript:disableHotkeys()\"
259 onchange=\"javascript:search($feed);\">
260 <a class=\"button\" href=\"javascript:resetSearch()\">Reset</a>
261 </td></tr>";
262 print "<tr>
263 <td colspan=\"3\" class=\"title\">" . $line["title"] . "</td></tr>";
264
265 $search = $_GET["search"];
266
267 if (search) {
268 $search_query_part = "(upper(title) LIKE upper('%$search%')
269 OR content LIKE '%$search%') AND";
270 }
271
272 $result = pg_query("SELECT count(id) AS total_entries
273 FROM ttrss_entries WHERE
274 $search_query_part
275 feed_id = '$feed'");
276
277 $total_entries = pg_fetch_result($result, 0, "total_entries");
278
279 $result = pg_query("SELECT
280 id,title,updated,unread,feed_id,
281 EXTRACT(EPOCH FROM last_read) AS last_read_ts,
282 EXTRACT(EPOCH FROM updated) AS updated_ts
283 FROM
284 ttrss_entries
285 WHERE
286 $search_query_part
287 feed_id = '$feed' ORDER BY updated DESC LIMIT ".HEADLINES_PER_PAGE." OFFSET $skip");
288
289 $lnum = 0;
290
291 $num_unread = 0;
292
293 while ($line = pg_fetch_assoc($result)) {
294
295 $class = ($lnum % 2) ? "even" : "odd";
296
297 if ($line["last_read_ts"] < $line["updated_ts"] && $line["unread"] == "f") {
298 $update_pic = "<img src=\"images/updated.png\" alt=\"Updated\">";
299 ++$num_unread;
300 } else {
301 $update_pic = "&nbsp;";
302 }
303
304 if ($line["unread"] == "t") {
305 $class .= "Unread";
306 ++$num_unread;
307 }
308
309 $id = $line["id"];
310 $feed_id = $line["feed_id"];
311
312 $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
313 $line["title"] . "</a>";
314
315 print "<tr class='$class' id='RROW-$id'>";
316
317 print "<td id='FUPDPIC-$id' valign='center' class='headlineUpdateMark'>$update_pic</td>";
318
319 print "<td class='headlineUpdated'>
320 <a href=\"javascript:view($id,$feed_id);\">".$line["updated"]."</a></td>";
321 print "<td class='headlineTitle'>$content_link</td>";
322
323 print "</tr>";
324
325 ++$lnum;
326 }
327
328 if ($lnum == 0) {
329 print "<tr><td align='center'>No entries found.</td></tr>";
330 }
331
332 while ($lnum < HEADLINES_PER_PAGE) {
333 ++$lnum;
334 print "<tr><td>&nbsp;</td></tr>";
335 }
336
337 // start unholy navbar block
338
339 print "<tr><td colspan=\"3\" class=\"headlineToolbar\">";
340
341 $next_skip = $skip + HEADLINES_PER_PAGE;
342 $prev_skip = $skip - HEADLINES_PER_PAGE;
343
344 print "Navigate: ";
345
346 if ($prev_skip >= 0) {
347 print "<a class=\"button\"
348 href=\"javascript:viewfeed($feed, $prev_skip);\">Previous Page</a>";
349 } else {
350 print "<a class=\"disabledButton\">Previous Page</a>";
351 }
352 print "&nbsp;";
353
354 if ($next_skip < $total_entries) {
355 print "<a class=\"button\"
356 href=\"javascript:viewfeed($feed, $next_skip);\">Next Page</a>";
357 } else {
358 print "<a class=\"disabledButton\">Next Page</a>";
359 }
360 print "&nbsp;&nbsp;Feed: ";
361
362 print "<a class=\"button\"
363 href=\"javascript:viewfeed($feed, 0, 'ForceUpdate');\">Update</a>";
364
365 print "&nbsp;&nbsp;Mark as read: ";
366
367 if ($num_unread > 0) {
368 print "<a class=\"button\" id=\"btnCatchupPage\"
369 href=\"javascript:catchupPage($feed);\">This Page</a>";
370 print "&nbsp;";
371 } else {
372 print "<a class=\"disabledButton\">This Page</a>";
373 print "&nbsp;";
374 }
375
376 print "<a class=\"button\"
377 href=\"javascript:viewfeed($feed, $skip, 'MarkAllRead');\">All Posts</a>";
378
379 print "</td></tr>";
380
381 // end unholy navbar block
382
383 print "</table>";
384
385 $result = pg_query("SELECT id, (SELECT count(id) FROM ttrss_entries
386 WHERE feed_id = ttrss_feeds.id) AS total,
387 (SELECT count(id) FROM ttrss_entries
388 WHERE feed_id = ttrss_feeds.id AND unread = true) as unread
389 FROM ttrss_feeds WHERE id = '$feed'");
390
391 $total = pg_fetch_result($result, 0, "total");
392 $unread = pg_fetch_result($result, 0, "unread");
393
394 print "<div class=\"invisible\" id=\"FACTIVE\">$feed</div>";
395 print "<div class=\"invisible\" id=\"FTOTAL\">$total</div>";
396 print "<div class=\"invisible\" id=\"FUNREAD\">$unread</div>";
397
398 }
399
400 if ($op == "pref-rpc") {
401
402 $subop = $_GET["subop"];
403
404 if ($subop == "unread") {
405 $ids = split(",", $_GET["ids"]);
406 foreach ($ids as $id) {
407 pg_query("UPDATE ttrss_entries SET unread = true WHERE feed_id = '$id'");
408 }
409
410 print "Marked selected feeds as read.";
411 }
412
413 if ($subop == "read") {
414 $ids = split(",", $_GET["ids"]);
415 foreach ($ids as $id) {
416 pg_query("UPDATE ttrss_entries
417 SET unread = false,last_read = NOW() WHERE feed_id = '$id'");
418 }
419
420 print "Marked selected feeds as unread.";
421
422 }
423
424 }
425
426 if ($op == "pref-feeds") {
427
428 $subop = $_GET["subop"];
429
430 if ($subop == "editSave") {
431 $feed_title = pg_escape_string($_GET["t"]);
432 $feed_link = pg_escape_string($_GET["l"]);
433 $feed_id = $_GET["id"];
434
435 $result = pg_query("UPDATE ttrss_feeds SET
436 title = '$feed_title', feed_url = '$feed_link' WHERE id = '$feed_id'");
437
438 }
439
440 if ($subop == "remove") {
441
442 if (!WEB_DEMO_MODE) {
443
444 $ids = split(",", $_GET["ids"]);
445
446 foreach ($ids as $id) {
447 pg_query("BEGIN");
448 pg_query("DELETE FROM ttrss_entries WHERE feed_id = '$id'");
449 pg_query("DELETE FROM ttrss_feeds WHERE id = '$id'");
450 pg_query("COMMIT");
451
452 if (file_exists(ICONS_DIR . "/$id.ico")) {
453 unlink(ICONS_DIR . "/$id.ico");
454 }
455 }
456 }
457 }
458
459 if ($subop == "add") {
460
461 if (!WEB_DEMO_MODE) {
462
463 $feed_link = pg_escape_string($_GET["link"]);
464
465 $result = pg_query(
466 "INSERT INTO ttrss_feeds (feed_url,title) VALUES ('$feed_link', '')");
467
468 $result = pg_query(
469 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'");
470
471 $feed_id = pg_fetch_result($result, 0, "id");
472
473 if ($feed_id) {
474 update_rss_feed($link, $feed_link, $feed_id);
475 }
476 }
477 }
478
479 $result = pg_query("SELECT
480 id,title,feed_url,substring(last_updated,1,16) as last_updated
481 FROM
482 ttrss_feeds ORDER by title");
483
484 print "<p><table width=\"100%\" class=\"prefFeedList\" id=\"prefFeedList\">";
485 print "<tr class=\"title\">
486 <td>&nbsp;</td><td>Select</td><td width=\"40%\">Title</td>
487 <td width=\"40%\">Link</td><td>Last Updated</td></tr>";
488
489 $lnum = 0;
490
491 while ($line = pg_fetch_assoc($result)) {
492
493 $class = ($lnum % 2) ? "even" : "odd";
494
495 $feed_id = $line["id"];
496
497 $edit_feed_id = $_GET["id"];
498
499 if ($subop == "edit" && $feed_id != $edit_feed_id) {
500 $class .= "Grayed";
501 }
502
503 print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
504
505 $icon_file = ICONS_DIR . "/$feed_id.ico";
506
507 if (file_exists($icon_file) && filesize($icon_file) > 0) {
508 $feed_icon = "<img width=\"16\" height=\"16\"
509 src=\"" . ICONS_URL . "/$feed_id.ico\">";
510 } else {
511 $feed_icon = "&nbsp;";
512 }
513 print "<td align='center'>$feed_icon</td>";
514
515 if (!$edit_feed_id || $subop != "edit") {
516
517 print "<td><input onclick='toggleSelectRow(this);'
518 type=\"checkbox\" id=\"FRCHK-".$line["id"]."\"></td>";
519
520 print "<td><a href=\"javascript:editFeed($feed_id);\">" .
521 $line["title"] . "</td>";
522 print "<td><a href=\"javascript:editFeed($feed_id);\">" .
523 $line["feed_url"] . "</td>";
524
525
526 } else if ($feed_id != $edit_feed_id) {
527
528 print "<td><input disabled=\"true\" type=\"checkbox\"
529 id=\"FRCHK-".$line["id"]."\"></td>";
530
531 print "<td>".$line["title"]."</td>";
532 print "<td>".$line["feed_url"]."</td>";
533
534 } else {
535
536 print "<td><input disabled=\"true\" type=\"checkbox\"></td>";
537
538 print "<td><input id=\"iedit_title\" value=\"".$line["title"]."\"></td>";
539 print "<td><input id=\"iedit_link\" value=\"".$line["feed_url"]."\"></td>";
540
541 }
542
543 print "<td>" . $line["last_updated"] . "</td>";
544
545 print "</tr>";
546
547 ++$lnum;
548 }
549
550 print "</table>";
551
552 print "<p>";
553
554 if ($subop == "edit") {
555 print "Edit feed:&nbsp;
556 <a class=\"button\" href=\"javascript:feedEditCancel()\">Cancel</a>&nbsp;
557 <a class=\"button\" href=\"javascript:feedEditSave()\">Save</a>";
558 } else {
559
560 print "
561 Selection:&nbsp;
562 <a class=\"button\"
563 href=\"javascript:editSelectedFeed()\">Edit</a>&nbsp;
564 <a class=\"buttonWarn\"
565 href=\"javascript:removeSelectedFeeds()\">Remove</a>&nbsp;
566 <a class=\"button\"
567 href=\"javascript:readSelectedFeeds()\">Mark as read</a>&nbsp;
568 <a class=\"button\"
569 href=\"javascript:unreadSelectedFeeds()\">Mark as unread</a>&nbsp;
570 Page:
571 <a class=\"button\"
572 href=\"javascript:updateFeedList()\">Refresh</a>&nbsp;
573 OPML:
574 <a class=\"button\" href=\"opml.php?op=Export\">Export</a>
575 ";
576
577 }
578
579 }
580
581 pg_close($link);
582 ?>